Fixes parts of #5389.
Sounds
------
-**NOTE: Not fully implemented yet.**
+**NOTE: max_hear_distance and connecting to objects is not implemented.**
Only Ogg Vorbis files are supported.
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,
}
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. `""`
{
ModApiUtil::InitializeClient(L, top);
ModApiClient::Initialize(L, top);
- ModApiSound::Initialize(L, top);
ModApiStorage::Initialize(L, top);
ModApiEnvMod::InitializeClient(L, top);
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);
API_FCT(get_wielded_item);
API_FCT(disconnect);
API_FCT(get_meta);
+ API_FCT(sound_play);
+ API_FCT(sound_stop);
}
// 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);
};
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);
{
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;
}