1
0
mirror of https://codeberg.org/tenplus1/ambience.git synced 2024-12-26 10:40:29 +01:00

code tweak, set detection

This commit is contained in:
tenplus1 2021-06-09 08:53:52 +01:00
parent bffbb1c822
commit 0b7b1fd27d
4 changed files with 52 additions and 51 deletions

View File

@ -18,5 +18,6 @@ Based on Immersive Sounds .36 mod by Neuromancer and optimized to run on servers
- 1.3 - Added API for use with other mods, code rewrite
- 1.4 - Re-ordered water sets to come before fire and lava, day/night sounds play when leaves around and above ground
- 1.5 - Added 'flame_sound' and fire redo check, code tidy and tweak, added ephemeral flag for background sounds.
- 1.6 - Finding env_sounds disables water and lava sets, added 'ambience_water_move' flag to override water walking sounds, use eye level for head node.
Code license: MIT

View File

@ -1,13 +1,6 @@
ambience = {}
-- 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 = {} })
-- settings
local SOUNDVOLUME = 1.0
local MUSICVOLUME = 1.0
@ -132,13 +125,14 @@ local get_ambience = function(player, tod, name)
-- get foot and head level nodes at player position
local pos = player:get_pos() ; if not pos then return end
local prop = player:get_properties()
pos.y = pos.y + 1.4 -- head level
pos.y = pos.y + prop.eye_height -- eye level
local nod_head = pplus and name and playerplus[name]
and playerplus[name].nod_head or minetest.get_node(pos).name
pos.y = pos.y - 1.2 -- foot level
pos.y = (pos.y - prop.eye_height) + 0.2 -- foot level
local nod_feet = pplus and name and playerplus[name]
and playerplus[name].nod_feet or minetest.get_node(pos).name
@ -190,19 +184,17 @@ minetest.register_globalstep(function(dtime)
if timer < 1 then return end
timer = 0
-- get list of players and set some variables
local players = minetest.get_connected_players()
local player_name, number, chance, ambience, handler, ok
local tod = minetest.get_timeofday()
-- loop through players
for n = 1, #players do
for _, player in ipairs(minetest.get_connected_players()) do
player_name = players[n]:get_player_name()
player_name = player:get_player_name()
--local t1 = os.clock()
local set_name, MORE_GAIN = get_ambience(players[n], tod, player_name)
local set_name, MORE_GAIN = get_ambience(player, tod, player_name)
--print(string.format("elapsed time: %.4f\n", os.clock() - t1))
@ -260,18 +252,13 @@ minetest.register_globalstep(function(dtime)
-- set timer to stop sound
minetest.after(ambience.length, function()
--print("-- after", set_name, handler)
--print("-- timed stop after", set_name, handler, player_name)
-- make sure we are stopping same sound we started
if playing[player_name]
and playing[player_name].handler
and playing[player_name].handler == handler then
minetest.sound_stop(handler)
--print("-- timed stop", set_name, handler)
-- make sure player still around and reset variables
if playing[player_name] then
minetest.sound_stop(handler)
-- reset player variables
playing[player_name].set = nil
playing[player_name].gain = nil
playing[player_name].handler = nil

View File

@ -1,2 +1,5 @@
# If enabled will play a random music file from ./minetest/sounds at midnight
ambience_music (Ambience music) bool true
# If enabled then ambience will take over sounds when moving in water
ambience_water_move (Ambience water movement) bool true

View File

@ -25,7 +25,15 @@ ambience.add_set("underwater", {
end
})
-- Splashing sound plays when player walks inside water nodes
-- Splashing sound plays when player walks inside water nodes (if enabled)
if minetest.settings:get_bool("ambience_water_move") ~= false then
-- 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 = {} })
ambience.add_set("splash", {
@ -49,7 +57,9 @@ ambience.add_set("splash", {
end
})
-- check for env_sounds mod, if not found enable water flowing sounds
end
-- check for env_sounds mod, if not found enable water flowing and lava sounds
if not minetest.get_modpath("env_sounds") then
-- Water sound plays when near flowing water
@ -102,6 +112,32 @@ ambience.add_set("river", {
end
})
-- Lava sound plays when near lava
ambience.add_set("lava", {
frequency = 1000,
sounds = {
{name = "lava", length = 7}
},
nodes = {"default:lava_source", "default:lava_flowing"},
sound_check = function(def)
local c = (def.totals["default:lava_source"] or 0)
+ (def.totals["default:lava_flowing"] or 0)
if c > 20 then
return "lava", 0.5
elseif c > 5 then
return "lava"
end
end
})
else
print ("[Ambience] found env_sounds, flowing water sounds disabled.")
end
@ -170,32 +206,6 @@ ambience.add_set("largefire", {
end
-- Lava sound plays when near lava
ambience.add_set("lava", {
frequency = 1000,
sounds = {
{name = "lava", length = 7}
},
nodes = {"default:lava_source", "default:lava_flowing"},
sound_check = function(def)
local c = (def.totals["default:lava_source"] or 0)
+ (def.totals["default:lava_flowing"] or 0)
if c > 20 then
return "lava", 0.5
elseif c > 5 then
return "lava"
end
end
})
-- Beach sounds play when below y-pos 6 and 150+ water source found
ambience.add_set("beach", {