25 protected bool m_HasCompletions =
false;
29 m_OwnerState = ownerState;
54 FSMStateBase curr = state;
58 curr = curr.GetParentState();
60 return path.Count() > 0;
69 m_InitialState = initial_state;
76 m_Transitions.Insert(t);
78 if (t.m_event == NULL)
80 Print(
"Warning (performance): FSM " +
this +
" has completion transition for src=" + t.m_srcState +
" ---NULL----|> dst=" + t.m_dstState);
81 m_HasCompletions =
true;
89 void Start (FSMEventBase initial_event = NULL,
bool useExistingState =
false)
93 if (!useExistingState)
99 ProcessCompletionTransitions();
113 m_State.OnExit(terminal_event);
117 void Abort (FSMEventBase abort_event = NULL)
137 fsmDebugPrint(
"[hfsm] (local abort) state=" + t.m_srcState.ToString() +
"-------- ABORT event=" + e.ToString() +
"[G=" + t.m_guard.ToString() +
"]/A=" + t.m_action.ToString() +
" --------|> dst=" + t.m_dstState.ToString());
142 t.m_action.Action(e);
144 auto tmp = t.m_srcState.GetParentState();
145 if (tmp == t.m_dstState.GetParentState())
149 if (t.m_dstState != NULL)
156 fsmDebugPrint(
"[hfsm] abort & terminating fsm: state=" + t.m_srcState.ToString() +
" event=" + e.ToString());
180 fsmDebugPrint(
"[hfsm] root::FindAbortDestinationState(" + e.Type().ToString() +
")");
186 FSMStateBase abort_dst = a.FindAbortDestinationState(e);
195 int i = FindFirstUnguardedTransition(e);
219 fsmDebugPrint(
"[hfsm] root::ProcessAbortEvent(" + e.Type().ToString() +
")");
226 FSMStateBase abort_dst = a.ProcessAbortEvent(e, subfsm_res);
242 if (GetOwnerState() == abort_dst.GetParentState())
271 int i = FindFirstUnguardedTransition(e);
321 fsmDebugPrint(
"[hfsm] root::ProcessEvent(" + e.Type().ToString() +
" =" + e.DumpToString());
324 if (m_HasCompletions)
325 ProcessCompletionTransitions();
351 int i = FindFirstUnguardedTransition(e);
360 if (row.m_dstState != NULL)
363 if (row.m_srcState.GetParentState() == row.m_dstState.GetParentState())
364 res = LocalTransition(i, e);
366 Error(
"cross-hierarchy transition or misconfigured transition detected!");
372 if (row.m_srcState.GetParentState() == GetOwnerState())
373 res = LocalTransition(i, e);
375 Error(
"cross-hierarchy transition or misconfigured transition detected!");
383 FSMStateBase curr_state =
m_State;
385 int count = m_Transitions.Count();
386 for (
int i = 0; i < count; ++i)
389 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
392 bool hasGuard = t.m_guard != NULL;
393 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e)))
405 FSMStateBase curr_state = s;
407 int count = m_Transitions.Count();
408 for (
int i = 0; i < count; ++i)
411 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
421 FSMStateBase curr_state = s;
423 int count = m_Transitions.Count();
424 for (
int i = 0; i < count; ++i)
427 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
429 bool hasGuard = t.m_guard != NULL;
430 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e)))
443 FSMStateBase curr_state =
m_State;
445 int count = m_Transitions.Count();
446 for (
int i = 0; i < count; ++i)
451 if ((t.m_srcState.Type() == curr_state.Type()) && (t.m_event == NULL))
453 bool hasGuard = t.m_guard != NULL;
454 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(NULL)))
473 fsmDebugPrint(
"[hfsm] (local) state=" + t.m_srcState.ToString() +
"-------- event=" + e.ToString() +
"[G=" + t.m_guard.ToString() +
"]/A=" + t.m_action.ToString() +
" --------|> dst=" + t.m_dstState.ToString());
478 t.m_action.Action(e);
482 if (t.m_dstState != NULL)
487 GetOwnerState().OnSubMachineChanged(t.m_srcState, t.m_dstState);
490 m_State.OnStateChanged(t.m_srcState, t.m_dstState);
496 fsmDebugPrint(
"[hfsm] terminating fsm: state=" + t.m_srcState.ToString() +
" event=" + e.ToString());
499 GetOwnerState().OnSubMachineChanged(t.m_srcState, NULL);
518 int completionIdx = FindFirstCompletionTransition();
519 while (completionIdx != -1)
523 if (row.m_dstState != NULL)
526 if (row.m_srcState.GetParentState() == row.m_dstState.GetParentState())
527 res = LocalTransition(completionIdx, NULL);
529 Error(
"cross-hierarchy transition or misconfigured transition detected!");
535 if (row.m_srcState.GetParentState() == GetOwnerState())
536 res = LocalTransition(completionIdx, NULL);
538 Error(
"cross-hierarchy transition or misconfigured transition detected!");
542 completionIdx = FindFirstCompletionTransition();
void fsmDebugSpam(string s)
void fsmDebugPrint(string s)
protected float m_DrainThreshold protected bool m_State
Super root of all classes in Enforce script.
represents transition src -— event[guard]/action -—|> dst
protected ProcessEventResult LocalTransition(int i, FSMEventBase e)
ProcessEventResult ProcessEvent(FSMEventBase e)
instructs the hierarchical state machine to process the event e
void Start(FSMEventBase initial_event=NULL, bool useExistingState=false)
starts the state machine by entering the initial_state (using intial_event as argument to initial sta...
protected int FindFirstCompletionTransition()
FSMStateBase GetCurrentState()
FSMStateBase ProcessAbortEvent(FSMEventBase e, out ProcessEventResult result)
instructs the hierarchical state machine to process the event e
protected ref FSMStateBase m_InitialState
state that owns this fsm (or null if root)
void SetInitialState(FSMStateBase initial_state)
protected ProcessEventResult ProcessCompletionTransitions()
FSMStateBase GetOwnerState()
void Update(float dt)
if machine running, call OnUpdate() on current state
void AddTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t)
adds transition into transition table
FSMStateBase FindTransitionState(FSMStateBase s, FSMEventBase e)
bool IsRunning()
returns true if machine is in running state
void HFSMBase(FSMStateBase ownerState=NULL)
FSMStateBase FindGuardedTransitionState(FSMStateBase s, FSMEventBase e)
protected FSMStateBase m_OwnerState
current fsm state
protected ref FSMStateBase m_State
protected ProcessEventResult ProcessAbortTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t, FSMEventBase e)
FSMStateBase FindAbortDestinationState(FSMEventBase e)
i
protected int FindFirstUnguardedTransition(FSMEventBase e)
void Abort(FSMEventBase abort_event=NULL)
bool GetHierarchyPath(FSMStateBase state, out array< FSMStateBase > path)
returns hierarchic state (path to root) of a state
protected ProcessEventResult ProcessLocalTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t, FSMEventBase e)
instructs the state machine to process the event locally - no hierarchy is crossed
void Terminate(FSMEventBase terminal_event=NULL)
terminates the state machine
base class for hierarchic finite state machine
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
void Error(string err)
Messagebox with error message.
proto void Print(void var)
Prints content of variable to console/log.
static proto string ToString(void var, bool type=false, bool name=false, bool quotes=true)
Return string representation of variable.