2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
ambience = {}
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2016-12-23 20:35:59 +01:00
|
|
|
-- override default water sounds
|
|
|
|
minetest.override_item("default:water_source", { sounds = {} })
|
|
|
|
minetest.override_item("default:water_flowing", { sounds = {} })
|
|
|
|
minetest.override_item("default:river_water_source", { sounds = {} })
|
|
|
|
minetest.override_item("default:river_water_flowing", { sounds = {} })
|
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- settings
|
|
|
|
local SOUNDVOLUME = 1.0
|
|
|
|
local MUSICVOLUME = 1.0
|
2017-10-09 13:19:58 +02:00
|
|
|
local play_music = minetest.settings:get_bool("ambience_music") ~= false
|
2018-03-24 10:43:55 +01:00
|
|
|
local pplus = minetest.get_modpath("playerplus")
|
2016-04-01 18:06:29 +02:00
|
|
|
local radius = 6
|
2018-08-30 16:09:48 +02:00
|
|
|
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
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- global functions
|
|
|
|
ambience.add_set = function(set_name, def)
|
2018-03-24 10:43:55 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if set_name and def then
|
2018-03-24 10:43:55 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
sound_sets[set_name] = {
|
|
|
|
frequency = def.frequency or 50,
|
|
|
|
sounds = def.sounds,
|
|
|
|
sound_check = def.sound_check,
|
|
|
|
nodes = def.nodes,
|
|
|
|
}
|
2018-03-24 10:43:55 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- add set name to the sound_set_order table
|
|
|
|
local can_add = true
|
2018-03-24 10:43:55 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
for i = 1, #sound_set_order do
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if sound_set_order[i] == set_name then
|
|
|
|
can_add = false
|
|
|
|
end
|
|
|
|
end
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if can_add then
|
|
|
|
table.insert(sound_set_order, set_name)
|
|
|
|
end
|
2015-02-04 19:30:43 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- add any missing nodes to the set_nodes table
|
|
|
|
if def.nodes then
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
for i = 1, #def.nodes do
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
can_add = def.nodes[i]
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
for j = 1, #set_nodes do
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if def.nodes[i] == set_nodes[j] then
|
|
|
|
can_add = false
|
|
|
|
end
|
|
|
|
end
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if can_add then
|
|
|
|
table.insert(set_nodes, can_add)
|
|
|
|
end
|
|
|
|
end
|
2017-07-06 13:29:23 +02:00
|
|
|
end
|
|
|
|
end
|
2018-08-30 16:09:48 +02:00
|
|
|
end
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2015-01-14 15:50:19 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
ambience.get_set = function(set_name)
|
2015-01-14 15:50:19 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if sound_sets[set_name] then
|
|
|
|
return sound_sets[set_name]
|
|
|
|
end
|
|
|
|
end
|
2016-06-13 11:56:24 +02:00
|
|
|
|
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
ambience.del_set = function(set_name)
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
sound_sets[set_name] = nil
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
local can_del = false
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
for i = 1, #sound_set_order do
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if sound_set_order[i] == set_name then
|
|
|
|
can_del = i
|
|
|
|
end
|
2016-06-17 22:29:18 +02:00
|
|
|
end
|
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if can_del then
|
|
|
|
table.remove(sound_set_order, can_del)
|
2014-11-09 20:32:21 +01:00
|
|
|
end
|
2018-08-30 16:09:48 +02:00
|
|
|
end
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2017-08-23 11:15:53 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- plays music and selects sound set
|
|
|
|
local get_ambience = function(player, tod, name)
|
2015-09-30 12:47:52 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- play server or local music if available
|
|
|
|
if play_music and playing[name] then
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- play at midnight
|
|
|
|
if tod >= 0.0 and tod <= 0.01 then
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if not playing[name].music then
|
2016-04-01 18:06:29 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
playing[name].music = minetest.sound_play("ambience_music", {
|
|
|
|
to_player = player:get_player_name(),
|
|
|
|
gain = MUSICVOLUME
|
|
|
|
})
|
|
|
|
end
|
2016-04-01 18:06:29 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
elseif tod > 0.1 and playing[name].music then
|
2016-06-06 11:32:09 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
playing[name].music = nil
|
2016-06-06 11:32:09 +02:00
|
|
|
end
|
2014-11-09 20:32:21 +01:00
|
|
|
end
|
2015-02-04 19:30:43 +01:00
|
|
|
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- get foot and head level nodes at player position
|
|
|
|
local pos = player:get_pos()
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
pos.y = pos.y + 1.4 -- head level
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
local nod_head = pplus and playerplus[name].nod_head or minetest.get_node(pos).name
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
pos.y = pos.y - 1.2 -- foot level
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
local nod_feet = pplus and playerplus[name].nod_feet or minetest.get_node(pos).name
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
pos.y = pos.y - 0.2 -- reset pos
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- 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)
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-09-04 11:16:55 +02:00
|
|
|
-- loop through sets in order and choose one that meets it's conditions
|
2018-08-30 16:09:48 +02:00
|
|
|
for n = 1, #sound_set_order do
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
local set = sound_sets[ sound_set_order[n] ]
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if set and set.sound_check then
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
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
|
|
|
|
})
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if set_name then
|
|
|
|
return set_name, gain
|
|
|
|
end
|
|
|
|
end
|
2014-11-09 20:32:21 +01:00
|
|
|
end
|
2015-02-16 16:46:59 +01:00
|
|
|
end
|
2015-02-04 19:30:43 +01:00
|
|
|
|
2016-06-17 22:29:18 +02:00
|
|
|
|
2014-11-09 20:32:21 +01:00
|
|
|
local timer = 0
|
2018-08-30 16:09:48 +02:00
|
|
|
local random = math.random
|
2016-06-17 22:29:18 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- player routine
|
2014-11-09 20:32:21 +01:00
|
|
|
minetest.register_globalstep(function(dtime)
|
2015-02-16 16:46:59 +01:00
|
|
|
|
2015-02-04 19:30:43 +01:00
|
|
|
-- every 1 second
|
2016-02-26 22:40:53 +01:00
|
|
|
timer = timer + dtime
|
2015-02-04 19:30:43 +01:00
|
|
|
if timer < 1 then return end
|
2014-11-09 20:32:21 +01:00
|
|
|
timer = 0
|
2015-02-04 19:30:43 +01:00
|
|
|
|
2016-06-06 11:32:09 +02:00
|
|
|
local players = minetest.get_connected_players()
|
2018-08-30 16:09:48 +02:00
|
|
|
local player_name, number, chance, ambience, handler
|
|
|
|
local tod = minetest.get_timeofday()
|
2016-06-06 11:32:09 +02:00
|
|
|
|
|
|
|
for n = 1, #players do
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
player_name = players[n]:get_player_name()
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2017-01-25 21:21:40 +01:00
|
|
|
--local t1 = os.clock()
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
local set_name, MORE_GAIN = get_ambience(players[n], tod, player_name)
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2017-01-25 21:21:40 +01:00
|
|
|
--print(string.format("elapsed time: %.4f\n", os.clock() - t1))
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if set_name then
|
2014-11-09 20:32:21 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- stop sound if another set active
|
|
|
|
if playing[player_name]
|
|
|
|
and playing[player_name].handler then
|
2015-09-30 12:47:52 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
if playing[player_name].sound ~= set_name
|
|
|
|
or (playing[player_name].sound == set_name
|
|
|
|
and playing[player_name].gain ~= MORE_GAIN) then
|
2016-02-26 22:40:53 +01:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
minetest.sound_stop(playing[player_name].handler)
|
|
|
|
|
|
|
|
playing[player_name].sound = nil
|
|
|
|
playing[player_name].handler = nil
|
|
|
|
playing[player_name].gain = nil
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-04 11:16:55 +02:00
|
|
|
-- choose random sound from set selected
|
2018-08-30 16:09:48 +02:00
|
|
|
number = random(1, #sound_sets[set_name].sounds)
|
|
|
|
ambience = sound_sets[set_name].sounds[number]
|
|
|
|
|
|
|
|
math.randomseed(tod + number)
|
|
|
|
|
|
|
|
chance = random(1, 1000)
|
|
|
|
|
|
|
|
if chance < sound_sets[set_name].frequency then
|
|
|
|
|
|
|
|
handler = minetest.sound_play(ambience.name, {
|
|
|
|
to_player = player_name,
|
|
|
|
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * SOUNDVOLUME
|
|
|
|
})
|
|
|
|
|
|
|
|
--print ("playing... " .. ambience.name .. " (" .. chance .. " < "
|
|
|
|
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN)
|
|
|
|
|
|
|
|
if handler then
|
|
|
|
|
|
|
|
playing[player_name] = playing[player_name] or {}
|
|
|
|
playing[player_name].handler = handler
|
|
|
|
playing[player_name].sound = set_name
|
|
|
|
playing[player_name].gain = MORE_GAIN
|
|
|
|
|
|
|
|
minetest.after(ambience.length, function(args)
|
|
|
|
|
|
|
|
local player_name = args[2]
|
|
|
|
|
|
|
|
if playing[player_name]
|
|
|
|
and playing[player_name].handler
|
|
|
|
and playing[player_name].sound == set_name then
|
|
|
|
|
|
|
|
minetest.sound_stop(playing[player_name].handler)
|
|
|
|
|
|
|
|
playing[player_name].sound = nil
|
|
|
|
playing[player_name].handler = nil
|
|
|
|
playing[player_name].gain = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end, {ambience, player_name})
|
|
|
|
end
|
2014-11-09 20:32:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
|
2017-07-06 13:29:23 +02:00
|
|
|
-- set volume commands
|
2014-11-09 20:32:21 +01:00
|
|
|
minetest.register_chatcommand("svol", {
|
|
|
|
params = "<svol>",
|
|
|
|
description = "set sound volume (0.1 to 1.0)",
|
2015-09-30 12:47:52 +02:00
|
|
|
privs = {server = true},
|
2016-06-17 22:29:18 +02:00
|
|
|
|
2014-11-09 20:32:21 +01:00
|
|
|
func = function(name, param)
|
2016-06-17 22:29:18 +02:00
|
|
|
|
|
|
|
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
|
2014-11-09 20:32:21 +01:00
|
|
|
end,
|
2016-02-26 22:40:53 +01:00
|
|
|
})
|
2017-07-06 13:29:23 +02:00
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
|
2017-07-06 13:29:23 +02:00
|
|
|
minetest.register_chatcommand("mvol", {
|
|
|
|
params = "<mvol>",
|
|
|
|
description = "set music volume (0.1 to 1.0)",
|
|
|
|
privs = {server = true},
|
|
|
|
|
|
|
|
func = function(name, param)
|
|
|
|
|
|
|
|
MUSICVOLUME = tonumber(param) or MUSICVOLUME
|
|
|
|
|
2018-08-30 16:09:48 +02:00
|
|
|
-- ability to stop music just as it begins
|
|
|
|
if MUSICVOLUME == 0 and playing[name].music then
|
|
|
|
minetest.sound_stop(playing[name].music)
|
|
|
|
end
|
|
|
|
|
2017-07-06 13:29:23 +02:00
|
|
|
if MUSICVOLUME < 0.1 then MUSICVOLUME = 0.1 end
|
|
|
|
if MUSICVOLUME > 1.0 then MUSICVOLUME = 1.0 end
|
|
|
|
|
|
|
|
return true, "Music volume set to " .. MUSICVOLUME
|
|
|
|
end,
|
|
|
|
})
|
2018-08-30 16:09:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- load default sound sets
|
|
|
|
dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
|
|
|
|
|
|
|
|
|
|
|
|
print("[MOD] Ambience Lite loaded")
|