lua_AIState.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. namespace gameplay
  10. {
  11. void luaRegister_AIState()
  12. {
  13. const luaL_Reg lua_members[] =
  14. {
  15. {"addRef", lua_AIState_addRef},
  16. {"getId", lua_AIState_getId},
  17. {"getRefCount", lua_AIState_getRefCount},
  18. {"release", lua_AIState_release},
  19. {"setListener", lua_AIState_setListener},
  20. {NULL, NULL}
  21. };
  22. const luaL_Reg lua_statics[] =
  23. {
  24. {"create", lua_AIState_static_create},
  25. {NULL, NULL}
  26. };
  27. std::vector<std::string> scopePath;
  28. ScriptUtil::registerClass("AIState", lua_members, NULL, lua_AIState__gc, lua_statics, scopePath);
  29. }
  30. static AIState* getInstance(lua_State* state)
  31. {
  32. void* userdata = luaL_checkudata(state, 1, "AIState");
  33. luaL_argcheck(state, userdata != NULL, 1, "'AIState' expected.");
  34. return (AIState*)((ScriptUtil::LuaObject*)userdata)->instance;
  35. }
  36. int lua_AIState__gc(lua_State* state)
  37. {
  38. // Get the number of parameters.
  39. int paramCount = lua_gettop(state);
  40. // Attempt to match the parameters to a valid binding.
  41. switch (paramCount)
  42. {
  43. case 1:
  44. {
  45. if ((lua_type(state, 1) == LUA_TUSERDATA))
  46. {
  47. void* userdata = luaL_checkudata(state, 1, "AIState");
  48. luaL_argcheck(state, userdata != NULL, 1, "'AIState' expected.");
  49. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)userdata;
  50. if (object->owns)
  51. {
  52. AIState* instance = (AIState*)object->instance;
  53. SAFE_RELEASE(instance);
  54. }
  55. return 0;
  56. }
  57. else
  58. {
  59. lua_pushstring(state, "lua_AIState__gc - Failed to match the given parameters to a valid function signature.");
  60. lua_error(state);
  61. }
  62. break;
  63. }
  64. default:
  65. {
  66. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  67. lua_error(state);
  68. break;
  69. }
  70. }
  71. return 0;
  72. }
  73. int lua_AIState_addRef(lua_State* state)
  74. {
  75. // Get the number of parameters.
  76. int paramCount = lua_gettop(state);
  77. // Attempt to match the parameters to a valid binding.
  78. switch (paramCount)
  79. {
  80. case 1:
  81. {
  82. if ((lua_type(state, 1) == LUA_TUSERDATA))
  83. {
  84. AIState* instance = getInstance(state);
  85. instance->addRef();
  86. return 0;
  87. }
  88. else
  89. {
  90. lua_pushstring(state, "lua_AIState_addRef - Failed to match the given parameters to a valid function signature.");
  91. lua_error(state);
  92. }
  93. break;
  94. }
  95. default:
  96. {
  97. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  98. lua_error(state);
  99. break;
  100. }
  101. }
  102. return 0;
  103. }
  104. int lua_AIState_getId(lua_State* state)
  105. {
  106. // Get the number of parameters.
  107. int paramCount = lua_gettop(state);
  108. // Attempt to match the parameters to a valid binding.
  109. switch (paramCount)
  110. {
  111. case 1:
  112. {
  113. if ((lua_type(state, 1) == LUA_TUSERDATA))
  114. {
  115. AIState* instance = getInstance(state);
  116. const char* result = instance->getId();
  117. // Push the return value onto the stack.
  118. lua_pushstring(state, result);
  119. return 1;
  120. }
  121. else
  122. {
  123. lua_pushstring(state, "lua_AIState_getId - Failed to match the given parameters to a valid function signature.");
  124. lua_error(state);
  125. }
  126. break;
  127. }
  128. default:
  129. {
  130. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  131. lua_error(state);
  132. break;
  133. }
  134. }
  135. return 0;
  136. }
  137. int lua_AIState_getRefCount(lua_State* state)
  138. {
  139. // Get the number of parameters.
  140. int paramCount = lua_gettop(state);
  141. // Attempt to match the parameters to a valid binding.
  142. switch (paramCount)
  143. {
  144. case 1:
  145. {
  146. if ((lua_type(state, 1) == LUA_TUSERDATA))
  147. {
  148. AIState* instance = getInstance(state);
  149. unsigned int result = instance->getRefCount();
  150. // Push the return value onto the stack.
  151. lua_pushunsigned(state, result);
  152. return 1;
  153. }
  154. else
  155. {
  156. lua_pushstring(state, "lua_AIState_getRefCount - Failed to match the given parameters to a valid function signature.");
  157. lua_error(state);
  158. }
  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_AIState_release(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. AIState* instance = getInstance(state);
  182. instance->release();
  183. return 0;
  184. }
  185. else
  186. {
  187. lua_pushstring(state, "lua_AIState_release - Failed to match the given parameters to a valid function signature.");
  188. lua_error(state);
  189. }
  190. break;
  191. }
  192. default:
  193. {
  194. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  195. lua_error(state);
  196. break;
  197. }
  198. }
  199. return 0;
  200. }
  201. int lua_AIState_setListener(lua_State* state)
  202. {
  203. // Get the number of parameters.
  204. int paramCount = lua_gettop(state);
  205. // Attempt to match the parameters to a valid binding.
  206. switch (paramCount)
  207. {
  208. case 2:
  209. {
  210. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  211. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  212. {
  213. // Get parameter 1 off the stack.
  214. AIState::Listener* param1 = ScriptUtil::getObjectPointer<AIState::Listener>(2, "AIStateListener", false);
  215. AIState* instance = getInstance(state);
  216. instance->setListener(param1);
  217. return 0;
  218. }
  219. else
  220. {
  221. lua_pushstring(state, "lua_AIState_setListener - Failed to match the given parameters to a valid function signature.");
  222. lua_error(state);
  223. }
  224. break;
  225. }
  226. default:
  227. {
  228. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  229. lua_error(state);
  230. break;
  231. }
  232. }
  233. return 0;
  234. }
  235. int lua_AIState_static_create(lua_State* state)
  236. {
  237. // Get the number of parameters.
  238. int paramCount = lua_gettop(state);
  239. // Attempt to match the parameters to a valid binding.
  240. switch (paramCount)
  241. {
  242. case 1:
  243. {
  244. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  245. {
  246. // Get parameter 1 off the stack.
  247. const char* param1 = ScriptUtil::getString(1, false);
  248. void* returnPtr = (void*)AIState::create(param1);
  249. if (returnPtr)
  250. {
  251. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  252. object->instance = returnPtr;
  253. object->owns = false;
  254. luaL_getmetatable(state, "AIState");
  255. lua_setmetatable(state, -2);
  256. }
  257. else
  258. {
  259. lua_pushnil(state);
  260. }
  261. return 1;
  262. }
  263. else
  264. {
  265. lua_pushstring(state, "lua_AIState_static_create - 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 1).");
  273. lua_error(state);
  274. break;
  275. }
  276. }
  277. return 0;
  278. }
  279. }