33 protected bool m_HasCompletions =
false;
37 m_OwnerState = ownerState;
73 FSMStateBase curr = state;
77 curr = curr.GetParentState();
79 return path.Count() > 0;
88 m_InitialState = initial_state;
95 m_Transitions.Insert(t);
97 if (t.m_event == NULL)
99 Print(
"Warning (performance): FSM " +
this +
" has completion transition for src=" + t.m_srcState +
" ---NULL----|> dst=" + t.m_dstState);
100 m_HasCompletions =
true;
108 void Start (FSMEventBase initial_event = NULL,
bool useExistingState =
false)
112 fsmDebugPrint(
"[hfsm] " + this.
ToString() +
"::Start(" + initial_event.ToString() +
"), init_state=" + m_InitialState.ToString());
115 if (!useExistingState)
118 m_State.OnEntry(initial_event);
120 if (m_HasCompletions)
121 ProcessCompletionTransitions();
139 m_State.OnExit(terminal_event);
143 void Abort (FSMEventBase abort_event = NULL)
169 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());
175 t.m_action.Action(e);
177 auto tmp = t.m_srcState.GetParentState();
178 if (tmp == t.m_dstState.GetParentState())
182 if (t.m_dstState != NULL)
191 fsmDebugPrint(
"[hfsm] abort & terminating fsm: state=" + t.m_srcState.ToString() +
" event=" + e.ToString());
217 fsmDebugPrint(
"[hfsm] SUB! " + GetOwnerState().
Type().
ToString() +
"::FindAbortDestinationState(" + e.Type().ToString() +
")");
219 fsmDebugPrint(
"[hfsm] root::FindAbortDestinationState(" + e.Type().ToString() +
")");
226 FSMStateBase abort_dst = a.FindAbortDestinationState(e);
235 int i = FindFirstUnguardedTransition(e);
240 fsmDebugPrint(
"[hfsm] abort event has no transition: src=" +
m_State.ToString() +
" e=" + e.Type().ToString());
265 fsmDebugPrint(
"[hfsm] root::ProcessAbortEvent(" + e.Type().ToString() +
")");
273 FSMStateBase abort_dst = a.ProcessAbortEvent(e, subfsm_res);
294 if (GetOwnerState() == abort_dst.GetParentState())
298 fsmDebugPrint(
"[hfsm] aborted sub machine=" +
m_State.ToString() +
" & abort destination reached.");
329 int i = FindFirstUnguardedTransition(e);
334 fsmDebugPrint(
"[hfsm] abort event has no transition: src=" +
m_State.ToString() +
" e=" + e.Type().ToString());
354 fsmDebugPrint(
"[hfsm] aborted sub machine=" +
m_State.ToString() +
" will fall-through to dst=" + t.m_dstState);
390 fsmDebugPrint(
"[hfsm] root::ProcessEvent(" + e.Type().ToString() +
" =" + e.DumpToString());
394 if (m_HasCompletions)
395 ProcessCompletionTransitions();
421 int i = FindFirstUnguardedTransition(e);
426 fsmDebugPrint(
"[hfsm] event has no transition: src=" +
m_State.ToString() +
" e=" + e.Type().ToString());
433 if (row.m_dstState != NULL)
436 if (row.m_srcState.GetParentState() == row.m_dstState.GetParentState())
437 res = LocalTransition(i, e);
439 Error(
"cross-hierarchy transition or misconfigured transition detected!");
445 if (row.m_srcState.GetParentState() == GetOwnerState())
446 res = LocalTransition(i, e);
448 Error(
"cross-hierarchy transition or misconfigured transition detected!");
456 FSMStateBase curr_state =
m_State;
458 int count = m_Transitions.Count();
459 for (
int i = 0; i < count; ++i)
462 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
465 bool hasGuard = t.m_guard != NULL;
466 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e)))
478 FSMStateBase curr_state = s;
480 int count = m_Transitions.Count();
481 for (
int i = 0; i < count; ++i)
484 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
494 FSMStateBase curr_state = s;
496 int count = m_Transitions.Count();
497 for (
int i = 0; i < count; ++i)
500 if ((t.m_srcState == curr_state) && (t.m_event != NULL) && (t.m_event.Type() == e.Type()))
502 bool hasGuard = t.m_guard != NULL;
503 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(e)))
516 FSMStateBase curr_state =
m_State;
518 int count = m_Transitions.Count();
519 for (
int i = 0; i < count; ++i)
524 if ((t.m_srcState.Type() == curr_state.Type()) && (t.m_event == NULL))
526 bool hasGuard = t.m_guard != NULL;
527 if (!hasGuard || (hasGuard && t.m_guard.GuardCondition(NULL)))
548 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());
554 t.m_action.Action(e);
558 if (t.m_dstState != NULL)
563 GetOwnerState().OnSubMachineChanged(t.m_srcState, t.m_dstState);
566 m_State.OnStateChanged(t.m_srcState, t.m_dstState);
574 fsmDebugPrint(
"[hfsm] terminating fsm: state=" + t.m_srcState.ToString() +
" event=" + e.ToString());
578 GetOwnerState().OnSubMachineChanged(t.m_srcState, NULL);
597 int completionIdx = FindFirstCompletionTransition();
598 while (completionIdx != -1)
602 if (row.m_dstState != NULL)
605 if (row.m_srcState.GetParentState() == row.m_dstState.GetParentState())
606 res = LocalTransition(completionIdx, NULL);
608 Error(
"cross-hierarchy transition or misconfigured transition detected!");
614 if (row.m_srcState.GetParentState() == GetOwnerState())
615 res = LocalTransition(completionIdx, NULL);
617 Error(
"cross-hierarchy transition or misconfigured transition detected!");
621 completionIdx = FindFirstCompletionTransition();
Super root of all classes in Enforce script.
represents transition src -— event[guard]/action -—|> dst
ProcessEventResult ProcessEvent(FSMEventBase e)
instructs the hierarchical state machine to process the event e
ProcessEventResult ProcessAbortTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t, FSMEventBase e)
ref FSMStateBase m_InitialState
state that owns this fsm (or null if root)
void HFSMBase(FSMStateBase ownerState=NULL)
FSMStateBase GetOwnerState()
ProcessEventResult ProcessCompletionTransitions()
void Update(float dt)
if machine running, call OnUpdate() on current state
bool IsRunning()
returns true if machine is in running state
FSMStateBase m_OwnerState
current fsm state
FSMStateBase FindGuardedTransitionState(FSMStateBase s, FSMEventBase e)
FSMStateBase FindTransitionState(FSMStateBase s, FSMEventBase 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...
void Abort(FSMEventBase abort_event=NULL)
bool GetHierarchyPath(FSMStateBase state, out array< FSMStateBase > path)
returns hierarchic state (path to root) of a state
void SetCurrentState(FSMStateBase state)
ProcessEventResult LocalTransition(int i, FSMEventBase e)
FSMStateBase ProcessAbortEvent(FSMEventBase e, out ProcessEventResult result)
instructs the hierarchical state machine to process the event e
void AddTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t)
adds transition into transition table
FSMStateBase GetCurrentState()
int FindFirstCompletionTransition()
ProcessEventResult ProcessLocalTransition(FSMTransition< FSMStateBase, FSMEventBase, FSMActionBase, FSMGuardBase > t, FSMEventBase e)
instructs the state machine to process the event locally - no hierarchy is crossed
FSMStateBase FindAbortDestinationState(FSMEventBase e)
i
int FindFirstUnguardedTransition(FSMEventBase e)
void SetInitialState(FSMStateBase initial_state)
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.
proto void PrintToRPT(void var)
Prints content of variable to RPT file (performance warning - each write means fflush!...
void fsmDebugSpam(string s)
void fsmDebugPrint(string s)
enum EObjectTemperatureState m_State