Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarBehaviorDatabase.hpp
blob: 6892bf64ea4c2a8542528bbb0a85d9754fa98dec (plain)
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
#pragma once

#include "StarGameTypes.hpp"
#include "StarJson.hpp"

namespace Star {

STAR_CLASS(BehaviorDatabase);
STAR_STRUCT(ActionNode);
STAR_STRUCT(DecoratorNode);
STAR_STRUCT(SequenceNode);
STAR_STRUCT(SelectorNode);
STAR_STRUCT(ParallelNode);
STAR_STRUCT(DynamicNode);
STAR_STRUCT(RandomizeNode);
STAR_STRUCT(BehaviorTree);

typedef Variant<SequenceNode, SelectorNode, ParallelNode, DynamicNode, RandomizeNode> CompositeNode;

typedef Variant<ActionNode, DecoratorNode, CompositeNode, BehaviorTreeConstPtr> BehaviorNode;
typedef std::shared_ptr<const BehaviorNode> BehaviorNodeConstPtr;

enum class NodeParameterType : uint8_t {
  Json,
  Entity,
  Position,
  Vec2,
  Number,
  Bool,
  List,
  Table,
  String
};
extern EnumMap<NodeParameterType> const NodeParameterTypeNames;

typedef Variant<String, Json> NodeParameterValue;
typedef pair<NodeParameterType, NodeParameterValue> NodeParameter;
typedef pair<NodeParameterType, pair<Maybe<String>, bool>> NodeOutput;

NodeParameterValue nodeParameterValueFromJson(Json const& json);

Json jsonFromNodeParameter(NodeParameter const& parameter);
NodeParameter jsonToNodeParameter(Json const& json);

Json jsonFromNodeOutput(NodeOutput const& output);
NodeOutput jsonToNodeOutput(Json const& json);

enum class BehaviorNodeType : uint16_t {
  Action,
  Decorator,
  Composite,
  Module
};
extern EnumMap<BehaviorNodeType> const BehaviorNodeTypeNames;

enum class CompositeType : uint16_t {
  Sequence,
  Selector,
  Parallel,
  Dynamic,
  Randomize
};
extern EnumMap<CompositeType> const CompositeTypeNames;

// replaces global tags in nodeParameters in place
NodeParameterValue replaceBehaviorTag(NodeParameterValue const& parameter, StringMap<NodeParameterValue> const& treeParameters);
Maybe<String> replaceOutputBehaviorTag(Maybe<String> const& output, StringMap<NodeParameterValue> const& treeParameters);
void applyTreeParameters(StringMap<NodeParameter>& nodeParameters, StringMap<NodeParameterValue> const& treeParameters);

struct ActionNode {
  ActionNode(String name, StringMap<NodeParameter> parameters, StringMap<NodeOutput> output);

  String name;
  StringMap<NodeParameter> parameters;
  StringMap<NodeOutput> output;
};

struct DecoratorNode {
  DecoratorNode(String const& name, StringMap<NodeParameter> parameters, BehaviorNodeConstPtr child);

  String name;
  StringMap<NodeParameter> parameters;
  BehaviorNodeConstPtr child;
};

struct SequenceNode {
  SequenceNode(List<BehaviorNodeConstPtr> children);

  List<BehaviorNodeConstPtr> children;
};

struct SelectorNode {
  SelectorNode(List<BehaviorNodeConstPtr> children);

  List<BehaviorNodeConstPtr> children;
};

struct ParallelNode {
  ParallelNode(StringMap<NodeParameter>, List<BehaviorNodeConstPtr> children);

  int succeed;
  int fail;
  List<BehaviorNodeConstPtr> children;
};

struct DynamicNode {
  DynamicNode(List<BehaviorNodeConstPtr> children);

  List<BehaviorNodeConstPtr> children;
};

struct RandomizeNode {
  RandomizeNode(List<BehaviorNodeConstPtr> children);

  List<BehaviorNodeConstPtr> children;
};

struct BehaviorTree {
  BehaviorTree(String const& name, StringSet scripts, JsonObject const& parameters);

  String name;
  StringSet scripts;
  StringSet functions;
  JsonObject parameters;

  BehaviorNodeConstPtr root;
};

typedef std::shared_ptr<const BehaviorNode> BehaviorNodeConstPtr;

class BehaviorDatabase {
public:
  BehaviorDatabase();

  BehaviorTreeConstPtr behaviorTree(String const& name) const;
  BehaviorTreeConstPtr buildTree(Json const& config, StringMap<NodeParameterValue> const& overrides = {}) const;
  Json behaviorConfig(String const& name) const;

private:
  StringMap<Json> m_configs;
  StringMap<BehaviorTreeConstPtr> m_behaviors;
  StringMap<StringMap<NodeParameter>> m_nodeParameters;
  StringMap<StringMap<NodeOutput>> m_nodeOutput;

  void loadTree(String const& name);

  // constructs node variants
  CompositeNode compositeNode(Json const& config, StringMap<NodeParameter> parameters, StringMap<NodeParameterValue> const& treeParameters, BehaviorTree& tree) const;
  BehaviorNodeConstPtr behaviorNode(Json const& json, StringMap<NodeParameterValue> const& treeParameters, BehaviorTree& tree) const;
};

}