1
0
mirror of https://github.com/minetest/minetest_game.git synced 2024-12-22 23:10:17 +01:00

Add lava environment sound (#2683)

This commit is contained in:
sfan5 2020-07-16 23:26:13 +02:00 committed by GitHub
parent 8c01a5b288
commit e193f9fda3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 27 deletions

View File

@ -11,3 +11,7 @@ Authors of media (sounds)
Yuval (CC0 1.0) Yuval (CC0 1.0)
https://freesound.org/people/Yuval/sounds/197023/ https://freesound.org/people/Yuval/sounds/197023/
env_sounds_water.*.ogg env_sounds_water.*.ogg
Halion (CC0 1.0)
https://freesound.org/people/Halion/sounds/17785/
env_sounds_lava.*.ogg

View File

@ -1,11 +1,41 @@
-- Parameters -- Parameters
local radius = 8 -- Water node search radius around player -- Node search radius around player
local radius = 8
-- End of parameters local allsounds = {
["env_sounds_water"] = {
trigger = {"default:water_flowing", "default:river_water_flowing"},
base_volume = 0.04,
max_volume = 0.4,
per_node = 0.004,
},
["env_sounds_lava"] = {
trigger = {"default:lava_source", "default:lava_flowing"},
base_volume = 0,
max_volume = 0.6,
per_node = {
["default:lava_source"] = 0.008,
["default:lava_flowing"] = 0.002,
},
},
}
if minetest.settings:get_bool("river_source_sounds") then
table.insert(allsounds["env_sounds_water"].trigger,
"default:river_water_source")
end
local river_source_sounds = minetest.settings:get_bool("river_source_sounds") -- Cache the union of all trigger nodes
local cache_triggers = {}
for sound, def in pairs(allsounds) do
for _, name in ipairs(def.trigger) do
table.insert(cache_triggers, name)
end
end
-- Update sound for player -- Update sound for player
@ -13,39 +43,57 @@ local river_source_sounds = minetest.settings:get_bool("river_source_sounds")
local function update_sound(player) local function update_sound(player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local ppos = player:get_pos() local ppos = player:get_pos()
ppos = vector.add(ppos, player:get_properties().eye_height)
local areamin = vector.subtract(ppos, radius) local areamin = vector.subtract(ppos, radius)
local areamax = vector.add(ppos, radius) local areamax = vector.add(ppos, radius)
local water_nodes = {"default:water_flowing", "default:river_water_flowing"}
if river_source_sounds then local pos = minetest.find_nodes_in_area(areamin, areamax, cache_triggers, true)
table.insert(water_nodes, "default:river_water_source") if next(pos) == nil then -- If table empty
end
local wpos, _ = minetest.find_nodes_in_area(areamin, areamax, water_nodes)
local waters = #wpos
if waters == 0 then
return return
end end
for sound, def in pairs(allsounds) do
-- Find average position of water positions -- Find average position
local wposav = vector.new() local posav = {0, 0, 0}
for _, pos in ipairs(wpos) do local count = 0
wposav.x = wposav.x + pos.x for _, name in ipairs(def.trigger) do
wposav.y = wposav.y + pos.y if pos[name] then
wposav.z = wposav.z + pos.z for _, p in ipairs(pos[name]) do
posav[1] = posav[1] + p.x
posav[2] = posav[2] + p.y
posav[3] = posav[3] + p.z
end
count = count + #pos[name]
end
end end
wposav = vector.divide(wposav, waters)
minetest.sound_play( if count > 0 then
"env_sounds_water", posav = vector.new(posav[1] / count, posav[2] / count,
{ posav[3] / count)
pos = wposav,
-- Calculate gain
local gain = def.base_volume
if type(def.per_node) == 'table' then
for name, multiplier in pairs(def.per_node) do
if pos[name] then
gain = gain + #pos[name] * multiplier
end
end
else
gain = gain + count * def.per_node
end
gain = math.min(gain, def.max_volume)
minetest.sound_play(sound, {
pos = posav,
to_player = player_name, to_player = player_name,
gain = math.min(0.04 + waters * 0.004, 0.4), gain = gain,
} }, true)
) end
end
end end
-- Update sound 'on joinplayer' -- Update sound when player joins
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
update_sound(player) update_sound(player)

Binary file not shown.

Binary file not shown.