Merge pull request #2 from AngledLuffa/fewer_tables

Fewer tables
This commit is contained in:
JoeyDP 2021-03-09 16:42:29 +01:00 committed by GitHub
commit 986997ece2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 44 deletions

View File

@ -58,18 +58,6 @@ function energyCraftingModifier(entity)
end end
end end
function getSuctionRate(entity)
if not entity.is_crafting() and getSpaceForPollution(entity) == 0 then
return 0
else
return getBasePurificationRate(entity) * entity.crafting_speed * energyCraftingModifier(entity)
end
end
function getAbsorptionRate(entity)
return math.min(getSpaceForPollution(entity), getSuctionRate(entity))
end
function pollutionInPollutedWater(amount) function pollutionInPollutedWater(amount)
return amount * 6 / 10 return amount * 6 / 10
end end
@ -87,6 +75,26 @@ function getSpaceForPollution(entity)
return capacity - pollution return capacity - pollution
end end
function getSuctionRate(entity)
if not entity.is_crafting() and getSpaceForPollution(entity) == 0 then
return 0
else
return getBasePurificationRate(entity) * entity.crafting_speed * energyCraftingModifier(entity)
end
end
function getAbsorptionRate(entity)
return math.min(getSpaceForPollution(entity), getSuctionRate(entity))
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
@ -99,7 +107,6 @@ function inRadius(filter, radius)
end end
end end
-- ##################### -- #####################
-- # Update script # -- # Update script #
-- ##################### -- #####################
@ -113,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
@ -149,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
@ -167,23 +178,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") local steps = stepsToOrigin(dx, dy)
-- game.print("From: " .. chunkFrom.x .. ", " .. chunkFrom.y) toPollute = toPollute / #steps
for _, chunkVia in pairs(chunksVia) do for _, step in pairs(steps) do
-- game.print("To: " .. chunkVia.x .. ", " .. chunkVia.y) position[1] = (chunkTo.x + step[1]) * 32
chunkVia:pollute(toPollute / #chunksVia) position[2] = (chunkTo.y + step[2]) * 32
surface.pollute(position, toPollute)
--game.print("To: " .. position[1] .. ", " .. position[2] .. " (" .. toPollute .. ")")
end end
chunkFrom:pollute(-toPollute)
end end
end end
@ -374,15 +388,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
@ -413,6 +418,9 @@ function FilteredChunk:addFilter(filter)
end end
end end
-- Remove a filter machine from its chunk
-- If this is removing the last filter, the record
-- for this chunk is dropped.
function FilteredChunk:removeFilter(filter) function FilteredChunk:removeFilter(filter)
for i, f in pairs(self.filters) do for i, f in pairs(self.filters) do
if f.unit_number == filter.unit_number then if f.unit_number == filter.unit_number then
@ -425,6 +433,9 @@ function FilteredChunk:removeFilter(filter)
end end
end end
-- either return an existing FilteredChunk for the given coordinates,
-- or create a new one at the corresponding coordinates if there are
-- no existing filters on this chunk
function getFilteredChunk(surface, x, y) function getFilteredChunk(surface, x, y)
local chunkListX = global.air_filtered_chunks_map[surface.name] local chunkListX = global.air_filtered_chunks_map[surface.name]
if chunkListX ~= nil then if chunkListX ~= nil then
@ -449,6 +460,8 @@ function isAirFilterMachine(entity)
return starts_with(entity.name, "air-filter-machine-") return starts_with(entity.name, "air-filter-machine-")
end end
-- when a new machine is created, add it to the list of filters
-- on whichever chunk it is on
function onEntityCreated(event) function onEntityCreated(event)
if isAirFilterMachine(event.created_entity) then if isAirFilterMachine(event.created_entity) then
local chunkPos = positionToChunk(event.created_entity.position) local chunkPos = positionToChunk(event.created_entity.position)

View File

@ -1,6 +1,6 @@
{ {
"name": "better-air-filtering", "name": "better-air-filtering",
"version": "0.3.2", "version": "0.3.3",
"title": "Better Air Filtering", "title": "Better Air Filtering",
"author": "Joey De Pauw", "author": "Joey De Pauw",
"contact": "joeydepauw@gmail.com", "contact": "joeydepauw@gmail.com",