RandomWait.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "RandomWait.h"
  23. #include "BadBehavior/core/Runner.h"
  24. #include "math/mMathFn.h"
  25. #include "console/consoleTypes.h"
  26. using namespace BadBehavior;
  27. //------------------------------------------------------------------------------
  28. // RandomWait leaf node
  29. //------------------------------------------------------------------------------
  30. IMPLEMENT_CONOBJECT(RandomWait);
  31. RandomWait::RandomWait()
  32. : mWaitMinMs(0),
  33. mWaitMaxMs(99999)
  34. {
  35. }
  36. void RandomWait::initPersistFields()
  37. {
  38. addGroup( "Behavior" );
  39. addProtectedField( "waitMinMs", TypeS32, Offset(mWaitMinMs, RandomWait), &_setWaitMin, &defaultProtectedGetFn,
  40. "The minimum time period in ms to wait before completion." );
  41. addProtectedField( "waitMaxMs", TypeS32, Offset(mWaitMaxMs, RandomWait), &_setWaitMax, &defaultProtectedGetFn,
  42. "The maximum time period in ms to wait before completion." );
  43. endGroup( "Behavior" );
  44. Parent::initPersistFields();
  45. }
  46. bool RandomWait::_setWaitMin(void *object, const char *index, const char *data)
  47. {
  48. RandomWait *node = static_cast<RandomWait *>( object );
  49. node->mWaitMinMs = getMin(node->mWaitMaxMs, dAtoi( data ));
  50. return false;
  51. }
  52. bool RandomWait::_setWaitMax(void *object, const char *index, const char *data)
  53. {
  54. RandomWait *node = static_cast<RandomWait *>( object );
  55. node->mWaitMaxMs = getMax(node->mWaitMinMs, dAtoi( data ));
  56. return false;
  57. }
  58. Task *RandomWait::createTask(SimObject &owner, BehaviorTreeRunner &runner)
  59. {
  60. return new RandomWaitTask(*this, owner, runner);
  61. }
  62. //------------------------------------------------------------------------------
  63. // RandomWait task
  64. //------------------------------------------------------------------------------
  65. RandomWaitTask::RandomWaitTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
  66. : Parent(node, owner, runner)
  67. {
  68. }
  69. RandomWaitTask::~RandomWaitTask()
  70. {
  71. cancelEvent();
  72. }
  73. void RandomWaitTask::cancelEvent()
  74. {
  75. if(Sim::isEventPending(mEventId))
  76. {
  77. Sim::cancelEvent(mEventId);
  78. mEventId = 0;
  79. }
  80. }
  81. void RandomWaitTask::onInitialize()
  82. {
  83. Parent::onInitialize();
  84. cancelEvent();
  85. }
  86. void RandomWaitTask::onTerminate()
  87. {
  88. Parent::onTerminate();
  89. cancelEvent();
  90. }
  91. Task* RandomWaitTask::update()
  92. {
  93. if(mStatus == RESUME)
  94. {
  95. mStatus = SUCCESS;
  96. mIsComplete = true;
  97. }
  98. else if(mStatus == INVALID)
  99. {
  100. RandomWait *node = static_cast<RandomWait*>(mNodeRep);
  101. mEventId = Sim::postEvent(mRunner, new TaskReactivateEvent(*this), Sim::getCurrentTime() + mRandI(node->getWaitMinMs(), node->getWaitMaxMs()));
  102. mStatus = SUSPENDED;
  103. }
  104. return NULL;
  105. }