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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
/***************************************** The Meta Behavior Tree Library Copyright (c) 2017 David Feng Distributed under the MIT License. *****************************************/ #ifndef TEST_HPP #define TEST_HPP #include "behavior_tree.hpp" #include <boost/mpl/vector.hpp> #include <random> #include <time.h> #include <iostream> namespace test { /* Define a custom player class with the following behavior tree configuration , in which decorator_node::execute, decorator_node::report and leaf_node::step all return a random status. ___________________________________Sel0_________________________________ / | | \ Dec0 _________Sel1__________ _______Seq2________ Leaf14 | / | \ / | \ Dec1 Seq0 Seq1 Leaf7 Dec6 Dec7 Leaf13 | / \ / \ | | Leaf0 Dec2 Dec3 Dec4 Dec5 Seq3 Sel4 | | | | / \ / \ Leaf1 Sel2 Sel3 Leaf6 Leaf8 Leaf9 Leaf10 Seq4 / \ / \ / \ Leaf2 Leaf3 Leaf4 Leaf5 Leaf11 Leaf12 */ class player_def : public mbt::behavior_tree_def { static mbt::status Rand() { return (mbt::status)(rand() % 3); } public: // Define sequence nodes class Seq0 : public mbt::sequence_node { public: Seq0() { dtor_bind = std::bind(std::mem_fn(&Seq0::dtor), this); std::cout << "Seq0::Ctor" << std::endl; } ~Seq0() { dtor_sequence_node(); } void dtor() { std::cout << "Seq0::Dtor" << std::endl; dtor_sequence_node(); } }; class Seq1 : public mbt::sequence_node { public: Seq1() { dtor_bind = std::bind(std::mem_fn(&Seq1::dtor), this); std::cout << "Seq1::Ctor" << std::endl; } ~Seq1() { dtor_sequence_node(); } void dtor() { std::cout << "Seq1::Dtor" << std::endl; dtor_sequence_node(); } }; class Seq2 : public mbt::sequence_node { public: Seq2() { dtor_bind = std::bind(std::mem_fn(&Seq2::dtor), this); std::cout << "Seq2::Ctor" << std::endl; } ~Seq2() { dtor_sequence_node(); } void dtor() { std::cout << "Seq2::Dtor" << std::endl; dtor_sequence_node(); } }; class Seq3 : public mbt::sequence_node { public: Seq3() { dtor_bind = std::bind(std::mem_fn(&Seq3::dtor), this); std::cout << "Seq3::Ctor" << std::endl; } ~Seq3() { dtor_sequence_node(); } void dtor() { std::cout << "Seq3::Dtor" << std::endl; dtor_sequence_node(); } }; class Seq4 : public mbt::sequence_node { public: Seq4() { dtor_bind = std::bind(std::mem_fn(&Seq4::dtor), this); std::cout << "Seq4::Ctor" << std::endl; } ~Seq4() { dtor_sequence_node(); } void dtor() { std::cout << "Seq4::Dtor" << std::endl; dtor_sequence_node(); } }; // Define selector nodes class Sel0 : public mbt::selector_node { public: Sel0() { dtor_bind = std::bind(std::mem_fn(&Sel0::dtor), this); std::cout << "Sel0::Ctor" << std::endl; } ~Sel0() { dtor_selector_node(); } void dtor() { std::cout << "Sel0::Dtor" << std::endl; dtor_selector_node(); } }; class Sel1 : public mbt::selector_node { public: Sel1() { dtor_bind = std::bind(std::mem_fn(&Sel1::dtor), this); std::cout << "Sel1::Ctor" << std::endl; } ~Sel1() { dtor_selector_node(); } void dtor() { std::cout << "Sel1::Dtor" << std::endl; dtor_selector_node(); } }; class Sel2 : public mbt::selector_node { public: Sel2() { dtor_bind = std::bind(std::mem_fn(&Sel2::dtor), this); std::cout << "Sel2::Ctor" << std::endl; } ~Sel2() { dtor_selector_node(); } void dtor() { std::cout << "Sel2::Dtor" << std::endl; dtor_selector_node(); } }; class Sel3 : public mbt::selector_node { public: Sel3() { dtor_bind = std::bind(std::mem_fn(&Sel3::dtor), this); std::cout << "Sel3::Ctor" << std::endl; } ~Sel3() { dtor_selector_node(); } void dtor() { std::cout << "Sel3::Dtor" << std::endl; dtor_selector_node(); } }; class Sel4 : public mbt::selector_node { public: Sel4() { dtor_bind = std::bind(std::mem_fn(&Sel4::dtor), this); std::cout << "Sel4::Ctor" << std::endl; } ~Sel4() { dtor_selector_node(); } void dtor() { std::cout << "Sel4::Dtor" << std::endl; dtor_selector_node(); } }; // Define decorator nodes class Dec0 : public mbt::decorator_node { public: Dec0() { dtor_bind = std::bind(std::mem_fn(&Dec0::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec0::execute), this); report_bind = std::bind(std::mem_fn(&Dec0::report), this, std::placeholders::_1); std::cout << "Dec0::Ctor" << std::endl; } ~Dec0() { dtor_decorator_node(); } void dtor() { std::cout << "Dec0::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec0::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec0::report(" << s << ")" << std::endl; return Rand(); } }; class Dec1 : public mbt::decorator_node { public: Dec1() { dtor_bind = std::bind(std::mem_fn(&Dec1::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec1::execute), this); report_bind = std::bind(std::mem_fn(&Dec1::report), this, std::placeholders::_1); std::cout << "Dec1::Ctor" << std::endl; } ~Dec1() { dtor_decorator_node(); } void dtor() { std::cout << "Dec1::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec1::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec1::report(" << s << ")" << std::endl; return Rand(); } }; class Dec2 : public mbt::decorator_node { public: Dec2() { dtor_bind = std::bind(std::mem_fn(&Dec2::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec2::execute), this); report_bind = std::bind(std::mem_fn(&Dec2::report), this, std::placeholders::_1); std::cout << "Dec2::Ctor" << std::endl; } ~Dec2() { dtor_decorator_node(); } void dtor() { std::cout << "Dec2::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec2::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec2::report(" << s << ")" << std::endl; return Rand(); } }; class Dec3 : public mbt::decorator_node { public: Dec3() { dtor_bind = std::bind(std::mem_fn(&Dec3::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec3::execute), this); report_bind = std::bind(std::mem_fn(&Dec3::report), this, std::placeholders::_1); std::cout << "Dec3::Ctor" << std::endl; } ~Dec3() { dtor_decorator_node(); } void dtor() { std::cout << "Dec3::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec3::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec3::report(" << s << ")" << std::endl; return Rand(); } }; class Dec4 : public mbt::decorator_node { public: Dec4() { dtor_bind = std::bind(std::mem_fn(&Dec4::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec4::execute), this); report_bind = std::bind(std::mem_fn(&Dec4::report), this, std::placeholders::_1); std::cout << "Dec4::Ctor" << std::endl; } ~Dec4() { dtor_decorator_node(); } void dtor() { std::cout << "Dec4::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec4::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec4::report(" << s << ")" << std::endl; return Rand(); } }; class Dec5 : public mbt::decorator_node { public: Dec5() { dtor_bind = std::bind(std::mem_fn(&Dec5::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec5::execute), this); report_bind = std::bind(std::mem_fn(&Dec5::report), this, std::placeholders::_1); std::cout << "Dec5::Ctor" << std::endl; } ~Dec5() { dtor_decorator_node(); } void dtor() { std::cout << "Dec5::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec5::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec5::report(" << s << ")" << std::endl; return Rand(); } }; class Dec6 : public mbt::decorator_node { public: Dec6() { dtor_bind = std::bind(std::mem_fn(&Dec6::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec6::execute), this); report_bind = std::bind(std::mem_fn(&Dec6::report), this, std::placeholders::_1); std::cout << "Dec6::Ctor" << std::endl; } ~Dec6() { dtor_decorator_node(); } void dtor() { std::cout << "Dec6::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec6::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec6::report(" << s << ")" << std::endl; return Rand(); } }; class Dec7 : public mbt::decorator_node { public: Dec7() { dtor_bind = std::bind(std::mem_fn(&Dec7::dtor), this); execute_bind = std::bind(std::mem_fn(&Dec7::execute), this); report_bind = std::bind(std::mem_fn(&Dec7::report), this, std::placeholders::_1); std::cout << "Dec7::Ctor" << std::endl; } ~Dec7() { dtor_decorator_node(); } void dtor() { std::cout << "Dec7::Dtor" << std::endl; dtor_decorator_node(); } mbt::status execute() { std::cout << "Dec7::execute()" << std::endl; return Rand(); } mbt::status report(mbt::status s) { std::cout << "Dec7::report(" << s << ")" << std::endl; return Rand(); } }; // Define leaf nodes class Leaf0 : public mbt::leaf_node { public: Leaf0() { dtor_bind = std::bind(std::mem_fn(&Leaf0::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf0::step), this); std::cout << "Leaf0::Ctor" << std::endl; } ~Leaf0() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf0::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf0::step()" << std::endl; return Rand(); } }; class Leaf1 : public mbt::leaf_node { public: Leaf1() { dtor_bind = std::bind(std::mem_fn(&Leaf1::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf1::step), this); std::cout << "Leaf1::Ctor" << std::endl; } ~Leaf1() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf1::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf1::step()" << std::endl; return Rand(); } }; class Leaf2 : public mbt::leaf_node { public: Leaf2() { dtor_bind = std::bind(std::mem_fn(&Leaf2::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf2::step), this); std::cout << "Leaf2::Ctor" << std::endl; } ~Leaf2() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf2::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf2::step()" << std::endl; return Rand(); } }; class Leaf3 : public mbt::leaf_node { public: Leaf3() { dtor_bind = std::bind(std::mem_fn(&Leaf3::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf3::step), this); std::cout << "Leaf3::Ctor" << std::endl; } ~Leaf3() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf3::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf3::step()" << std::endl; return Rand(); } }; class Leaf4 : public mbt::leaf_node { public: Leaf4() { dtor_bind = std::bind(std::mem_fn(&Leaf4::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf4::step), this); std::cout << "Leaf4::Ctor" << std::endl; } ~Leaf4() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf4::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf4::step()" << std::endl; return Rand(); } }; class Leaf5 : public mbt::leaf_node { public: Leaf5() { dtor_bind = std::bind(std::mem_fn(&Leaf5::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf5::step), this); std::cout << "Leaf5::Ctor" << std::endl; } ~Leaf5() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf5::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf5::step()" << std::endl; return Rand(); } }; class Leaf6 : public mbt::leaf_node { public: Leaf6() { dtor_bind = std::bind(std::mem_fn(&Leaf6::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf6::step), this); std::cout << "Leaf6::Ctor" << std::endl; } ~Leaf6() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf6::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf6::step()" << std::endl; return Rand(); } }; class Leaf7 : public mbt::leaf_node { public: Leaf7() { dtor_bind = std::bind(std::mem_fn(&Leaf7::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf7::step), this); std::cout << "Leaf7::Ctor" << std::endl; } ~Leaf7() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf7::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf7::step()" << std::endl; return Rand(); } }; class Leaf8 : public mbt::leaf_node { public: Leaf8() { dtor_bind = std::bind(std::mem_fn(&Leaf8::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf8::step), this); std::cout << "Leaf8::Ctor" << std::endl; } ~Leaf8() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf8::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf8::step()" << std::endl; return Rand(); } }; class Leaf9 : public mbt::leaf_node { public: Leaf9() { dtor_bind = std::bind(std::mem_fn(&Leaf9::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf9::step), this); std::cout << "Leaf9::Ctor" << std::endl; } ~Leaf9() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf9::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf9::step()" << std::endl; return Rand(); } }; class Leaf10 : public mbt::leaf_node { public: Leaf10() { dtor_bind = std::bind(std::mem_fn(&Leaf10::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf10::step), this); std::cout << "Leaf10::Ctor" << std::endl; } ~Leaf10() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf10::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf10::step()" << std::endl; return Rand(); } }; class Leaf11 : public mbt::leaf_node { public: Leaf11() { dtor_bind = std::bind(std::mem_fn(&Leaf11::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf11::step), this); std::cout << "Leaf11::Ctor" << std::endl; } ~Leaf11() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf11::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf11::step()" << std::endl; return Rand(); } }; class Leaf12 : public mbt::leaf_node { public: Leaf12() { dtor_bind = std::bind(std::mem_fn(&Leaf12::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf12::step), this); std::cout << "Leaf12::Ctor" << std::endl; } ~Leaf12() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf12::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf12::step()" << std::endl; return Rand(); } }; class Leaf13 : public mbt::leaf_node { public: Leaf13() { dtor_bind = std::bind(std::mem_fn(&Leaf13::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf13::step), this); std::cout << "Leaf13::Ctor" << std::endl; } ~Leaf13() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf13::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf13::step()" << std::endl; return Rand(); } }; class Leaf14 : public mbt::leaf_node { public: Leaf14() { dtor_bind = std::bind(std::mem_fn(&Leaf14::dtor), this); step_bind = std::bind(std::mem_fn(&Leaf14::step), this); std::cout << "Leaf14::Ctor" << std::endl; } ~Leaf14() { dtor_leaf_node(); } void dtor() { std::cout << "Leaf14::Dtor" << std::endl; dtor_leaf_node(); } mbt::status step() { std::cout << "Leaf14::step()" << std::endl; return Rand(); } }; public: // Assign some composite node to root_node typedef Sel0 root_node; // Define config_table struct config_table: boost::mpl::vector< // composite_node children_nodes // +----------------+-----------------------------------------+ boost::mpl::vector< Sel0, Dec0,Dec1,Leaf0,Sel1,Seq2,Leaf14 >, boost::mpl::vector< Seq0, Dec2,Leaf1,Dec3,Sel2 >, boost::mpl::vector< Sel2, Leaf2,Leaf3 >, boost::mpl::vector< Sel1, Seq0,Seq1,Leaf7 >, boost::mpl::vector< Seq1, Dec4,Sel3,Dec5,Leaf6 >, boost::mpl::vector< Sel3, Leaf4,Leaf5 >, boost::mpl::vector< Seq2, Dec6,Seq3,Dec7,Sel4,Leaf13 >, boost::mpl::vector< Seq3, Leaf8,Leaf9 >, boost::mpl::vector< Sel4, Leaf10,Seq4 >, boost::mpl::vector< Seq4, Leaf11,Leaf12 > > {}; /* ___________________________________Sel0_________________________________ / | | \ Dec0 _________Sel1__________ _______Seq2________ Leaf14 | / | \ / | \ Dec1 Seq0 Seq1 Leaf7 Dec6 Dec7 Leaf13 | / \ / \ | | Leaf0 Dec2 Dec3 Dec4 Dec5 Seq3 Sel4 | | | | / \ / \ Leaf1 Sel2 Sel3 Leaf6 Leaf8 Leaf9 Leaf10 Seq4 / \ / \ / \ Leaf2 Leaf3 Leaf4 Leaf5 Leaf11 Leaf12 */ protected: // Overwrite member functions void execute() { std::cout << "player::execute()" << std::endl; } void report(mbt::status s) { std::cout << "player::report(" << s << ")" << std::endl; } void notify(mbt::base_node * n) { std::cout << "player::notify(" << n << ")" << std::endl; } void reset() { std::cout << "====== player::reset() ======" << std::endl; } }; // Define the player class typedef mbt::behavior_tree<player_def> player; // Driver function for testing void Run() { srand((size_t)time(0)); player p; for (size_t i = 0; i < 200; ++i) p.play(); p.reset(); for (size_t i = 0; i < 200; ++i) p.play(); } } #endif |
Copyright © 2016-2020 Qualgame, LLC