You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

280 lines
9.3 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "lua_api/l_particles.h"
  17. #include "lua_api/l_object.h"
  18. #include "lua_api/l_internal.h"
  19. #include "common/c_converter.h"
  20. #include "common/c_content.h"
  21. #include "server.h"
  22. #include "particles.h"
  23. // add_particle({pos=, velocity=, acceleration=, expirationtime=,
  24. // size=, collisiondetection=, collision_removal=, vertical=,
  25. // texture=, player=})
  26. // pos/velocity/acceleration = {x=num, y=num, z=num}
  27. // expirationtime = num (seconds)
  28. // size = num
  29. // collisiondetection = bool
  30. // collision_removal = bool
  31. // vertical = bool
  32. // texture = e.g."default_wood.png"
  33. // animation = TileAnimation definition
  34. // glow = num
  35. int ModApiParticles::l_add_particle(lua_State *L)
  36. {
  37. MAP_LOCK_REQUIRED;
  38. // Get parameters
  39. v3f pos, vel, acc;
  40. pos = vel = acc = v3f(0, 0, 0);
  41. float expirationtime, size;
  42. expirationtime = size = 1;
  43. bool collisiondetection, vertical, collision_removal;
  44. collisiondetection = vertical = collision_removal = false;
  45. struct TileAnimationParams animation;
  46. animation.type = TAT_NONE;
  47. std::string texture = "";
  48. std::string playername = "";
  49. u8 glow = 0;
  50. if (lua_gettop(L) > 1) // deprecated
  51. {
  52. log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
  53. pos = check_v3f(L, 1);
  54. vel = check_v3f(L, 2);
  55. acc = check_v3f(L, 3);
  56. expirationtime = luaL_checknumber(L, 4);
  57. size = luaL_checknumber(L, 5);
  58. collisiondetection = readParam<bool>(L, 6);
  59. texture = luaL_checkstring(L, 7);
  60. if (lua_gettop(L) == 8) // only spawn for a single player
  61. playername = luaL_checkstring(L, 8);
  62. }
  63. else if (lua_istable(L, 1))
  64. {
  65. lua_getfield(L, 1, "pos");
  66. pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
  67. lua_pop(L, 1);
  68. lua_getfield(L, 1, "vel");
  69. if (lua_istable(L, -1)) {
  70. vel = check_v3f(L, -1);
  71. log_deprecated(L, "The use of vel is deprecated. "
  72. "Use velocity instead");
  73. }
  74. lua_pop(L, 1);
  75. lua_getfield(L, 1, "velocity");
  76. vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
  77. lua_pop(L, 1);
  78. lua_getfield(L, 1, "acc");
  79. if (lua_istable(L, -1)) {
  80. acc = check_v3f(L, -1);
  81. log_deprecated(L, "The use of acc is deprecated. "
  82. "Use acceleration instead");
  83. }
  84. lua_pop(L, 1);
  85. lua_getfield(L, 1, "acceleration");
  86. acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
  87. lua_pop(L, 1);
  88. expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
  89. size = getfloatfield_default(L, 1, "size", 1);
  90. collisiondetection = getboolfield_default(L, 1,
  91. "collisiondetection", collisiondetection);
  92. collision_removal = getboolfield_default(L, 1,
  93. "collision_removal", collision_removal);
  94. vertical = getboolfield_default(L, 1, "vertical", vertical);
  95. lua_getfield(L, 1, "animation");
  96. animation = read_animation_definition(L, -1);
  97. lua_pop(L, 1);
  98. texture = getstringfield_default(L, 1, "texture", "");
  99. playername = getstringfield_default(L, 1, "playername", "");
  100. glow = getintfield_default(L, 1, "glow", 0);
  101. }
  102. getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size,
  103. collisiondetection, collision_removal, vertical, texture, animation, glow);
  104. return 1;
  105. }
  106. // add_particlespawner({amount=, time=,
  107. // minpos=, maxpos=,
  108. // minvel=, maxvel=,
  109. // minacc=, maxacc=,
  110. // minexptime=, maxexptime=,
  111. // minsize=, maxsize=,
  112. // collisiondetection=,
  113. // collision_removal=,
  114. // vertical=,
  115. // texture=,
  116. // player=})
  117. // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
  118. // minexptime/maxexptime = num (seconds)
  119. // minsize/maxsize = num
  120. // collisiondetection = bool
  121. // collision_removal = bool
  122. // vertical = bool
  123. // texture = e.g."default_wood.png"
  124. // animation = TileAnimation definition
  125. // glow = num
  126. int ModApiParticles::l_add_particlespawner(lua_State *L)
  127. {
  128. MAP_LOCK_REQUIRED;
  129. // Get parameters
  130. u16 amount = 1;
  131. v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
  132. minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
  133. float time, minexptime, maxexptime, minsize, maxsize;
  134. time= minexptime= maxexptime= minsize= maxsize= 1;
  135. bool collisiondetection, vertical, collision_removal;
  136. collisiondetection = vertical = collision_removal = false;
  137. struct TileAnimationParams animation;
  138. animation.type = TAT_NONE;
  139. ServerActiveObject *attached = NULL;
  140. std::string texture = "";
  141. std::string playername = "";
  142. u8 glow = 0;
  143. if (lua_gettop(L) > 1) //deprecated
  144. {
  145. log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
  146. amount = luaL_checknumber(L, 1);
  147. time = luaL_checknumber(L, 2);
  148. minpos = check_v3f(L, 3);
  149. maxpos = check_v3f(L, 4);
  150. minvel = check_v3f(L, 5);
  151. maxvel = check_v3f(L, 6);
  152. minacc = check_v3f(L, 7);
  153. maxacc = check_v3f(L, 8);
  154. minexptime = luaL_checknumber(L, 9);
  155. maxexptime = luaL_checknumber(L, 10);
  156. minsize = luaL_checknumber(L, 11);
  157. maxsize = luaL_checknumber(L, 12);
  158. collisiondetection = readParam<bool>(L, 13);
  159. texture = luaL_checkstring(L, 14);
  160. if (lua_gettop(L) == 15) // only spawn for a single player
  161. playername = luaL_checkstring(L, 15);
  162. }
  163. else if (lua_istable(L, 1))
  164. {
  165. amount = getintfield_default(L, 1, "amount", amount);
  166. time = getfloatfield_default(L, 1, "time", time);
  167. lua_getfield(L, 1, "minpos");
  168. minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
  169. lua_pop(L, 1);
  170. lua_getfield(L, 1, "maxpos");
  171. maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
  172. lua_pop(L, 1);
  173. lua_getfield(L, 1, "minvel");
  174. minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
  175. lua_pop(L, 1);
  176. lua_getfield(L, 1, "maxvel");
  177. maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
  178. lua_pop(L, 1);
  179. lua_getfield(L, 1, "minacc");
  180. minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
  181. lua_pop(L, 1);
  182. lua_getfield(L, 1, "maxacc");
  183. maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
  184. lua_pop(L, 1);
  185. minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
  186. maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
  187. minsize = getfloatfield_default(L, 1, "minsize", minsize);
  188. maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
  189. collisiondetection = getboolfield_default(L, 1,
  190. "collisiondetection", collisiondetection);
  191. collision_removal = getboolfield_default(L, 1,
  192. "collision_removal", collision_removal);
  193. lua_getfield(L, 1, "animation");
  194. animation = read_animation_definition(L, -1);
  195. lua_pop(L, 1);
  196. lua_getfield(L, 1, "attached");
  197. if (!lua_isnil(L, -1)) {
  198. ObjectRef *ref = ObjectRef::checkobject(L, -1);
  199. lua_pop(L, 1);
  200. attached = ObjectRef::getobject(ref);
  201. }
  202. vertical = getboolfield_default(L, 1, "vertical", vertical);
  203. texture = getstringfield_default(L, 1, "texture", "");
  204. playername = getstringfield_default(L, 1, "playername", "");
  205. glow = getintfield_default(L, 1, "glow", 0);
  206. }
  207. u32 id = getServer(L)->addParticleSpawner(amount, time,
  208. minpos, maxpos,
  209. minvel, maxvel,
  210. minacc, maxacc,
  211. minexptime, maxexptime,
  212. minsize, maxsize,
  213. collisiondetection,
  214. collision_removal,
  215. attached,
  216. vertical,
  217. texture, playername,
  218. animation, glow);
  219. lua_pushnumber(L, id);
  220. return 1;
  221. }
  222. // delete_particlespawner(id, player)
  223. // player (string) is optional
  224. int ModApiParticles::l_delete_particlespawner(lua_State *L)
  225. {
  226. MAP_LOCK_REQUIRED;
  227. // Get parameters
  228. u32 id = luaL_checknumber(L, 1);
  229. std::string playername = "";
  230. if (lua_gettop(L) == 2) {
  231. playername = luaL_checkstring(L, 2);
  232. }
  233. getServer(L)->deleteParticleSpawner(playername, id);
  234. return 1;
  235. }
  236. void ModApiParticles::Initialize(lua_State *L, int top)
  237. {
  238. API_FCT(add_particle);
  239. API_FCT(add_particlespawner);
  240. API_FCT(delete_particlespawner);
  241. }