ActiveSelector.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Guy Allard
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "ActiveSelector.h"
  23. #include "console/consoleTypes.h"
  24. using namespace BadBehavior;
  25. //------------------------------------------------------------------------------
  26. // Active selector node
  27. //------------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT(ActiveSelector);
  29. ActiveSelector::ActiveSelector()
  30. : mRecheckFrequency(0)
  31. {
  32. }
  33. Task *ActiveSelector::createTask(SimObject &owner, BehaviorTreeRunner &runner)
  34. {
  35. return new ActiveSelectorTask(*this, owner, runner);
  36. }
  37. void ActiveSelector::initPersistFields()
  38. {
  39. addGroup( "Behavior" );
  40. addField( "recheckFrequency", TypeS32, Offset(mRecheckFrequency, ActiveSelector),
  41. "@brief The minimum time period in milliseconds to wait between re-evaluations of higher priority branches.");
  42. endGroup( "Behavior" );
  43. Parent::initPersistFields();
  44. }
  45. //------------------------------------------------------------------------------
  46. // Active selector task
  47. //------------------------------------------------------------------------------
  48. ActiveSelectorTask::ActiveSelectorTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
  49. : Parent(node, owner, runner),
  50. mRecheckTime(0)
  51. {
  52. }
  53. void ActiveSelectorTask::onInitialize()
  54. {
  55. Parent::onInitialize();
  56. if(mBranches.empty())
  57. {
  58. for (VectorPtr<Task*>::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  59. {
  60. mBranches.push_back(BehaviorTreeBranch(*i));
  61. }
  62. }
  63. mCurrentBranch = mBranches.begin();
  64. mRunningBranch = mBranches.end();
  65. mRecheckTime = 0;
  66. }
  67. Task* ActiveSelectorTask::update()
  68. {
  69. // empty node, bail
  70. if(mBranches.empty())
  71. {
  72. mStatus = INVALID;
  73. return NULL;
  74. }
  75. // is it time to re-check higher priority branches?
  76. if(Sim::getCurrentTime() >= mRecheckTime)
  77. {
  78. // pick highest priority branch
  79. mCurrentBranch = mBranches.begin();
  80. // determine the next recheck time
  81. mRecheckTime = Sim::getCurrentTime() + static_cast<ActiveSelector *>(mNodeRep)->getRecheckFrequency();
  82. }
  83. // run a branch, if it fails move on to the next
  84. for(mCurrentBranch; mCurrentBranch != mBranches.end(); ++mCurrentBranch)
  85. {
  86. // reset the branch if it's not the current running branch
  87. if(mCurrentBranch != mRunningBranch)
  88. mCurrentBranch->reset();
  89. mStatus = mCurrentBranch->update();
  90. if(mStatus == FAILURE) // move on to next
  91. continue;
  92. if(mStatus == RUNNING || mStatus == SUSPENDED) // track the current running branch
  93. mRunningBranch = mCurrentBranch;
  94. break;
  95. }
  96. if( (mStatus != RUNNING && mStatus != SUSPENDED) || mCurrentBranch == mBranches.end() )
  97. mIsComplete = true;
  98. return NULL;
  99. }
  100. Status ActiveSelectorTask::getStatus()
  101. {
  102. if(mStatus == SUSPENDED && mCurrentBranch != mBranches.end())
  103. return mCurrentBranch->getStatus(); // suspended branch may have resumed
  104. return mStatus;
  105. }