Advertisement
brensen11

Build

Aug 21st, 2022 (edited)
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. local m = require("blib")
  2. local map = {x = 1, y = 0, z = 1, xf = 0, yf = 1}
  3. local Length
  4. local Width
  5. local Height
  6. local data
  7.  
  8.  
  9. function MapTurnRight()
  10.     turtle.turnRight()
  11.     if map.xf == 1 then
  12.         map.xf = 0
  13.         map.yf = -1
  14.     elseif map.xf == -1 then
  15.         map.xf = 0
  16.         map.yf = 1
  17.     elseif map.yf == 1 then
  18.         map.yf = 0
  19.         map.xf = 1
  20.     elseif map.yf == -1 then
  21.         map.yf = 0
  22.         map.xf = -1
  23.     end
  24. end
  25.  
  26. function MapTurnLeft()
  27.     turtle.turnLeft()
  28.     if map.xf == 1 then
  29.         map.xf = 0
  30.         map.yf = 1
  31.     elseif map.xf == -1 then
  32.         map.xf = 0
  33.         map.yf = -1
  34.     elseif map.yf == 1 then
  35.         map.yf = 0
  36.         map.xf = -1
  37.     elseif map.yf == -1 then
  38.         map.yf = 0
  39.         map.xf = 1
  40.     end
  41. end
  42.  
  43. function MapForward()
  44.     turtle.forward()
  45.     if map.xf == 1 then
  46.         map.x = map.x + 1
  47.     elseif map.xf == -1 then
  48.         map.x = map.x - 1
  49.     elseif map.yf == 1 then
  50.         map.y = map.y + 1
  51.     elseif map.yf == -1 then
  52.         map.y = map.y - 1
  53.     end
  54. end
  55.  
  56. function MapBackward()
  57.     turtle.back()
  58.     if map.xf == 1 then
  59.         map.x = map.x - 1
  60.     elseif map.xf == -1 then
  61.         map.x = map.x + 1
  62.     elseif map.yf == 1 then
  63.         map.y = map.y - 1
  64.     elseif map.yf == -1 then
  65.         map.y = map.y + 1
  66.     end
  67. end
  68.  
  69. function MapUp()
  70.     turtle.up()
  71.     map.z = map.z + 1
  72. end
  73.  
  74.  
  75. -- Example ANY File
  76. -- {
  77.     -- Dimensions
  78.     -- "X" : 2
  79.     -- "Y" : 2
  80.     -- "Z" : 2
  81.     -- List of blocks needed
  82.     -- "list" : {"name" : 1, "cobblestone" : 5, "name again" : 7} etc. for k, v in pairs(list) or whatever
  83.     -- "1,1,1" : "minecraft:cobblestone"
  84.     -- "2,1,1" : "Something else blockname idk"
  85. -- }
  86.  
  87.  
  88. function PlaceBlock()
  89.     local fx = map.x
  90.     local fy = map.y
  91.     local fz = map.z
  92.  
  93.     if(map.xf ~= 0) then
  94.         fx = fx + map.xf
  95.     else
  96.         fy = fy + map.yf
  97.     end
  98.     local key = tostring(fx) .. "," .. tostring(fy) .. "," .. tostring(fz)
  99.     local blockName = data[key]
  100.     if blockName ~= nil then
  101.         if m.SelectItem(blockName) then
  102.             turtle.place()
  103.         end
  104.     else
  105.         return
  106.     end
  107.    
  108. end
  109.  
  110. function PlaceBlockDown()
  111.     local fx = map.x
  112.     local fy = map.y
  113.     local fz = map.z - 1
  114.     local key = tostring(fx) .. "," .. tostring(fy) .. "," .. tostring(fz)
  115.     local blockName = data[key]
  116.     if blockName ~= nil then
  117.         if m.SelectItem(blockName) then
  118.             turtle.placeDown()
  119.         end
  120.     else
  121.         return
  122.     end
  123.  
  124. end
  125.  
  126.  
  127.  
  128.  
  129. --- Building Process ---
  130. function Start()
  131.  
  132.     MapTurnRight()
  133.     MapTurnRight()           -- The turtle starts outside the square, as such, it enters at the start, and the Y index
  134.     MapBackward()            -- is actually decremented by one, since mining on the x axis will manage one block off each
  135.                              -- level change
  136.  
  137.  
  138.     for z = 1, Height do
  139.        
  140.         for x = 1, Width do                         -- starting length cuts one turn off, but not one column off. see below
  141.  
  142.             for y = 1, Length - 1 do                -- width cuts one length down off the next
  143.  
  144.                 -- check for fuel
  145.                 if(not m.CheckFuel(10)) then
  146.                     return Shutdown("Out of Fuel!")
  147.                 end        
  148.  
  149.                 MapBackward()
  150.                 PlaceBlock()
  151.             end
  152.  
  153.             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
  154.                 if(lastTurn == "Right") then -- if we're on an even column, turn left
  155.                     MapTurnLeft()
  156.                     MapBackward()
  157.                     PlaceBlock()
  158.                     MapTurnLeft()
  159.                     lastTurn = "Left"
  160.                 else -- odd columns (start is 1 btw) turn right!
  161.                     MapTurnRight()
  162.                     MapBackward()
  163.                     PlaceBlock()
  164.                     MapTurnRight()
  165.                     lastTurn = "Right"
  166.                 end
  167.             end
  168.  
  169.         end
  170.  
  171.        
  172.         MapUp()
  173.         PlaceBlockDown()
  174.         MapTurnRight()
  175.         MapTurnRight()
  176.  
  177.        
  178.        
  179.     end
  180.  
  181.     return 1
  182. end
  183.  
  184. function Main()
  185.     local response
  186.     print("Welcome to the Minecraft file building program")
  187.     print("please enter file you would like to build from")
  188.     local FileName = read()
  189.     local f = fs.open(FileName, "r") -- add nil safety
  190.     local serializedData = f.readAll()
  191.     data = textutils.unserialize(serializedData)
  192.     Length = data.Y
  193.     Width = data.X
  194.     Height = data.Z
  195.  
  196.     Start()
  197.  
  198.     print("Finished!")
  199. end
  200. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement