2015-12-16 19:01:43 +01:00
|
|
|
--
|
|
|
|
-- Ambiance mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
|
|
|
ambiance = {}
|
|
|
|
|
|
|
|
ambiance.sounds = {}
|
|
|
|
|
|
|
|
ambiance.sounds["birds"] = {
|
|
|
|
length = 5.0,
|
|
|
|
chance = 4,
|
|
|
|
file = "ambiance_birds",
|
|
|
|
dist = 8,
|
|
|
|
nodename = "group:leaves",
|
|
|
|
can_play = function(pos)
|
2017-05-12 05:24:40 +02:00
|
|
|
local tod = (minetest.get_timeofday() or 1) * 2
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
if tod > 0.47 and tod < 1.53 then -- bit of overlap into crickets
|
|
|
|
return true
|
|
|
|
end
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
return false
|
|
|
|
end,
|
2015-12-16 19:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ambiance.sounds["crickets"] = {
|
|
|
|
length = 6.0,
|
|
|
|
chance = 3,
|
|
|
|
file = "ambiance_crickets",
|
|
|
|
dist = 8,
|
|
|
|
nodename = "group:grass",
|
|
|
|
can_play = function(pos)
|
2017-05-12 05:24:40 +02:00
|
|
|
local tod = (minetest.get_timeofday() or 1) * 2
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
if tod < 0.5 or tod > 1.5 then
|
|
|
|
return true
|
|
|
|
end
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
return false
|
|
|
|
end,
|
2015-12-16 19:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ambiance.sounds["flowing_water"] = {
|
|
|
|
length = 3.3,
|
|
|
|
chance = 1,
|
2017-05-12 05:24:40 +02:00
|
|
|
file = "ambiance_water",
|
2015-12-16 19:01:43 +01:00
|
|
|
dist = 16,
|
|
|
|
nodename = "group:flowing_water",
|
|
|
|
}
|
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
local ambiance_volume = tonumber(minetest.settings:get("ambiance_volume")) or 1.0
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
if minetest.settings:get_bool("ambiance_enable") == true then
|
2017-05-12 05:24:40 +02:00
|
|
|
local soundspec = {}
|
|
|
|
local lastsound = {}
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
local function ambient_node_near(sound, pos)
|
|
|
|
local nodepos = minetest.find_node_near(pos, sound.dist, sound.nodename)
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
if nodepos ~= nil and math.random(1, sound.chance) == 1 then
|
|
|
|
return nodepos
|
|
|
|
end
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
return nil
|
|
|
|
end
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
local function step(dtime)
|
|
|
|
local player_positions = {}
|
|
|
|
|
|
|
|
for _, player in ipairs(minetest.get_connected_players()) do
|
2019-03-20 15:04:20 +01:00
|
|
|
local pos = player:get_pos()
|
2017-05-12 05:24:40 +02:00
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
for soundname, sound in pairs(ambiance.sounds) do
|
2017-06-27 21:13:30 +02:00
|
|
|
if not minetest.settings:get_bool("ambiance_disable_" .. soundname) then
|
2019-08-30 20:32:18 +02:00
|
|
|
if lastsound[name] == nil then
|
|
|
|
-- lastsound is not initialized yet
|
|
|
|
return
|
|
|
|
end
|
2017-05-12 05:24:40 +02:00
|
|
|
if lastsound[name][soundname] then
|
|
|
|
lastsound[name][soundname] = lastsound[name][soundname] + dtime
|
|
|
|
else
|
|
|
|
lastsound[name][soundname] = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if lastsound[name][soundname] > sound.length then
|
|
|
|
local sourcepos = ambient_node_near(sound, pos)
|
|
|
|
|
|
|
|
if sound.can_play and sourcepos ~= nil and (not sound.can_play(sourcepos)) then
|
|
|
|
sourcepos = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if sourcepos == nil then
|
|
|
|
if soundspec[name][soundname] then
|
|
|
|
minetest.sound_stop(soundspec[name][soundname])
|
|
|
|
|
|
|
|
soundspec[name][soundname] = nil
|
|
|
|
lastsound[name][soundname] = 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local ok = true
|
|
|
|
for _, p in pairs(player_positions) do
|
|
|
|
if (p.x * pos.x) + (p.y * pos.y) + (p.z * pos.z) < sound.dist * sound.dist then
|
|
|
|
ok = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if ok then
|
|
|
|
soundspec[name][soundname] = minetest.sound_play(
|
|
|
|
sound.file,
|
|
|
|
{
|
|
|
|
pos = sourcepos,
|
|
|
|
max_hear_distance = sound.dist,
|
|
|
|
gain = ambiance_volume,
|
|
|
|
})
|
|
|
|
|
|
|
|
lastsound[name][soundname] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(player_positions, pos)
|
|
|
|
end
|
|
|
|
end
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
local function on_joinplayer(player)
|
2015-12-16 19:01:43 +01:00
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
soundspec[name] = {}
|
|
|
|
lastsound[name] = {}
|
2015-12-16 19:01:43 +01:00
|
|
|
end
|
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
local function on_leaveplayer(player)
|
|
|
|
local name = player:get_player_name()
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
soundspec[name] = nil
|
|
|
|
lastsound[name] = nil
|
|
|
|
end
|
2015-12-16 19:01:43 +01:00
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
minetest.register_on_joinplayer(on_joinplayer)
|
|
|
|
minetest.register_on_leaveplayer(on_leaveplayer)
|
|
|
|
minetest.register_globalstep(step)
|
2015-12-16 19:01:43 +01:00
|
|
|
end
|
|
|
|
|
2017-05-12 05:24:40 +02:00
|
|
|
default.log("mod:ambiance", "loaded")
|