1
0
mirror of https://codeberg.org/tenplus1/ambience.git synced 2025-07-21 09:40:43 +02:00

Compare commits

...

5 Commits

Author SHA1 Message Date
adecc1b95c tweak icecrack and birds 2025-01-12 09:10:45 +00:00
703f6c4370 add ambience.group_total() function, tweak & tidy code 2024-12-18 10:34:03 +00:00
db65aebead add missing textdomain 2024-12-13 14:35:31 +00:00
e392d8d769 add polish translation (thx spageektti) 2024-12-13 14:33:37 +00:00
1b428c5a2c remove need for minetest.after 2024-11-28 09:56:39 +00:00
5 changed files with 151 additions and 139 deletions

View File

@ -22,5 +22,7 @@ Based on Immersive Sounds .36 mod by Neuromancer and optimized to run on servers
- 1.7 - Music will play every 20-30 minutes if found, use '/mvol 0' to stop playing music or disable in-game. - 1.7 - Music will play every 20-30 minutes if found, use '/mvol 0' to stop playing music or disable in-game.
- 1.8 - Players can set induvidual volume for sound and music which is saved. - 1.8 - Players can set induvidual volume for sound and music which is saved.
- 1.9 - Tidy code, refactor music playing, add biome name to sound_check. - 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.
Code license: MIT Code license: MIT

12
api.txt
View File

@ -43,14 +43,22 @@ ambience.add_set("windy", {
{name = "crow", length = 3, ephemeral = true}, {name = "crow", length = 3, ephemeral = true},
}, },
sound_check = function(def) sound_check = function(def)
local number = totals["default:sand"] or 0 -- yep, can also be nil local number = def.totals["default:sand"] or 0 -- yep, can also be nil
if number > 20 then if number > 20 then
return "windy", 0.2 -- return set to play and optional gain volume return "windy", 0.2 -- return set to play and optional gain volume
end end
end, end
}) })
Counting group: nodes
---------------------
Instead of counting each node total for things like leaves within the sound_check function, you could use the following helper function to return their total instead e.g.
local number = ambience.group_totals(def.totals, "leaves") -- count all group:leaves
Getting Sound Set Getting Sound Set
----------------- -----------------

135
init.lua
View File

@ -10,7 +10,7 @@ local MUSICVOLUME = 0.6
local MUSICINTERVAL = 60 * 20 local MUSICINTERVAL = 60 * 20
local play_music = minetest.settings:get_bool("ambience_music") ~= false local play_music = minetest.settings:get_bool("ambience_music") ~= false
local radius = 6 local radius = 6
local playing = {} local playing = {} -- user settings, timers and current set playing
local sound_sets = {} -- all the sounds and their settings local sound_sets = {} -- all the sounds and their settings
local sound_set_order = {} -- needed because pairs loops randomly through tables local sound_set_order = {} -- needed because pairs loops randomly through tables
local set_nodes = {} -- all the nodes needed for sets local set_nodes = {} -- all the nodes needed for sets
@ -21,7 +21,7 @@ local S = minetest.get_translator("ambience")
-- add set to list -- add set to list
ambience.add_set = function(set_name, def) function ambience.add_set(set_name, def)
if not set_name or not def then return end if not set_name or not def then return end
@ -61,13 +61,13 @@ end
-- return set from list using name -- return set from list using name
ambience.get_set = function(set_name) function ambience.get_set(set_name)
return sound_sets[set_name] return sound_sets[set_name]
end end
-- remove set from list -- remove set from list
ambience.del_set = function(set_name) function ambience.del_set(set_name)
sound_sets[set_name] = nil sound_sets[set_name] = nil
@ -81,6 +81,26 @@ ambience.del_set = function(set_name)
if can_del then table.remove(sound_set_order, can_del) end if can_del then table.remove(sound_set_order, can_del) end
end end
-- return node total belonging to a specific group:
function ambience.group_total(ntab, ngrp)
local tot = 0
local def, grp
for _,n in pairs(ntab) do
def = minetest.registered_nodes[_]
grp = def and def.groups and def.groups[ngrp]
if grp and grp > 0 then
tot = tot + n
end
end
return tot
end
-- setup table when player joins -- setup table when player joins
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
@ -93,6 +113,7 @@ minetest.register_on_joinplayer(function(player)
playing[name] = { playing[name] = {
mvol = tonumber(meta:get_string("ambience.mvol")) or MUSICVOLUME, mvol = tonumber(meta:get_string("ambience.mvol")) or MUSICVOLUME,
svol = tonumber(meta:get_string("ambience.svol")) or SOUNDVOLUME, svol = tonumber(meta:get_string("ambience.svol")) or SOUNDVOLUME,
timer = 0,
music = 0, music = 0,
music_handler = nil music_handler = nil
} }
@ -108,12 +129,12 @@ end)
-- plays music and selects sound set -- plays music and selects sound set
local get_ambience = function(player, tod, name) local function get_ambience(player, tod, name)
-- if enabled and not already playing, play local/server music on interval check -- if enabled and not already playing, play local/server music on interval check
if play_music and playing[name] and playing[name].mvol > 0 then if play_music and playing[name] and playing[name].mvol > 0 then
-- increase music interval -- increase music time interval
playing[name].music = playing[name].music + 1 playing[name].music = playing[name].music + 1
-- play music on interval check -- play music on interval check
@ -146,8 +167,8 @@ local get_ambience = function(player, tod, name)
-- get all set nodes around player -- get all set nodes around player
local ps, cn = minetest.find_nodes_in_area( local ps, cn = minetest.find_nodes_in_area(
{x = pos.x - radius, y = pos.y - radius, z = pos.z - radius}, {x = pos.x - radius, y = pos.y - radius, z = pos.z - radius},
{x = pos.x + radius, y = pos.y + radius, z = pos.z + radius}, set_nodes) {x = pos.x + radius, y = pos.y + radius, z = pos.z + radius}, set_nodes)
-- loop through sets in order and choose first that meets conditions set -- loop through sets in order and choose first that meets conditions set
for n = 1, #sound_set_order do for n = 1, #sound_set_order do
@ -160,7 +181,7 @@ local get_ambience = function(player, tod, name)
local bdata = minetest.get_biome_data(pos) local bdata = minetest.get_biome_data(pos)
local biome = bdata and minetest.get_biome_name(bdata.biome) or "" local biome = bdata and minetest.get_biome_name(bdata.biome) or ""
-- pass settings to function for condition check -- pass settings to set function for condition check
local set_name, gain = set.sound_check({ local set_name, gain = set.sound_check({
player = player, player = player,
pos = pos, pos = pos,
@ -180,45 +201,68 @@ local get_ambience = function(player, tod, name)
return nil, nil return nil, nil
end end
-- players routine
local timer = 0 local timer = 0
local random = math.random local random = math.random
-- players routine
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
-- one second timer local players = minetest.get_connected_players()
timer = timer + dtime local pname
if timer < 1 then return end
timer = 0
local player_name, number, chance, ambience, handler, ok -- reduce sound timer for each player and stop/reset when needed
for _, player in pairs(players) do
pname = player:get_player_name()
if playing[pname] and playing[pname].timer > 0 then
playing[pname].timer = playing[pname].timer - dtime
if playing[pname].timer <= 0 then
if playing[pname].handler then
minetest.sound_stop(playing[pname].handler)
end
playing[pname].set = nil
playing[pname].gain = nil
playing[pname].handler = nil
end
end
end
-- one second timer
timer = timer + dtime ; if timer < 1 then return end ; timer = 0
local number, chance, ambience, handler, ok
local tod = minetest.get_timeofday() local tod = minetest.get_timeofday()
-- loop through players -- loop through players
for _, player in pairs(minetest.get_connected_players()) do for _, player in pairs(players) do
player_name = player:get_player_name() pname = player:get_player_name()
local set_name, MORE_GAIN = get_ambience(player, tod, player_name) local set_name, MORE_GAIN = get_ambience(player, tod, pname)
ok = playing[player_name] -- everything starts off ok if player found ok = playing[pname] -- everything starts off ok if player found
-- are we playing something already? -- are we playing something already?
if ok and playing[player_name].handler then if ok and playing[pname].handler then
-- stop current sound if another set active or gain changed -- stop current sound if another set active or gain changed
if playing[player_name].set ~= set_name if playing[pname].set ~= set_name
or playing[player_name].gain ~= MORE_GAIN then or playing[pname].gain ~= MORE_GAIN then
--print ("-- change stop", set_name, playing[player_name].handler) --print ("-- change stop", set_name, playing[pname].handler)
minetest.sound_stop(playing[player_name].handler) minetest.sound_stop(playing[pname].handler)
playing[player_name].set = nil playing[pname].set = nil
playing[player_name].gain = nil playing[pname].gain = nil
playing[player_name].handler = nil playing[pname].handler = nil
playing[pname].timer = 0
else else
ok = false -- sound set still playing, skip new sound ok = false -- sound set still playing, skip new sound
end end
@ -235,41 +279,21 @@ minetest.register_globalstep(function(dtime)
-- play sound -- play sound
handler = minetest.sound_play(ambience.name, { handler = minetest.sound_play(ambience.name, {
to_player = player_name, to_player = pname,
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * playing[player_name].svol, gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * playing[pname].svol,
pitch = ambience.pitch or 1.0 pitch = ambience.pitch or 1.0
}, ambience.ephemeral) }, ambience.ephemeral)
--print ("playing... " .. ambience.name .. " (" .. chance .. " < " --print ("playing... " .. ambience.name .. " (" .. chance .. " < "
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN, handler) -- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN, handler)
-- only continue if sound playing returns handler
if handler then if handler then
--print("-- current handler", handler) -- set what player is currently listening to if handler found
playing[pname].set = set_name
-- set what player is currently listening to playing[pname].gain = MORE_GAIN
playing[player_name].set = set_name playing[pname].handler = handler
playing[player_name].gain = MORE_GAIN playing[pname].timer = ambience.length
playing[player_name].handler = handler
-- set timer to stop sound
minetest.after(ambience.length, function(handler, player_name)
--print("-- timed stop", set_name, handler)
if handler then minetest.sound_stop(handler) end
-- reset variables if handlers match
if playing[player_name]
and playing[player_name].handler == handler then
--print("-- timed reset", handler, player_name)
playing[player_name].set = nil
playing[player_name].gain = nil
playing[player_name].handler = nil
end
end, handler, player_name)
end end
end end
end end
@ -341,4 +365,3 @@ dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
print("[MOD] Ambience Lite loaded") print("[MOD] Ambience Lite loaded")

7
locale/ambience.pl.tr Normal file
View File

@ -0,0 +1,7 @@
# textdomain: ambience
<svol>=<sgłośność>
set sound volume (0.1 to 1.0)=ustaw głośność dźwięku (0.1 do 1.0)
Sound volume set to @1=Głośność dźwięku ustawiona na @1
<mvol>=<mgłośność>
set music volume (0.1 to 1.0, 0 to stop music)=ustaw głośność muzyki (0.1 do 1.0, 0 aby zatrzymać muzykę)
Music volume set to @1=Głośność muzyki ustawiona na @1

View File

@ -76,75 +76,74 @@ end
-- check for env_sounds mod, if not found enable water flowing and lava sounds -- check for env_sounds mod, if not found enable water flowing and lava sounds
if not minetest.get_modpath("env_sounds") then if not minetest.get_modpath("env_sounds") then
-- Water sound plays when near flowing water -- Water sound plays when near flowing water
ambience.add_set("flowing_water", { ambience.add_set("flowing_water", {
frequency = 1000, frequency = 1000,
sounds = { sounds = {
{name = "waterfall", length = 6} {name = "waterfall", length = 6}
}, },
nodes = {"group:water"}, nodes = {"group:water"},
sound_check = function(def) sound_check = function(def)
local c = (def.totals["default:water_flowing"] or 0) local c = (def.totals["default:water_flowing"] or 0)
+ (def.totals["mcl_core:water_flowing"] or 0) + (def.totals["mcl_core:water_flowing"] or 0)
if c > 40 then return "flowing_water", 0.5 if c > 40 then return "flowing_water", 0.5
elseif c > 5 then return "flowing_water" end elseif c > 5 then return "flowing_water" end
end end
}) })
-- River sound plays when near flowing river -- River sound plays when near flowing river
ambience.add_set("river", { ambience.add_set("river", {
frequency = 1000, frequency = 1000,
sounds = { sounds = {
{name = "river", length = 4, gain = 0.1} {name = "river", length = 4, gain = 0.1}
}, },
sound_check = function(def) sound_check = function(def)
local c = (def.totals["default:river_water_flowing"] or 0) local c = (def.totals["default:river_water_flowing"] or 0)
+ (def.totals["mclx_core:river_water_flowing"] or 0) + (def.totals["mclx_core:river_water_flowing"] or 0)
if c > 20 then return "river", 0.5 if c > 20 then return "river", 0.5
elseif c > 5 then return "river" end elseif c > 5 then return "river" end
end end
}) })
-- Lava sound plays when near lava -- Lava sound plays when near lava
ambience.add_set("lava", { ambience.add_set("lava", {
frequency = 1000, frequency = 1000,
sounds = { sounds = {
{name = "lava", length = 7} {name = "lava", length = 7}
}, },
nodes = {"group:lava"}, nodes = {"group:lava"},
sound_check = function(def) sound_check = function(def)
local c = (def.totals["default:lava_source"] or 0) local c = (def.totals["default:lava_source"] or 0)
+ (def.totals["default:lava_flowing"] or 0) + (def.totals["default:lava_flowing"] or 0)
+ (def.totals["mcl_core:lava_source"] or 0) + (def.totals["mcl_core:lava_source"] or 0)
+ (def.totals["mcl_core:lava_flowing"] or 0) + (def.totals["mcl_core:lava_flowing"] or 0)
if c > 20 then return "lava", 0.5 if c > 20 then return "lava", 0.5
elseif c > 5 then return "lava" end
end
})
elseif c > 5 then return "lava" end
end
})
else else
print ("[MOD] Ambience - found env_sounds, using for water and lava sounds.") print ("[MOD] Ambience - found env_sounds, using for water and lava sounds.")
end end
@ -178,7 +177,7 @@ ambience.add_set("beach", {
ambience.add_set("ice", { ambience.add_set("ice", {
frequency = 250, frequency = 80,
sounds = { sounds = {
{name = "icecrack", length = 5, gain = 1.1}, {name = "icecrack", length = 5, gain = 1.1},
@ -194,7 +193,7 @@ ambience.add_set("ice", {
+(def.totals["mcl_core:ice"] or 0) +(def.totals["mcl_core:ice"] or 0)
+ (def.totals["mcl_core:packed_ice"] or 0) + (def.totals["mcl_core:packed_ice"] or 0)
if c > 100 then return "ice" end if c > 400 then return "ice" end
end end
}) })
@ -324,24 +323,10 @@ ambience.add_set("day", {
sound_check = function(def) sound_check = function(def)
-- we used group:leaves but still need to specify actual nodes for total -- use handy function to count all nodes in group:leaves
local c = (def.totals["default:leaves"] or 0) local c = ambience.group_total(def.totals, "leaves")
+ (def.totals["default:bush_leaves"] or 0)
+ (def.totals["default:pine_needles"] or 0)
+ (def.totals["default:aspen_leaves"] or 0)
+ (def.totals["mcl_trees:leaves_spruce"] or 0)
+ (def.totals["mcl_trees:leaves_oak"] or 0)
+ (def.totals["mcl_trees:leaves_mangrove"] or 0)
+ (def.totals["mcl_trees:leaves_birch"] or 0)
+ (def.totals["mcl_trees:leaves_acacia"] or 0)
+ (def.totals["ethereal:birch_leaves"] or 0)
+ (def.totals["ethereal:lemon_leaves"] or 0)
+ (def.totals["ethereal:olive_leaves"] or 0)
+ (def.totals["ethereal:redwood_leaves"] or 0)
+ (def.totals["ethereal:sakura_leaves"] or 0)
+ (def.totals["ethereal:sakura_leaves2"] or 0)
if (def.tod > 0.2 and def.tod < 0.8) and def.pos.y > -10 and c > 5 then if (def.tod > 0.2 and def.tod < 0.8) and def.pos.y > 0 and c > 50 then
return "day" return "day"
end end
end end
@ -360,29 +345,16 @@ ambience.add_set("night", {
{name = "cricket", length = 6, ephemeral = true}, {name = "cricket", length = 6, ephemeral = true},
{name = "deer", length = 7, ephemeral = true}, {name = "deer", length = 7, ephemeral = true},
{name = "frog", length = 1, ephemeral = true}, {name = "frog", length = 1, ephemeral = true},
{name = "frog", length = 1, pitch = 1.2, ephemeral = true} {name = "frog", length = 1, pitch = 1.2, ephemeral = true},
{name = "wind", length = 9}
}, },
sound_check = function(def) sound_check = function(def)
-- leaves were added in last set, so don't need to be added to this one -- use handy function to count all nodes in group:leaves
local c = (def.totals["default:leaves"] or 0) local c = ambience.group_total(def.totals, "leaves")
+ (def.totals["default:bush_leaves"] or 0)
+ (def.totals["default:pine_needles"] or 0)
+ (def.totals["default:aspen_leaves"] or 0)
+ (def.totals["mcl_trees:leaves_spruce"] or 0)
+ (def.totals["mcl_trees:leaves_oak"] or 0)
+ (def.totals["mcl_trees:leaves_mangrove"] or 0)
+ (def.totals["mcl_trees:leaves_birch"] or 0)
+ (def.totals["mcl_trees:leaves_acacia"] or 0)
+ (def.totals["ethereal:birch_leaves"] or 0)
+ (def.totals["ethereal:lemon_leaves"] or 0)
+ (def.totals["ethereal:olive_leaves"] or 0)
+ (def.totals["ethereal:redwood_leaves"] or 0)
+ (def.totals["ethereal:sakura_leaves"] or 0)
+ (def.totals["ethereal:sakura_leaves2"] or 0)
if (def.tod < 0.2 or def.tod > 0.8) and def.pos.y > -10 and c > 5 then if (def.tod < 0.2 or def.tod > 0.8) and def.pos.y > 0 and c > 50 then
return "night" return "night"
end end
end end