56 const ConditionPtr& _condition;
58 LUACondition(
const ConditionPtr& condition) :
59 _condition(condition) {
62 inline const ConditionPtr& getCondition()
const {
70 LUACondition *_condition;
71 std::vector<LUANode*> _children;
75 LUANode(
const TreeNodePtr& node, LUATree *tree,
const IAIFactory& aiFactory) :
76 _node(node), _condition(
nullptr), _tree(tree), _aiFactory(aiFactory) {
87 inline TreeNodePtr& getTreeNode() {
91 inline const TreeNodePtr& getTreeNode()
const {
95 inline void setCondition(LUACondition *condition) {
96 _condition = condition;
97 _node->setCondition(condition->getCondition());
100 inline const std::vector<LUANode*>& getChildren()
const {
104 inline LUACondition* getCondition()
const {
110 const TreeNodePtr& child = parser.getTreeNode(ctx.name);
114 LUANode *node =
new LUANode(child, _tree, _aiFactory);
115 _children.push_back(node);
116 _node->addChild(child);
128 _name(name), _ctx(ctx), _root(
nullptr) {
131 inline const IAIFactory& getAIFactory()
const{
132 return _ctx->getAIFactory();
135 inline bool setRoot(LUANode* root) {
136 if (_ctx->addTree(_name, root->getTreeNode())) {
144 inline const std::string& getName()
const {
148 inline LUANode* getRoot()
const {
154 return lua::LUA::getGlobalData<LUATreeLoader>(l,
"Loader");
157 static LUATree* luaGetTreeContext(lua_State * l,
int n) {
158 return lua::LUA::getUserData<LUATree>(l, n,
"Tree");
161 static LUANode* luaGetNodeContext(lua_State * l,
int n) {
162 return lua::LUA::getUserData<LUANode>(l, n,
"Node");
166 static LUACondition* luaGetConditionContext(lua_State * l,
int n) {
167 return lua::LUA::getUserData<LUACondition>(l, n,
"Condition");
171 static int luaMain_CreateTree(lua_State * l) {
173 const std::string name = luaL_checkstring(l, 1);
174 lua::LUA::newUserdata<LUATree>(l,
"Tree",
new LUATree(name, ctx));
178 static int luaTree_GC(lua_State * l) {
179 LUATree *tree = luaGetTreeContext(l, 1);
184 static int luaTree_ToString(lua_State * l) {
185 const LUATree *tree = luaGetTreeContext(l, 1);
186 lua_pushfstring(l,
"tree: %s [%s]", tree->getName().c_str(), tree->getRoot() ?
"root" :
"no root");
190 static int luaTree_GetName(lua_State * l) {
191 const LUATree *tree = luaGetTreeContext(l, 1);
192 lua_pushstring(l, tree->getName().c_str());
196 static int luaNode_GC(lua_State * l) {
197 LUANode *node = luaGetNodeContext(l, 1);
202 static int luaNode_ToString(lua_State * l) {
203 const LUANode *node = luaGetNodeContext(l, 1);
204 const LUACondition* condition = node->getCondition();
205 lua_pushfstring(l,
"node: %d children [condition: %s]", (
int)node->getChildren().size(),
206 condition ? condition->getCondition()->getName().c_str() :
"no condition");
210 static int luaNode_GetName(lua_State * l) {
211 const LUANode *node = luaGetNodeContext(l, 1);
212 lua_pushstring(l, node->getTreeNode()->getName().c_str());
216 static int luaTree_CreateRoot(lua_State * l) {
217 LUATree *ctx = luaGetTreeContext(l, 1);
218 const std::string
id = luaL_checkstring(l, 2);
219 const std::string name = luaL_checkstring(l, 3);
222 const TreeNodePtr& node = parser.getTreeNode(name);
224 return lua::LUA::returnError(l,
"Could not create a node for " +
id);
227 LUANode* udata = lua::LUA::newUserdata<LUANode>(l,
"Node",
new LUANode(node, ctx, ctx->getAIFactory()));
228 if (!ctx->setRoot(udata)) {
230 return lua::LUA::returnError(l, loader->
getError());
235 static int luaNode_AddNode(lua_State * l) {
236 LUANode *node = luaGetNodeContext(l, 1);
237 const std::string
id = luaL_checkstring(l, 2);
238 const std::string name = luaL_checkstring(l, 3);
241 LUANode* udata = lua::LUA::newUserdata<LUANode>(l,
"Node", node->addChild(
id, factoryCtx));
242 if (udata ==
nullptr) {
243 return lua::LUA::returnError(l,
"Could not create a node for " +
id);
248 static int luaNode_SetCondition(lua_State * l) {
250 LUANode *node = luaGetNodeContext(l, 1);
251 const std::string conditionExpression = luaL_checkstring(l, 2);
254 const ConditionPtr& condition = parser.getCondition();
256 return lua::LUA::returnError(l,
"Could not create a condition for " + conditionExpression +
": " + parser.getError());
259 LUACondition* udata = lua::LUA::newUserdata<LUACondition>(l,
"Condition",
new LUACondition(condition));
260 node->setCondition(udata);
272 bool init(
const std::string& luaString) {
276 luaL_Reg createTree = {
"createTree", luaMain_CreateTree };
277 luaL_Reg eof = {
nullptr,
nullptr };
278 luaL_Reg funcs[] = { createTree, eof };
281 tree.addFunction(
"createRoot", luaTree_CreateRoot);
282 tree.addFunction(
"getName", luaTree_GetName);
283 tree.addFunction(
"__gc", luaTree_GC);
284 tree.addFunction(
"__tostring", luaTree_ToString);
287 node.addFunction(
"addNode", luaNode_AddNode);
288 node.addFunction(
"getName", luaNode_GetName);
289 node.addFunction(
"setCondition", luaNode_SetCondition);
290 node.addFunction(
"__gc", luaNode_GC);
291 node.addFunction(
"__tostring", luaNode_ToString);
293 lua.reg(
"AI", funcs);
295 if (!lua.load(luaString)) {
296 setError(
"%s", lua.error().c_str());
303 setError(
"%s", lua.error().c_str());
310 empty = _treeMap.empty();
313 setError(
"No behaviour trees specified");
Implementation of ITreeLoader that gets its data from a lua script.
Definition: LUATreeLoader.h:50
Transforms the string representation of a TreeNode with all its parameters into a TreeNode instance...
Definition: TreeNodeParser.h:22
Context for ITreeNodeFactory.
Definition: AIFactories.h:15
void std::string getError() const
Gives access to the last error state of the ITreeLoader.
Definition: ITreeLoader.h:111
bool execute(const std::string &function, int returnValues=0)
Definition: LUA.h:170
Definition: IAIFactory.h:39
This class must be extended to load behaviour trees. The contract here is that the parsing only happe...
Definition: ITreeLoader.h:24
Transforms the string representation of a condition with all its sub conditions and parameters into a...
Definition: ConditionParser.h:23
bool init(const std::string &luaString)
this will initialize the loader once with all the defined behaviours from the given lua string...
Definition: LUATreeLoader.h:272