Advertisement
sunbro3

Historically Accurate Tree Restoration [Factorio]

Nov 28th, 2020 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. /c
  2. --[[ HISTORICALLY ACCURATE TREE RESTORATION ]]
  3. local TREES = {"tree-01","tree-02","tree-02-red","tree-03","tree-04","tree-05","tree-06","tree-06-brown","tree-07","tree-08","tree-08-brown","tree-08-red","tree-09","tree-09-brown","tree-09-red"}
  4. local function bounding_box_from_gps_tags(s)
  5.   local x1 = math.huge
  6.   local y1,x2,y2 = x1,-x1,-x1
  7.   for x,y in s:gmatch("%[gps=([+-]?%d+),([+-]?%d+)%]") do
  8.     x1 = math.min(x1, x+0)
  9.     y1 = math.min(y1, y+0)
  10.     x2 = math.max(x2, x+0)
  11.     y2 = math.max(y2, y+0)
  12.   end
  13.   return { left_top = {x=x1,y=y1}, right_bottom = {x=x2,y=y2} }
  14. end
  15. local function chunks_at_least_partly_inside(surface, bb)
  16.   local result = {}
  17.   for c in surface.get_chunks() do
  18.     if c.area.left_top.x <= bb.right_bottom.x and
  19.        c.area.left_top.y <= bb.right_bottom.y and
  20.        c.area.right_bottom.x >= bb.left_top.x and
  21.        c.area.right_bottom.y >= bb.left_top.y
  22.     then
  23.       result[#result+1] = c
  24.     end
  25.   end
  26.   return result
  27. end
  28. local function go(s)
  29.   local surface = game.player.surface
  30.   local chunks = chunks_at_least_partly_inside(surface, bounding_box_from_gps_tags(s))
  31.   if #chunks > 0 then
  32.     surface.regenerate_entity(TREES, chunks)
  33.     game.print("Restored default trees to " .. #chunks .. " chunks.")
  34.   end
  35. end
  36. go ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement