DayZ Scripts
v1.21.156300 · Jun 20, 2023
 
Loading...
Searching...
No Matches
FSMBase.c
Go to the documentation of this file.
1void fsmbDebugPrint (string s)
2{
3 //Print("" + s); // comment/uncomment to hide/see debug logs
4}
5void fsmbDebugSpam (string s)
6{
7 //Print("" + s); // comment/uncomment to hide/see debug spam
8}
9
10
14class FSMTransition<Class FSMStateBase, Class FSMEventBase, Class FSMActionBase, Class FSMGuardBase>
15{
16 ref FSMStateBase m_srcState;
17 ref FSMEventBase m_event; // @NOTE: NULL event means "completion transition" in UML speak
18 ref FSMStateBase m_dstState; // @NOTE: NULL dst state == UML terminate pseudonode
19 ref FSMActionBase m_action;
20 ref FSMGuardBase m_guard;
21
22 void FSMTransition (FSMStateBase src, FSMEventBase e, FSMStateBase dst, FSMActionBase a = NULL, FSMGuardBase g = NULL)
23 {
24 m_srcState = src;
25 m_event = e;
26 m_dstState = dst;
27 m_action = a;
28 m_guard = g;
29 }
30};
31
33{
38};
39
46class FSMBase<Class FSMStateBase, Class FSMEventBase, Class FSMActionBase, Class FSMGuardBase>
47{
48 protected ref FSMStateBase m_state;
49 protected ref FSMStateBase m_initialState;
50 protected ref FSMEventBase m_initialEvent;
52
53 void FSMBase ()
54 {
56 }
57
62 FSMStateBase GetCurrentState ()
63 {
64 return m_state;
65 }
66
70 void SetInitialState (FSMStateBase initial_state)
71 {
72 m_initialState = initial_state;
73 }
74
79 void Start (FSMEventBase initial_event = NULL)
80 {
81 fsmbDebugPrint("[fsm] " + this.ToString() + "::Start(" + initial_event.ToString() + "), init_state=" + m_initialState.ToString());
82
83 m_state = m_initialState;
84 m_state.OnEntry(initial_event);
85 }
86
90 bool IsRunning () { return m_state != NULL; }
91
95 void Terminate (FSMEventBase terminal_event = NULL)
96 {
97 if (IsRunning())
98 {
99 m_state.OnExit(terminal_event);
100 m_state = NULL;
101 }
102 }
103
107 void Update (float dt)
108 {
109 if (IsRunning())
110 m_state.OnUpdate(dt);
111 }
112
117 {
118 m_transitions.Insert(t);
119 }
120
127 {
128 FSMStateBase curr_state = m_state;
129
130 int count = m_transitions.Count();
131 for (int i = 0; i < count; ++i)
132 {
134 if (row.m_srcState.Type() == curr_state.Type() && row.m_event.Type() == e.Type())
135 {
137 bool hasGuard = t.m_guard != NULL;
138 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e))) // 1) exec guard (if any)
139 {
140 ProcessLocalTransition(t, e); // 2) process transition allowed by guard
141 }
142 }
143 }
144 return ProcessEventResult.FSM_NO_TRANSITION;
145 }
146
154 {
155 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());
156
157 m_state.OnExit(e); // 1) call onExit on old state
158
159 if (t.m_action)
160 t.m_action.Action(e); // 2) execute transition action (if any)
161
162 m_state = t.m_dstState; // 3) change state to new
163
164 if (t.m_dstState != NULL)
165 {
166 m_state.OnEntry(e); // 4a) call onEntry on new state
167 return ProcessEventResult.FSM_OK;
168 }
169 else
170 {
171 fsmbDebugPrint("[fsm] terminating fsm: state=" + t.m_srcState.ToString() + " event=" + e.ToString());
172 return ProcessEventResult.FSM_TERMINATED; // 4b) or terminate
173 }
174 }
175};
176
ref HandEventBase m_event
proto string ToString()
ProcessEventResult
Definition FSMBase.c:33
@ FSM_ABORTED
Definition FSMBase.c:36
@ FSM_OK
Definition FSMBase.c:34
@ FSM_TERMINATED
Definition FSMBase.c:35
@ FSM_NO_TRANSITION
Definition FSMBase.c:37
void fsmbDebugPrint(string s)
Definition FSMBase.c:1
void fsmbDebugSpam(string s)
Definition FSMBase.c:5
class WeaponEndAction extends WeaponStartAction m_action
Super root of all classes in Enforce script.
Definition EnScript.c:11
ProcessEventResult ProcessEvent(FSMEventBase e)
instructs the state machine to process the event e
Definition FSMBase.c:126
void SetInitialState(FSMStateBase initial_state)
sets the initial_state for starting the machine
Definition FSMBase.c:70
void Update(float dt)
if machine running, call OnUpdate() on current state
Definition FSMBase.c:107
void AddTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t)
adds transition into transition table
Definition FSMBase.c:116
protected ref array< ref FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > > m_transitions
configurable initial event to start the machine (null by default)
Definition FSMBase.c:51
protected ref FSMEventBase m_initialEvent
configurable initial state of the machine
Definition FSMBase.c:50
void Start(FSMEventBase initial_event=NULL)
starts the state machine by entering the initial_state (using intial_event as argument to initial sta...
Definition FSMBase.c:79
protected ProcessEventResult ProcessLocalTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t, FSMEventBase e)
instructs the state machine to process the event locally - no hierarchy is crossed
Definition FSMBase.c:153
void Terminate(FSMEventBase terminal_event=NULL)
terminates the state machine
Definition FSMBase.c:95
base class for finite state machine
void FSMTransition(FSMStateBase src, FSMEventBase e, FSMStateBase dst, FSMActionBase a=NULL, FSMGuardBase g=NULL)
Definition FSMBase.c:22
represents transition src -— event[guard]/action -—|> dst
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
static proto string ToString(void var, bool type=false, bool name=false, bool quotes=true)
Return string representation of variable.
bool IsRunning()
Definition tools.c:263