lua_world.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "world.h"
  26. #include "gui.h"
  27. #include "temp_allocator.h"
  28. #include "array.h"
  29. #include "lua_assert.h"
  30. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. static int world_spawn_unit(lua_State* L)
  34. {
  35. LuaStack stack(L);
  36. World* world = stack.get_world(1);
  37. const char* name = stack.get_string(2);
  38. const Vector3& pos = stack.num_args() > 2 ? stack.get_vector3(3) : vector3::ZERO;
  39. const Quaternion& rot = stack.num_args() > 3 ? stack.get_quaternion(4) : quaternion::IDENTITY;
  40. LUA_ASSERT(device()->resource_manager()->can_get(UNIT_EXTENSION, name), stack, "Unit '%s' not found", name);
  41. UnitId unit = world->spawn_unit(name, pos, rot);
  42. stack.push_unit(world->get_unit(unit));
  43. return 1;
  44. }
  45. //-----------------------------------------------------------------------------
  46. static int world_destroy_unit(lua_State* L)
  47. {
  48. LuaStack stack(L);
  49. stack.get_world(1)->destroy_unit(stack.get_unit(2)->id());
  50. return 0;
  51. }
  52. //-----------------------------------------------------------------------------
  53. static int world_num_units(lua_State* L)
  54. {
  55. LuaStack stack(L);
  56. stack.push_uint32(stack.get_world(1)->num_units());
  57. return 1;
  58. }
  59. //-----------------------------------------------------------------------------
  60. static int world_units(lua_State* L)
  61. {
  62. LuaStack stack(L);
  63. World* world = stack.get_world(1);
  64. TempAllocator1024 alloc;
  65. Array<UnitId> all_units(alloc);
  66. world->units(all_units);
  67. stack.push_table();
  68. for (uint32_t i = 0; i < array::size(all_units); i++)
  69. {
  70. stack.push_key_begin((int32_t) i + 1);
  71. stack.push_unit(world->get_unit(all_units[i]));
  72. stack.push_key_end();
  73. }
  74. return 1;
  75. }
  76. //-----------------------------------------------------------------------------
  77. static int world_update_animations(lua_State* L)
  78. {
  79. LuaStack stack(L);
  80. stack.get_world(1)->update_animations(stack.get_float(2));
  81. return 0;
  82. }
  83. //-----------------------------------------------------------------------------
  84. static int world_update_scene(lua_State* L)
  85. {
  86. LuaStack stack(L);
  87. stack.get_world(1)->update_scene(stack.get_float(2));
  88. return 0;
  89. }
  90. //-----------------------------------------------------------------------------
  91. static int world_update(lua_State* L)
  92. {
  93. LuaStack stack(L);
  94. stack.get_world(1)->update(stack.get_float(2));
  95. return 0;
  96. }
  97. //-----------------------------------------------------------------------------
  98. static int world_play_sound(lua_State* L)
  99. {
  100. LuaStack stack(L);
  101. World* world = stack.get_world(1);
  102. const char* name = stack.get_string(2);
  103. int32_t nargs = stack.num_args();
  104. const bool loop = nargs > 2 ? stack.get_bool(3) : false;
  105. const float volume = nargs > 3 ? stack.get_float(4) : 1.0f;
  106. const Vector3& pos = nargs > 4 ? stack.get_vector3(5) : vector3::ZERO;
  107. const float range = nargs > 5 ? stack.get_float(6) : 1000.0f;
  108. LUA_ASSERT(device()->resource_manager()->can_get(SOUND_EXTENSION, name), stack, "Sound '%s' not found", name);
  109. stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
  110. return 1;
  111. }
  112. //-----------------------------------------------------------------------------
  113. static int world_stop_sound(lua_State* L)
  114. {
  115. LuaStack stack(L);
  116. stack.get_world(1)->stop_sound(stack.get_sound_instance_id(2));
  117. return 0;
  118. }
  119. //-----------------------------------------------------------------------------
  120. static int world_link_sound(lua_State* L)
  121. {
  122. LuaStack stack(L);
  123. stack.get_world(1)->link_sound(stack.get_sound_instance_id(2),
  124. stack.get_unit(3),
  125. stack.get_int(4));
  126. return 0;
  127. }
  128. //-----------------------------------------------------------------------------
  129. static int world_set_listener_pose(lua_State* L)
  130. {
  131. LuaStack stack(L);
  132. stack.get_world(1)->set_listener_pose(stack.get_matrix4x4(2));
  133. return 0;
  134. }
  135. //-----------------------------------------------------------------------------
  136. static int world_set_sound_position(lua_State* L)
  137. {
  138. LuaStack stack(L);
  139. stack.get_world(1)->set_sound_position(stack.get_sound_instance_id(2),
  140. stack.get_vector3(3));
  141. return 0;
  142. }
  143. //-----------------------------------------------------------------------------
  144. static int world_set_sound_range(lua_State* L)
  145. {
  146. LuaStack stack(L);
  147. stack.get_world(1)->set_sound_range(stack.get_sound_instance_id(2),
  148. stack.get_float(3));
  149. return 0;
  150. }
  151. //-----------------------------------------------------------------------------
  152. static int world_set_sound_volume(lua_State* L)
  153. {
  154. LuaStack stack(L);
  155. stack.get_world(1)->set_sound_volume(stack.get_sound_instance_id(2),
  156. stack.get_float(3));
  157. return 0;
  158. }
  159. //-----------------------------------------------------------------------------
  160. static int world_create_window_gui(lua_State* L)
  161. {
  162. LuaStack stack(L);
  163. World* world = stack.get_world(1);
  164. GuiId id = world->create_window_gui(stack.get_int(2), stack.get_int(3), stack.get_string(4));
  165. stack.push_gui(world->get_gui(id));
  166. return 1;
  167. }
  168. //-----------------------------------------------------------------------------
  169. static int world_destroy_gui(lua_State* L)
  170. {
  171. LuaStack stack(L);
  172. stack.get_world(1)->destroy_gui(stack.get_gui(2)->id());
  173. return 0;
  174. }
  175. //-----------------------------------------------------------------------------
  176. static int world_create_debug_line(lua_State* L)
  177. {
  178. LuaStack stack(L);
  179. stack.push_debug_line(stack.get_world(1)->create_debug_line(stack.get_bool(2)));
  180. return 1;
  181. }
  182. //-----------------------------------------------------------------------------
  183. static int world_destroy_debug_line(lua_State* L)
  184. {
  185. LuaStack stack(L);
  186. stack.get_world(1)->destroy_debug_line(stack.get_debug_line(2));
  187. return 0;
  188. }
  189. //-----------------------------------------------------------------------------
  190. static int world_load_level(lua_State* L)
  191. {
  192. LuaStack stack(L);
  193. stack.get_world(1)->load_level(stack.get_string(2));
  194. return 0;
  195. }
  196. //-----------------------------------------------------------------------------
  197. static int world_physics_world(lua_State* L)
  198. {
  199. LuaStack stack(L);
  200. stack.push_physics_world(stack.get_world(1)->physics_world());
  201. return 1;
  202. }
  203. //-----------------------------------------------------------------------------
  204. static int world_sound_world(lua_State* L)
  205. {
  206. LuaStack stack(L);
  207. stack.push_sound_world(stack.get_world(1)->sound_world());
  208. return 1;
  209. }
  210. //-----------------------------------------------------------------------------
  211. static int world_tostring(lua_State* L)
  212. {
  213. LuaStack stack(L);
  214. World* w = stack.get_world(1);
  215. stack.push_fstring("World (%p)", w);
  216. return 1;
  217. }
  218. //-----------------------------------------------------------------------------
  219. void load_world(LuaEnvironment& env)
  220. {
  221. env.load_module_function("World", "spawn_unit", world_spawn_unit);
  222. env.load_module_function("World", "destroy_unit", world_destroy_unit);
  223. env.load_module_function("World", "num_units", world_num_units);
  224. env.load_module_function("World", "units", world_units);
  225. env.load_module_function("World", "update_animations", world_update_animations);
  226. env.load_module_function("World", "update_scene", world_update_scene);
  227. env.load_module_function("World", "update", world_update);
  228. env.load_module_function("World", "play_sound", world_play_sound);
  229. env.load_module_function("World", "stop_sound", world_stop_sound);
  230. env.load_module_function("World", "link_sound", world_link_sound);
  231. env.load_module_function("World", "set_listener_pose", world_set_listener_pose);
  232. env.load_module_function("World", "set_sound_position", world_set_sound_position);
  233. env.load_module_function("World", "set_sound_range", world_set_sound_range);
  234. env.load_module_function("World", "set_sound_volume", world_set_sound_volume);
  235. env.load_module_function("World", "create_window_gui", world_create_window_gui);
  236. env.load_module_function("World", "destroy_gui", world_destroy_gui);
  237. env.load_module_function("World", "create_debug_line", world_create_debug_line);
  238. env.load_module_function("World", "destroy_debug_line", world_destroy_debug_line);
  239. env.load_module_function("World", "load_level", world_load_level);
  240. env.load_module_function("World", "physics_world", world_physics_world);
  241. env.load_module_function("World", "sound_world", world_sound_world);
  242. env.load_module_function("World", "__index", "World");
  243. env.load_module_function("World", "__tostring", world_tostring);
  244. }
  245. } // namespace crown