]> git.lizzy.rs Git - minetest.git/blobdiff - src/scriptapi.cpp
Add shutdown hook interface to Lua API
[minetest.git] / src / scriptapi.cpp
index 9fd55b2e3b5cd658d46ff66b95e8ecca5a257e29..e5815c4620dd57cd1bcea72c14c88a6d8c40d867 100644 (file)
@@ -2716,8 +2716,8 @@ class ObjectRef
                return 0;
        }
 
-       // setanimations(self, frames, frame_speed, frame_blend)
-       static int l_set_animations(lua_State *L)
+       // set_animation(self, frame_range, frame_speed, frame_blend)
+       static int l_set_animation(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
                ServerActiveObject *co = getobject(ref);
@@ -2732,12 +2732,12 @@ class ObjectRef
                float frame_blend = 0;
                if(!lua_isnil(L, 4))
                        frame_blend = lua_tonumber(L, 4);
-               co->setAnimations(frames, frame_speed, frame_blend);
+               co->setAnimation(frames, frame_speed, frame_blend);
                return 0;
        }
 
-       // setboneposrot(self, std::string bone, v3f position, v3f rotation)
-       static int l_set_bone_posrot(lua_State *L)
+       // set_bone_position(self, std::string bone, v3f position, v3f rotation)
+       static int l_set_bone_position(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
                ServerActiveObject *co = getobject(ref);
@@ -2752,12 +2752,12 @@ class ObjectRef
                v3f rotation = v3f(0, 0, 0);
                if(!lua_isnil(L, 4))
                        rotation = read_v3f(L, 4);
-               co->setBonePosRot(bone, position, rotation);
+               co->setBonePosition(bone, position, rotation);
                return 0;
        }
 
-       // set_attachment(self, parent, bone, position, rotation)
-       static int l_set_attachment(lua_State *L)
+       // set_attach(self, parent, bone, position, rotation)
+       static int l_set_attach(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
                ObjectRef *parent_ref = checkobject(L, 2);
@@ -2779,8 +2779,8 @@ class ObjectRef
                return 0;
        }
 
-       // set_detachment(self)
-       static int l_set_detachment(lua_State *L)
+       // set_detach(self)
+       static int l_set_detach(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
                ServerActiveObject *co = getobject(ref);
@@ -3025,7 +3025,54 @@ class ObjectRef
                lua_pushlstring(L, formspec.c_str(), formspec.size());
                return 1;
        }
-
+       
+       // get_player_control(self)
+       static int l_get_player_control(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               Player *player = getplayer(ref);
+               if(player == NULL){
+                       lua_pushlstring(L, "", 0);
+                       return 1;
+               }
+               // Do it
+               PlayerControl control = player->getPlayerControl();
+               lua_newtable(L);
+               lua_pushboolean(L, control.up);
+               lua_setfield(L, -2, "up");
+               lua_pushboolean(L, control.down);
+               lua_setfield(L, -2, "down");
+               lua_pushboolean(L, control.left);
+               lua_setfield(L, -2, "left");
+               lua_pushboolean(L, control.right);
+               lua_setfield(L, -2, "right");
+               lua_pushboolean(L, control.jump);
+               lua_setfield(L, -2, "jump");
+               lua_pushboolean(L, control.aux1);
+               lua_setfield(L, -2, "aux1");
+               lua_pushboolean(L, control.sneak);
+               lua_setfield(L, -2, "sneak");
+               lua_pushboolean(L, control.LMB);
+               lua_setfield(L, -2, "LMB");
+               lua_pushboolean(L, control.RMB);
+               lua_setfield(L, -2, "RMB");
+               return 1;
+       }
+       
+       // get_player_control_bits(self)
+       static int l_get_player_control_bits(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               Player *player = getplayer(ref);
+               if(player == NULL){
+                       lua_pushlstring(L, "", 0);
+                       return 1;
+               }
+               // Do it        
+               lua_pushnumber(L, player->keyPressed);
+               return 1;
+       }
+       
 public:
        ObjectRef(ServerActiveObject *object):
                m_object(object)
@@ -3104,10 +3151,10 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, get_wielded_item),
        method(ObjectRef, set_wielded_item),
        method(ObjectRef, set_armor_groups),
-       method(ObjectRef, set_animations),
-       method(ObjectRef, set_bone_posrot),
-       method(ObjectRef, set_attachment),
-       method(ObjectRef, set_detachment),
+       method(ObjectRef, set_animation),
+       method(ObjectRef, set_bone_position),
+       method(ObjectRef, set_attach),
+       method(ObjectRef, set_detach),
        method(ObjectRef, set_properties),
        // LuaEntitySAO-only
        method(ObjectRef, setvelocity),
@@ -3128,6 +3175,8 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, get_look_yaw),
        method(ObjectRef, set_inventory_formspec),
        method(ObjectRef, get_inventory_formspec),
+       method(ObjectRef, get_player_control),
+       method(ObjectRef, get_player_control_bits),
        {0,0}
 };
 
@@ -5527,6 +5576,19 @@ bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
        return ate;
 }
 
+void scriptapi_on_shutdown(lua_State *L)
+{
+       realitycheck(L);
+       assert(lua_checkstack(L, 20));
+       StackUnroller stack_unroller(L);
+
+       // Get registered shutdown hooks
+       lua_getglobal(L, "minetest");
+       lua_getfield(L, -1, "registered_on_shutdown");
+       // Call callbacks
+       scriptapi_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
+}
+
 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
 {
        realitycheck(L);