Advertisement
brensen11

Scan

Aug 20th, 2022 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.59 KB | None | 0 0
  1. local m = require("blib")
  2. local lastTurn = "Left"
  3. local FileName = ""
  4. local Length
  5. local Width
  6. local Height
  7.  
  8. local map = {x = 1, y = 0, z = 1, xf = 0, yf = 1}
  9.  
  10. function MapTurnRight()
  11.     turtle.turnRight()
  12.     if map.xf == 1 then
  13.         map.xf = 0
  14.         map.yf = -1
  15.     elseif map.xf == -1 then
  16.         map.xf = 0
  17.         map.yf = 1
  18.     elseif map.yf == 1 then
  19.         map.yf = 0
  20.         map.xf = 1
  21.     elseif map.yf == -1 then
  22.         map.yf = 0
  23.         map.xf = -1
  24.     end
  25. end
  26.  
  27. function MapTurnLeft()
  28.     turtle.turnLeft()
  29.     if map.xf == 1 then
  30.         map.xf = 0
  31.         map.yf = 1
  32.     elseif map.xf == -1 then
  33.         map.xf = 0
  34.         map.yf = -1
  35.     elseif map.yf == 1 then
  36.         map.yf = 0
  37.         map.xf = -1
  38.     elseif map.yf == -1 then
  39.         map.yf = 0
  40.         map.xf = 1
  41.     end
  42. end
  43.  
  44. function MapForward()
  45.     turtle.forward()
  46.     if map.xf == 1 then
  47.         map.x = map.x + 1
  48.     elseif map.xf == -1 then
  49.         map.x = map.x - 1
  50.     elseif map.yf == 1 then
  51.         map.y = map.y + 1
  52.     elseif map.yf == -1 then
  53.         map.y = map.y - 1
  54.     end
  55. end
  56.  
  57. function MapUp()
  58.     turtle.up()
  59.     map.z = map.z + 1
  60. end
  61.  
  62.  
  63. -- Example ANY File
  64. -- {
  65.     -- Dimensions
  66.     -- "X" : 2
  67.     -- "Y" : 2
  68.     -- "Z" : 2
  69.     -- List of blocks needed
  70.     -- "list" : {"name" : 1, "cobblestone" : 5, "name again" : 7} etc. for k, v in pairs(list) or whatever
  71.     -- "1,1,1" : "minecraft:cobblestone"
  72.     -- "2,1,1" : "Something else blockname idk"
  73. -- }
  74.  
  75. local scan = {}
  76.  
  77. function PlaceBlock()
  78.     local success, data = turtle.inspect()
  79.     local fx = map.x
  80.     local fy = map.y
  81.     local fz = map.z
  82.  
  83.     if(map.xf ~= 0) then
  84.         fx = fx + map.xf
  85.     else
  86.         fy = fy + map.yf
  87.     end
  88.  
  89.  
  90.     local key = tostring(fx) .. "," .. tostring(fy) .. "," .. tostring(fz)
  91.     print("key: " .. key)
  92.     if success then
  93.         scan[key] = data.name
  94.         print("val: " .. data.name)
  95.     else
  96.         scan[key] = nil
  97.         print("val: nil")
  98.     end
  99.     turtle.dig()
  100. end
  101.  
  102. function ScanAndDigUp()
  103.     local success, data = turtle.inspectUp()
  104.     local fx = map.x
  105.     local fy = map.y
  106.     local fz = map.z + 1
  107.  
  108.     local key = tostring(fx) .. "," .. tostring(fy) .. "," .. tostring(fz)
  109.     print("key: " .. key)
  110.     if success then
  111.         scan[key] = data.name
  112.         print("val: " .. data.name)
  113.     else
  114.         scan[key] = nil
  115.         print("val: nil")
  116.     end
  117.     turtle.digUp()
  118. end
  119.  
  120. function dump(o)
  121.     if type(o) == 'table' then
  122.        local s = '{ '
  123.        for k,v in pairs(o) do
  124.           if type(k) ~= 'number' then k = '"'..k..'"' end
  125.           s = s .. '['..k..'] = ' .. dump(v) .. ','
  126.        end
  127.        return s .. '} '
  128.     else
  129.        return tostring(o)
  130.     end
  131.  end
  132.  
  133.  
  134. --- Scanning Process ---
  135. function Start()
  136.  
  137.     PlaceBlock() -- The turtle starts outside the square, as such, it enters at the start, and the Y index
  138.     MapForward()             -- is actually decremented by one, since mining on the x axis will manage one block off each
  139.                  -- level change
  140.  
  141.  
  142.     for z = 1, Height do
  143.        
  144.         for x = 1, Width do                         -- starting length cuts one turn off, but not one column off. see below
  145.  
  146.             for y = 1, Length - 1 do                -- width cuts one length down off the next
  147.  
  148.                 -- check for fuel
  149.                 if(not m.CheckFuel(10)) then
  150.                     return Shutdown("Out of Fuel!")
  151.                 end
  152.                
  153.                 -- check if inventory is full
  154.                 if(reqChests == true) then
  155.                     if(m.InventoryFull() == true) then
  156.                         if(m.DumpItemsChest() == false) then
  157.                             return Shutdown("Out of chests!")
  158.                         end
  159.                     end
  160.                 end
  161.  
  162.                 PlaceBlock()
  163.                 MapForward()
  164.             end
  165.  
  166.             if( Width - x ~= 0) then -- Technically on the last column, we want to continue forward so we can't do 'Width - 1', but don't turn into the next column
  167.                 if(lastTurn == "Right") then -- if we're on an even column, turn left
  168.                     MapTurnLeft()
  169.                     PlaceBlock()
  170.                     MapForward()
  171.                     MapTurnLeft()
  172.                     lastTurn = "Left"
  173.                 else -- odd columns (start is 1 btw) turn right!
  174.                     MapTurnRight()
  175.                     PlaceBlock()
  176.                     MapForward()
  177.                     MapTurnRight()
  178.                     lastTurn = "Right"
  179.                 end
  180.             end
  181.  
  182.         end
  183.  
  184.         if( Height - z ~= 0 ) then
  185.             ScanAndDigUp()
  186.             MapUp()
  187.             MapTurnRight()
  188.             MapTurnRight()
  189.  
  190.         end
  191.        
  192.     end
  193.  
  194.     return 1
  195. end
  196.  
  197. function Main()
  198.     local response
  199.     print("Welcome to the Minecraft file scanning program")
  200.     print("please enter the X, Y, and Z lengths of the area you'd like to scan")
  201.     print("Length: ")
  202.     Length = read()
  203.     scan.Y = Length
  204.     print("Width: ")
  205.     Width = read()
  206.     scan.X = Width
  207.     print("Height: ")
  208.     Height = read()
  209.     scan.Z = Height
  210.     print("What do you want to call this build file? (e.g. Sand_Castle, Cool-Cannon)")
  211.     print("(Careful! it will overwrite existing files!)")
  212.     FileName = read()        
  213.     Start()
  214.     local data = textutils.serialize(scan)
  215.     print(data)
  216.     print("Data written!")
  217.     local f = fs.open(FileName .. ".b", "w")
  218.     f.write(data)
  219.     f.close()
  220.  
  221.     print("Finished!")
  222. end
  223. Main() -- testing post request lol
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement