Created
October 21, 2024 15:37
-
-
Save morsk/b1c25e0a859ffe1ced39efa27d660d08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/sc --[[ undo landfill 3.0, for Patch 2.0 ]] | |
--[[ | |
EXPERIMENTAL!!! I took my 1.1 script and tried to make it work in 2.0 in a hurry!! | |
Truly undoes landfill. Regenerates the original chunks in a separate surface to see which water/ | |
shore tiles used to be under the landfill, copies from those tiles, and cleans up afterwards. | |
Ends in an unterminated quote, intended to contain map pings. | |
Will select a box region around all pings. | |
Any number of pings 2+ will work, but it's most intuitive to ping 2 opposing corners, or 4 sides. | |
Insert pings into the command line, and then a closing quote " before using. | |
--]] | |
local function bounding_box_from_gps_tags(s) | |
local a,b,c,d,m,M,n=1/0,1/0,-1/0,-1/0,math.min,math.max | |
for x,y,z in s:gmatch("%[gps=([+-]?[%d%.]+),([+-]?[%d%.]+),?([^%]]*)%]")do a=m(a,x+0)b=m(b,y+0)c=M(c,x+0)d=M(d,y+0)n=z end | |
return{left_top={x=a,y=b},right_bottom={x=c,y=d}}, game.get_surface(#n>0 and n or 1) | |
end | |
local function count_in(t, s) | |
t[s] = (t[s] or 0) + 1 | |
end | |
local function go2(gps) | |
local bb, surface = bounding_box_from_gps_tags(gps) | |
local s2 = game.create_surface("undo_landfill", surface.map_gen_settings) | |
local new_water,counts = {},{} | |
for _,t in pairs(surface.find_tiles_filtered{area=bb, name="landfill"}) do | |
local pos = t.position | |
if surface.count_entities_filtered{collision_mask={"ghost","object","player","rail"}, area={left_top=pos, right_bottom={pos.x+1,pos.y+1}}} > 0 then | |
count_in(counts, "skipped") | |
else | |
new_water[#new_water+1] = { position=pos, name="" } | |
s2.request_to_generate_chunks(pos, 0) | |
end | |
end | |
s2.force_generate_chunk_requests() | |
for _,nw in pairs(new_water) do | |
nw.name = s2.get_tile(nw.position.x, nw.position.y).name | |
count_in(counts, nw.name) | |
end | |
surface.set_tiles(new_water) | |
game.player.print("undo landfill:") | |
for name,count in pairs(counts) do | |
game.player.print(" "..name..": "..count) | |
end | |
end | |
local function go(...) | |
local ok, result = pcall(go2, ...) | |
if not ok then game.player.print(result) end | |
if game.get_surface("undo_landfill") then game.delete_surface("undo_landfill") end | |
end | |
go " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment