-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerializedBrain.cs
161 lines (134 loc) · 4.5 KB
/
SerializedBrain.cs
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
// Copyright (c) 2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/UtilityAI
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Profiling;
using Zor.SimpleBlackboard.Core;
using Zor.UtilityAI.Builder;
using Zor.UtilityAI.Core;
using Zor.UtilityAI.Debugging;
using Zor.UtilityAI.Serialization.SerializedActions;
using Zor.UtilityAI.Serialization.SerializedConsiderations;
namespace Zor.UtilityAI.Serialization
{
/// <summary>
/// Serialized <see cref="Brain"/>.
/// </summary>
[CreateAssetMenu(
menuName = "Utility AI/Serialized Brain",
fileName = "SerializedBrain",
order = 449
)]
public sealed class SerializedBrain : SerializedBrain_Base
{
[SerializeField] private SerializedAction_Base[] m_SerializedActions;
[SerializeField] private SerializedConsideration_Base[] m_SerializedConsiderations;
[SerializeField] private ConsiderationIndices[] m_ConsiderationIndices;
[SerializeField] private BrainSettings m_BrainSettings;
/// <summary>
/// Cached <see cref="BrainBuilder"/>.
/// Serialized data is deserialized only once on the first call of <see cref="CreateBrain"/> and cached here.
/// </summary>
private BrainBuilder m_builder;
public override Brain CreateBrain(Blackboard blackboard)
{
Profiler.BeginSample("SerializedBrain.CreateBrain");
Profiler.BeginSample(name);
Deserialize();
Brain brain = m_builder.Build(blackboard, m_BrainSettings);
Profiler.EndSample();
Profiler.EndSample();
return brain;
}
/// <summary>
/// Tries to deserialize data. It does nothing if the data has already been deserialized.
/// </summary>
private void Deserialize()
{
if (m_builder != null)
{
return;
}
Profiler.BeginSample("SerializedBrain.Deserialize");
UtilityAIDebug.Log("Start deserializing brain");
m_builder = new BrainBuilder();
for (int actionIndex = 0, actionCount = m_SerializedActions.Length;
actionIndex < actionCount;
++actionIndex)
{
SerializedAction_Base serializedActionBase = m_SerializedActions[actionIndex];
UtilityAIDebug.Log("Deserialize Action");
UtilityAIDebug.Log(serializedActionBase.actionType.FullName);
serializedActionBase.AddAction(m_builder);
int[] considerationIndices = m_ConsiderationIndices[actionIndex].considerations;
UtilityAIDebug.Log("Deserialize Considerations");
for (int considerationIndex = 0, considerationCount = considerationIndices.Length;
considerationIndex < considerationCount;
++considerationIndex)
{
SerializedConsideration_Base serializedConsiderationBase =
m_SerializedConsiderations[considerationIndices[considerationIndex]];
UtilityAIDebug.Log(serializedConsiderationBase.considerationType.FullName);
serializedConsiderationBase.AddConsideration(m_builder);
}
}
UtilityAIDebug.Log("Finish deserializing brain");
Profiler.EndSample();
}
[ContextMenu("Log")]
private void Log()
{
Deserialize();
Debug.Log($"SerializedBehaviorTree {name}\n{m_builder}");
}
private void OnValidate()
{
m_builder = null;
int serializedActionCount = m_SerializedActions.Length;
Array.Resize(ref m_ConsiderationIndices, serializedActionCount);
for (int actionIndex = 0, actionCount = m_ConsiderationIndices.Length;
actionIndex < actionCount;
++actionIndex)
{
int[] considerationIndices = m_ConsiderationIndices[actionIndex].considerations;
for (int considerationIndex = 0, considerationCount = considerationIndices.Length;
considerationIndex < considerationCount;
++considerationIndex)
{
int consideration = considerationIndices[considerationIndex];
if (consideration < 0 || consideration >= serializedActionCount)
{
RemoveElement(ref considerationIndices, considerationIndex--);
considerationCount = considerationIndices.Length;
}
}
}
}
private static void RemoveElement(ref int[] array, int element)
{
for (int i = element, count = array.Length - 1; i < count; ++i)
{
array[i] = array[i + 1];
}
Array.Resize(ref array, array.Length - 1);
}
/// <summary>
/// Action to considerations binding.
/// </summary>
[Serializable]
private struct ConsiderationIndices
{
[SerializeField] private int[] m_Considerations;
/// <summary>
/// <see cref="Consideration"/> indices.
/// </summary>
[NotNull]
public int[] considerations
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_Considerations;
}
}
}
}