1
0
mirror of https://codeberg.org/tenplus1/ambience.git synced 2024-12-24 09:40:30 +01:00

Update: fire sounds added, optimized routines

This commit is contained in:
tenplus1 2015-02-04 18:30:43 +00:00
parent a10f69d5e3
commit b1fb60e2c2
3 changed files with 83 additions and 53 deletions

136
init.lua
View File

@ -1,11 +1,12 @@
--= Ambience lite by TenPlus1 (14th Jan 2015) --= Ambience lite by TenPlus1 (4th Feb 2015)
local max_frequency_all = 1000 -- larger number means more frequent sounds (100-2000) local max_frequency_all = 1000 -- larger number means more frequent sounds (100-2000)
local SOUNDVOLUME = 1 local SOUNDVOLUME = 1
local volume = 0.3 local volume = 0.3
local ambiences local ambiences
local played_on_start = false local played_on_start = false
local tempy = 0
-- sound sets -- sound sets
local night = { local night = {
@ -55,11 +56,11 @@ local flowing_water = {
handler = {}, frequency = 1000, handler = {}, frequency = 1000,
{name="waterfall", length=6} {name="waterfall", length=6}
} }
local underwater = { local underwater = {
handler = {}, frequency = 1000, handler = {}, frequency = 1000,
{name="scuba", length=8} {name="scuba", length=8}
} }
local splash = { local splash = {
handler = {}, frequency = 1000, handler = {}, frequency = 1000,
@ -71,79 +72,79 @@ local lava = {
{name="lava", length=7} {name="lava", length=7}
} }
local smallfire = {
handler = {}, frequency = 1000,
{name="fire_small", length=6}
}
local largefire = {
handler = {}, frequency = 1000,
{name="fire_large", length=8}
}
-- find how many nodes in range -- find how many nodes in range
local nodes_in_range = function(pos, search_distance, node_name) local nodes_in_range = function(pos, search_distance, node_name)
nodes = minetest.env:find_nodes_in_area({x=pos.x-search_distance,y=pos.y-search_distance, z=pos.z-search_distance}, local nodes = minetest.env:find_nodes_in_area({x=pos.x-search_distance,y=pos.y-search_distance, z=pos.z-search_distance},
{x=pos.x+search_distance,y=pos.y+search_distance, z=pos.z+search_distance}, {x=pos.x+search_distance,y=pos.y+search_distance, z=pos.z+search_distance}, node_name)
node_name)
return #nodes return #nodes
end end
local nodes_in_coords = function(minp, maxp, node_name)
nodes = minetest.env:find_nodes_in_area(minp, maxp, node_name)
return #nodes
end
-- find at least (threshold) nodes in area
local atleast_nodes_in_grid = function(pos, distance, height, node, threshold)
nodes = minetest.env:find_nodes_in_area({x=pos.x-distance,y=height, z=pos.z+20},
{x=pos.x+distance,y=height, z=pos.z+20}, node)
totalnodes = #nodes
if totalnodes >= threshold then return true end
nodes = minetest.env:find_nodes_in_area({x=pos.x-distance,y=height, z=pos.z-20},
{x=pos.x+distance,y=height, z=pos.z-20}, node)
totalnodes = totalnodes + #nodes
if totalnodes >= threshold then return true end
nodes = minetest.env:find_nodes_in_area({x=pos.x+20,y=height, z=pos.z+distance},
{x=pos.x+20,y=height, z=pos.z-distance}, node)
totalnodes = totalnodes + #nodes
if totalnodes >= threshold then return true end
nodes = minetest.env:find_nodes_in_area({x=pos.x-20,y=height, z=pos.z-distance},
{x=pos.x-20,y=height, z=pos.z+distance}, node)
totalnodes = totalnodes + #nodes
if totalnodes >= threshold then return true end
return false
end
-- check where player is and which sounds are played -- check where player is and which sounds are played
local get_ambience = function(player) local get_ambience = function(player)
-- where am I?
local pos = player:getpos() local pos = player:getpos()
pos.y = pos.y + 1.4 -- head level -- what is around me?
pos.y = pos.y - 0.1 -- standing on
local nod_stand = minetest.get_node(pos).name
if minetest.get_node(pos).name == "default:water_source" pos.y = pos.y + 1.5 -- head level
or minetest.get_node(pos).name == "default:water_flowing" then local nod_head = minetest.get_node(pos).name
pos.y = pos.y - 1.2 -- feet level
local nod_feet = minetest.get_node(pos).name
pos.y = pos.y - 0.2 -- reset pos
--= START Ambiance
if nod_head == "default:water_source"
or nod_head == "default:water_flowing" then
return {underwater=underwater} return {underwater=underwater}
end end
pos.y = pos.y - 1.2 -- feet level if nod_feet == "default:water_source"
or nod_feet == "default:water_flowing" then
if minetest.get_node(pos).name == "default:water_source"
or minetest.get_node(pos).name == "default:water_flowing" then
return {splash=splash} return {splash=splash}
end end
pos.y=pos.y - 0.2 if minetest.get_modpath("fire") then
if fire.mod == "redo" then
if nodes_in_range(pos, 7, {"default:lava_flowing", "default:lava_source"}) > 5 then tempy = nodes_in_range(pos, 6, {"fire:basic_flame", "bakedclay:safe_fire"})
if tempy > 8 then
return {largefire=largefire}
elseif tempy > 0 then
return {smallfire=smallfire}
end
end
end
if nodes_in_range(pos, 5, {"default:lava_flowing", "default:lava_source"}) > 5 then
return {lava=lava} return {lava=lava}
end end
if nodes_in_range(pos, 6, "default:water_flowing") > 45 then if nodes_in_range(pos, 5, "default:water_flowing") > 45 then
return {flowing_water=flowing_water} return {flowing_water=flowing_water}
end end
if pos.y < 7 and pos.y > 0 and atleast_nodes_in_grid(pos, 60, 1, "default:water_source", 51 ) then if pos.y < 7 and pos.y > 0 and nodes_in_range(pos, 20, {"default:water_source"}) > 100 then
return {beach=beach} return {beach=beach}
end end
if nodes_in_range(pos, 6, {"default:desert_sand", "default:desert_stone", "default:sandstone"}) > 250 then if nodes_in_range(pos, 5, {"default:desert_sand", "default:desert_stone", "default:sandstone"}) > 250 then
return {desert=desert} return {desert=desert}
end end
@ -156,6 +157,8 @@ local get_ambience = function(player)
else else
return {night=night} return {night=night}
end end
-- END Ambiance
end end
-- play sound, set handler then delete handler when sound finished -- play sound, set handler then delete handler when sound finished
@ -266,7 +269,7 @@ local stop_sound = function(still_playing, player)
list.handler[player_name] = nil list.handler[player_name] = nil
end end
end end
if not still_playing.underwater then if not still_playing.underwater then
local list = underwater local list = underwater
if list.handler[player_name] then if list.handler[player_name] then
@ -276,7 +279,7 @@ local stop_sound = function(still_playing, player)
minetest.sound_stop(list.handler[player_name]) minetest.sound_stop(list.handler[player_name])
list.handler[player_name] = nil list.handler[player_name] = nil
end end
end end
if not still_playing.lava then if not still_playing.lava then
local list = lava local list = lava
@ -288,6 +291,29 @@ local stop_sound = function(still_playing, player)
list.handler[player_name] = nil list.handler[player_name] = nil
end end
end end
if not still_playing.smallfire then
local list = smallfire
if list.handler[player_name] then
if list.on_stop then
minetest.sound_play(list.on_stop, {to_player=player:get_player_name(),gain=SOUNDVOLUME})
end
minetest.sound_stop(list.handler[player_name])
list.handler[player_name] = nil
end
end
if not still_playing.largefire then
local list = largefire
if list.handler[player_name] then
if list.on_stop then
minetest.sound_play(list.on_stop, {to_player=player:get_player_name(),gain=SOUNDVOLUME})
end
minetest.sound_stop(list.handler[player_name])
list.handler[player_name] = nil
end
end
end end
@ -295,10 +321,13 @@ end
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
timer = timer + dtime timer = timer + dtime
if timer < 2 then return end
-- every 1 second
if timer < 1 then return end
timer = 0 timer = 0
for _,player in ipairs(minetest.get_connected_players()) do for _,player in ipairs(minetest.get_connected_players()) do
ambiences = get_ambience(player) ambiences = get_ambience(player)
stop_sound(ambiences, player) stop_sound(ambiences, player)
@ -313,6 +342,7 @@ minetest.register_globalstep(function(dtime)
play_sound(player, ambience, math.random(1, #ambience)) play_sound(player, ambience, math.random(1, #ambience))
end end
end end
end end
end) end)

BIN
sounds/fire_large.ogg Normal file

Binary file not shown.

BIN
sounds/fire_small.ogg Normal file

Binary file not shown.