WaitForSignal.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "WaitForSignal.h"
  23. #include "BadBehavior/core/Runner.h"
  24. #include "console/consoleTypes.h"
  25. using namespace BadBehavior;
  26. //------------------------------------------------------------------------------
  27. // WaitForSignal leaf node
  28. //------------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT(WaitForSignal);
  30. WaitForSignal::WaitForSignal()
  31. : mTimeoutMs(0)
  32. {
  33. }
  34. void WaitForSignal::initPersistFields()
  35. {
  36. addGroup( "Behavior" );
  37. addField( "signalName", TypeRealString, Offset(mSignalName, WaitForSignal),
  38. "The time in ms that the node should wait before completion." );
  39. addProtectedField( "timeoutMs", TypeS32, Offset(mTimeoutMs, WaitForSignal), &_setTimeout, &defaultProtectedGetFn,
  40. "The maximum time in ms that the node should wait for the signal.\n"
  41. "A value of 0 indicates no limit");
  42. endGroup( "Behavior" );
  43. Parent::initPersistFields();
  44. }
  45. Task *WaitForSignal::createTask(SimObject &owner, BehaviorTreeRunner &runner)
  46. {
  47. return new WaitForSignalTask(*this, owner, runner);
  48. }
  49. bool WaitForSignal::_setTimeout(void *object, const char *index, const char *data)
  50. {
  51. WaitForSignal *node = static_cast<WaitForSignal *>( object );
  52. node->mTimeoutMs = getMax(0, dAtoi( data ));
  53. return false;
  54. }
  55. //------------------------------------------------------------------------------
  56. // Wait task
  57. //------------------------------------------------------------------------------
  58. WaitForSignalTask::WaitForSignalTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
  59. : Parent(node, owner, runner),
  60. mEventId(0)
  61. {
  62. }
  63. WaitForSignalTask::~WaitForSignalTask()
  64. {
  65. unsubscribe();
  66. cancelEvent();
  67. }
  68. void WaitForSignalTask::subscribe()
  69. {
  70. mRunner->subscribeToSignal(static_cast<WaitForSignal *>(mNodeRep)->getSignalName().c_str(), this);
  71. }
  72. void WaitForSignalTask::unsubscribe()
  73. {
  74. mRunner->unsubscribeFromSignal(static_cast<WaitForSignal *>(mNodeRep)->getSignalName().c_str(), this);
  75. }
  76. void WaitForSignalTask::onSignal()
  77. {
  78. onResume();
  79. }
  80. void WaitForSignalTask::onTimeout()
  81. {
  82. mStatus = FAILURE;
  83. }
  84. void WaitForSignalTask::cancelEvent()
  85. {
  86. if(mEventId != 0 && Sim::isEventPending(mEventId))
  87. {
  88. Sim::cancelEvent(mEventId);
  89. mEventId = 0;
  90. }
  91. }
  92. void WaitForSignalTask::onInitialize()
  93. {
  94. Parent::onInitialize();
  95. subscribe();
  96. cancelEvent();
  97. }
  98. void WaitForSignalTask::onTerminate()
  99. {
  100. Parent::onTerminate();
  101. unsubscribe();
  102. cancelEvent();
  103. }
  104. Task* WaitForSignalTask::update()
  105. {
  106. if(mStatus == RESUME)
  107. {
  108. mStatus = SUCCESS;
  109. }
  110. else if (mStatus == INVALID)
  111. {
  112. mStatus = SUSPENDED;
  113. S32 timeout = static_cast<WaitForSignal *>(mNodeRep)->getTimeoutMs();
  114. if(timeout > 0)
  115. mEventId = Sim::postEvent(mRunner, new WaitForSignalTimeoutEvent(*this), Sim::getCurrentTime() + timeout);
  116. }
  117. if(mStatus == SUCCESS || mStatus == FAILURE)
  118. mIsComplete = true;
  119. return NULL;
  120. }