tools/behaviourtree.h
00001 /* 00002 Crystal Space Entity Layer 00003 Copyright (C) 2004-2006 by Jorrit Tyberghein 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef __CEL_BEHAVIOUR_TREE__ 00021 #define __CEL_BEHAVIOUR_TREE__ 00022 00023 #include "behaviourlayer/behave.h" 00024 #include "tools/decorators.h" 00025 #include "tools/parameters.h" 00026 #include "tools/rewards.h" 00027 #include "tools/triggers.h" 00028 00029 //------------------------------------------------------------------------- 00030 // Behaviour tree statuses 00031 //------------------------------------------------------------------------- 00032 00040 enum BTStatus{ 00041 BT_NOT_STARTED = 0, // Node has not yet started 00042 BT_RUNNING, // Node is currently executing 00043 BT_SUCCESS, // Node completed succesfully, making changes to state as expected 00044 BT_FAIL_CLEAN, // Node failed, but cleanly (typically with no changes to state) 00045 BT_UNEXPECTED_ERROR // Node failed unexpectedly, possibly changing state (should be handled by parent) 00046 }; 00047 00048 //------------------------------------------------------------------------- 00049 // Behaviour tree nodes 00050 //------------------------------------------------------------------------- 00051 00079 struct iBTNode : public virtual iBase 00080 { 00081 SCF_INTERFACE (iBTNode, 0, 0, 1); 00082 00087 virtual BTStatus Execute (iCelParameterBlock* params, csRefArray<iBTNode>* BTStack = 0) = 0; 00088 00092 virtual bool AddChild (iBTNode* child) = 0; 00093 00097 virtual BTStatus GetStatus () = 0; 00098 00102 virtual void SetStatus (BTStatus newStatus) = 0; 00103 00107 virtual void SetName (csString nodeName) = 0; 00108 }; 00109 00110 //------------------------------------------------------------------------- 00111 // Specific behaviour tree node implementations. 00112 //------------------------------------------------------------------------- 00113 00118 struct iBTAction: public virtual iBase 00119 { 00120 SCF_INTERFACE (iBTAction, 0, 0, 1); 00121 00125 virtual void AddReward (iReward* reward) = 0; 00126 }; 00127 00132 struct iParameterCheckCondition: public virtual iBase 00133 { 00134 SCF_INTERFACE (iParameterCheckCondition, 0, 0, 1); 00135 00139 virtual void SetParameter (const char* parameter) = 0; 00140 00144 virtual void SetValue (const char* value) = 0; 00145 }; 00146 00147 00153 struct iTriggerFiredCondition: public virtual iBase 00154 { 00155 SCF_INTERFACE (iTriggerFiredCondition, 0, 0, 1); 00156 00160 virtual void SetTrigger (iTrigger* trigger) = 0; 00161 00169 virtual void SetFireOnce (bool once) = 0; 00170 }; 00171 00172 //------------------------------------------------------------------------- 00173 00177 #define CEL_DECLARE_BTNODE(className) \ 00178 class cel##className : public scfImplementation2< \ 00179 cel##className ,iBTNode, iComponent> \ 00180 { \ 00181 private: \ 00182 iObjectRegistry* object_reg; \ 00183 csRefArray<iBTNode> children; \ 00184 BTStatus status; \ 00185 csString name; \ 00186 public: \ 00187 cel##className (iBase* parent); \ 00188 virtual ~cel##className () { } \ 00189 virtual bool Initialize (iObjectRegistry*); \ 00190 virtual BTStatus Execute (iCelParameterBlock* params, csRefArray<iBTNode>* BTStack = 0); \ 00191 virtual bool AddChild (iBTNode* child); \ 00192 virtual BTStatus GetStatus (); \ 00193 virtual void SetStatus (BTStatus newStatus); \ 00194 virtual void SetName (csString nodeName); \ 00195 }; 00196 00200 #define CEL_IMPLEMENT_BTNODE(className) \ 00201 cel##className::cel##className (iBase* parent) \ 00202 : scfImplementationType (this, parent), object_reg(0) \ 00203 { \ 00204 } \ 00205 bool cel##className::Initialize ( \ 00206 iObjectRegistry* object_reg) \ 00207 { \ 00208 cel##className::object_reg = object_reg; \ 00209 cel##className::status = BT_NOT_STARTED; \ 00210 cel##className::name = "un-named node"; \ 00211 return true; \ 00212 } \ 00213 BTStatus cel##className::GetStatus () \ 00214 { \ 00215 return cel##className::status; \ 00216 } \ 00217 void cel##className::SetStatus (BTStatus newStatus) \ 00218 { \ 00219 cel##className::status = newStatus; \ 00220 } \ 00221 void cel##className::SetName (csString nodeName) \ 00222 { \ 00223 cel##className::name = nodeName; \ 00224 } 00225 00226 #endif // __CEL_BEHAVIOUR_TREE__
Generated for CEL: Crystal Entity Layer 2.1 by doxygen 1.6.1
