1
0
mirror of https://codeberg.org/tenplus1/ambience.git synced 2026-01-12 03:35:25 +01:00

Update to ver 2.2, added looped background sounds ability.

This commit is contained in:
tenplus1
2026-01-10 08:52:35 +00:00
parent 741ad2522e
commit b742b1b3c3
4 changed files with 75 additions and 10 deletions

View File

@@ -24,5 +24,5 @@ Based on Immersive Sounds .36 mod by Neuromancer and optimized to run on servers
- 1.9 - Tidy code, refactor music playing, add biome name to sound_check.
- 2.0 - Add Mineclone support, add ethereal leaf check, remove minetest.after for custom timer, add Polish translation, tweak & tidy code.
- 2.1 - Add ambience.group_total() function for easy counting of group: nodes inside a set.
- 2.2 - Adds background sounds to sets that play looped in the background while normal sounds play on top (when conditions met).
Code license: MIT

29
api.txt
View File

@@ -19,6 +19,11 @@ ambience.add_set(set_name, def)
'def' contains the following:
'frequency' how often the sound set is played (1 to 1000) higher is more
'nodes' contains a table of nodes needed for checks
'background' contains a table of sounds that play in the background when
sound_check conditions are met.
'sounds' contains a table of sounds that play on top of any background
sounds that can stop when conditions are not met, or can be
ephemeral for shorter sounds.
'sound_check(def)' function to check if sounds can be played, def contains:
'player' player userdata
'pos' position of player
@@ -34,11 +39,31 @@ function for it to play. If ephemeral is true then no handler will be used and
e.g.
ambience.add_set("village", {
frequency = 500,
nodes = {"mymod:village_sound_block"}, -- activates sounds when in range
background = {
{"name = "village_background", gain = 0.5, fade = 1.0},
},
sounds = {
{name = "dogbark", gain = 0.4, ephemeral = true}, -- quick sound effect
{name = "talking_villagers", gain = 0.3}, -- stops when out of range
{name = "chicken", gain = 0.6, ephemeral = true},
{name = "chicken", gain = 0.6, pitch = 1.2, ephemeral = true},
},
sound_check = function(def)
local number = def.totals["mymod:village_sound_block"] or 0
if number > 0 then
return "village"
end
end,
})
ambience.add_set("windy", {
frequency = 500,
nodes = {"default:sand"},
sounds = {
{name = "wind", length = 9, gain = 0.3, fade = 1.0},
{name = "wind", length = 9, gain = 0.3, fade = 1.0}, -- fade in sound
{name = "desertwind", length = 8, gain = 0.3},
{name = "crow", length = 3, ephemeral = true},
},
@@ -66,7 +91,7 @@ ambience.add_to_set(set_name, sound_table)
e.g.
ambience.add_to_set("underwater", {name = "scuba", pitch = 1.2, length = 8})
ambience.add_to_set("cave", {name = "monster_whispers", pitch = 1.2, length = 8})
Getting Sound Set

View File

@@ -27,7 +27,8 @@ function ambience.add_set(set_name, def)
sound_sets[set_name] = {
frequency = def.frequency or 50,
sounds = def.sounds,
background = def.background or {},
sounds = def.sounds or {},
sound_check = def.sound_check,
nodes = def.nodes
}
@@ -254,9 +255,42 @@ core.register_globalstep(function(dtime)
pname = player:get_player_name()
local set_name, MORE_GAIN = get_ambience(player, tod, pname)
local set_def = sound_sets[set_name]
ok = playing[pname] -- everything starts off ok if player found
-- are we playing any available background sounds?
if ok and not playing[pname].bg and set_def and #set_def.background > 0 then
-- choose a random sound from the background set
local bg_num = random(#set_def.background)
local bg_amb = set_def.background[bg_num]
-- only play sound if set differs from last one played
if set_name ~= playing[pname].bg_set then
playing[pname].bg = core.sound_play(bg_amb.name, {
to_player = pname,
gain = (bg_amb.gain or 0.3) * playing[pname].svol,
pitch = bg_amb.pitch, fade = bg_amb.fade, loop = true
})
--print("-- bg start", playing[pname].bg)
playing[pname].bg_set = set_name
end
elseif ok and playing[pname].bg and set_name ~= playing[pname].bg_set then
--print("-- bg stop", playing[pname].bg, set_name, playing[pname].bg_set)
core.sound_stop(playing[pname].bg)
playing[pname].bg = nil
playing[pname].bg_set = nil
end
-------
-- are we playing something already?
if ok and playing[pname].handler then
@@ -281,10 +315,11 @@ core.register_globalstep(function(dtime)
chance = random(1000)
-- if chance is lower than set frequency then select set
if ok and set_name and chance < sound_sets[set_name].frequency then
if ok and set_name and chance < set_def.frequency
and set_def.sounds and #set_def.sounds > 0 then
number = random(#sound_sets[set_name].sounds) -- choose random sound from set
ambience = sound_sets[set_name].sounds[number] -- grab sound information
number = random(#set_def.sounds) -- choose random sound from set
ambience = set_def.sounds[number] -- grab sound information
-- play sound
handler = core.sound_play(ambience.name, {

View File

@@ -62,10 +62,15 @@ ambience.add_set("underwater", {
frequency = 1000,
sounds = {
{name = "scuba", length = 8}
background = {
{name = "scuba", length = 8},
{name = "scuba", pitch = 1.2, length = 8}
},
-- sounds = {
-- {name = "scuba", length = 8}
-- },
sound_check = function(def)
local nodef = core.registered_nodes[def.head_node]
@@ -78,7 +83,7 @@ ambience.add_set("underwater", {
-- add new sound to above set
ambience.add_to_set("underwater", {name = "scuba", pitch = 1.2, length = 8})
--ambience.add_to_set("underwater", {name = "scuba", pitch = 1.2, length = 8})
-- Splashing sound plays when player walks inside water nodes (if enabled)