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 pathMyPerTreeBotMemory.cs
More file actions
90 lines (77 loc) · 3.13 KB
/
Copy pathMyPerTreeBotMemory.cs
File metadata and controls
90 lines (77 loc) · 3.13 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
using System;
using System.Collections.Generic;
using VRage.Utils;
using System.Diagnostics;
using VRage.Collections;
using VRage.Game;
namespace Sandbox.Game.AI.BehaviorTree
{
public class MyPerTreeBotMemory
{
private List<MyBehaviorTreeNodeMemory> m_nodesMemory;
private Dictionary<MyStringId, MyBBMemoryValue> m_blackboardMemory;
public int NodesMemoryCount { get { return m_nodesMemory.Count; } }
public ListReader<MyBehaviorTreeNodeMemory> NodesMemory { get { return new ListReader<MyBehaviorTreeNodeMemory>(m_nodesMemory); } }
public IEnumerable<KeyValuePair<MyStringId, MyBBMemoryValue>> BBMemory { get { return m_blackboardMemory; } }
public MyPerTreeBotMemory()
{
m_nodesMemory = new List<MyBehaviorTreeNodeMemory>(20);
m_blackboardMemory = new Dictionary<MyStringId, MyBBMemoryValue>(20, MyStringId.Comparer);
}
public void AddNodeMemory(MyBehaviorTreeNodeMemory nodeMemory)
{
m_nodesMemory.Add(nodeMemory);
}
public void AddBlackboardMemoryInstance(string name, MyBBMemoryValue obj)
{
MyStringId stringId = MyStringId.GetOrCompute(name);
m_blackboardMemory.Add(stringId, obj);
}
public void RemoveBlackboardMemoryInstance(MyStringId name)
{
m_blackboardMemory.Remove(name);
}
public MyBehaviorTreeNodeMemory GetNodeMemoryByIndex(int index)
{
return m_nodesMemory[index];
}
public void ClearNodesData()
{
foreach (var nodeMemory in m_nodesMemory)
nodeMemory.ClearNodeState();
}
public void Clear()
{
m_nodesMemory.Clear();
m_blackboardMemory.Clear();
}
public bool TryGetFromBlackboard<T>(MyStringId id, out T value)
where T : MyBBMemoryValue
{
MyBBMemoryValue output = null;
bool found = m_blackboardMemory.TryGetValue(id, out output);
value = output as T;
return found;
}
public void SaveToBlackboard(MyStringId id, MyBBMemoryValue value)
{
Debug.Assert(id != MyStringId.NullOrEmpty, "Empty id for memory.");
if (id != MyStringId.NullOrEmpty)
{
m_blackboardMemory[id] = value;
}
}
public MyBBMemoryValue TrySaveToBlackboard(MyStringId id, Type type)
{
Debug.Assert(type.IsSubclassOf(typeof(MyBBMemoryValue)) || type == typeof(MyBBMemoryValue), "Invalid type is being saved to blackboard memory");
if (!(type.IsSubclassOf(typeof(MyBBMemoryValue)) || type == typeof(MyBBMemoryValue)))
return null;
Debug.Assert(type.GetConstructor(Type.EmptyTypes) != null, "Invalid type is being saved to blackboard memory");
if (type.GetConstructor(Type.EmptyTypes) == null)
return null;
var newMemoryChunk = Activator.CreateInstance(type) as MyBBMemoryValue;
m_blackboardMemory[id] = newMemoryChunk;
return newMemoryChunk;
}
}
}