2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
ambience = {}
|
|
|
|
|
|
|
|
-- settings
|
|
|
|
local SOUNDVOLUME = 1.0
|
2021-06-27 17:39:50 +02:00
|
|
|
local MUSICVOLUME = 0.6
|
2020-10-26 17:38:53 +01:00
|
|
|
local play_music = minetest.settings:get_bool("ambience_music") ~= false
|
|
|
|
local pplus = minetest.get_modpath("playerplus")
|
|
|
|
local radius = 6
|
|
|
|
local playing = {}
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
-- add set to list
|
|
|
|
ambience.add_set = function(set_name, def)
|
|
|
|
|
|
|
|
if not set_name or not def then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if sound_set_order[i] == set_name then
|
|
|
|
can_add = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if can_add then
|
|
|
|
table.insert(sound_set_order, set_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
if def.nodes[i] == set_nodes[j] then
|
|
|
|
can_add = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if can_add then
|
|
|
|
table.insert(set_nodes, can_add)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- return set from list using name
|
|
|
|
ambience.get_set = function(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
|
|
|
|
ambience.del_set = function(set_name)
|
|
|
|
|
|
|
|
sound_sets[set_name] = nil
|
|
|
|
|
|
|
|
local can_del = false
|
|
|
|
|
|
|
|
for i = 1, #sound_set_order do
|
|
|
|
|
|
|
|
if sound_set_order[i] == set_name then
|
|
|
|
can_del = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if can_del then
|
|
|
|
table.remove(sound_set_order, can_del)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-05-22 19:45:55 +02:00
|
|
|
-- setup table when player joins
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2021-06-27 17:39:50 +02:00
|
|
|
playing[player:get_player_name()] = {music = -1}
|
2021-05-22 19:45:55 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- remove table when player leaves
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
playing[player:get_player_name()] = nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
-- plays music and selects sound set
|
|
|
|
local get_ambience = function(player, tod, name)
|
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
-- play server or local music if music enabled and music not already playing
|
2021-07-24 12:22:10 +02:00
|
|
|
if play_music and MUSICVOLUME > 0
|
|
|
|
and playing[name] and playing[name].music < 0 then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
-- count backwards
|
|
|
|
playing[name].music = playing[name].music -1
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
-- play music every 20 minutes
|
|
|
|
if playing[name].music < -(60 * 20) then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
playing[name].music = minetest.sound_play("ambience_music", {
|
|
|
|
to_player = name,
|
|
|
|
gain = MUSICVOLUME
|
|
|
|
})
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
-- reset music timer after 10 minutes
|
|
|
|
minetest.after(60 * 10, function(name)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
if playing[name] then
|
|
|
|
playing[name].music = -1
|
|
|
|
end
|
|
|
|
end, name)
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
2021-06-27 17:39:50 +02:00
|
|
|
|
|
|
|
--print("-- music count", playing[name].music)
|
|
|
|
|
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
|
|
|
|
2021-10-29 13:57:02 +02:00
|
|
|
pos.y = pos.y + eyeh
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-05-16 11:33:49 +02:00
|
|
|
local nod_head = pplus and name and playerplus[name]
|
|
|
|
and playerplus[name].nod_head or 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
|
|
|
|
2021-05-16 11:33:49 +02:00
|
|
|
local nod_feet = pplus and name and playerplus[name]
|
|
|
|
and playerplus[name].nod_feet or 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(
|
|
|
|
{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)
|
|
|
|
|
|
|
|
-- loop through sets in order and choose first that meets it's conditions
|
|
|
|
for n = 1, #sound_set_order do
|
|
|
|
|
|
|
|
local set = sound_sets[ sound_set_order[n] ]
|
|
|
|
|
|
|
|
if set and set.sound_check then
|
|
|
|
|
|
|
|
-- pass settings to function for condition check
|
|
|
|
local set_name, gain = set.sound_check({
|
|
|
|
player = player,
|
|
|
|
pos = pos,
|
|
|
|
tod = tod,
|
|
|
|
totals = cn,
|
|
|
|
positions = ps,
|
|
|
|
head_node = nod_head,
|
|
|
|
feet_node = nod_feet
|
|
|
|
})
|
|
|
|
|
|
|
|
-- if conditions met return set name and gain value
|
|
|
|
if set_name then
|
|
|
|
return set_name, gain
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-22 19:45:55 +02:00
|
|
|
|
|
|
|
return nil, nil -- ADDED
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local timer = 0
|
|
|
|
local random = math.random
|
|
|
|
|
|
|
|
-- players routine
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
|
|
|
|
-- one second timer
|
|
|
|
timer = timer + dtime
|
|
|
|
if timer < 1 then return end
|
|
|
|
timer = 0
|
|
|
|
|
|
|
|
local player_name, number, chance, ambience, handler, ok
|
|
|
|
local tod = minetest.get_timeofday()
|
|
|
|
|
|
|
|
-- loop through players
|
2021-06-12 20:20:04 +02:00
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-12 20:20:04 +02:00
|
|
|
player_name = player:get_player_name()
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
--local t1 = os.clock()
|
|
|
|
|
2021-06-12 20:20:04 +02:00
|
|
|
local set_name, MORE_GAIN = get_ambience(player, tod, player_name)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
--print(string.format("elapsed time: %.4f\n", os.clock() - t1))
|
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
ok = playing[player_name] -- 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?
|
2021-05-22 19:45:55 +02:00
|
|
|
if ok and playing[player_name].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
|
2020-10-26 17:38:53 +01:00
|
|
|
if playing[player_name].set ~= set_name
|
2021-03-31 10:57:51 +02:00
|
|
|
or playing[player_name].gain ~= MORE_GAIN then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-03-31 10:57:51 +02:00
|
|
|
--print ("-- change stop", set_name, playing[player_name].handler)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-03-31 10:57:51 +02:00
|
|
|
minetest.sound_stop(playing[player_name].handler)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
playing[player_name].set = nil
|
|
|
|
playing[player_name].gain = nil
|
2021-05-22 19:45:55 +02:00
|
|
|
playing[player_name].handler = nil
|
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
|
2020-10-26 17:38:53 +01:00
|
|
|
chance = random(1, 1000)
|
|
|
|
|
|
|
|
-- if chance is lower than set frequency then select set
|
|
|
|
if ok and set_name and chance < sound_sets[set_name].frequency then
|
|
|
|
|
|
|
|
-- choose random sound from set
|
|
|
|
number = random(#sound_sets[set_name].sounds)
|
|
|
|
ambience = sound_sets[set_name].sounds[number]
|
|
|
|
|
|
|
|
-- play sound
|
|
|
|
handler = minetest.sound_play(ambience.name, {
|
|
|
|
to_player = player_name,
|
|
|
|
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * SOUNDVOLUME,
|
|
|
|
pitch = ambience.pitch or 1.0
|
|
|
|
}, ambience.ephemeral)
|
|
|
|
|
|
|
|
--print ("playing... " .. ambience.name .. " (" .. chance .. " < "
|
|
|
|
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN, handler)
|
|
|
|
|
|
|
|
-- only continue if sound playing returns handler
|
|
|
|
if handler then
|
|
|
|
|
|
|
|
--print("-- current handler", handler)
|
|
|
|
|
|
|
|
-- set what player is currently listening to
|
2021-05-22 19:45:55 +02:00
|
|
|
playing[player_name].set = set_name
|
|
|
|
playing[player_name].gain = MORE_GAIN
|
|
|
|
playing[player_name].handler = handler
|
2020-10-26 17:38:53 +01:00
|
|
|
|
|
|
|
-- set timer to stop sound
|
2021-06-12 20:20:04 +02:00
|
|
|
minetest.after(ambience.length, function(handler, player_name)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-12 20:20:04 +02:00
|
|
|
--print("-- timed stop", set_name, handler)
|
|
|
|
if handler then
|
|
|
|
minetest.sound_stop(handler)
|
|
|
|
end
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-12 20:20:04 +02:00
|
|
|
-- reset variables if handlers match
|
2020-10-26 17:38:53 +01:00
|
|
|
if playing[player_name]
|
2021-03-31 10:57:51 +02:00
|
|
|
and playing[player_name].handler == handler then
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-06-12 20:20:04 +02:00
|
|
|
--print("-- timed reset", handler, player_name)
|
2020-10-26 17:38:53 +01:00
|
|
|
|
2021-05-22 19:45:55 +02:00
|
|
|
playing[player_name].set = nil
|
|
|
|
playing[player_name].gain = nil
|
|
|
|
playing[player_name].handler = nil
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
2021-06-12 20:20:04 +02:00
|
|
|
end, handler, player_name)
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
-- sound volume command
|
|
|
|
minetest.register_chatcommand("svol", {
|
|
|
|
params = "<svol>",
|
|
|
|
description = "set sound volume (0.1 to 1.0)",
|
|
|
|
privs = {server = true},
|
|
|
|
|
|
|
|
func = function(name, param)
|
|
|
|
|
|
|
|
SOUNDVOLUME = tonumber(param) or SOUNDVOLUME
|
|
|
|
|
|
|
|
if SOUNDVOLUME < 0.1 then SOUNDVOLUME = 0.1 end
|
|
|
|
if SOUNDVOLUME > 1.0 then SOUNDVOLUME = 1.0 end
|
|
|
|
|
|
|
|
return true, "Sound volume set to " .. SOUNDVOLUME
|
2021-05-22 19:45:55 +02:00
|
|
|
end
|
2020-10-26 17:38:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
-- music volume command (0 stops music)
|
|
|
|
minetest.register_chatcommand("mvol", {
|
|
|
|
params = "<mvol>",
|
2021-06-27 17:39:50 +02:00
|
|
|
description = "set music volume (0.1 to 1.0, 0 to stop music)",
|
2020-10-26 17:38:53 +01:00
|
|
|
privs = {server = true},
|
|
|
|
|
|
|
|
func = function(name, param)
|
|
|
|
|
|
|
|
MUSICVOLUME = tonumber(param) or MUSICVOLUME
|
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
-- ability to stop music by setting volume to 0
|
|
|
|
if MUSICVOLUME == 0 and playing[name].music
|
|
|
|
and playing[name].music >= 0 then
|
|
|
|
|
2020-10-26 17:38:53 +01:00
|
|
|
minetest.sound_stop(playing[name].music)
|
2021-06-27 17:39:50 +02:00
|
|
|
|
|
|
|
playing[name].music = -1
|
2020-10-26 17:38:53 +01:00
|
|
|
end
|
|
|
|
|
2021-06-27 17:39:50 +02:00
|
|
|
if MUSICVOLUME < 0 then MUSICVOLUME = 0 end
|
2020-10-26 17:38:53 +01:00
|
|
|
if MUSICVOLUME > 1.0 then MUSICVOLUME = 1.0 end
|
|
|
|
|
|
|
|
return true, "Music volume set to " .. MUSICVOLUME
|
2021-05-22 19:45:55 +02:00
|
|
|
end
|
2020-10-26 17:38:53 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
-- load default sound sets
|
|
|
|
dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
|
|
|
|
|
|
|
|
|
|
|
|
print("[MOD] Ambience Lite loaded")
|