Tune caves
[oweals/minetest.git] / src / scriptapi.cpp
index 27ab2950be7c404446e605cc79f8f967d97f270d..3c7b9bb3eed1c4c88938051a763a0c77a29bd87a 100644 (file)
@@ -807,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
 */
@@ -1038,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;
 }
@@ -2190,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);
@@ -2204,7 +2236,7 @@ private:
                ServerActiveObject *co = ref->m_object;
                return co;
        }
-       
+private:
        static LuaEntitySAO* getluaobject(ObjectRef *ref)
        {
                ServerActiveObject *obj = getobject(ref);
@@ -3102,10 +3134,6 @@ const luaL_reg EnvRef::methods[] = {
        {0,0}
 };
 
-/*
-       Global functions
-*/
-
 class LuaABM : public ActiveBlockModifier
 {
 private:
@@ -3179,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)
@@ -3642,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},
@@ -3659,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}
 };