2020-04-26 18:11:38 +02:00
|
|
|
-- initialize API interface
|
2020-04-13 01:55:39 +02:00
|
|
|
local api = {}
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- define various standard effect cycle lengths
|
|
|
|
api.SHORT_CYCLE = 0 -- for particles and fast animations (use GSCYCLE)
|
|
|
|
api.DEFAULT_CYCLE = 0.1 -- for most effect types
|
|
|
|
api.MEDIUM_CYCLE = 2.0 -- for ressource intensive tasks
|
|
|
|
api.LONG_CYCLE = 5.0 -- for write operations and skybox changes
|
2020-04-13 01:55:39 +02:00
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- register new weather presets (like rain)
|
|
|
|
-- @param name <string> Unique preset name, ideally prefixed
|
|
|
|
-- @param conditions <table> A collection of required influences
|
|
|
|
-- @param effects <table> A <table> containing all applied effects as keys and parameters as values
|
2020-04-13 01:55:39 +02:00
|
|
|
function api.register_weather(name, conditions, effects)
|
|
|
|
climate_mod.weathers[name] = {
|
|
|
|
conditions = conditions,
|
|
|
|
effects = effects,
|
|
|
|
active_players = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- register new weather effects (like particles)
|
|
|
|
-- @param name <string> Unique effect name, ideally prefixed
|
|
|
|
-- @param handler <function> A function to be called when the effect is active
|
|
|
|
-- @param htype <string: start|tick|stop> Determines when the function is called
|
2020-04-13 01:55:39 +02:00
|
|
|
function api.register_effect(name, handler, htype)
|
|
|
|
-- check for valid handler types
|
|
|
|
if htype ~= "start" and htype ~= "tick" and htype ~= "stop" then
|
2020-04-22 00:56:34 +02:00
|
|
|
minetest.log("warning", "[Climate API] Effect " .. dump(name) .. " uses invalid callback type: " .. dump(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
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- set cycle length of given effect
|
|
|
|
-- @param name <string> Name of the affected effect
|
|
|
|
-- @param cycle <number> Duration between function calls
|
2020-04-13 01:55:39 +02:00
|
|
|
function api.set_effect_cycle(name, cycle)
|
|
|
|
climate_mod.cycles[name].timespan = cycle
|
|
|
|
end
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- register new environment influence that is independent of position
|
|
|
|
-- @param name <string> Unique influence name
|
|
|
|
-- @param func <function> Returns current influence value for entire world
|
2020-04-25 15:49:53 +02:00
|
|
|
function api.register_global_influence(name, func)
|
|
|
|
climate_mod.global_influences[name] = func
|
|
|
|
end
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- register new environment influence based on position
|
|
|
|
-- @param name <string> Unique influence name
|
|
|
|
-- @param func <function> Returns current influence value for given position
|
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-26 18:11:38 +02:00
|
|
|
-- register new Active Block Modifier dependent on weather status
|
|
|
|
-- Uses same config as Minetest.register_abm() but also adds
|
|
|
|
-- conditions similiar to weather presets and provides local environment
|
|
|
|
-- to action event handler as third parameter.
|
|
|
|
-- @param config <table> ABM configuration with additional information
|
2020-04-16 19:12:20 +02:00
|
|
|
function api.register_abm(config)
|
2020-04-24 01:35:07 +02:00
|
|
|
if not climate_mod.settings.block_updates then return end
|
|
|
|
|
2020-04-16 19:12:20 +02:00
|
|
|
local conditions = config.conditions
|
|
|
|
local action = config.action
|
|
|
|
local pos_override = config.pos_override
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- override action handler to inject weather status
|
2020-04-16 19:12:20 +02:00
|
|
|
local override = function(pos, node)
|
|
|
|
if type(pos_override) == "function" then
|
|
|
|
pos = pos_override(pos)
|
2020-04-26 18:11:38 +02:00
|
|
|
node = minetest.get_node(pos)
|
2020-04-16 19:12:20 +02:00
|
|
|
end
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- get environment influences for current position
|
|
|
|
local env = climate_mod.trigger.get_position_environment(pos)
|
|
|
|
|
2020-04-16 19:12:20 +02:00
|
|
|
if conditions == nil then
|
|
|
|
return action(pos, node, env)
|
|
|
|
end
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- check if all conditions are met
|
2020-04-16 19:12:20 +02:00
|
|
|
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
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- register overridden abm setup
|
2020-04-16 19:12:20 +02:00
|
|
|
config.conditions = nil
|
|
|
|
config.action = override
|
|
|
|
minetest.register_abm(config)
|
|
|
|
end
|
|
|
|
|
2020-04-26 18:11:38 +02:00
|
|
|
-- return supplied API endpoint
|
2020-04-13 01:55:39 +02:00
|
|
|
return api
|