Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
fsmbase.c
Go to the documentation of this file.
1 void fsmbDebugPrint (string s)
2 {
3 #ifdef FSM_DEBUG
4  PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
5 #else
6  //Print("" + s); // comment/uncomment to hide/see debug logs
7 #endif
8 }
9 void fsmbDebugSpam (string s)
10 {
11 #ifdef FSM_DEBUG_SPAM
12  PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
13 #else
14  //Print("" + s); // comment/uncomment to hide/see debug logs
15 #endif
16 }
17 
18 
22 class FSMTransition<Class FSMStateBase, Class FSMEventBase, Class FSMActionBase, Class FSMGuardBase>
23 {
24  ref FSMStateBase m_srcState;
25  ref FSMEventBase m_event; // @NOTE: NULL event means "completion transition" in UML speak
26  ref FSMStateBase m_dstState; // @NOTE: NULL dst state == UML terminate pseudonode
27  ref FSMActionBase m_action;
28  ref FSMGuardBase m_guard;
29 
30  void FSMTransition (FSMStateBase src, FSMEventBase e, FSMStateBase dst, FSMActionBase a = NULL, FSMGuardBase g = NULL)
31  {
32  m_srcState = src;
33  m_event = e;
34  m_dstState = dst;
35  m_action = a;
36  m_guard = g;
37  }
38 };
39 
41 {
46 };
47 
54 class FSMBase<Class FSMStateBase, Class FSMEventBase, Class FSMActionBase, Class FSMGuardBase>
55 {
56  protected ref FSMStateBase m_state;
57  protected ref FSMStateBase m_initialState;
58  protected ref FSMEventBase m_initialEvent;
60 
61  void FSMBase ()
62  {
64  }
65 
70  FSMStateBase GetCurrentState ()
71  {
72  return m_state;
73  }
74 
78  void SetInitialState (FSMStateBase initial_state)
79  {
80  m_initialState = initial_state;
81  }
82 
87  void Start (FSMEventBase initial_event = NULL)
88  {
89  if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[fsm] " + this.ToString() + "::Start(" + initial_event.ToString() + "), init_state=" + m_initialState.ToString());
90 
91  m_state = m_initialState;
92  m_state.OnEntry(initial_event);
93  }
94 
98  bool IsRunning () { return m_state != NULL; }
99 
103  void Terminate (FSMEventBase terminal_event = NULL)
104  {
105  if (IsRunning())
106  {
107  m_state.OnExit(terminal_event);
108  m_state = NULL;
109  }
110  }
111 
115  void Update (float dt)
116  {
117  if (IsRunning())
118  m_state.OnUpdate(dt);
119  }
120 
125  {
126  m_transitions.Insert(t);
127  }
128 
134  ProcessEventResult ProcessEvent (FSMEventBase e)
135  {
136  FSMStateBase curr_state = m_state;
137 
138  int count = m_transitions.Count();
139  for (int i = 0; i < count; ++i)
140  {
142  if (row.m_srcState.Type() == curr_state.Type() && row.m_event.Type() == e.Type())
143  {
145  bool hasGuard = t.m_guard != NULL;
146  if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e))) // 1) exec guard (if any)
147  {
148  ProcessLocalTransition(t, e); // 2) process transition allowed by guard
149  }
150  }
151  }
152  return ProcessEventResult.FSM_NO_TRANSITION;
153  }
154 
161  protected ProcessEventResult ProcessLocalTransition (FSMTransition<FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase> t, FSMEventBase e)
162  {
163  if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[fsm] (local) state=" + t.m_srcState.ToString() + "-------- event=" + e.ToString() + "[G=" + t.m_guard.ToString() +"]/A=" + t.m_action.ToString() + " --------|> dst=" + t.m_dstState.ToString());
164 
165  m_state.OnExit(e); // 1) call onExit on old state
166 
167  if (t.m_action)
168  t.m_action.Action(e); // 2) execute transition action (if any)
169 
170  m_state = t.m_dstState; // 3) change state to new
171 
172  if (t.m_dstState != NULL)
173  {
174  m_state.OnEntry(e); // 4a) call onEntry on new state
175  return ProcessEventResult.FSM_OK;
176  }
177  else
178  {
179  if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[fsm] terminating fsm: state=" + t.m_srcState.ToString() + " event=" + e.ToString());
180  return ProcessEventResult.FSM_TERMINATED; // 4b) or terminate
181  }
182  }
183 };
184 
FSMBase
base class for finite state machine
IsRunning
bool IsRunning()
Definition: tools.c:264
FSM_ABORTED
@ FSM_ABORTED
Definition: fsmbase.c:44
LogManager
Definition: debug.c:734
ProcessEvent
void ProcessEvent(EPresenceNotifierNoiseEventType pEventType)
processing of external one-time events (land, fire, etc.)
Definition: pluginpresencenotifier.c:200
m_action
class WeaponEndAction extends WeaponStartAction m_action
ToString
proto string ToString()
FSM_NO_TRANSITION
@ FSM_NO_TRANSITION
Definition: fsmbase.c:45
FSMTransition
FSMTransition
Definition: weapon_base.c:11
PrintToRPT
proto void PrintToRPT(void var)
Prints content of variable to RPT file (performance warning - each write means fflush!...
FSMTransition
represents transition src -— event[guard]/action -—|> dst
array
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
Definition: isboxcollidinggeometryproxyclasses.c:27
Update
proto native volatile void Update()
Definition: playersoundmanager.c:125
m_event
ref HandEventBase m_event
Definition: dayzplayerinventory.c:133
FSM_OK
@ FSM_OK
Definition: fsmbase.c:42
ProcessEventResult
ProcessEventResult
Definition: fsmbase.c:40
FSM_TERMINATED
@ FSM_TERMINATED
Definition: fsmbase.c:43
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
Start
void Start()
Plays all elements this effects consists of.
Definition: effect.c:153
fsmbDebugPrint
void fsmbDebugPrint(string s)
Definition: fsmbase.c:1
fsmbDebugSpam
void fsmbDebugSpam(string s)
Definition: fsmbase.c:9