lua_AIController.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. ScriptUtil::LuaArray<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. lua_pushstring(state, "lua_AIController_findAgent - Failed to match the given parameters to a valid function signature.");
  58. lua_error(state);
  59. break;
  60. }
  61. default:
  62. {
  63. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  64. lua_error(state);
  65. break;
  66. }
  67. }
  68. return 0;
  69. }
  70. int lua_AIController_sendMessage(lua_State* state)
  71. {
  72. // Get the number of parameters.
  73. int paramCount = lua_gettop(state);
  74. // Attempt to match the parameters to a valid binding.
  75. switch (paramCount)
  76. {
  77. case 2:
  78. {
  79. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  80. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  81. {
  82. // Get parameter 1 off the stack.
  83. bool param1Valid;
  84. ScriptUtil::LuaArray<AIMessage> param1 = ScriptUtil::getObjectPointer<AIMessage>(2, "AIMessage", false, &param1Valid);
  85. if (!param1Valid)
  86. {
  87. lua_pushstring(state, "Failed to convert parameter 1 to type 'AIMessage'.");
  88. lua_error(state);
  89. }
  90. AIController* instance = getInstance(state);
  91. instance->sendMessage(param1);
  92. return 0;
  93. }
  94. lua_pushstring(state, "lua_AIController_sendMessage - Failed to match the given parameters to a valid function signature.");
  95. lua_error(state);
  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. bool param1Valid;
  106. ScriptUtil::LuaArray<AIMessage> param1 = ScriptUtil::getObjectPointer<AIMessage>(2, "AIMessage", false, &param1Valid);
  107. if (!param1Valid)
  108. {
  109. lua_pushstring(state, "Failed to convert parameter 1 to type 'AIMessage'.");
  110. lua_error(state);
  111. }
  112. // Get parameter 2 off the stack.
  113. float param2 = (float)luaL_checknumber(state, 3);
  114. AIController* instance = getInstance(state);
  115. instance->sendMessage(param1, param2);
  116. return 0;
  117. }
  118. lua_pushstring(state, "lua_AIController_sendMessage - Failed to match the given parameters to a valid function signature.");
  119. lua_error(state);
  120. break;
  121. }
  122. default:
  123. {
  124. lua_pushstring(state, "Invalid number of parameters (expected 2 or 3).");
  125. lua_error(state);
  126. break;
  127. }
  128. }
  129. return 0;
  130. }
  131. }