[CSM] Add support for positional audio. (#5516)
authorred-001 <red-001@outlook.ie>
Thu, 6 Apr 2017 06:14:31 +0000 (07:14 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Thu, 6 Apr 2017 06:14:31 +0000 (08:14 +0200)
Fixes parts of #5389.

doc/client_lua_api.md
src/script/clientscripting.cpp
src/script/lua_api/l_client.cpp
src/script/lua_api/l_client.h
src/script/lua_api/l_sound.cpp

index 68156efd337b935d50be9d08e04afc231f657ce3..25055c98a9d47d5d5926b82444fad5ba5d2ebf11 100644 (file)
@@ -131,7 +131,7 @@ The `:` prefix can also be used for maintaining backwards compatibility.
 
 Sounds
 ------
-**NOTE: Not fully implemented yet.**
+**NOTE: max_hear_distance and connecting to objects is not implemented.**
 
 Only Ogg Vorbis files are supported.
 
@@ -158,18 +158,12 @@ from the available ones of the following files:
 
 Examples of sound parameter tables:
 
-    -- Play locationless on all clients
+    -- Play locationless
     {
         gain = 1.0, -- default
     }
-    -- Play locationless to one player
+    -- Play locationless, looped
     {
-        to_player = name,
-        gain = 1.0, -- default
-    }
-    -- Play locationless to one player, looped
-    {
-        to_player = name,
         gain = 1.0, -- default
         loop = true,
     }
@@ -187,8 +181,7 @@ Examples of sound parameter tables:
         loop = true,
     }
 
-Looped sounds must either be connected to an object or played locationless to
-one player using `to_player = name,`
+Looped sounds must either be connected to an object or played locationless.
 
 ### SimpleSoundSpec
 * e.g. `""`
index 17c53985d64c0e80e1bb6327cd123448aaa30a2c..6f91b82ec8f2a946eb881849b1b819b990c810ef 100644 (file)
@@ -62,7 +62,6 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
 {
        ModApiUtil::InitializeClient(L, top);
        ModApiClient::Initialize(L, top);
-       ModApiSound::Initialize(L, top);
        ModApiStorage::Initialize(L, top);
        ModApiEnvMod::InitializeClient(L, top);
 
index 5a0cd5cc3eb88daaf876ff3670a7d50cbec78aef..2d906985f7c5d81e51e7dbd8a7cfa61c23f96e15 100644 (file)
@@ -194,6 +194,45 @@ int ModApiClient::l_get_meta(lua_State *L)
        return 1;
 }
 
+int ModApiClient::l_sound_play(lua_State *L)
+{
+       ISoundManager *sound = getClient(L)->getSoundManager();
+
+       SimpleSoundSpec spec;
+       read_soundspec(L, 1, spec);
+       float gain = 1.0 ;
+       bool looped = false;
+       s32 handle;
+
+       if (lua_istable(L, 2)) {
+               getfloatfield(L, 2, "gain", gain);
+               getboolfield(L, 2, "loop", looped);
+
+               lua_getfield(L, 2, "pos");
+               if (!lua_isnil(L, -1)) {
+                       v3f pos = read_v3f(L, -1) * BS;
+                       lua_pop(L, 1);
+                       handle = sound->playSoundAt(spec.name, looped, gain * spec.gain, pos);
+                       lua_pushinteger(L, handle);
+                       return 1;
+               }
+       }
+
+       handle = sound->playSound(spec.name, looped, gain * spec.gain);
+       lua_pushinteger(L, handle);
+
+       return 1;
+}
+
+int ModApiClient::l_sound_stop(lua_State *L)
+{
+       u32 handle = luaL_checkinteger(L, 1);
+
+       getClient(L)->getSoundManager()->stopSound(handle);
+
+       return 0;
+}
+
 void ModApiClient::Initialize(lua_State *L, int top)
 {
        API_FCT(get_current_modname);
@@ -209,4 +248,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
        API_FCT(get_wielded_item);
        API_FCT(disconnect);
        API_FCT(get_meta);
+       API_FCT(sound_play);
+       API_FCT(sound_stop);
 }
index 94b154bb429d22733edba85bbc4c99522298abfc..4e7f63d6a639957bc5e8d874e86f67a4be974abe 100644 (file)
@@ -65,6 +65,10 @@ private:
        // get_meta(pos)
        static int l_get_meta(lua_State *L);
 
+       static int l_sound_play(lua_State *L);
+
+       static int l_sound_stop(lua_State *L);
+
 public:
        static void Initialize(lua_State *L, int top);
 };
index 774b5be0c79e74a53a18791fa0da5afced2bc282..07ce36daae05d70aa606a811970201b0c0b2d6c9 100644 (file)
@@ -30,12 +30,7 @@ int ModApiSound::l_sound_play(lua_State *L)
        read_soundspec(L, 1, spec);
        bool looped = lua_toboolean(L, 2);
 
-       s32 handle;
-       if (Client *client = getClient(L))
-               handle = client->getSoundManager()->playSound(spec, looped);
-       // Main menu doesn't have access to client, use guiEngine
-       else
-               handle = getGuiEngine(L)->playSound(spec, looped);
+       s32 handle = getGuiEngine(L)->playSound(spec, looped);
 
        lua_pushinteger(L, handle);
 
@@ -46,11 +41,7 @@ int ModApiSound::l_sound_stop(lua_State *L)
 {
        u32 handle = luaL_checkinteger(L, 1);
 
-       if (Client *client = getClient(L))
-               client->getSoundManager()->stopSound(handle);
-       // Main menu doesn't have access to client, use guiEngine
-       else
-               getGuiEngine(L)->stopSound(handle);
+       getGuiEngine(L)->stopSound(handle);
 
        return 1;
 }