#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 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 const& nodeParameters, uint64_t nodeId); void setOutput(ActionNode const& node, LuaTable const& output); // takes the set of currently held ephemeral values Set> takeEphemerals(); // clears any provided ephemerals that are not currently held void clearEphemerals(Set> ephemerals); private: LuaTable m_luaContext; HashMap m_parameters; HashMap> m_board; HashMap>>> m_input; StringMap>> m_vectorNumberInput; Set> m_ephemeral; }; typedef Maybe> NodeState; typedef shared_ptr NodeStatePtr; typedef pair Coroutine; enum class NodeStatus { Invalid, Success, Failure, Running }; typedef LuaTupleReturn 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 children; }; class BehaviorState { public: BehaviorState(BehaviorTreeConstPtr tree, LuaTable context, Maybe 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 m_board; // Keep threads here for recycling List m_threads; StringMap m_functions; float m_lastDt; }; }