Tune caves
[oweals/minetest.git] / src / scriptapi.cpp
index 2ee727b3f010c16aa9d6dd9f0d788a6d79f5845f..3c7b9bb3eed1c4c88938051a763a0c77a29bd87a 100644 (file)
@@ -46,6 +46,7 @@ extern "C" {
 #include "content_nodemeta.h"
 #include "utility.h"
 #include "tool.h"
+#include "daynightratio.h"
 
 static void stackDump(lua_State *L, std::ostream &o)
 {
@@ -806,6 +807,23 @@ static void push_pointed_thing(lua_State *L, const PointedThing& pointed)
        }
 }
 
+/*
+       SimpleSoundSpec
+*/
+
+static void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
+{
+       if(index < 0)
+               index = lua_gettop(L) + 1 + index;
+       if(lua_isnil(L, index)){
+       } else if(lua_istable(L, index)){
+               getstringfield(L, index, "name", spec.name);
+               getfloatfield(L, index, "gain", spec.gain);
+       } else if(lua_isstring(L, index)){
+               spec.name = lua_tostring(L, index);
+       }
+}
+
 /*
        ItemDefinition
 */
@@ -1037,6 +1055,21 @@ static ContentFeatures read_content_features(lua_State *L, int index)
        getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
        // Set to true if wall_mounted used to be set to true
        getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
+       
+       // Sound table
+       lua_getfield(L, index, "sounds");
+       if(lua_istable(L, -1)){
+               lua_getfield(L, -1, "footstep");
+               read_soundspec(L, -1, f.sound_footstep);
+               lua_pop(L, 1);
+               lua_getfield(L, -1, "dig");
+               read_soundspec(L, -1, f.sound_dig);
+               lua_pop(L, 1);
+               lua_getfield(L, -1, "dug");
+               read_soundspec(L, -1, f.sound_dug);
+               lua_pop(L, 1);
+       }
+       lua_pop(L, 1);
 
        return f;
 }
@@ -2189,7 +2222,7 @@ private:
 
        static const char className[];
        static const luaL_reg methods[];
-
+public:
        static ObjectRef *checkobject(lua_State *L, int narg)
        {
                luaL_checktype(L, narg, LUA_TUSERDATA);
@@ -2203,7 +2236,7 @@ private:
                ServerActiveObject *co = ref->m_object;
                return co;
        }
-       
+private:
        static LuaEntitySAO* getluaobject(ObjectRef *ref)
        {
                ServerActiveObject *obj = getobject(ref);
@@ -2240,7 +2273,7 @@ private:
                ObjectRef *ref = checkobject(L, 1);
                ServerActiveObject *co = getobject(ref);
                if(co == NULL) return 0;
-               infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
+               verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
                co->m_removed = true;
                return 0;
        }
@@ -2297,17 +2330,22 @@ private:
                return 0;
        }
 
-       // punch(self, puncher); puncher = an another ObjectRef
+       // punch(self, puncher, tool_capabilities, direction, time_from_last_punch)
        static int l_punch(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
-               ObjectRef *ref2 = checkobject(L, 2);
+               ObjectRef *puncher_ref = checkobject(L, 2);
                ServerActiveObject *co = getobject(ref);
-               ServerActiveObject *co2 = getobject(ref2);
+               ServerActiveObject *puncher = getobject(puncher_ref);
                if(co == NULL) return 0;
-               if(co2 == NULL) return 0;
+               if(puncher == NULL) return 0;
+               ToolCapabilities toolcap = read_tool_capabilities(L, 3);
+               v3f dir = read_v3f(L, 4);
+               float time_from_last_punch = 1000000;
+               if(lua_isnumber(L, 5))
+                       time_from_last_punch = lua_tonumber(L, 5);
                // Do it
-               co->punch(co2);
+               puncher->punch(dir, &toolcap, puncher, time_from_last_punch);
                return 0;
        }
 
@@ -2335,8 +2373,8 @@ private:
                ServerActiveObject *co = getobject(ref);
                if(co == NULL) return 0;
                int hp = lua_tonumber(L, 2);
-               infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
-                               <<" hp="<<hp<<std::endl;
+               /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
+                               <<" hp="<<hp<<std::endl;*/
                // Do it
                co->setHP(hp);
                // Return
@@ -2352,8 +2390,8 @@ private:
                ServerActiveObject *co = getobject(ref);
                if(co == NULL) return 0;
                int hp = co->getHP();
-               infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
-                               <<" hp="<<hp<<std::endl;
+               /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
+                               <<" hp="<<hp<<std::endl;*/
                // Return
                lua_pushnumber(L, hp);
                return 1;
@@ -2533,6 +2571,19 @@ private:
                return 0;
        }
 
+       // set_armor_groups(self, groups)
+       static int l_set_armor_groups(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               LuaEntitySAO *co = getluaobject(ref);
+               if(co == NULL) return 0;
+               // Do it
+               ItemGroupList groups;
+               read_groups(L, 2, groups);
+               co->setArmorGroups(groups);
+               return 0;
+       }
+
        // DEPRECATED
        // get_entity_name(self)
        static int l_get_entity_name(lua_State *L)
@@ -2695,6 +2746,7 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, getyaw),
        method(ObjectRef, settexturemod),
        method(ObjectRef, setsprite),
+       method(ObjectRef, set_armor_groups),
        method(ObjectRef, get_entity_name),
        method(ObjectRef, get_luaentity),
        // Player-only
@@ -2878,7 +2930,7 @@ private:
                if(item.empty() || !item.isKnown(get_server(L)->idef()))
                        return 0;
                // Do it
-               ServerActiveObject *obj = new ItemSAO(env, pos, item.getItemString());
+               ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
                int objectid = env->addActiveObject(obj);
                // If failed to add, return nothing (reads as nil)
                if(objectid == 0)
@@ -2892,15 +2944,8 @@ private:
        // pos = {x=num, y=num, z=num}
        static int l_add_rat(lua_State *L)
        {
-               infostream<<"EnvRef::l_add_rat()"<<std::endl;
-               EnvRef *o = checkobject(L, 1);
-               ServerEnvironment *env = o->m_env;
-               if(env == NULL) return 0;
-               // pos
-               v3f pos = checkFloatPos(L, 2);
-               // Do it
-               ServerActiveObject *obj = new RatSAO(env, pos);
-               env->addActiveObject(obj);
+               infostream<<"EnvRef::l_add_rat(): C++ mobs have been removed."
+                               <<" Doing nothing."<<std::endl;
                return 0;
        }
 
@@ -2908,15 +2953,8 @@ private:
        // pos = {x=num, y=num, z=num}
        static int l_add_firefly(lua_State *L)
        {
-               infostream<<"EnvRef::l_add_firefly()"<<std::endl;
-               EnvRef *o = checkobject(L, 1);
-               ServerEnvironment *env = o->m_env;
-               if(env == NULL) return 0;
-               // pos
-               v3f pos = checkFloatPos(L, 2);
-               // Do it
-               ServerActiveObject *obj = new FireflySAO(env, pos);
-               env->addActiveObject(obj);
+               infostream<<"EnvRef::l_add_firefly(): C++ mobs have been removed."
+                               <<" Doing nothing."<<std::endl;
                return 0;
        }
 
@@ -3024,12 +3062,12 @@ public:
        EnvRef(ServerEnvironment *env):
                m_env(env)
        {
-               infostream<<"EnvRef created"<<std::endl;
+               //infostream<<"EnvRef created"<<std::endl;
        }
 
        ~EnvRef()
        {
-               infostream<<"EnvRef destructing"<<std::endl;
+               //infostream<<"EnvRef destructing"<<std::endl;
        }
 
        // Creates an EnvRef and leaves it on top of stack
@@ -3096,10 +3134,6 @@ const luaL_reg EnvRef::methods[] = {
        {0,0}
 };
 
-/*
-       Global functions
-*/
-
 class LuaABM : public ActiveBlockModifier
 {
 private:
@@ -3173,6 +3207,47 @@ public:
        }
 };
 
+/*
+       ServerSoundParams
+*/
+
+static void read_server_sound_params(lua_State *L, int index,
+               ServerSoundParams &params)
+{
+       if(index < 0)
+               index = lua_gettop(L) + 1 + index;
+       // Clear
+       params = ServerSoundParams();
+       if(lua_istable(L, index)){
+               getfloatfield(L, index, "gain", params.gain);
+               getstringfield(L, index, "to_player", params.to_player);
+               lua_getfield(L, index, "pos");
+               if(!lua_isnil(L, -1)){
+                       v3f p = read_v3f(L, -1)*BS;
+                       params.pos = p;
+                       params.type = ServerSoundParams::SSP_POSITIONAL;
+               }
+               lua_pop(L, 1);
+               lua_getfield(L, index, "object");
+               if(!lua_isnil(L, -1)){
+                       ObjectRef *ref = ObjectRef::checkobject(L, -1);
+                       ServerActiveObject *sao = ObjectRef::getobject(ref);
+                       if(sao){
+                               params.object = sao->getId();
+                               params.type = ServerSoundParams::SSP_OBJECT;
+                       }
+               }
+               lua_pop(L, 1);
+               params.max_hear_distance = BS*getfloatfield_default(L, index,
+                               "max_hear_distance", params.max_hear_distance/BS);
+               getboolfield(L, index, "loop", params.loop);
+       }
+}
+
+/*
+       Global functions
+*/
+
 // debug(text)
 // Writes a line to dstream
 static int l_debug(lua_State *L)
@@ -3225,7 +3300,7 @@ static int l_register_item_raw(lua_State *L)
        lua_getfield(L, table, "name");
        if(lua_isstring(L, -1)){
                std::string name = lua_tostring(L, -1);
-               infostream<<"register_item_raw: "<<name<<std::endl;
+               verbosestream<<"register_item_raw: "<<name<<std::endl;
        } else {
                throw LuaError(L, "register_item_raw: name is not defined or not a string");
        }
@@ -3546,10 +3621,7 @@ static int l_get_player_privs(lua_State *L)
        // Do it
        lua_newtable(L);
        int table = lua_gettop(L);
-       u64 privs_i = server->getPlayerAuthPrivs(name);
-       // Special case for the "name" setting (local player / server owner)
-       if(name == g_settings->get("name"))
-               privs_i = PRIV_ALL;
+       u64 privs_i = server->getPlayerEffectivePrivs(name);
        std::set<std::string> privs_s = privsToSet(privs_i);
        for(std::set<std::string>::const_iterator
                        i = privs_s.begin(); i != privs_s.end(); i++){
@@ -3639,6 +3711,26 @@ static int l_get_worldpath(lua_State *L)
        return 1;
 }
 
+// sound_play(spec, parameters)
+static int l_sound_play(lua_State *L)
+{
+       SimpleSoundSpec spec;
+       read_soundspec(L, 1, spec);
+       ServerSoundParams params;
+       read_server_sound_params(L, 2, params);
+       s32 handle = get_server(L)->playSound(spec, params);
+       lua_pushinteger(L, handle);
+       return 1;
+}
+
+// sound_stop(handle)
+static int l_sound_stop(lua_State *L)
+{
+       int handle = luaL_checkinteger(L, 1);
+       get_server(L)->stopSound(handle);
+       return 0;
+}
+
 static const struct luaL_Reg minetest_f [] = {
        {"debug", l_debug},
        {"log", l_log},
@@ -3656,6 +3748,8 @@ static const struct luaL_Reg minetest_f [] = {
        {"get_current_modname", l_get_current_modname},
        {"get_modpath", l_get_modpath},
        {"get_worldpath", l_get_worldpath},
+       {"sound_play", l_sound_play},
+       {"sound_stop", l_sound_stop},
        {NULL, NULL}
 };
 
@@ -3667,7 +3761,7 @@ void scriptapi_export(lua_State *L, Server *server)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_export"<<std::endl;
+       verbosestream<<"scriptapi_export()"<<std::endl;
        StackUnroller stack_unroller(L);
 
        // Store server as light userdata in registry
@@ -3727,7 +3821,7 @@ void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_add_environment"<<std::endl;
+       verbosestream<<"scriptapi_add_environment"<<std::endl;
        StackUnroller stack_unroller(L);
 
        // Create EnvRef on stack
@@ -4221,12 +4315,11 @@ void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp)
        luaentity
 */
 
-bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
-               const std::string &staticdata)
+bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
+       verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
                        <<name<<"\""<<std::endl;
        StackUnroller stack_unroller(L);
        
@@ -4269,6 +4362,21 @@ bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
        lua_pushvalue(L, object); // Copy object to top of stack
        lua_settable(L, -3);
        
+       return true;
+}
+
+void scriptapi_luaentity_activate(lua_State *L, u16 id,
+               const std::string &staticdata)
+{
+       realitycheck(L);
+       assert(lua_checkstack(L, 20));
+       verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
+       StackUnroller stack_unroller(L);
+       
+       // Get minetest.luaentities[id]
+       luaentity_get(L, id);
+       int object = lua_gettop(L);
+       
        // Get on_activate function
        lua_pushvalue(L, object);
        lua_getfield(L, -1, "on_activate");
@@ -4278,18 +4386,16 @@ bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
                lua_pushlstring(L, staticdata.c_str(), staticdata.size());
                // Call with 2 arguments, 0 results
                if(lua_pcall(L, 2, 0, 0))
-                       script_error(L, "error running function %s:on_activate: %s\n",
-                                       name, lua_tostring(L, -1));
+                       script_error(L, "error running function on_activate: %s\n",
+                                       lua_tostring(L, -1));
        }
-       
-       return true;
 }
 
 void scriptapi_luaentity_rm(lua_State *L, u16 id)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
+       verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
 
        // Get minetest.luaentities table
        lua_getglobal(L, "minetest");
@@ -4309,7 +4415,7 @@ std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
+       //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
        StackUnroller stack_unroller(L);
 
        // Get minetest.luaentities[id]
@@ -4339,7 +4445,7 @@ void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
+       //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
        StackUnroller stack_unroller(L);
 
        // Get minetest.luaentities[id]
@@ -4348,6 +4454,8 @@ void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
 
        /* Read stuff */
        
+       prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
+
        getboolfield(L, -1, "physical", prop->physical);
 
        getfloatfield(L, -1, "weight", prop->weight);
@@ -4415,9 +4523,11 @@ void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
                script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
 }
 
-// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch)
+// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
+//                       tool_capabilities, direction)
 void scriptapi_luaentity_punch(lua_State *L, u16 id,
-               ServerActiveObject *puncher, float time_from_last_punch)
+               ServerActiveObject *puncher, float time_from_last_punch,
+               const ToolCapabilities *toolcap, v3f dir)
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
@@ -4436,8 +4546,10 @@ void scriptapi_luaentity_punch(lua_State *L, u16 id,
        lua_pushvalue(L, object); // self
        objectref_get_or_create(L, puncher); // Clicker reference
        lua_pushnumber(L, time_from_last_punch);
-       // Call with 2 arguments, 0 results
-       if(lua_pcall(L, 3, 0, 0))
+       push_tool_capabilities(L, *toolcap);
+       push_v3f(L, dir);
+       // Call with 5 arguments, 0 results
+       if(lua_pcall(L, 5, 0, 0))
                script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
 }