The chunk iterator "iterates" through only 1 chunk (solved, it was a stupid typo - x instead of y. I use a qwertz keyboard so y is right on the left of x).
Code: Select all
pollution_multiplier_table = {
["grass-1"]=1, --changes tile name to pollution multiplier
["grass-2"]=1,
["grass-3"]=2,
["grass-4"]=3,
["dirt-4"]=4,
["dirt-6"]=5,
["dirt-7"]=6,
["dirt-5"]=7,
["dirt-3"]=8,
["dirt-1"]=9,
["dirt-2"]=10,
["red-desert-3"]=11,
["dry-dirt"]=9,
["sand-3"]=12,
["sand-2"]=13
}
terrain = {
["grass-1"]="grass-3", --changes tile name to tile name to set
["grass-2"]="grass-3",
["grass-3"]="grass-4",
["grass-4"]="dirt-4",
["dirt-4"]="dirt-6",
["dirt-6"]="dirt-7",
["dirt-7"]="dirt-5",
["dirt-5"]="dirt-3",
["dirt-3"]="dirt-1",
["dirt-1"]="dirt-2",
["dirt-2"]="red-desert-3",
["red-desert-3"]="sand-3",
["dry-dirt"]="dirt-2",
["sand-3"]="sand-2",
["sand-2"]="sand-1"
}
local tiles_to_set = {} --A table that will contain all tiles that will be set to another tile
script.on_event(defines.events.on_tick, function(event)
if event.tick % 300 == 0 then --every 5 seconds
for chunk in game.surfaces[1].get_chunks() do --!iterate through chunks (not working?)
local tile = game.surfaces[1].get_tile(chunk.x/32, chunk.y/32)
if pollution_multiplier_table[tile.name] ~= nil then
add_tiles_to_table(pollution_multiplier_table[tile.name], chunk, tile)
end
if #tiles_to_set > 0 then --set tiles if there are some to set
game.surfaces[1].set_tiles(tiles_to_set)
tiles_to_set = {} --remove everything from tiles_to_set
end
end
end
end)
function add_tiles_to_table(pollution_multiplier, chunk, tile)
local chunk_pollution = game.surfaces[1].get_pollution({chunk.x, chunk.y})
if chunk_pollution >= pollution_multiplier*72 then --*final value is 1008
local tile_name = terrain[tile.name]
for x = -16,16 do --Loop through chunk tile positions
for y = -16,16 do
local current_tile = game.surfaces[1].get_tile(tile.position.x+x, tile.position.y+y)
if terrain[current_tile.name] ~= nil then --if tile name is in the terrain table
table.insert(tiles_to_set, {name = tile_name, position = current_tile.position})
if not string.find(terrain[current_tile.name], "grass") then --if tile name to set isn't some kind of grass
game.surfaces[1].destroy_decoratives{area={current_tile.position, current_tile.position}} --remove decoratives (bushes, tall grass, etc.)
end
end
end
end
end
end