File size: 8,160 Bytes
00437a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using Photon.Deterministic;
using System;
using Quantum.Collections;

namespace Quantum
{
	public unsafe partial struct AIBlackboardComponent
	{
		#region Init/Free
		public void InitializeBlackboardComponent(Frame frame, AIBlackboard blackboardAsset)
		{
			Board = blackboardAsset;

			var assetEntries = blackboardAsset.Entries;

			if (Entries.Ptr != default)
			{
				FreeBlackboardComponent(frame);
			}

			QList<BlackboardEntry> entriesList = frame.AllocateList<BlackboardEntry>(blackboardAsset.Entries.Length);

			for (int i = 0; i < assetEntries.Length; i++)
			{
				BlackboardValue newValue = CreateValueFromEntry(assetEntries[i]);
				entriesList.Add(new BlackboardEntry { Value = newValue });
				//entriesList.Add(newValue);
			}

			Entries = entriesList;
		}

		public void FreeBlackboardComponent(Frame frame)
		{
			if (Entries.Ptr != default)
			{
				frame.FreeList(Entries);
				Entries = default;
			}
		}

		private BlackboardValue CreateValueFromEntry(AIBlackboardEntry entry)
		{
			BlackboardValue newValue = new BlackboardValue();

			if (entry.Type == AIBlackboardValueType.Boolean)
			{
				*newValue.BooleanValue = default;
			}

			if (entry.Type == AIBlackboardValueType.Byte)
			{
				*newValue.ByteValue = default;
			}

			if (entry.Type == AIBlackboardValueType.Integer)
			{
				*newValue.IntegerValue = default;
			}

			if (entry.Type == AIBlackboardValueType.FP)
			{
				*newValue.FPValue = default;
			}

			if (entry.Type == AIBlackboardValueType.Vector2)
			{
				*newValue.FPVector2Value = default;
			}

			if (entry.Type == AIBlackboardValueType.Vector3)
			{
				*newValue.FPVector3Value = default;
			}

			if (entry.Type == AIBlackboardValueType.EntityRef)
			{
				*newValue.EntityRefValue = default;
			}

			return newValue;
		}
		#endregion

		#region Getters
		public QBoolean GetBoolean(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.BooleanValue;
		}

		public byte GetByte(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.ByteValue;
		}

		public Int32 GetInteger(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.IntegerValue;
		}

		public FP GetFP(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.FPValue;
		}

		public FPVector2 GetVector2(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.FPVector2Value;
		}

		public FPVector3 GetVector3(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.FPVector3Value;
		}

		public EntityRef GetEntityRef(Frame frame, string key)
		{
			var bbValue = GetBlackboardValue(frame, key);
			return *bbValue.EntityRefValue;
		}
		#endregion

		#region Setters
		public BlackboardEntry* Set(Frame frame, string key, QBoolean value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.BooleanValue = value;

			return valueList.GetPointer(ID);
		}

		public BlackboardEntry* Set(Frame frame, string key, byte value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.ByteValue = value;

			return valueList.GetPointer(ID);
		}

		public BlackboardEntry* Set(Frame frame, string key, Int32 value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.IntegerValue = value;

			return valueList.GetPointer(ID);
		}

		public BlackboardEntry* Set(Frame frame, string key, FP value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.FPValue = value;

			return valueList.GetPointer(ID);
		}

		public BlackboardEntry* Set(Frame frame, string key, FPVector2 value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.FPVector2Value = value;

			return valueList.GetPointer(ID);
		}

		public BlackboardEntry* Set(Frame frame, string key, FPVector3 value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.FPVector3Value = value;

			return valueList.GetPointer(ID);

		}

		public BlackboardEntry* Set(Frame frame, string key, EntityRef value)
		{
			QList<BlackboardEntry> valueList = frame.ResolveList(Entries);
			var ID = GetID(frame, key);
			*valueList.GetPointer(ID)->Value.EntityRefValue = value;

			return valueList.GetPointer(ID);
		}
		#endregion

		#region Helpers
		public BlackboardEntry* GetBlackboardEntry(Frame frame, string key)
		{
			var bbAsset = frame.FindAsset<AIBlackboard>(Board.Id);
			var ID = bbAsset.GetEntryID(key);
			var values = frame.ResolveList(Entries);
			return values.GetPointer(ID);
		}

		public BlackboardValue GetBlackboardValue(Frame frame, string key)
		{
			Assert.Check(string.IsNullOrEmpty(key) == false, "The Key cannot be empty or null.");

			var bbAsset = frame.FindAsset<AIBlackboard>(Board.Id);
			var ID = bbAsset.GetEntryID(key);
			var values = frame.ResolveList(Entries);

			return values[ID].Value;
		}

		public Int32 GetID(Frame frame, string key)
		{
			Assert.Check(string.IsNullOrEmpty(key) == false, "The Key cannot be empty or null.");

			var bbAsset = frame.FindAsset<AIBlackboard>(Board.Id);
			var ID = bbAsset.GetEntryID(key);

			return ID;
		}

		public bool HasEntry(Frame frame, string key)
		{
			var boardAsset = frame.FindAsset<AIBlackboard>(Board.Id);
			return boardAsset.HasEntry(key);
		}
		#endregion

		#region BT Specific
		public void RegisterReactiveDecorator(Frame frame, string key, BTDecorator decorator)
		{
			var blackboardEntry = GetBlackboardEntry(frame, key);

			QList<AssetRefBTDecorator> reactiveDecorators;
			if (blackboardEntry->ReactiveDecorators.Ptr == default)
			{
				reactiveDecorators = frame.AllocateList<AssetRefBTDecorator>();
			}
			else
			{
				reactiveDecorators = frame.ResolveList<AssetRefBTDecorator>(blackboardEntry->ReactiveDecorators);
			}
			reactiveDecorators.Add(decorator);

			blackboardEntry->ReactiveDecorators = reactiveDecorators;
		}

		public void UnregisterReactiveDecorator(Frame frame, string key, BTDecorator decorator)
		{
			var blackboardEntry = GetBlackboardEntry(frame, key);

			if (blackboardEntry->ReactiveDecorators.Ptr != default)
			{
				QList<AssetRefBTDecorator> reactiveDecorators = frame.ResolveList<AssetRefBTDecorator>(blackboardEntry->ReactiveDecorators);
				reactiveDecorators.Remove(decorator);
				blackboardEntry->ReactiveDecorators = reactiveDecorators;
			}
		}
		#endregion

		#region Debug
		public void Dump(Frame frame)
		{
			string dumpText = "";
			var bbAsset = frame.FindAsset<AIBlackboard>(Board.Id);
			dumpText += "Blackboard Path and ID: " + bbAsset.Path + "  |  " + Board.Id.Value;

			var valuesList = frame.ResolveList(Entries);
			for (int i = 0; i < valuesList.Count; i++)
			{
				string value = "NONE";
				if (valuesList[i].Value.Field == BlackboardValue.BOOLEANVALUE) value = valuesList[i].Value.BooleanValue->Value.ToString();
				if (valuesList[i].Value.Field == BlackboardValue.BYTEVALUE) value = valuesList[i].Value.ByteValue->ToString();
				if (valuesList[i].Value.Field == BlackboardValue.INTEGERVALUE) value = valuesList[i].Value.IntegerValue->ToString();
				if (valuesList[i].Value.Field == BlackboardValue.FPVALUE) value = valuesList[i].Value.FPValue->ToString();
				if (valuesList[i].Value.Field == BlackboardValue.FPVECTOR2VALUE) value = valuesList[i].Value.FPVector2Value->ToString();
				if (valuesList[i].Value.Field == BlackboardValue.FPVECTOR3VALUE) value = valuesList[i].Value.FPVector3Value->ToString();
				if (valuesList[i].Value.Field == BlackboardValue.ENTITYREFVALUE) value = valuesList[i].Value.EntityRefValue->ToString();

				dumpText += "\nName: " + bbAsset.GetEntryName(i) + ", Value: " + value;
			}

			Log.Info(dumpText);
		}
		#endregion
	}
}