lua_AIAgent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. {"addScript", lua_AIAgent_addScript},
  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. {"removeScript", lua_AIAgent_removeScript},
  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_addScript(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 2:
  114. {
  115. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  116. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  117. {
  118. // Get parameter 1 off the stack.
  119. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  120. AIAgent* instance = getInstance(state);
  121. int result = instance->addScript(param1);
  122. // Push the return value onto the stack.
  123. lua_pushinteger(state, result);
  124. return 1;
  125. }
  126. lua_pushstring(state, "lua_AIAgent_addScript - Failed to match the given parameters to a valid function signature.");
  127. lua_error(state);
  128. break;
  129. }
  130. default:
  131. {
  132. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  133. lua_error(state);
  134. break;
  135. }
  136. }
  137. return 0;
  138. }
  139. int lua_AIAgent_getId(lua_State* state)
  140. {
  141. // Get the number of parameters.
  142. int paramCount = lua_gettop(state);
  143. // Attempt to match the parameters to a valid binding.
  144. switch (paramCount)
  145. {
  146. case 1:
  147. {
  148. if ((lua_type(state, 1) == LUA_TUSERDATA))
  149. {
  150. AIAgent* instance = getInstance(state);
  151. const char* result = instance->getId();
  152. // Push the return value onto the stack.
  153. lua_pushstring(state, result);
  154. return 1;
  155. }
  156. lua_pushstring(state, "lua_AIAgent_getId - Failed to match the given parameters to a valid function signature.");
  157. lua_error(state);
  158. break;
  159. }
  160. default:
  161. {
  162. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  163. lua_error(state);
  164. break;
  165. }
  166. }
  167. return 0;
  168. }
  169. int lua_AIAgent_getNode(lua_State* state)
  170. {
  171. // Get the number of parameters.
  172. int paramCount = lua_gettop(state);
  173. // Attempt to match the parameters to a valid binding.
  174. switch (paramCount)
  175. {
  176. case 1:
  177. {
  178. if ((lua_type(state, 1) == LUA_TUSERDATA))
  179. {
  180. AIAgent* instance = getInstance(state);
  181. void* returnPtr = (void*)instance->getNode();
  182. if (returnPtr)
  183. {
  184. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  185. object->instance = returnPtr;
  186. object->owns = false;
  187. luaL_getmetatable(state, "Node");
  188. lua_setmetatable(state, -2);
  189. }
  190. else
  191. {
  192. lua_pushnil(state);
  193. }
  194. return 1;
  195. }
  196. lua_pushstring(state, "lua_AIAgent_getNode - Failed to match the given parameters to a valid function signature.");
  197. lua_error(state);
  198. break;
  199. }
  200. default:
  201. {
  202. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  203. lua_error(state);
  204. break;
  205. }
  206. }
  207. return 0;
  208. }
  209. int lua_AIAgent_getRefCount(lua_State* state)
  210. {
  211. // Get the number of parameters.
  212. int paramCount = lua_gettop(state);
  213. // Attempt to match the parameters to a valid binding.
  214. switch (paramCount)
  215. {
  216. case 1:
  217. {
  218. if ((lua_type(state, 1) == LUA_TUSERDATA))
  219. {
  220. AIAgent* instance = getInstance(state);
  221. unsigned int result = instance->getRefCount();
  222. // Push the return value onto the stack.
  223. lua_pushunsigned(state, result);
  224. return 1;
  225. }
  226. lua_pushstring(state, "lua_AIAgent_getRefCount - Failed to match the given parameters to a valid function signature.");
  227. lua_error(state);
  228. break;
  229. }
  230. default:
  231. {
  232. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  233. lua_error(state);
  234. break;
  235. }
  236. }
  237. return 0;
  238. }
  239. int lua_AIAgent_getStateMachine(lua_State* state)
  240. {
  241. // Get the number of parameters.
  242. int paramCount = lua_gettop(state);
  243. // Attempt to match the parameters to a valid binding.
  244. switch (paramCount)
  245. {
  246. case 1:
  247. {
  248. if ((lua_type(state, 1) == LUA_TUSERDATA))
  249. {
  250. AIAgent* instance = getInstance(state);
  251. void* returnPtr = (void*)instance->getStateMachine();
  252. if (returnPtr)
  253. {
  254. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  255. object->instance = returnPtr;
  256. object->owns = false;
  257. luaL_getmetatable(state, "AIStateMachine");
  258. lua_setmetatable(state, -2);
  259. }
  260. else
  261. {
  262. lua_pushnil(state);
  263. }
  264. return 1;
  265. }
  266. lua_pushstring(state, "lua_AIAgent_getStateMachine - Failed to match the given parameters to a valid function signature.");
  267. lua_error(state);
  268. break;
  269. }
  270. default:
  271. {
  272. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  273. lua_error(state);
  274. break;
  275. }
  276. }
  277. return 0;
  278. }
  279. int lua_AIAgent_isEnabled(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 1:
  287. {
  288. if ((lua_type(state, 1) == LUA_TUSERDATA))
  289. {
  290. AIAgent* instance = getInstance(state);
  291. bool result = instance->isEnabled();
  292. // Push the return value onto the stack.
  293. lua_pushboolean(state, result);
  294. return 1;
  295. }
  296. lua_pushstring(state, "lua_AIAgent_isEnabled - Failed to match the given parameters to a valid function signature.");
  297. lua_error(state);
  298. break;
  299. }
  300. default:
  301. {
  302. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  303. lua_error(state);
  304. break;
  305. }
  306. }
  307. return 0;
  308. }
  309. int lua_AIAgent_release(lua_State* state)
  310. {
  311. // Get the number of parameters.
  312. int paramCount = lua_gettop(state);
  313. // Attempt to match the parameters to a valid binding.
  314. switch (paramCount)
  315. {
  316. case 1:
  317. {
  318. if ((lua_type(state, 1) == LUA_TUSERDATA))
  319. {
  320. AIAgent* instance = getInstance(state);
  321. instance->release();
  322. return 0;
  323. }
  324. lua_pushstring(state, "lua_AIAgent_release - Failed to match the given parameters to a valid function signature.");
  325. lua_error(state);
  326. break;
  327. }
  328. default:
  329. {
  330. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  331. lua_error(state);
  332. break;
  333. }
  334. }
  335. return 0;
  336. }
  337. int lua_AIAgent_removeScript(lua_State* state)
  338. {
  339. // Get the number of parameters.
  340. int paramCount = lua_gettop(state);
  341. // Attempt to match the parameters to a valid binding.
  342. switch (paramCount)
  343. {
  344. case 2:
  345. {
  346. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  347. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  348. {
  349. // Get parameter 1 off the stack.
  350. const char* param1 = gameplay::ScriptUtil::getString(2, false);
  351. AIAgent* instance = getInstance(state);
  352. bool result = instance->removeScript(param1);
  353. // Push the return value onto the stack.
  354. lua_pushboolean(state, result);
  355. return 1;
  356. }
  357. lua_pushstring(state, "lua_AIAgent_removeScript - Failed to match the given parameters to a valid function signature.");
  358. lua_error(state);
  359. break;
  360. }
  361. default:
  362. {
  363. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  364. lua_error(state);
  365. break;
  366. }
  367. }
  368. return 0;
  369. }
  370. int lua_AIAgent_setEnabled(lua_State* state)
  371. {
  372. // Get the number of parameters.
  373. int paramCount = lua_gettop(state);
  374. // Attempt to match the parameters to a valid binding.
  375. switch (paramCount)
  376. {
  377. case 2:
  378. {
  379. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  380. lua_type(state, 2) == LUA_TBOOLEAN)
  381. {
  382. // Get parameter 1 off the stack.
  383. bool param1 = gameplay::ScriptUtil::luaCheckBool(state, 2);
  384. AIAgent* instance = getInstance(state);
  385. instance->setEnabled(param1);
  386. return 0;
  387. }
  388. lua_pushstring(state, "lua_AIAgent_setEnabled - Failed to match the given parameters to a valid function signature.");
  389. lua_error(state);
  390. break;
  391. }
  392. default:
  393. {
  394. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  395. lua_error(state);
  396. break;
  397. }
  398. }
  399. return 0;
  400. }
  401. int lua_AIAgent_setListener(lua_State* state)
  402. {
  403. // Get the number of parameters.
  404. int paramCount = lua_gettop(state);
  405. // Attempt to match the parameters to a valid binding.
  406. switch (paramCount)
  407. {
  408. case 2:
  409. {
  410. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  411. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  412. {
  413. // Get parameter 1 off the stack.
  414. bool param1Valid;
  415. gameplay::ScriptUtil::LuaArray<AIAgent::Listener> param1 = gameplay::ScriptUtil::getObjectPointer<AIAgent::Listener>(2, "AIAgentListener", false, &param1Valid);
  416. if (!param1Valid)
  417. {
  418. lua_pushstring(state, "Failed to convert parameter 1 to type 'AIAgent::Listener'.");
  419. lua_error(state);
  420. }
  421. AIAgent* instance = getInstance(state);
  422. instance->setListener(param1);
  423. return 0;
  424. }
  425. lua_pushstring(state, "lua_AIAgent_setListener - Failed to match the given parameters to a valid function signature.");
  426. lua_error(state);
  427. break;
  428. }
  429. default:
  430. {
  431. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  432. lua_error(state);
  433. break;
  434. }
  435. }
  436. return 0;
  437. }
  438. int lua_AIAgent_static_create(lua_State* state)
  439. {
  440. // Get the number of parameters.
  441. int paramCount = lua_gettop(state);
  442. // Attempt to match the parameters to a valid binding.
  443. switch (paramCount)
  444. {
  445. case 0:
  446. {
  447. void* returnPtr = (void*)AIAgent::create();
  448. if (returnPtr)
  449. {
  450. gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
  451. object->instance = returnPtr;
  452. object->owns = true;
  453. luaL_getmetatable(state, "AIAgent");
  454. lua_setmetatable(state, -2);
  455. }
  456. else
  457. {
  458. lua_pushnil(state);
  459. }
  460. return 1;
  461. break;
  462. }
  463. default:
  464. {
  465. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  466. lua_error(state);
  467. break;
  468. }
  469. }
  470. return 0;
  471. }
  472. }