Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
ofsmbase.c
Go to the documentation of this file.
7class OFSMBase<Class FSMStateBase, Class FSMEventBase, Class FSMActionBase, Class FSMGuardBase>
8{
12
13 void OFSMBase ()
14 {
15 m_States = new array<ref FSMStateBase>;
16 m_InitialStates = new array<ref FSMStateBase>;
18 }
19
25 {
26 return m_States;
27 }
28
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
114
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
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
Super root of all classes in Enforce script.
Definition enscript.c:11
represents transition src -— event[guard]/action -—|> dst
void Start(array< ref FSMEventBase > initial_events=null)
starts the state machine by entering the initial_state (using intial_event as argument to initial sta...
Definition ofsmbase.c:44
ProcessEventResult ProcessLocalTransition(int s, FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t, FSMEventBase e)
instructs the state machine to process the event locally - no hierarchy is crossed
Definition ofsmbase.c:151
void AddTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t)
adds transition into transition table
Definition ofsmbase.c:110
void Update(float dt)
if machine running, call OnUpdate() on current state
Definition ofsmbase.c:96
ref array< ref FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > > m_Transitions
configurable initial state of the machine
Definition ofsmbase.c:11
ProcessEventResult ProcessEvent(FSMEventBase e)
instructs the state machine to process the event e
Definition ofsmbase.c:120
void Terminate(array< ref FSMEventBase > terminal_events=null)
terminates the state machine
Definition ofsmbase.c:77
base class for Orthogonal Finite State Machine
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto string ToString()
ProcessEventResult
Definition fsmbase.c:41
void fsmbDebugPrint(string s)
Definition fsmbase.c:1
bool IsRunning()
Definition tools.c:264