Slightly optimize the tables in the absorb and suction functions
This commit is contained in:
parent
422e24bf32
commit
798ed388cf
|
@ -87,6 +87,14 @@ function getAbsorptionRate(entity)
|
||||||
return math.min(getSpaceForPollution(entity), getSuctionRate(entity))
|
return math.min(getSpaceForPollution(entity), getSuctionRate(entity))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function getTotalAbsorptionRate(filters)
|
||||||
|
local totalAbsorptionRate = 0.0
|
||||||
|
for _, filter in pairs(filters) do
|
||||||
|
totalAbsorptionRate = totalAbsorptionRate + getAbsorptionRate(filter)
|
||||||
|
end
|
||||||
|
return totalAbsorptionRate
|
||||||
|
end
|
||||||
|
|
||||||
function inRadius(filter, radius)
|
function inRadius(filter, radius)
|
||||||
if filter.name == "air-filter-machine-1" then
|
if filter.name == "air-filter-machine-1" then
|
||||||
return radius <= 0
|
return radius <= 0
|
||||||
|
@ -112,27 +120,31 @@ function absorbPollution(event)
|
||||||
end
|
end
|
||||||
|
|
||||||
function absorbChunk(chunk)
|
function absorbChunk(chunk)
|
||||||
if chunk:get_pollution() == 0 then
|
local chunk_pollution = chunk:get_pollution()
|
||||||
|
if chunk_pollution == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local totalAbsorptionRate = chunk:getTotalAbsorptionRate()
|
local filters = chunk:getFilters()
|
||||||
|
local totalAbsorptionRate = getTotalAbsorptionRate(filters)
|
||||||
|
|
||||||
--game.print("totalAbsorptionRate: " .. totalAbsorptionRate)
|
--game.print("totalAbsorptionRate: " .. totalAbsorptionRate)
|
||||||
--game.print("filter count: " .. #chunk.filters)
|
--game.print("filter count: " .. #filters)
|
||||||
|
|
||||||
if totalAbsorptionRate == 0 then
|
if totalAbsorptionRate == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local toAbsorb = math.min(chunk:get_pollution(), totalAbsorptionRate)
|
local toAbsorb = math.min(chunk_pollution, totalAbsorptionRate)
|
||||||
-- game.print("To absorb: " .. toAbsorb)
|
-- game.print("To absorb: " .. toAbsorb)
|
||||||
|
|
||||||
local totalInsertedAmount = 0.0
|
local totalInsertedAmount = 0.0
|
||||||
for _, filter in pairs(chunk:getFilters()) do
|
local fluid = { name = "pollution", amount = 0.0 }
|
||||||
|
for _, filter in pairs(filters) do
|
||||||
local toInsert = (getAbsorptionRate(filter) / totalAbsorptionRate) * toAbsorb
|
local toInsert = (getAbsorptionRate(filter) / totalAbsorptionRate) * toAbsorb
|
||||||
if toInsert > 0 then
|
if toInsert > 0 then
|
||||||
local insertedAmount = filter.insert_fluid({ name = "pollution", amount = toInsert })
|
fluid.amount = toInsert
|
||||||
|
local insertedAmount = filter.insert_fluid(fluid)
|
||||||
game.pollution_statistics.on_flow(filter.name, -insertedAmount)
|
game.pollution_statistics.on_flow(filter.name, -insertedAmount)
|
||||||
totalInsertedAmount = totalInsertedAmount + insertedAmount
|
totalInsertedAmount = totalInsertedAmount + insertedAmount
|
||||||
end
|
end
|
||||||
|
@ -148,10 +160,10 @@ function stepsToOrigin(x, y)
|
||||||
-- Provide coordinates of possible 1-steps toward (0, 0)
|
-- Provide coordinates of possible 1-steps toward (0, 0)
|
||||||
local steps = {}
|
local steps = {}
|
||||||
if x ~= 0 then
|
if x ~= 0 then
|
||||||
table.insert(steps, { x = x - sign(x), y = y })
|
table.insert(steps, { x - sign(x), y })
|
||||||
end
|
end
|
||||||
if y ~= 0 then
|
if y ~= 0 then
|
||||||
table.insert(steps, { x = x, y = y - sign(y) })
|
table.insert(steps, { x, y - sign(y) })
|
||||||
end
|
end
|
||||||
return steps
|
return steps
|
||||||
end
|
end
|
||||||
|
@ -178,11 +190,10 @@ function suctionUpdateChunk(chunkTo, dx, dy)
|
||||||
--game.print("Moving " .. toPollute .. " pollution")
|
--game.print("Moving " .. toPollute .. " pollution")
|
||||||
--game.print("From: " .. position[1] .. ", " .. position[2] .. " (" .. toPollute .. ")")
|
--game.print("From: " .. position[1] .. ", " .. position[2] .. " (" .. toPollute .. ")")
|
||||||
|
|
||||||
-- TODO: creating the table in stepsToOrigin can be unrolled
|
|
||||||
local steps = stepsToOrigin(dx, dy)
|
local steps = stepsToOrigin(dx, dy)
|
||||||
for _, step in pairs(steps) do
|
for _, step in pairs(steps) do
|
||||||
position[1] = (chunkTo.x + step.x) * 32
|
position[1] = (chunkTo.x + step[1]) * 32
|
||||||
position[2] = (chunkTo.y + step.y) * 32
|
position[2] = (chunkTo.y + step[2]) * 32
|
||||||
surface.pollute(position, toPollute / #steps)
|
surface.pollute(position, toPollute / #steps)
|
||||||
--game.print("To: " .. position[1] .. ", " .. position[2] .. " (" .. (toPollute / #steps) .. ")")
|
--game.print("To: " .. position[1] .. ", " .. position[2] .. " (" .. (toPollute / #steps) .. ")")
|
||||||
end
|
end
|
||||||
|
@ -376,15 +387,6 @@ function FilteredChunk:removeFromMap()
|
||||||
--end
|
--end
|
||||||
end
|
end
|
||||||
|
|
||||||
function FilteredChunk:getTotalAbsorptionRate()
|
|
||||||
local totalAbsorptionRate = 0.0
|
|
||||||
for _, filter in pairs(self:getFilters()) do
|
|
||||||
local absorptionRate = getAbsorptionRate(filter)
|
|
||||||
totalAbsorptionRate = totalAbsorptionRate + absorptionRate
|
|
||||||
end
|
|
||||||
return totalAbsorptionRate
|
|
||||||
end
|
|
||||||
|
|
||||||
function FilteredChunk:getTotalSuctionRate(distance)
|
function FilteredChunk:getTotalSuctionRate(distance)
|
||||||
local totalSuctionRate = 0.0
|
local totalSuctionRate = 0.0
|
||||||
for _, filter in pairs(self:getFilters()) do
|
for _, filter in pairs(self:getFilters()) do
|
||||||
|
|
Reference in New Issue