Jharii

bottomDweller

Jan 22nd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.72 KB | None | 0 0
  1. os.loadAPI("turtleShell")
  2.  
  3. -- bottomDweller
  4. -- In this program, the turtle will mine from level 5 to level 15.
  5. -- He will hollow out the entirety of level 5, 8, 11, and 14.
  6. -- During this process, he will be detecting above and below him,
  7. -- ignoring the designated blocks in slots 2-4.  The turtle should be
  8. -- stocked with fuel in slot 1, but if there is no fuel, he will
  9. -- look for an alternative source in all slots.
  10.  
  11. -- Eventually, the turtle will have the ability to broadcast his
  12. -- coordinates to the server.
  13.  
  14.  
  15. -- Information specific to rednet
  16. job = "Bottom Dwelling"
  17. broadcast = false
  18. -- End rednet
  19.  
  20. task = "" -- Will also be broadcast on rednet
  21.  
  22. xHome = 0
  23. zHome = 0
  24. yHome = 0
  25. facingHome = 1
  26.  
  27. facings = {"west", "north", "east", "south"}
  28.  
  29. yActual = 0
  30. yDestination = 5
  31. radius = 0
  32.  
  33. xMinimum = 0
  34. xMaximum = 0
  35. zMinimum = 0
  36. zMaximum = 0
  37. yMaximum = 15
  38.  
  39. -- Where the turtle is at, relative to home.
  40. xCurrent = 0
  41. zCurrent = 0
  42. yCurrent = 0
  43. facingCurrent = 1
  44.  
  45. -- If the turtle needs to unload, store coordinates here.
  46. xTemp = 0
  47. yTemp = 0
  48. zTemp = 0
  49. facingTemp = 0
  50.  
  51. currentQuadrant = 0
  52. currentLevel = 5
  53.  
  54. ignoreInventory = {}
  55.  
  56. success = false
  57. incomplete = true
  58. continue = true
  59.  
  60. function getParameters()
  61.   confirm = false
  62.   while not(confirm) do
  63.     term.clear()
  64.     term.setCursorPos(1, 1)
  65.    
  66.     print("Enter Y coordinate of turtle:")
  67.     yActual = read()
  68.  
  69.     -- print("Enter current facing (1=W/2=N/3=E/4=S):")
  70.     -- facingCurrent = read()
  71.  
  72.     print("Enter radius:")
  73.     radius = read()
  74.  
  75.     term.clear()
  76.     print("Current Y = " .. yActual)
  77.     -- print("Facing " .. facingCurrent)
  78.     print("Mine a " .. radius .. " block radius.")
  79.     print("Confirm (Y/N)?")
  80.  
  81.     response = read()
  82.     if response == "Y" or response == "y" then
  83.       -- facingHome = facingCurrent
  84.       confirm = true
  85.     end
  86.   end
  87. end
  88.  
  89. function initialize()
  90.   task = "Initializing"
  91.  
  92.   -- begin rednet
  93.   if broadcast then
  94.   end
  95.   -- end rednet
  96.  
  97.   xMinimum = xHome - radius -- west edge
  98.   xMaximum = xHome + radius -- east edge
  99.   zMinimum = zHome - radius -- south edge
  100.   zMaximum = zHome + radius -- north edge
  101.   for i = 1,16 do
  102.     table.insert(ignoreInventory,i)
  103.   end
  104. end
  105.  
  106. function checkBlockAboveAndBelow()
  107.   digUp = true
  108.   digDown = true
  109.   for i = 1,#ignoreInventory do
  110.     turtle.select(ignoreInventory[i])
  111.     if turtle.compareUp() then
  112.       digUp = false
  113.     end
  114.     if turtle.compareDown() then
  115.       digDown = false
  116.     end
  117.   end
  118.   if digUp then
  119.     if turtleShell.betterDigUp() then
  120.     end
  121.   end
  122.   if digDown then
  123.     if turtleShell.betterDigDown() then
  124.     end
  125.   end
  126. end    
  127.    
  128. function checkBlockForward()
  129.   digForward = true
  130.   for i = 1,#ignoreInventory do
  131.     turtle.select(ignoreInventory[i])
  132.     if turtle.compare() then
  133.       digForward = false
  134.     end
  135.   end
  136.   if digForward then
  137.     if turtleShell.betterDig() then
  138.     end
  139.   end
  140. end
  141.  
  142. -- The amount of fuel needed to return home.
  143. function checkDistance()
  144.   return xCurrent + zCurrent + yCurrent
  145. end
  146.  
  147. function trackForward(checkBlocks)
  148.   -- If we are on the perimeter of the square, we also want to
  149.   -- check the walls.  Check will tell us "left", "forward",
  150.   -- "right", "forwardleft", "forwardright", or "nothing".
  151.   -- All settings except "nothing" will check up and down .
  152.   checkBlocks = checkBlocks or "nothing"
  153.  
  154.   -- check fuel
  155.   if not(turtleShell.fuelUp(checkDistance())) then
  156.     returnHome(false)
  157.   end
  158.  
  159.   if turtleShell.betterDig() then
  160.   end
  161.  
  162.   if turtle.forward() then
  163.     if facingCurrent == 1 then
  164.       xCurrent = xCurrent - 1
  165.     elseif facingCurrent == 2 then
  166.       zCurrent = zCurrent + 1
  167.     elseif facingCurrent == 3 then
  168.       xCurrent = xCurrent + 1
  169.     elseif facingCurrent == 4 then
  170.       yCurrent = yCurrent - 1
  171.     end
  172.   else
  173.     returnHome(false)
  174.   end
  175.  
  176.   -- Check adjacent blocks
  177.   if not(checkBlocks == "nothing") then
  178.     checkBlockAboveAndBelow()
  179.     if checkBlocks == "forward" then
  180.       checkBlockForward()
  181.     elseif checkBlocks == "right" then
  182.       trackRight()
  183.       checkBlockForward()
  184.       trackLeft()
  185.     elseif checkBlocks == "left" then
  186.       trackLeft()
  187.       checkBlockForward()
  188.       trackRight()
  189.     elseif checkBlocks == "forwardright" then -- corners
  190.       checkBlockForward()  
  191.       trackRight()          
  192.       checkBlockForward()
  193.       trackLeft()
  194.     elseif checkBlocks == "forwardleft" then  -- corners
  195.       checkBlockForward()  
  196.       trackLeft()          
  197.       checkBlockForward()
  198.       trackRight()
  199.     end
  200.   end
  201.  
  202.   -- Rather than checking inventory every time the turtle successfully
  203.   -- digs, check it once at the end.  The turtle will be sucking up
  204.   -- everything when it returns anyhow, should anything be left behind.
  205.   if turtleShell.checkInventory() then
  206.     returnHome(false)
  207.   end
  208.  
  209.   turtle.suck()
  210.   turtle.suckUp()
  211.   turtle.suckDown()
  212. end
  213.  
  214. function trackDown(digShaft)
  215.   -- The only time that we have to dig is during the initial descent,
  216.   -- so we grant the ability to bypass the digging.
  217.   digShaft = digShaft or false
  218.  
  219.  -- check fuel
  220.   if not(turtleShell.fuelUp(checkDistance())) then
  221.     returnHome(false)
  222.   end
  223.  
  224.   if digShaft then
  225.     if turtleShell.betterDigDown() then
  226.       if (turtle.down()) then
  227.         yCurrent = yCurrent - 1
  228.       end
  229.     else
  230.       continue = false
  231.       returnHome()
  232.     end
  233.  
  234.     if turtleShell.checkInventory() then
  235.       returnHome(false)
  236.     end
  237.   else
  238.     if (turtle.down()) then
  239.       yCurrent = yCurrent - 1
  240.     end
  241.   end
  242.  
  243.   turtle.suck()
  244.   turtle.suckUp()
  245.   turtle.suckDown()
  246. end
  247.  
  248. -- The only time that we will ever be moving up is when going up
  249. -- the mine shaft.  We never have to check for fuel during this
  250. -- process because we will always be closer to home, so the check
  251. -- has already been made.
  252. function trackUp()
  253.   if (turtle.up()) then
  254.     yCurrent = yCurrent + 1
  255.   end
  256. end
  257.  
  258. function trackRight()
  259.   turtle.turnRight()
  260.   facingCurrent = facingCurrent + 1
  261.   if facingCurrent == 5 then
  262.     facingCurrent = 1
  263.   end
  264. end
  265.  
  266. function trackLeft()
  267.   turtle.turnLeft()
  268.   facingCurrent = facingCurrent - 1
  269.   if facingCurrent == 0 then
  270.     facingCurrent = 4
  271.   end
  272. end
  273.  
  274. -- face(dDirection,cDirection)
  275. -- sDirection - destination direction
  276. -- cDirection - current direction (if not provided, uses currentDirection)
  277. function face(dDirection,cDirection)
  278.   cDirection = cDirection or facingCurrent
  279.     if cDirection - dDirection == 0 then
  280.     elseif cDirection - dDirection == 1 or cDirection - dDirection == -3 then
  281.       trackLeft()
  282.     elseif cDirection - dDirection == -1 or cDirection - dDirection == 3 then
  283.       trackRight()
  284.     elseif cDirection - dDirection == 2 or cDirection - dDirection == -2 then
  285.       trackLeft()
  286.       trackLeft()
  287.     else
  288.       -- Unknown direction.
  289.     end
  290. end
  291.  
  292. function faceWest(cDirection)
  293.   face(1,cDirection)
  294. end
  295.  
  296. function faceNorth(cDirection)
  297.   face(2,cDirection)
  298. end
  299.  
  300. function faceEast(cDirection)
  301.   face(3,cDirection)
  302. end
  303.  
  304. function faceSouth(cDirection)
  305.   face(4,cDirection)
  306. end
  307.  
  308. -- if in quadrant 1, go south then east
  309. -- if in quadrant 2, go west then south
  310. -- if in quadrant 3, go north then west
  311. -- if in quadrant 4, go east then north
  312. function returnHome(quadrant,returnToSurface)
  313.   -- If the turtle needs to return to the surface, flag as true  
  314.   quadrant = quadrant or currentQuadrant
  315.   returnToSurface = returnToSurface or false
  316.  
  317.   if returnToSurface then
  318.     task = "Returning to mine origin"
  319.   else
  320.     task = "Returning to level origin"
  321.   end
  322.  
  323.   -- begin rednet
  324.   if broadcast then
  325.   end
  326.   -- end rednet
  327.  
  328.   -- Remember where we were at
  329.   xTemp = xCurrent
  330.   zTemp = zCurrent
  331.   yTemp = yCurrent
  332.   facingTemp = facingCurrent
  333.  
  334.   pDirection = quadrant - 1
  335.   if pDirection == 0 then
  336.     pDirection = 4
  337.   end
  338.   sDirection = quadrant - 2
  339.   if sDirection == 0 then
  340.     sDirection = 4
  341.   elseif sDirection == -1 then
  342.     sDirection = 3
  343.   end
  344.  
  345.   for i = 1,2 do
  346.     if i == 1 then
  347.       direction = pDirection
  348.     else
  349.       direction = sDirection
  350.     end
  351.    
  352.     face(direction)
  353.     if direction == 1 or direction == 3 then
  354.       while xCurrent ~= xHome do
  355.         trackForward("nothing")
  356.       end
  357.     end
  358.    
  359.     if direction == 2 or direction == 4 then
  360.       while zCurrent ~= zHome do
  361.         trackForward("nothing")
  362.       end
  363.     end
  364.   end
  365.  
  366.   if returnToSurface then
  367.     while yCurrent ~= yHome do
  368.       trackUp()
  369.     end
  370.   end
  371. end
  372.  
  373. -- if in quadrant 1, go west then north
  374. -- if in quadrant 2, go north then east
  375. -- if in quadrant 3, go east then south
  376. -- if in quadrant 4, go south then west
  377. function returnToMining(quadrant)
  378.   quadrant = quadrant or currentQuadrant
  379.  
  380.   task = "Returning to mining"
  381.  
  382.   -- begin rednet
  383.   if broadcast then
  384.   end
  385.   -- end rednet
  386.  
  387.   pDirection = quadrant
  388.   sDirection = quadrant + 1
  389.   if sDirection == 5 then
  390.     sDirection = 1
  391.   end
  392.  
  393.   while yCurrent ~= yTemp do
  394.     trackDown()
  395.   end
  396.  
  397.   for i = 1,2 do
  398.     if i == 1 then
  399.       direction = pDirection
  400.     else
  401.       direction = sDirection
  402.     end
  403.    
  404.     face(direction)
  405.    
  406.     if direction == 1 or direction == 3 then
  407.       while xCurrent ~= xTemp do
  408.         trackForward("nothing")
  409.       end
  410.     end
  411.    
  412.     if direction == 2 or direction == 4 then
  413.       while zCurrent ~= zTemp do
  414.         trackForward("nothing")
  415.       end
  416.     end
  417.   end
  418.  
  419.   face(facingTemp)
  420. end
  421.  
  422. function unloadInventory()
  423.   task = "Unloading inventory"
  424.  
  425.   -- begin rednet
  426.   if broadcast then
  427.   end
  428.   -- end rednet
  429.  
  430.   face(facingHome)
  431.  
  432.   for i = 1,16 do
  433.     if not(ignoreInventory[i]) then
  434.       turtle.select(i)
  435.       turtle.drop()
  436.     end
  437.   end
  438. end
  439.    
  440. -- quadrants
  441. -- 1 2
  442. -- 4 3
  443. -- quadrant 1 will dig z = 0 between 1 and 4
  444. -- quadrant 2 will dig x = 0 between 2 and 1
  445. -- quadrant 3 will dig z = 0 between 3 and 2
  446. -- quadrant 4 will dig x = 0 between 4 and 3
  447.  
  448. -- digQuadrant
  449. -- Digging     -- Direction
  450. -- I********#  -- IBBBBBBBBB
  451. -- *XXXXXXXX#  -- BH<<<<<<<#
  452. -- *XXXXXXXX#  -- B>>>>>>>^#
  453. -- *XXXXXXXX#  -- B^<<<<<<<#
  454. -- *XXXXXXXX#  -- B>>>>>>>^#
  455. -- *XXXXXXXX#  -- B^<<<<<<<#
  456. -- *XXXXXXXX#  -- B>>>>>>>^#
  457. -- *XXXXXXXX#  -- B^<<<<<<<#
  458. -- *XXXXXXXX#  -- B>>>>>>>^#
  459. -- *XXXXXXXX0  -- B^<<<<<<<<
  460. --
  461. -- 0 = home x,z (mine shaft)
  462. -- X = dig and check up and down
  463. -- * = do not dig, but check
  464. -- # = will be dug by subsequent quadrant run
  465. -- B = border
  466. -- < = go in primary direction
  467. -- > = go opposite of primary direction
  468. -- ^ = go in secondary direction
  469. -- H = go home
  470. -- I = inaccessible
  471. --
  472. --
  473. -- This will be run 4 times for each level.
  474. -- Picture the above graphic with a push pin
  475. -- placed directly in the home (0).  Then
  476. -- rotate that 90 degrees for each run.
  477. function digQuadrant(quadrant,length)
  478.   length = length or radius
  479.  
  480.   task = "Mining quadrant " .. quadrant
  481.  
  482.   -- begin rednet
  483.   if broadcast then
  484.   end
  485.   -- end rednet
  486.  
  487.   pDirection = quadrant      -- primary direction   -  <
  488.   sDirection = quadrant + 1  -- secondary direction -  ^
  489.   if sDirection == 5 then
  490.     sDirection = 1
  491.   end
  492.   tDirection = quadrant + 2
  493.   if tDirection == 5 then     -- tertiary direction  -  >
  494.     tDirection = 1
  495.   elseif tDirection == 6 then
  496.     tDirection = 2
  497.   end
  498.  
  499.   evenDig = true
  500.   pEnd = false
  501.   sEnd = false
  502.  
  503.   trackForward("normal")
  504.  
  505.   for s = 1,length do    -- secondary
  506.     if s == length then
  507.       sEnd = true
  508.     end
  509.     for p = 1,length - 1 do  -- primary
  510.       if p == length - 1 then
  511.         pEnd = true
  512.       end
  513.      
  514.       if pEnd and sEnd and evenDig then
  515.         trackForward("forwardright")
  516.       elseif pEnd and sEnd and not(evenDig) then
  517.         trackForward("left")
  518.       elseif pEnd and not(sEnd) and evenDig then
  519.         trackForward("forward")
  520.       elseif pEnd and not(sEnd) and not(evenDig) then
  521.         trackForward("normal")
  522.       elseif not(pEnd) and sEnd and evenDig then
  523.         trackForward("right")
  524.       elseif not(pEnd) and sEnd and not(evenDig) then
  525.         trackForward("left")
  526.       elseif not(pEnd) and not(sEnd) and evenDig then
  527.         trackForward("normal")
  528.       elseif not(pEnd) and not(sEnd) and not(evenDig) then
  529.         trackForward("normal")
  530.       end
  531.     end
  532.    
  533.     if not(sEnd) and evenDig then
  534.       trackRight()
  535.       trackForward("left")
  536.       trackRight()
  537.       evenDig = false
  538.     elseif not(sEnd) and not(evenDig) then
  539.       trackLeft()
  540.       trackForward("normal")
  541.       trackLeft()
  542.       evenDig = true
  543.     end
  544.  
  545.     pEnd = false
  546.   end
  547.  
  548.   returnHome()
  549. end
  550.  
  551. function digLevel()
  552.   for i = 1,4 do
  553.     currentQuadrant = i
  554.     digQuadrant(i)
  555.   end
  556. end
  557.  
  558. function digMineShaft()
  559.   task = "Digging mine shaft"
  560.  
  561.   -- begin rednet
  562.   if broadcast then
  563.   end
  564.   -- end rednet
  565.  
  566.   while yActual + yCurrent ~= yDestination and continue do
  567.     trackDown(true)
  568.   end
  569. end
  570.  
  571. getParameters()
  572. initialize()
  573. digMineShaft()
  574. while currentLevel < yMaximum do
  575.   digLevel()
  576.   trackUp()
  577.   trackUp()
  578.   trackUp()
  579.   currentLevel = currentLevel + 3
  580. end
  581. returnHome()
  582. unloadInventory()
Advertisement
Add Comment
Please, Sign In to add comment