lua_AIStateMachine.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  43. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  44. {
  45. // Get parameter 1 off the stack.
  46. const char* param1 = ScriptUtil::getString(2, false);
  47. AIStateMachine* instance = getInstance(state);
  48. void* returnPtr = (void*)instance->addState(param1);
  49. if (returnPtr)
  50. {
  51. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  52. object->instance = returnPtr;
  53. object->owns = false;
  54. luaL_getmetatable(state, "AIState");
  55. lua_setmetatable(state, -2);
  56. }
  57. else
  58. {
  59. lua_pushnil(state);
  60. }
  61. return 1;
  62. }
  63. else if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  64. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  65. {
  66. // Get parameter 1 off the stack.
  67. AIState* param1 = ScriptUtil::getObjectPointer<AIState>(2, "AIState", false);
  68. AIStateMachine* instance = getInstance(state);
  69. instance->addState(param1);
  70. return 0;
  71. }
  72. else
  73. {
  74. lua_pushstring(state, "lua_AIStateMachine_addState - Failed to match the given parameters to a valid function signature.");
  75. lua_error(state);
  76. }
  77. break;
  78. }
  79. default:
  80. {
  81. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  82. lua_error(state);
  83. break;
  84. }
  85. }
  86. return 0;
  87. }
  88. int lua_AIStateMachine_getActiveState(lua_State* state)
  89. {
  90. // Get the number of parameters.
  91. int paramCount = lua_gettop(state);
  92. // Attempt to match the parameters to a valid binding.
  93. switch (paramCount)
  94. {
  95. case 1:
  96. {
  97. if ((lua_type(state, 1) == LUA_TUSERDATA))
  98. {
  99. AIStateMachine* instance = getInstance(state);
  100. void* returnPtr = (void*)instance->getActiveState();
  101. if (returnPtr)
  102. {
  103. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  104. object->instance = returnPtr;
  105. object->owns = false;
  106. luaL_getmetatable(state, "AIState");
  107. lua_setmetatable(state, -2);
  108. }
  109. else
  110. {
  111. lua_pushnil(state);
  112. }
  113. return 1;
  114. }
  115. else
  116. {
  117. lua_pushstring(state, "lua_AIStateMachine_getActiveState - Failed to match the given parameters to a valid function signature.");
  118. lua_error(state);
  119. }
  120. break;
  121. }
  122. default:
  123. {
  124. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  125. lua_error(state);
  126. break;
  127. }
  128. }
  129. return 0;
  130. }
  131. int lua_AIStateMachine_getAgent(lua_State* state)
  132. {
  133. // Get the number of parameters.
  134. int paramCount = lua_gettop(state);
  135. // Attempt to match the parameters to a valid binding.
  136. switch (paramCount)
  137. {
  138. case 1:
  139. {
  140. if ((lua_type(state, 1) == LUA_TUSERDATA))
  141. {
  142. AIStateMachine* instance = getInstance(state);
  143. void* returnPtr = (void*)instance->getAgent();
  144. if (returnPtr)
  145. {
  146. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  147. object->instance = returnPtr;
  148. object->owns = false;
  149. luaL_getmetatable(state, "AIAgent");
  150. lua_setmetatable(state, -2);
  151. }
  152. else
  153. {
  154. lua_pushnil(state);
  155. }
  156. return 1;
  157. }
  158. else
  159. {
  160. lua_pushstring(state, "lua_AIStateMachine_getAgent - Failed to match the given parameters to a valid function signature.");
  161. lua_error(state);
  162. }
  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. else
  205. {
  206. lua_pushstring(state, "lua_AIStateMachine_getState - Failed to match the given parameters to a valid function signature.");
  207. lua_error(state);
  208. }
  209. break;
  210. }
  211. default:
  212. {
  213. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  214. lua_error(state);
  215. break;
  216. }
  217. }
  218. return 0;
  219. }
  220. int lua_AIStateMachine_removeState(lua_State* state)
  221. {
  222. // Get the number of parameters.
  223. int paramCount = lua_gettop(state);
  224. // Attempt to match the parameters to a valid binding.
  225. switch (paramCount)
  226. {
  227. case 2:
  228. {
  229. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  230. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  231. {
  232. // Get parameter 1 off the stack.
  233. AIState* param1 = ScriptUtil::getObjectPointer<AIState>(2, "AIState", false);
  234. AIStateMachine* instance = getInstance(state);
  235. instance->removeState(param1);
  236. return 0;
  237. }
  238. else
  239. {
  240. lua_pushstring(state, "lua_AIStateMachine_removeState - Failed to match the given parameters to a valid function signature.");
  241. lua_error(state);
  242. }
  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. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  264. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  265. {
  266. // Get parameter 1 off the stack.
  267. const char* param1 = ScriptUtil::getString(2, false);
  268. AIStateMachine* instance = getInstance(state);
  269. void* returnPtr = (void*)instance->setState(param1);
  270. if (returnPtr)
  271. {
  272. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  273. object->instance = returnPtr;
  274. object->owns = false;
  275. luaL_getmetatable(state, "AIState");
  276. lua_setmetatable(state, -2);
  277. }
  278. else
  279. {
  280. lua_pushnil(state);
  281. }
  282. return 1;
  283. }
  284. else if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  285. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  286. {
  287. // Get parameter 1 off the stack.
  288. AIState* param1 = ScriptUtil::getObjectPointer<AIState>(2, "AIState", false);
  289. AIStateMachine* instance = getInstance(state);
  290. bool result = instance->setState(param1);
  291. // Push the return value onto the stack.
  292. lua_pushboolean(state, result);
  293. return 1;
  294. }
  295. else
  296. {
  297. lua_pushstring(state, "lua_AIStateMachine_setState - Failed to match the given parameters to a valid function signature.");
  298. lua_error(state);
  299. }
  300. break;
  301. }
  302. default:
  303. {
  304. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  305. lua_error(state);
  306. break;
  307. }
  308. }
  309. return 0;
  310. }
  311. }