ScriptEval.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "ScriptEval.h"
  23. #include "console/engineAPI.h"
  24. #include "platform/profiler.h"
  25. using namespace BadBehavior;
  26. //------------------------------------------------------------------------------
  27. // ScriptEval node - warning, slow!
  28. //------------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT(ScriptEval);
  30. ScriptEval::ScriptEval()
  31. : mDefaultReturnStatus(SUCCESS)
  32. {
  33. }
  34. void ScriptEval::initPersistFields()
  35. {
  36. addGroup( "Behavior" );
  37. addField( "behaviorScript", TypeCommand, Offset(mBehaviorScript, ScriptEval),
  38. "@brief The command to execute when the leaf is ticked. Max 255 characters." );
  39. addField( "defaultReturnStatus", TYPEID< BadBehavior::Status >(), Offset(mDefaultReturnStatus, ScriptEval),
  40. "@brief The default value for this node to return.");
  41. endGroup( "Behavior" );
  42. Parent::initPersistFields();
  43. }
  44. Task *ScriptEval::createTask(SimObject &owner, BehaviorTreeRunner &runner)
  45. {
  46. return new ScriptEvalTask(*this, owner, runner);
  47. }
  48. Status ScriptEval::evaluateScript( SimObject *owner )
  49. {
  50. PROFILE_SCOPE(ScriptEval_evaluateScript);
  51. if(mBehaviorScript.isEmpty())
  52. return mDefaultReturnStatus;
  53. // get the result
  54. const char *result = Con::evaluatef("%%obj=%s; %s return %s;",
  55. owner->getIdString(),
  56. mBehaviorScript.c_str(),
  57. EngineMarshallData< BehaviorReturnType >(mDefaultReturnStatus));
  58. if(result[0] == '1' || result[0] == '0')
  59. // map true or false to SUCCEED or FAILURE
  60. return static_cast<Status>(dAtoi(result));
  61. // convert the returned value to our internal enum type
  62. return EngineUnmarshallData< BehaviorReturnType >()( result );
  63. }
  64. //------------------------------------------------------------------------------
  65. // RunScript task
  66. //------------------------------------------------------------------------------
  67. ScriptEvalTask::ScriptEvalTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
  68. : Parent(node, owner, runner)
  69. {
  70. }
  71. Task* ScriptEvalTask::update()
  72. {
  73. mStatus = static_cast<ScriptEval*>(mNodeRep)->evaluateScript( mOwner );
  74. return NULL; // leaves don't have children
  75. }