mirror of
https://github.com/t-affeldt/climate_api.git
synced 2025-06-30 07:20:48 +02:00
Sync first working prototype
This commit is contained in:
62
lib/environment.lua
Normal file
62
lib/environment.lua
Normal file
@ -0,0 +1,62 @@
|
||||
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 random = weather_mod.state.heat;
|
||||
return (base + biome + height) * 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
|
||||
end
|
||||
|
||||
local function is_acceptable_weather_param(value, attr, config)
|
||||
local min = config.conditions["min_" .. attr] or -10000
|
||||
local max = config.conditions["max_" .. attr] or math.huge
|
||||
minetest.log(attr .. ": " .. value .. " <=> " .. min .. "," .. max)
|
||||
return value > min and value <= max
|
||||
end
|
||||
|
||||
function weather_mod.get_weather(pos, wind)
|
||||
local params = {}
|
||||
params.heat = weather_mod.get_heat(pos)
|
||||
params.humidity = weather_mod.get_humidity(pos)
|
||||
params.windspeed = vector.length(wind)
|
||||
minetest.log(params.heat .. ", " .. params.humidity .. ", " .. params.windspeed)
|
||||
|
||||
local weather
|
||||
local priority = -1
|
||||
local attributes = { "heat", "humidity", "windspeed" }
|
||||
for name, config in pairs(weather_mod.weathers) do
|
||||
minetest.log(dump2(priority, "p"))
|
||||
if type(priority) ~= "nil" and config.priority < priority then
|
||||
minetest.log("skipped " .. name)
|
||||
|
||||
elseif type(config.conditions) == "nil" then
|
||||
weather = name
|
||||
priority = config.priority
|
||||
minetest.log("selected (nil) " .. name)
|
||||
|
||||
else
|
||||
local check = true
|
||||
for _, attr in ipairs(attributes) do
|
||||
if not is_acceptable_weather_param(params[attr], attr, config) then
|
||||
check = false
|
||||
end
|
||||
end
|
||||
if check then
|
||||
weather = name
|
||||
priority = config.priority
|
||||
minetest.log("selected " .. name)
|
||||
end
|
||||
end
|
||||
end
|
||||
if type(weather) == "nil" then
|
||||
minetest.log("error", "[Ultimate Weather] No default weather registered")
|
||||
end
|
||||
minetest.log(weather)
|
||||
return weather
|
||||
end
|
3
lib/lightning.lua
Normal file
3
lib/lightning.lua
Normal file
@ -0,0 +1,3 @@
|
||||
if minetest.get_modpath("lightning") then
|
||||
lightning.auto = false
|
||||
end
|
130
lib/main.lua
Normal file
130
lib/main.lua
Normal file
@ -0,0 +1,130 @@
|
||||
local GSCYCLE = 0
|
||||
local temperature = 10
|
||||
local humidity = 100
|
||||
local wind = vector.new(10, 0, -0.25)
|
||||
|
||||
weather_mod.weathers = {}
|
||||
function weather_mod.register_weather(name, weather)
|
||||
-- TODO: check and sanitize
|
||||
weather_mod.weathers[name] = weather
|
||||
end
|
||||
|
||||
function weather_mod.set_weather(name)
|
||||
if type(weather_mod.weathers[name]) == nil then
|
||||
minetest.log("warning", "[Ultimate Weathers] Weather does not exist")
|
||||
return
|
||||
end
|
||||
weather_mod.state.current_weather = name
|
||||
end
|
||||
|
||||
local function is_outside(player, wind)
|
||||
local ppos = player:getpos()
|
||||
local wind_pos = vector.multiply(wind,-1)
|
||||
local skylight_pos = vector.add(ppos, vector.new(0, 40, 0))
|
||||
local downfall_origin = vector.add(skylight_pos,wind_pos)
|
||||
return weather_mod.is_outdoors(player, downfall_origin)
|
||||
end
|
||||
|
||||
local function get_texture(particles)
|
||||
if type(particles.textures) == "nil" or next(particles.textures) == nil then
|
||||
return particles.texture
|
||||
end
|
||||
return particles.textures[math.random(#particles.textures)]
|
||||
end
|
||||
|
||||
local function spawn_particles(player, particles, wind)
|
||||
local ppos = player:getpos()
|
||||
local wind_pos = vector.multiply(wind,-1)
|
||||
local wind_speed = vector.length(wind)
|
||||
|
||||
local texture = get_texture(particles)
|
||||
|
||||
local minp = vector.add(vector.add(ppos, particles.min_pos),wind_pos)
|
||||
local maxp = vector.add(vector.add(ppos, particles.max_pos),wind_pos)
|
||||
|
||||
local vel = {x=wind.x,y=-particles.falling_speed,z=wind.z}
|
||||
local acc = {x=0, y=0, z=0}
|
||||
|
||||
local exp = particles.exptime
|
||||
local vertical = wind_speed < 3
|
||||
|
||||
minetest.add_particlespawner({
|
||||
amount=particles.amount,
|
||||
time=0.5,
|
||||
minpos=minp,
|
||||
maxpos=maxp,
|
||||
minvel=vel,
|
||||
maxvel=vel,
|
||||
minacc=acc,
|
||||
maxacc=acc,
|
||||
minexptime=exp,
|
||||
maxexptime=exp,
|
||||
minsize=particles.size,
|
||||
maxsize=particles.size,
|
||||
collisiondetection=true,
|
||||
collision_removal=true,
|
||||
vertical=vertical,
|
||||
texture=texture,
|
||||
player=player:get_player_name()
|
||||
})
|
||||
end
|
||||
|
||||
local sound_handles = {}
|
||||
local function play_sound(player, sound)
|
||||
local playername = player:get_player_name()
|
||||
if not sound_handles[playername] then
|
||||
local handle = minetest.sound_play(sound, {
|
||||
to_player = playername,
|
||||
loop = true
|
||||
})
|
||||
if handle then
|
||||
sound_handles[playername] = handle
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function stop_sound(player)
|
||||
local playername = player:get_player_name()
|
||||
if sound_handles[playername] then
|
||||
minetest.sound_stop(sound_handles[playername])
|
||||
sound_handles[playername] = nil
|
||||
end
|
||||
end
|
||||
|
||||
local function handle_weather()
|
||||
for _, player in ipairs(minetest.get_connected_players()) do
|
||||
local ppos = player:getpos()
|
||||
weather_mod.set_weather(weather_mod.get_weather(ppos, wind))
|
||||
|
||||
if ppos.y < weather_mod.settings.min_height or ppos.y > weather_mod.settings.max_height then
|
||||
return
|
||||
end
|
||||
|
||||
local weather = weather_mod.weathers[weather_mod.state.current_weather]
|
||||
local clouds = weather.clouds
|
||||
clouds.speed = vector.multiply(wind, 2)
|
||||
player:set_clouds(clouds)
|
||||
|
||||
weather_mod.set_headwind(player, wind)
|
||||
--player:set_clouds({ density = 0.6, color = "#a4a0b6e5", speed = wind })
|
||||
|
||||
local outdoors = is_outside(player, wind)
|
||||
if type(weather.particles) ~= "nil" and outdoors then
|
||||
spawn_particles(player, weather.particles, wind)
|
||||
end
|
||||
|
||||
if type(weather.sound) ~= "nil" and outdoors then
|
||||
play_sound(player, weather.sound)
|
||||
else
|
||||
stop_sound(player)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local timer = 0
|
||||
minetest.register_globalstep(function(dtime)
|
||||
timer = timer + dtime
|
||||
if timer < GSCYCLE then return end
|
||||
timer = 0
|
||||
handle_weather()
|
||||
end)
|
72
lib/player.lua
Normal file
72
lib/player.lua
Normal file
@ -0,0 +1,72 @@
|
||||
local mod_player_monoids = minetest.get_modpath("player_monoids") ~= nil
|
||||
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)
|
||||
elseif mod_playerphysics then
|
||||
playerphysics.add_physics_factor(player, effect, id, value)
|
||||
else
|
||||
local override = {}
|
||||
override[effect] = value
|
||||
player:set_physics_override(override)
|
||||
end
|
||||
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)
|
||||
elseif mod_playerphysics then
|
||||
playerphysics.remove_physics_factor(player, effect, id)
|
||||
else
|
||||
local override = {}
|
||||
override[effect] = 1
|
||||
player:set_physics_override(override)
|
||||
end
|
||||
end
|
||||
|
||||
-- function taken from weather mod
|
||||
-- see https://github.com/theFox6/minetest_mod_weather/blob/master/weather/api.lua#L110
|
||||
local function raycast(player, origin)
|
||||
local hitpos = vector.add(player:get_pos(),vector.new(0,1,0))
|
||||
local ray = minetest.raycast(origin,hitpos)
|
||||
local o = ray:next()
|
||||
if not o then return false end
|
||||
if o.type~="object" then return false end -- hit node or something
|
||||
if not o.ref:is_player() then return false end -- hit different object
|
||||
if o.ref:get_player_name() ~= player:get_player_name() then
|
||||
return false --hit other player
|
||||
end
|
||||
o = ray:next()
|
||||
if o then
|
||||
minetest.log("warning","[ultimate_weather] raycast hit more after hitting the player\n"..
|
||||
dump2(o,"o"))
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function check_light(player)
|
||||
return minetest.get_node_light(player:getpos(), 0.5) == 15
|
||||
end
|
||||
|
||||
function weather_mod.is_outdoors(player, origin)
|
||||
if weather_mod.settings.raycasting then
|
||||
return raycast(player, origin)
|
||||
else
|
||||
return check_light(player)
|
||||
end
|
||||
end
|
||||
|
||||
function weather_mod.set_headwind(player, wind)
|
||||
local movement = vector.normalize(player:get_player_velocity())
|
||||
local product = vector.dot(movement, wind)
|
||||
-- logistic function, scales between 0 and 2
|
||||
-- see https://en.wikipedia.org/wiki/Logistic_function
|
||||
local L = 2 -- maximum value
|
||||
local k = 0.1 -- growth rate
|
||||
local z = 1 -- midpoint
|
||||
local factor = L / (1 + math.exp(-k * (product - z)))
|
||||
weather_mod.add_physics(player, "speed", factor)
|
||||
end
|
Reference in New Issue
Block a user