Skip to content

Instantly share code, notes, and snippets.

@morsk
Created February 7, 2021 02:31
Show Gist options
  • Save morsk/96fe30b3faa7d2009e483c5c5eea9541 to your computer and use it in GitHub Desktop.
Save morsk/96fe30b3faa7d2009e483c5c5eea9541 to your computer and use it in GitHub Desktop.
Factorio Chunk Remover. Put map pings inside the double quotes at the end to select a region.
/c
--[[ CHUNK REMOVER --]]
local SAFE_PLAYER_DISTANCE = 64 --[[ must be >= 32 --]]
local function bounding_box_from_gps_tags(s)
local x1 = math.huge
local y1,x2,y2 = x1,-x1,-x1
for x,y in s:gmatch("%[gps=([+-]?%d+),([+-]?%d+)%]") do
x1 = math.min(x1, x+0)
y1 = math.min(y1, y+0)
x2 = math.max(x2, x+0)
y2 = math.max(y2, y+0)
end
return { left_top = {x=x1,y=y1}, right_bottom = {x=x2,y=y2} }
end
local function move_unsafe_players(surface, bb)
for _, p in pairs(game.players) do
if p.surface == surface then
if p.position.x > (bb.right_bottom.x - SAFE_PLAYER_DISTANCE) or
p.position.y > (bb.right_bottom.y - SAFE_PLAYER_DISTANCE) or
p.position.x < (bb.left_top.x + SAFE_PLAYER_DISTANCE) or
p.position.y < (bb.left_top.y + SAFE_PLAYER_DISTANCE)
then
p.teleport(p.force.get_spawn_position(surface))
game.print("Moved player "..p.name.." to spawn.")
end
end
end
end
local function chunks_wholly_outside(surface, bb)
local result = {}
for c in surface.get_chunks() do
if c.area.left_top.x > bb.right_bottom.x or
c.area.left_top.y > bb.right_bottom.y or
c.area.right_bottom.x < bb.left_top.x or
c.area.right_bottom.y < bb.left_top.y
then
result[#result+1] = c
end
end
return result
end
local function go(s)
local surface = game.player.surface
local bb = bounding_box_from_gps_tags(s)
move_unsafe_players(surface, bb)
local chunks = chunks_wholly_outside(surface, bb)
if #chunks > 0 then
for _,c in pairs(chunks) do
surface.delete_chunk(c)
end
game.print("Removed "..#chunks.." chunks from surface '"..surface.name.."'.")
end
end
go ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment