lua_AIAgent.cpp 16 KB

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