Changed routines

This commit is contained in:
tenplus1 2015-02-16 15:46:59 +00:00
parent b1fb60e2c2
commit 351055fc5c
2 changed files with 75 additions and 155 deletions

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# ambience
Ambience Lite mod for Minetest
Based on Immersive Sounds .36 mod by Neuromancer and optimized to run on servers with new fire sounds added when Fire Redo mod is detected...

226
init.lua
View File

@ -1,12 +1,11 @@
--= Ambience lite by TenPlus1 (4th Feb 2015) --= Ambience lite by TenPlus1 (16th 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 = {
@ -82,13 +81,6 @@ local largefire = {
{name="fire_large", length=8} {name="fire_large", length=8}
} }
-- find how many nodes in range
local nodes_in_range = function(pos, search_distance, node_name)
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}, node_name)
return #nodes
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)
@ -96,15 +88,12 @@ local get_ambience = function(player)
local pos = player:getpos() local pos = player:getpos()
-- what is around me? -- what is around me?
pos.y = pos.y - 0.1 -- standing on pos.y = pos.y + 1.4 -- head level
local nod_stand = minetest.get_node(pos).name
pos.y = pos.y + 1.5 -- head level
local nod_head = minetest.get_node(pos).name local nod_head = minetest.get_node(pos).name
pos.y = pos.y - 1.2 -- feet level pos.y = pos.y - 1.2 -- feet level
local nod_feet = minetest.get_node(pos).name local nod_feet = minetest.get_node(pos).name
pos.y = pos.y - 0.2 -- reset pos pos.y = pos.y - 0.2 -- reset pos
--= START Ambiance --= START Ambiance
@ -119,46 +108,69 @@ local get_ambience = function(player)
return {splash=splash} return {splash=splash}
end end
if minetest.get_modpath("fire") then -- this may seem messy but is faster than checking each area separately for specific nodes
if fire.mod == "redo" then local num_fire = 0
local num_lava = 0
local num_water_source = 0
local num_water_flowing = 0
local num_desert = 0
tempy = nodes_in_range(pos, 6, {"fire:basic_flame", "bakedclay:safe_fire"}) -- get block of nodes we need to check
if tempy > 8 then local tempy = minetest.find_nodes_in_area( {x=pos.x-6,y=pos.y-3, z=pos.z-6},
{x=pos.x+6,y=pos.y+3, z=pos.z+6},
{"fire:basic_flame", "bakedclay:safe_fire", "default:lava_flowing", "default:lava_source",
"default:water_flowing", "default:water_source", "default:desert_sand", "default:desert_stone"})
-- count separate instances in block
for _, npos in ipairs(tempy) do
local node = minetest.get_node(npos).name
if node == "fire:basic_flame" or node == "bakedclay:safe_fire" then num_fire = num_fire + 1 end
if node == "default:lava_flowing" or node == "default:lava_source" then num_lava = num_lava + 1 end
if node == "default:water_flowing" then num_water_flowing = num_water_flowing + 1 end
if node == "default:water_source" then num_water_source = num_water_source + 1 end
if node == "default:desert_sand" or node == "default:desert_stone" then num_desert = num_desert + 1 end
end
-- END messy
--print ("fire:"..num_fire.." , lava:"..num_lava.." , watflow:"..num_water_flowing.." , watsrc:"..num_water_source.." , desert:"..num_desert)
-- is fire redo mod active?
if fire.mod and fire.mod == "redo" then
if num_fire > 8 then
return {largefire=largefire} return {largefire=largefire}
elseif tempy > 0 then elseif num_fire > 0 then
return {smallfire=smallfire} return {smallfire=smallfire}
end end
end
end end
if nodes_in_range(pos, 5, {"default:lava_flowing", "default:lava_source"}) > 5 then if num_lava > 5 then
return {lava=lava} return {lava=lava}
end end
if nodes_in_range(pos, 5, "default:water_flowing") > 45 then if num_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 nodes_in_range(pos, 20, {"default:water_source"}) > 100 then if pos.y < 7 and pos.y > 0 and num_water_source > 100 then
return {beach=beach} return {beach=beach}
end end
if nodes_in_range(pos, 5, {"default:desert_sand", "default:desert_stone", "default:sandstone"}) > 250 then if num_desert > 150 then -- was 250
return {desert=desert} return {desert=desert}
end end
if player:getpos().y < -10 then if player:getpos().y < -10 then
return {cave=cave} return {cave=cave}
end end
if minetest.env:get_timeofday() > 0.2 and minetest.env:get_timeofday() < 0.8 then if minetest.get_timeofday() > 0.2 and minetest.get_timeofday() < 0.8 then
return {day=day} return {day=day}
else else
return {night=night} return {night=night}
end end
-- END Ambiance -- END Ambiance
end end
-- play sound, set handler then delete handler when sound finished -- play sound, set handler then delete handler when sound finished
@ -188,132 +200,35 @@ local play_sound = function(player, list, number)
end end
end end
-- stop all sounds that are not in still_playing -- stop sound in still_playing
local stop_sound = function(still_playing, player) local stop_sound = function (list, player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
if not still_playing.cave then if list.handler[player_name] then
local list = cave if list.on_stop then
if list.handler[player_name] then minetest.sound_play(list.on_stop, {to_player=player:get_player_name(),gain=SOUNDVOLUME})
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
minetest.sound_stop(list.handler[player_name])
list.handler[player_name] = nil
end end
if not still_playing.beach then end
local list = beach
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.desert then -- check sounds that are not in still_playing
local list = desert local still_playing = function(still_playing, player)
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.night then
local list = night
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.day then
local list = day
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.flowing_water then
local list = flowing_water
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.splash then
local list = splash
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.underwater then
local list = underwater
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.lava then
local list = lava
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.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
if not still_playing.cave then stop_sound(cave, player) end
if not still_playing.beach then stop_sound(beach, player) end
if not still_playing.desert then stop_sound(desert, player) end
if not still_playing.night then stop_sound(night, player) end
if not still_playing.day then stop_sound(day, player) end
if not still_playing.flowing_water then stop_sound(flowing_water, player) end
if not still_playing.splash then stop_sound(splash, player) end
if not still_playing.underwater then stop_sound(underwater, player) end
if not still_playing.lava then stop_sound(lava, player) end
if not still_playing.smallfire then stop_sound(smallfire, player) end
if not still_playing.largefire then stop_sound(largefire, player) end
end end
@ -321,15 +236,16 @@ end
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
timer = timer + dtime timer = timer + dtime
-- every 1 second -- every 1 second
if timer < 1 then return end 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
--local t1 = os.clock()
ambiences = get_ambience(player) ambiences = get_ambience(player)
stop_sound(ambiences, player) --print ("[TEST] "..math.ceil((os.clock() - t1) * 1000).." ms")
still_playing(ambiences, player)
for _,ambience in pairs(ambiences) do for _,ambience in pairs(ambiences) do
@ -342,7 +258,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)