1
0
mirror of https://codeberg.org/tenplus1/ambience.git synced 2024-12-23 17:20:30 +01:00

tweak & tidy code, re-order some sound sets

This commit is contained in:
tenplus1 2020-06-14 13:20:55 +01:00
parent 2b143286b7
commit c5f4d6b9ec
2 changed files with 200 additions and 122 deletions

View File

@ -20,6 +20,8 @@ local set_nodes = {} -- all the nodes needed for sets
-- global functions
-- add set to list
ambience.add_set = function(set_name, def)
if not set_name or not def then
@ -69,6 +71,7 @@ ambience.add_set = function(set_name, def)
end
-- return set from list using name
ambience.get_set = function(set_name)
if sound_sets[set_name] then
@ -77,6 +80,7 @@ ambience.get_set = function(set_name)
end
-- remove set from list
ambience.del_set = function(set_name)
sound_sets[set_name] = nil
@ -138,13 +142,14 @@ 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}, set_nodes)
-- loop through sets in order and choose one that meets it's conditions
-- loop through sets in order and choose first that meets it's conditions
for n = 1, #sound_set_order do
local set = sound_sets[ sound_set_order[n] ]
if set and set.sound_check then
-- pass settings to function for condition check
local set_name, gain = set.sound_check({
player = player,
pos = pos,
@ -155,6 +160,7 @@ local get_ambience = function(player, tod, name)
feet_node = nod_feet
})
-- if conditions met return set name and gain value
if set_name then
return set_name, gain
end
@ -166,18 +172,21 @@ end
local timer = 0
local random = math.random
-- player routine
-- players routine
minetest.register_globalstep(function(dtime)
-- every 1 second
-- one second timer
timer = timer + 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
local tod = minetest.get_timeofday()
local ok = true
-- loop through players
for n = 1, #players do
player_name = players[n]:get_player_name()
@ -188,17 +197,20 @@ minetest.register_globalstep(function(dtime)
--print(string.format("elapsed time: %.4f\n", os.clock() - t1))
local ok = true
ok = true -- everything starts off ok
-- check set exists
if set_name ~= nil and not sound_sets[set_name] then
print("[ambience] sound set doesn't exist:", set_name)
ok = false
ok = false -- don't continue, set missing
end
-- are we ok and have a set name
if ok and set_name then
-- stop sound if another set active
-- stop current sound if another set active or gain changed
if playing[player_name]
and playing[player_name].handler then
@ -212,42 +224,47 @@ local ok = true
playing[player_name].handler = nil
playing[player_name].gain = nil
else
-- return
ok = false
ok = false -- sound set still playing, skip new sound
end
end
if ok then
-- choose random sound from set selected
number = random(1, #sound_sets[set_name].sounds)
ambience = sound_sets[set_name].sounds[number]
math.randomseed(tod + number)
-- set random chance and reset seed
chance = random(1, 1000)
if chance < sound_sets[set_name].frequency then
math.randomseed(tod + chance)
-- if chance is lower than set frequency then select set
if ok and chance < sound_sets[set_name].frequency then
-- choose random sound from set selected
number = random(1, #sound_sets[set_name].sounds)
ambience = sound_sets[set_name].sounds[number]
-- play sound
handler = minetest.sound_play(ambience.name, {
to_player = player_name,
gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * SOUNDVOLUME
})
-- only continue if sound is playing
if handler then
--print ("playing... " .. ambience.name .. " (" .. chance .. " < "
-- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN)
if handler then
-- set what player is currently listening to
playing[player_name] = playing[player_name] or {}
playing[player_name].handler = handler
playing[player_name].sound = set_name
playing[player_name].gain = MORE_GAIN
playing[player_name].old_handler = handler
-- set timer to stop sound
minetest.after(ambience.length, function(args)
local player_name = args[2]
-- make sure we are stopping same sound we started
if playing[player_name]
and playing[player_name].handler
and playing[player_name].sound == set_name
@ -255,6 +272,7 @@ if ok then
--print("-- timed stop")
minetest.sound_stop(playing[player_name].handler)
-- reset player variables
playing[player_name].sound = nil
playing[player_name].handler = nil
playing[player_name].gain = nil
@ -264,15 +282,12 @@ if ok then
end, {ambience, player_name})
end
end
end -- end ok
end
end
end)
-- set volume commands
-- sound volume command
minetest.register_chatcommand("svol", {
params = "<svol>",
description = "set sound volume (0.1 to 1.0)",
@ -290,6 +305,7 @@ minetest.register_chatcommand("svol", {
})
-- music volume command (0 stops music)
minetest.register_chatcommand("mvol", {
params = "<mvol>",
description = "set music volume (0.1 to 1.0)",

View File

@ -9,26 +9,32 @@
-- Underwater sounds play when player head is submerged
ambience.add_set("underwater", {
frequency = 1000,
sounds = {
{name = "scuba", length = 8}
},
sound_check = function(def)
if minetest.registered_nodes[def.head_node]
and minetest.registered_nodes[def.head_node].groups.water then
return "underwater"
end
end,
end
})
-- Splashing sound plays when player walks inside water nodes
ambience.add_set("splash", {
frequency = 1000,
sounds = {
{name = "swim_splashing", length = 3},
},
sound_check = function(def)
if minetest.registered_nodes[def.feet_node]
@ -40,49 +46,62 @@ ambience.add_set("splash", {
return "splash"
end
end
end,
end
})
-- check for env_sounds mod, if not found enable water flowing sounds
if not minetest.get_modpath("env_sounds") then
-- Water sound plays when near flowing water, will get louder if more than 50
-- Water sound plays when near flowing water
ambience.add_set("flowing_water", {
frequency = 1000,
sounds = {
{name = "waterfall", length = 6}
},
nodes = {"default:water_flowing"},
sound_check = function(def)
local c = (def.totals["default:water_flowing"] or 0)
if c > 5 then
return "flowing_water", math.min(0.04 + c * 0.004, 0.6)
end
if c > 40 then
return "flowing_water", 0.5
end,
nodes = {"default:water_flowing"}
elseif c > 5 then
return "flowing_water"
end
end
})
-- River sound plays when near flowing river, will get louder if more than 20
-- River sound plays when near flowing river
ambience.add_set("river", {
frequency = 1000,
sounds = {
{name = "river", length = 4, gain = 0.1}
},
nodes = {"default:river_water_flowing"},
sound_check = function(def)
local c = (def.totals["default:river_water_flowing"] or 0)
if c > 5 then
return "river", math.min(0.04 + c * 0.004, 0.5)
end
if c > 20 then
return "river", 0.5
end,
nodes = {"default:river_water_flowing"}
elseif c > 5 then
return "river"
end
end
})
else
print ("[Ambience] found env_sounds, flowing water sounds disabled.")
end
@ -98,105 +117,124 @@ if flame_sound and not fire_redo then
else
-- Small fire sound plays when near flame, will get louder if more than 3
-- Small fire sound plays when near lower than 9 flames
ambience.add_set("smallfire", {
frequency = 1000,
sounds = {
{name = "fire_small", length = 6, gain = 0.1}
},
nodes = {"fire:basic_flame", "fire:permanent_flame"},
sound_check = function(def)
local c = (def.totals["fire:basic_flame"] or 0)
+ (def.totals["fire:permanent_flame"] or 0)
if c > 3 and c < 9 then
return "smallfire", 0.2
if c > 5 and c < 9 then
return "smallfire", 0.5
elseif c > 0 and c < 4 then
elseif c > 0 and c < 9 then
return "smallfire"
end
end,
nodes = {"fire:basic_flame", "fire:permanent_flame"}
end
})
-- Large fire sound plays when near flames, will get louder if more than 16
-- Large fire sound plays when near more than 9 flames
ambience.add_set("largefire", {
frequency = 1000,
sounds = {
{name = "fire_large", length = 8, gain = 0.4}
},
sound_check = function(def)
-- fire nodes were added in last set, so don't need to be added in this one
local c = (def.totals["fire:basic_flame"] or 0)
+ (def.totals["fire:permanent_flame"] or 0)
if c > 16 then
return "largefire", 0.4
if c > 20 then
return "largefire", 0.5
elseif c > 8 then
return "largefire"
end
end,
nodes = {"fire:basic_flame", "fire:permanent_flame"}
end
})
end
-- Lava sound plays when near lava, will get louder if more than 50
-- 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 > 50 then
if c > 20 then
return "lava", 0.5
elseif c > 5 then
return "lava"
end
end,
nodes = {"default:lava_source", "default:lava_flowing"}
end
})
-- Beach sounds play when around 0-7 player Y position and 150+ water source found
-- Beach sounds play when below y-pos 6 and 150+ water source found
ambience.add_set("beach", {
frequency = 40,
sounds = {
{name = "seagull", length = 4.5},
{name = "beach", length = 13},
{name = "gull", length = 1},
{name = "beach_2", length = 6},
{name = "beach_2", length = 6}
},
nodes = {"default:water_source"},
sound_check = function(def)
local c = (def.totals["default:water_source"] or 0)
if def.pos.y < 7 and def.pos.y > 0
and c > 150 then
if def.pos.y < 6 and def.pos.y > 0 and c > 150 then
return "beach"
end
end,
nodes = {"default:water_source"}
end
})
-- Ice sounds play when 100 or more ice are nearby
ambience.add_set("ice", {
frequency = 250,
sounds = {
{name = "icecrack", length = 23},
{name = "desertwind", length = 8},
{name = "wind", length = 9},
{name = "wind", length = 9}
},
nodes = {"default:ice"},
sound_check = function(def)
local c = (def.totals["default:ice"] or 0)
@ -204,19 +242,23 @@ ambience.add_set("ice", {
if c > 100 then
return "ice"
end
end,
nodes = {"default:ice"},
end
})
-- Desert sounds play when near 150+ desert or normal sand
ambience.add_set("desert", {
frequency = 20,
sounds = {
{name = "coyote", length = 2.5},
{name = "wind", length = 9},
{name = "desertwind", length = 8}
},
nodes = {"default:desert_sand", "default:sand"},
sound_check = function(def)
local c = (def.totals["default:desert_sand"] or 0)
@ -225,57 +267,44 @@ ambience.add_set("desert", {
if c > 150 and def.pos.y > 10 then
return "desert"
end
end,
nodes = {"default:desert_sand", "default:sand"}
})
-- Winds play when player is above 50 Y position and near 150+ snow blocks
ambience.add_set("high_up", {
frequency = 40,
sounds = {
{name = "desertwind", length = 8},
{name = "wind", length = 9},
},
sound_check = function(def)
local c = (def.totals["default:snowblock"] or 0)
if def.pos.y > 50
or c > 150 then
return "high_up"
end
end,
nodes = {"default:snowblock"}
end
})
-- Cave sounds play when below player position Y -25
ambience.add_set("cave", {
frequency = 60,
sounds = {
{name = "drippingwater1", length = 1.5},
{name = "drippingwater2", length = 1.5}
},
sound_check = function(def)
if def.pos.y < -25 then
return "cave"
end
end,
end
})
-- Jungle sounds play during daytime and when around 90 jungletree trunks
-- Jungle sounds play during day and when around 90 jungletree trunks
ambience.add_set("jungle", {
frequency = 200,
sounds = {
{name = "jungle_day_1", length = 7},
{name = "deer", length = 7},
{name = "canadianloon2", length = 14},
{name = "bird1", length = 11},
{name = "peacock", length = 2},
{name = "peacock", length = 2}
},
nodes = {"default:jungletree"},
sound_check = function(def)
local c = (def.totals["default:jungletree"] or 0)
@ -283,62 +312,39 @@ ambience.add_set("jungle", {
if def.tod > 0.2 and def.tod < 0.8 and c > 90 then
return "jungle"
end
end,
nodes = {"default:jungletree"}
end
})
-- Jungle sounds play during nighttime and when around 90 jungletree trunks
-- Jungle sounds play during night and when around 90 jungletree trunks
ambience.add_set("jungle_night", {
frequency = 200,
sounds = {
{name = "jungle_night_1", length = 4},
{name = "jungle_night_2", length = 4},
{name = "deer", length = 7},
{name = "frog", length = 1},
{name = "frog", length = 1}
},
sound_check = function(def)
-- jungle tree was added in last set, so doesnt need to be added in this one
local c = (def.totals["default:jungletree"] or 0)
if (def.tod < 0.2 or def.tod > 0.8) and c > 90 then
return "jungle"
return "jungle_night"
end
end,
nodes = {"default:jungletree"}
})
-- Nighttime sounds play at night when around leaves and above ground
ambience.add_set("night", {
frequency = 40,
sounds = {
{name = "hornedowl", length = 2},
{name = "wolves", length = 4, gain = 0.4},
{name = "cricket", length = 6},
{name = "deer", length = 7},
{name = "frog", length = 1},
},
sound_check = function(def)
local c = (def.totals["default:leaves"] or 0)
+ (def.totals["default:bush_leaves"] or 0)
+ (def.totals["default:pine_needles"] or 0)
+ (def.totals["default:aspen_leaves"] or 0)
if (def.tod < 0.2 or def.tod > 0.8)
and def.pos.y > -10
and c > 5 then
return "night"
end
end,
nodes = {"group:leaves"}
end
})
-- Daytime sounds play during day when around leaves and above ground
ambience.add_set("day", {
frequency = 40,
sounds = {
{name = "cardinal", length = 3},
{name = "craw", length = 3},
@ -348,10 +354,14 @@ ambience.add_set("day", {
{name = "bird2", length = 6},
{name = "crestedlark", length = 6},
{name = "peacock", length = 2},
{name = "wind", length = 9},
{name = "wind", length = 9}
},
nodes = {"group:leaves"},
sound_check = function(def)
-- we used group:leaves but still need to specify actual nodes for total
local c = (def.totals["default:leaves"] or 0)
+ (def.totals["default:bush_leaves"] or 0)
+ (def.totals["default:pine_needles"] or 0)
@ -362,6 +372,58 @@ ambience.add_set("day", {
and c > 5 then
return "day"
end
end,
nodes = {"group:leaves"}
end
})
-- Nighttime sounds play at night when above ground near leaves
ambience.add_set("night", {
frequency = 40,
sounds = {
{name = "hornedowl", length = 2},
{name = "wolves", length = 4, gain = 0.4},
{name = "cricket", length = 6},
{name = "deer", length = 7},
{name = "frog", length = 1}
},
sound_check = function(def)
-- leaves were added in last set, so don't need to be added to this one
local c = (def.totals["default:leaves"] or 0)
+ (def.totals["default:bush_leaves"] or 0)
+ (def.totals["default:pine_needles"] or 0)
+ (def.totals["default:aspen_leaves"] or 0)
if (def.tod < 0.2 or def.tod > 0.8)
and def.pos.y > -10
and c > 5 then
return "night"
end
end
})
-- Winds play when player is above 50 y-pos or near 150+ snow blocks
ambience.add_set("high_up", {
frequency = 40,
sounds = {
{name = "desertwind", length = 8},
{name = "wind", length = 9}
},
nodes = {"default:snowblock"},
sound_check = function(def)
local c = (def.totals["default:snowblock"] or 0)
if def.pos.y > 50 or c > 150 then
return "high_up"
end
end
})