1
0
mirror of https://codeberg.org/tenplus1/ambience.git synced 2024-09-16 17:50:33 +02:00

tweat code

This commit is contained in:
tenplus1 2024-07-11 14:08:26 +01:00
parent 9ad6bac0cb
commit f3f2cedd4b
4 changed files with 36 additions and 57 deletions

View File

@ -80,7 +80,7 @@ Additional Commands
Two volume commands have been added to set sound and music volume: Two volume commands have been added to set sound and music volume:
/svol (0.1 to 1.0) /svol (0.1 to 1.0)
/mvol (0.1 to 1.0) -- 0 can be used to stop music from playing when it begins /mvol (0.1 to 1.0) -- 0 can be used to stop music curently playing
Music Music
@ -88,4 +88,5 @@ Music
Music can be stored in the sounds folder either on server or locally and so long Music can be stored in the sounds folder either on server or locally and so long
as it is named 'ambience_music.1', 'ambience_music.2' etc. then it will select as it is named 'ambience_music.1', 'ambience_music.2' etc. then it will select
a song randomly at midnight and play player. a song randomly to play every 20 minutes.

View File

@ -4,8 +4,8 @@ ambience = {}
-- settings -- settings
local SOUNDVOLUME = 1.0 local SOUNDVOLUME = 1.0
local MUSICVOLUME = 0.6 local MUSICVOLUME = 0.6
local MUSICINTERVAL = 60 * 20
local play_music = minetest.settings:get_bool("ambience_music") ~= false local play_music = minetest.settings:get_bool("ambience_music") ~= false
local pplus = minetest.get_modpath("playerplus")
local radius = 6 local radius = 6
local playing = {} local playing = {}
local sound_sets = {} -- all the sounds and their settings local sound_sets = {} -- all the sounds and their settings
@ -97,20 +97,14 @@ minetest.register_on_joinplayer(function(player)
if player then if player then
local name = player:get_player_name() local name = player:get_player_name()
playing[name] = {music = -1}
local mvol, svol
local meta = player:get_meta() local meta = player:get_meta()
mvol = meta:get_string("ambience.mvol") playing[name] = {
svol = meta:get_string("ambience.svol") mvol = tonumber(meta:get_string("ambience.mvol")) or MUSICVOLUME,
svol = tonumber(meta:get_string("ambience.svol")) or SOUNDVOLUME,
mvol = tonumber(mvol) or MUSICVOLUME music = 0,
svol = tonumber(svol) or SOUNDVOLUME music_handler = nil
}
playing[name].mvol = mvol
playing[name].svol = svol
end end
end) end)
@ -126,33 +120,23 @@ end)
-- plays music and selects sound set -- plays music and selects sound set
local get_ambience = function(player, tod, name) local get_ambience = function(player, tod, name)
-- play server or local music if music enabled and music not already playing -- if enabled and not already playing, play local/server music on interval check
if play_music if play_music and playing[name] and playing[name].mvol > 0 then
and playing[name] and playing[name].music < 0
and playing[name].mvol > 0 then
-- count backwards -- increase music interval
playing[name].music = playing[name].music -1 playing[name].music = playing[name].music + 1
-- play music every 20 minutes -- play music on interval check
if playing[name].music < -(60 * 20) then if playing[name].music > MUSICINTERVAL and playing[name].music_handler == nil then
playing[name].music = minetest.sound_play("ambience_music", { playing[name].music_handler = minetest.sound_play("ambience_music", {
to_player = name, to_player = name,
gain = playing[name].mvol gain = playing[name].mvol
}) })
-- reset music timer after 10 minutes playing[name].music = 0 -- reset interval
minetest.after(60 * 10, function(name)
if playing[name] then
playing[name].music = -1
end
end, name)
end end
--print("-- music timer", playing[name].music .. "/" .. MUSICINTERVAL)
--print("-- music count", playing[name].music)
end end
-- get foot and head level nodes at player position -- get foot and head level nodes at player position
@ -160,15 +144,13 @@ local get_ambience = function(player, tod, name)
local prop = player:get_properties() local prop = player:get_properties()
local eyeh = prop.eye_height or 1.47 -- eye level with fallback local eyeh = prop.eye_height or 1.47 -- eye level with fallback
pos.y = pos.y + eyeh pos.y = pos.y + eyeh -- head level
local nod_head = pplus and name and playerplus[name] local nod_head = minetest.get_node(pos).name
and playerplus[name].nod_head or minetest.get_node(pos).name
pos.y = (pos.y - eyeh) + 0.2 -- foot level pos.y = (pos.y - eyeh) + 0.2 -- foot level
local nod_feet = pplus and name and playerplus[name] local nod_feet = minetest.get_node(pos).name
and playerplus[name].nod_feet or minetest.get_node(pos).name
pos.y = pos.y - 0.2 -- reset pos pos.y = pos.y - 0.2 -- reset pos
@ -177,7 +159,7 @@ local get_ambience = function(player, tod, name)
{x = pos.x - radius, y = pos.y - radius, z = pos.z - radius}, {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) {x = pos.x + radius, y = pos.y + radius, z = pos.z + radius}, set_nodes)
-- loop through sets in order and choose first that meets it's conditions -- loop through sets in order and choose first that meets conditions set
for n = 1, #sound_set_order do for n = 1, #sound_set_order do
local set = sound_sets[ sound_set_order[n] ] local set = sound_sets[ sound_set_order[n] ]
@ -202,7 +184,7 @@ local get_ambience = function(player, tod, name)
end end
end end
return nil, nil -- ADDED return nil, nil
end end
@ -225,12 +207,8 @@ minetest.register_globalstep(function(dtime)
player_name = player:get_player_name() player_name = player:get_player_name()
--local t1 = os.clock()
local set_name, MORE_GAIN = get_ambience(player, tod, player_name) local set_name, MORE_GAIN = get_ambience(player, tod, player_name)
--print(string.format("elapsed time: %.4f\n", os.clock() - t1))
ok = playing[player_name] -- everything starts off ok if player found ok = playing[player_name] -- everything starts off ok if player found
-- are we playing something already? -- are we playing something already?
@ -258,9 +236,8 @@ minetest.register_globalstep(function(dtime)
-- if chance is lower than set frequency then select set -- if chance is lower than set frequency then select set
if ok and set_name and chance < sound_sets[set_name].frequency then if ok and set_name and chance < sound_sets[set_name].frequency then
-- choose random sound from set number = random(#sound_sets[set_name].sounds) -- choose random sound from set
number = random(#sound_sets[set_name].sounds) ambience = sound_sets[set_name].sounds[number] -- grab sound information
ambience = sound_sets[set_name].sounds[number]
-- play sound -- play sound
handler = minetest.sound_play(ambience.name, { handler = minetest.sound_play(ambience.name, {
@ -342,13 +319,14 @@ minetest.register_chatcommand("mvol", {
local mvol = tonumber(param) or playing[name].mvol local mvol = tonumber(param) or playing[name].mvol
-- ability to stop music by setting volume to 0 -- stop music currently playing by setting volume to 0
if mvol == 0 and playing[name].music if mvol == 0 and playing[name].music_handler then
and playing[name].music >= 0 then
minetest.sound_stop(playing[name].music) minetest.sound_stop(playing[name].music_handler)
playing[name].music = -1 playing[name].music_handler = nil
minetest.chat_send_player(name, S("Music stopped!"))
end end
if mvol < 0 then mvol = 0 end if mvol < 0 then mvol = 0 end
@ -371,3 +349,4 @@ dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
print("[MOD] Ambience Lite loaded") print("[MOD] Ambience Lite loaded")

View File

@ -1,5 +1,5 @@
name = ambience name = ambience
description = Adds realistic sound effects into your world. description = Adds realistic sound effects into your world.
depends = default depends = default
optional_depends = fire, playerplus optional_depends = fire
min_minetest_version = 5.0 min_minetest_version = 5.0

View File

@ -139,7 +139,7 @@ ambience.add_set("lava", {
}) })
else else
print ("[MOD] Ambience - found env_sounds, flowing water and lava sounds disabled.") print ("[MOD] Ambience - found env_sounds, using for water and lava sounds.")
end end
-- Only add fire sounds set if flame_sound is disabled or fire redo active -- Only add fire sounds set if flame_sound is disabled or fire redo active
@ -148,9 +148,7 @@ local flame_sound = minetest.settings:get_bool("flame_sound", true)
local fire_redo = minetest.get_modpath("fire") and fire.mod and fire.mod == "redo" local fire_redo = minetest.get_modpath("fire") and fire.mod and fire.mod == "redo"
if flame_sound and not fire_redo then if flame_sound and not fire_redo then
print ("[MOD] Ambience - fire sounds not enabled, already active in fire mod.") print ("[MOD] Ambience - fire sounds not enabled, already active in fire mod.")
else else
-- Small fire sound plays when near lower than 9 flames -- Small fire sound plays when near lower than 9 flames
@ -440,3 +438,4 @@ ambience.add_set("high_up", {
end end
end end
}) })