2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- global
|
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
ambience = {}
|
|
|
|
|
|
|
|
-- settings
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
local SOUNDVOLUME = 1.0
|
2021-06-27 17:39:50 +02:00
|
|
|
local MUSICVOLUME = 0.6
|
2024-12-19 12:55:40 +01:00
|
|
|
local MUSICINTERVAL = 60 * 20
|
2020-10-26 17:38:53 +01:00
|
|
|
local play_music = minetest.settings:get_bool("ambience_music") ~= false
|
|
|
|
local radius = 6
|
2024-12-19 12:55:40 +01:00
|
|
|
local playing = {} -- user settings, timers and current set playing
|
2020-10-26 17:38:53 +01:00
|
|
|
local sound_sets = {} -- all the sounds and their settings
|
|
|
|
local sound_set_order = {} -- needed because pairs loops randomly through tables
|
|
|
|
local set_nodes = {} -- all the nodes needed for sets
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- translation
|
|
|
|
|
|
|
|
local S = minetest.get_translator("ambience")
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
-- add set to list
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
function ambience.add_set(set_name, def)
|
|
|
|
|
|
|
|
if not set_name or not def then return end
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
sound_sets[set_name] = {
|
|
|
|
frequency = def.frequency or 50,
|
|
|
|
sounds = def.sounds,
|
|
|
|
sound_check = def.sound_check,
|
|
|
|
nodes = def.nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
-- add set name to the sound_set_order table
|
|
|
|
local can_add = true
|
|
|
|
|
|
|
|
for i = 1, #sound_set_order do
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if sound_set_order[i] == set_name then can_add = false end
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if can_add then table.insert(sound_set_order, set_name) end
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
-- add any missing nodes to the set_nodes table
|
|
|
|
if def.nodes then
|
|
|
|
|
|
|
|
for i = 1, #def.nodes do
|
|
|
|
|
|
|
|
can_add = def.nodes[i]
|
|
|
|
|
|
|
|
for j = 1, #set_nodes do
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if def.nodes[i] == set_nodes[j] then can_add = false end
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if can_add then table.insert(set_nodes, can_add) end
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- return set from list using name
|
2024-12-19 12:55:40 +01:00
|
|
|
|
|
|
|
function ambience.get_set(set_name)
|
2021-05-22 19:45:55 +02:00
|
|
|
return sound_sets[set_name]
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- remove set from list
|
2024-12-19 12:55:40 +01:00
|
|
|
|
|
|
|
function ambience.del_set(set_name)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
sound_sets[set_name] = nil
|
|
|
|
|
|
|
|
local can_del = false
|
|
|
|
|
|
|
|
for i = 1, #sound_set_order do
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if sound_set_order[i] == set_name then can_del = i end
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if can_del then table.remove(sound_set_order, can_del) end
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- 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
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-05-22 19:45:55 +02:00
|
|
|
-- setup table when player joins
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2021-05-22 19:45:55 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2021-12-05 14:17:16 +01:00
|
|
|
if player then
|
2024-12-19 12:55:40 +01:00
|
|
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local meta = player:get_meta()
|
|
|
|
|
|
|
|
playing[name] = {
|
|
|
|
mvol = tonumber(meta:get_string("ambience.mvol")) or MUSICVOLUME,
|
|
|
|
svol = tonumber(meta:get_string("ambience.svol")) or SOUNDVOLUME,
|
|
|
|
timer = 0,
|
|
|
|
music = 0,
|
|
|
|
music_handler = nil
|
|
|
|
}
|
2021-12-05 14:17:16 +01:00
|
|
|
end
|
2021-05-22 19:45:55 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- remove table when player leaves
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2021-05-22 19:45:55 +02:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if player then playing[player:get_player_name()] = nil end
|
|
|
|
end)
|
2021-05-22 19:45:55 +02:00
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
-- plays music and selects sound set
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local function get_ambience(player, tod, name)
|
|
|
|
|
|
|
|
-- 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
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- increase music time interval
|
|
|
|
playing[name].music = playing[name].music + 1
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- play music on interval check
|
|
|
|
if playing[name].music > MUSICINTERVAL and playing[name].music_handler == nil then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
playing[name].music_handler = minetest.sound_play("ambience_music", {
|
2021-06-27 17:39:50 +02:00
|
|
|
to_player = name,
|
2024-12-19 12:55:40 +01:00
|
|
|
gain = playing[name].mvol
|
2021-06-27 17:39:50 +02:00
|
|
|
})
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
playing[name].music = 0 -- reset interval
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
2024-12-19 12:55:40 +01:00
|
|
|
--print("-- music timer", playing[name].music .. "/" .. MUSICINTERVAL)
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- get foot and head level nodes at player position
|
2021-02-25 20:49:03 +01:00
|
|
|
local pos = player:get_pos() ; if not pos then return end
|
2021-06-12 20:20:04 +02:00
|
|
|
local prop = player:get_properties()
|
2021-10-29 13:57:02 +02:00
|
|
|
local eyeh = prop.eye_height or 1.47 -- eye level with fallback
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
pos.y = pos.y + eyeh -- head level
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local nod_head = minetest.get_node(pos).name
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-10-29 13:57:02 +02:00
|
|
|
pos.y = (pos.y - eyeh) + 0.2 -- foot level
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local nod_feet = minetest.get_node(pos).name
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
pos.y = pos.y - 0.2 -- reset pos
|
|
|
|
|
|
|
|
-- get all set nodes around player
|
|
|
|
local ps, cn = minetest.find_nodes_in_area(
|
2024-12-19 12:55:40 +01:00
|
|
|
{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)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- loop through sets in order and choose first that meets conditions set
|
2020-10-26 17:38:53 +01:00
|
|
|
for n = 1, #sound_set_order do
|
|
|
|
|
|
|
|
local set = sound_sets[ sound_set_order[n] ]
|
|
|
|
|
|
|
|
if set and set.sound_check then
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- get biome data
|
|
|
|
local bdata = minetest.get_biome_data(pos)
|
|
|
|
local biome = bdata and minetest.get_biome_name(bdata.biome) or ""
|
|
|
|
|
|
|
|
-- pass settings to set function for condition check
|
2020-10-26 17:38:53 +01:00
|
|
|
local set_name, gain = set.sound_check({
|
|
|
|
player = player,
|
|
|
|
pos = pos,
|
|
|
|
tod = tod,
|
|
|
|
totals = cn,
|
|
|
|
positions = ps,
|
|
|
|
head_node = nod_head,
|
2024-12-19 12:55:40 +01:00
|
|
|
feet_node = nod_feet,
|
|
|
|
biome = biome
|
2020-10-26 17:38:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- if conditions met return set name and gain value
|
2024-12-19 12:55:40 +01:00
|
|
|
if set_name then return set_name, gain end
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
end
|
2021-05-22 19:45:55 +02:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
return nil, nil
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- players routine
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
local timer = 0
|
|
|
|
local random = math.random
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
local pname
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
-- one second timer
|
2024-12-19 12:55:40 +01:00
|
|
|
timer = timer + dtime ; if timer < 1 then return end ; timer = 0
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local number, chance, ambience, handler, ok
|
2020-10-26 17:38:53 +01:00
|
|
|
local tod = minetest.get_timeofday()
|
|
|
|
|
|
|
|
-- loop through players
|
2024-12-19 12:55:40 +01:00
|
|
|
for _, player in pairs(players) do
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
pname = player:get_player_name()
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local set_name, MORE_GAIN = get_ambience(player, tod, pname)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
ok = playing[pname] -- everything starts off ok if player found
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-03-31 10:57:51 +02:00
|
|
|
-- are we playing something already?
|
2024-12-19 12:55:40 +01:00
|
|
|
if ok and playing[pname].handler then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-03-31 10:57:51 +02:00
|
|
|
-- stop current sound if another set active or gain changed
|
2024-12-19 12:55:40 +01:00
|
|
|
if playing[pname].set ~= set_name
|
|
|
|
or playing[pname].gain ~= MORE_GAIN then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
--print ("-- change stop", set_name, playing[pname].handler)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
minetest.sound_stop(playing[pname].handler)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
playing[pname].set = nil
|
|
|
|
playing[pname].gain = nil
|
|
|
|
playing[pname].handler = nil
|
|
|
|
playing[pname].timer = 0
|
2020-10-26 17:38:53 +01:00
|
|
|
else
|
|
|
|
ok = false -- sound set still playing, skip new sound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-31 10:57:51 +02:00
|
|
|
-- set random chance
|
2024-12-19 12:55:40 +01:00
|
|
|
chance = random(1000)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
-- if chance is lower than set frequency then select set
|
|
|
|
if ok and set_name and chance < sound_sets[set_name].frequency then
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
number = random(#sound_sets[set_name].sounds) -- choose random sound from set
|
|
|
|
ambience = sound_sets[set_name].sounds[number] -- grab sound information
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
-- play sound
|
|
|
|
handler = minetest.sound_play(ambience.name, {
|
2024-12-19 12:55:40 +01:00
|
|
|
to_player = pname,
|
|
|
|
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * playing[pname].svol,
|
2020-10-26 17:38:53 +01:00
|
|
|
pitch = ambience.pitch or 1.0
|
|
|
|
}, ambience.ephemeral)
|
|
|
|
|
|
|
|
--print ("playing... " .. ambience.name .. " (" .. chance .. " < "
|
|
|
|
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN, handler)
|
|
|
|
|
|
|
|
if handler then
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- set what player is currently listening to if handler found
|
|
|
|
playing[pname].set = set_name
|
|
|
|
playing[pname].gain = MORE_GAIN
|
|
|
|
playing[pname].handler = handler
|
|
|
|
playing[pname].timer = ambience.length
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- sound volume command
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
minetest.register_chatcommand("svol", {
|
2024-12-19 12:55:40 +01:00
|
|
|
params = S("<svol>"),
|
|
|
|
description = S("set sound volume (0.1 to 1.0)"),
|
|
|
|
privs = {},
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
func = function(name, param)
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local svol = tonumber(param) or playing[name].svol
|
|
|
|
|
|
|
|
if svol < 0.1 then svol = 0.1 end
|
|
|
|
if svol > 1.0 then svol = 1.0 end
|
|
|
|
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
local meta = player:get_meta()
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
meta:set_string("ambience.svol", svol)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
playing[name].svol = svol
|
|
|
|
|
|
|
|
return true, S("Sound volume set to @1", svol)
|
2021-05-22 19:45:55 +02:00
|
|
|
end
|
2020-10-26 17:38:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- music volume command (0 stops music)
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
minetest.register_chatcommand("mvol", {
|
2024-12-19 12:55:40 +01:00
|
|
|
params = S("<mvol>"),
|
|
|
|
description = S("set music volume (0.1 to 1.0, 0 to stop music)"),
|
|
|
|
privs = {},
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
func = function(name, param)
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
local mvol = tonumber(param) or playing[name].mvol
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
-- stop music currently playing by setting volume to 0
|
|
|
|
if mvol == 0 and playing[name].music_handler then
|
2021-06-27 17:39:50 +02:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
minetest.sound_stop(playing[name].music_handler)
|
2021-06-27 17:39:50 +02:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
playing[name].music_handler = nil
|
|
|
|
|
|
|
|
minetest.chat_send_player(name, S("Music stopped!"))
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
if mvol < 0 then mvol = 0 end
|
|
|
|
if mvol > 1.0 then mvol = 1.0 end
|
|
|
|
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
local meta = player:get_meta()
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2024-12-19 12:55:40 +01:00
|
|
|
meta:set_string("ambience.mvol", mvol)
|
|
|
|
|
|
|
|
playing[name].mvol = mvol
|
|
|
|
|
|
|
|
return true, S("Music volume set to @1", mvol)
|
2021-05-22 19:45:55 +02:00
|
|
|
end
|
2020-10-26 17:38:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- load default sound sets
|
2024-12-19 12:55:40 +01:00
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
|
|
|
|
|
|
|
|
|
|
|
|
print("[MOD] Ambience Lite loaded")
|