lua_world.cpp 6.6 KB

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