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.
 
 
 
 
 
 

174 lines
4.6 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_nodetimer.h"
  17. #include "lua_api/l_internal.h"
  18. #include "serverenvironment.h"
  19. #include "map.h"
  20. int NodeTimerRef::gc_object(lua_State *L) {
  21. NodeTimerRef *o = *(NodeTimerRef **)(lua_touserdata(L, 1));
  22. delete o;
  23. return 0;
  24. }
  25. NodeTimerRef* NodeTimerRef::checkobject(lua_State *L, int narg)
  26. {
  27. luaL_checktype(L, narg, LUA_TUSERDATA);
  28. void *ud = luaL_checkudata(L, narg, className);
  29. if(!ud) luaL_typerror(L, narg, className);
  30. return *(NodeTimerRef**)ud; // unbox pointer
  31. }
  32. int NodeTimerRef::l_set(lua_State *L)
  33. {
  34. MAP_LOCK_REQUIRED;
  35. NodeTimerRef *o = checkobject(L, 1);
  36. ServerEnvironment *env = o->m_env;
  37. if(env == NULL) return 0;
  38. f32 t = readParam<float>(L,2);
  39. f32 e = readParam<float>(L,3);
  40. env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p));
  41. return 0;
  42. }
  43. int NodeTimerRef::l_start(lua_State *L)
  44. {
  45. MAP_LOCK_REQUIRED;
  46. NodeTimerRef *o = checkobject(L, 1);
  47. ServerEnvironment *env = o->m_env;
  48. if(env == NULL) return 0;
  49. f32 t = readParam<float>(L,2);
  50. env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p));
  51. return 0;
  52. }
  53. int NodeTimerRef::l_stop(lua_State *L)
  54. {
  55. MAP_LOCK_REQUIRED;
  56. NodeTimerRef *o = checkobject(L, 1);
  57. ServerEnvironment *env = o->m_env;
  58. if(env == NULL) return 0;
  59. env->getMap().removeNodeTimer(o->m_p);
  60. return 0;
  61. }
  62. int NodeTimerRef::l_is_started(lua_State *L)
  63. {
  64. MAP_LOCK_REQUIRED;
  65. NodeTimerRef *o = checkobject(L, 1);
  66. ServerEnvironment *env = o->m_env;
  67. if(env == NULL) return 0;
  68. NodeTimer t = env->getMap().getNodeTimer(o->m_p);
  69. lua_pushboolean(L,(t.timeout != 0));
  70. return 1;
  71. }
  72. int NodeTimerRef::l_get_timeout(lua_State *L)
  73. {
  74. MAP_LOCK_REQUIRED;
  75. NodeTimerRef *o = checkobject(L, 1);
  76. ServerEnvironment *env = o->m_env;
  77. if(env == NULL) return 0;
  78. NodeTimer t = env->getMap().getNodeTimer(o->m_p);
  79. lua_pushnumber(L,t.timeout);
  80. return 1;
  81. }
  82. int NodeTimerRef::l_get_elapsed(lua_State *L)
  83. {
  84. MAP_LOCK_REQUIRED;
  85. NodeTimerRef *o = checkobject(L, 1);
  86. ServerEnvironment *env = o->m_env;
  87. if(env == NULL) return 0;
  88. NodeTimer t = env->getMap().getNodeTimer(o->m_p);
  89. lua_pushnumber(L,t.elapsed);
  90. return 1;
  91. }
  92. NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
  93. m_p(p),
  94. m_env(env)
  95. {
  96. }
  97. NodeTimerRef::~NodeTimerRef()
  98. {
  99. }
  100. // Creates an NodeTimerRef and leaves it on top of stack
  101. // Not callable from Lua; all references are created on the C side.
  102. void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)
  103. {
  104. NodeTimerRef *o = new NodeTimerRef(p, env);
  105. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  106. luaL_getmetatable(L, className);
  107. lua_setmetatable(L, -2);
  108. }
  109. void NodeTimerRef::set_null(lua_State *L)
  110. {
  111. NodeTimerRef *o = checkobject(L, -1);
  112. o->m_env = NULL;
  113. }
  114. void NodeTimerRef::Register(lua_State *L)
  115. {
  116. lua_newtable(L);
  117. int methodtable = lua_gettop(L);
  118. luaL_newmetatable(L, className);
  119. int metatable = lua_gettop(L);
  120. lua_pushliteral(L, "__metatable");
  121. lua_pushvalue(L, methodtable);
  122. lua_settable(L, metatable); // hide metatable from Lua getmetatable()
  123. lua_pushliteral(L, "__index");
  124. lua_pushvalue(L, methodtable);
  125. lua_settable(L, metatable);
  126. lua_pushliteral(L, "__gc");
  127. lua_pushcfunction(L, gc_object);
  128. lua_settable(L, metatable);
  129. lua_pop(L, 1); // drop metatable
  130. luaL_openlib(L, 0, methods, 0); // fill methodtable
  131. lua_pop(L, 1); // drop methodtable
  132. // Cannot be created from Lua
  133. //lua_register(L, className, create_object);
  134. }
  135. const char NodeTimerRef::className[] = "NodeTimerRef";
  136. const luaL_Reg NodeTimerRef::methods[] = {
  137. luamethod(NodeTimerRef, start),
  138. luamethod(NodeTimerRef, set),
  139. luamethod(NodeTimerRef, stop),
  140. luamethod(NodeTimerRef, is_started),
  141. luamethod(NodeTimerRef, get_timeout),
  142. luamethod(NodeTimerRef, get_elapsed),
  143. {0,0}
  144. };