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

namespace Quantum
{
	[Serializable]
	public unsafe partial class BTLoop : BTDecorator
	{
		public Int32 LoopIterations;
		public Boolean LoopForever;
		public FP LoopTimeout = -FP._1;

		public BTDataIndex StartTimeIndex;
		public BTDataIndex IterationCountIndex;

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

			agent->AddFPData(frame, 0);
			agent->AddIntData(frame, 0);
		}

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

			var frame = btParams.Frame;
			var currentTime = frame.DeltaTime * frame.Number;

			btParams.Agent->SetFPData(frame, currentTime, StartTimeIndex.Index);
			btParams.Agent->SetIntData(frame, 0, IterationCountIndex.Index);
		}

		protected override BTStatus OnUpdate(BTParams btParams)
		{
			var frame = btParams.Frame;

			int iteration = btParams.Agent->GetIntData(frame, IterationCountIndex.Index) + 1;
			btParams.Agent->SetIntData(frame, iteration, IterationCountIndex.Index);

			if (DryRun(btParams) == false)
			{
				return BTStatus.Success;
			}

			var childResult = BTStatus.Failure;
			if (_childInstance != null)
			{
				_childInstance.SetStatus(btParams.Frame, BTStatus.Inactive, btParams.Agent);
				childResult = _childInstance.RunUpdate(btParams);
			}

			return childResult;
		}

		public override Boolean DryRun(BTParams btParams)
		{
			if (LoopForever && LoopTimeout < FP._0)
			{
				return true;
			}
			else if (LoopForever)
			{
				var frame = btParams.Frame;
				FP startTime = btParams.Agent->GetFPData(frame, StartTimeIndex.Index);

				var currentTime = frame.DeltaTime * frame.Number;
				if (currentTime < startTime + LoopTimeout)
				{
					return true;
				}
			}
			else
			{
				var frame = btParams.Frame;
				int iteration = btParams.Agent->GetIntData(frame, IterationCountIndex.Index);
				if (iteration <= LoopIterations)
				{
					return true;
				}
			}

			return false;
		}
	}
}