api.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. -- initialize API interface
  2. local api = {}
  3. -- define various standard effect cycle lengths
  4. api.SHORT_CYCLE = 0 -- for particles and fast animations (use GSCYCLE)
  5. api.DEFAULT_CYCLE = 2.0 -- for most effect types
  6. api.LONG_CYCLE = 5.0 -- for ressource intensive tasks or timed effects
  7. -- register new weather presets (like rain)
  8. -- @param name <string> Unique preset name, ideally prefixed
  9. -- @param conditions <table> A collection of required influences
  10. -- @param effects <table> A <table> containing all applied effects as keys and parameters as values
  11. function api.register_weather(name, conditions, effects)
  12. climate_mod.weathers[name] = {
  13. conditions = conditions,
  14. effects = effects,
  15. active_players = {}
  16. }
  17. end
  18. -- register new weather effects (like particles)
  19. -- @param name <string> Unique effect name, ideally prefixed
  20. -- @param handler <function> A function to be called when the effect is active
  21. -- @param htype <string: start|tick|stop> Determines when the function is called
  22. function api.register_effect(name, handler, htype)
  23. -- check for valid handler types
  24. if htype ~= "start" and htype ~= "tick" and htype ~= "stop" then
  25. minetest.log("warning", "[Climate API] Effect " .. dump(name) .. " uses invalid callback type: " .. dump(htype))
  26. return
  27. end
  28. -- create effect handler registry if not existent yet
  29. if type(climate_mod.effects[name]) == "nil" then
  30. climate_mod.effects[name] = { start = {}, tick = {}, stop = {} }
  31. climate_mod.cycles[name] = { timespan = api.DEFAULT_CYCLE, timer = 0 }
  32. end
  33. -- store effect handler
  34. table.insert(climate_mod.effects[name][htype], handler)
  35. end
  36. -- set cycle length of given effect
  37. -- @param name <string> Name of the affected effect
  38. -- @param cycle <number> Duration between function calls
  39. function api.set_effect_cycle(name, cycle)
  40. climate_mod.cycles[name].timespan = cycle
  41. end
  42. -- register new environment influence that is independent of position
  43. -- @param name <string> Unique influence name
  44. -- @param func <function> Returns current influence value for entire world
  45. function api.register_global_influence(name, func)
  46. climate_mod.global_influences[name] = func
  47. end
  48. -- register new environment influence based on position
  49. -- @param name <string> Unique influence name
  50. -- @param func <function> Returns current influence value for given position
  51. function api.register_influence(name, func)
  52. climate_mod.influences[name] = func
  53. end
  54. -- register new Active Block Modifier dependent on weather status
  55. -- Uses same config as Minetest.register_abm() but also adds
  56. -- conditions similiar to weather presets and provides local environment
  57. -- to action event handler as third parameter.
  58. -- @param config <table> ABM configuration with additional information
  59. function api.register_abm(config)
  60. if not climate_mod.settings.block_updates then return end
  61. local conditions = config.conditions
  62. local action = config.action
  63. local pos_override = config.pos_override
  64. -- override action handler to inject weather status
  65. local override = function(pos, node)
  66. if type(pos_override) == "function" then
  67. pos = pos_override(pos)
  68. node = minetest.get_node(pos)
  69. end
  70. -- get environment influences for current position
  71. local env = climate_mod.trigger.get_position_environment(pos)
  72. if conditions == nil then
  73. return action(pos, node, env)
  74. end
  75. -- check if all conditions are met
  76. for condition, goal in pairs(conditions) do
  77. local is_applicable = climate_mod.trigger.test_condition(condition, env, goal)
  78. if not is_applicable then return end
  79. end
  80. return action(pos, node, env)
  81. end
  82. -- register overridden abm setup
  83. config.conditions = nil
  84. config.action = override
  85. minetest.register_abm(config)
  86. end
  87. -- return supplied API endpoint
  88. return api