climate_api/lib/environment.lua

80 lines
2.3 KiB
Lua
Raw Normal View History

2020-04-09 20:25:02 +02:00
local mod_lightning = minetest.get_modpath("lightning")
local LIGHTNING_CHANCE = 1000
if mod_lightning then
lightning.auto = false
end
2020-04-09 09:03:02 +02:00
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()
2020-04-09 09:03:02 +02:00
local random = weather_mod.state.heat;
return (base + biome + height) * time * date + random
2020-04-09 09:03:02 +02:00
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
2020-04-09 09:03:02 +02:00
end
function weather_mod.get_climate(pos)
local climate = {pos = pos}
climate.heat = weather_mod.get_heat(pos)
climate.humidity = weather_mod.get_humidity(pos)
climate.windspeed = vector.length(weather_mod.state.wind)
return climate
end
2020-04-09 09:03:02 +02:00
local function is_acceptable_weather_param(value, attr, config)
local min = config.conditions["min_" .. attr] or -math.huge
2020-04-09 09:03:02 +02:00
local max = config.conditions["max_" .. attr] or math.huge
return value > min and value <= max
end
function weather_mod.get_effects(climate)
local forced_weather = weather_mod.state.current_weather
if type(forced_weather) ~= nil and forced_weather ~= "auto" then
return { forced_weather }
end
2020-04-09 09:03:02 +02:00
local params = {}
params.heat = climate.heat
params.humidity = climate.humidity
params.windspeed = vector.length(weather_mod.state.wind)
params.height = climate.pos.y
2020-04-09 09:03:02 +02:00
local effects = {}
local attributes = { "heat", "humidity", "windspeed", "height" }
for name, effect in pairs(weather_mod.weathers) do
if type(effect.config.conditions) == "nil" then
table.insert(effects, name)
goto continue
end
2020-04-09 09:03:02 +02:00
for _, attr in ipairs(attributes) do
if not is_acceptable_weather_param(params[attr], attr, effect.config) then
goto continue
2020-04-09 09:03:02 +02:00
end
end
table.insert(effects, name)
::continue::
2020-04-09 09:03:02 +02:00
end
return effects
2020-04-09 20:25:02 +02:00
end
function weather_mod.handle_events(player, flags)
local ppos = player:get_pos()
if mod_lightning and weather_mod.settings.lightning and type(flags["lightning"]) ~= "nil" then
local random = rng:next(1, LIGHTNING_CHANCE)
if random == 1 then
lightning.strike(ppos)
end
end
if type(flags["damage"]) ~= "nil" then
weather_mod.damage_player(player, 1)
end
2020-04-09 09:03:02 +02:00
end