From 3a4bfdb258bec16292764306fb540dd53f8fed44 Mon Sep 17 00:00:00 2001 From: Aaron Suen Date: Mon, 2 Sep 2019 11:34:14 -0400 Subject: [PATCH] One last refactor to ambient sounds. Make tree sounds managed by the mod that defines the nodes in question using register_ambient, consistent with other things like fluids. nc_envsounds is now responsible for just the air moving and cave dripping sounds made by air itself. Common windiness logic moved into api layer. --- mods/nc_api/util_sound.lua | 10 +++++-- mods/nc_envsound/init.lua | 28 ++---------------- mods/nc_tree/ambiance.lua | 20 +++++++++++++ mods/nc_tree/init.lua | 2 ++ .../sounds/nc_tree_breeze.0.ogg} | Bin .../sounds/nc_tree_breeze.1.ogg} | Bin .../sounds/nc_tree_breeze.2.ogg} | Bin 7 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 mods/nc_tree/ambiance.lua rename mods/{nc_envsound/sounds/nc_envsound_tree.0.ogg => nc_tree/sounds/nc_tree_breeze.0.ogg} (100%) rename mods/{nc_envsound/sounds/nc_envsound_tree.1.ogg => nc_tree/sounds/nc_tree_breeze.1.ogg} (100%) rename mods/{nc_envsound/sounds/nc_envsound_tree.2.ogg => nc_tree/sounds/nc_tree_breeze.2.ogg} (100%) diff --git a/mods/nc_api/util_sound.lua b/mods/nc_api/util_sound.lua index 2522e343..87d7bf41 100644 --- a/mods/nc_api/util_sound.lua +++ b/mods/nc_api/util_sound.lua @@ -3,8 +3,8 @@ local ItemStack, ipairs, math, minetest, nodecore, pairs, type, unpack, vector = ItemStack, ipairs, math, minetest, nodecore, pairs, type, unpack, vector -local math_exp, math_random - = math.exp, math.random +local math_exp, math_random, math_sin, math_sqrt + = math.exp, math.random, math.sin, math.sqrt -- LUALOCALS > --------------------------------------------------------- local oldplay = minetest.sound_play @@ -15,6 +15,12 @@ function minetest.sound_play(name, spec, ...) return oldplay(name, spec, ...) end +function nodecore.windiness(y) + if y < 0 then return 0 end + if y > 512 then y = 512 end + return math_sqrt(y) * (1 + 0.5 * math_sin(minetest.get_gametime() / 5)) +end + function nodecore.stack_sounds(pos, kind, stack) stack = stack or nodecore.stack_get(pos) stack = ItemStack(stack) diff --git a/mods/nc_envsound/init.lua b/mods/nc_envsound/init.lua index 505b8cd8..da06878c 100644 --- a/mods/nc_envsound/init.lua +++ b/mods/nc_envsound/init.lua @@ -1,32 +1,10 @@ -- LUALOCALS < --------------------------------------------------------- local math, minetest, nodecore, pairs, vector = math, minetest, nodecore, pairs, vector -local math_exp, math_random, math_sin, math_sqrt - = math.exp, math.random, math.sin, math.sqrt +local math_exp, math_random + = math.exp, math.random -- LUALOCALS > --------------------------------------------------------- -local function windiness(y) - if y < 0 then return 0 end - if y > 512 then y = 512 end - return math_sqrt(y) * (1 + 0.5 * math_sin(minetest.get_gametime() / 5)) -end - -nodecore.register_ambiance({ - label = "Tree Leaves Ambiance", - nodenames = {"nc_tree:leaves"}, - neigbors = {"air"}, - interval = 1, - chance = 100, - sound_name = "nc_envsound_tree", - check = function(pos) - pos.y = pos.y + 1 - if pos.y <= 0 then return end - return minetest.get_node(pos).name == "air" - and minetest.get_node_light(pos, 0.5) == 15 - and { gain = windiness(pos.y) / 20 } - end - }) - local function check(pos, done) local sp = { x = pos.x + math_random() * 64 - 32, @@ -46,7 +24,7 @@ local function check(pos, done) if sp.y <= 0 then return end minetest.sound_play("nc_envsound_air", { pos = sp, - gain = windiness(sp.y) / 100 + gain = nodecore.windiness(sp.y) / 100 }) elseif light < 4 then minetest.sound_play("nc_envsound_drip", { diff --git a/mods/nc_tree/ambiance.lua b/mods/nc_tree/ambiance.lua new file mode 100644 index 00000000..05af3905 --- /dev/null +++ b/mods/nc_tree/ambiance.lua @@ -0,0 +1,20 @@ +-- LUALOCALS < --------------------------------------------------------- +local minetest, nodecore + = minetest, nodecore +-- LUALOCALS > --------------------------------------------------------- + +nodecore.register_ambiance({ + label = "Tree Leaves Ambiance", + nodenames = {"nc_tree:leaves"}, + neigbors = {"air"}, + interval = 1, + chance = 100, + sound_name = "nc_tree_breeze", + check = function(pos) + pos.y = pos.y + 1 + if pos.y <= 0 then return end + return minetest.get_node(pos).name == "air" + and minetest.get_node_light(pos, 0.5) == 15 + and { gain = nodecore.windiness(pos.y) / 20 } + end + }) diff --git a/mods/nc_tree/init.lua b/mods/nc_tree/init.lua index 5a74e26c..4ec5a5ea 100644 --- a/mods/nc_tree/init.lua +++ b/mods/nc_tree/init.lua @@ -12,3 +12,5 @@ include("stick") include("schematic") include("decor") include("cultivation") + +include("ambiance") diff --git a/mods/nc_envsound/sounds/nc_envsound_tree.0.ogg b/mods/nc_tree/sounds/nc_tree_breeze.0.ogg similarity index 100% rename from mods/nc_envsound/sounds/nc_envsound_tree.0.ogg rename to mods/nc_tree/sounds/nc_tree_breeze.0.ogg diff --git a/mods/nc_envsound/sounds/nc_envsound_tree.1.ogg b/mods/nc_tree/sounds/nc_tree_breeze.1.ogg similarity index 100% rename from mods/nc_envsound/sounds/nc_envsound_tree.1.ogg rename to mods/nc_tree/sounds/nc_tree_breeze.1.ogg diff --git a/mods/nc_envsound/sounds/nc_envsound_tree.2.ogg b/mods/nc_tree/sounds/nc_tree_breeze.2.ogg similarity index 100% rename from mods/nc_envsound/sounds/nc_envsound_tree.2.ogg rename to mods/nc_tree/sounds/nc_tree_breeze.2.ogg