add sound to torchspines

This commit is contained in:
FaceDeer 2022-08-16 00:03:43 -06:00
parent 5dbf0248e3
commit f47db1e0c2
9 changed files with 151 additions and 7 deletions

View File

@ -97,7 +97,7 @@ df_ambience.add_set({
end, end,
}) })
-- blackcap left silent -- blackcap left silent, torchspines make their own noise
-- Sunless sea -- Sunless sea

View File

@ -1,4 +1,4 @@
name = df_trees name = df_trees
description = Adds various types of underground fungal "trees". Light kills their saplings, they only grow in the dark. description = Adds various types of underground fungal "trees". Light kills their saplings, they only grow in the dark.
depends = df_dependencies depends = df_dependencies
optional_depends = doc, basic_materials, df_farming, mapgen_helper optional_depends = doc, basic_materials, df_farming, mapgen_helper, looped_node_sound

Binary file not shown.

Binary file not shown.

View File

@ -1,2 +1,5 @@
dfcaverns_fungus_footstep are from https://freesound.org/people/jakeh111/sounds/60853/ under CC-BY-SA 3.0 in 2008 by jakeh111 dfcaverns_fungus_footstep are from https://freesound.org/people/jakeh111/sounds/60853/ under CC-BY-SA 3.0 in 2008 by jakeh111
dfcaverns_spore_tree_pitter_patter are from https://freesound.org/people/martinimeniscus/sounds/199332/ by martinimeniscus under the CC0 public domain license dfcaverns_spore_tree_pitter_patter are from https://freesound.org/people/martinimeniscus/sounds/199332/ by martinimeniscus under the CC0 public domain license
dfcaverns_torchspine_loop is from https://freesound.org/people/PegasusCZ/sounds/569332/ by PegasusCZ under the CC0 public domain license
dfcaverns_torchspine_ignite is from https://freesound.org/people/deleted_user_4772965/sounds/256510/ by Patrick Hunt5 under the CC0 public domain license

View File

@ -3,6 +3,7 @@ local S = minetest.get_translator(minetest.get_current_modname())
local torchspine_min_delay = df_trees.config.blood_thorn_delay_multiplier*df_trees.config.tree_min_growth_delay local torchspine_min_delay = df_trees.config.blood_thorn_delay_multiplier*df_trees.config.tree_min_growth_delay
local torchspine_max_delay = df_trees.config.blood_thorn_delay_multiplier*df_trees.config.tree_max_growth_delay local torchspine_max_delay = df_trees.config.blood_thorn_delay_multiplier*df_trees.config.tree_max_growth_delay
local looped_node_sound_modpath = minetest.get_modpath("looped_node_sound")
-- Rather than make this whole mod depend on subterrane just for this, I copied and pasted a chunk of stalactite code. -- Rather than make this whole mod depend on subterrane just for this, I copied and pasted a chunk of stalactite code.
local x_disp = 0.125 local x_disp = 0.125
@ -130,6 +131,7 @@ minetest.register_node("df_trees:torchspine_1", {
local above_def = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name] local above_def = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name]
if above_def and above_def.buildable_to then if above_def and above_def.buildable_to then
minetest.swap_node(pos, {name="df_trees:torchspine_1_lit", param2=minetest.get_node(pos).param2}) minetest.swap_node(pos, {name="df_trees:torchspine_1_lit", param2=minetest.get_node(pos).param2})
minetest.sound_play({pos = pos}, {name="dfcaverns_torchspine_ignite"}, true)
end end
minetest.get_node_timer(pos):start(math.random(torchspine_min_delay, torchspine_max_delay)) minetest.get_node_timer(pos):start(math.random(torchspine_min_delay, torchspine_max_delay))
end, end,
@ -169,6 +171,15 @@ minetest.register_node("df_trees:torchspine_1_lit", {
end, end,
}) })
if looped_node_sound_modpath then
looped_node_sound.register({
node_list = {"df_trees:torchspine_1_lit"},
sound = "dfcaverns_torchspine_loop",
max_gain = 0.5,
gain_per_node = 0.05,
})
end
minetest.register_node("df_trees:torchspine_2", { minetest.register_node("df_trees:torchspine_2", {
description = S("Torchspine"), description = S("Torchspine"),
_doc_items_longdesc = df_trees.doc.torchspine_desc, _doc_items_longdesc = df_trees.doc.torchspine_desc,
@ -299,6 +310,7 @@ minetest.register_node("df_trees:torchspine_ember", {
on_timer = function(pos) on_timer = function(pos)
minetest.swap_node(pos, {name="df_trees:torchspine_1", param2=minetest.get_node(pos).param2}) minetest.swap_node(pos, {name="df_trees:torchspine_1", param2=minetest.get_node(pos).param2})
minetest.get_node_timer(pos):start(math.random(torchspine_min_delay, torchspine_max_delay)) minetest.get_node_timer(pos):start(math.random(torchspine_min_delay, torchspine_max_delay))
minetest.sound_play({pos = pos}, {name="dfcaverns_torchspine_ignite"}, true)
end, end,
}) })
@ -359,10 +371,8 @@ df_trees.spawn_torchspine_vm = function(vi, area, data, data_param2, height, lit
local pos = area:position(vi) local pos = area:position(vi)
pos.y = pos.y+height-1 pos.y = pos.y+height-1
minetest.after(10, function()
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
minetest.get_node_timer(pos):start(math.random()*3000) minetest.get_node_timer(pos):start(math.random()*3000)
end)
end end
minetest.register_lbm({ minetest.register_lbm({

111
looped_node_sound/init.lua Normal file
View File

@ -0,0 +1,111 @@
looped_node_sound = {}
looped_node_sound.register = function(def)
local handles = {}
local timer = 0
-- Parameters
local radius = def.radius or 8 -- node search radius around player
local cycle = def.cycle_time or 3 -- Cycle time for sound updates
assert(def.node_list, "[looped_sound_node] register function called without a node_list in its definition")
assert(def.sound, "[looped_sound_node] register function called without a sound in its definition")
local node_list = def.node_list
local sound = def.sound
local gain_per_node = def.gain_per_node or 0.125
local gain_max = def.max_gain or 1.0
local max_hear_distance = def.max_hear_distance or 32
-- Update sound for player
function update_player_sound(player)
local player_name = player:get_player_name()
-- Search for nodes in radius around player
local ppos = player:get_pos()
local areamin = vector.subtract(ppos, radius)
local areamax = vector.add(ppos, radius)
local fpos, num = minetest.find_nodes_in_area(
areamin,
areamax,
node_list
)
-- Total number of nodes in radius
local total = 0
for _, count in pairs(num) do
total = total + count
end
-- Stop previous sound
if handles[player_name] then
minetest.sound_stop(handles[player_name])
end
-- If nodes
if total > 0 then
-- Find centre of node positions
local fposmid = fpos[1]
-- If more than 1 node
if #fpos > 1 then
local fposmin = areamax
local fposmax = areamin
for i = 1, #fpos do
local fposi = fpos[i]
if fposi.x > fposmax.x then
fposmax.x = fposi.x
end
if fposi.y > fposmax.y then
fposmax.y = fposi.y
end
if fposi.z > fposmax.z then
fposmax.z = fposi.z
end
if fposi.x < fposmin.x then
fposmin.x = fposi.x
end
if fposi.y < fposmin.y then
fposmin.y = fposi.y
end
if fposi.z < fposmin.z then
fposmin.z = fposi.z
end
end
fposmid = vector.divide(vector.add(fposmin, fposmax), 2)
end
-- Play sound
local handle = minetest.sound_play(sound, {
pos = fposmid,
to_player = player_name,
gain = math.min(total * gain_per_node, gain_max),
max_hear_distance = max_hear_distance,
loop = true -- In case of lag
})
-- Store sound handle for this player
if handle then
handles[player_name] = handle
else
handles[player_name] = nil
end
else
handles[player_name] = nil
end
end
-- Cycle for updating players sounds
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < cycle then
return
end
timer = 0
local players = minetest.get_connected_players()
for n = 1, #players do
update_player_sound(players[n])
end
end)
-- Stop sound and clear handle on player leave
minetest.register_on_leaveplayer(function(player)
local player_name = player:get_player_name()
if handles[player_name] then
minetest.sound_stop(handles[player_name])
handles[player_name] = nil
end
end)
end

View File

@ -0,0 +1,16 @@
License of source code
----------------------
GNU Lesser General Public License, version 2.1
Copyright (C) 2012-2016 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2012-2016 Various Minetest developers and contributors
Copyright (C) 2022 FaceDeer, pulled code out of the default game fire mod and made generic
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation;
either version 2.1 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details:
https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html

View File

@ -0,0 +1,4 @@
name = looped_node_sound
description = A simple API for causing nodes to appear to be emitting a looped sound
depends =
optional_depends =