]> git.lizzy.rs Git - minetest.git/blobdiff - src/scriptapi.cpp
Add a setting to enable always flying fast
[minetest.git] / src / scriptapi.cpp
index 74de50ef8c3e4e551e5075194540ba32ec45ad65..8e4a43266c645733a8ef6572fbfaacff8eb44be1 100644 (file)
@@ -48,6 +48,7 @@ extern "C" {
 #include "noise.h" // PseudoRandom for LuaPseudoRandom
 #include "util/pointedthing.h"
 #include "rollback.h"
+#include "treegen.h"
 
 static void stackDump(lua_State *L, std::ostream &o)
 {
@@ -944,45 +945,6 @@ static void read_object_properties(lua_State *L, int index,
                prop->visual_size = read_v2f(L, -1);
        lua_pop(L, 1);
 
-       lua_getfield(L, -1, "animation_frames");
-       if(lua_istable(L, -1))
-       {
-               lua_rawgeti (L, -1, 1);
-               lua_rawgeti (L, -2, 2);
-               prop->animation_frames.X = lua_tonumber(L, -2);
-               prop->animation_frames.Y = lua_tonumber(L, -1);
-               lua_pop(L, 2);
-       }
-       lua_pop(L, 1);
-
-       getfloatfield(L, -1, "animation_speed", prop->animation_speed);
-
-       getfloatfield(L, -1, "animation_blend", prop->animation_blend);
-
-       lua_getfield(L, -1, "animation_bone_position");
-       if(lua_istable(L, -1))
-       {
-               lua_rawgeti (L, -1, 1);
-               lua_rawgeti (L, -2, 2);
-               std::string bone_name = lua_tostring(L, -2);
-               v3f bone_pos = read_v3f(L, -1);
-               prop->animation_bone_position[bone_name] = bone_pos;
-               lua_pop(L, 2);
-       }
-       lua_pop(L, 1);
-
-       lua_getfield(L, -1, "animation_bone_rotation");
-       if(lua_istable(L, -1))
-       {
-               lua_rawgeti (L, -1, 1);
-               lua_rawgeti (L, -2, 2);
-               std::string bone_name = lua_tostring(L, -2);
-               v3f bone_rot = read_v3f(L, -1);
-               prop->animation_bone_rotation[bone_name] = bone_rot;
-               lua_pop(L, 2);
-       }
-       lua_pop(L, 1);
-
        lua_getfield(L, -1, "textures");
        if(lua_istable(L, -1)){
                prop->textures.clear();
@@ -999,6 +961,23 @@ static void read_object_properties(lua_State *L, int index,
                }
        }
        lua_pop(L, 1);
+
+       lua_getfield(L, -1, "colors");
+       if(lua_istable(L, -1)){
+               prop->colors.clear();
+               int table = lua_gettop(L);
+               lua_pushnil(L);
+               while(lua_next(L, table) != 0){
+                       // key at index -2 and value at index -1
+                       if(lua_isstring(L, -1))
+                               prop->colors.push_back(readARGB8(L, -1));
+                       else
+                               prop->colors.push_back(video::SColor(255, 255, 255, 255));
+                       // removes value, keeps key for next iteration
+                       lua_pop(L, 1);
+               }
+       }
+       lua_pop(L, 1);
        
        lua_getfield(L, -1, "spritediv");
        if(lua_istable(L, -1))
@@ -2070,6 +2049,43 @@ class InvRef
                return 1;
        }
 
+       // get_location() -> location (like minetest.get_inventory(location))
+       static int l_get_location(lua_State *L)
+       {
+               InvRef *ref = checkobject(L, 1);
+               const InventoryLocation &loc = ref->m_loc;
+               switch(loc.type){
+               case InventoryLocation::PLAYER:
+                       lua_newtable(L);
+                       lua_pushstring(L, "player");
+                       lua_setfield(L, -2, "type");
+                       lua_pushstring(L, loc.name.c_str());
+                       lua_setfield(L, -2, "name");
+                       return 1;
+               case InventoryLocation::NODEMETA:
+                       lua_newtable(L);
+                       lua_pushstring(L, "nodemeta");
+                       lua_setfield(L, -2, "type");
+                       push_v3s16(L, loc.p);
+                       lua_setfield(L, -2, "name");
+                       return 1;
+               case InventoryLocation::DETACHED:
+                       lua_newtable(L);
+                       lua_pushstring(L, "detached");
+                       lua_setfield(L, -2, "type");
+                       lua_pushstring(L, loc.name.c_str());
+                       lua_setfield(L, -2, "name");
+                       return 1;
+               case InventoryLocation::UNDEFINED:
+               case InventoryLocation::CURRENT_PLAYER:
+                       break;
+               }
+               lua_newtable(L);
+               lua_pushstring(L, "undefined");
+               lua_setfield(L, -2, "type");
+               return 1;
+       }
+
 public:
        InvRef(const InventoryLocation &loc):
                m_loc(loc)
@@ -2145,6 +2161,7 @@ const luaL_reg InvRef::methods[] = {
        method(InvRef, room_for_item),
        method(InvRef, contains_item),
        method(InvRef, remove_item),
+       method(InvRef, get_location),
        {0,0}
 };
 
@@ -2738,6 +2755,80 @@ class ObjectRef
                return 0;
        }
 
+       // 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);
+               if(co == NULL) return 0;
+               // Do it
+               v2f frames = v2f(1, 1);
+               if(!lua_isnil(L, 2))
+                       frames = read_v2f(L, 2);
+               float frame_speed = 15;
+               if(!lua_isnil(L, 3))
+                       frame_speed = lua_tonumber(L, 3);
+               float frame_blend = 0;
+               if(!lua_isnil(L, 4))
+                       frame_blend = lua_tonumber(L, 4);
+               co->setAnimation(frames, frame_speed, frame_blend);
+               return 0;
+       }
+
+       // 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);
+               if(co == NULL) return 0;
+               // Do it
+               std::string bone = "";
+               if(!lua_isnil(L, 2))
+                       bone = lua_tostring(L, 2);
+               v3f position = v3f(0, 0, 0);
+               if(!lua_isnil(L, 3))
+                       position = read_v3f(L, 3);
+               v3f rotation = v3f(0, 0, 0);
+               if(!lua_isnil(L, 4))
+                       rotation = read_v3f(L, 4);
+               co->setBonePosition(bone, position, rotation);
+               return 0;
+       }
+
+       // 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);
+               ServerActiveObject *co = getobject(ref);
+               ServerActiveObject *parent = getobject(parent_ref);
+               if(co == NULL) return 0;
+               if(parent == NULL) return 0;
+               // Do it
+               std::string bone = "";
+               if(!lua_isnil(L, 3))
+                       bone = lua_tostring(L, 3);
+               v3f position = v3f(0, 0, 0);
+               if(!lua_isnil(L, 4))
+                       position = read_v3f(L, 4);
+               v3f rotation = v3f(0, 0, 0);
+               if(!lua_isnil(L, 5))
+                       rotation = read_v3f(L, 5);
+               co->setAttachment(parent->getId(), bone, position, rotation);
+               return 0;
+       }
+
+       // set_detach(self)
+       static int l_set_detach(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               ServerActiveObject *co = getobject(ref);
+               if(co == NULL) return 0;
+               // Do it
+               co->setAttachment(0, "", v3f(0,0,0), v3f(0,0,0));
+               return 0;
+       }
+
        // set_properties(self, properties)
        static int l_set_properties(lua_State *L)
        {
@@ -2973,7 +3064,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)
@@ -3052,6 +3190,10 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, get_wielded_item),
        method(ObjectRef, set_wielded_item),
        method(ObjectRef, set_armor_groups),
+       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),
@@ -3072,6 +3214,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}
 };
 
@@ -3396,20 +3540,7 @@ class EnvRef
                v3s16 pos = read_v3s16(L, 2);
                MapNode n = readnode(L, 3, ndef);
                // Do it
-               MapNode n_old = env->getMap().getNodeNoEx(pos);
-               // Call destructor
-               if(ndef->get(n_old).has_on_destruct)
-                       scriptapi_node_on_destruct(L, pos, n_old);
-               // Replace node
-               bool succeeded = env->getMap().addNodeWithEvent(pos, n);
-               if(succeeded){
-                       // Call post-destructor
-                       if(ndef->get(n_old).has_after_destruct)
-                               scriptapi_node_after_destruct(L, pos, n_old);
-                       // Call constructor
-                       if(ndef->get(n).has_on_construct)
-                               scriptapi_node_on_construct(L, pos, n);
-               }
+               bool succeeded = env->setNode(pos, n);
                lua_pushboolean(L, succeeded);
                return 1;
        }
@@ -3430,20 +3561,8 @@ class EnvRef
                // parameters
                v3s16 pos = read_v3s16(L, 2);
                // Do it
-               MapNode n_old = env->getMap().getNodeNoEx(pos);
-               // Call destructor
-               if(ndef->get(n_old).has_on_destruct)
-                       scriptapi_node_on_destruct(L, pos, n_old);
-               // Replace with air
-               // This is slightly optimized compared to addNodeWithEvent(air)
-               bool succeeded = env->getMap().removeNodeWithEvent(pos);
-               if(succeeded){
-                       // Call post-destructor
-                       if(ndef->get(n_old).has_after_destruct)
-                               scriptapi_node_after_destruct(L, pos, n_old);
-               }
+               bool succeeded = env->removeNode(pos);
                lua_pushboolean(L, succeeded);
-               // Air doesn't require constructor
                return 1;
        }
 
@@ -3499,7 +3618,7 @@ class EnvRef
                if(lua_isnumber(L, 3))
                        time_of_day = 24000.0 * lua_tonumber(L, 3);
                time_of_day %= 24000;
-               u32 dnr = time_to_daynight_ratio(time_of_day);
+               u32 dnr = time_to_daynight_ratio(time_of_day, true);
                MapNode n = env->getMap().getNodeNoEx(pos);
                try{
                        MapNode n = env->getMap().getNode(pos);
@@ -3899,6 +4018,54 @@ class EnvRef
                return 0;
        }
 
+       static int l_spawn_tree(lua_State *L)
+       {
+               EnvRef *o = checkobject(L, 1);
+               ServerEnvironment *env = o->m_env;
+               if(env == NULL) return 0;
+               v3s16 p0 = read_v3s16(L, 2);
+
+               treegen::TreeDef tree_def;
+               std::string trunk,leaves,fruit;
+               INodeDefManager *ndef = env->getGameDef()->ndef();
+
+               if(lua_istable(L, 3))
+               {
+                       getstringfield(L, 3, "axiom", tree_def.initial_axiom);
+                       getstringfield(L, 3, "rules_a", tree_def.rules_a);
+                       getstringfield(L, 3, "rules_b", tree_def.rules_b);
+                       getstringfield(L, 3, "rules_c", tree_def.rules_c);
+                       getstringfield(L, 3, "rules_d", tree_def.rules_d);
+                       getstringfield(L, 3, "trunk", trunk);
+                       tree_def.trunknode=ndef->getId(trunk);
+                       getstringfield(L, 3, "leaves", leaves);
+                       tree_def.leavesnode=ndef->getId(leaves);
+                       tree_def.leaves2_chance=0;
+                       getstringfield(L, 3, "leaves2", leaves);
+                       if (leaves !="")
+                       {
+                               tree_def.leaves2node=ndef->getId(leaves);
+                               getintfield(L, 3, "leaves2_chance", tree_def.leaves2_chance);
+                       }
+                       getintfield(L, 3, "angle", tree_def.angle);
+                       getintfield(L, 3, "iterations", tree_def.iterations);
+                       getintfield(L, 3, "random_level", tree_def.iterations_random_level);
+                       getstringfield(L, 3, "trunk_type", tree_def.trunk_type);
+                       getboolfield(L, 3, "thin_branches", tree_def.thin_branches);
+                       tree_def.fruit_chance=0;
+                       getstringfield(L, 3, "fruit", fruit);
+                       if (fruit != "")
+                       {
+                               tree_def.fruitnode=ndef->getId(fruit);
+                               getintfield(L, 3, "fruit_chance",tree_def.fruit_chance);
+                       }
+               }
+               else
+                       return 0;
+               treegen::spawn_ltree (env, p0, ndef, tree_def);
+               return 1;
+       }
+
 public:
        EnvRef(ServerEnvironment *env):
                m_env(env)
@@ -3981,6 +4148,7 @@ const luaL_reg EnvRef::methods[] = {
        method(EnvRef, find_nodes_in_area),
        method(EnvRef, get_perlin),
        method(EnvRef, clear_objects),
+       method(EnvRef, spawn_tree),
        {0,0}
 };
 
@@ -4753,6 +4921,22 @@ static int l_create_detached_inventory_raw(lua_State *L)
        return 1;
 }
 
+// show_formspec(playername,formname,formspec)
+static int l_show_formspec(lua_State *L)
+{
+       const char *playername = luaL_checkstring(L, 1);
+       const char *formname = luaL_checkstring(L, 2);
+       const char *formspec = luaL_checkstring(L, 3);
+
+       if(get_server(L)->showFormspec(playername,formspec,formname))
+       {
+               lua_pushboolean(L, true);
+       }else{
+               lua_pushboolean(L, false);
+       }
+       return 1;
+}
+
 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
 static int l_get_dig_params(lua_State *L)
 {
@@ -5082,6 +5266,7 @@ static const struct luaL_Reg minetest_f [] = {
        {"unban_player_or_ip", l_unban_player_of_ip},
        {"get_inventory", l_get_inventory},
        {"create_detached_inventory_raw", l_create_detached_inventory_raw},
+       {"show_formspec", l_show_formspec},
        {"get_dig_params", l_get_dig_params},
        {"get_hit_params", l_get_hit_params},
        {"get_current_modname", l_get_current_modname},
@@ -5471,6 +5656,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);