Advertisement
Cratonz

CEX v1.03

Sep 13th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.46 KB | None | 0 0
  1. -- Check that some fuel exists (consumes one if it does exist; there is no other way to verify)
  2. function CheckInitialFuel()
  3.     for i = 1, 16 do
  4.         turtle.select(i)
  5.         if turtle.refuel(1) then -- If fuel was successfully used, swap the stack to the first slot (faster refueling next time, probably unnecessary)
  6.             --turtle.transferTo(i, 1)
  7.             return true
  8.         end
  9.     end
  10.    
  11.     return false
  12. end
  13.  
  14. function DigThenMove(dir)
  15.     local movedSuccessfully = false
  16.    
  17.     while movedSuccessfully == false do
  18.         if dir == "forward" then
  19.             while turtle.detect() do
  20.                 turtle.dig()
  21.             end    
  22.         elseif dir == "up" then
  23.             while turtle.detectUp() do
  24.                 turtle.digUp()         
  25.             end
  26.         elseif dir == "down" then
  27.             while turtle.detectDown() do
  28.                 turtle.digDown()           
  29.             end
  30.         end
  31.        
  32.         if dir == "forward" then
  33.             movedSuccessfully = turtle.forward()
  34.         elseif dir == "up" then
  35.             movedSuccessfully = turtle.up()
  36.         else
  37.             movedSuccessfully = turtle.down()
  38.         end
  39.     end
  40. end
  41.  
  42. -- Mines all blocks directly in front of the turtle (one square), then moves one square forward (blocks like gravel or sand can fall and result in a block being directly in front of the turtle multiple times)
  43. function RefuelIfNeeded()
  44.     if turtle.getFuelLevel() <= 5 then -- If the turtle is empty or nearly empty on fuel, search for a fuel source and refuel (prioritize left to right, top to bottom)
  45.         for i = 1, 16 do
  46.             turtle.select(i)
  47.             if turtle.refuel(1) then -- If fuel was successfully used, swap the stack to the first slot (faster refueling next time, probably unnecessary)
  48.                 return
  49.             end            
  50.         end
  51.         WaitForFuel()
  52.     end
  53. end
  54.  
  55. function WaitForFuel()
  56.     print("Please add fuel.")
  57.    
  58.     while true do
  59.         for i = 1, 16 do
  60.             turtle.select(i)
  61.             if turtle.refuel(1) then
  62.                 return
  63.             end            
  64.         end
  65.         sleep(5)
  66.     end
  67. end
  68.  
  69. function TurnTurtle(reverseDirection)
  70.     -- The reverse direction flag is used for turning back to the initial facing direction (i.e. forward) after a row of clearing
  71.     if (reverseDirection) then
  72.         if rightOrLeft == "right" then
  73.             turtle.turnLeft()
  74.         else
  75.             turtle.turnRight()
  76.         end
  77.     else
  78.         if (rightOrLeft == "right") then
  79.             turtle.turnRight()
  80.         else
  81.             turtle.turnLeft()
  82.         end
  83.     end
  84. end
  85.  
  86. function MoveVertically()
  87.     if upOrDown == "up" then
  88.         DigThenMove("up")
  89.         --turtle.up()
  90.     else -- direction == "down"
  91.         DigThenMove("down")
  92.         --turtle.down()
  93.     end
  94. end
  95.  
  96. -- Return the turtle to whence he came
  97. function ReturnToStart()
  98.     -- Return to the starting plane (height)
  99.     if length%2 == 0 then
  100.         for i = 1,height-1 do
  101.             if initialVertical == "up" then
  102.                 turtle.down()
  103.             else
  104.                 turtle.up()
  105.             end
  106.         end
  107.     end
  108.    
  109.     -- Return to the side of the cuboid the turtle started on (left or right edge)
  110.     if (height*length)%2 ~= 0 then
  111.         if initialHorizontal == "right" then
  112.             turtle.turnLeft()
  113.         else
  114.             turtle.turnRight()
  115.         end
  116.  
  117.         for i = 1,width-1 do          
  118.             turtle.forward()
  119.         end
  120.  
  121.         -- Adjust facing to be the same as when the turtle started
  122.         if initialHorizontal == "right" then
  123.             turtle.turnRight()
  124.         else
  125.             turtle.turnLeft()
  126.         end
  127.     end
  128.  
  129.     -- Return to the side of the cuboid the turtle started (rear edge).  Turtle should now be at the starting coordinates
  130.     for i =1,length-1 do
  131.             turtle.back()
  132.     end
  133. end
  134.  
  135. function DumpItems()       
  136.     turtle.turnLeft()
  137.     if turtle.detect() then
  138.             for i = 1,16 do
  139.                 turtle.select(i)
  140.                 turtle.drop()
  141.             end
  142.     end
  143.    
  144.     turtle.turnRight()
  145.     --turtle.turnRight()
  146.     -- Add code to check multiple sides for a chest (detect gets any block, though)
  147.         -- Could attempt a refuel and if successful move fuel to slot 1 then dump the rest of the items (useful for dump and continue logic)
  148. end
  149.  
  150. -- Consider adding default values for fields left blank
  151. function PerformExcavation()
  152.     for k = 1,length do
  153.     DigThenMove("forward")
  154.  
  155.     for j = 1,height do
  156.             if width > 1 then -- Only need to do turning if the width is greater than 1
  157.                 TurnTurtle(false)
  158.             end
  159.  
  160.             for i = 1,width-1 do
  161.                 if RefuelIfNeeded() == false then
  162.                     return false
  163.                 end
  164.                 DigThenMove("forward")
  165.             end
  166.            
  167.             if width > 1 then
  168.                 TurnTurtle(true)
  169.             end
  170.  
  171.             if (rightOrLeft == "right") then
  172.                 rightOrLeft = "left"
  173.             else          
  174.                 rightOrLeft = "right"            
  175.             end
  176.          
  177.             if j < height then
  178.                 MoveVertically()
  179.             end
  180.     end
  181.        
  182.     -- Change vertical direction the turtle will move to clear the next 2D piece (width x height)
  183.     if upOrDown == "up" then
  184.         upOrDown = "down"
  185.     else
  186.         upOrDown = "up"
  187.     end
  188.     end
  189.    
  190.     return true
  191. end
  192.  
  193.  
  194. -- Gather mining parameters from input (Should it go from current square down or up?  Should it go from current square left or right?  How wide / tall / long?  That is, determine which corner to start at and how big of a cuboid to mine.)
  195. print("Clear left or right?")
  196. print("[VALID: left | right]: ")
  197. while true do
  198.     rightOrLeft = string.lower(read())
  199.     if (rightOrLeft == "right" or rightOrLeft == "left") then
  200.             break
  201.         else
  202.             print("Bad entry. Clear left or right?")
  203.             print("[VALID: left | right]: ")      
  204.     end
  205. end
  206.  
  207.  
  208. print("Clear up or down?")
  209. print("[VALID: up | down]: ")
  210. while true do
  211.     upOrDown = string.lower(read())
  212.     if (upOrDown == "up" or upOrDown == "down") then
  213.         break      
  214.     else
  215.         print("Bad entry. Clear up or down?")
  216.         print("[VALID: up | down]: ")
  217.     end
  218. end
  219.  
  220.  
  221. print("Enter width (left/right)")
  222. print("[VALID: Any integer]: ")
  223. while true do
  224.     if pcall(function () width = tonumber(read()) end) then
  225.         break
  226.     else
  227.         print("Bad entry. Enter width (left/right)?")
  228.         print("[VALID: Any integer]: ")
  229.     end
  230. end
  231.  
  232.  
  233. print("Enter height (above/below)")
  234. print("[VALID: Any integer]: ")
  235. while true do
  236.     if pcall(function () height = tonumber(read()) end) then
  237.         break;
  238.     else
  239.         write ("Bad entry. Enter height (above/below)?")
  240.         print("[VALID: Any integer]: ")
  241.     end
  242. end
  243.  
  244.  
  245. print("Enter length (forward)")
  246. print("[VALID: Any integer]: ")
  247. while true do
  248.     if pcall(function () length = tonumber(read()) end) then
  249.         break;
  250.     else
  251.         write ("Bad entry. Enter length (forward)?")
  252.         print("[VALID: Any integer]: ")
  253.     end
  254. end
  255.  
  256. initialVertical = upOrDown
  257. initialHorizontal = leftOrRight
  258.  
  259. RefuelIfNeeded()
  260.  
  261. if PerformExcavation() then
  262.     ReturnToStart()
  263.     --DumpItems()
  264. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement