1
0
Fork 0

Merge pull request 'Fix weather lag' (#2091) from fix-weather-lag into master

Reviewed-on: MineClone2/MineClone2#2091
This commit is contained in:
cora 2022-03-30 10:25:44 +00:00
commit 6851fa759e
4 changed files with 176 additions and 134 deletions

View File

@ -1,36 +1,66 @@
mcl_weather.nether_dust = {} mcl_weather.nether_dust = {}
mcl_weather.nether_dust.particles_count = 99 mcl_weather.nether_dust.particlespawners = {}
-- calculates coordinates and draw particles for Nether dust local psdef= {
function mcl_weather.nether_dust.add_dust_particles(player) amount = 150,
for i=mcl_weather.nether_dust.particles_count, 1,-1 do time = 0,
local rpx, rpy, rpz = mcl_weather.get_random_pos_by_player_look_dir(player) minpos = vector.new(-15,-15,-15),
minetest.add_particle({ maxpos =vector.new(15,15,15),
pos = {x = rpx, y = rpy - math.random(6, 18), z = rpz}, minvel = vector.new(-0.3,-0.15,-1),
velocity = {x = math.random(-30,30)*0.01, y = math.random(-15,15)*0.01, z = math.random(-30,30)*0.01}, maxvel = vector.new(0.3,0.15,0.3),
acceleration = {x = math.random(-50,50)*0.02, y = math.random(-20,20)*0.02, z = math.random(-50,50)*0.02}, minacc = vector.new(-1,-0.4,-1),
expirationtime = 3, maxacc = vector.new(1,0.4,1),
size = math.random(6,20)*0.01, minexptime = 1,
collisiondetection = false, maxexptime = 10,
object_collision = false, minsize = 0.2,
vertical = false, maxsize = 0.7,
glow = math.random(0,minetest.LIGHT_MAX), collisiondetection = false,
texture = "mcl_particles_nether_dust"..tostring(i%3+1)..".png", collision_removal = false,
playername = player:get_player_name() object_collision = false,
}) vertical = false
}
local function check_player(player)
local name=player:get_player_name()
if mcl_worlds.has_dust(player:get_pos()) and not mcl_weather.nether_dust.particlespawners[name] then
return true
end
end
mcl_weather.nether_dust.add_particlespawners = function(player)
local name=player:get_player_name(name)
mcl_weather.nether_dust.particlespawners[name]={}
psdef.playername = name
psdef.attached = player
psdef.glow = math.random(0,minetest.LIGHT_MAX)
for i=1,3 do
psdef.texture="mcl_particles_nether_dust"..i..".png"
mcl_weather.nether_dust.particlespawners[name][i]=minetest.add_particlespawner(psdef)
end end
end end
local timer = 0 mcl_weather.nether_dust.delete_particlespawners = function(player)
minetest.register_globalstep(function(dtime) local name=player:get_player_name(name)
timer = timer + dtime if mcl_weather.nether_dust.particlespawners[name] then
if timer < 0.7 then return end for i=1,3 do
timer = 0 minetest.delete_particlespawner(mcl_weather.nether_dust.particlespawners[name][i])
for _, player in pairs(minetest.get_connected_players()) do
if not mcl_worlds.has_dust(player:get_pos()) then
return false
end end
mcl_weather.nether_dust.add_dust_particles(player) mcl_weather.nether_dust.particlespawners[name]=nil
end
end
mcl_worlds.register_on_dimension_change(function(player, dimension)
if check_player(player) then
return mcl_weather.nether_dust.add_particlespawners(player)
end
mcl_weather.nether_dust.delete_particlespawners(player)
end)
minetest.register_on_joinplayer(function(player)
if check_player(player) then
mcl_weather.nether_dust.add_particlespawners(player)
end end
end) end)
minetest.register_on_leaveplayer(function(player)
mcl_weather.nether_dust.delete_particlespawners(player)
end)

View File

@ -1,5 +1,5 @@
local PARTICLES_COUNT_RAIN = 30 local PARTICLES_COUNT_RAIN = 100
local PARTICLES_COUNT_THUNDER = 45 local PARTICLES_COUNT_THUNDER = 300
local get_connected_players = minetest.get_connected_players local get_connected_players = minetest.get_connected_players
@ -19,6 +19,45 @@ 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", {
@ -44,42 +83,18 @@ function mcl_weather.rain.set_sky_box()
end end
end end
-- creating manually parctiles instead of particles spawner because of easier to control -- no no no NO NO f*.. no. no manual particle creatin' PLS!! this sends EVERY particle over the net.
-- spawn position.
function mcl_weather.rain.add_rain_particles(player) function mcl_weather.rain.add_rain_particles(player)
mcl_weather.rain.last_rp_count = 0 mcl_weather.rain.last_rp_count = mcl_weather.rain.particles_count
for i=mcl_weather.rain.particles_count, 1,-1 do for k,v in pairs(textures) do
local random_pos_x, random_pos_y, random_pos_z = mcl_weather.get_random_pos_by_player_look_dir(player) psdef.texture=v
if mcl_weather.is_outdoor({x=random_pos_x, y=random_pos_y, z=random_pos_z}) then mcl_weather.add_spawner_player(player,"rain"..k,psdef)
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
end psdef_backsplash.texture=textures[math.random(1,#textures)]
local l=mcl_weather.add_spawner_player(player,"rainbacksplash",psdef_backsplash)
-- Simple random texture getter if l then
function mcl_weather.rain.get_texture() update_sound[player:get_player_name()]=true
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.
@ -89,6 +104,7 @@ 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
@ -99,26 +115,15 @@ 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
@ -136,6 +141,7 @@ 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.
@ -158,7 +164,8 @@ 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)
end mcl_weather.remove_spawners_player(player)
end
end end
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
@ -177,8 +184,10 @@ function mcl_weather.rain.make_weather()
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())) then local pos=player:get_pos()
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)
@ -190,8 +199,12 @@ 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

View File

@ -5,30 +5,25 @@ 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
-- calculates coordinates and draw particles for snow weather local psdef= {
function mcl_weather.snow.add_snow_particles(player) amount = 99,
mcl_weather.rain.last_rp_count = 0 time = 0, --stay on til we turn it off
for i=mcl_weather.snow.particles_count, 1,-1 do minpos = vector.new(-15,-5,-15),
local random_pos_x, _, random_pos_z = mcl_weather.get_random_pos_by_player_look_dir(player) maxpos =vector.new(15,10,15),
local random_pos_y = math.random() + math.random(player:get_pos().y - 1, player:get_pos().y + 7) minvel = vector.new(0,-1,0),
if minetest.get_node_light({x=random_pos_x, y=random_pos_y, z=random_pos_z}, 0.5) == 15 then maxvel = vector.new(0,-4,0),
mcl_weather.rain.last_rp_count = mcl_weather.rain.last_rp_count + 1 minacc = vector.new(0,-1,0),
minetest.add_particle({ maxacc = vector.new(0,-4,0),
pos = {x=random_pos_x, y=random_pos_y, z=random_pos_z}, minexptime = 1,
velocity = {x = math.random(-100,100)*0.001, y = math.random(-300,-100)*0.004, z = math.random(-100,100)*0.001}, maxexptime = 1,
acceleration = {x = 0, y=0, z = 0}, minsize = 0.5,
expirationtime = 8.0, maxsize = 5,
size = 1, collisiondetection = true,
collisiondetection = true, collision_removal = true,
collision_removal = true, object_collision = true,
object_collision = false, vertical = true,
vertical = false, glow = 1
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(
@ -48,6 +43,7 @@ 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
@ -74,10 +70,14 @@ 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())) then 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
mcl_weather.remove_spawners_player(player)
return false return false
end end
mcl_weather.snow.add_snow_particles(player) for i=1,2 do
psdef.texture="weather_pack_snow_snowflake"..i..".png"
mcl_weather.add_spawner_player(player,"snow"..i,psdef)
end
end end
end) end)

View File

@ -47,6 +47,35 @@ 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
@ -92,36 +121,6 @@ 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)