lua_AIAgent.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_AIAgent.h"
  4. #include "AIAgent.h"
  5. #include "Base.h"
  6. #include "Game.h"
  7. #include "Node.h"
  8. #include "Ref.h"
  9. #include "ScriptController.h"
  10. #include "ScriptTarget.h"
  11. namespace gameplay
  12. {
  13. void luaRegister_AIAgent()
  14. {
  15. const luaL_Reg lua_members[] =
  16. {
  17. {"addRef", lua_AIAgent_addRef},
  18. {"addScriptCallback", lua_AIAgent_addScriptCallback},
  19. {"getId", lua_AIAgent_getId},
  20. {"getNode", lua_AIAgent_getNode},
  21. {"getRefCount", lua_AIAgent_getRefCount},
  22. {"getStateMachine", lua_AIAgent_getStateMachine},
  23. {"isEnabled", lua_AIAgent_isEnabled},
  24. {"release", lua_AIAgent_release},
  25. {"removeScriptCallback", lua_AIAgent_removeScriptCallback},
  26. {"setEnabled", lua_AIAgent_setEnabled},
  27. {"setListener", lua_AIAgent_setListener},
  28. {NULL, NULL}
  29. };
  30. const luaL_Reg lua_statics[] =
  31. {
  32. {"create", lua_AIAgent_static_create},
  33. {NULL, NULL}
  34. };
  35. std::vector<std::string> scopePath;
  36. gameplay::ScriptUtil::registerClass("AIAgent", lua_members, NULL, lua_AIAgent__gc, lua_statics, scopePath);
  37. }
  38. static AIAgent* getInstance(lua_State* state)
  39. {
  40. void* userdata = luaL_checkudata(state, 1, "AIAgent");
  41. luaL_argcheck(state, userdata != NULL, 1, "'AIAgent' expected.");
  42. return (AIAgent*)((gameplay::ScriptUtil::LuaObject*)userdata)->instance;
  43. }
  44. int lua_AIAgent__gc(lua_State* state)
  45. {
  46. // Get the number of parameters.
  47. int paramCount = lua_gettop(state);
  48. // Attempt to match the parameters to a valid binding.
  49. switch (paramCount)
  50. {
  51. case 1:
  52. {
  53. if ((lua_type(state, 1) == LUA_TUSERDATA))
  54. {
  55. void* userdata = luaL_checkudata(state, 1, "AIAgent");
  56. luaL_argcheck(state, userdata != NULL, 1, "'AIAgent' expected.");
  57. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)userdata;
  58. if (object->owns)
  59. {
  60. AIAgent* instance = (AIAgent*)object->instance;
  61. SAFE_RELEASE(instance);
  62. }
  63. return 0;
  64. }
  65. lua_pushstring(state, "lua_AIAgent__gc - Failed to match the given parameters to a valid function signature.");
  66. lua_error(state);
  67. break;
  68. }
  69. default:
  70. {
  71. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  72. lua_error(state);
  73. break;
  74. }
  75. }
  76. return 0;
  77. }
  78. int lua_AIAgent_addRef(lua_State* state)
  79. {
  80. // Get the number of parameters.
  81. int paramCount = lua_gettop(state);
  82. // Attempt to match the parameters to a valid binding.
  83. switch (paramCount)
  84. {
  85. case 1:
  86. {
  87. if ((lua_type(state, 1) == LUA_TUSERDATA))
  88. {
  89. AIAgent* instance = getInstance(state);
  90. instance->addRef();
  91. return 0;
  92. }
  93. lua_pushstring(state, "lua_AIAgent_addRef - Failed to match the given parameters to a valid function signature.");
  94. lua_error(state);
  95. break;
  96. }
  97. default:
  98. {
  99. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  100. lua_error(state);
  101. break;
  102. }
  103. }
  104. return 0;
  105. }
  106. int lua_AIAgent_addScriptCallback(lua_State* state)
  107. {
  108. // Get the number of parameters.
  109. int paramCount = lua_gettop(state);
  110. // Attempt to match the parameters to a valid binding.
  111. switch (paramCount)
  112. {
  113. case 3:
  114. {
  115. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  116. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  117. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  118. {
  119. // Get parameter 1 off the stack.
  120. std::string param1 = gameplay::ScriptUtil::getString(2, true);
  121. // Get parameter 2 off the stack.
  122. std::string param2 = gameplay::ScriptUtil::getString(3, true);
  123. AIAgent* instance = getInstance(state);
  124. instance->addScriptCallback(param1, param2);
  125. return 0;
  126. }
  127. lua_pushstring(state, "lua_AIAgent_addScriptCallback - Failed to match the given parameters to a valid function signature.");
  128. lua_error(state);
  129. break;
  130. }
  131. default:
  132. {
  133. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  134. lua_error(state);
  135. break;
  136. }
  137. }
  138. return 0;
  139. }
  140. int lua_AIAgent_getId(lua_State* state)
  141. {
  142. // Get the number of parameters.
  143. int paramCount = lua_gettop(state);
  144. // Attempt to match the parameters to a valid binding.
  145. switch (paramCount)
  146. {
  147. case 1:
  148. {
  149. if ((lua_type(state, 1) == LUA_TUSERDATA))
  150. {
  151. AIAgent* instance = getInstance(state);
  152. const char* result = instance->getId();
  153. // Push the return value onto the stack.
  154. lua_pushstring(state, result);
  155. return 1;
  156. }
  157. lua_pushstring(state, "lua_AIAgent_getId - Failed to match the given parameters to a valid function signature.");
  158. lua_error(state);
  159. break;
  160. }
  161. default:
  162. {
  163. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  164. lua_error(state);
  165. break;
  166. }
  167. }
  168. return 0;
  169. }
  170. int lua_AIAgent_getNode(lua_State* state)
  171. {
  172. // Get the number of parameters.
  173. int paramCount = lua_gettop(state);
  174. // Attempt to match the parameters to a valid binding.
  175. switch (paramCount)
  176. {
  177. case 1:
  178. {
  179. if ((lua_type(state, 1) == LUA_TUSERDATA))
  180. {
  181. AIAgent* instance = getInstance(state);
  182. void* returnPtr = (void*)instance->getNode();
  183. if (returnPtr)
  184. {
  185. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  186. object->instance = returnPtr;
  187. object->owns = false;
  188. luaL_getmetatable(state, "Node");
  189. lua_setmetatable(state, -2);
  190. }
  191. else
  192. {
  193. lua_pushnil(state);
  194. }
  195. return 1;
  196. }
  197. lua_pushstring(state, "lua_AIAgent_getNode - Failed to match the given parameters to a valid function signature.");
  198. lua_error(state);
  199. break;
  200. }
  201. default:
  202. {
  203. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  204. lua_error(state);
  205. break;
  206. }
  207. }
  208. return 0;
  209. }
  210. int lua_AIAgent_getRefCount(lua_State* state)
  211. {
  212. // Get the number of parameters.
  213. int paramCount = lua_gettop(state);
  214. // Attempt to match the parameters to a valid binding.
  215. switch (paramCount)
  216. {
  217. case 1:
  218. {
  219. if ((lua_type(state, 1) == LUA_TUSERDATA))
  220. {
  221. AIAgent* instance = getInstance(state);
  222. unsigned int result = instance->getRefCount();
  223. // Push the return value onto the stack.
  224. lua_pushunsigned(state, result);
  225. return 1;
  226. }
  227. lua_pushstring(state, "lua_AIAgent_getRefCount - Failed to match the given parameters to a valid function signature.");
  228. lua_error(state);
  229. break;
  230. }
  231. default:
  232. {
  233. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  234. lua_error(state);
  235. break;
  236. }
  237. }
  238. return 0;
  239. }
  240. int lua_AIAgent_getStateMachine(lua_State* state)
  241. {
  242. // Get the number of parameters.
  243. int paramCount = lua_gettop(state);
  244. // Attempt to match the parameters to a valid binding.
  245. switch (paramCount)
  246. {
  247. case 1:
  248. {
  249. if ((lua_type(state, 1) == LUA_TUSERDATA))
  250. {
  251. AIAgent* instance = getInstance(state);
  252. void* returnPtr = (void*)instance->getStateMachine();
  253. if (returnPtr)
  254. {
  255. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  256. object->instance = returnPtr;
  257. object->owns = false;
  258. luaL_getmetatable(state, "AIStateMachine");
  259. lua_setmetatable(state, -2);
  260. }
  261. else
  262. {
  263. lua_pushnil(state);
  264. }
  265. return 1;
  266. }
  267. lua_pushstring(state, "lua_AIAgent_getStateMachine - Failed to match the given parameters to a valid function signature.");
  268. lua_error(state);
  269. break;
  270. }
  271. default:
  272. {
  273. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  274. lua_error(state);
  275. break;
  276. }
  277. }
  278. return 0;
  279. }
  280. int lua_AIAgent_isEnabled(lua_State* state)
  281. {
  282. // Get the number of parameters.
  283. int paramCount = lua_gettop(state);
  284. // Attempt to match the parameters to a valid binding.
  285. switch (paramCount)
  286. {
  287. case 1:
  288. {
  289. if ((lua_type(state, 1) == LUA_TUSERDATA))
  290. {
  291. AIAgent* instance = getInstance(state);
  292. bool result = instance->isEnabled();
  293. // Push the return value onto the stack.
  294. lua_pushboolean(state, result);
  295. return 1;
  296. }
  297. lua_pushstring(state, "lua_AIAgent_isEnabled - Failed to match the given parameters to a valid function signature.");
  298. lua_error(state);
  299. break;
  300. }
  301. default:
  302. {
  303. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  304. lua_error(state);
  305. break;
  306. }
  307. }
  308. return 0;
  309. }
  310. int lua_AIAgent_release(lua_State* state)
  311. {
  312. // Get the number of parameters.
  313. int paramCount = lua_gettop(state);
  314. // Attempt to match the parameters to a valid binding.
  315. switch (paramCount)
  316. {
  317. case 1:
  318. {
  319. if ((lua_type(state, 1) == LUA_TUSERDATA))
  320. {
  321. AIAgent* instance = getInstance(state);
  322. instance->release();
  323. return 0;
  324. }
  325. lua_pushstring(state, "lua_AIAgent_release - Failed to match the given parameters to a valid function signature.");
  326. lua_error(state);
  327. break;
  328. }
  329. default:
  330. {
  331. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  332. lua_error(state);
  333. break;
  334. }
  335. }
  336. return 0;
  337. }
  338. int lua_AIAgent_removeScriptCallback(lua_State* state)
  339. {
  340. // Get the number of parameters.
  341. int paramCount = lua_gettop(state);
  342. // Attempt to match the parameters to a valid binding.
  343. switch (paramCount)
  344. {
  345. case 3:
  346. {
  347. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  348. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  349. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  350. {
  351. // Get parameter 1 off the stack.
  352. std::string param1 = gameplay::ScriptUtil::getString(2, true);
  353. // Get parameter 2 off the stack.
  354. std::string param2 = gameplay::ScriptUtil::getString(3, true);
  355. AIAgent* instance = getInstance(state);
  356. instance->removeScriptCallback(param1, param2);
  357. return 0;
  358. }
  359. lua_pushstring(state, "lua_AIAgent_removeScriptCallback - Failed to match the given parameters to a valid function signature.");
  360. lua_error(state);
  361. break;
  362. }
  363. default:
  364. {
  365. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  366. lua_error(state);
  367. break;
  368. }
  369. }
  370. return 0;
  371. }
  372. int lua_AIAgent_setEnabled(lua_State* state)
  373. {
  374. // Get the number of parameters.
  375. int paramCount = lua_gettop(state);
  376. // Attempt to match the parameters to a valid binding.
  377. switch (paramCount)
  378. {
  379. case 2:
  380. {
  381. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  382. lua_type(state, 2) == LUA_TBOOLEAN)
  383. {
  384. // Get parameter 1 off the stack.
  385. bool param1 = gameplay::ScriptUtil::luaCheckBool(state, 2);
  386. AIAgent* instance = getInstance(state);
  387. instance->setEnabled(param1);
  388. return 0;
  389. }
  390. lua_pushstring(state, "lua_AIAgent_setEnabled - Failed to match the given parameters to a valid function signature.");
  391. lua_error(state);
  392. break;
  393. }
  394. default:
  395. {
  396. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  397. lua_error(state);
  398. break;
  399. }
  400. }
  401. return 0;
  402. }
  403. int lua_AIAgent_setListener(lua_State* state)
  404. {
  405. // Get the number of parameters.
  406. int paramCount = lua_gettop(state);
  407. // Attempt to match the parameters to a valid binding.
  408. switch (paramCount)
  409. {
  410. case 2:
  411. {
  412. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  413. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  414. {
  415. // Get parameter 1 off the stack.
  416. bool param1Valid;
  417. gameplay::ScriptUtil::LuaArray<AIAgent::Listener> param1 = gameplay::ScriptUtil::getObjectPointer<AIAgent::Listener>(2, "AIAgentListener", false, &param1Valid);
  418. if (!param1Valid)
  419. {
  420. lua_pushstring(state, "Failed to convert parameter 1 to type 'AIAgent::Listener'.");
  421. lua_error(state);
  422. }
  423. AIAgent* instance = getInstance(state);
  424. instance->setListener(param1);
  425. return 0;
  426. }
  427. lua_pushstring(state, "lua_AIAgent_setListener - Failed to match the given parameters to a valid function signature.");
  428. lua_error(state);
  429. break;
  430. }
  431. default:
  432. {
  433. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  434. lua_error(state);
  435. break;
  436. }
  437. }
  438. return 0;
  439. }
  440. int lua_AIAgent_static_create(lua_State* state)
  441. {
  442. // Get the number of parameters.
  443. int paramCount = lua_gettop(state);
  444. // Attempt to match the parameters to a valid binding.
  445. switch (paramCount)
  446. {
  447. case 0:
  448. {
  449. void* returnPtr = (void*)AIAgent::create();
  450. if (returnPtr)
  451. {
  452. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  453. object->instance = returnPtr;
  454. object->owns = true;
  455. luaL_getmetatable(state, "AIAgent");
  456. lua_setmetatable(state, -2);
  457. }
  458. else
  459. {
  460. lua_pushnil(state);
  461. }
  462. return 1;
  463. break;
  464. }
  465. default:
  466. {
  467. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  468. lua_error(state);
  469. break;
  470. }
  471. }
  472. return 0;
  473. }
  474. }