Advertisement
vittoema96

CC Turtle digRoom (Updated)

May 2nd, 2019 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | None | 0 0
  1. -- This program must be pasted to a Turtle (Basic or Advanced)
  2. -- If the turtle has a modem
  3.  
  4.    
  5. maxWidth = 1
  6. maxLength = 1
  7. maxHeight = 1
  8.  
  9. actualWidth = 1
  10. actualLength = 1
  11. actualHeight = 1
  12.  
  13. RL=1
  14.  
  15.  
  16. -- Improved version of dig()
  17. -- z1 : actual height
  18. -- 1) Refuels if needed (slot 1)
  19. -- 2) Digs forwards until it can move, then moves
  20. -- 3) If inventory is full, goes down to ground,
  21. --    places and dumps inventory in a chest (slot 2),
  22. --    then gets back into place
  23.  
  24. function digPlus()
  25.     -- Refuels if fuel below 60
  26.     if turtle.getFuelLevel()<60 then
  27.         turtle.select(1)
  28.         turtle.refuel(1)
  29.     end
  30.    
  31.     -- Digs until it can go forward
  32.     turtle.dig()
  33.     while turtle.forward()==false do
  34.        turtle.dig()
  35.     end
  36.    
  37.     local n=0
  38.     for i=1, 16 do
  39.         if turtle.getItemCount(i)>0 then
  40.             n=n+1
  41.         end
  42.     end
  43.     if n==16 then
  44.         local dwn = 0
  45.         if fromTop() then
  46.             dwn = maxHeight-actualHeight
  47.         else
  48.             dwn = actualHeight-1
  49.         end
  50.         for i=1, dwn do
  51.             turtle.digDown()
  52.             turtle.down()
  53.         end
  54.         turtle.select(2)
  55.         turtle.digDown()
  56.         turtle.placeDown()
  57.         for i=3, 16 do
  58.             turtle.select(i)
  59.             turtle.dropDown()
  60.         end
  61.         for i=1, dwn do
  62.             turtle.up()
  63.         end
  64.     end
  65. end
  66.  
  67. function digZsquare()
  68.     while actualWidth <= maxWidth do
  69.         digXline()
  70.         if actualWidth < maxWidth then
  71.             if fromLeft() == (RL%2==1) then
  72.                 turtle.turnRight()
  73.                 digPlus()
  74.                 turtle.turnRight()
  75.             else
  76.                 turtle.turnLeft()
  77.                 digPlus()
  78.                 turtle.turnLeft()
  79.             end
  80.             RL=RL+1
  81.             actualLength=1 
  82.         end
  83.         actualWidth=actualWidth+1
  84.     end
  85. end
  86.  
  87. function digXline()
  88.     while actualLength < maxLength do      
  89.         digPlus()
  90.         actualLength=actualLength+1
  91.         term.setCursorPos(14,2)
  92.         term.write("Width    :   "..actualWidth.."/"..maxWidth)
  93.         term.setCursorPos(14,4)
  94.         term.write("Length   :   "..actualLength.."/"..maxLength)
  95.         term.setCursorPos(14,6)
  96.         term.write("Height :     "..actualHeight.."/"..maxHeight)
  97.         local perc = math.floor(100*(actualLength + maxLength*(actualWidth-1)+maxWidth*maxLength*(actualHeight-1))/(maxWidth*maxLength*maxHeight))
  98.         term.setCursorPos(34,9)
  99.         term.write(perc.."%")
  100.         if math.floor(perc*28/100)>0 then
  101.             term.setCursorPos(2+math.floor(perc*28/100),9)
  102.             term.write("#")
  103.         end
  104.         if hasModem then
  105.             rednet.send(7, perc.."%"..actualWidth.."x"..maxWidth.."X"..actualLength.."y"..maxLength.."Y"..actualHeight.."z"..maxHeight.."Z", "Turtle")
  106.         end
  107.     end
  108. end
  109.  
  110. function fromTop()
  111.     return corner>2
  112. end
  113.  
  114. function fromLeft()
  115.     return corner==1 or corner==2
  116. end
  117.  
  118. -- Gets type of computer based on screen size and screen color
  119. function getTypeOfComputer()
  120.     local x, y = term.getSize()
  121.     local type = ""
  122.     if x==51 and y==19 then
  123.         type = "computer"
  124.     elseif x==26 and y==20 then
  125.         type = "pocket"
  126.     elseif x==39 and y==13 then
  127.         type = "turtle"
  128.     else   
  129.         type = "monitor"
  130.     end
  131.     if term.isColor() then
  132.             type = "adv_"..type
  133.     end
  134.     return type
  135. end
  136.  
  137. function checkForModem()
  138.     for _,side in ipairs(rs.getSides()) do
  139.         if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  140.             rednet.open(side)
  141.             return true
  142.         end
  143.     end
  144.     return false
  145. end
  146.  
  147. function errorScreen(msg)
  148.     term.clear()
  149.     local x, y = term.getSize()
  150.     for i=1, x do
  151.         for k=1, y do
  152.             if k==1 or k==y or i==1 or i==x then
  153.                 term.setCursorPos(i, k)
  154.                 term.write("!")
  155.             end
  156.         end
  157.     end
  158.     local lines = 0
  159.    
  160.     lines = math.floor(string.len(msg)/(x-4))
  161.     for i=0, lines do
  162.         term.setCursorPos(3, math.floor(y/2)+i)
  163.         if i==lines-1 then
  164.             term.write(string.sub(msg, lines*(x-4)+1))
  165.         else
  166.             term.write(string.sub(msg, lines*(x-4)+1, lines*(x-3)+1))
  167.         end
  168.     end
  169.     sleep(10)
  170. end
  171.  
  172.  
  173. Name = os.getComputerLabel()
  174. Type = getTypeOfComputer()
  175. hasModem = checkForModem()  
  176.  
  177. if not Type == "turtle" then
  178.     errorScreen("This is not a turtle, be sure to download the program on an actual turtle, then try to run it")
  179. else
  180.     term.clear()
  181.     term.setCursorPos(1,1) 
  182.     print("Before starting, be sure to have placed:")
  183.     print("- Some fuel in slot 1")
  184.     print("- Some chests in slot 2")
  185.     print(" ")
  186.     print("Keep in mind that the turtle will start ")
  187.     print("from the bottom left size of the room")
  188.     print(" ")
  189.     print("Press ENTER to get started\n\n")
  190.     io.read()
  191.    
  192.     -- Throws error if fuel and chests or not provided
  193.     if turtle.select(1) and not turtle.refuel(0) then
  194.         errorScreen("You have to place fuel in slot #1 to proceed. Every type of fuel is accepted")
  195.     end
  196.     if turtle.select(2) and not turtle.getItemDetail().name=="chest" then
  197.         errorScreen("You have to place some chests in slot #2. Only chests will be accepted, no other type of container will work")
  198.     end
  199.    
  200.     -- Asks for sizes
  201.     term.clear()
  202.     term.setCursorPos(1,1)
  203.     print("Insert Width: ")
  204.     maxWidth = tonumber(read())
  205.     print("Insert Length: ")
  206.     maxLength = tonumber(read())
  207.     print("Insert Height: ")
  208.     maxHeight = tonumber(read())
  209.     term.clear()
  210.    
  211.     corner = -1
  212.     while corner > 4 or corner < 1 do
  213.         term.clear()
  214.         term.setCursorPos(1,1)
  215.         print("From which corner are you starting to dig?")
  216.         print("1   -   Bottom left")
  217.         print("2   -   Bottom right")
  218.         print("3   -   Top left")
  219.         print("4   -   Top right")
  220.         corner = tonumber(io.read())
  221.     end
  222.  
  223.     term.clear()
  224.    
  225.     term.setCursorPos(2,3)
  226.     term.write("PROGRESS ")
  227.     term.setCursorPos(2,4)
  228.     term.write("    /")
  229.     term.setCursorPos(2,5)
  230.     term.write("DIMENSION ")
  231.  
  232.     term.setCursorPos(2,8)
  233.     term.write(",----------------------------.")
  234.     term.setCursorPos(2,9)
  235.     term.write("|                            |")
  236.     term.setCursorPos(2,10)
  237.     term.write("'----------------------------'")
  238.  
  239.    
  240.  
  241.  
  242.     while actualHeight <= maxHeight do
  243.         digZsquare()
  244.         if actualHeight < maxHeight then
  245.             if fromTop() then
  246.                 turtle.digDown()
  247.                 turtle.down()
  248.             else
  249.                 turtle.digUp()
  250.                 turtle.up()
  251.             end
  252.             turtle.turnLeft()
  253.             turtle.turnLeft()
  254.             actualLength=1
  255.             actualWidth=1
  256.             actualHeight=actualHeight+1
  257.         end
  258.     end
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement