2020-04-13 01:55:39 +02:00
|
|
|
local api = {}
|
|
|
|
|
2020-04-13 13:40:05 +02:00
|
|
|
api.SHORT_CYCLE = 0.03 -- for particles and fast animations
|
2020-04-13 16:53:32 +02:00
|
|
|
api.DEFAULT_CYCLE = 0.1 -- for most effect types
|
2020-04-14 11:25:00 +02:00
|
|
|
api.MEDIUM_CYCLE = 2.0 -- for ressource intensive tasks
|
2020-04-13 16:53:32 +02:00
|
|
|
api.LONG_CYCLE = 5.0 -- for write operations and skybox changes
|
2020-04-13 01:55:39 +02:00
|
|
|
|
|
|
|
function api.register_weather(name, conditions, effects)
|
|
|
|
-- TODO: check and sanitize
|
|
|
|
climate_mod.weathers[name] = {
|
|
|
|
conditions = conditions,
|
|
|
|
effects = effects,
|
|
|
|
active_players = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function api.register_effect(name, handler, htype)
|
|
|
|
-- check for valid handler types
|
|
|
|
if htype ~= "start" and htype ~= "tick" and htype ~= "stop" then
|
2020-04-14 11:25:00 +02:00
|
|
|
minetest.log("warning", "[Climate API] Invalid effect handler type: " .. htype)
|
2020-04-13 01:55:39 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- create effect handler registry if not existent yet
|
|
|
|
if type(climate_mod.effects[name]) == "nil" then
|
|
|
|
climate_mod.effects[name] = { start = {}, tick = {}, stop = {} }
|
2020-04-14 11:25:00 +02:00
|
|
|
climate_mod.cycles[name] = { timespan = api.DEFAULT_CYCLE, timer = 0 }
|
2020-04-13 01:55:39 +02:00
|
|
|
end
|
|
|
|
-- store effect handler
|
|
|
|
table.insert(climate_mod.effects[name][htype], handler)
|
|
|
|
end
|
|
|
|
|
|
|
|
function api.set_effect_cycle(name, cycle)
|
|
|
|
climate_mod.cycles[name].timespan = cycle
|
|
|
|
end
|
|
|
|
|
2020-04-18 08:01:36 +02:00
|
|
|
function api.register_influence(name, func)
|
2020-04-14 11:25:00 +02:00
|
|
|
climate_mod.influences[name] = func
|
2020-04-18 08:01:36 +02:00
|
|
|
end
|
2020-04-14 11:25:00 +02:00
|
|
|
|
2020-04-16 19:12:20 +02:00
|
|
|
function api.register_abm(config)
|
|
|
|
local conditions = config.conditions
|
|
|
|
local action = config.action
|
|
|
|
local pos_override = config.pos_override
|
|
|
|
|
|
|
|
local override = function(pos, node)
|
|
|
|
local env = climate_mod.trigger.get_position_environment(pos)
|
|
|
|
if type(pos_override) == "function" then
|
|
|
|
pos = pos_override(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
if conditions == nil then
|
|
|
|
return action(pos, node, env)
|
|
|
|
end
|
|
|
|
|
|
|
|
for condition, goal in pairs(conditions) do
|
2020-04-18 08:01:36 +02:00
|
|
|
local is_applicable = climate_mod.trigger.test_condition(condition, env, goal)
|
|
|
|
if not is_applicable then return end
|
2020-04-16 19:12:20 +02:00
|
|
|
end
|
|
|
|
return action(pos, node, env)
|
|
|
|
end
|
|
|
|
|
|
|
|
config.conditions = nil
|
|
|
|
config.action = override
|
|
|
|
minetest.register_abm(config)
|
|
|
|
end
|
|
|
|
|
2020-04-13 01:55:39 +02:00
|
|
|
return api
|