lua_unit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "lua_stack.h"
  24. #include "lua_environment.h"
  25. #include "unit.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. static int unit_node(lua_State* L)
  30. {
  31. LuaStack stack(L);
  32. Unit* unit = stack.get_unit(1);
  33. const char* name = stack.get_string(2);
  34. stack.push_int32(unit->node(name));
  35. return 1;
  36. }
  37. //-----------------------------------------------------------------------------
  38. static int unit_has_node(lua_State* L)
  39. {
  40. LuaStack stack(L);
  41. Unit* unit = stack.get_unit(1);
  42. const char* name = stack.get_string(2);
  43. stack.push_bool(unit->has_node(name));
  44. return 1;
  45. }
  46. //-----------------------------------------------------------------------------
  47. static int unit_num_nodes(lua_State* L)
  48. {
  49. LuaStack stack(L);
  50. Unit* unit = stack.get_unit(1);
  51. stack.push_int32(unit->num_nodes());
  52. return 1;
  53. }
  54. //-----------------------------------------------------------------------------
  55. static int unit_local_position(lua_State* L)
  56. {
  57. LuaStack stack(L);
  58. Unit* unit = stack.get_unit(1);
  59. const int32_t node = stack.get_int(2);
  60. stack.push_vector3(unit->local_position(node));
  61. return 1;
  62. }
  63. //-----------------------------------------------------------------------------
  64. static int unit_local_rotation(lua_State* L)
  65. {
  66. LuaStack stack(L);
  67. Unit* unit = stack.get_unit(1);
  68. const int32_t node = stack.get_int(2);
  69. stack.push_quaternion(unit->local_rotation(node));
  70. return 1;
  71. }
  72. //-----------------------------------------------------------------------------
  73. static int unit_local_pose(lua_State* L)
  74. {
  75. LuaStack stack(L);
  76. Unit* unit = stack.get_unit(1);
  77. const int32_t node = stack.get_int(2);
  78. stack.push_matrix4x4(unit->local_pose(node));
  79. return 1;
  80. }
  81. //-----------------------------------------------------------------------------
  82. static int unit_world_position(lua_State* L)
  83. {
  84. LuaStack stack(L);
  85. Unit* unit = stack.get_unit(1);
  86. const int32_t node = stack.get_int(2);
  87. stack.push_vector3(unit->world_position(node));
  88. return 1;
  89. }
  90. //-----------------------------------------------------------------------------
  91. static int unit_world_rotation(lua_State* L)
  92. {
  93. LuaStack stack(L);
  94. Unit* unit = stack.get_unit(1);
  95. const int32_t node = stack.get_int(2);
  96. stack.push_quaternion(unit->world_rotation(node));
  97. return 1;
  98. }
  99. //-----------------------------------------------------------------------------
  100. static int unit_world_pose(lua_State* L)
  101. {
  102. LuaStack stack(L);
  103. Unit* unit = stack.get_unit(1);
  104. const int32_t node = stack.get_int(2);
  105. stack.push_matrix4x4(unit->world_pose(node));
  106. return 1;
  107. }
  108. //-----------------------------------------------------------------------------
  109. static int unit_set_local_position(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. Unit* unit = stack.get_unit(1);
  113. const int32_t node = stack.get_int(2);
  114. Vector3& pos = stack.get_vector3(3);
  115. unit->set_local_position(node, pos);
  116. return 0;
  117. }
  118. //-----------------------------------------------------------------------------
  119. static int unit_set_local_rotation(lua_State* L)
  120. {
  121. LuaStack stack(L);
  122. Unit* unit = stack.get_unit(1);
  123. const int32_t node = stack.get_int(2);
  124. Quaternion& rot = stack.get_quaternion(3);
  125. unit->set_local_rotation(node, rot);
  126. return 0;
  127. }
  128. //-----------------------------------------------------------------------------
  129. static int unit_set_local_pose(lua_State* L)
  130. {
  131. LuaStack stack(L);
  132. Unit* unit = stack.get_unit(1);
  133. const int32_t node = stack.get_int(2);
  134. Matrix4x4& pose = stack.get_matrix4x4(3);
  135. unit->set_local_pose(node, pose);
  136. return 0;
  137. }
  138. //-----------------------------------------------------------------------------
  139. static int unit_link_node(lua_State* L)
  140. {
  141. LuaStack stack(L);
  142. Unit* unit = stack.get_unit(1);
  143. const int32_t child = stack.get_int(2);
  144. const int32_t parent = stack.get_int(3);
  145. unit->link_node(child, parent);
  146. return 0;
  147. }
  148. //-----------------------------------------------------------------------------
  149. static int unit_unlink_node(lua_State* L)
  150. {
  151. LuaStack stack(L);
  152. Unit* unit = stack.get_unit(1);
  153. const int32_t child = stack.get_int(2);
  154. unit->unlink_node(child);
  155. return 0;
  156. }
  157. //-----------------------------------------------------------------------------
  158. static int unit_camera(lua_State* L)
  159. {
  160. LuaStack stack(L);
  161. Unit* unit = stack.get_unit(1);
  162. if (stack.is_number(2))
  163. {
  164. stack.push_camera(unit->camera((uint32_t) stack.get_int(2)));
  165. return 1;
  166. }
  167. stack.push_camera(unit->camera(stack.get_string(2)));
  168. return 1;
  169. }
  170. //-----------------------------------------------------------------------------
  171. static int unit_material(lua_State* L)
  172. {
  173. LuaStack stack(L);
  174. Unit* unit = stack.get_unit(1);
  175. if (stack.is_number(2))
  176. {
  177. stack.push_material(unit->material((uint32_t) stack.get_int(2)));
  178. return 1;
  179. }
  180. stack.push_material(unit->material(stack.get_string(2)));
  181. return 1;
  182. }
  183. //-----------------------------------------------------------------------------
  184. static int unit_mesh(lua_State* L)
  185. {
  186. LuaStack stack(L);
  187. Unit* unit = stack.get_unit(1);
  188. if (stack.is_number(2))
  189. {
  190. stack.push_mesh(unit->mesh((uint32_t) stack.get_int(2)));
  191. return 1;
  192. }
  193. stack.push_mesh(unit->mesh(stack.get_string(2)));
  194. return 1;
  195. }
  196. //-----------------------------------------------------------------------------
  197. static int unit_sprite(lua_State* L)
  198. {
  199. LuaStack stack(L);
  200. Unit* unit = stack.get_unit(1);
  201. if (stack.is_number(2))
  202. {
  203. stack.push_sprite(unit->sprite((uint32_t) stack.get_int(2)));
  204. return 1;
  205. }
  206. stack.push_sprite(unit->sprite(stack.get_string(2)));
  207. return 1;
  208. }
  209. //-----------------------------------------------------------------------------
  210. static int unit_actor(lua_State* L)
  211. {
  212. LuaStack stack(L);
  213. Unit* unit = stack.get_unit(1);
  214. if (stack.is_number(2))
  215. {
  216. stack.push_actor(unit->actor((uint32_t) stack.get_int(2)));
  217. return 1;
  218. }
  219. stack.push_actor(unit->actor(stack.get_string(2)));
  220. return 1;
  221. }
  222. //-----------------------------------------------------------------------------
  223. static int unit_controller(lua_State* L)
  224. {
  225. LuaStack stack(L);
  226. Unit* unit = stack.get_unit(1);
  227. Controller* controller = unit->controller();
  228. controller ? stack.push_controller(controller) : stack.push_nil();
  229. return 1;
  230. }
  231. //-----------------------------------------------------------------------------
  232. static int unit_is_a(lua_State* L)
  233. {
  234. LuaStack stack(L);
  235. Unit* unit = stack.get_unit(1);
  236. const char* name = stack.get_string(2);
  237. stack.push_bool(unit->is_a(name));
  238. return 1;
  239. }
  240. //-----------------------------------------------------------------------------
  241. static int unit_play_sprite_animation(lua_State* L)
  242. {
  243. LuaStack stack(L);
  244. Unit* unit = stack.get_unit(1);
  245. const char* anim = stack.get_string(2);
  246. unit->play_sprite_animation(anim, stack.get_bool(3));
  247. return 0;
  248. }
  249. //-----------------------------------------------------------------------------
  250. static int unit_stop_sprite_animation(lua_State* L)
  251. {
  252. LuaStack stack(L);
  253. Unit* unit = stack.get_unit(1);
  254. unit->stop_sprite_animation();
  255. return 0;
  256. }
  257. //-----------------------------------------------------------------------------
  258. static int unit_has_key(lua_State* L)
  259. {
  260. LuaStack stack(L);
  261. Unit* unit = stack.get_unit(1);
  262. stack.push_bool(unit->has_key(stack.get_string(2)));
  263. return 1;
  264. }
  265. //-----------------------------------------------------------------------------
  266. static int unit_get_key(lua_State* L)
  267. {
  268. LuaStack stack(L);
  269. Unit* unit = stack.get_unit(1);
  270. const char* key = stack.get_string(2);
  271. switch (unit->value_type(key))
  272. {
  273. case ValueType::BOOL:
  274. {
  275. bool val;
  276. unit->get_key(key, val);
  277. stack.push_bool(val);
  278. return 1;
  279. }
  280. case ValueType::FLOAT:
  281. {
  282. float val;
  283. unit->get_key(key, val);
  284. stack.push_float(val);
  285. return 1;
  286. }
  287. case ValueType::STRING:
  288. {
  289. StringId32 val;
  290. unit->get_key(key, val);
  291. stack.push_uint32(val);
  292. return 1;
  293. }
  294. case ValueType::VECTOR3:
  295. {
  296. Vector3 val;
  297. unit->get_key(key, val);
  298. stack.push_vector3(val);
  299. return 1;
  300. }
  301. default: CE_FATAL("Unknown value type"); break;
  302. }
  303. return 0;
  304. }
  305. //-----------------------------------------------------------------------------
  306. static int unit_set_key(lua_State* L)
  307. {
  308. LuaStack stack(L);
  309. Unit* unit = stack.get_unit(1);
  310. const char* key = stack.get_string(2);
  311. switch (stack.value_type(3))
  312. {
  313. case LUA_TBOOLEAN: unit->set_key(key, stack.get_bool(3)); break;
  314. case LUA_TNUMBER: unit->set_key(key, stack.get_float(3)); break;
  315. case LUA_TSTRING: unit->set_key(key, stack.get_string(3)); break;
  316. case LUA_TLIGHTUSERDATA: unit->set_key(key, stack.get_vector3(3)); break;
  317. default: CE_FATAL("Unsupported value type"); break; // FIXME use LUA_ASSERT
  318. }
  319. return 0;
  320. }
  321. //-----------------------------------------------------------------------------
  322. void load_unit(LuaEnvironment& env)
  323. {
  324. env.load_module_function("Unit", "node", unit_node);
  325. env.load_module_function("Unit", "has_node", unit_has_node);
  326. env.load_module_function("Unit", "num_nodes", unit_num_nodes);
  327. env.load_module_function("Unit", "local_position", unit_local_position);
  328. env.load_module_function("Unit", "local_rotation", unit_local_rotation);
  329. env.load_module_function("Unit", "local_pose", unit_local_pose);
  330. env.load_module_function("Unit", "world_position", unit_world_position);
  331. env.load_module_function("Unit", "world_rotation", unit_world_rotation);
  332. env.load_module_function("Unit", "world_pose", unit_world_pose);
  333. env.load_module_function("Unit", "set_local_position", unit_set_local_position);
  334. env.load_module_function("Unit", "set_local_rotation", unit_set_local_rotation);
  335. env.load_module_function("Unit", "set_local_pose", unit_set_local_pose);
  336. env.load_module_function("Unit", "link_node", unit_link_node);
  337. env.load_module_function("Unit", "unlink_node", unit_unlink_node);
  338. env.load_module_function("Unit", "camera", unit_camera);
  339. env.load_module_function("Unit", "material", unit_material);
  340. env.load_module_function("Unit", "mesh", unit_mesh);
  341. env.load_module_function("Unit", "sprite", unit_sprite);
  342. env.load_module_function("Unit", "actor", unit_actor);
  343. env.load_module_function("Unit", "controller", unit_controller);
  344. env.load_module_function("Unit", "is_a", unit_is_a);
  345. env.load_module_function("Unit", "play_sprite_animation", unit_play_sprite_animation);
  346. env.load_module_function("Unit", "stop_sprite_animation", unit_stop_sprite_animation);
  347. env.load_module_function("Unit", "has_key", unit_has_key);
  348. env.load_module_function("Unit", "get_key", unit_get_key);
  349. env.load_module_function("Unit", "set_key", unit_set_key);
  350. }
  351. } // namespace crown