lua_AIState.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. else
  62. {
  63. lua_pushstring(state, "lua_AIState__gc - Failed to match the given parameters to a valid function signature.");
  64. lua_error(state);
  65. }
  66. break;
  67. }
  68. default:
  69. {
  70. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  71. lua_error(state);
  72. break;
  73. }
  74. }
  75. return 0;
  76. }
  77. int lua_AIState_addRef(lua_State* state)
  78. {
  79. // Get the number of parameters.
  80. int paramCount = lua_gettop(state);
  81. // Attempt to match the parameters to a valid binding.
  82. switch (paramCount)
  83. {
  84. case 1:
  85. {
  86. if ((lua_type(state, 1) == LUA_TUSERDATA))
  87. {
  88. AIState* instance = getInstance(state);
  89. instance->addRef();
  90. return 0;
  91. }
  92. else
  93. {
  94. lua_pushstring(state, "lua_AIState_addRef - Failed to match the given parameters to a valid function signature.");
  95. lua_error(state);
  96. }
  97. break;
  98. }
  99. default:
  100. {
  101. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  102. lua_error(state);
  103. break;
  104. }
  105. }
  106. return 0;
  107. }
  108. int lua_AIState_addScriptCallback(lua_State* state)
  109. {
  110. // Get the number of parameters.
  111. int paramCount = lua_gettop(state);
  112. // Attempt to match the parameters to a valid binding.
  113. switch (paramCount)
  114. {
  115. case 3:
  116. {
  117. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  118. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  119. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  120. {
  121. // Get parameter 1 off the stack.
  122. std::string param1 = ScriptUtil::getString(2, true);
  123. // Get parameter 2 off the stack.
  124. std::string param2 = ScriptUtil::getString(3, true);
  125. AIState* instance = getInstance(state);
  126. instance->addScriptCallback(param1, param2);
  127. return 0;
  128. }
  129. else
  130. {
  131. lua_pushstring(state, "lua_AIState_addScriptCallback - Failed to match the given parameters to a valid function signature.");
  132. lua_error(state);
  133. }
  134. break;
  135. }
  136. default:
  137. {
  138. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  139. lua_error(state);
  140. break;
  141. }
  142. }
  143. return 0;
  144. }
  145. int lua_AIState_getId(lua_State* state)
  146. {
  147. // Get the number of parameters.
  148. int paramCount = lua_gettop(state);
  149. // Attempt to match the parameters to a valid binding.
  150. switch (paramCount)
  151. {
  152. case 1:
  153. {
  154. if ((lua_type(state, 1) == LUA_TUSERDATA))
  155. {
  156. AIState* instance = getInstance(state);
  157. const char* result = instance->getId();
  158. // Push the return value onto the stack.
  159. lua_pushstring(state, result);
  160. return 1;
  161. }
  162. else
  163. {
  164. lua_pushstring(state, "lua_AIState_getId - Failed to match the given parameters to a valid function signature.");
  165. lua_error(state);
  166. }
  167. break;
  168. }
  169. default:
  170. {
  171. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  172. lua_error(state);
  173. break;
  174. }
  175. }
  176. return 0;
  177. }
  178. int lua_AIState_getRefCount(lua_State* state)
  179. {
  180. // Get the number of parameters.
  181. int paramCount = lua_gettop(state);
  182. // Attempt to match the parameters to a valid binding.
  183. switch (paramCount)
  184. {
  185. case 1:
  186. {
  187. if ((lua_type(state, 1) == LUA_TUSERDATA))
  188. {
  189. AIState* instance = getInstance(state);
  190. unsigned int result = instance->getRefCount();
  191. // Push the return value onto the stack.
  192. lua_pushunsigned(state, result);
  193. return 1;
  194. }
  195. else
  196. {
  197. lua_pushstring(state, "lua_AIState_getRefCount - Failed to match the given parameters to a valid function signature.");
  198. lua_error(state);
  199. }
  200. break;
  201. }
  202. default:
  203. {
  204. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  205. lua_error(state);
  206. break;
  207. }
  208. }
  209. return 0;
  210. }
  211. int lua_AIState_release(lua_State* state)
  212. {
  213. // Get the number of parameters.
  214. int paramCount = lua_gettop(state);
  215. // Attempt to match the parameters to a valid binding.
  216. switch (paramCount)
  217. {
  218. case 1:
  219. {
  220. if ((lua_type(state, 1) == LUA_TUSERDATA))
  221. {
  222. AIState* instance = getInstance(state);
  223. instance->release();
  224. return 0;
  225. }
  226. else
  227. {
  228. lua_pushstring(state, "lua_AIState_release - Failed to match the given parameters to a valid function signature.");
  229. lua_error(state);
  230. }
  231. break;
  232. }
  233. default:
  234. {
  235. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  236. lua_error(state);
  237. break;
  238. }
  239. }
  240. return 0;
  241. }
  242. int lua_AIState_removeScriptCallback(lua_State* state)
  243. {
  244. // Get the number of parameters.
  245. int paramCount = lua_gettop(state);
  246. // Attempt to match the parameters to a valid binding.
  247. switch (paramCount)
  248. {
  249. case 3:
  250. {
  251. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  252. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  253. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  254. {
  255. // Get parameter 1 off the stack.
  256. std::string param1 = ScriptUtil::getString(2, true);
  257. // Get parameter 2 off the stack.
  258. std::string param2 = ScriptUtil::getString(3, true);
  259. AIState* instance = getInstance(state);
  260. instance->removeScriptCallback(param1, param2);
  261. return 0;
  262. }
  263. else
  264. {
  265. lua_pushstring(state, "lua_AIState_removeScriptCallback - Failed to match the given parameters to a valid function signature.");
  266. lua_error(state);
  267. }
  268. break;
  269. }
  270. default:
  271. {
  272. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  273. lua_error(state);
  274. break;
  275. }
  276. }
  277. return 0;
  278. }
  279. int lua_AIState_setListener(lua_State* state)
  280. {
  281. // Get the number of parameters.
  282. int paramCount = lua_gettop(state);
  283. // Attempt to match the parameters to a valid binding.
  284. switch (paramCount)
  285. {
  286. case 2:
  287. {
  288. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  289. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  290. {
  291. // Get parameter 1 off the stack.
  292. ScriptUtil::LuaArray<AIState::Listener> param1 = ScriptUtil::getObjectPointer<AIState::Listener>(2, "AIStateListener", false);
  293. AIState* instance = getInstance(state);
  294. instance->setListener(param1);
  295. return 0;
  296. }
  297. else
  298. {
  299. lua_pushstring(state, "lua_AIState_setListener - Failed to match the given parameters to a valid function signature.");
  300. lua_error(state);
  301. }
  302. break;
  303. }
  304. default:
  305. {
  306. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  307. lua_error(state);
  308. break;
  309. }
  310. }
  311. return 0;
  312. }
  313. int lua_AIState_static_create(lua_State* state)
  314. {
  315. // Get the number of parameters.
  316. int paramCount = lua_gettop(state);
  317. // Attempt to match the parameters to a valid binding.
  318. switch (paramCount)
  319. {
  320. case 1:
  321. {
  322. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  323. {
  324. // Get parameter 1 off the stack.
  325. ScriptUtil::LuaArray<const char> param1 = ScriptUtil::getString(1, false);
  326. void* returnPtr = (void*)AIState::create(param1);
  327. if (returnPtr)
  328. {
  329. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  330. object->instance = returnPtr;
  331. object->owns = true;
  332. luaL_getmetatable(state, "AIState");
  333. lua_setmetatable(state, -2);
  334. }
  335. else
  336. {
  337. lua_pushnil(state);
  338. }
  339. return 1;
  340. }
  341. else
  342. {
  343. lua_pushstring(state, "lua_AIState_static_create - Failed to match the given parameters to a valid function signature.");
  344. lua_error(state);
  345. }
  346. break;
  347. }
  348. default:
  349. {
  350. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  351. lua_error(state);
  352. break;
  353. }
  354. }
  355. return 0;
  356. }
  357. }