This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 888
Expand file tree
/
Copy pathMyBehaviorTree.cs
More file actions
115 lines (99 loc) · 3.56 KB
/
Copy pathMyBehaviorTree.cs
File metadata and controls
115 lines (99 loc) · 3.56 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
using Sandbox.Definitions;
using System.Collections.Generic;
using System.Text;
using VRage.Utils;
namespace Sandbox.Game.AI.BehaviorTree
{
public class MyBehaviorTree
{
public class MyBehaviorTreeDesc
{
public List<MyBehaviorTreeNode> Nodes { get; private set; }
public HashSet<MyStringId> ActionIds { get; private set; }
public int MemorableNodesCounter { get; set; }
public MyBehaviorTreeDesc()
{
Nodes = new List<MyBehaviorTreeNode>(20);
ActionIds = new HashSet<MyStringId>(MyStringId.Comparer);
MemorableNodesCounter = 0;
}
}
private static List<MyStringId> m_tmpHelper = new List<MyStringId>();
private MyBehaviorTreeNode m_root;
private MyBehaviorTreeDesc m_treeDesc;
public int TotalNodeCount { get { return m_treeDesc.Nodes.Count; } }
private MyBehaviorDefinition m_behaviorDefinition;
public MyBehaviorDefinition BehaviorDefinition { get { return m_behaviorDefinition; } }
public string BehaviorTreeName { get { return m_behaviorDefinition.Id.SubtypeName; } }
public MyStringHash BehaviorTreeId { get { return m_behaviorDefinition.Id.SubtypeId; } }
public MyBehaviorTree(MyBehaviorDefinition def)
{
m_behaviorDefinition = def;
m_treeDesc = new MyBehaviorTreeDesc();
}
public void ReconstructTree(MyBehaviorDefinition def)
{
m_behaviorDefinition = def;
Construct();
}
public void Construct()
{
ClearData();
m_root = new MyBehaviorTreeRoot();
m_root.Construct(m_behaviorDefinition.FirstNode, m_treeDesc);
}
public void ClearData()
{
m_treeDesc.MemorableNodesCounter = 0;
m_treeDesc.ActionIds.Clear();
m_treeDesc.Nodes.Clear();
}
public void Tick(IMyBot bot)
{
m_root.Tick(bot, bot.BotMemory.CurrentTreeBotMemory);
}
public void CallPostTickOnPath(IMyBot bot, MyPerTreeBotMemory botTreeMemory, IEnumerable<int> postTickNodes)
{
foreach (var nodeIdx in postTickNodes)
{
m_treeDesc.Nodes[nodeIdx].PostTick(bot, botTreeMemory);
}
}
public bool IsCompatibleWithBot(ActionCollection botActions)
{
foreach (MyStringId actionId in m_treeDesc.ActionIds)
{
if (!botActions.ContainsActionDesc(actionId))
{
m_tmpHelper.Add(actionId);
}
}
if (m_tmpHelper.Count > 0)
{
StringBuilder failText = new StringBuilder("Error! The behavior tree is not compatible with the bot. Missing bot actions: ");
foreach (var action in m_tmpHelper)
{
failText.Append(action.ToString());
failText.Append(", ");
}
System.Diagnostics.Debug.Fail(failText.ToString());
m_tmpHelper.Clear();
return false;
}
else
{
return true;
}
}
public MyBehaviorTreeNode GetNodeByIndex(int index)
{
if (index >= m_treeDesc.Nodes.Count)
return null;
return m_treeDesc.Nodes[index];
}
public override int GetHashCode()
{
return m_root.GetHashCode();
}
}
}