pollution suction
This commit is contained in:
parent
d984fd890d
commit
2d640e57e6
|
@ -1,25 +1,17 @@
|
||||||
function onTick(event)
|
|
||||||
local maxRadius = 4
|
|
||||||
local radius = event.tick % 30
|
|
||||||
if radius <= maxRadius then
|
|
||||||
for _, surface in pairs(game.surfaces) do
|
|
||||||
local filters = surface.find_entities_filtered {
|
|
||||||
name = {"air-filter-machine-1", "air-filter-machine-2", "air-filter-machine-3"}
|
|
||||||
}
|
|
||||||
updateAirFilters(surface, filters, radius)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function updateAirFilters(surface, filters, radius)
|
|
||||||
-- Carefull not to suck in more pollution than can process
|
function updateAirFilters(surface, filters)
|
||||||
-- This would make air filters OP because they would capture cloud without the need to filter
|
|
||||||
for _, filter in pairs(filters) do
|
for _, filter in pairs(filters) do
|
||||||
local chunk_pollution = surface.get_pollution(filter.position)
|
local chunk_pollution = surface.get_pollution(filter.position)
|
||||||
if chunk_pollution > 0 then
|
if chunk_pollution > 0 then
|
||||||
local to_insert = math.min(chunk_pollution, 4)
|
local suctionRate = getSuctionRate(filter)
|
||||||
local inserted_amount = filter.insert_fluid({name="pollution", amount=to_insert})
|
game.print(suctionRate)
|
||||||
surface.pollute(filter.position, -inserted_amount)
|
if suctionRate > 0 then
|
||||||
|
local to_insert = math.min(chunk_pollution, suctionRate)
|
||||||
|
local inserted_amount = filter.insert_fluid({name="pollution", amount=to_insert})
|
||||||
|
surface.pollute(filter.position, -inserted_amount)
|
||||||
|
game.print("Inserted: " .. inserted_amount)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -52,23 +44,109 @@ function chunkToPosition(chunk)
|
||||||
return {x=chunk.x *32, y=chunk.y*32}
|
return {x=chunk.x *32, y=chunk.y*32}
|
||||||
end
|
end
|
||||||
|
|
||||||
function movePollution(chunkFrom, chunkTo, amount)
|
function movePollution(surface, chunkFrom, chunkTo, amount)
|
||||||
|
amount = math.min(amount, surface.get_pollution(chunkToPosition(chunkFrom)))
|
||||||
|
surface.pollute(chunkToPosition(chunkFrom), -amount)
|
||||||
|
surface.pollute(chunkToPosition(chunkTo), amount)
|
||||||
|
return amount
|
||||||
end
|
end
|
||||||
|
|
||||||
function getPurificationRate(entity)
|
function getBasePurificationRate(entity)
|
||||||
-- TODO implement
|
-- Depends mostly on recipe. Should be multiplied by crafting speed to achieve actual max purification rate
|
||||||
-- 0 if not able to run
|
if entity.name == "air-filter-machine-1" then
|
||||||
-- based on crafting speed and amount of power (if possible)
|
return 4 -- 4 max pollution cleaning per second among mk1 recipes
|
||||||
|
elseif entity.name == "air-filter-machine-2" or entity.name == "air-filter-machine-3" then
|
||||||
|
return 6 -- 6 max pollution cleaning for mk2 and mk3 recipes (liquid)
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function getSucktionRate(entity)
|
function getSuctionRate(entity)
|
||||||
return 1/4 * getPurificationRate(entity)
|
if not entity.is_crafting() and getSpaceForPollution(entity) == 0 then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return getBasePurificationRate(entity) * entity.crafting_speed * energyCraftingModifier(entity)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
function energyCraftingModifier(entity)
|
||||||
-- Make more efficient
|
-- Approximation to speed modifier for machine running out of power
|
||||||
-- flow rate of machines
|
if entity.electric_buffer_size then
|
||||||
|
return entity.energy / entity.electric_buffer_size
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function getSpaceForPollution(entity)
|
||||||
|
if #entity.fluidbox < 1 then
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
local capacity = entity.fluidbox.get_capacity(1)
|
||||||
|
local pollutionFluid = entity.fluidbox[1]
|
||||||
|
local pollution = 0
|
||||||
|
if pollutionFluid then
|
||||||
|
pollution = pollutionFluid.amount
|
||||||
|
end
|
||||||
|
return capacity - pollution
|
||||||
|
end
|
||||||
|
|
||||||
|
function generateFunctions()
|
||||||
|
local functions = {}
|
||||||
|
|
||||||
|
local function test(event)
|
||||||
|
for _, surface in pairs(game.surfaces) do
|
||||||
|
local filters = surface.find_entities_filtered {
|
||||||
|
name = {"air-filter-machine-1", "air-filter-machine-2", "air-filter-machine-3"}
|
||||||
|
}
|
||||||
|
updateAirFilters(surface, filters, 0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(functions, test)
|
||||||
|
|
||||||
|
|
||||||
|
return functions
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function spreadOverTicks(functions, interval)
|
||||||
|
local tickMap = {}
|
||||||
|
local funcs = {}
|
||||||
|
for index, f in pairs(functions) do
|
||||||
|
-- amount of functions to be inserted in this tick update to fit them all in the remaining interval
|
||||||
|
local functionsPerTick = math.ceil((#functions - index) / (interval - #tickMap - 1))
|
||||||
|
table.insert(funcs, f)
|
||||||
|
if #funcs >= functionsPerTick then
|
||||||
|
table.insert(tickMap, funcs)
|
||||||
|
funcs = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if #funcs > 0 then
|
||||||
|
table.insert(tickMap, funcs)
|
||||||
|
funcs = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local function onTick(event)
|
||||||
|
local step = (event.tick % interval) + 1
|
||||||
|
game.print("Step: " .. step)
|
||||||
|
local funcs = tickMap[step]
|
||||||
|
if funcs ~= nil then
|
||||||
|
for _, f in pairs(funcs) do
|
||||||
|
f(event)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return onTick
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
script.on_load(function()
|
||||||
|
local functions = generateFunctions()
|
||||||
|
local onTick = spreadOverTicks(functions, 30)
|
||||||
|
|
||||||
|
script.on_event(defines.events.on_tick, onTick)
|
||||||
|
end)
|
||||||
|
|
||||||
script.on_event(defines.events.on_tick, onTick)
|
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ data:extend({
|
||||||
enabled = "true",
|
enabled = "true",
|
||||||
ingredients =
|
ingredients =
|
||||||
{
|
{
|
||||||
{ type = "fluid", name = "pollution", amount = 25, fluidbox_index = 1 },
|
{ type = "fluid", name = "pollution", amount = 20, fluidbox_index = 1 },
|
||||||
{ type = "item", name = "unused-air-filter", amount = 1 },
|
{ type = "item", name = "unused-air-filter", amount = 1 },
|
||||||
},
|
},
|
||||||
results = { { type = "item", name = "used-air-filter", amount = 1 } }
|
results = { { type = "item", name = "used-air-filter", amount = 1 } }
|
||||||
|
@ -117,7 +117,7 @@ data:extend({
|
||||||
category = "air-filtering-advanced",
|
category = "air-filtering-advanced",
|
||||||
subgroup = "raw-material",
|
subgroup = "raw-material",
|
||||||
order = "f[plastic-bar]-f[filter-air]",
|
order = "f[plastic-bar]-f[filter-air]",
|
||||||
energy_required = 0.5,
|
energy_required = 1,
|
||||||
enabled = "true",
|
enabled = "true",
|
||||||
ingredients =
|
ingredients =
|
||||||
{
|
{
|
||||||
|
|
Reference in New Issue