DayZ 1.24
Loading...
Searching...
No Matches
OFSMBase.c
Go to the documentation of this file.
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
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
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
78 {
79 if (IsRunning())
80 {
81 for (int s = 0; s < m_States.Count(); ++s)
82 {
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 m_States[s].OnUpdate(dt);
102 }
103 }
104
112
119 {
120 int count = m_Transitions.Count();
121 for (int i = 0; i < count; ++i)
122 {
124 if (row.m_event.Type() == e.Type())
125 {
126 for (int s = 0; s < m_States.Count(); ++s)
127 {
128 if (row.m_srcState.Type() == m_States[s].Type() && row.m_event.Type() == e.Type())
129 {
131 bool hasGuard = t.m_guard != NULL;
132 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e))) // 1) exec guard (if any)
133 {
134 ProcessLocalTransition(s, t, e); // 2) process transition allowed by guard
135 }
136 }
137 }
138 }
139 }
140 return ProcessEventResult.FSM_NO_TRANSITION;
141 }
142
150 {
151 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());
152
153 m_States[s].OnExit(e); // 1) call onExit on old state
154
155 if (t.m_action)
156 t.m_action.Action(e); // 2) execute transition action (if any)
157
158 m_States[s] = t.m_dstState; // 3) change state to new
159
160 if (t.m_dstState != NULL)
161 {
162 m_States[s].OnEntry(e); // 4a) call onEntry on new state
163 return ProcessEventResult.FSM_OK;
164 }
165 else
166 {
167 if (LogManager.IsInventoryHFSMLogEnable()) fsmbDebugPrint("[ofsm] terminating fsm: state=" + t.m_srcState.ToString() + " event=" + e.ToString());
168 return ProcessEventResult.FSM_TERMINATED; // 4b) or terminate
169 }
170 }
171};
172
proto string ToString()
ProcessEventResult
Definition FSMBase.c:41
void fsmbDebugPrint(string s)
Definition FSMBase.c:1
Super root of all classes in Enforce script.
Definition EnScript.c:11
static bool IsInventoryHFSMLogEnable()
Definition Debug.c:749
void AddTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t)
adds transition into transition table
Definition OFSMBase.c:108
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:149
ProcessEventResult ProcessEvent(FSMEventBase e)
instructs the state machine to process the event e
Definition OFSMBase.c:118
void Update(float dt)
if machine running, call OnUpdate() on current state
Definition OFSMBase.c:96
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
void Terminate(array< ref FSMEventBase > terminal_events=null)
terminates the state machine
Definition OFSMBase.c:77
ref array< ref FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > > m_Transitions
configurable initial state of the machine
Definition OFSMBase.c:11
base class for Orthogonal Finite State Machine
bool IsRunning()
Definition tools.c:252
proto native void OnUpdate()
Definition tools.c:333