minor code cleanup

This commit is contained in:
Arturas Norkus 2017-06-24 19:15:33 +03:00
parent 1f07735c44
commit e90133b30a
7 changed files with 187 additions and 202 deletions

View File

@ -2,13 +2,13 @@ local modpath = minetest.get_modpath("weather_pack");
-- If skylayer mod not located then embeded version will be loaded.
if minetest.get_modpath("skylayer") == nil then
dofile(modpath.."/embedded_sky_layer_api.lua")
dofile(modpath.."/embedded_sky_layer_api.lua")
end
-- If happy_weather_api mod not located then embeded version will be loaded.
if minetest.get_modpath("happy_weather_api") == nil then
dofile(modpath.."/embedded_happy_weather_api.lua")
dofile(modpath.."/commands.lua")
dofile(modpath.."/embedded_happy_weather_api.lua")
dofile(modpath.."/commands.lua")
end
-- Happy Weather utilities
@ -20,10 +20,10 @@ dofile(modpath.."/heavy_rain.lua")
dofile(modpath.."/snow.lua")
if minetest.get_modpath("lightning") ~= nil then
dofile(modpath.."/thunder.lua")
-- Turn off lightning mod 'auto mode'
lightning.auto = false
dofile(modpath.."/thunder.lua")
-- Turn off lightning mod 'auto mode'
lightning.auto = false
end
dofile(modpath.."/abm.lua")

View File

@ -9,6 +9,7 @@
local light_rain = {}
light_rain.last_check = 0
light_rain.check_interval = 200
light_rain.chance = 0.15
-- Weather identification code
light_rain.code = "light_rain"
@ -26,7 +27,7 @@ local SKYCOLOR_LAYER = "happy_weather_light_rain_sky"
light_rain.is_starting = function(dtime, position)
if light_rain.last_check + light_rain.check_interval < os.time() then
light_rain.last_check = os.time()
if math.random() < 0.15 then
if math.random() < light_rain.chance then
return true
end
end
@ -83,7 +84,7 @@ local remove_rain_sound = function(player)
if sound ~= nil then
minetest.sound_stop(sound)
sound_handlers[player:get_player_name()] = nil
end
end
end
light_rain.add_player = function(player)
@ -98,16 +99,10 @@ end
-- Random texture getter
local choice_random_rain_drop_texture = function()
local texture_name
local random_number = math.random()
if random_number > 0.33 then
texture_name = "happy_weather_light_rain_raindrop_1.png"
elseif random_number > 0.66 then
texture_name = "happy_weather_light_rain_raindrop_2.png"
else
texture_name = "happy_weather_light_rain_raindrop_3.png"
end
return texture_name;
local base_name = "happy_weather_light_rain_raindrop_"
local number = math.random(1, 4)
local extension = ".png"
return base_name .. number .. extension
end
local add_rain_particle = function(player)
@ -121,16 +116,16 @@ local add_rain_particle = function(player)
if hw_utils.is_outdoor(random_pos) then
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 = 2,
size = math.random(0.5, 3),
collisiondetection = true,
collision_removal = true,
vertical = true,
texture = choice_random_rain_drop_texture(),
playername = player:get_player_name()
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 = 2,
size = math.random(0.5, 3),
collisiondetection = true,
collision_removal = true,
vertical = true,
texture = choice_random_rain_drop_texture(),
playername = player:get_player_name()
})
end
end

View File

@ -9,6 +9,7 @@
local rain = {}
rain.last_check = 0
rain.check_interval = 300
rain.chance = 0.1
-- Weather identification code
rain.code = "rain"
@ -24,14 +25,14 @@ local manual_trigger_end = false
local SKYCOLOR_LAYER = "happy_weather_rain_sky"
rain.is_starting = function(dtime, position)
if rain.last_check + rain.check_interval < os.time() then
rain.last_check = os.time()
if math.random() < 0.1 then
happy_weather.request_to_end("light_rain")
happy_weather.request_to_end("heavy_rain")
return true
end
end
if rain.last_check + rain.check_interval < os.time() then
rain.last_check = os.time()
if math.random() < rain.chance then
happy_weather.request_to_end("light_rain")
happy_weather.request_to_end("heavy_rain")
return true
end
end
if manual_trigger_start then
manual_trigger_start = false
@ -42,13 +43,13 @@ rain.is_starting = function(dtime, position)
end
rain.is_ending = function(dtime)
if rain.last_check + rain.check_interval < os.time() then
rain.last_check = os.time()
if math.random() < 0.6 then
happy_weather.request_to_start("light_rain")
return true
end
end
if rain.last_check + rain.check_interval < os.time() then
rain.last_check = os.time()
if math.random() < 0.6 then
happy_weather.request_to_start("light_rain")
return true
end
end
if manual_trigger_end then
manual_trigger_end = false
@ -86,7 +87,7 @@ local remove_rain_sound = function(player)
if sound ~= nil then
minetest.sound_stop(sound)
sound_handlers[player:get_player_name()] = nil
end
end
end
rain.add_player = function(player)
@ -101,16 +102,10 @@ end
-- Random texture getter
local choice_random_rain_drop_texture = function()
local texture_name
local random_number = math.random()
if random_number > 0.33 then
texture_name = "happy_weather_light_rain_raindrop_1.png"
elseif random_number > 0.66 then
texture_name = "happy_weather_light_rain_raindrop_2.png"
else
texture_name = "happy_weather_light_rain_raindrop_3.png"
end
return texture_name;
local base_name = "happy_weather_light_rain_raindrop_"
local number = math.random(1, 4)
local extension = ".png"
return base_name .. number .. extension
end
local add_rain_particle = function(player)
@ -124,16 +119,16 @@ local add_rain_particle = function(player)
if hw_utils.is_outdoor(random_pos) then
minetest.add_particle({
pos = {x=random_pos.x, y=random_pos.y, z=random_pos.z},
velocity = {x=0, y=-15, z=0},
acceleration = {x=0, y=-35, z=0},
expirationtime = 2,
size = math.random(1, 6),
collisiondetection = true,
collision_removal = true,
vertical = true,
texture = choice_random_rain_drop_texture(),
playername = player:get_player_name()
pos = {x=random_pos.x, y=random_pos.y, z=random_pos.z},
velocity = {x=0, y=-15, z=0},
acceleration = {x=0, y=-35, z=0},
expirationtime = 2,
size = math.random(1, 4),
collisiondetection = true,
collision_removal = true,
vertical = true,
texture = choice_random_rain_drop_texture(),
playername = player:get_player_name()
})
end
end
@ -147,10 +142,10 @@ local display_rain_particles = function(player)
end
rain.in_area = function(position)
if hw_utils.is_biome_frozen(position) or
hw_utils.is_biome_dry(position) then
return false
end
if hw_utils.is_biome_frozen(position) or
hw_utils.is_biome_dry(position) then
return false
end
if position.y > -10 then
return true

View File

@ -9,6 +9,7 @@
local snow = {}
snow.last_check = 0
snow.check_interval = 200
snow.chance = 0.2
-- Weather identification code
snow.code = "snow"
@ -21,12 +22,12 @@ local manual_trigger_end = false
local SKYCOLOR_LAYER = "happy_weather_snow_sky"
snow.is_starting = function(dtime, position)
if snow.last_check + snow.check_interval < os.time() then
snow.last_check = os.time()
if math.random() < 0.2 then
return true
end
end
if snow.last_check + snow.check_interval < os.time() then
snow.last_check = os.time()
if math.random() < snow.chance then
return true
end
end
if manual_trigger_start then
manual_trigger_start = false
@ -37,12 +38,12 @@ snow.is_starting = function(dtime, position)
end
snow.is_ending = function(dtime)
if snow.last_check + snow.check_interval < os.time() then
snow.last_check = os.time()
if math.random() < 0.5 then
return true
end
end
if snow.last_check + snow.check_interval < os.time() then
snow.last_check = os.time()
if math.random() < 0.5 then
return true
end
end
if manual_trigger_end then
manual_trigger_end = false
@ -59,7 +60,7 @@ local set_sky_box = function(player_name)
sl.data = {gradient_data={}}
sl.data.gradient_data.colors = {
{r=0, g=0, b=0},
{r=241, g=244, b=249},
{r=231, g=234, b=239},
{r=0, g=0, b=0}
}
skylayer.add_layer(player_name, sl)
@ -75,16 +76,10 @@ end
-- Random texture getter
local choice_random_rain_drop_texture = function()
local texture_name
local random_number = math.random()
if random_number > 0.33 then
texture_name = "happy_weather_light_snow_snowflake_1.png"
elseif random_number > 0.66 then
texture_name = "happy_weather_light_snow_snowflake_2.png"
else
texture_name = "happy_weather_light_snow_snowflake_3.png"
end
return texture_name;
local base_name = "happy_weather_light_snow_snowflake_"
local number = math.random(1, 3)
local extension = ".png"
return base_name .. number .. extension
end
local add_particle = function(player)
@ -100,9 +95,9 @@ local add_particle = function(player)
minetest.add_particle({
pos = {x=random_pos.x, y=random_pos.y, z=random_pos.z},
velocity = {x = math.random(-1,-0.5), y = math.random(-2,-1), z = math.random(-1,-0.5)},
acceleration = {x = math.random(-1,-0.5), y=-0.5, z = math.random(-1,-0.5)},
expirationtime = 2.0,
size = math.random(0.5, 2),
acceleration = {x = math.random(-1,-0.5), y=-0.5, z = math.random(-1,-0.5)},
expirationtime = 2.0,
size = math.random(0.5, 2),
collisiondetection = true,
collision_removal = true,
vertical = true,
@ -123,19 +118,19 @@ end
local particles_number_per_update = 10
snow.render = function(dtime, player)
for i=particles_number_per_update, 1,-1 do
display_particles(player)
display_particles(player)
end
end
snow.in_area = function(position)
if hw_utils.is_biome_frozen(position) == false then
return false
end
if hw_utils.is_biome_frozen(position) == false then
return false
end
if position.y > -10 then
return true
end
return false
if position.y > -10 then
return true
end
return false
end
snow.start = function()

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 B

View File

@ -11,6 +11,7 @@
local thunder = {}
thunder.last_check = 0
thunder.check_interval = 100
thunder.chance = 0.8
-- Weather identification code
thunder.code = "thunder"
@ -33,7 +34,7 @@ thunder.is_starting = function(dtime)
if thunder.last_check + thunder.check_interval < os.time() then
thunder.last_check = os.time()
if math.random() < 0.8 and happy_weather.is_weather_active("heavy_rain") then
if math.random() < thunder.chance and happy_weather.is_weather_active("heavy_rain") then
return true
end
end
@ -68,8 +69,8 @@ local calculate_thunder_strike_delay = function()
end
thunder.render = function(dtime, player)
if happy_weather.is_player_in_weather_area(player:get_player_name(),
"heavy_rain") == false then
local player_name = player:get_player_name()
if happy_weather.is_player_in_weather_area(player_name, "heavy_rain") == false then
return
end

191
utils.lua
View File

@ -25,129 +25,128 @@ end
-- checks if player is undewater. This is needed in order to
-- turn off weather particles generation.
hw_utils.is_underwater = function(player)
local ppos = player:getpos()
local offset = player:get_eye_offset()
local player_eye_pos = {x = ppos.x + offset.x,
y = ppos.y + offset.y + 1.5,
z = ppos.z + offset.z}
local node_level = minetest.get_node_level(player_eye_pos)
if node_level == 8 or node_level == 7 then
return true
end
return false
local ppos = player:getpos()
local offset = player:get_eye_offset()
local player_eye_pos = {
x = ppos.x + offset.x,
y = ppos.y + offset.y + 1.5,
z = ppos.z + offset.z}
local node_level = minetest.get_node_level(player_eye_pos)
if node_level == 8 or node_level == 7 then
return true
end
return false
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.
hw_utils.get_random_pos = function(player, offset)
local look_dir = player:get_look_dir()
local player_pos = player:getpos()
local look_dir = player:get_look_dir()
local player_pos = player:getpos()
local random_pos_x = 0
local random_pos_y = 0
local random_pos_z = 0
local random_pos_x = 0
local random_pos_y = 0
local random_pos_z = 0
if look_dir.x > 0 then
if look_dir.z > 0 then
random_pos_x = math.random(player_pos.x - offset.back, player_pos.x + offset.front) + math.random()
random_pos_z = math.random(player_pos.z - offset.back, player_pos.z + offset.front) + math.random()
else
random_pos_x = math.random(player_pos.x - offset.back, player_pos.x + offset.front) + math.random()
random_pos_z = math.random(player_pos.z - offset.front, player_pos.z + offset.back) + math.random()
end
else
if look_dir.z > 0 then
random_pos_x = math.random(player_pos.x - offset.front, player_pos.x + offset.back) + math.random()
random_pos_z = math.random(player_pos.z - offset.back, player_pos.z + offset.front) + math.random()
else
random_pos_x = math.random(player_pos.x - offset.front, player_pos.x + offset.back) + math.random()
random_pos_z = math.random(player_pos.z - offset.front, player_pos.z + offset.back) + math.random()
end
end
if look_dir.x > 0 then
if look_dir.z > 0 then
random_pos_x = math.random(player_pos.x - offset.back, player_pos.x + offset.front) + math.random()
random_pos_z = math.random(player_pos.z - offset.back, player_pos.z + offset.front) + math.random()
else
random_pos_x = math.random(player_pos.x - offset.back, player_pos.x + offset.front) + math.random()
random_pos_z = math.random(player_pos.z - offset.front, player_pos.z + offset.back) + math.random()
end
else
if look_dir.z > 0 then
random_pos_x = math.random(player_pos.x - offset.front, player_pos.x + offset.back) + math.random()
random_pos_z = math.random(player_pos.z - offset.back, player_pos.z + offset.front) + math.random()
else
random_pos_x = math.random(player_pos.x - offset.front, player_pos.x + offset.back) + math.random()
random_pos_z = math.random(player_pos.z - offset.front, player_pos.z + offset.back) + math.random()
end
end
if offset.bottom ~= nil then
random_pos_y = math.random(player_pos.y - offset.bottom, player_pos.y + offset.top)
else
random_pos_y = player_pos.y + offset.top
end
if offset.bottom ~= nil then
random_pos_y = math.random(player_pos.y - offset.bottom, player_pos.y + offset.top)
else
random_pos_y = player_pos.y + offset.top
end
return {x=random_pos_x, y=random_pos_y, z=random_pos_z}
return {x=random_pos_x, y=random_pos_y, z=random_pos_z}
end
local np_temp = {
offset = 50,
scale = 50,
spread = {x = 1000, y = 1000, z = 1000},
seed = 5349,
octaves = 3,
persist = 0.5,
lacunarity = 2.0
offset = 50,
scale = 50,
spread = {x = 1000, y = 1000, z = 1000},
seed = 5349,
octaves = 3,
persist = 0.5,
lacunarity = 2.0
}
local np_humid = {
offset = 50,
scale = 50,
spread = {x = 1000, y = 1000, z = 1000},
seed = 842,
octaves = 3,
persist = 0.5,
lacunarity = 2.0
offset = 50,
scale = 50,
spread = {x = 1000, y = 1000, z = 1000},
seed = 842,
octaves = 3,
persist = 0.5,
lacunarity = 2.0
}
hw_utils.is_biome_frozen = function(position)
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
return false -- v6 not supported yet.
end
return hw_utils.is_biome_frozen_v7(position)
local is_biome_frozen_v7 = function(position)
local posx = math.floor(position.x)
local posz = math.floor(position.z)
local noise_obj = minetest.get_perlin(np_temp)
local noise_temp = noise_obj:get2d({x = posx, y = posz})
-- below 35 heat biome considered to be frozen type
return noise_temp < 35
end
hw_utils.is_biome_frozen_v7 = function(position)
local posx = math.floor(position.x)
local posz = math.floor(position.z)
local noise_obj = minetest.get_perlin(np_temp)
local noise_temp = noise_obj:get2d({x = posx, y = posz})
hw_utils.is_biome_frozen = function(position)
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
return false -- v6 not supported yet.
end
return is_biome_frozen_v7(position)
end
-- below 35 heat biome considered to be frozen type
return noise_temp < 35
local is_biome_dry_v7 = function(position)
local posx = math.floor(position.x)
local posz = math.floor(position.z)
local noise_obj = minetest.get_perlin(np_humid)
local noise_humid = noise_obj:get2d({x = posx, y = posz})
-- below 50 humid biome considered to be dry type (at least by this mod)
return noise_humid < 50
end
hw_utils.is_biome_dry = function(position)
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
return false -- v6 not supported yet.
end
return hw_utils.is_biome_dry_v7(position)
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
return false -- v6 not supported yet.
end
return is_biome_dry_v7(position)
end
hw_utils.is_biome_dry_v7 = function(position)
local posx = math.floor(position.x)
local posz = math.floor(position.z)
local noise_obj = minetest.get_perlin(np_humid)
local noise_humid = noise_obj:get2d({x = posx, y = posz})
local is_biome_tropic_v7 = function(position)
local posx = math.floor(position.x)
local posz = math.floor(position.z)
local noise_obj = minetest.get_perlin(np_humid)
local noise_humid = noise_obj:get2d({x = posx, y = posz})
noise_obj = minetest.get_perlin(np_temp)
local noise_temp = noise_obj:get2d({x = posx, y = posz})
-- below 50 humid biome considered to be dry type (at least by this mod)
return noise_humid < 50
-- humid and temp values are taked by testing flying around world (not sure actually)
return noise_humid > 55 and noise_temp > 80
end
hw_utils.is_biome_tropic = function(position)
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
return false -- v6 not supported yet.
end
return hw_utils.is_biome_tropic_v7(position)
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
return false -- v6 not supported yet.
end
return is_biome_tropic_v7(position)
end
hw_utils.is_biome_tropic_v7 = function(position)
local posx = math.floor(position.x)
local posz = math.floor(position.z)
local noise_obj = minetest.get_perlin(np_humid)
local noise_humid = noise_obj:get2d({x = posx, y = posz})
noise_obj = minetest.get_perlin(np_temp)
local noise_temp = noise_obj:get2d({x = posx, y = posz})
-- humid and temp values are taked by testing flying around world (not sure actually)
return noise_humid > 55 and noise_temp > 80
end