Paradox4624

makeFloor.lua

Feb 1st, 2022
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. sLight = ""
  2. sFloor = ""
  3.  
  4. function MoveBack()
  5.     while not turtle.back() do
  6.         print("Cant move back, attempting to clear...")
  7.         TurnAround()
  8.         ClearAhead()
  9.         TurnAround()
  10.     end
  11. end
  12.  
  13. function ClearAhead()
  14.     while turtle.detect() do
  15.         print("Found obstruction, removing...")
  16.         turtle.dig()
  17.         sleep(1)
  18.     end
  19.     local iCount = 0
  20.     while turtle.attack() do
  21.         if iCount % 5 == 0 then
  22.             print("Found mob in path, removing...")    
  23.         end
  24.         iCount = iCount + 1
  25.     end
  26. end
  27.  
  28. function TurnAround()
  29.     turtle.turnRight()
  30.     turtle.turnRight()
  31. end
  32.  
  33. function PlaceFloor()
  34.     while not GetFloor() do
  35.         print("Cant find floor object, waiting for more...")
  36.         sleep(5)
  37.     end
  38.     local bSuccess, oBlockData = turtle.inspectDown()
  39.     if bSuccess then
  40.         if oBlockData.name == sFloor then
  41.             --found flooring already in place, skipping
  42.         else
  43.             if turtle.digDown() then
  44.                 while not turtle.placeDown() do
  45.                     print("Cant place floor, need help!")
  46.                     sleep(5)
  47.                 end
  48.             else
  49.                 print("Cant replace flooring, skipping...")
  50.             end
  51.         end
  52.     else
  53.         while not turtle.placeDown() do
  54.             print("Cant place floor, need help!")
  55.             sleep(5)
  56.         end
  57.     end    
  58. end
  59.  
  60. function PlaceLight()
  61.     while not GetLight() do
  62.         print("Cant find light object, waiting for more...")
  63.         sleep(5)
  64.     end
  65.     while not turtle.place() do
  66.         print("Cant place light, clearing...")
  67.         ClearAhead()
  68.     end
  69. end
  70.  
  71. function GetItemTypes()
  72.     local oItemDetails
  73.     if turtle.getItemCount(1) > 0 then
  74.         oItemDetails = turtle.getItemDetail(1)
  75.         sLight = oItemDetails.name
  76.     end
  77.     if turtle.getItemCount(2) > 0 then
  78.         oItemDetails = turtle.getItemDetail(2)
  79.         sFloor = oItemDetails.name
  80.     end
  81.     if sLight == "" or sFloor == "" then
  82.         return false
  83.     else
  84.         return true
  85.     end
  86. end
  87.  
  88. function GetLight()
  89.     local oItemDetails
  90.     local bFound = false
  91.     local iSlot = 1
  92.     while not bFound and iSlot <= 16 do
  93.         if turtle.getItemCount(iSlot) > 0 then
  94.             oItemDetails = turtle.getItemDetail(iSlot)
  95.             if oItemDetails.name == sLight then
  96.                 bFound = true
  97.                 turtle.select(iSlot)
  98.             else
  99.                 iSlot = iSlot + 1
  100.             end
  101.         else
  102.             iSlot = iSlot + 1
  103.         end
  104.     end
  105.     return bFound
  106. end
  107.  
  108. function GetFloor()
  109.     local oItemDetails
  110.     local bFound = false
  111.     local iSlot = 1
  112.     while not bFound and iSlot <= 16 do
  113.         if turtle.getItemCount(iSlot) > 0 then
  114.             oItemDetails = turtle.getItemDetail(iSlot)
  115.             if oItemDetails.name == sFloor then
  116.                 bFound = true
  117.                 turtle.select(iSlot)
  118.             else
  119.                 iSlot = iSlot + 1
  120.             end
  121.         else
  122.             iSlot = iSlot + 1
  123.         end
  124.     end
  125.     return bFound
  126. end
  127.  
  128. function CalcLight(a,b)
  129.     if (b % 4) == 0 then
  130.         if (b % 8) == 0 then
  131.             if (a % 7) == 0 then
  132.                 return true
  133.             end
  134.         else
  135.             if ((a + 4) % 7) == 0 then
  136.                 return true
  137.             end
  138.         end
  139.     end    
  140.     return false
  141. end
  142.  
  143. function PrintHelp()
  144.     print( "Usage: mkFloor <length> [<width>]" )
  145.     print("")
  146.     print( "<length>   Must be a positive number." )
  147.     print( "           How far forward to go." )
  148.     print( "<width>    Optional. how far to the")
  149.     print( "           right to go. if left blank" )
  150.     print( "           default to <length>." )
  151.     print("")
  152.     print( "Place light objects in slot 1 and" )
  153.     print( "flooring in slot 2, so the tutrle can" )
  154.     print( "detect their details. All other slots" )
  155.     print( "can contain anything else in any order." )
  156. end
  157.  
  158. function TurnAtEnd(w)
  159.     if w % 2 == 0 then
  160.         turtle.turnRight()
  161.     else
  162.         turtle.turnLeft()
  163.     end
  164. end
  165.  
  166. local tArgs = { ... }
  167. if not (#tArgs >= 1) then
  168.     PrintHelp()
  169.     return
  170. end
  171.  
  172. iLength = tonumber( tArgs[1] )
  173. if iLength < 1 then
  174.     print( "Tunnel length must be positive" )
  175.     return
  176. end
  177. iWidth = iLength
  178. if #tArgs > 1 then
  179.     iWidth = tonumber( tArgs[2] )  
  180.     if iWidth < 1 then iWidth = iLength end
  181. end
  182.  
  183. print("Making floor area: "..tostring(iLength).."x"..tostring(iWidth))
  184.  
  185. if GetItemTypes() then
  186.     TurnAround()
  187.     for w = 0, (iWidth - 1) do
  188.         for q = 0, (iLength - 2) do
  189.             PlaceFloor()
  190.             MoveBack()
  191.             if CalcLight(q,w) then
  192.                 PlaceLight()
  193.             end            
  194.         end
  195.         if not (w == (iWidth - 1)) then
  196.             TurnAtEnd(w)
  197.             PlaceFloor()
  198.             MoveBack()
  199.             if (w % 4) == 0 then
  200.                 PlaceLight()           
  201.             end
  202.             TurnAtEnd(w)
  203.         end
  204.     end
  205.     PlaceFloor()
  206. else
  207.     print("Error: Cant find lights or flooring")
  208.     print("")
  209.     PrintHelp()
  210. end
Add Comment
Please, Sign In to add comment