Dynamic Rain


Adds rain cycles, lightning and thunder effects, and fire suppression

Tweaks
5 days ago
2.0
3.35K
Environment

i Handler for chunks cleared of pollution via rain

16 days ago

Hello!

I am loving the dynamic rain mod, it's great. Thank you for writing it. :D

I was hoping you could expose a handler I could register to when a rainstorm cleans up all the pollution in a chunk. I've got a treez mod with a tree planting mechanic that is trying to detect if the rain cleans up the pollution in a chunk so it can figure out if it can/should plant a seed in a purified chunk. Right now I'm checking when a rainstorm is going and what the pollution values are (and if any hit zero) during the rain-storm as an indirect proxy for a handler. My hope is to be considerate of server load so having a direct handler/detection method would be useful. Please let me know what you think,

Sudain

Suggested code:
events.lua:
-- Shared event definitions. Required by both control.lua and pollution.lua
-- so the ID is created exactly once and is consistent across both files.
return {
on_chunk_pollution_cleared = script.generate_event_name(),
}

--==--
pollution.lua
local events = require("events")

-- ...inside M.on_tick, replace the existing pollute block with:
if current > 0 then
local removed = math.min(current, cfg.rain_pollution_cleanup_cap)
if removed > 0 then
surface.pollute(pos, -removed, "dynamic-rain-pollution-cleaner")

-- If removed == current, this call brought the chunk to exactly zero.
if removed == current then
  script.raise_event(events.on_chunk_pollution_cleared, {
    surface_index  = surface.index,
    chunk_position = { x = chunk.x, y = chunk.y },
  })
end

end
end

--==--
control.lua
local events = require("events")

remote.add_interface("dynamic-rain", {
is_raining = function(surface_index) ... end, -- existing, unchanged
time_left = function(surface_index) ... end, -- existing, unchanged
is_sunshower = function(surface_index) ... end, -- existing, unchanged

-- New: other mods call this to get the event ID, then use script.on_event()
get_events = function()
return { on_chunk_pollution_cleared = events.on_chunk_pollution_cleared }
end,
})

15 days ago
(updated 15 days ago)

Hello!
Thanks for the suggestion, and I'm glad you're enjoying the mod :)

I think the use case makes sense, but I'd prefer to shape the API a little differently. An event named "chunk pollution cleared" feels a bit too specific and could be misleading, because Dynamic Rain only knows about pollution removed by this mod. A chunk can also become clean through natural absorption, reduced production, pollution spreading, or another mod, and Dynamic Rain would not detect those cases.

What I'm considering instead is a rain-cleanup event that reports what Dynamic Rain actually did during its cleanup pass.

Something like:

on_rain_pollution_cleaned

with a batched payload:

{
surface_index = surface.index,
chunks = {
{
position = { x = chunk.x, y = chunk.y },
pollution_before = current,
pollution_removed = removed,
pollution_after = current - removed,
},
...
}
}

The event would only be raised when rain cleanup actually removes pollution from at least one chunk. Consumers could then decide whether they care about pollution_after == 0, a minimum removed amount, or some other threshold.

I'd probably batch the chunks into one event per tick/surface rather than raising one event per chunk.

Would this work for your tree-planting logic?

15 days ago

Yup, that'd work out really well. I then I can maintain a list on my side to detect if a new chunk (for a particular rain storm) cleared any pollution to zero. If so, plant seeds and add it to list of chunks already planted in. If it's a new chunk I have planted in, then I can skip planting. What version of dynamic rain should I build listeners to listen to?

New response