lua_world.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2012-2015 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 StringId64 name = stack.get_resource_id(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_TYPE, name), stack, "Unit not found");
  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_link_unit(lua_State* L)
  57. {
  58. LuaStack stack(L);
  59. stack.get_world(1)->link_unit(stack.get_unit(2)->id(), stack.get_unit(3)->id());
  60. return 0;
  61. }
  62. static int world_unlink_unit(lua_State* L)
  63. {
  64. LuaStack stack(L);
  65. stack.get_world(1)->unlink_unit(stack.get_unit(2)->id());
  66. return 0;
  67. }
  68. static int world_update_animations(lua_State* L)
  69. {
  70. LuaStack stack(L);
  71. stack.get_world(1)->update_animations(stack.get_float(2));
  72. return 0;
  73. }
  74. static int world_update_scene(lua_State* L)
  75. {
  76. LuaStack stack(L);
  77. stack.get_world(1)->update_scene(stack.get_float(2));
  78. return 0;
  79. }
  80. static int world_update(lua_State* L)
  81. {
  82. LuaStack stack(L);
  83. stack.get_world(1)->update(stack.get_float(2));
  84. return 0;
  85. }
  86. static int world_play_sound(lua_State* L)
  87. {
  88. LuaStack stack(L);
  89. World* world = stack.get_world(1);
  90. const StringId64 name = stack.get_resource_id(2);
  91. const int32_t nargs = stack.num_args();
  92. const bool loop = nargs > 2 ? stack.get_bool(3) : false;
  93. const float volume = nargs > 3 ? stack.get_float(4) : 1.0f;
  94. const Vector3& pos = nargs > 4 ? stack.get_vector3(5) : vector3::ZERO;
  95. const float range = nargs > 5 ? stack.get_float(6) : 1000.0f;
  96. LUA_ASSERT(device()->resource_manager()->can_get(SOUND_TYPE, name), stack, "Sound not found");
  97. stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
  98. return 1;
  99. }
  100. static int world_stop_sound(lua_State* L)
  101. {
  102. LuaStack stack(L);
  103. stack.get_world(1)->stop_sound(stack.get_sound_instance_id(2));
  104. return 0;
  105. }
  106. static int world_link_sound(lua_State* L)
  107. {
  108. LuaStack stack(L);
  109. stack.get_world(1)->link_sound(stack.get_sound_instance_id(2),
  110. stack.get_unit(3),
  111. stack.get_int(4));
  112. return 0;
  113. }
  114. static int world_set_listener_pose(lua_State* L)
  115. {
  116. LuaStack stack(L);
  117. stack.get_world(1)->set_listener_pose(stack.get_matrix4x4(2));
  118. return 0;
  119. }
  120. static int world_set_sound_position(lua_State* L)
  121. {
  122. LuaStack stack(L);
  123. stack.get_world(1)->set_sound_position(stack.get_sound_instance_id(2),
  124. stack.get_vector3(3));
  125. return 0;
  126. }
  127. static int world_set_sound_range(lua_State* L)
  128. {
  129. LuaStack stack(L);
  130. stack.get_world(1)->set_sound_range(stack.get_sound_instance_id(2),
  131. stack.get_float(3));
  132. return 0;
  133. }
  134. static int world_set_sound_volume(lua_State* L)
  135. {
  136. LuaStack stack(L);
  137. stack.get_world(1)->set_sound_volume(stack.get_sound_instance_id(2),
  138. stack.get_float(3));
  139. return 0;
  140. }
  141. static int world_create_window_gui(lua_State* L)
  142. {
  143. LuaStack stack(L);
  144. World* world = stack.get_world(1);
  145. GuiId id = world->create_window_gui(stack.get_int(2), stack.get_int(3), stack.get_string(4));
  146. stack.push_gui(world->get_gui(id));
  147. return 1;
  148. }
  149. static int world_destroy_gui(lua_State* L)
  150. {
  151. LuaStack stack(L);
  152. stack.get_world(1)->destroy_gui(stack.get_gui(2)->id());
  153. return 0;
  154. }
  155. static int world_create_debug_line(lua_State* L)
  156. {
  157. LuaStack stack(L);
  158. stack.push_debug_line(stack.get_world(1)->create_debug_line(stack.get_bool(2)));
  159. return 1;
  160. }
  161. static int world_destroy_debug_line(lua_State* L)
  162. {
  163. LuaStack stack(L);
  164. stack.get_world(1)->destroy_debug_line(stack.get_debug_line(2));
  165. return 0;
  166. }
  167. static int world_load_level(lua_State* L)
  168. {
  169. LuaStack stack(L);
  170. StringId64 name = stack.get_resource_id(2);
  171. LUA_ASSERT(device()->resource_manager()->can_get(LEVEL_TYPE, name), stack, "Level not found");
  172. stack.get_world(1)->load_level(name);
  173. return 0;
  174. }
  175. static int world_physics_world(lua_State* L)
  176. {
  177. LuaStack stack(L);
  178. stack.push_physics_world(stack.get_world(1)->physics_world());
  179. return 1;
  180. }
  181. static int world_sound_world(lua_State* L)
  182. {
  183. LuaStack stack(L);
  184. stack.push_sound_world(stack.get_world(1)->sound_world());
  185. return 1;
  186. }
  187. static int world_tostring(lua_State* L)
  188. {
  189. LuaStack stack(L);
  190. World* w = stack.get_world(1);
  191. stack.push_fstring("World (%p)", w);
  192. return 1;
  193. }
  194. void load_world(LuaEnvironment& env)
  195. {
  196. env.load_module_function("World", "spawn_unit", world_spawn_unit);
  197. env.load_module_function("World", "destroy_unit", world_destroy_unit);
  198. env.load_module_function("World", "num_units", world_num_units);
  199. env.load_module_function("World", "units", world_units);
  200. env.load_module_function("World", "link_unit", world_link_unit);
  201. env.load_module_function("World", "unlink_unit", world_unlink_unit);
  202. env.load_module_function("World", "update_animations", world_update_animations);
  203. env.load_module_function("World", "update_scene", world_update_scene);
  204. env.load_module_function("World", "update", world_update);
  205. env.load_module_function("World", "play_sound", world_play_sound);
  206. env.load_module_function("World", "stop_sound", world_stop_sound);
  207. env.load_module_function("World", "link_sound", world_link_sound);
  208. env.load_module_function("World", "set_listener_pose", world_set_listener_pose);
  209. env.load_module_function("World", "set_sound_position", world_set_sound_position);
  210. env.load_module_function("World", "set_sound_range", world_set_sound_range);
  211. env.load_module_function("World", "set_sound_volume", world_set_sound_volume);
  212. env.load_module_function("World", "create_window_gui", world_create_window_gui);
  213. env.load_module_function("World", "destroy_gui", world_destroy_gui);
  214. env.load_module_function("World", "create_debug_line", world_create_debug_line);
  215. env.load_module_function("World", "destroy_debug_line", world_destroy_debug_line);
  216. env.load_module_function("World", "load_level", world_load_level);
  217. env.load_module_function("World", "physics_world", world_physics_world);
  218. env.load_module_function("World", "sound_world", world_sound_world);
  219. env.load_module_function("World", "__index", "World");
  220. env.load_module_function("World", "__tostring", world_tostring);
  221. }
  222. } // namespace crown