File size: 4,572 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
using Photon.Deterministic;
using System;

namespace Quantum
{
	public unsafe abstract partial class BTComposite : BTNode
	{
		public AssetRefBTNode[] Children;
		public AssetRefBTService[] Services;
		[BotSDKHidden] public AssetRefBTNode TopmostDecorator;

		public BTDataIndex CurrentChildIndex;

		protected BTNode[] _childInstances;
		protected BTService[] _serviceInstances;
		protected BTNode _topmostDecoratorInstance;

		public bool IsDynamic;

		public BTNode[] ChildInstances
		{
			get
			{
				return _childInstances;
			}
		}

		public BTService[] ServiceInstances
		{
			get
			{
				return _serviceInstances;
			}
		}

		public override BTNodeType NodeType
		{
			get
			{
				return BTNodeType.Composite;
			}
		}

		internal Int32 GetCurrentChild(Frame frame, BTAgent* agent)
		{
			Byte currentChild = (Byte)agent->GetIntData(frame, CurrentChildIndex.Index);
			return currentChild;
		}

		internal void SetCurrentChild(Frame frame, Int32 currentIndex, BTAgent* agent)
		{
			agent->SetIntData(frame, currentIndex, CurrentChildIndex.Index);
		}

		/// <summary>
		/// When a Composite node is Updated, it only increase the current child updated
		/// when the child results in either FAIL/SUCCESS. So we need this callback
		/// to be used when the child was RUNNING and then had some result, to properly increase the current 
		/// child ID
		/// </summary>
		/// <param name="btParams"></param>
		/// <param name="childResult"></param>
		internal virtual void ChildCompletedRunning(BTParams btParams, BTStatus childResult)
		{
		}

		public override void Init(Frame frame, AIBlackboardComponent* blackboard, BTAgent* agent)
		{
			base.Init(frame, blackboard, agent);

			agent->AddIntData(frame, 0);

			for (Int32 i = 0; i < Services.Length; i++)
			{
				BTService service = frame.FindAsset<BTService>(Services[i].Id);
				service.Init(frame, agent, blackboard);
			}
		}

		public override void OnEnter(BTParams btParams)
		{
			BTManager.OnNodeEnter?.Invoke(btParams.Entity, Guid.Value);
			SetCurrentChild(btParams.Frame, 0, btParams.Agent);
		}

		public override void OnEnterRunning(BTParams btParams)
		{
			var activeServicesList = btParams.Frame.ResolveList<AssetRefBTService>(btParams.Agent->ActiveServices);

			for (Int32 i = 0; i < _serviceInstances.Length; i++)
			{
				_serviceInstances[i].OnEnter(btParams);

				activeServicesList.Add(Services[i]);
			}

			if (IsDynamic == true)
			{
				var dynamicComposites = btParams.Frame.ResolveList<AssetRefBTComposite>(btParams.Agent->DynamicComposites);
				dynamicComposites.Add(this);
			}
		}

		public override void OnReset(BTParams btParams)
		{
			base.OnReset(btParams);

			OnExit(btParams);

			for (Int32 i = 0; i < _childInstances.Length; i++)
				_childInstances[i].OnReset(btParams);
		}

		public override void OnExit(BTParams btParams)
		{
			base.OnExit(btParams);

			BTManager.OnNodeExit?.Invoke(btParams.Entity, Guid.Value);

			var activeServicesList = btParams.Frame.ResolveList<AssetRefBTService>(btParams.Agent->ActiveServices);
			for (Int32 i = 0; i < _serviceInstances.Length; i++)
			{
				activeServicesList.Remove(Services[i]);
			}

			if (IsDynamic == true)
			{
				var dynamicComposites = btParams.Frame.ResolveList<AssetRefBTComposite>(btParams.Agent->DynamicComposites);
				dynamicComposites.Remove(this);
			}
		}

		public override bool OnDynamicRun(BTParams btParams)
		{
			if (_topmostDecoratorInstance != null)
			{
				return _topmostDecoratorInstance.OnDynamicRun(btParams);
			}

			return true;
		}


		public void AbortNodes(BTParams btParams, Int32 firstIndex = 0)
		{
			for (int i = firstIndex; i < _childInstances.Length; i++)
			{
				_childInstances[i].SetStatus(btParams.Frame, BTStatus.Abort, btParams.Agent);
			}
		}

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

			// Cache the child assets links
			_childInstances = new BTNode[Children.Length];
			for (Int32 i = 0; i < Children.Length; i++)
			{
				_childInstances[i] = (BTNode)resourceManager.GetAsset(Children[i].Id);
				_childInstances[i].Parent = this;
				_childInstances[i].ParentIndex = i;
			}

			// Cache the service assets links
			_serviceInstances = new BTService[Services.Length];
			for (Int32 i = 0; i < Services.Length; i++)
			{
				_serviceInstances[i] = (BTService)resourceManager.GetAsset(Services[i].Id);
			}

			if (TopmostDecorator != null)
			{
				_topmostDecoratorInstance = (BTDecorator)resourceManager.GetAsset(TopmostDecorator.Id);
			}
		}
	}
}