-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializedBehaviorTree.cs
More file actions
426 lines (366 loc) · 10.7 KB
/
SerializedBehaviorTree.cs
File metadata and controls
426 lines (366 loc) · 10.7 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright (c) 2020-2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Behavior-Tree
using System;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Profiling;
using Zor.BehaviorTree.Builder;
using Zor.BehaviorTree.Core;
using Zor.BehaviorTree.Core.Composites;
using Zor.BehaviorTree.Core.Decorators;
using Zor.BehaviorTree.Core.Leaves;
using Zor.BehaviorTree.Debugging;
using Zor.BehaviorTree.Serialization.SerializedBehaviors;
using Zor.SimpleBlackboard.Core;
using Object = UnityEngine.Object;
namespace Zor.BehaviorTree.Serialization
{
/// <summary>
/// Serialized behavior tree.
/// </summary>
[CreateAssetMenu(
menuName = "Behavior Tree/Serialized Behavior Tree",
fileName = "SerializedBehaviorTree",
order = 448
)]
public sealed class SerializedBehaviorTree : SerializedBehaviorTree_Base
{
/// <summary>
/// Serialized behaviors.
/// </summary>
[SerializeField] private SerializedBehaviorData[] m_SerializedBehaviorData;
/// <summary>
/// Root behavior index.
/// </summary>
[SerializeField] private int m_RootNode = -1;
/// <summary>
/// Node graph info for drawing a root node in a behavior tree graph view.
/// </summary>
[SerializeField, UsedImplicitly] private NodeGraphInfo m_RootGraphInfo = new NodeGraphInfo
{
position = new Vector2(100f, 450f)
};
/// <summary>
/// Behavior tree builder cache.
/// </summary>
private TreeBuilder m_treeBuilder;
/// <summary>
/// Creates a behavior tree out of its serialized data.
/// </summary>
/// <param name="blackboard">Behavior tree blackboard.</param>
/// <returns>Root of the created behavior tree.</returns>
/// <remarks>
/// On first call, it deserializes serialized data and caches it for later usage.
/// </remarks>
[Pure]
public override TreeRoot CreateTree(Blackboard blackboard)
{
Profiler.BeginSample("SerializedBehaviorTree.CreateTree(Blackboard)");
Profiler.BeginSample(name);
Deserialize();
BehaviorTreeDebug.Log($"Start creating a behavior tree {name}");
TreeRoot treeRoot = m_treeBuilder.Build(blackboard);
BehaviorTreeDebug.Log($"Finish creating a behavior tree {name}");
Profiler.EndSample();
Profiler.EndSample();
return treeRoot;
}
/// <summary>
/// Deserializes a whole tree.
/// </summary>
/// <remarks>
/// On first call, it deserializes serialized data and caches it for later usage.
/// </remarks>
private void Deserialize()
{
if (m_treeBuilder == null)
{
BehaviorTreeDebug.Log("Start deserializing a tree builder");
m_treeBuilder = new TreeBuilder();
Deserialize(m_RootNode);
BehaviorTreeDebug.Log("Finish deserializing a tree builder");
}
}
/// <summary>
/// Deserializes data at index <paramref name="index"/>.
/// </summary>
/// <param name="index">Data index.</param>
/// <exception cref="InvalidOperationException">
/// Thrown when there's no data at index <paramref name="index"/>.
/// </exception>
/// <remarks>
/// The method automatically deserializes children.
/// </remarks>
private void Deserialize(int index)
{
#if DEBUG
if (index < 0 || index >= m_SerializedBehaviorData.Length)
{
m_treeBuilder = null;
throw new InvalidOperationException($"Failed to get a serialized behavior at index {index}.");
}
#endif
SerializedBehaviorData data = m_SerializedBehaviorData[index];
data.serializedBehavior.AddBehavior(m_treeBuilder);
int[] children = data.childrenIndices;
for (int i = 0, count = children.Length; i < count; ++i)
{
int child = children[i];
if (child >= 0)
{
Deserialize(child);
}
}
m_treeBuilder.Complete();
}
[ContextMenu("Log")]
private void Log()
{
Deserialize();
Debug.Log($"SerializedBehaviorTree {name}\n{m_treeBuilder}");
}
#if UNITY_EDITOR
public event Action OnAssetChanged;
private void OnValidate()
{
ValidateNulls();
ValidateCopies();
ValidateChildrenCounts();
ValidateChildren();
ValidateSubAssets();
ValidateDependedAssets();
m_treeBuilder = null;
OnAssetChanged?.Invoke();
}
private void ValidateNulls()
{
for (int i = 0; i < m_SerializedBehaviorData.Length; ++i)
{
SerializedBehaviorData serializedBehaviorData = m_SerializedBehaviorData[i];
SerializedBehavior_Base serializedBehavior = serializedBehaviorData.serializedBehavior;
if (serializedBehavior == null)
{
RemoveBehavior(i);
--i;
}
}
}
private void ValidateCopies()
{
for (int i = 0; i < m_SerializedBehaviorData.Length; ++i)
{
SerializedBehaviorData serializedBehaviorData = m_SerializedBehaviorData[i];
SerializedBehavior_Base serializedBehavior = serializedBehaviorData.serializedBehavior;
bool copy = false;
for (int behaviorIndex = 0; behaviorIndex < i & !copy; ++behaviorIndex)
{
copy = serializedBehavior == m_SerializedBehaviorData[behaviorIndex].serializedBehavior;
}
if (copy)
{
RemoveBehavior(i);
--i;
}
}
}
private void ValidateChildrenCounts()
{
for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
{
SerializedBehaviorData serializedBehaviorData = m_SerializedBehaviorData[i];
int[] children = serializedBehaviorData.childrenIndices;
Type serializedType = serializedBehaviorData.serializedBehavior.serializedBehaviorType;
if (serializedType.IsSubclassOf(typeof(Leaf)))
{
if (children.Length > 0)
{
serializedBehaviorData.childrenIndices = new int[0];
}
}
else if (serializedType.IsSubclassOf(typeof(Decorator)))
{
if (children.Length > 1)
{
int child = children[0];
serializedBehaviorData.childrenIndices = new[] {child};
}
else if (children.Length < 1)
{
serializedBehaviorData.childrenIndices = new[] {-1};
}
}
else if (serializedType.IsSubclassOf(typeof(Composite)))
{
if (children.Length < 2)
{
int[] newChildren = {-1, -1};
if (children.Length == 1)
{
newChildren[0] = children[0];
}
serializedBehaviorData.childrenIndices = newChildren;
}
}
m_SerializedBehaviorData[i] = serializedBehaviorData;
}
}
private void ValidateChildren()
{
// Self and out of bounds validation
for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
{
SerializedBehaviorData serializedBehaviorData = m_SerializedBehaviorData[i];
int[] children = serializedBehaviorData.childrenIndices;
for (int childIndex = 0, childCount = children.Length; childIndex < childCount; ++childIndex)
{
int child = children[childIndex];
if (child == i || child >= count)
{
children[childIndex] = -1;
}
}
}
// Multiple links to the same node validation
for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
{
int parentIndex = -1;
int parentPort = -1;
for (int nodeIndex = 0; nodeIndex < count & parentIndex < 0; ++nodeIndex)
{
int[] children = m_SerializedBehaviorData[nodeIndex].childrenIndices;
for (int childIndex = 0, childCount = children.Length;
childIndex < childCount & parentIndex < 0;
++childIndex)
{
if (children[childIndex] == i)
{
parentIndex = nodeIndex;
parentPort = childIndex;
}
}
}
if (parentIndex < 0)
{
continue;
}
int[] parentChildren = m_SerializedBehaviorData[parentIndex].childrenIndices;
for (int parentPortIndex = parentPort + 1, parentPortCount = parentChildren.Length;
parentPortIndex < parentPortCount;
++parentPortIndex)
{
if (parentChildren[parentPortIndex] == i)
{
parentChildren[parentPortIndex] = -1;
}
}
for (int nodeIndex = parentIndex + 1; nodeIndex < count; ++nodeIndex)
{
int[] children = m_SerializedBehaviorData[nodeIndex].childrenIndices;
for (int childIndex = 0, childCount = children.Length; childIndex < childCount; ++childIndex)
{
if (children[childIndex] == i)
{
children[childIndex] = -1;
}
}
}
}
// Root out of bounds validation
if (m_RootNode >= m_SerializedBehaviorData.Length)
{
m_RootNode = -1;
}
if (m_RootNode < 0)
{
return;
}
// Multiple links with root validation
for (int i = 0, count = m_SerializedBehaviorData.Length; i < count; ++i)
{
int[] children = m_SerializedBehaviorData[i].childrenIndices;
for (int childIndex = 0, childCount = children.Length; childIndex < childCount; ++childIndex)
{
if (children[childIndex] == m_RootNode)
{
children[childIndex] = -1;
}
}
}
}
private void ValidateSubAssets()
{
string thisAssetPath = UnityEditor.AssetDatabase.GetAssetPath(this);
Object[] subAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(thisAssetPath);
for (int i = 0, count = subAssets.Length; i < count; ++i)
{
Object subAsset = subAssets[i];
if (subAsset == null)
{
continue;
}
bool needed = false;
for (int childIndex = 0, childCount = m_SerializedBehaviorData.Length;
childIndex < childCount & !needed;
++childIndex)
{
needed = m_SerializedBehaviorData[childIndex].serializedBehavior == subAsset;
}
if (!needed)
{
UnityEditor.AssetDatabase.RemoveObjectFromAsset(subAsset);
}
}
}
private void ValidateDependedAssets()
{
for (int i = 0; i < m_SerializedBehaviorData.Length; ++i)
{
SerializedBehavior_Base serializedBehavior = m_SerializedBehaviorData[i].serializedBehavior;
string path = UnityEditor.AssetDatabase.GetAssetPath(serializedBehavior);
Object mainAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(path);
if (mainAsset == null || mainAsset == this)
{
continue;
}
RemoveBehavior(i);
--i;
}
}
private void RemoveBehavior(int index)
{
int lastIndex = m_SerializedBehaviorData.Length - 1;
if (index < lastIndex)
{
Array.Copy(m_SerializedBehaviorData, index + 1, m_SerializedBehaviorData, index, lastIndex - index);
}
Array.Resize(ref m_SerializedBehaviorData, lastIndex);
for (int behaviorIndex = 0, behaviorCount = m_SerializedBehaviorData.Length;
behaviorIndex < behaviorCount;
++behaviorIndex)
{
int[] children = m_SerializedBehaviorData[behaviorIndex].childrenIndices;
for (int childIndex = 0, childCount = children.Length;
childIndex < childCount;
++childIndex)
{
if (children[childIndex] == index)
{
children[childIndex] = -1;
}
else if (children[childIndex] > index)
{
--children[childIndex];
}
}
}
if (m_RootNode == index)
{
m_RootNode = -1;
}
else if (m_RootNode > index)
{
--m_RootNode;
}
}
#endif
}
}