Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
ofsmbase.c
Go to the documentation of this file.
1 
7 class OFSMBase<Class FSMStateBase, Class FSMEventBase, Class FSMActionBase, Class FSMGuardBase>
8 {
9  protected ref array<ref FSMStateBase> m_States;
10  protected ref array<ref FSMStateBase> m_InitialStates;
12 
13  void OFSMBase ()
14  {
15  m_States = new array<ref FSMStateBase>;
16  m_InitialStates = new array<ref FSMStateBase>;
18  }
19 
24  array<ref FSMStateBase> GetCurrentState ()
25  {
26  return m_States;
27  }
28 
32  void SetInitialStates (array<ref FSMStateBase> initial_states)
33  {
34  m_InitialStates = initial_states;
35 
36  for (int s = 0; s < initial_states.Count(); ++s)
37  m_States.Insert(initial_states[s]);
38  }
39 
44  void Start (array<ref FSMEventBase> initial_events = null)
45  {
46  if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[ofsm] " + this.ToString() + "::Start(" + initial_events.ToString() + "), init_state=" + m_InitialStates.ToString());
47 
48  for (int s = 0; s < m_States.Count(); ++s)
49  {
50  m_States[s] = m_InitialStates[s];
51 
52  if (initial_events)
53  m_States[s].OnEntry(initial_events[s]);
54  else
55  m_States[s].OnEntry(null);
56  }
57  }
58 
62  bool IsRunning ()
63  {
64  int sc = m_States.Count();
65  if (sc)
66  {
67  for (int s = 0; s < sc; ++s)
68  if (m_States[s] != null)
69  return true;
70  }
71  return false;
72  }
73 
77  void Terminate (array<ref FSMEventBase> terminal_events = null)
78  {
79  if (IsRunning())
80  {
81  for (int s = 0; s < m_States.Count(); ++s)
82  {
83  if (terminal_events)
84  m_States[s].OnExit(terminal_events[s]);
85  else
86  m_States[s].OnExit(null);
87 
88  m_States[s] = null;
89  }
90  }
91  }
92 
96  void Update (float dt)
97  {
98  if (IsRunning())
99  {
100  for (int s = 0; s < m_States.Count(); ++s)
101  {
102  m_States[s].OnUpdate(dt);
103  }
104  }
105  }
106 
111  {
112  m_Transitions.Insert(t);
113  }
114 
120  ProcessEventResult ProcessEvent (FSMEventBase e)
121  {
122  int count = m_Transitions.Count();
123  for (int i = 0; i < count; ++i)
124  {
126  if (row.m_event.Type() == e.Type())
127  {
128  for (int s = 0; s < m_States.Count(); ++s)
129  {
130  if (row.m_srcState.Type() == m_States[s].Type() && row.m_event.Type() == e.Type())
131  {
133  bool hasGuard = t.m_guard != NULL;
134  if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e))) // 1) exec guard (if any)
135  {
136  ProcessLocalTransition(s, t, e); // 2) process transition allowed by guard
137  }
138  }
139  }
140  }
141  }
142  return ProcessEventResult.FSM_NO_TRANSITION;
143  }
144 
151  protected ProcessEventResult ProcessLocalTransition (int s, FSMTransition<FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase> t, FSMEventBase e)
152  {
153  if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[ofsm] (local) state=" + t.m_srcState.ToString() + "-------- event=" + e.ToString() + "[G=" + t.m_guard.ToString() +"]/A=" + t.m_action.ToString() + " --------|> dst=" + t.m_dstState.ToString());
154 
155  m_States[s].OnExit(e); // 1) call onExit on old state
156 
157  if (t.m_action)
158  t.m_action.Action(e); // 2) execute transition action (if any)
159 
160  m_States[s] = t.m_dstState; // 3) change state to new
161 
162  if (t.m_dstState != NULL)
163  {
164  m_States[s].OnEntry(e); // 4a) call onEntry on new state
165  return ProcessEventResult.FSM_OK;
166  }
167  else
168  {
169  if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[ofsm] terminating fsm: state=" + t.m_srcState.ToString() + " event=" + e.ToString());
170  return ProcessEventResult.FSM_TERMINATED; // 4b) or terminate
171  }
172  }
173 };
174 
IsRunning
bool IsRunning()
Definition: tools.c:264
OFSMBase
base class for Orthogonal Finite State Machine
LogManager
Definition: debug.c:734
ProcessEvent
void ProcessEvent(EPresenceNotifierNoiseEventType pEventType)
processing of external one-time events (land, fire, etc.)
Definition: pluginpresencenotifier.c:200
ToString
proto string ToString()
FSMTransition
represents transition src -— event[guard]/action -—|> dst
array< ref FSMStateBase >
Update
proto native volatile void Update()
Definition: playersoundmanager.c:125
ProcessEventResult
ProcessEventResult
Definition: fsmbase.c:40
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