lua_AIStateMachine.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_AIStateMachine.h"
  4. #include "AIAgent.h"
  5. #include "AIMessage.h"
  6. #include "AIStateMachine.h"
  7. #include "Base.h"
  8. #include "Game.h"
  9. namespace gameplay
  10. {
  11. void luaRegister_AIStateMachine()
  12. {
  13. const luaL_Reg lua_members[] =
  14. {
  15. {"addState", lua_AIStateMachine_addState},
  16. {"getActiveState", lua_AIStateMachine_getActiveState},
  17. {"getAgent", lua_AIStateMachine_getAgent},
  18. {"getState", lua_AIStateMachine_getState},
  19. {"removeState", lua_AIStateMachine_removeState},
  20. {"setState", lua_AIStateMachine_setState},
  21. {NULL, NULL}
  22. };
  23. const luaL_Reg* lua_statics = NULL;
  24. std::vector<std::string> scopePath;
  25. ScriptUtil::registerClass("AIStateMachine", lua_members, NULL, NULL, lua_statics, scopePath);
  26. }
  27. static AIStateMachine* getInstance(lua_State* state)
  28. {
  29. void* userdata = luaL_checkudata(state, 1, "AIStateMachine");
  30. luaL_argcheck(state, userdata != NULL, 1, "'AIStateMachine' expected.");
  31. return (AIStateMachine*)((ScriptUtil::LuaObject*)userdata)->instance;
  32. }
  33. int lua_AIStateMachine_addState(lua_State* state)
  34. {
  35. // Get the number of parameters.
  36. int paramCount = lua_gettop(state);
  37. // Attempt to match the parameters to a valid binding.
  38. switch (paramCount)
  39. {
  40. case 2:
  41. {
  42. do
  43. {
  44. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  45. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  46. {
  47. // Get parameter 1 off the stack.
  48. const char* param1 = ScriptUtil::getString(2, false);
  49. AIStateMachine* instance = getInstance(state);
  50. void* returnPtr = (void*)instance->addState(param1);
  51. if (returnPtr)
  52. {
  53. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  54. object->instance = returnPtr;
  55. object->owns = false;
  56. luaL_getmetatable(state, "AIState");
  57. lua_setmetatable(state, -2);
  58. }
  59. else
  60. {
  61. lua_pushnil(state);
  62. }
  63. return 1;
  64. }
  65. } while (0);
  66. do
  67. {
  68. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  69. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  70. {
  71. // Get parameter 1 off the stack.
  72. bool param1Valid;
  73. ScriptUtil::LuaArray<AIState> param1 = ScriptUtil::getObjectPointer<AIState>(2, "AIState", false, &param1Valid);
  74. if (!param1Valid)
  75. break;
  76. AIStateMachine* instance = getInstance(state);
  77. instance->addState(param1);
  78. return 0;
  79. }
  80. } while (0);
  81. lua_pushstring(state, "lua_AIStateMachine_addState - Failed to match the given parameters to a valid function signature.");
  82. lua_error(state);
  83. break;
  84. }
  85. default:
  86. {
  87. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  88. lua_error(state);
  89. break;
  90. }
  91. }
  92. return 0;
  93. }
  94. int lua_AIStateMachine_getActiveState(lua_State* state)
  95. {
  96. // Get the number of parameters.
  97. int paramCount = lua_gettop(state);
  98. // Attempt to match the parameters to a valid binding.
  99. switch (paramCount)
  100. {
  101. case 1:
  102. {
  103. if ((lua_type(state, 1) == LUA_TUSERDATA))
  104. {
  105. AIStateMachine* instance = getInstance(state);
  106. void* returnPtr = (void*)instance->getActiveState();
  107. if (returnPtr)
  108. {
  109. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  110. object->instance = returnPtr;
  111. object->owns = false;
  112. luaL_getmetatable(state, "AIState");
  113. lua_setmetatable(state, -2);
  114. }
  115. else
  116. {
  117. lua_pushnil(state);
  118. }
  119. return 1;
  120. }
  121. lua_pushstring(state, "lua_AIStateMachine_getActiveState - Failed to match the given parameters to a valid function signature.");
  122. lua_error(state);
  123. break;
  124. }
  125. default:
  126. {
  127. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  128. lua_error(state);
  129. break;
  130. }
  131. }
  132. return 0;
  133. }
  134. int lua_AIStateMachine_getAgent(lua_State* state)
  135. {
  136. // Get the number of parameters.
  137. int paramCount = lua_gettop(state);
  138. // Attempt to match the parameters to a valid binding.
  139. switch (paramCount)
  140. {
  141. case 1:
  142. {
  143. if ((lua_type(state, 1) == LUA_TUSERDATA))
  144. {
  145. AIStateMachine* instance = getInstance(state);
  146. void* returnPtr = (void*)instance->getAgent();
  147. if (returnPtr)
  148. {
  149. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  150. object->instance = returnPtr;
  151. object->owns = false;
  152. luaL_getmetatable(state, "AIAgent");
  153. lua_setmetatable(state, -2);
  154. }
  155. else
  156. {
  157. lua_pushnil(state);
  158. }
  159. return 1;
  160. }
  161. lua_pushstring(state, "lua_AIStateMachine_getAgent - Failed to match the given parameters to a valid function signature.");
  162. lua_error(state);
  163. break;
  164. }
  165. default:
  166. {
  167. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  168. lua_error(state);
  169. break;
  170. }
  171. }
  172. return 0;
  173. }
  174. int lua_AIStateMachine_getState(lua_State* state)
  175. {
  176. // Get the number of parameters.
  177. int paramCount = lua_gettop(state);
  178. // Attempt to match the parameters to a valid binding.
  179. switch (paramCount)
  180. {
  181. case 2:
  182. {
  183. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  184. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  185. {
  186. // Get parameter 1 off the stack.
  187. const char* param1 = ScriptUtil::getString(2, false);
  188. AIStateMachine* instance = getInstance(state);
  189. void* returnPtr = (void*)instance->getState(param1);
  190. if (returnPtr)
  191. {
  192. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  193. object->instance = returnPtr;
  194. object->owns = false;
  195. luaL_getmetatable(state, "AIState");
  196. lua_setmetatable(state, -2);
  197. }
  198. else
  199. {
  200. lua_pushnil(state);
  201. }
  202. return 1;
  203. }
  204. lua_pushstring(state, "lua_AIStateMachine_getState - Failed to match the given parameters to a valid function signature.");
  205. lua_error(state);
  206. break;
  207. }
  208. default:
  209. {
  210. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  211. lua_error(state);
  212. break;
  213. }
  214. }
  215. return 0;
  216. }
  217. int lua_AIStateMachine_removeState(lua_State* state)
  218. {
  219. // Get the number of parameters.
  220. int paramCount = lua_gettop(state);
  221. // Attempt to match the parameters to a valid binding.
  222. switch (paramCount)
  223. {
  224. case 2:
  225. {
  226. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  227. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  228. {
  229. // Get parameter 1 off the stack.
  230. bool param1Valid;
  231. ScriptUtil::LuaArray<AIState> param1 = ScriptUtil::getObjectPointer<AIState>(2, "AIState", false, &param1Valid);
  232. if (!param1Valid)
  233. {
  234. lua_pushstring(state, "Failed to convert parameter 1 to type 'AIState'.");
  235. lua_error(state);
  236. }
  237. AIStateMachine* instance = getInstance(state);
  238. instance->removeState(param1);
  239. return 0;
  240. }
  241. lua_pushstring(state, "lua_AIStateMachine_removeState - Failed to match the given parameters to a valid function signature.");
  242. lua_error(state);
  243. break;
  244. }
  245. default:
  246. {
  247. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  248. lua_error(state);
  249. break;
  250. }
  251. }
  252. return 0;
  253. }
  254. int lua_AIStateMachine_setState(lua_State* state)
  255. {
  256. // Get the number of parameters.
  257. int paramCount = lua_gettop(state);
  258. // Attempt to match the parameters to a valid binding.
  259. switch (paramCount)
  260. {
  261. case 2:
  262. {
  263. do
  264. {
  265. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  266. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  267. {
  268. // Get parameter 1 off the stack.
  269. const char* param1 = ScriptUtil::getString(2, false);
  270. AIStateMachine* instance = getInstance(state);
  271. void* returnPtr = (void*)instance->setState(param1);
  272. if (returnPtr)
  273. {
  274. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  275. object->instance = returnPtr;
  276. object->owns = false;
  277. luaL_getmetatable(state, "AIState");
  278. lua_setmetatable(state, -2);
  279. }
  280. else
  281. {
  282. lua_pushnil(state);
  283. }
  284. return 1;
  285. }
  286. } while (0);
  287. do
  288. {
  289. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  290. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  291. {
  292. // Get parameter 1 off the stack.
  293. bool param1Valid;
  294. ScriptUtil::LuaArray<AIState> param1 = ScriptUtil::getObjectPointer<AIState>(2, "AIState", false, &param1Valid);
  295. if (!param1Valid)
  296. break;
  297. AIStateMachine* instance = getInstance(state);
  298. bool result = instance->setState(param1);
  299. // Push the return value onto the stack.
  300. lua_pushboolean(state, result);
  301. return 1;
  302. }
  303. } while (0);
  304. lua_pushstring(state, "lua_AIStateMachine_setState - Failed to match the given parameters to a valid function signature.");
  305. lua_error(state);
  306. break;
  307. }
  308. default:
  309. {
  310. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  311. lua_error(state);
  312. break;
  313. }
  314. }
  315. return 0;
  316. }
  317. }