ScriptFunc.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "ScriptFunc.h"
  23. #include "console/engineAPI.h"
  24. #include "platform/profiler.h"
  25. using namespace BadBehavior;
  26. //------------------------------------------------------------------------------
  27. // ScriptFunc node
  28. //------------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT(ScriptFunc);
  30. ScriptFunc::ScriptFunc()
  31. : mDefaultReturnStatus(SUCCESS),
  32. mScriptFunction(StringTable->insert(""))
  33. {
  34. for(U8 i = 0; i < MAX_COMMAND_ARGS; ++i)
  35. mScriptArgs[i] = StringTable->insert("");
  36. }
  37. void ScriptFunc::initPersistFields()
  38. {
  39. addGroup( "Behavior" );
  40. addField( "func", TypeCaseString, Offset(mScriptFunction, ScriptFunc),
  41. "@brief The function to execute when the leaf is ticked." );
  42. addField( "args", TypeCaseString, Offset(mScriptArgs, ScriptFunc), MAX_COMMAND_ARGS,
  43. "@brief The arguments to be passed to the function to be executed." );
  44. addField( "defaultReturnStatus", TYPEID< BadBehavior::Status >(), Offset(mDefaultReturnStatus, ScriptFunc),
  45. "@brief The default value for this node to return.");
  46. endGroup( "Behavior" );
  47. Parent::initPersistFields();
  48. }
  49. Task *ScriptFunc::createTask(SimObject &owner, BehaviorTreeRunner &runner)
  50. {
  51. return new ScriptFuncTask(*this, owner, runner);
  52. }
  53. Status ScriptFunc::evaluate( SimObject *owner )
  54. {
  55. PROFILE_SCOPE(ScriptFunc_evaluate);
  56. if(!owner)
  57. return INVALID;
  58. if(!mScriptFunction || !mScriptFunction[0])
  59. return mDefaultReturnStatus;
  60. S32 argc = 0;
  61. const char *args[MAX_COMMAND_ARGS + 2];
  62. args[0] = mScriptFunction;
  63. while(argc < MAX_COMMAND_ARGS && (mScriptArgs[argc] && mScriptArgs[argc][0]))
  64. {
  65. args[argc + 2] = mScriptArgs[argc];
  66. ++argc;
  67. }
  68. argc += 2;
  69. // get the result
  70. const char *result = Con::execute(owner, argc, args);
  71. // if function didn't return a result, return our default status
  72. if(!result || !result[0])
  73. return mDefaultReturnStatus;
  74. // map true or false to SUCCEED or FAILURE
  75. if(result[0] == '1' || result[0] == '0')
  76. return static_cast<Status>(dAtoi(result));
  77. // convert the returned value to our internal enum type
  78. return EngineUnmarshallData< BehaviorReturnType >()( result );
  79. }
  80. //------------------------------------------------------------------------------
  81. // ScriptFunc task
  82. //------------------------------------------------------------------------------
  83. ScriptFuncTask::ScriptFuncTask(Node &node, SimObject &owner, BehaviorTreeRunner &runner)
  84. : Parent(node, owner, runner)
  85. {
  86. }
  87. Task* ScriptFuncTask::update()
  88. {
  89. mStatus = static_cast<ScriptFunc*>(mNodeRep)->evaluate( mOwner );
  90. if(mStatus != RUNNING && mStatus != SUSPENDED)
  91. mIsComplete = true;
  92. return NULL; // leaves don't have children
  93. }