NearEDGE

NearEDGE - DigCubeArea

May 8th, 2017
2,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. -- IO Test --
  2. dofile("NearEDGE-Functions")
  3.  
  4.  
  5. -- Direction is relative to the starting orientation of the turtle
  6. Dir = 0
  7.  
  8. --  0
  9. -- 3 1
  10. --  2
  11.  
  12. DirNames =
  13. {
  14.     "in +Z",
  15.     "in +X",
  16.     "in -Z",
  17.     "in -X"
  18. }
  19.  
  20.  
  21. -- Location is NOT the absolute location of the turtle.
  22. --   It is relative to the starting position of the turtle
  23. LocX = 0
  24. LocY = 0
  25. LocZ = 0
  26.  
  27.  
  28. function TurnLeft()
  29.     turtle.turnLeft()
  30.     Dir = (Dir-1)%4
  31. end
  32.  
  33. function TurnRight()
  34.     turtle.turnRight()
  35.     Dir = (Dir+1)%4
  36. end
  37.  
  38. function TurnShortest(dir)
  39.  
  40.     if((Dir-1)%4 == dir) then
  41.         TurnLeft()
  42.     else
  43.         TurnRight()
  44.     end
  45. end
  46.  
  47. function UpdateLocZX()
  48.  
  49.     IsX = Dir%2
  50.     isNeg = (Dir > 1)
  51.  
  52.     local Val = 1
  53.  
  54.     if(isNeg) then
  55.         Val = -1
  56.     end
  57.  
  58.     if(IsX ~= 0) then
  59.         LocX = LocX + Val
  60.     else
  61.         LocZ = LocZ + Val
  62.     end
  63.  
  64.  
  65. end
  66.  
  67. function DoDigMove()
  68.     assert(DigMove(), "Could not move " .. DirNames[Dir+1] .. "!")
  69.     UpdateLocZX()
  70. end
  71.  
  72. function DoDigMoveUp()
  73.     assert(DigMoveUp(), "Could not move up!")
  74.     LocY = LocY + 1
  75. end
  76.  
  77. function DoDigMoveDown()
  78.     assert(DigMoveDown(), "Could not move down!")
  79.     LocY = LocY - 1
  80. end
  81.  
  82.  
  83. function TurnToDir(dir)
  84.     while Dir ~= dir do
  85.         TurnShortest(dir)
  86.     end
  87. end
  88.  
  89. function DigMoveToY(y)
  90.  
  91.     local DisY = (y-LocY)
  92.  
  93.     while LocY ~= y do
  94.         if(DisY>0) then
  95.             DoDigMoveUp()
  96.         else
  97.             DoDigMoveDown()
  98.         end
  99.     end
  100. end
  101.  
  102. function DigMoveToX(x)
  103.  
  104.     local DisX = (x-LocX)
  105.     local dir = 3
  106.  
  107.     if(DisX>0) then
  108.         dir = 1
  109.     end
  110.  
  111.     TurnToDir(dir)
  112.  
  113.     while LocX ~= x do
  114.         DoDigMove()
  115.     end
  116. end
  117.  
  118. function DigMoveToZ(z)
  119.  
  120.     local DisZ = (z-LocZ)
  121.     local dir = 2
  122.  
  123.     if(DisZ>0) then
  124.         dir = 0
  125.     end
  126.  
  127.     TurnToDir(dir)
  128.  
  129.     while LocZ ~= z do
  130.         DoDigMove()
  131.     end
  132. end
  133.  
  134.  
  135. function DigMoveTo(x, y, z)
  136.     DigMoveToY(y)
  137.     DigMoveToX(x)
  138.     DigMoveToZ(z)
  139. end
  140.  
  141.  
  142. takeInitialStep = false
  143.  
  144.  
  145. LenX = 3
  146. LenZ = 3
  147. LenY = 3
  148.  
  149. function CalculateFuelCost()
  150.     local _fuelCost = (LenX*LenY*LenZ-1) + (LenX-1)+(LenY-1)+(LenZ-1)
  151.  
  152.     if(takeInitialStep) then
  153.         _fuelCost = _fuelCost+2
  154.     end
  155.      return _fuelCost
  156. end
  157.  
  158. -----------------------------------------------------------------
  159.  
  160. cls()
  161. term.setCursorPos(8,6)
  162. textutils.slowPrint("DigCubeArea ----")
  163. term.setCursorPos(14,7)
  164. textutils.slowPrint("---- By NearEDGE")
  165. sleep(1)
  166.  
  167. cls()
  168. print("DigCubeArea v1.0\n---------------------------------\n")
  169.  
  170. LenZ = ReadNum("Please enter the number of blocks to dig forward:")
  171.  
  172. cls()
  173. print("DigCubeArea v1.0\n---------------------------------\n")
  174. LenX = ReadNum("Please enter the number of blocks to dig to the right:")
  175.  
  176.  
  177. cls()
  178. print("DigCubeArea v1.0\n---------------------------------\n")
  179. LenY = ReadNum("Please enter the number of blocks to dig upward:")
  180.  
  181. cls()
  182. print("DigCubeArea v1.0\n---------------------------------\n")
  183. takeInitialStep = BooleanQuery("Should the turtle take a step forward then start the cube?")
  184.  
  185. cls()
  186. print("DigCubeArea v1.0\n---------------------------------\n")
  187. __HaltOnInterruption = BooleanQuery("Should the turtle stop if its movement is obstructed?")
  188.  
  189. cls()
  190. print("DigCubeArea v1.0\n---------------------------------\n")
  191.  
  192. print(string.format(" X: %i\n Y: %i\n Z: %i\n\n Take Initial Step: %s\n Halt on Interruption: %s\n", LenX, LenY, LenZ, tostring(takeInitialStep), tostring(__HaltOnInterruption)))
  193. continue = BooleanQuery("Is this correct?")
  194.  
  195. cls()
  196.  
  197. CurrentFuelLevel = turtle.getFuelLevel()
  198.  
  199. if (continue and CurrentFuelLevel~= "unlimited") then
  200.     FuelNeeded = CalculateFuelCost()
  201.  
  202.     if(FuelNeeded > turtle.getFuelLimit()) then
  203.         print("This operation requires too much fuel for this turtle.\nPlease try an advanced turtle!\n\nFuel Needed: " .. FuelNeeded)
  204.         continue = false
  205.     else
  206.         if(CurrentFuelLevel < FuelNeeded) then
  207.             RecommendedFuel = GetRecommendedFuel(FuelNeeded)
  208.             print("This operation will expend more fuel than the turtle currently has.\n\n Please insert at least "..FuelNeeded.." fuel units then press enter to continue.\n  (Approx. "..RecommendedFuel[2].." "..RecommendedFuel[1].." (Recommended))\n")
  209.  
  210.             io.read()
  211.             FuelConsumed = ConsumeFuel()
  212.  
  213.             CurrentFuelLevel = turtle.getFuelLevel()
  214.  
  215.             if(not FuelConsumed or CurrentFuelLevel < FuelNeeded) then
  216.                 print("Not enough fuel. Stopping!\n")
  217.                 continue = false
  218.             end
  219.         end
  220.     end
  221. end
  222.  
  223. -----------------------------------------------------------------
  224.  
  225.  
  226. if(not continue) then
  227.     print("Please try again!")
  228.  
  229. else
  230.     cls()
  231.     print("DigCubeArea v1.0\n---------------------------------\n\n Started!")
  232.  
  233.  
  234.     ModX = 0
  235.     ModZ = 0
  236.  
  237.     if(takeInitialStep) then
  238.         DoDigMove()
  239.         LocZ = 0
  240.     end
  241.  
  242.  
  243.     while LocY ~= LenY do
  244.         while LocX ~= (LenX-1 + ModX) do
  245.             while LocZ ~= (LenZ-1 + ModZ) do
  246.                 DoDigMove()
  247.             end
  248.  
  249.             oldDir = Dir
  250.             DirC = ((ModX*(1/(LenX-1)))*-2)+1
  251.  
  252.             while Dir ~= DirC do
  253.                 TurnShortest(DirC)
  254.             end
  255.  
  256.             DoDigMove()
  257.  
  258.             DirC = (oldDir+2)%4
  259.  
  260.             while Dir ~= DirC do
  261.                 TurnShortest(DirC)
  262.             end
  263.  
  264.             ModZ = LocZ*-1
  265.  
  266.         end
  267.  
  268.         while LocZ ~= (LenZ-1 + ModZ) do
  269.             DoDigMove()
  270.         end
  271.  
  272.         oldDir = Dir
  273.         DirC = (oldDir+2)%4
  274.  
  275.         while Dir ~= DirC do
  276.             TurnShortest(DirC)
  277.         end
  278.  
  279.         ModZ = LocZ*-1
  280.         ModX = LocX*-1
  281.  
  282.         if(LocY ~= LenY-1) then
  283.             DoDigMoveUp()
  284.         else
  285.             break
  286.         end
  287.  
  288.     end
  289.  
  290.     if(takeInitialStep) then
  291.         DigMoveTo(0,0,-1)
  292.     else
  293.         DigMoveTo(0,0,0)
  294.     end
  295.  
  296.     cls()
  297.     print("Finished!")
  298. end
Advertisement
Add Comment
Please, Sign In to add comment