Advertisement
Aixler

MineResume

Oct 9th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.45 KB | None | 0 0
  1. --TODO continue after chunk reload
  2.  
  3. function printUsage()               --printing usage if mistakes are made
  4.     print( "Usage:" )
  5.     print( "Mine <forward> <right> <down>" )
  6. end
  7.  
  8. resumeArgs = 13             --setting number of Args for resuming
  9. finishArgs = 9              --and finishing
  10.  
  11. tArgs = { ... }
  12.  
  13. for x=1, #tArgs do
  14.     print(tArgs[x])
  15. end
  16. print("done printing args")
  17.  
  18. if not (#tArgs == 3 or #tArgs == finishArgs or #tArgs == resumeArgs) then   --making sure program is used correctly            
  19.     printUsage()
  20.     return
  21. elseif tonumber(tArgs[1]) == nil or tonumber(tArgs[2]) == nil or tonumber(tArgs[3]) == nil then
  22.     printUsage()
  23.     return
  24. end
  25. print("args check 1 done")
  26.  
  27. -- setting general parameters
  28. down = "down"
  29. up = "up"
  30. left = "left"
  31. right = "right"
  32. deep = 3
  33. remainingLine = 0
  34. remainingSquare = 0
  35. slot = 2
  36. turtle.select(slot)
  37. A = tonumber(tArgs[1])
  38. B = tonumber(tArgs[2])
  39. C = tonumber(tArgs[3])
  40. print("setting params done")
  41.  
  42. if #tArgs == 3 or #tArgs == finishArgs then         --setting paramaters if its first time usage
  43. print("args 3 or 9")
  44.     locx = 1    --forward
  45.     locy = 1    --right
  46.     locz = 2    --down
  47.     d = 0   --direction
  48. end
  49.  
  50. if #tArgs == 3 then
  51. print("args 3")
  52.     restarted = 0
  53.     mined = 0
  54.     attacked = 0
  55.     dropped = 0
  56.     turned = "right"
  57.     left_to_right = 1
  58. end
  59.  
  60. if #tArgs == finishArgs or #tArgs == resumeArgs then
  61. print("args 9 or 13")
  62.     --restarted, mined, attacked, dropped, turned, left_to_right = tonumber(tArgs[4]), tonumber(tArgs[5]), tonumber(tArgs[6]), tonumber(tArgs[7]), tArgs[8], tArgs[9]
  63.     restarted = tonumber(tArgs[4])
  64.     mined = tonumber(tArgs[5])
  65.     attacked = tonumber(tArgs[6])
  66.     dropped = tonumber(tArgs[7])
  67.     turned = tArgs[8]
  68.     left_to_right = tArgs[9]
  69. end
  70.  
  71. if #tArgs == resumeArgs then
  72. print("args 13")
  73.     --locx, locy, locz, d = tonumber(tArgs[10]), tonumber(tArgs[11]), tonumber(tArgs[12]), tonumber(tArgs[13])
  74.     locx = tonumber(tArgs[10])
  75.     locy = tonumber(tArgs[11])
  76.     locz = tonumber(tArgs[12])
  77.     d = tonumber(tArgs[13])
  78. end
  79.  
  80. if A < 1 or A > 512 then            --making sure valid numbers are used
  81.     print("forward must be between 1 and 512")
  82.     error()
  83. elseif B < 1 or B > 512 then
  84.     print("right must be between 1 and 512")
  85.     error()
  86. elseif C < 1 or C > 256 then
  87.     print("down must be between 1 and 256")
  88.     error()
  89. end
  90.  
  91. goForward = A - 1                       --doing math on dimensions
  92. goRight = B - 1
  93. goDown = math.floor(C / 3) - 1      --square is 3 layers thick, floor because using remaining
  94. downRemaining = math.fmod(C,3)      --for mining out the last 1 or 2 layers
  95. if goDown < 0 then                  --preventing negative numbers
  96.     goDown = 0
  97.     downRemaining = 0
  98. end
  99.  
  100. function resume()                           --resuming mining if turtle got rebooted
  101. print("resume called")
  102.     restarted = restarted + 1
  103.     if turtle.getItemCount(1) == 0 then     --dig up if slot 1 is empty, thus enderchest was placed
  104. print("getitemcount")
  105.         turtle.select(1)
  106.         dig(up)        
  107.         turtle.select(slot)
  108.     else
  109.         dig(up)
  110.     end
  111.     dig(down)
  112.     --by now the turtle finished its last action
  113.    
  114.     if downRemaining == 0 then     
  115. print("downremaining 0")
  116.         if locz % 3 == 1 then               --check if turtle is on the right level, or in the proces of moving down
  117.             move(down)
  118.             if left_to_right == 1 then      --changing side to side direction as it failed to do so before unloading
  119.                 left_to_right = 0
  120.                 save()
  121.             else
  122.                 left_to_right = 1
  123.                 save()
  124.             end
  125.         elseif locz % 3 == 0 then
  126.             move(down)
  127.             move(down)
  128.             if left_to_right == 1 then      --changing side to side direction as it failed to do so before unloading
  129.                 left_to_right = 0
  130.                 save()
  131.             else
  132.                 left_to_right = 1
  133.                 save()
  134.             end
  135.         end
  136.     elseif downRemaining == 2 then          --check if turtle is busy with the last layers
  137. print("downremaining 2")
  138.         if locz == C - 2 then               --check if turtle is on the right level, or in the proces of moving down
  139.             move(down)
  140.             if left_to_right == 1 then      --changing side to side direction as it failed to do so before unloading
  141.                 left_to_right = 0
  142.                 save()
  143.             else
  144.                 left_to_right = 1
  145.                 save()
  146.             end
  147.         end
  148.     end
  149.     --by now turtle is on an actual mining level   
  150.  
  151.     if (turned == "right" and left_to_right == 1) or (turned == "left" and left_to_right == 0) then
  152. print("turning right")
  153.         while d ~= 0 do
  154.             turn(right)
  155.         end
  156.         remainingLine = A - locx
  157.         line(remainingLine)
  158.     else
  159. print("turning left")
  160.         while d ~= 2 do
  161.             turn(left)
  162.         end
  163.         remainingLine = locx - 1
  164.         line(remainingLine)
  165.     end
  166.     --by now turtle is at the end of a line
  167.    
  168.     if left_to_right == 1 then
  169.         if locy < B then
  170.             turning()
  171.         end
  172.         remainingSquare = B - locy
  173.         square(remainingSquare)
  174.     elseif left_to_right == 0 then
  175.         if locy > 1 then
  176.             turning()
  177.         end
  178.         remainingSquare = locy - 1
  179.         square(remainingSquare)
  180.     end
  181.     --by now turtle is at the end of a square
  182.    
  183.     if locz ~= C - 1 then           --if turtle is not at the last square, go down and turn to get in position for mining the rest
  184.         if C - locz == 2 or C - locz == 3 then      --if turtle is at the second to last layer ánd there is downremaining, move down less
  185.             for x=1, C - locz  - 1 do
  186.                 move(down)
  187.             end
  188.         else
  189.             for x=1, 3 do
  190.                 move(down)
  191.             end
  192.         end
  193.         dig(down)
  194.         turn(right)
  195.         turn(right)
  196.         shell.run("mine", A, B, C, restarted, mined, attacked, dropped, turned, left_to_right)
  197.         error()     --laziness hayo
  198.     end
  199. end
  200.  
  201. function fuel()                     --fuel calculation
  202.     if C < 3 then                   --calculating fuel usage for moving down
  203.         movingDown = 0
  204.     else
  205.         movingDown = C - 3
  206.     end
  207.     fuelNeeded = (A * B - 1) * math.ceil(C / 3) + movingDown
  208.     fuelLevel = turtle.getFuelLevel()
  209.     fuelDifference = fuelNeeded - fuelLevel
  210.     fuelCoal = math.ceil(fuelDifference / 80)
  211.     if fuelLevel >= fuelNeeded then
  212.         print("*Fuel usage: ".. fuelNeeded .." out of ".. fuelLevel .."")
  213.         print("*Enough fuel to proceed")
  214.     else
  215.         print("*Need ".. fuelDifference .." more fuel (".. fuelCoal .." Coal)")
  216.         while fuelNeeded > fuelLevel do
  217.             C = C - 1
  218.             if C < 3 then
  219.                 movingDown = 0
  220.             else
  221.                 movingDown = C - 3
  222.             end
  223.         fuelNeeded = (A * B - 1) * math.ceil(C / 3) + movingDown
  224.         end
  225.         print("*Max area with current fuel: ".. A .." ".. B .." ".. C .."")
  226.         print("*Shutting Down...")
  227.         error()
  228.     end
  229. end
  230.  
  231. function full()                 --checking if inventory is full
  232.     if turtle.getItemCount(16) > 0 then
  233.         dropOff()
  234.     end
  235. end
  236.  
  237. function dropOff()              --if inventory full, drop off in (ender)chest
  238.     turtle.select(1)
  239.     while turtle.placeUp() == false do
  240.         if turtle.digUp() == false then
  241.             turtle.attackUp()
  242.         end
  243.     end
  244.     turtle.select(2)
  245.     slot = 2
  246.     for e=1, 15 do
  247.         turtle.select(slot)
  248.         turtle.dropUp()
  249.         slot = slot + 1
  250.     end
  251.     turtle.select(1)
  252.     turtle.digUp()
  253.     slot = 2
  254.     turtle.select(slot)
  255.     dropped = dropped + 1
  256. end
  257.  
  258. function mine()             --mine up/down/forward and move forward
  259.     move()
  260.     dig(up)
  261.     dig(down)
  262. end
  263.  
  264. function line(distance)             --mine a whole line
  265.     for x=1, distance do
  266.         mine()
  267.     end
  268. end
  269.  
  270. function square(distance)           --mine a square
  271.     for x=1, distance do
  272.         line(goForward)
  273.         turning()
  274.     end
  275.     line(goForward)
  276. end
  277.  
  278. function turning()                      --check if this turn goes to the right or left
  279.     if turned == "right" then          
  280.         turn(right)
  281.         move()
  282.         turned = "left"
  283.         save()
  284.         dig(up)
  285.         dig(down)
  286.         turn(right)
  287.     else
  288.         turn(left)
  289.         move()
  290.         turned = "right"
  291.         save()
  292.         dig(up)
  293.         dig(down)  
  294.         turn(left)
  295.     end
  296. end
  297.  
  298. function cube()         --mine the full cube using digCube()
  299.     dig(down)
  300.     dig(up)
  301.     square(goRight)
  302.     for x=1, goDown do  --mine all full squares
  303.         digCube(true)
  304.     end
  305.     if downRemaining ~= 0 then  --mine the last 1-2 layers if needed
  306.         deep = downRemaining
  307.         digCube(true)
  308.     end
  309. end
  310.  
  311. function digCube(change)        --mine squares and move into position for next one
  312.     for x=1, deep do
  313.         move(down)
  314.     end
  315.     if change == true then
  316.         if left_to_right == 1 then
  317.             left_to_right = 0
  318.             save()
  319.         else
  320.             left_to_right = 1
  321.             save()
  322.         end
  323.     end
  324.     dig(down)
  325.     turn(right)
  326.     turn(right)
  327.     square(goRight)
  328. end
  329.  
  330. function dig(direction)             --turtle digging
  331.     full()                          --check if full
  332.     if direction == "up" then
  333.         if turtle.digUp() then
  334.             mined = mined + 1
  335.             return true
  336.         else
  337.             return false
  338.         end
  339.     elseif direction == "down" then
  340.         if turtle.digDown() then
  341.             mined = mined + 1
  342.             return true
  343.         else
  344.             return false       
  345.         end
  346.     else
  347.         if turtle.dig() then
  348.             mined = mined + 1
  349.             return true
  350.         else
  351.             return false
  352.         end
  353.     end
  354. end
  355.  
  356. function turn(direction)            --turtle turning
  357.     if direction == "right" then
  358.         d = d + 1
  359.         if d > 3 then d = d - 4 end
  360.         save()
  361.         turtle.turnRight()
  362.     elseif direction == "left" then
  363.         d = d - 1
  364.         if d < 0 then d = d + 4 end
  365.         save()
  366.         turtle.turnLeft()
  367.     end
  368. end
  369.  
  370. function move(direction)            --turtle moving
  371.     if direction == "up" then
  372.         while not turtle.up() do
  373.             if not dig(up) then
  374.                 if turtle.attackUp() then
  375.                     attacked = attacked + 1
  376.                 end            
  377.             end
  378.         end
  379.         locz = locz - 1
  380.         save()
  381.     elseif direction == "down" then
  382.         while not turtle.down() do
  383.             if not dig(down) then
  384.                 if turtle.attackDown() then
  385.                     attacked = attacked + 1
  386.                 end            
  387.             end
  388.         end
  389.         locz = locz + 1
  390.         save()
  391.     else
  392.         while not turtle.forward() do
  393.             if not dig() then
  394.                 if turtle.attack() then
  395.                     attacked = attacked + 1
  396.                 end            
  397.             end
  398.         end
  399.         if d == 0 then          --saving new location based on the direction its facing
  400.             locx = locx + 1
  401.             save()
  402.         elseif d == 1 then
  403.             locy = locy + 1
  404.             save()
  405.         elseif d == 2 then
  406.             locx = locx - 1
  407.             save()
  408.         elseif d == 3 then
  409.             locy = locy - 1
  410.             save()
  411.         end
  412.     end
  413. end
  414.  
  415. function save()             --saving position
  416.     file = fs.open("startup", "w")      --open startup in writemode
  417.     file.write('shell.run("mine", '..A..', '..B..', '..C..', '..restarted..', '..mined..', '..attacked..', '..dropped..', \''..turned..'\', '..left_to_right..', '..locx..', '..locy..', '..locz..', '..d..')')
  418.     file.close()
  419. end
  420.  
  421. --running main program
  422. print("running main program")
  423. if #tArgs == 3 then             --if first time manual startup, interact
  424.     fuel()          --checking if there is enough fuel
  425.     print("'Put an enderchest in slot 1'")
  426.     print("'Turtle will mine forward, right, down'")
  427.     print("'Type in anything to start'")
  428.     io.read()
  429. end
  430.  
  431. if #tArgs == resumeArgs then        --call resume if resuming...
  432.     resume()
  433. else
  434.     cube()              --start working
  435. end
  436.  
  437. file = fs.open("startup", "w")
  438. file.write("")      --removing startup persistence now that program is done
  439. file.close()
  440.  
  441. dropOff()       --final dropoff to empty inventory
  442. print ("mined "..mined.." blocks")
  443. if attacked > 0 then
  444.     print ("attacked "..attacked.." times")
  445. end
  446. print ("dropped off "..dropped.." times")
  447. if restarted > 0 then
  448.     print ("restarted "..restarted.." times")
  449. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement