lua_AIController.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_AIController.h"
  4. #include "AIController.h"
  5. #include "Base.h"
  6. #include "Game.h"
  7. namespace gameplay
  8. {
  9. void luaRegister_AIController()
  10. {
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"findAgent", lua_AIController_findAgent},
  14. {"sendMessage", lua_AIController_sendMessage},
  15. {NULL, NULL}
  16. };
  17. const luaL_Reg* lua_statics = NULL;
  18. std::vector<std::string> scopePath;
  19. ScriptUtil::registerClass("AIController", lua_members, NULL, NULL, lua_statics, scopePath);
  20. }
  21. static AIController* getInstance(lua_State* state)
  22. {
  23. void* userdata = luaL_checkudata(state, 1, "AIController");
  24. luaL_argcheck(state, userdata != NULL, 1, "'AIController' expected.");
  25. return (AIController*)((ScriptUtil::LuaObject*)userdata)->instance;
  26. }
  27. int lua_AIController_findAgent(lua_State* state)
  28. {
  29. // Get the number of parameters.
  30. int paramCount = lua_gettop(state);
  31. // Attempt to match the parameters to a valid binding.
  32. switch (paramCount)
  33. {
  34. case 2:
  35. {
  36. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  37. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  38. {
  39. // Get parameter 1 off the stack.
  40. const char* param1 = ScriptUtil::getString(2, false);
  41. AIController* instance = getInstance(state);
  42. void* returnPtr = (void*)instance->findAgent(param1);
  43. if (returnPtr)
  44. {
  45. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  46. object->instance = returnPtr;
  47. object->owns = false;
  48. luaL_getmetatable(state, "AIAgent");
  49. lua_setmetatable(state, -2);
  50. }
  51. else
  52. {
  53. lua_pushnil(state);
  54. }
  55. return 1;
  56. }
  57. else
  58. {
  59. lua_pushstring(state, "lua_AIController_findAgent - Failed to match the given parameters to a valid function signature.");
  60. lua_error(state);
  61. }
  62. break;
  63. }
  64. default:
  65. {
  66. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  67. lua_error(state);
  68. break;
  69. }
  70. }
  71. return 0;
  72. }
  73. int lua_AIController_sendMessage(lua_State* state)
  74. {
  75. // Get the number of parameters.
  76. int paramCount = lua_gettop(state);
  77. // Attempt to match the parameters to a valid binding.
  78. switch (paramCount)
  79. {
  80. case 2:
  81. {
  82. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  83. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  84. {
  85. // Get parameter 1 off the stack.
  86. AIMessage* param1 = ScriptUtil::getObjectPointer<AIMessage>(2, "AIMessage", false);
  87. AIController* instance = getInstance(state);
  88. instance->sendMessage(param1);
  89. return 0;
  90. }
  91. else
  92. {
  93. lua_pushstring(state, "lua_AIController_sendMessage - Failed to match the given parameters to a valid function signature.");
  94. lua_error(state);
  95. }
  96. break;
  97. }
  98. case 3:
  99. {
  100. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  101. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL) &&
  102. lua_type(state, 3) == LUA_TNUMBER)
  103. {
  104. // Get parameter 1 off the stack.
  105. AIMessage* param1 = ScriptUtil::getObjectPointer<AIMessage>(2, "AIMessage", false);
  106. // Get parameter 2 off the stack.
  107. float param2 = (float)luaL_checknumber(state, 3);
  108. AIController* instance = getInstance(state);
  109. instance->sendMessage(param1, param2);
  110. return 0;
  111. }
  112. else
  113. {
  114. lua_pushstring(state, "lua_AIController_sendMessage - Failed to match the given parameters to a valid function signature.");
  115. lua_error(state);
  116. }
  117. break;
  118. }
  119. default:
  120. {
  121. lua_pushstring(state, "Invalid number of parameters (expected 2 or 3).");
  122. lua_error(state);
  123. break;
  124. }
  125. }
  126. return 0;
  127. }
  128. }