Camas/mods/ambiance/init.lua

187 lines
4.9 KiB
Lua
Raw Normal View History

2015-12-16 19:01:43 +01:00
--
-- Ambiance mod
-- By Kaadmy, for Pixture
--
2019-09-21 19:38:34 +02:00
local mod_weather = minetest.get_modpath("weather")
2015-12-16 19:01:43 +01:00
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
2019-09-21 19:38:34 +02:00
if mod_weather then
if weather.weather ~= "clear" then
return false
end
end
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,
2019-09-21 20:42:31 +02:00
nodename = {"group:normal_grass", "group:dry_grass"},
2015-12-16 19:01:43 +01:00
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
2019-09-21 19:38:34 +02:00
if mod_weather then
if weather.weather ~= "clear" then
return false
end
end
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
}
2019-09-21 20:42:31 +02:00
ambiance.sounds["frog"] = {
length = 0.5,
chance = 64,
pitch_min = -10,
pitch_max = 10,
file = "ambiance_frog",
dist = 16,
nodename = "group:swamp_grass",
can_play = function(pos)
local tod = (minetest.get_timeofday() or 1) * 2
if tod < 0.4 or tod > 1.6 then
return true
end
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",
}
local ambiance_volume = tonumber(minetest.settings:get("ambiance_volume")) or 1.0
2015-12-16 19:01:43 +01: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
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
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
2019-09-21 20:42:31 +02:00
local pitch = nil
if sound.pitch_min and sound.pitch_max then
pitch = 1 + 0.01 * math.random(sound.pitch_min, sound.pitch_max)
end
2017-05-12 05:24:40 +02:00
soundspec[name][soundname] = minetest.sound_play(
sound.file,
{
pos = sourcepos,
max_hear_distance = sound.dist,
gain = ambiance_volume,
2019-09-21 20:42:31 +02:00
pitch = pitch,
2017-05-12 05:24:40 +02:00
})
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")