Create fewer tables when spreading pollution

This commit is contained in:
John Bauer 2021-03-08 10:51:09 -08:00
parent 680e2c09e0
commit 422e24bf32
1 changed files with 16 additions and 14 deletions

View File

@ -99,7 +99,6 @@ function inRadius(filter, radius)
end end
end end
-- ##################### -- #####################
-- # Update script # -- # Update script #
-- ##################### -- #####################
@ -167,23 +166,26 @@ function suctionUpdateChunk(chunkTo, dx, dy)
-- game.print("From " .. dx .. ", " .. dy) -- game.print("From " .. dx .. ", " .. dy)
-- game.print("suction: " .. totalSuction) -- game.print("suction: " .. totalSuction)
local chunkFrom = getFilteredChunk(chunkTo.surface, chunkTo.x + dx, chunkTo.y + dy) local surface = chunkTo.surface
local pollution = chunkFrom:get_pollution() -- get_pollution can handle indexed x, y as well as named x, y
local position = { (chunkTo.x + dx) * 32, (chunkTo.y + dy) * 32 }
local pollution = surface.get_pollution(position)
if pollution > 0.1 then if pollution > 0.1 then
local toPollute = math.min(pollution, totalSuction) local toPollute = math.min(pollution, totalSuction)
local chunksVia = {} -- first, unpollute the chunkFrom
for _, step in pairs(stepsToOrigin(dx, dy)) do surface.pollute(position, -toPollute)
local chunk = getFilteredChunk(chunkTo.surface, chunkTo.x + step.x, chunkTo.y + step.y) --game.print("Moving " .. toPollute .. " pollution")
table.insert(chunksVia, chunk) --game.print("From: " .. position[1] .. ", " .. position[2] .. " (" .. toPollute .. ")")
end
-- game.print("Moving " .. toPollute .. " pollution") -- TODO: creating the table in stepsToOrigin can be unrolled
-- game.print("From: " .. chunkFrom.x .. ", " .. chunkFrom.y) local steps = stepsToOrigin(dx, dy)
for _, chunkVia in pairs(chunksVia) do for _, step in pairs(steps) do
-- game.print("To: " .. chunkVia.x .. ", " .. chunkVia.y) position[1] = (chunkTo.x + step.x) * 32
chunkVia:pollute(toPollute / #chunksVia) position[2] = (chunkTo.y + step.y) * 32
surface.pollute(position, toPollute / #steps)
--game.print("To: " .. position[1] .. ", " .. position[2] .. " (" .. (toPollute / #steps) .. ")")
end end
chunkFrom:pollute(-toPollute)
end end
end end