Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement minetest.sound_fade()
  • Loading branch information
sfan5 authored and nerzhul committed Apr 11, 2020
1 parent 054c5df commit 5f3a17e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
7 changes: 7 additions & 0 deletions doc/client_lua_api.txt
Expand Up @@ -734,6 +734,13 @@ Call these functions only at load time!
* `spec` is a `SimpleSoundSpec`
* `parameters` is a sound parameter table
* `minetest.sound_stop(handle)`
* `handle` is a handle returned by `minetest.sound_play`
* `minetest.sound_fade(handle, step, gain)`
* `handle` is a handle returned by `minetest.sound_play`
* `step` determines how fast a sound will fade.
Negative step will lower the sound volume, positive step will increase
the sound volume.
* `gain` the target gain for the fade.

### Timing
* `minetest.after(time, func, ...)`
Expand Down
22 changes: 19 additions & 3 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -209,7 +209,7 @@ int ModApiClient::l_gettext(lua_State *L)
return 1;
}

// get_node(pos)
// get_node_or_nil(pos)
// pos = {x=num, y=num, z=num}
int ModApiClient::l_get_node_or_nil(lua_State *L)
{
Expand All @@ -228,6 +228,7 @@ int ModApiClient::l_get_node_or_nil(lua_State *L)
return 1;
}

// get_langauge()
int ModApiClient::l_get_language(lua_State *L)
{
#ifdef _WIN32
Expand All @@ -244,6 +245,7 @@ int ModApiClient::l_get_language(lua_State *L)
return 2;
}

// get_wielded_item()
int ModApiClient::l_get_wielded_item(lua_State *L)
{
Client *client = getClient(L);
Expand All @@ -266,12 +268,14 @@ int ModApiClient::l_get_meta(lua_State *L)
return 1;
}

// sound_play(spec, parameters)
int ModApiClient::l_sound_play(lua_State *L)
{
ISoundManager *sound = getClient(L)->getSoundManager();

SimpleSoundSpec spec;
read_soundspec(L, 1, spec);

float gain = 1.0f;
float pitch = 1.0f;
bool looped = false;
Expand All @@ -293,21 +297,32 @@ int ModApiClient::l_sound_play(lua_State *L)
}
}

handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
handle = sound->playSound(spec.name, looped, gain * spec.gain, spec.fade, pitch);
lua_pushinteger(L, handle);

return 1;
}

// sound_stop(handle)
int ModApiClient::l_sound_stop(lua_State *L)
{
u32 handle = luaL_checkinteger(L, 1);
s32 handle = luaL_checkinteger(L, 1);

getClient(L)->getSoundManager()->stopSound(handle);

return 0;
}

// sound_fade(handle, step, gain)
int ModApiClient::l_sound_fade(lua_State *L)
{
s32 handle = luaL_checkinteger(L, 1);
float step = readParam<float>(L, 2);
float gain = readParam<float>(L, 3);
getClient(L)->getSoundManager()->fadeSound(handle, step, gain);
return 0;
}

// get_server_info()
int ModApiClient::l_get_server_info(lua_State *L)
{
Expand Down Expand Up @@ -426,6 +441,7 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(get_meta);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_fade);
API_FCT(get_server_info);
API_FCT(get_item_def);
API_FCT(get_node_def);
Expand Down
6 changes: 6 additions & 0 deletions src/script/lua_api/l_client.h
Expand Up @@ -69,6 +69,7 @@ class ModApiClient : public ModApiBase
// get_node(pos)
static int l_get_node_or_nil(lua_State *L);

// get_language()
static int l_get_language(lua_State *L);

// get_wielded_item()
Expand All @@ -77,10 +78,15 @@ class ModApiClient : public ModApiBase
// get_meta(pos)
static int l_get_meta(lua_State *L);

// sound_play(spec, parameters)
static int l_sound_play(lua_State *L);

// sound_stop(handle)
static int l_sound_stop(lua_State *L);

// sound_fade(handle, step, gain)
static int l_sound_fade(lua_State *L);

// get_server_info()
static int l_get_server_info(lua_State *L);

Expand Down
2 changes: 2 additions & 0 deletions src/script/lua_api/l_nodemeta.cpp
Expand Up @@ -55,11 +55,13 @@ Metadata* NodeMetaRef::getmeta(bool auto_create)

void NodeMetaRef::clearMeta()
{
SANITY_CHECK(!m_is_local);
m_env->getMap().removeNodeMetadata(m_p);
}

void NodeMetaRef::reportMetadataChange(const std::string *name)
{
SANITY_CHECK(!m_is_local);
// NOTE: This same code is in rollback_interface.cpp
// Inform other things that the metadata has changed
NodeMetadata *meta = dynamic_cast<NodeMetadata*>(m_meta);
Expand Down

0 comments on commit 5f3a17e

Please sign in to comment.