Tune caves
[oweals/minetest.git] / src / scriptapi.cpp
index 7bdf7a9f4ca9b248231ea85150674ff51ac4b432..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;
        }
@@ -2340,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
@@ -2357,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;
@@ -3029,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
@@ -3101,10 +3134,6 @@ const luaL_reg EnvRef::methods[] = {
        {0,0}
 };
 
-/*
-       Global functions
-*/
-
 class LuaABM : public ActiveBlockModifier
 {
 private:
@@ -3178,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)
@@ -3230,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");
        }
@@ -3551,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++){
@@ -3644,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},
@@ -3661,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}
 };
 
@@ -3672,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
@@ -3732,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
@@ -4230,7 +4319,7 @@ 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);
        
@@ -4281,7 +4370,7 @@ void scriptapi_luaentity_activate(lua_State *L, u16 id,
 {
        realitycheck(L);
        assert(lua_checkstack(L, 20));
-       infostream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
+       verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
        StackUnroller stack_unroller(L);
        
        // Get minetest.luaentities[id]
@@ -4306,7 +4395,7 @@ 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");
@@ -4326,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]
@@ -4356,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]