File size: 5,971 Bytes
ce81a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using Photon.Deterministic;
using System;
using System.Collections.Generic;

namespace Quantum
{
	[AssetObjectConfig(GenerateLinkingScripts = true, GenerateAssetCreateMenu = false, GenerateAssetResetMethod = false)]
	public unsafe partial class HFSMState : AssetObject
	{
		public string Label;
		public AssetRefAIAction[] OnUpdateLinks;
		public AssetRefAIAction[] OnEnterLinks;
		public AssetRefAIAction[] OnExitLinks;
		public HFSMTransition[] Transitions;

		public AssetRefHFSMState[] ChildrenLinks;
		public AssetRefHFSMState ParentLink;
		public int Level;

		[NonSerialized]
		public AIAction[] OnUpdate;
		[NonSerialized]
		public AIAction[] OnEnter;
		[NonSerialized]
		public AIAction[] OnExit;
		[NonSerialized]
		public HFSMState[] Children;
		[NonSerialized]
		public HFSMState Parent;

		public override void Loaded(IResourceManager resourceManager, Native.Allocator allocator)
		{
			base.Loaded(resourceManager, allocator);

			OnUpdate = new AIAction[OnUpdateLinks == null ? 0 : OnUpdateLinks.Length];
			if (OnUpdateLinks != null)
			{
				for (Int32 i = 0; i < OnUpdateLinks.Length; i++)
				{
					OnUpdate[i] = (AIAction)resourceManager.GetAsset(OnUpdateLinks[i].Id);
				}
			}
			OnEnter = new AIAction[OnEnterLinks == null ? 0 : OnEnterLinks.Length];
			if (OnEnterLinks != null)
			{
				for (Int32 i = 0; i < OnEnterLinks.Length; i++)
				{
					OnEnter[i] = (AIAction)resourceManager.GetAsset(OnEnterLinks[i].Id);
				}
			}
			OnExit = new AIAction[OnExitLinks == null ? 0 : OnExitLinks.Length];
			if (OnExitLinks != null)
			{
				for (Int32 i = 0; i < OnExitLinks.Length; i++)
				{
					OnExit[i] = (AIAction)resourceManager.GetAsset(OnExitLinks[i].Id);
				}
			}

			Children = new HFSMState[ChildrenLinks == null ? 0 : ChildrenLinks.Length];
			if (ChildrenLinks != null)
			{
				for (Int32 i = 0; i < ChildrenLinks.Length; i++)
				{
					Children[i] = (HFSMState)resourceManager.GetAsset(ChildrenLinks[i].Id);
				}
			}

			Parent = (HFSMState)resourceManager.GetAsset(ParentLink.Id);
			if (Transitions != null)
			{
				for (int i = 0; i < Transitions.Length; i++)
				{
					Transitions[i].Setup(resourceManager);
				}
			}
		}

		internal Boolean UpdateState(Frame frame, FP deltaTime, HFSMData* hfsmData, EntityRef entity)
		{
			HFSMState parent = Parent;
			Boolean transition = false;

			if (parent != null)
			{
				transition = parent.UpdateState(frame, deltaTime, hfsmData, entity);
			}

			if (transition == true)
				return true;

			*hfsmData->Times.GetPointer(Level) += deltaTime;

			DoUpdateActions(frame, entity);
			return CheckStateTransitions(frame, hfsmData, entity, 0);
		}

		internal Boolean Event(Frame frame, HFSMData* hfsmData, EntityRef entity, Int32 eventInt)
		{
			HFSMState p = Parent;
			Boolean transition = false;
			if (p != null)
			{
				transition = p.Event(frame, hfsmData, entity, eventInt);
			}

			if (transition)
			{
				return true;
			}

			return CheckStateTransitions(frame, hfsmData, entity, eventInt);
		}

		private void DoUpdateActions(Frame frame, EntityRef entity)
		{
			for (int i = 0; i < OnUpdate.Length; i++)
			{
				OnUpdate[i].Update(frame, entity);
				int nextAction = OnUpdate[i].NextAction(frame, entity);
				if (nextAction > i)
				{
					i = nextAction;
				}
			}
		}
		private void DoEnterActions(Frame frame, EntityRef entity)
		{
			for (int i = 0; i < OnEnter.Length; i++)
			{
				OnEnter[i].Update(frame, entity);
				int nextAction = OnEnter[i].NextAction(frame, entity);
				if (nextAction > i)
				{
					i = nextAction;
				}
			}
		}
		private void DoExitActions(Frame frame, EntityRef entity)
		{
			for (int i = 0; i < OnExit.Length; i++)
			{
				OnExit[i].Update(frame, entity);
				int nextAction = OnExit[i].NextAction(frame, entity);
				if (nextAction > i)
				{
					i = nextAction;
				}
			}
		}

		private bool CheckStateTransitions(Frame frame, HFSMData* hfsmData, EntityRef entity, Int32 eventKey = 0)
		{
			hfsmData->Time = *hfsmData->Times.GetPointer(Level);

			return CheckTransitions(frame, Transitions, hfsmData, entity, eventKey);
		}

		private static bool CheckTransitions(Frame frame, HFSMTransition[] transitions, HFSMData* hfsmData, EntityRef entity, int eventKey, int depth = 0)
		{
			// Just to avoid accidental loops
			if (depth == 10)
				return false;

			if (transitions == null)
				return false;

			for (int i = 0; i < transitions.Length; i++)
			{
				var transition = transitions[i];

				if (transition.State == null && transition.TransitionSet == null)
					continue;

				// Only consider evaluating the event if this transition HAS a event as requisite (EventKey != 0)
				if (transition.EventKey != 0 && transition.EventKey != eventKey)
					continue;

				if (transition.Decision != null && transition.Decision.Decide(frame, entity) == false)
					continue;

				if (transition.State != null)
				{
					HFSMManager.ChangeState(transition.State, frame, hfsmData, entity, transition.Id);
					return true;
				}
				else if (CheckTransitions(frame, transition.TransitionSet.Transitions, hfsmData, entity, eventKey, depth + 1) == true)
				{
					return true;
				}
			}

			return false;
		}

		internal void EnterState(Frame frame, HFSMData* hfsmData, EntityRef entity)
		{
			*hfsmData->Times.GetPointer(Level) = FP._0;
			DoEnterActions(frame, entity);
			if (Children != null && Children.Length > 0)
			{
				HFSMState child = Children[0];
				HFSMManager.ChangeState(child, frame, hfsmData, entity, "");
			}
		}

		internal void ExitState(HFSMState nextState, Frame frame, HFSMData* hfsmData, EntityRef entity)
		{
			if (nextState != null && nextState.IsChildOf(this) == true)
				return;

			DoExitActions(frame, entity);
			Parent?.ExitState(nextState, frame, hfsmData, entity);
		}

		internal bool IsChildOf(HFSMState state)
		{
			HFSMState parent = Parent;

			if (parent == null)
				return false;

			if (parent == state)
				return true;

			return parent.IsChildOf(state);
		}
	}
}