forked from VoxeLibre/VoxeLibre
Compare commits
No commits in common. "fix-weather-lag" and "master" have entirely different histories.
fix-weathe
...
master
|
@ -1,5 +1,5 @@
|
||||||
local PARTICLES_COUNT_RAIN = 100
|
local PARTICLES_COUNT_RAIN = 30
|
||||||
local PARTICLES_COUNT_THUNDER = 300
|
local PARTICLES_COUNT_THUNDER = 45
|
||||||
|
|
||||||
local get_connected_players = minetest.get_connected_players
|
local get_connected_players = minetest.get_connected_players
|
||||||
|
|
||||||
|
@ -19,45 +19,6 @@ mcl_weather.rain = {
|
||||||
|
|
||||||
init_done = false,
|
init_done = false,
|
||||||
}
|
}
|
||||||
local update_sound={}
|
|
||||||
local vel=math.random(0,3)
|
|
||||||
local falling_speed=math.random(10,15)
|
|
||||||
local size = math.random(1,3)
|
|
||||||
local psdef= {
|
|
||||||
amount = mcl_weather.rain.particles_count,
|
|
||||||
time=0,
|
|
||||||
minpos = vector.new(-6,3,-6),
|
|
||||||
maxpos = vector.new(6,15,6),
|
|
||||||
minvel = vector.new(-vel,-falling_speed,-vel),
|
|
||||||
maxvel = vector.new(vel,-falling_speed+vel,vel),
|
|
||||||
minacc = vector.new(0,0,0),
|
|
||||||
maxacc = vector.new(0,-0.4,0),
|
|
||||||
minexptime = 0.5,
|
|
||||||
maxexptime = 2,
|
|
||||||
minsize = size,
|
|
||||||
maxsize= size*2,
|
|
||||||
collisiondetection = true,
|
|
||||||
collision_removal = true,
|
|
||||||
vertical = true,
|
|
||||||
}
|
|
||||||
local psdef_backsplash= {
|
|
||||||
amount = 10,
|
|
||||||
time=0,
|
|
||||||
minpos = vector.new(-3,-1,-3),
|
|
||||||
maxpos = vector.new(3,0,3),
|
|
||||||
minvel = vector.new(-vel,falling_speed*2,-vel),
|
|
||||||
maxvel = math.random(vel,falling_speed*2+vel,vel),
|
|
||||||
minacc = vector.new(0,0,0),
|
|
||||||
maxacc = vector.new(0,0,0),
|
|
||||||
minexptime = 0.1,
|
|
||||||
maxexptime = 0.2,
|
|
||||||
minsize = size*0.1,
|
|
||||||
maxsize= size*0.5,
|
|
||||||
collisiondetection = true,
|
|
||||||
collision_removal = true,
|
|
||||||
vertical = true,
|
|
||||||
}
|
|
||||||
local textures = {"weather_pack_rain_raindrop_1.png", "weather_pack_rain_raindrop_2.png", "weather_pack_rain_raindrop_1.png"}
|
|
||||||
|
|
||||||
function mcl_weather.rain.sound_handler(player)
|
function mcl_weather.rain.sound_handler(player)
|
||||||
return minetest.sound_play("weather_rain", {
|
return minetest.sound_play("weather_rain", {
|
||||||
|
@ -83,18 +44,42 @@ function mcl_weather.rain.set_sky_box()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- no no no NO NO f*.. no. no manual particle creatin' PLS!! this sends EVERY particle over the net.
|
-- creating manually parctiles instead of particles spawner because of easier to control
|
||||||
|
-- spawn position.
|
||||||
function mcl_weather.rain.add_rain_particles(player)
|
function mcl_weather.rain.add_rain_particles(player)
|
||||||
mcl_weather.rain.last_rp_count = mcl_weather.rain.particles_count
|
mcl_weather.rain.last_rp_count = 0
|
||||||
for k,v in pairs(textures) do
|
for i=mcl_weather.rain.particles_count, 1,-1 do
|
||||||
psdef.texture=v
|
local random_pos_x, random_pos_y, random_pos_z = mcl_weather.get_random_pos_by_player_look_dir(player)
|
||||||
mcl_weather.add_spawner_player(player,"rain"..k,psdef)
|
if mcl_weather.is_outdoor({x=random_pos_x, y=random_pos_y, z=random_pos_z}) then
|
||||||
|
mcl_weather.rain.last_rp_count = mcl_weather.rain.last_rp_count + 1
|
||||||
|
minetest.add_particle({
|
||||||
|
pos = {x=random_pos_x, y=random_pos_y, z=random_pos_z},
|
||||||
|
velocity = {x=0, y=-10, z=0},
|
||||||
|
acceleration = {x=0, y=-30, z=0},
|
||||||
|
expirationtime = 1.0,
|
||||||
|
size = math.random(0.5, 3),
|
||||||
|
collisiondetection = true,
|
||||||
|
collision_removal = true,
|
||||||
|
vertical = true,
|
||||||
|
texture = mcl_weather.rain.get_texture(),
|
||||||
|
playername = player:get_player_name()
|
||||||
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
psdef_backsplash.texture=textures[math.random(1,#textures)]
|
end
|
||||||
local l=mcl_weather.add_spawner_player(player,"rainbacksplash",psdef_backsplash)
|
|
||||||
if l then
|
-- Simple random texture getter
|
||||||
update_sound[player:get_player_name()]=true
|
function mcl_weather.rain.get_texture()
|
||||||
|
local texture_name
|
||||||
|
local random_number = math.random()
|
||||||
|
if random_number > 0.33 then
|
||||||
|
texture_name = "weather_pack_rain_raindrop_1.png"
|
||||||
|
elseif random_number > 0.66 then
|
||||||
|
texture_name = "weather_pack_rain_raindrop_2.png"
|
||||||
|
else
|
||||||
|
texture_name = "weather_pack_rain_raindrop_3.png"
|
||||||
end
|
end
|
||||||
|
return texture_name;
|
||||||
end
|
end
|
||||||
|
|
||||||
-- register player for rain weather.
|
-- register player for rain weather.
|
||||||
|
@ -104,7 +89,6 @@ function mcl_weather.rain.add_player(player)
|
||||||
local player_meta = {}
|
local player_meta = {}
|
||||||
player_meta.origin_sky = {player:get_sky()}
|
player_meta.origin_sky = {player:get_sky()}
|
||||||
mcl_weather.players[player:get_player_name()] = player_meta
|
mcl_weather.players[player:get_player_name()] = player_meta
|
||||||
update_sound[player:get_player_name()]=true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -115,15 +99,26 @@ function mcl_weather.rain.remove_player(player)
|
||||||
if player_meta and player_meta.origin_sky then
|
if player_meta and player_meta.origin_sky then
|
||||||
player:set_clouds({color="#FFF0F0E5"})
|
player:set_clouds({color="#FFF0F0E5"})
|
||||||
mcl_weather.players[player:get_player_name()] = nil
|
mcl_weather.players[player:get_player_name()] = nil
|
||||||
update_sound[player:get_player_name()]=true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
mcl_worlds.register_on_dimension_change(function(player, dimension)
|
||||||
|
if dimension ~= "overworld" and dimension ~= "void" then
|
||||||
|
mcl_weather.rain.remove_sound(player)
|
||||||
|
mcl_weather.rain.remove_player(player)
|
||||||
|
elseif dimension == "overworld" then
|
||||||
|
mcl_weather.rain.update_sound(player)
|
||||||
|
if mcl_weather.rain.raining then
|
||||||
|
mcl_weather.rain.add_rain_particles(player)
|
||||||
|
mcl_weather.rain.add_player(player)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
-- adds and removes rain sound depending how much rain particles around player currently exist.
|
-- adds and removes rain sound depending how much rain particles around player currently exist.
|
||||||
-- have few seconds delay before each check to avoid on/off sound too often
|
-- have few seconds delay before each check to avoid on/off sound too often
|
||||||
-- when player stay on 'edge' where sound should play and stop depending from random raindrop appearance.
|
-- when player stay on 'edge' where sound should play and stop depending from random raindrop appearance.
|
||||||
function mcl_weather.rain.update_sound(player)
|
function mcl_weather.rain.update_sound(player)
|
||||||
if not update_sound[player:get_player_name()] then return end
|
|
||||||
local player_meta = mcl_weather.players[player:get_player_name()]
|
local player_meta = mcl_weather.players[player:get_player_name()]
|
||||||
if player_meta then
|
if player_meta then
|
||||||
if player_meta.sound_updated and player_meta.sound_updated + 5 > minetest.get_gametime() then
|
if player_meta.sound_updated and player_meta.sound_updated + 5 > minetest.get_gametime() then
|
||||||
|
@ -141,7 +136,6 @@ function mcl_weather.rain.update_sound(player)
|
||||||
|
|
||||||
player_meta.sound_updated = minetest.get_gametime()
|
player_meta.sound_updated = minetest.get_gametime()
|
||||||
end
|
end
|
||||||
update_sound[player:get_player_name()]=false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- rain sound removed from player.
|
-- rain sound removed from player.
|
||||||
|
@ -164,8 +158,7 @@ function mcl_weather.rain.clear()
|
||||||
for _, player in pairs(get_connected_players()) do
|
for _, player in pairs(get_connected_players()) do
|
||||||
mcl_weather.rain.remove_sound(player)
|
mcl_weather.rain.remove_sound(player)
|
||||||
mcl_weather.rain.remove_player(player)
|
mcl_weather.rain.remove_player(player)
|
||||||
mcl_weather.remove_spawners_player(player)
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(function(dtime)
|
||||||
|
@ -184,10 +177,8 @@ function mcl_weather.rain.make_weather()
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, player in pairs(get_connected_players()) do
|
for _, player in pairs(get_connected_players()) do
|
||||||
local pos=player:get_pos()
|
if (mcl_weather.is_underwater(player) or not mcl_worlds.has_weather(player:get_pos())) then
|
||||||
if mcl_weather.is_underwater(player) or not mcl_worlds.has_weather(pos) or not mcl_weather.is_outdoor(pos) then
|
|
||||||
mcl_weather.rain.remove_sound(player)
|
mcl_weather.rain.remove_sound(player)
|
||||||
mcl_weather.remove_spawners_player(player)
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
mcl_weather.rain.add_player(player)
|
mcl_weather.rain.add_player(player)
|
||||||
|
@ -199,12 +190,8 @@ end
|
||||||
-- Switch the number of raindrops: "thunder" for many raindrops, otherwise for normal raindrops
|
-- Switch the number of raindrops: "thunder" for many raindrops, otherwise for normal raindrops
|
||||||
function mcl_weather.rain.set_particles_mode(mode)
|
function mcl_weather.rain.set_particles_mode(mode)
|
||||||
if mode == "thunder" then
|
if mode == "thunder" then
|
||||||
psdef.amount=PARTICLES_COUNT_THUNDER
|
|
||||||
psdef_backsplash.amount=PARTICLES_COUNT_THUNDER
|
|
||||||
mcl_weather.rain.particles_count = PARTICLES_COUNT_THUNDER
|
mcl_weather.rain.particles_count = PARTICLES_COUNT_THUNDER
|
||||||
else
|
else
|
||||||
psdef.amount=PARTICLES_COUNT_RAIN
|
|
||||||
psdef_backsplash.amount=PARTICLES_COUNT_RAIN
|
|
||||||
mcl_weather.rain.particles_count = PARTICLES_COUNT_RAIN
|
mcl_weather.rain.particles_count = PARTICLES_COUNT_RAIN
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,25 +5,30 @@ mcl_weather.snow = {}
|
||||||
mcl_weather.snow.particles_count = 15
|
mcl_weather.snow.particles_count = 15
|
||||||
mcl_weather.snow.init_done = false
|
mcl_weather.snow.init_done = false
|
||||||
|
|
||||||
local psdef= {
|
-- calculates coordinates and draw particles for snow weather
|
||||||
amount = 99,
|
function mcl_weather.snow.add_snow_particles(player)
|
||||||
time = 0, --stay on til we turn it off
|
mcl_weather.rain.last_rp_count = 0
|
||||||
minpos = vector.new(-15,-5,-15),
|
for i=mcl_weather.snow.particles_count, 1,-1 do
|
||||||
maxpos =vector.new(15,10,15),
|
local random_pos_x, _, random_pos_z = mcl_weather.get_random_pos_by_player_look_dir(player)
|
||||||
minvel = vector.new(0,-1,0),
|
local random_pos_y = math.random() + math.random(player:get_pos().y - 1, player:get_pos().y + 7)
|
||||||
maxvel = vector.new(0,-4,0),
|
if minetest.get_node_light({x=random_pos_x, y=random_pos_y, z=random_pos_z}, 0.5) == 15 then
|
||||||
minacc = vector.new(0,-1,0),
|
mcl_weather.rain.last_rp_count = mcl_weather.rain.last_rp_count + 1
|
||||||
maxacc = vector.new(0,-4,0),
|
minetest.add_particle({
|
||||||
minexptime = 1,
|
pos = {x=random_pos_x, y=random_pos_y, z=random_pos_z},
|
||||||
maxexptime = 1,
|
velocity = {x = math.random(-100,100)*0.001, y = math.random(-300,-100)*0.004, z = math.random(-100,100)*0.001},
|
||||||
minsize = 0.5,
|
acceleration = {x = 0, y=0, z = 0},
|
||||||
maxsize = 5,
|
expirationtime = 8.0,
|
||||||
collisiondetection = true,
|
size = 1,
|
||||||
collision_removal = true,
|
collisiondetection = true,
|
||||||
object_collision = true,
|
collision_removal = true,
|
||||||
vertical = true,
|
object_collision = false,
|
||||||
glow = 1
|
vertical = false,
|
||||||
}
|
texture = mcl_weather.snow.get_texture(),
|
||||||
|
playername = player:get_player_name()
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function mcl_weather.snow.set_sky_box()
|
function mcl_weather.snow.set_sky_box()
|
||||||
mcl_weather.skycolor.add_layer(
|
mcl_weather.skycolor.add_layer(
|
||||||
|
@ -43,7 +48,6 @@ end
|
||||||
function mcl_weather.snow.clear()
|
function mcl_weather.snow.clear()
|
||||||
mcl_weather.skycolor.remove_layer("weather-pack-snow-sky")
|
mcl_weather.skycolor.remove_layer("weather-pack-snow-sky")
|
||||||
mcl_weather.snow.init_done = false
|
mcl_weather.snow.init_done = false
|
||||||
mcl_weather.remove_all_spawners()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Simple random texture getter
|
-- Simple random texture getter
|
||||||
|
@ -70,14 +74,10 @@ minetest.register_globalstep(function(dtime)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, player in pairs(get_connected_players()) do
|
for _, player in pairs(get_connected_players()) do
|
||||||
if (mcl_weather.is_underwater(player) or not mcl_worlds.has_weather(player:get_pos()) or not mcl_weather.is_outdoor(player:get_pos())) then
|
if (mcl_weather.is_underwater(player) or not mcl_worlds.has_weather(player:get_pos())) then
|
||||||
mcl_weather.remove_spawners_player(player)
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
for i=1,2 do
|
mcl_weather.snow.add_snow_particles(player)
|
||||||
psdef.texture="weather_pack_snow_snowflake"..i..".png"
|
|
||||||
mcl_weather.add_spawner_player(player,"snow"..i,psdef)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
|
@ -47,35 +47,6 @@ local function save_weather()
|
||||||
end
|
end
|
||||||
minetest.register_on_shutdown(save_weather)
|
minetest.register_on_shutdown(save_weather)
|
||||||
|
|
||||||
local particlespawners={}
|
|
||||||
function mcl_weather.add_spawner_player(pl,id,ps)
|
|
||||||
local name=pl:get_player_name()
|
|
||||||
if not particlespawners[name] then
|
|
||||||
particlespawners[name] = {}
|
|
||||||
end
|
|
||||||
if not particlespawners[name][id] then
|
|
||||||
ps.playername =name
|
|
||||||
ps.attached = pl
|
|
||||||
particlespawners[name][id]=minetest.add_particlespawner(ps)
|
|
||||||
return particlespawners[name][id]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function mcl_weather.remove_spawners_player(pl)
|
|
||||||
local name=pl:get_player_name()
|
|
||||||
if not particlespawners[name] then return end
|
|
||||||
for k,v in pairs(particlespawners[name]) do
|
|
||||||
minetest.delete_particlespawner(v)
|
|
||||||
end
|
|
||||||
particlespawners[name] = nil
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function mcl_weather.remove_all_spawners()
|
|
||||||
for k,v in pairs(minetest.get_connected_players()) do
|
|
||||||
mcl_weather.remove_spawners_player(v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function mcl_weather.get_rand_end_time(min_duration, max_duration)
|
function mcl_weather.get_rand_end_time(min_duration, max_duration)
|
||||||
local r
|
local r
|
||||||
if min_duration and max_duration then
|
if min_duration and max_duration then
|
||||||
|
@ -121,6 +92,36 @@ function mcl_weather.is_underwater(player)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- trying to locate position for particles by player look direction for performance reason.
|
||||||
|
-- it is costly to generate many particles around player so goal is focus mainly on front view.
|
||||||
|
function mcl_weather.get_random_pos_by_player_look_dir(player)
|
||||||
|
local look_dir = player:get_look_dir()
|
||||||
|
local player_pos = player:get_pos()
|
||||||
|
|
||||||
|
local random_pos_x, random_pos_y, random_pos_z
|
||||||
|
|
||||||
|
if look_dir.x > 0 then
|
||||||
|
if look_dir.z > 0 then
|
||||||
|
random_pos_x = math.random() + math.random(player_pos.x - 2.5, player_pos.x + 5)
|
||||||
|
random_pos_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5)
|
||||||
|
else
|
||||||
|
random_pos_x = math.random() + math.random(player_pos.x - 2.5, player_pos.x + 5)
|
||||||
|
random_pos_z = math.random() + math.random(player_pos.z - 5, player_pos.z + 2.5)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if look_dir.z > 0 then
|
||||||
|
random_pos_x = math.random() + math.random(player_pos.x - 5, player_pos.x + 2.5)
|
||||||
|
random_pos_z = math.random() + math.random(player_pos.z - 2.5, player_pos.z + 5)
|
||||||
|
else
|
||||||
|
random_pos_x = math.random() + math.random(player_pos.x - 5, player_pos.x + 2.5)
|
||||||
|
random_pos_z = math.random() + math.random(player_pos.z - 5, player_pos.z + 2.5)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
random_pos_y = math.random() + math.random(player_pos.y + 10, player_pos.y + 15)
|
||||||
|
return random_pos_x, random_pos_y, random_pos_z
|
||||||
|
end
|
||||||
|
|
||||||
local t, wci = 0, mcl_weather.check_interval
|
local t, wci = 0, mcl_weather.check_interval
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(function(dtime)
|
||||||
|
|
Loading…
Reference in New Issue