lua_AIState.cpp 12 KB

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