Advertisement
Guest User

xerox.lua

a guest
Apr 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.45 KB | None | 0 0
  1. local function Key2Coords(k)
  2.   local i = k:find(",")
  3.   local j = k:find(",",(i or -1) +1)
  4.   if not i or not j then
  5.     return nil
  6.   end
  7.  
  8.   local v = {}
  9.   v.x = tonumber(k:sub(1,i-1))
  10.   v.y = tonumber(k:sub(i+1,j-1))
  11.   v.z = tonumber(k:sub(j+1,-1))
  12.   return v,v.x,v.y,v.z
  13. end
  14.  
  15. local function Coords2Key(x,y,z)
  16.   if type(x) == "table" then
  17.     z = x.z
  18.     y = x.y
  19.     x = x.x
  20.   end
  21.   return tostring(x) .. "," ..
  22.          tostring(y) .. "," ..
  23.          tostring(z)
  24. end
  25.  
  26. local function LoadSchematic(filename)
  27.   local f = fs.open(filename,"r")
  28.   if not f then
  29.     return {}
  30.   end
  31.   local data = textutils.unserialize(
  32.                  f.readAll()
  33.                )
  34.   f.close()
  35.   return data or {}
  36. end
  37.  
  38. local function SaveSchematic(filename,schematic)
  39.   local f = fs.open(filename,"w")
  40.   f.write(textutils.serialize(schematic))
  41.   f.close()
  42. end
  43.  
  44. local function Go(n)
  45.   for i=1,(n or 1) do
  46.     while not turtle.forward() do
  47.       print("Unable to move forward")
  48.       sleep(1)
  49.     end
  50.   end
  51. end
  52.  
  53. function CreateSchematic(width,
  54.                          height,
  55.                          depth,
  56.                          filename)
  57.   if not width or
  58.      not height or
  59.      not depth then
  60.     print("Usage:")
  61.     print("CreateSchematic(x,y,z,filename)")
  62.     print("x,y,z are the dimensions")
  63.     print("  turtle is placed on top of")
  64.     print("  the NW corner of the 3D region")
  65.     print("filename is the name of the file")
  66.     print("to be created by the scan")
  67.     return
  68.   end
  69.  
  70.   local schematic = LoadSchematic(filename)
  71.   if schematic.complete then
  72.     error("File already exists")
  73.   end
  74.  
  75.   for y = height,1,-1 do
  76.     for x = 1,width do
  77.       for z = 1,depth do
  78.         local s,data = turtle.inspectDown()
  79.         if s then
  80.           if turtle.digDown() then
  81.             local t = {}
  82.             t.name = data.name
  83.             t.half = data.state.half
  84.             t.facing = data.state.facing
  85.            
  86.             local k = Coords2Key(x,y,z)
  87.             schematic[k] = t
  88.             SaveSchematic(filename,schematic)
  89.           end
  90.         end
  91.         if z < depth then
  92.           Go(1)
  93.         end
  94.       end --end for z
  95.       --turn around and go back
  96.       turtle.turnRight()
  97.       turtle.turnRight()
  98.       Go(depth-1)
  99.       if x < width then
  100.         --move to next column
  101.         turtle.turnLeft()
  102.         Go(1)
  103.         turtle.turnLeft()
  104.       else
  105.         turtle.turnRight()
  106.         Go(width-1)
  107.         turtle.turnRight()
  108.       end
  109.     end --end for x
  110.     while not turtle.down() do
  111.       print("Can't move down")
  112.       sleep(1)
  113.     end
  114.   end --end for y
  115.   schematic.complete = true
  116.   SaveSchematic(filename,schematic)
  117.   print("Done!")
  118. end
  119.  
  120. function PostProcessSchematic(filename)
  121.   local schematic = LoadSchematic(filename)
  122.   if not schematic.complete then
  123.     error("Invalid schematic file")
  124.   end
  125.   local mats = {}
  126.   local size = {x=1,y=1,z=1}
  127.   --local layers = {}
  128.   for k,v in pairs(schematic) do
  129.     if type(k) == "string" then
  130.       local pos = Key2Coords(k)
  131.       if pos and v.name then
  132.         --update mats table
  133.         if not mats[v.name] then
  134.           mats[v.name] =
  135.           {
  136.             qty = 0,
  137.             stateData = not not
  138.               --(v.half or v.facing)
  139.               v.facing
  140.           }
  141.         end
  142.         mats[v.name].qty =
  143.           mats[v.name].qty + 1
  144.         ----update layers table
  145.         --if not layers[v.pos.y] then
  146.         --  layers[v.pos.y] = {}
  147.         --end
  148.        
  149.      
  150.      
  151.         --update size
  152.         for dir,_ in pairs(size) do
  153.           --dir = "x", "y", or "z"
  154.           if size[dir] < pos[dir] then
  155.             size[dir] = pos[dir]
  156.           end
  157.         end
  158.        
  159.       end --end if key is a position
  160.     end --end if type == string
  161.   end --end for
  162.   --convert mats set into sorted array
  163.   local materials = {}
  164.   local i = 1
  165.   for k,v in pairs(mats) do
  166.     materials[i] = v
  167.     materials[i].name = k
  168.     i = i + 1
  169.   end
  170.  
  171.   table.sort(materials,
  172.     function(a,b)
  173.       if a.stateData ~= b.stateData then
  174.         return b.stateData
  175.       end
  176.       return a.name < b.name
  177.     end)
  178.  
  179.  
  180.   schematic.materials = materials
  181.   schematic.size = size
  182.   SaveSchematic(filename,schematic)
  183. end
  184.  
  185. local function PlaceBlock(blockdata)
  186.   --find item with same name
  187.   local selected
  188.   while not selected do
  189.     for i=1,16 do
  190.       local details = turtle.getItemDetail(i)
  191.       if details and details.name == blockdata.name then
  192.         turtle.select(i)
  193.         selected = true
  194.       end
  195.     end
  196.     if not selected then
  197.       print("Out of: " .. blockdata.name)
  198.       sleep(5)
  199.     end
  200.   end
  201.  
  202.   --place the block
  203.   while not turtle.placeDown() do
  204.     print("Can't place block")
  205.     sleep(1)
  206.   end
  207.  
  208.   --is the block facing the right way?
  209.   if blockdata.facing then
  210.     local s,x = turtle.inspectDown()
  211.     if not s then --something is messing with us
  212.       return
  213.     end
  214.  
  215.  
  216.     if blockdata.facing and
  217.        blockdata.facing ~= x.state.facing then
  218.       --spin in place
  219.       for i=1,4 do
  220.         turtle.turnRight()
  221.         if x and x.state and
  222.             blockdata.facing ~= x.state.facing then
  223.           turtle.digDown()
  224.           while not turtle.placeDown() do
  225.             print("Can't place block")
  226.             sleep(1)
  227.           end
  228.         end
  229.         s,x = turtle.inspectDown()
  230.  
  231.       end
  232.     end
  233.  
  234.   end
  235. end
  236.  
  237. function Build(filename)
  238.   local schematic = LoadSchematic(filename)
  239.   if not schematic.materials then
  240.     PostProcessSchematic(filename)
  241.   end
  242.  
  243.   local size = schematic.size
  244.   local width = size.x
  245.   local height = size.y
  246.   local depth = size.z
  247.  
  248.   for y = 1,height do
  249.     --move up to new level
  250.     while not turtle.up() do
  251.       print("Can't move up")
  252.       sleep(1)
  253.     end
  254.  
  255.     for x = 1,width do
  256.       for z = 1,depth do
  257.         local k = Coords2Key(x,y,z)
  258.         local blockdata = schematic[k]
  259.         if blockdata then
  260.           PlaceBlock(blockdata)
  261.         end
  262.         if z < depth then
  263.           Go(1)
  264.         end
  265.       end --end for z
  266.       --turn around and go back
  267.       turtle.turnRight()
  268.       turtle.turnRight()
  269.       Go(depth-1)
  270.       if x < width then
  271.         --move to next column
  272.         turtle.turnLeft()
  273.         Go(1)
  274.         turtle.turnLeft()
  275.       else
  276.         turtle.turnRight()
  277.         Go(width-1)
  278.         turtle.turnRight()
  279.       end
  280.     end --end for x
  281.   end --end for y
  282.   print("Done!")
  283.   turtle.select(1)
  284. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement