File size: 11,623 Bytes
ac55997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using Photon.Deterministic;
using Quantum.Collections;

namespace Quantum
{
	public unsafe partial struct UtilityReasoner
	{
		public void Initialize(Frame frame, AssetRefUTRoot utRootRef = default, EntityRef entity = default)
		{
			// If we don't receive the UTRoot as parameter, we try to find it on the component itself
			// Useful for pre-seting the UTRoot on a Prototype
			UTRoot utRootInstance;
			if (utRootRef == default)
			{
				utRootInstance = frame.FindAsset<UTRoot>(UTRoot.Id);
			}
			else
			{
				UTRoot = utRootRef;
				utRootInstance = frame.FindAsset<UTRoot>(utRootRef.Id);
			}

			// Initialize the Reasoner's considerations.
			// Can be useful further when creating dynamically added Considerations to the Agent (runtime)
			QList<AssetRefConsideration> considerationsList = frame.AllocateList<AssetRefConsideration>();
			for (int i = 0; i < utRootInstance.ConsiderationsRefs.Length; i++)
			{
				considerationsList.Add(utRootInstance.ConsiderationsRefs[i]);
			}
			Considerations = considerationsList;

			CooldownsDict = frame.AllocateDictionary<AssetRefConsideration, FP>();

			QList<AssetRefConsideration> previousExecution = frame.AllocateList<AssetRefConsideration>();
			for (int i = 0; i < 6; i++)
			{
				previousExecution.Add(default);
			}
			PreviousExecution = previousExecution;

			MomentumList = frame.AllocateList<UTMomentumPack>();

			if (entity != default)
			{
				UTManager.SetupDebugger?.Invoke(entity, utRootInstance.Path);
			}
		}

		public void Free(Frame frame)
		{
			UTRoot = default;
			frame.FreeList<AssetRefConsideration>(Considerations);
			frame.FreeDictionary<AssetRefConsideration, FP>(CooldownsDict);
			frame.FreeList<AssetRefConsideration>(PreviousExecution);
			frame.FreeList<UTMomentumPack>(MomentumList);
		}

		public void Update(Frame frame, UtilityReasoner* reasoner, EntityRef entity = default)
		{
			Consideration[] considerations = SolveConsiderationsList(frame, Considerations, reasoner, entity);
			Consideration chosenConsideration = SelectBestConsideration(frame, considerations, 1, reasoner, entity);

			if (chosenConsideration != default)
			{
				chosenConsideration.OnUpdate(frame, reasoner, entity);
				UTManager.ConsiderationChosen?.Invoke(entity, chosenConsideration.Identifier.Guid.Value);
			}

			TickMomentum(frame, entity);
		}

		public Consideration[] SolveConsiderationsList(Frame frame, QListPtr<AssetRefConsideration> considerationsRefs, UtilityReasoner* reasoner, EntityRef entity = default)
		{
			QList<AssetRefConsideration> solvedRefs = frame.ResolveList(considerationsRefs);
			Consideration[] considerationsArray = new Consideration[solvedRefs.Count];
			for (int i = 0; i < solvedRefs.Count; i++)
			{
				considerationsArray[i] = frame.FindAsset<Consideration>(solvedRefs[i].Id);
			}

			return considerationsArray;
		}

		public Consideration SelectBestConsideration(Frame frame, Consideration[] considerations, byte depth, UtilityReasoner* reasoner, EntityRef entity = default)
		{
			if (considerations == default)
				return null;

			QList<UTMomentumPack> momentumList = frame.ResolveList(MomentumList);

			// We get the Rank of every Consideration Set
			// This "filters" the Considerations with higher absolute utility
			AssetRefConsideration[] highRankConsiderations = new AssetRefConsideration[considerations.Length];
			int highestRank = -1;
			int counter = 0;
			QDictionary<AssetRefConsideration, FP> cooldowns = frame.ResolveDictionary(CooldownsDict);

			for (int i = 0; i < considerations.Length; i++)
			{
				Consideration consideration = considerations[i];

				// Force low Rank for Considerations in Cooldown
				if (cooldowns.Count > 0 && cooldowns.ContainsKey(considerations[i]) == true)
				{
					cooldowns[considerations[i]] -= frame.DeltaTime;
					if (cooldowns[considerations[i]] <= 0)
					{
						cooldowns.Remove(considerations[i]);
					}
					{
						continue;
					}
				}

				// If the Consideration has Momentum, then it's Rank should is defined by it
				// Otherwise, we calculate the Rank dynamically
				int rank;
				if (ContainsMomentum(momentumList, consideration, out var momentum) == true)
				{
					rank = momentum.Value;
				}
				else
				{
					rank = consideration.GetRank(frame, entity);
				}

				if (rank > highestRank)
				{
					counter = 0;

					highestRank = rank;
					highRankConsiderations[counter] = considerations[i];
				}
				else if (highestRank == rank)
				{
					counter++;
					highRankConsiderations[counter] = considerations[i];
				}
			}

			// We clean the indices on the high rank sets that were not selected
			for (int i = counter + 1; i < highRankConsiderations.Length; i++)
			{
				if (highRankConsiderations[i] == default)
					break;

				highRankConsiderations[i] = default;
			}

			// Based on the higher rank, we check which Considerations sets have greater utility
			// Then we choose that set this frame
			Consideration chosenConsideration = default;
			FP highestScore = FP.UseableMin;
			for (int i = 0; i <= counter; i++)
			{
				if (highRankConsiderations[i] == default)
					continue;

				Consideration consideration = frame.FindAsset<Consideration>(highRankConsiderations[i].Id);

				FP score = consideration.Score(frame, entity);
				if (highestScore < score)
				{
					highestScore = score;
					chosenConsideration = consideration;
				}
			}

			if (chosenConsideration != default)
			{
				// If the chosen Consideration and it is not already under Momentum,
				// we add add it there, replacing the previous Momentum (if any)
				if (chosenConsideration.MomentumData.Value > 0 && ContainsMomentum(momentumList, chosenConsideration, out var momentum) == false)
				{
					InsertMomentum(frame, momentumList, chosenConsideration);
				}

				// If the chosen Consideration has cooldown and it is not yet on the cooldowns dictionary,
				// we add it there
				if (chosenConsideration.Cooldown > 0 && cooldowns.ContainsKey(chosenConsideration) == false)
				{
					cooldowns.Add(chosenConsideration, chosenConsideration.Cooldown);
				}

				// Add the chosen set to the choices history
				OnConsiderationChosen(frame, reasoner, chosenConsideration, entity);
			}
			else
			{
				OnNoConsiderationChosen(frame, reasoner, depth, entity);
			}

			// We return the chosen set so it can be executed
			return chosenConsideration;
		}

		#region Momentum
		private bool ContainsMomentum(QList<UTMomentumPack> momentumList, Consideration consideration, out UTMomentumData momentum)
		{
			for (int i = 0; i < momentumList.Count; i++)
			{
				if (momentumList[i].ConsiderationRef == consideration)
				{
					momentum = momentumList[i].MomentumData;
					return true;
				}
			}

			momentum = default;
			return false;
		}

		private void InsertMomentum(Frame frame, QList<UTMomentumPack> momentumList, AssetRefConsideration considerationRef)
		{
			Consideration newConsideration = frame.FindAsset<Consideration>(considerationRef.Id);

			// First, we check if this should be a replacement, which happens if:
			// . The momentum list already have that same Depth added
			// . Or when it have a higher Depth added
			bool wasReplacedment = false;
			for (int i = 0; i < momentumList.Count; i++)
			{
				Consideration currentConsideration = frame.FindAsset<Consideration>(momentumList[i].ConsiderationRef.Id);
				if (currentConsideration.Depth == newConsideration.Depth || currentConsideration.Depth > newConsideration.Depth)
				{
					momentumList.GetPointer(i)->ConsiderationRef = considerationRef;
					momentumList.GetPointer(i)->MomentumData = frame.FindAsset<Consideration>(considerationRef.Id).MomentumData;

					// We clear the rightmost momentum entries
					if (i < momentumList.Count - 1)
					{
						for (int k = i + 1; k < momentumList.Count; k++)
						{
							momentumList.RemoveAt(k);
						}
					}

					wasReplacedment = true;
					break;
				}
			}

			// If there was no replacement, we simply add it to the end of the list as this
			// consideration probably has higher Depth than the others currently on the list
			// which can also mean that the list was empty
			if (wasReplacedment == false)
			{
				UTMomentumPack newMomentum = new UTMomentumPack()
				{
					ConsiderationRef = considerationRef,
					MomentumData = frame.FindAsset<Consideration>(considerationRef.Id).MomentumData,
				};
				momentumList.Add(newMomentum);
			}
		}

		private void TickMomentum(Frame frame, EntityRef entity = default)
		{
			QList<UTMomentumPack> momentumList = frame.ResolveList(MomentumList);

			// We decrease the timer and check if it is time already to decay all of the current Momentums
			TimeToTick -= frame.DeltaTime;
			bool decay = false;
			if (TimeToTick <= 0)
			{
				decay = true;
				TimeToTick = 1;
			}

			for (int i = 0; i < momentumList.Count; i++)
			{
				UTMomentumPack* momentum = momentumList.GetPointer(i);

				// If we currently have a commitment, we check if it is done already
				// If it is done, that Consideration's Rank shall be re-calculated
				// If it is not done, then the Consideration's Rank will be kept due to the commitment
				// unless some other Consideration has greater Rank and replaces the current commitment
				Consideration momentumConsideration = frame.FindAsset<Consideration>(momentum->ConsiderationRef.Id);

				if (momentum->MomentumData.Value > 0 && momentumConsideration.MomentumData.DecayAmount > 0)
				{
					if (decay)
					{
						momentum->MomentumData.Value -= momentumConsideration.MomentumData.DecayAmount;
					}
				}


				bool isDone = false;
				if (momentumConsideration.Commitment != default)
				{
					isDone = momentumConsideration.Commitment.Execute(frame, entity);
				}
				if (isDone == true || momentum->MomentumData.Value <= 0)
				{
					momentum->MomentumData.Value = 0;
					momentumList.RemoveAt(i);
				}
			}
		}
		#endregion

		#region ConsiderationsChoiceReactions
		private static void OnConsiderationChosen(Frame frame, UtilityReasoner* reasoner, AssetRefConsideration chosenConsiderationRef, EntityRef entity = default)
		{
			Consideration chosenConsideration = frame.FindAsset<Consideration>(chosenConsiderationRef.Id);

			QList<AssetRefConsideration> previousExecution = frame.ResolveList(reasoner->PreviousExecution);

			if (previousExecution[chosenConsideration.Depth - 1] != chosenConsideration)
			{
				// Exit the one that we're replacing
				var replacedSet = frame.FindAsset<Consideration>(previousExecution[chosenConsideration.Depth - 1].Id);
				if (replacedSet != default)
				{
					replacedSet.OnExit(frame, reasoner, entity);
				}

				// Exit the consecutive ones
				for (int i = chosenConsideration.Depth; i < previousExecution.Count; i++)
				{
					var cs = frame.FindAsset<Consideration>(previousExecution[i].Id);
					if (cs == default)
						break;

					cs.OnExit(frame, reasoner, entity);
					previousExecution[i] = default;
				}

				// Insert and Enter on the new chosen consideration
				previousExecution[chosenConsideration.Depth - 1] = chosenConsideration;
				chosenConsideration.OnEnter(frame, reasoner, entity);
			}
		}

		private static void OnNoConsiderationChosen(Frame frame, UtilityReasoner* reasoner, byte depth, EntityRef entity = default)
		{
			QList<AssetRefConsideration> previousExecution = frame.ResolveList(reasoner->PreviousExecution);

			for (int i = depth - 1; i < previousExecution.Count; i++)
			{
				var cs = frame.FindAsset<Consideration>(previousExecution[i].Id);
				if (cs == default)
					break;

				cs.OnExit(frame, reasoner, entity);
				previousExecution[i] = default;
			}
		}
		#endregion
	}
}