Advertisement
jeriktelorian

DigBox2.0

Sep 3rd, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.44 KB | None | 0 0
  1. --  digBox 2.0
  2. --  JerikTelorian
  3. --  Jerikorp Industrial & Technology Solutions
  4. --  Copyright 2015
  5.  
  6. --  VARIABLES ----------------------------
  7.  
  8. -- None
  9.  
  10. --  FUNCTIONS ----------------------------
  11.  
  12. --  Digs Upwards
  13. function DigAscend(height)
  14.     count = 1
  15.     while (count < height) do
  16.         turtle.digUp()
  17.         result = turtle.up()
  18.         if result then
  19.             count = count + 1
  20.         end
  21.     end
  22. end
  23.  
  24.  
  25. --  Digs a line straight ahead
  26. function DigStraightAhead(distance)
  27.     count = 1
  28.     while count <= distance do
  29.         turtle.dig()
  30.         result = turtle.forward()
  31.         print("----Block Count: ", count)
  32.         if result then -- Only increment if the move was a success
  33.             count = count + 1
  34.         end
  35.     end
  36. end
  37.  
  38. function RefuelToLevel(targetFuelLevel)
  39.     for i=1, 16 do  
  40.         while true do
  41.             res = turtle.Refuel()
  42.            
  43.             if not res or turtle.getFuelLevel() > targetFuelLevel then
  44.                 break
  45.             end
  46.         end
  47.        
  48.         if turtle.getFuelLevel() > targetFuelLevel then
  49.             return true
  50.         end
  51.     end
  52.     return false    
  53. end
  54.  
  55. --  Starting at the lowest point, ascends and digs a vertical slice of
  56. --      the specified height and depth. Plane is Saggital with respect to the
  57. --      orientation of the turtle.
  58. function DigSaggitalPlane(height, depth)
  59.     DigAscend(height)
  60.     count = 1
  61.     while (count <= height) do
  62.         DigStraightAhead(depth)
  63.        
  64.         --  Does an about face and goes down one level.
  65.         --  No need to check since this should always work.
  66.         if (count < height) then
  67.             turtle.turnLeft()
  68.             turtle.turnLeft()
  69.             turtle.digDown()
  70.             turtle.down()
  71.         end
  72.         count = count + 1
  73.     end
  74. end
  75.  
  76. --  DigCoronalPlane digs a coronal plane with respect to the orientation of the turtle at start
  77. function DigCoronalPlane(width, depth, isOnLeftSide, alreadyInPlane)
  78.     rowCount = 1
  79.     nextTurn = 0
  80.    
  81.     if isOnLeftSide then
  82.         nextTurn = 1 --  Positive indicates a right turn
  83.     else
  84.         nextTurn = -1 --  Negative indicates a left turn
  85.     end
  86.    
  87.     --  If you aren't already in the plane, go forward one block to enter
  88.     if not alreadyInPlane then
  89.         DigStraightAhead(1)
  90.     end
  91.    
  92.     while (rowCount <= width) do
  93.         print("--Row Count: ", rowCount)
  94.         DigStraightAhead(depth-1)
  95.        
  96.        
  97.         --  We only need to execute the turn if you're not on the last set
  98.         if rowCount < width then
  99.             --  Code for turning right
  100.             if nextTurn > 0 then
  101.                 turtle.turnRight()
  102.                
  103.                 while true do --  Ensure that the turtle moves
  104.                     turtle.dig()
  105.                     res = turtle.forward()
  106.                     if res then
  107.                         break
  108.                     end
  109.                 end
  110.                
  111.                 turtle.turnRight()
  112.             --  Code for turning left
  113.             else
  114.                 turtle.turnLeft()
  115.                
  116.                 while true do --  Ensure that the turtle moves
  117.                     turtle.dig()
  118.                     res = turtle.forward()
  119.                     if res then
  120.                         break
  121.                     end
  122.                 end
  123.                
  124.                 turtle.turnLeft()
  125.             end
  126.            
  127.             --  Invert the turn so the next one goes the other way
  128.         end
  129.        
  130.         nextTurn = nextTurn * -1
  131.             rowCount = rowCount + 1
  132.        
  133.     end
  134. end
  135.  
  136. function DigBox(w, h, d)
  137.  
  138.     -- User is instructed to place the bot at the bottom-left corner of the box.
  139.     isOnLeftSide = true
  140.  
  141.     --  First go to the top corner.
  142.     DigAscend(h)
  143.    
  144.     for i=h,1, -1 do
  145.     print("Level Count: ", i)
  146.         if i==h then -- On the first one, you need to enter the plane
  147.             DigCoronalPlane(w, d, isOnLeftSide, false)
  148.         else -- on subsequent, you're already in the plane
  149.             DigCoronalPlane(w, d, isOnLeftSide, true)
  150.         end
  151.        
  152.         -- 180deg turn
  153.         turtle.turnLeft()
  154.         turtle.turnLeft()
  155.        
  156.         -- Descend
  157.         if i > 1 then
  158.             turtle.digDown()
  159.             turtle.down()
  160.         end
  161.        
  162.         -- For even widths, we alternate the first turns
  163.         if w%2 == 0 then
  164.             isOnLeftSide = not isOnLeftSide
  165.         end
  166.     end
  167. end
  168.  
  169. --  Estimates fuel needs. Returns true if the job can be completed within the
  170. --      maximum fuel level for the turtle, false if not.
  171. function VerifyFuel(dims)
  172.     totalCost = dims["w"] * dims["d"] * dims["h"]
  173.     print("Fuel cost estimated at: ", totalCost)
  174.     print("CurrentFuel: ", turtle.getFuelLevel())
  175.  
  176.     if totalCost > turtle.getFuelLimit() then
  177.         print("Warning! Fuel Cost Higher than maximum fuel level!")
  178.         print("Maximum fuel level is: ", turtle.getFuelLimit())
  179.         return false
  180.     elseif totalCost > turtle.getFuelLevel() then
  181.         print("Attempt to refuel? y/n")
  182.         if io.read() == "y" then
  183.             return RefuelToLevel(totalCost)
  184.         else
  185.             return false
  186.         end
  187.     else
  188.         return true
  189.     end
  190. end
  191.  
  192. function RefuelToLevel(level)
  193.     for i=1,16 do
  194.         turtle.select(i)
  195.        
  196.         while turtle.getFuelLevel() <= level do
  197.             success = turtle.refuel(1)
  198.             if not success then
  199.                 break
  200.             end
  201.         end
  202.        
  203.         if turtle.getFuelLevel() > level then
  204.             print("Success. Fuel level: ", turtle.getFuelLevel())
  205.             return true
  206.         end
  207.     end
  208.     print("Not enough fuel. Current: ", turtle.getFuelLevel())
  209.     return false
  210. end
  211.  
  212. function GetInput()
  213.     result = {}
  214.  
  215.     io.write("How wide should the box be?")
  216.     w = math.floor(tonumber(io.read()))
  217.     io.write("How deep should the box be?")
  218.     d = math.floor(tonumber(io.read()))
  219.     io.write("How high should the box be?")
  220.     h = math.floor(tonumber(io.read()))
  221.    
  222.    
  223.     result["w"] = w
  224.     result["d"] = d
  225.     result["h"] = h
  226.    
  227.     return result
  228. end
  229.  
  230. function ObtainAndVerifyInput()
  231.  
  232.     gotInput,dims = pcall(GetInput)
  233.    
  234.     if not gotInput then
  235.         return false
  236.     end
  237.     print("**Input obtained.")
  238.        
  239.     if (dims["w"] < 1) or (dims["d"] < 1) or (dims["h"] < 1) then
  240.         print("Error: All dimensions must be greater than or equal to 1")
  241.         return false
  242.     end
  243.     print("**Input formatted correctly.")
  244.    
  245.     if not VerifyFuel(dims) then
  246.         return false
  247.     end
  248.     print("**Fuel levels nominal.")
  249.    
  250.     return true, dims
  251. end
  252.  
  253. function Main()
  254.     term.clear()
  255.     term.setCursorPos(1,1)
  256.    
  257.     goodToGo, dims = ObtainAndVerifyInput()
  258.     print("Dig parameters:")
  259.     print("Width: ", dims["w"])
  260.     print("Depth: ", dims["d"])
  261.     print("Height: ", dims["h"])
  262.     print("Execute Dig? y/n")
  263.     ans = io.read()
  264.    
  265.     if goodToGo and ans == "y" then
  266.         term.clear()
  267.         term.setCursorPos(1,1)
  268.         print("Beginning dig")        
  269.         DigBox(dims["w"], dims["h"], dims["d"])
  270.         print("Dig finished")
  271.     else
  272.         print("Dig cancelled.")
  273.     end
  274.    
  275.     print("Goodbye")
  276. end
  277.  
  278. function DispLogo()
  279. print("___________________________            ")
  280. sleep(0.05)
  281. print("++++++++++..++...+++....+++++++        ")
  282. sleep(0.05)
  283. print("+++++++++...++..+++...+++++++++++      ")
  284. sleep(0.05)
  285. print("+++++++++..++...+...+++++++++++++++    ")
  286. sleep(0.05)
  287. print("++++++++...++..+..,++++++++++++++++++  ")
  288. sleep(0.05)
  289. print("++++++++..++......+++++++++++++++++++++")
  290. sleep(0.05)
  291. print("+++++++..:+=..+,..+++++++++++++++++++  ")
  292. sleep(0.05)
  293. print("+...++...++..~++..~+++++++++++++++     ")
  294. sleep(0.05)
  295. print("+.......++...+++...+++++++++++++       ")
  296. sleep(0.05)
  297. print("++~..,++++..++++...+++++++++++         ")
  298. sleep(0.05)
  299. print("---------------------------            ")
  300. sleep(0.05)
  301. print("Jerikorp: Today's Tech,Tomorrow's World")
  302. sleep(2)
  303. end
  304.  
  305. term.clear()
  306. term.setCursorPos(1,1)
  307. DispLogo()
  308. print("DigBox 2.0 -- A Jerikorp® Product\n")
  309. print("Copyright Jerikorp Industrial \n\tand Technological Solutions 2015\n")
  310. io.write("Instructions: Place the Turtle at the BOTTOM-LEFT corner of the box you want to dig.\n")
  311. io.write("All dimensions must be an integer greater than 0.\n\n")
  312. print("Press Enter to Continue")
  313. io.read()
  314. -- Executes main program.
  315. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement