diff --git a/README.md b/README.md index 3373b84..2db84cc 100644 --- a/README.md +++ b/README.md @@ -7,18 +7,18 @@ This mod features three tiers of air filtering machines. These machines remove pollution from the air in their region by using air filters. Each consecutive tier has a larger radius and stronger filtering effect. Keep in mind that you will still need filters spread around your base at strategic locations to effectively counteract the spread of pollution. -#### Air filter machine 1 +### Air filter machine 1 You start with a basic passive air filter machine that cleans the air in its own [chunk](https://wiki.factorio.com/Map_structure#Chunk). It should help keep biter attacks at bay through the early game. -#### Air filter machine 2 +### Air filter machine 2 The upgraded version has a stronger filtering effect. In addition, by using a moderate amount of electricity this machine is able to pull in pollution from neighboring chunks in the shape of a diamond. This version has a radius of two chunks (in [manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry)). Note that a continuous amount of energy is consumed for this suction effect in addition to the cost of filtering the air. ![range_mark_2](https://github.com/JoeyDP/Factorio-Better-Air-Filtering/blob/master/res/radius_mk2.png?raw=true) -#### Air filter machine 3 +### Air filter machine 3 The third and final upgrade of the air filtering machine features a larger radius of three chunks along with more air filtering per second. -#### Filter Types +### Filter Types There are currently two types of filters: __expendable filters__ and __recyclable filters__. The first ones are easier to craft, but filter out less pollution and are destroyed upon use. Recyclable filters are more expensive but can be refreshed with a bit of coal to be used again. > Known issue: used air filters cannot be extracted from the first tier of air filter machines by inserters. Factorio does not feature inserters that can extract from burnt_result_inventory. diff --git a/better-air-filtering/changelog.txt b/better-air-filtering/changelog.txt index dd5b97c..4889ff9 100644 --- a/better-air-filtering/changelog.txt +++ b/better-air-filtering/changelog.txt @@ -7,3 +7,11 @@ Date: 2019.10.08 - Only English License: - Using MIT License. +--------------------------------------------------------------------------------------------------- +Version: 0.1.1 +Date: 2019.10.09 + Features: + - Pollution is now registered in the pollution statistics rather than the fluid statistics. + - Air filter machines now accept speed and efficiency modules or beacon effects. + Fixes: + - Invalid entity issue when removing air filter machine during update resolved. diff --git a/better-air-filtering/control.lua b/better-air-filtering/control.lua index f8bdf12..b9eaa50 100644 --- a/better-air-filtering/control.lua +++ b/better-air-filtering/control.lua @@ -38,13 +38,6 @@ function positionToChunk(position) return { x = math.floor(position.x / 32), y = math.floor(position.y / 32) } end -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 - function getBasePurificationRate(entity) -- Depends mostly on recipe (optimal recipe used per machine). Should be multiplied by crafting speed to achieve actual max purification rate if entity.name == "air-filter-machine-1" then @@ -176,10 +169,11 @@ function absorbChunk(chunk) -- game.print("To absorb: " .. toAbsorb) local totalInsertedAmount = 0.0 - for _, filter in pairs(chunk.filters) do + for _, filter in pairs(chunk:getFilters()) do local toInsert = (getAbsorptionRate(filter) / totalAbsorptionRate) * toAbsorb if toInsert > 0 then local insertedAmount = filter.insert_fluid({ name = "pollution", amount = toInsert }) + game.pollution_statistics.on_flow(filter.name, -insertedAmount) totalInsertedAmount = totalInsertedAmount + insertedAmount end end @@ -332,13 +326,14 @@ local FilteredChunk = { surface = nil, x = 0, y = 0, - filters = {}, + --filters = {} } function FilteredChunk:new (o) o = o or {} setmetatable(o, self) self.__index = self + o.filters = o.filters or {} return o end @@ -347,7 +342,6 @@ function createFilteredChunk(surface, x, y) chunk.surface = surface chunk.x = x chunk.y = y - chunk.filters = {} -- this statement, though it appears to have no effect, has a large impact on the saving of global state. return chunk end @@ -355,7 +349,25 @@ function FilteredChunk:equal(other) return self.surface.name == other.surface.name and self.x == other.x and self.y == other.y end +function FilteredChunk:getFilters() + local filters = {} + for _, filter in pairs(self.filters) do + if filter.valid then + table.insert(filters, filter) + end + end + self.filters = filters + return filters +end + function FilteredChunk:addToMap() + + --game.print("Active chunks before: ") + --for i, c in pairs(air_filtered_chunks) do + -- game.print(serpent.line(c)) + --end + --game.print(serpent.block(global.air_filtered_chunks_map)) + --game.print("Adding chunk to map") local chunkListX = global.air_filtered_chunks_map[self.surface.name] or {} local chunkListY = chunkListX[self.x] or {} @@ -364,23 +376,47 @@ function FilteredChunk:addToMap() chunkListX[self.x] = chunkListY global.air_filtered_chunks_map[self.surface.name] = chunkListX table.insert(air_filtered_chunks, self) + + + --game.print("Active chunks after: ") + --for i, c in pairs(air_filtered_chunks) do + -- game.print(serpent.line(c)) + --end + --game.print(serpent.block(global.air_filtered_chunks_map)) end function FilteredChunk:removeFromMap() --game.print("Removing chunk from map") - table.remove(global.air_filtered_chunks_map[self.surface.name][self.x], self.y) + global.air_filtered_chunks_map[self.surface.name][self.x][self.y] = nil + for i, c in pairs(air_filtered_chunks) do if self:equal(c) then - table.remove(air_filtered_chunks, i) --game.print("Removing chunk from list") + table.remove(air_filtered_chunks, i) break end end + + --local i = 1 + --while i <= #air_filtered_chunks do + -- local c = air_filtered_chunks[i] + -- if self:equal(c) then + -- --game.print("Removing chunk from list") + -- table.remove(air_filtered_chunks, i) + -- else + -- i = i + 1 + -- end + --end + + --game.print("Remaining chunks: ") + --for _, c in pairs(air_filtered_chunks) do + -- game.print(serpent.line(c)) + --end end function FilteredChunk:getTotalAbsorptionRate() local totalAbsorptionRate = 0.0 - for _, filter in pairs(self.filters) do + for _, filter in pairs(self:getFilters()) do local absorptionRate = getAbsorptionRate(filter) totalAbsorptionRate = totalAbsorptionRate + absorptionRate end @@ -389,7 +425,7 @@ end function FilteredChunk:getTotalSuctionRate(distance) local totalSuctionRate = 0.0 - for _, filter in pairs(self.filters) do + for _, filter in pairs(self:getFilters()) do if inRadius(filter, distance) then local suctionRate = getSuctionRate(filter) totalSuctionRate = totalSuctionRate + suctionRate @@ -498,6 +534,7 @@ function onEntityRemoved(event) if pollution > 0 then --game.print("Dispersing " .. pollution .. " pollution back") event.entity.surface.pollute(event.entity.position, pollution) + game.pollution_statistics.on_flow(event.entity.name, pollution) end end @@ -522,6 +559,7 @@ function preEntityRemoved(event) if pollution > 0 then --game.print("Dispersing " .. pollution .. " pollution back") event.entity.surface.pollute(event.entity.position, pollution) + game.pollution_statistics.on_flow(event.entity.name, pollution) end end diff --git a/better-air-filtering/info.json b/better-air-filtering/info.json index 2459ff5..e39f332 100644 --- a/better-air-filtering/info.json +++ b/better-air-filtering/info.json @@ -1,6 +1,6 @@ { "name": "better-air-filtering", - "version": "0.1.0", + "version": "0.1.1", "title": "Better Air Filtering", "author": "Joey De Pauw", "contact": "joeydepauw@gmail.com", diff --git a/better-air-filtering/locale/en/air-filtering.cfg b/better-air-filtering/locale/en/air-filtering.cfg index 571cc6f..93f5a47 100644 --- a/better-air-filtering/locale/en/air-filtering.cfg +++ b/better-air-filtering/locale/en/air-filtering.cfg @@ -5,8 +5,8 @@ air-filter-machine-3=Air filter machine 3 [entity-description] air-filter-machine-1=Passive air filter that reduces pollution in the current chunk using air filters. -air-filter-machine-2=A stronger air filter that uses electricity to pull pollution towards it from a 2 chunk radius. -air-filter-machine-3=An even stronger air filter, capable of cleaning pollution in a 3 chunk radius. +air-filter-machine-2=An air filter machine that uses electricity to pull pollution towards it from a 2 chunk radius. +air-filter-machine-3=Powerful air filter, capable of cleaning pollution in a 3 chunk radius. [item-name] diff --git a/better-air-filtering/migrations/0.1.1.lua b/better-air-filtering/migrations/0.1.1.lua new file mode 100644 index 0000000..c456141 --- /dev/null +++ b/better-air-filtering/migrations/0.1.1.lua @@ -0,0 +1,9 @@ +for surfaceName, chunkListX in pairs(global.air_filtered_chunks_map) do + for x, chunkListY in pairs(chunkListX) do + for y, chunk in pairs(chunkListY) do + if chunk.filters == nil or #chunk.filters == 0 then + global.air_filtered_chunks_map[surfaceName][x][y] = nil + end + end + end +end diff --git a/better-air-filtering/prototypes/entity.lua b/better-air-filtering/prototypes/entity.lua index bb51cb1..2147e5b 100644 --- a/better-air-filtering/prototypes/entity.lua +++ b/better-air-filtering/prototypes/entity.lua @@ -72,7 +72,10 @@ data:extend({ fixed_recipe = "filter-air", ingredient_count = 1, return_ingredients_on_change = true, - module_slots = 0, + module_specification = + { + module_slots = 0 + }, allowed_effects=nil }, { @@ -162,9 +165,12 @@ data:extend({ energy_usage = "50kW", fixed_recipe = "filter-air2", ingredient_count = 2, + module_specification = + { + module_slots = 2 + }, + allowed_effects = {"consumption", "speed"}, return_ingredients_on_change = true, - module_slots = 0, - allowed_effects=nil }, { type = "assembling-machine", @@ -251,8 +257,12 @@ data:extend({ energy_usage = "100kW", fixed_recipe = "filter-air2", ingredient_count = 2, + module_specification = + { + module_slots = 3 + }, + allowed_effects = {"consumption", "speed"}, return_ingredients_on_change = true, module_slots = 0, - allowed_effects=nil } }) diff --git a/better-air-filtering/prototypes/recipe.lua b/better-air-filtering/prototypes/recipe.lua index 16ba3af..fadb072 100644 --- a/better-air-filtering/prototypes/recipe.lua +++ b/better-air-filtering/prototypes/recipe.lua @@ -109,6 +109,7 @@ data:extend({ type = "recipe", name = "filter-air", hide_from_player_crafting = true, + hide_from_stats = true, icons = { { icon = "__base__/graphics/icons/fluid/pollution.png" @@ -135,6 +136,7 @@ data:extend({ type = "recipe", name = "filter-air2", hide_from_player_crafting = true, + hide_from_stats = true, icons = { { icon = "__base__/graphics/icons/fluid/pollution.png" @@ -163,6 +165,7 @@ data:extend({ type = "recipe", name = "liquid-pollution", hide_from_player_crafting = true, + hide_from_stats = true, category = "air-filtering-advanced", subgroup = "raw-material", order = "c[filter-air]",