Add datastorage, fix player_monoids support, tweak wind

This commit is contained in:
Till Affeldt 2020-04-10 03:06:57 +02:00
parent dd73a0df17
commit dd8b07056a
10 changed files with 115 additions and 24 deletions

View File

@ -1,10 +1,5 @@
default
datastorage?
lightning?
farming?
flowers?
flowers_plus?
bakedclay?
moreplants?
lemontree?
player_monoids?
playerphysics?

View File

@ -33,16 +33,15 @@ weather_mod.settings = {
min_height = getNumericSetting("min_height", -50)
}
weather_mod.state = {
heat = 1,
humidity = 1,
wind = vector.new(0, 0, -0.25)
}
dofile(weather_mod.modpath.."/lib/datastorage.lua")
weather_mod.state = weather_mod.get_storage()
-- import core API
dofile(weather_mod.modpath.."/lib/player.lua")
dofile(weather_mod.modpath.."/lib/environment.lua")
dofile(weather_mod.modpath.."/lib/wind.lua")
dofile(weather_mod.modpath.."/lib/calendar_dictionary.lua")
dofile(weather_mod.modpath.."/lib/calendar.lua")
dofile(weather_mod.modpath.."/lib/main.lua")
dofile(weather_mod.modpath.."/lib/commands.lua")

18
lib/calendar.lua Normal file
View File

@ -0,0 +1,18 @@
function weather_mod.get_time_heat()
local time = minetest.get_timeofday()
return math.cos((2 * time + 1) * math.pi) / 3 + 1
end
function weather_mod.get_calendar_heat()
-- European heat center in August instead of June
local progression = (weather_mod.state.time.day + 61) / 365
return math.cos((2 * progression + 1) * math.pi) / 3 + 1
end
function weather_mod.handle_time_progression()
local time = minetest.get_timeofday()
if time < weather_mod.state.time.last_check then
weather_mod.state.time.day = weather_mod.state.time.day + 1
end
weather_mod.state.last_check = time
end

View File

@ -0,0 +1,24 @@
local days = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
}
local months = {
{ name = "January", days = 31 },
{ name = "February", days = 28 },
{ name = "March", days = 31 },
{ name = "April", days = 30 },
{ name = "May", days = 31 },
{ name = "June", days = 30 },
{ name = "July", days = 31 },
{ name = "August", days = 31 },
{ name = "September", days = 30 },
{ name = "October", days = 31 },
{ name = "November", days = 30 },
{ name = "December", days = 31 }
}

48
lib/datastorage.lua Normal file
View File

@ -0,0 +1,48 @@
local default_state = {
heat = 1,
humidity = 1,
wind = vector.new(0, 0, -0.25),
current_weather = "auto",
time = {
last_check = 0,
day = 1
}
}
local function use_datastorage()
local state = datastorage.get(weather_mod.modname, "weather_state")
for key, val in pairs(default_state) do
if type(state[key]) == "nil" then
state[key] = val
end
end
return state
end
local function use_filesystem()
local file_name = minetest.get_worldpath() .. "/" .. weather_mod.modname
minetest.register_on_shutdown(function()
local file = io.open(file_name, "w")
file:write(minetest.serialize(weather_mod.state))
file:close()
end)
local file = io.open(file_name, "r")
if file ~= nil then
local storage = minetest.deserialize(file:read("*a"))
file:close()
if type(storage) == "table" then
return storage
end
end
return default_state
end
function weather_mod.get_storage()
local mod_datastorage = minetest.get_modpath("datastorage") ~= nil
if mod_datastorage then
return use_datastorage()
else
return use_filesystem()
end
end

View File

@ -9,15 +9,17 @@ function weather_mod.get_heat(pos)
local base = weather_mod.settings.heat;
local biome = minetest.get_heat(pos)
local height = math.min(math.max(-pos.y / 15, -10), 10)
local time = weather_mod.get_time_heat()
local date = weather_mod.get_calendar_heat()
local random = weather_mod.state.heat;
return (base + biome + height) * random
return (base + biome + height) * time * date + random
end
function weather_mod.get_humidity(pos)
local base = weather_mod.settings.humidity
local biome = minetest.get_humidity(pos)
local random = weather_mod.state.humidity;
return (base + biome) * random
return (base + biome) + random
end
function weather_mod.get_climate(pos)

View File

@ -1,5 +1,5 @@
local GSCYCLE = 0.05
local RECALCCYCLE = 1
local RECALCCYCLE = 0
weather_mod.weathers = {}
function weather_mod.register_effect(name, config, override)
@ -91,7 +91,7 @@ local function handle_weather_effects(player)
local outdoors = weather_mod.is_outdoors(player)
if type(config.particles) ~= "nil" and outdoors then
spawn_particles(player, config.particles, wind)
spawn_particles(player, config.particles, weather_mod.state.wind)
end
if type(config.sound) ~= "nil" and outdoors then
sounds[effect] = config.sound
@ -116,6 +116,7 @@ minetest.register_globalstep(function(dtime)
handle_weather_effects(player)
if timer >= RECALCCYCLE then
weather_mod.set_clouds(player)
weather_mod.set_headwind(player)
end
end
timer = 0

View File

@ -4,7 +4,7 @@ local mod_playerphysics = minetest.get_modpath("playerphysics") ~= nil
function weather_mod.add_physics(player, effect, value)
local id = weather_mod.modname .. ":" .. effect
if mod_player_monoids then
player_monoids[effect].add_change(player, value, id)
player_monoids[effect]:add_change(player, value, id)
elseif mod_playerphysics then
playerphysics.add_physics_factor(player, effect, id, value)
else
@ -17,7 +17,7 @@ end
function weather_mod.remove_physics(player, effect)
local id = weather_mod.modname .. ":" .. effect
if mod_player_monoids then
player_monoids[effect].del_change(player, id)
player_monoids[effect]:del_change(player, id)
elseif mod_playerphysics then
playerphysics.remove_physics_factor(player, effect, id)
else
@ -88,5 +88,5 @@ function weather_mod.damage_player(player, amount, reason)
return
end
local hp = player:get_hp()
player:set_hp(current_hp - amount, reason)
player:set_hp(hp - amount, reason)
end

View File

@ -1,8 +1,12 @@
function weather_mod.set_headwind(player, wind)
function weather_mod.set_headwind(player)
local movement = vector.normalize(player:get_player_velocity())
local product = vector.dot(movement, wind)
-- logistic function, scales between 0.5 and 1.5
local product = vector.dot(movement, weather_mod.state.wind)
-- logistic function, scales between 0 and 2
-- see https://en.wikipedia.org/wiki/Logistic_function
local factor = 1 / (1 + math.exp(-0.1 * (product - 0.5))) + 0.5
local L = 1.6 -- maximum value
local k = 0.15 -- growth rate
local z = 0.8 -- midpoint
local o = 0.1 -- y offset
local factor = L / (1 + math.exp(-k * (product - z))) + o
weather_mod.add_physics(player, "speed", factor)
end

View File

@ -10,11 +10,11 @@ config.sound = {
config.conditions = {
min_height = weather_mod.settings.min_height,
max_height = weather_mod.settings.max_height,
min_windspeed = 5
min_windspeed = 3.5
}
local function override(params)
local avg_windspeed = 6
local avg_windspeed = 5
local intensity = params.windspeed / avg_windspeed
local dynamic_config = {
sound = {