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

summaryrefslogtreecommitdiff
path: root/source/game/StarBehaviorState.hpp
blob: cac684cc24109f46e2504a878f507a95317061fe (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
#pragma once

#include "StarBehaviorDatabase.hpp"
#include "StarLua.hpp"

namespace Star {

STAR_CLASS(Blackboard);
STAR_CLASS(BehaviorState);
STAR_STRUCT(ActionState);
STAR_STRUCT(DecoratorState);
STAR_STRUCT(CompositeState);

STAR_EXCEPTION(BehaviorException, StarException);

extern List<NodeParameterType> BlackboardTypes;

class Blackboard {
public:
  Blackboard(LuaTable luaContext);

  LuaValue get(NodeParameterType type, String const& key) const;
  void set(NodeParameterType type, String const& key, LuaValue value);

  LuaTable parameters(StringMap<NodeParameter> const& nodeParameters, uint64_t nodeId);
  void setOutput(ActionNode const& node, LuaTable const& output);

  // takes the set of currently held ephemeral values
  Set<pair<NodeParameterType, String>> takeEphemerals();

  // clears any provided ephemerals that are not currently held
  void clearEphemerals(Set<pair<NodeParameterType, String>> ephemerals);
private:
  LuaTable m_luaContext;

  HashMap<uint64_t, LuaTable> m_parameters;
  HashMap<NodeParameterType, StringMap<LuaValue>> m_board;

  HashMap<NodeParameterType, StringMap<List<pair<uint64_t, String>>>> m_input;
  StringMap<List<pair<uint64_t, LuaTable>>> m_vectorNumberInput;

  Set<pair<NodeParameterType, String>> m_ephemeral;
};

typedef Maybe<Variant<ActionState,DecoratorState,CompositeState>> NodeState;
typedef shared_ptr<NodeState> NodeStatePtr;

typedef pair<LuaFunction, LuaThread> Coroutine;

enum class NodeStatus {
  Invalid,
  Success,
  Failure,
  Running
};

typedef LuaTupleReturn<NodeStatus, LuaValue> ActionReturn;
struct ActionState {
  LuaThread thread;
};

struct DecoratorState {
  DecoratorState(LuaThread thread);
  LuaThread thread;
  NodeStatePtr child;
};

struct CompositeState {
  CompositeState(size_t children);
  CompositeState(size_t children, size_t index);

  size_t index;
  List<NodeStatePtr> children;
};

class BehaviorState {
public:
  BehaviorState(BehaviorTreeConstPtr tree, LuaTable context, Maybe<BlackboardWeakPtr> blackboard = {});

  NodeStatus run(float dt);
  void clear();

  BlackboardWeakPtr blackboardPtr();
private:
  BlackboardPtr board();

  LuaThread nodeLuaThread(String const& funcName);

  NodeStatus runNode(BehaviorNode const& node, NodeState& state);

  NodeStatus runAction(ActionNode const& node, NodeState& state);
  NodeStatus runDecorator(DecoratorNode const& node, NodeState& state);

  NodeStatus runComposite(CompositeNode const& node, NodeState& state);
  NodeStatus runSequence(SequenceNode const& node, NodeState& state);
  NodeStatus runSelector(SelectorNode const& node, NodeState& state);
  NodeStatus runParallel(ParallelNode const& node, NodeState& state);
  NodeStatus runDynamic(DynamicNode const& node, NodeState& state);
  NodeStatus runRandomize(RandomizeNode const& node, NodeState& state);

  BehaviorTreeConstPtr m_tree;
  NodeState m_rootState;

  LuaTable m_luaContext;

  // The blackboard can either be created and owned by this behavior,
  // or a blackboard from another behavior can be used
  Variant<BlackboardPtr, BlackboardWeakPtr> m_board;

  // Keep threads here for recycling
  List<LuaThread> m_threads;
  StringMap<LuaFunction> m_functions;

  float m_lastDt;
};

}