lua_world.cpp 8.9 KB

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