Advertisement
TheProdigy22

deconstruct

Mar 13th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. local args = {...}
  2.  
  3. if #args ~= 4 then
  4.     print("Usage:")
  5.     print("deconstruct <width> <length> <height> <file>")
  6.     return
  7. end
  8.  
  9. local blueprint = {}
  10. blueprint.x = tonumber(args[1])
  11. blueprint.y = tonumber(args[2])
  12. blueprint.h = tonumber(args[3])
  13. blueprint.slots = {}
  14. blueprint.tcode = {}
  15.  
  16. local userFile = shell.resolve(args[4])
  17.  
  18. if fs.exists(userFile) then
  19.     print("Error: A file by that name already exists")
  20.     return
  21. end
  22.  
  23. local bpFile = fs.open(userFile,"w")
  24.  
  25. --returns whatever slot the block was put into
  26. function scan(up)
  27.     local slotCounts = {}
  28.     for i=1,16 do
  29.         slotCounts[i] = turtle.getItemCount(i)
  30.     end
  31.     if up then
  32.         turtle.digUp()
  33.     else
  34.         turtle.dig()
  35.     end
  36.     local slot = 0
  37.     for i=1,16 do
  38.         if turtle.getItemCount(i) > slotCounts[i] then
  39.             slot = i
  40.         end
  41.     end
  42.     if slot > 0 then
  43.         --if slot hasn't been used yet
  44.         if not blueprint.slots[slot] then
  45.             --get item name for slot
  46.             local item = turtle.getItemDetail(slot).name
  47.             blueprint.slots[slot] = {name = item, count = 0}
  48.         end
  49.         --add 1 to the slot count
  50.         blueprint.slots[slot].count = turtle.getItemCount(slot)
  51.     end
  52.     --add the slot number to the tcode
  53.     return slot
  54. end
  55.  
  56. local i = 1
  57. local right = true
  58. blueprint.tcode[i] = scan()
  59. i = i + 1
  60. turtle.forward()
  61. for h=1,blueprint.h do
  62.     for x=1,blueprint.x do
  63.         for y=1,blueprint.y - 1 do
  64.             blueprint.tcode[i] = scan()
  65.             turtle.forward()
  66.             i = i + 1
  67.         end
  68.         if x < blueprint.x then
  69.             if right then
  70.                 turtle.turnRight()
  71.                 blueprint.tcode[i] = scan()
  72.                 i = i + 1
  73.                 turtle.forward()
  74.                 turtle.turnRight()
  75.                 right = false
  76.             else
  77.                 turtle.turnLeft()
  78.                 blueprint.tcode[i] = scan()
  79.                 i = i + 1
  80.                 turtle.forward()
  81.                 turtle.turnLeft()
  82.                 right = true
  83.             end
  84.         end
  85.     end
  86.     if h < blueprint.h then
  87.         blueprint.tcode[i] = scan(true)
  88.         i = i + 1
  89.         turtle.turnRight()
  90.         turtle.turnRight()
  91.         turtle.up()
  92.     end
  93. end
  94.  
  95. local output = textutils.serialize(blueprint)
  96. bpFile.write(output)
  97. bpFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement