From dd8b07056a6f2637ebbce05a9f7ba67d0a70c8b7 Mon Sep 17 00:00:00 2001 From: Till Affeldt Date: Fri, 10 Apr 2020 03:06:57 +0200 Subject: [PATCH] Add datastorage, fix player_monoids support, tweak wind --- depends.txt | 7 +----- init.lua | 9 ++++--- lib/calendar.lua | 18 ++++++++++++++ lib/calendar_dictionary.lua | 24 +++++++++++++++++++ lib/datastorage.lua | 48 +++++++++++++++++++++++++++++++++++++ lib/environment.lua | 6 +++-- lib/main.lua | 5 ++-- lib/player.lua | 6 ++--- lib/wind.lua | 12 ++++++---- weathers/storm.lua | 4 ++-- 10 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 lib/calendar.lua create mode 100644 lib/calendar_dictionary.lua create mode 100644 lib/datastorage.lua diff --git a/depends.txt b/depends.txt index dcecde5..511ef53 100644 --- a/depends.txt +++ b/depends.txt @@ -1,10 +1,5 @@ default +datastorage? lightning? -farming? -flowers? -flowers_plus? -bakedclay? -moreplants? -lemontree? player_monoids? playerphysics? \ No newline at end of file diff --git a/init.lua b/init.lua index 7eb8a39..82c219f 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/lib/calendar.lua b/lib/calendar.lua new file mode 100644 index 0000000..6eefe07 --- /dev/null +++ b/lib/calendar.lua @@ -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 \ No newline at end of file diff --git a/lib/calendar_dictionary.lua b/lib/calendar_dictionary.lua new file mode 100644 index 0000000..69a4042 --- /dev/null +++ b/lib/calendar_dictionary.lua @@ -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 } +} \ No newline at end of file diff --git a/lib/datastorage.lua b/lib/datastorage.lua new file mode 100644 index 0000000..e6738bd --- /dev/null +++ b/lib/datastorage.lua @@ -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 diff --git a/lib/environment.lua b/lib/environment.lua index dcef20e..9d57c70 100644 --- a/lib/environment.lua +++ b/lib/environment.lua @@ -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) diff --git a/lib/main.lua b/lib/main.lua index 966984f..c29df50 100644 --- a/lib/main.lua +++ b/lib/main.lua @@ -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 diff --git a/lib/player.lua b/lib/player.lua index 1adc7a3..1ed74ba 100644 --- a/lib/player.lua +++ b/lib/player.lua @@ -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 \ No newline at end of file diff --git a/lib/wind.lua b/lib/wind.lua index 14d6bf0..5722773 100644 --- a/lib/wind.lua +++ b/lib/wind.lua @@ -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 \ No newline at end of file diff --git a/weathers/storm.lua b/weathers/storm.lua index 7ac5caa..00161e2 100644 --- a/weathers/storm.lua +++ b/weathers/storm.lua @@ -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 = {