Advertisement
Te-ki

CCQuarry

Feb 27th, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.61 KB | None | 0 0
  1. --CCQuarry by Teki
  2. local tArgs = { ... }
  3. local monitor = term.current()
  4. local sizeX,sizeY = monitor.getSize()
  5. local messages
  6. local savedValues
  7. local savedFile
  8. local filePath = "ccquarry.save"
  9.  
  10. local targetX = 0
  11. local targetY = 0
  12. local targetZ = 0
  13. local curX = 0
  14. local curY = 0
  15. local curZ = 0
  16. local dir = 0
  17. local lastX = 0
  18. local lastY = 0
  19. local lastZ = 0
  20. local lastDir = 0
  21. local items = 0
  22. local alternate = false
  23. local needFuel = false
  24. local needClearInventory = false
  25. local done = false
  26. local tPause = false
  27. local steps = 3
  28.  
  29. local fuelSlot = 0
  30. local itemDetail
  31. local enderChest = 0
  32. local enderChestPlaced = false
  33. local relativeLastZ = 0
  34. local relativeTargetZ = 0
  35. local blocsLeftInCurLvl = 0
  36. local relativeLastY = 0
  37.  
  38. local id, msg
  39. local reponse = ""
  40. local side
  41. if peripheral.find("modem") == nil then
  42.     side = nil
  43. else
  44.     local sides = peripheral.getNames()
  45.     for i=1, #sides do
  46.         if peripheral.getType(sides[i]) == "modem" then
  47.             side = sides[i]
  48.         end
  49.     end
  50.     rednet.open(side)
  51. end
  52.  
  53. local fuelNames = {
  54.     "Coalfuel Can", --  1520 IndustrialCraft
  55.     "Lava", --  1000
  56.     "Biofuel Can", --   520 IndustrialCraft
  57.     "Coal Coke",    -- 320 RailCraft
  58.     "Blaze Rod", -- 120
  59.     "Scrap", -- 15 IndustrialCraft
  60.     "Wooden Scaffolding", --    15 IndustrialCraft
  61.     "Coal", --  80
  62.     "Charcoal", --  80
  63.     "Peat", --  80 Forestry
  64.     "Wood Blocks", --   15
  65.     --"Mushroom Blocks", -- 15
  66.     -- "Wooden Tools", --   10
  67.     "Stick", -- 5
  68.     "Sugar Cane" -- 0
  69. }
  70.  
  71. monitor.clear()
  72. monitor.setCursorPos(1,1)
  73.  
  74. local function roundTo(num, n)
  75.   local mult = 10^(n or 0)
  76.   return math.floor(num * mult + 0.5) / mult
  77. end
  78.  
  79. function pmsg(msg)
  80.     -- messages =  curX.." "..curY.." "..curZ.." "..dir.."\n"
  81.     -- ..lastX.." "..lastY.." "..lastZ.." "..lastDir.."\n"
  82.     -- ..targetX.." "..targetY.." "..targetZ.."\n"
  83.     -- ..tonumber(alternate).." "..tonumber(needFuel).." "..tonumber(needClearInventory).." "..tonumber(done)
  84.        
  85.     --monitor.clear()
  86.     monitor.setCursorPos(1,13)
  87.     monitor.clearLine()
  88.     monitor.write(msg)
  89.     --monitor.scroll(1)
  90.  
  91.     --textutils.slowPrint(msg, 20)
  92. end
  93.  
  94. function Save()
  95.     --pmsg("Save ...")
  96.     if dir > 3 then
  97.         dir = dir - 4
  98.     elseif dir < 0 then
  99.         dir = dir + 4
  100.     end
  101.    
  102.     savedFile = fs.open(filePath, "w")
  103.     savedValues = {
  104.         [1] = curX,
  105.         [2] = curY,
  106.         [3] = curZ,
  107.         [4] = dir,
  108.         [5] = lastX,
  109.         [6] = lastY,
  110.         [7] = lastZ,
  111.         [8] = lastDir,
  112.         [9] = targetX,
  113.         [10] = targetY,
  114.         [11] = targetZ,
  115.         [12] = tostring(alternate),
  116.         [13] = tostring(needFuel),
  117.         [14] = tostring(needClearInventory),
  118.         [15] = tostring(done),
  119.         [16] = tostring(enderChestPlaced)
  120.     }
  121.     savedFile.write(textutils.serialize(savedValues))
  122.     savedFile.flush()
  123.     savedFile.close()
  124. end
  125.  
  126. function FirstMove()
  127.     if curZ > targetZ + 1 then
  128.         if turtle.detectDown() then
  129.             turtle.digDown()
  130.         end
  131.         if turtle.down() then
  132.             curZ = curZ - 1
  133.             lastZ = curZ
  134.             Save()
  135.             if curZ > targetZ then
  136.                 if turtle.detectDown() then
  137.                     turtle.digDown()
  138.                 end
  139.             end
  140.         end
  141.     end
  142.     if curZ < targetZ - 1 then
  143.         if turtle.detectUp() then
  144.             turtle.digUp()
  145.         end
  146.         if turtle.up() then
  147.             curZ = curZ + 1
  148.             lastZ = curZ
  149.             Save()
  150.             if curZ < targetZ then
  151.                 if turtle.detectUp() then
  152.                     turtle.digUp()
  153.                 end
  154.             end
  155.         end
  156.     end
  157. end
  158.  
  159. function SetStartup()
  160.     if fs.exists("startup") then
  161.         fs.move("startup","startup.old")
  162.     end
  163.     shell.run("pastebin get 1yr45aRW startup")
  164. end
  165.  
  166. function WaitForRefuel()
  167.     relativeLastZ = lastZ
  168.     if lastZ < 0 then
  169.         relativeLastZ = -lastZ
  170.     end
  171.    
  172.     pmsg("Need fuel, going Home ...")
  173.     GoTo(0,0,0,2)
  174.     while  turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ)*2 + targetX+targetY do
  175.         pmsg("Waiting for fuel ...")
  176.         local myTimer = os.startTimer(5)
  177.         while true do
  178.             local event, timerID = os.pullEvent()
  179.             if (event == "timer" and timerID == myTimer) or event == "turtle_inventory" then break end
  180.         end
  181.         Refuel()
  182.     end
  183.     Unload()
  184. end
  185.  
  186. function Refuel()
  187.     pmsg("Refueling ...")
  188.     SearchEnderChest()
  189.     relativeLastZ = lastZ
  190.     if lastZ < 0 then
  191.         relativeLastZ = -lastZ
  192.     end
  193.    
  194.     if fuelSlot ~= nil and fuelSlot ~= 0 then
  195.         turtle.select(fuelSlot)
  196.         if turtle.refuel(1) then
  197.             while turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ)*2 and turtle.getItemCount(fuelSlot) > 0 do
  198.                 turtle.refuel(1)
  199.             end
  200.             if turtle.getItemCount(fuelSlot) == 0 then
  201.                 fuelSlot = 0
  202.             end
  203.         else
  204.             fuelSlot = 0
  205.         end
  206.     end
  207.    
  208.     for i=1, 16 do
  209.         if fuelSlot == 0 and i ~= enderChest then
  210.             turtle.select(i)
  211.             if turtle.refuel(1) then
  212.                 while turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ)*2 and turtle.getItemCount(i) > 0 do
  213.                     turtle.refuel(1)
  214.                     pmsg(turtle.getFuelLevel() .. " " .. (lastX+lastY+relativeLastZ)*2)
  215.                 end
  216.                 if turtle.getItemCount(i) > 0 then
  217.                     fuelSlot = i
  218.                 else
  219.                     fuelSlot = 0
  220.                 end
  221.             end
  222.         end
  223.     end
  224.    
  225.     if fuelSlot == 0 and enderChest ~= 0 and turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ)*2 then
  226.         fuelSlot = 1
  227.         while turtle.getItemCount(fuelSlot) > 0 and fuelSlot < 16 do
  228.             fuelSlot = fuelSlot+1
  229.         end
  230.         if turtle.getItemCount(fuelSlot) > 0 then
  231.             Unload()
  232.         else
  233.             if not enderChestPlaced then
  234.                 turtle.select(enderChest)
  235.                 if curZ < 0 or curZ < targetZ then
  236.                     turtle.placeUp()
  237.                 else
  238.                     turtle.placeDown()
  239.                 end
  240.                 enderChestPlaced = true
  241.                 Save()
  242.             end
  243.             turtle.select(fuelSlot)
  244.             if curZ < 0 or curZ < targetZ then
  245.                 turtle.suckUp()
  246.             else
  247.                 turtle.suckDown()
  248.             end
  249.             while turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ)*2 and turtle.getItemCount(fuelSlot) > 0 do
  250.                 if turtle.refuel(1) then
  251.                     -- if curZ < 0 or curZ < targetZ then
  252.                         -- turtle.suckUp()
  253.                     -- else
  254.                         -- turtle.suckDown()
  255.                     -- end
  256.                 else
  257.                     if curZ < 0 or curZ < targetZ then
  258.                         turtle.dropUp()
  259.                     else
  260.                         turtle.dropDown()
  261.                     end
  262.                 end
  263.             end
  264.             turtle.select(enderChest)
  265.             if curZ < 0 or curZ < targetZ then
  266.                 turtle.digUp()
  267.             else
  268.                 turtle.digDown()
  269.             end
  270.             enderChestPlaced = false
  271.             Save()
  272.         end
  273.     end
  274. end
  275.  
  276. function CheckFuel()
  277.     pmsg("Fuel Check ...")
  278.     relativeLastZ = lastZ
  279.     if lastZ < 0 then
  280.         relativeLastZ = -lastZ
  281.     end
  282.    
  283.     if turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ+1) then
  284.         Refuel()
  285.     end
  286.    
  287.     if not needFuel and turtle.getFuelLevel() <= (lastX+lastY+relativeLastZ+1) then
  288.         needFuel = true
  289.         WaitForRefuel()
  290.     elseif needFuel then
  291.         needFuel = false
  292.         GoTo(lastX,lastY,lastZ,lastDir)
  293.     end
  294. end
  295.  
  296. function Unload()
  297.     pmsg("Unloading ...")
  298.     SearchEnderChest()
  299.     if enderChest ~= 0 then
  300.         if not enderChestPlaced then
  301.             turtle.select(enderChest)
  302.             if curZ < 0 or curZ < targetZ then
  303.                 turtle.placeUp()
  304.             else
  305.                 turtle.placeDown()
  306.             end
  307.             enderChestPlaced = true
  308.             Save()
  309.         end
  310.     else
  311.         Refuel()
  312.         GoTo(0,0,0,2)
  313.     end
  314.    
  315.     for i=1, 16 do
  316.         if i ~= fuelSlot then
  317.             turtle.select(i)
  318.             itemDetail = turtle.getItemDetail()
  319.             if itemDetail ~= nil and itemDetail.name ~= "EnderStorage:enderChest" and itemDetail.name ~= "enderstorage:ender_storage" then
  320.                 if enderChest ~= 0 then
  321.                     if curZ < 0 or curZ < targetZ then
  322.                         while turtle.getItemCount(i) > 0 and not turtle.dropUp() do
  323.                         end
  324.                     else
  325.                         while turtle.getItemCount(i) > 0 and not turtle.dropDown() do
  326.                         end
  327.                     end
  328.                 else
  329.                     while turtle.getItemCount(i) > 0 and not turtle.drop() do
  330.                     end
  331.                 end
  332.             end
  333.         end
  334.     end
  335.    
  336.     if enderChest ~= 0 then
  337.         turtle.select(enderChest)
  338.         if curZ < 0 or curZ < targetZ then
  339.             turtle.digUp()
  340.         else
  341.             turtle.digDown()
  342.         end
  343.         enderChestPlaced = false
  344.         Save()
  345.     end
  346.    
  347.     items = 0
  348.     for i=1, 16 do
  349.         if turtle.getItemCount(i) > 0 then
  350.             items = items + 1
  351.         end
  352.     end
  353.    
  354.     if items > 2 then
  355.         needClearInventory = true
  356.     else
  357.         needClearInventory = false
  358.        
  359.         if not done then
  360.             CheckFuel()
  361.             GoTo(lastX,lastY,lastZ,lastDir)
  362.         else
  363.             GoTo(0,0,0,0)
  364.         end
  365.     end
  366. end
  367.  
  368. function CheckInventory()
  369.     pmsg("Inventory Check ...")
  370.     items = 0
  371.     for i=1, 16 do
  372.         if turtle.getItemCount(i) > 0 then
  373.             items = items + 1
  374.         end
  375.     end
  376.    
  377.     if items > 14 or needClearInventory then
  378.         needClearInventory = true
  379.         while needClearInventory do
  380.             Unload()
  381.         end
  382.     end
  383. end
  384.  
  385. function SearchEnderChest()
  386.     if enderChestPlaced then
  387.         for i=1, 16 do
  388.             turtle.select(i)
  389.             if turtle.getItemCount(i) == 0 then
  390.                 enderChest = i
  391.             end
  392.         end
  393.     else
  394.         for i=1, 16 do
  395.             turtle.select(i)
  396.             itemDetail = turtle.getItemDetail()
  397.             if itemDetail ~= nil then
  398.                 if itemDetail.name == "EnderStorage:enderChest" then
  399.                     enderChest = i
  400.                 elseif itemDetail.name == "enderstorage:ender_storage" then
  401.                     enderChest = i
  402.                 end
  403.             end
  404.         end
  405.     end
  406. end
  407.  
  408. function tUp()
  409.     if turtle.up() then
  410.         curZ = curZ + 1
  411.         Save()
  412.     else
  413.         while turtle.detectUp() and turtle.digUp() do
  414.         end
  415.         if not turtle.detectUp() then
  416.             turtle.attackUp()
  417.         end
  418.     end
  419. end
  420.  
  421. function tDown()
  422.     if turtle.down() then
  423.         curZ = curZ - 1
  424.         Save()
  425.     else
  426.         while turtle.detectDown() and turtle.digDown() do
  427.         end
  428.         if not turtle.detectDown() then
  429.             turtle.attackDown()
  430.         end
  431.     end
  432. end
  433.  
  434. function tForward(toX, toY)
  435.     if curZ < 0 or curZ < targetZ then
  436.         while turtle.detectUp() and turtle.digUp() do
  437.         end
  438.     end
  439.     if curZ > targetZ or curZ > 0 then
  440.         while turtle.detectDown() and turtle.digDown() do
  441.         end
  442.     end
  443.     if (curX == 0 and curY == 0 and curZ == 0) then
  444.     else
  445.         if (dir == 0 and curX < toX) or (dir == 2 and curX > 0) or (dir == 1 and curY < toY) or (dir == 3 and curY > 0) then
  446.             while turtle.detect() and turtle.dig() do
  447.             end
  448.         end
  449.     end
  450.    
  451.     if dir == 0 then
  452.         if curX < toX then
  453.             if turtle.forward() then
  454.                 curX = curX + 1
  455.                 Save()
  456.             elseif not turtle.detect() then
  457.                 turtle.attack()
  458.             end
  459.         else
  460.             if curY < toY then
  461.                 turtle.turnRight()
  462.                 dir = 1
  463.                 Save()
  464.             else
  465.                 turtle.turnLeft()
  466.                 dir = 3
  467.                 Save()
  468.             end
  469.         end
  470.     elseif dir == 1 then
  471.         if curY < toY and curX == toX then
  472.             if turtle.forward() then
  473.                 curY = curY + 1
  474.                 Save()
  475.             elseif not turtle.detect() then
  476.                 turtle.attack()
  477.             end
  478.         else
  479.             if curX < toX then
  480.                 turtle.turnLeft()
  481.                 dir = 0
  482.                 Save()
  483.             else
  484.                 turtle.turnRight()
  485.                 dir = 2
  486.                 Save()
  487.             end
  488.         end
  489.     elseif dir == 2 then
  490.         if curX > toX and curY == toY then
  491.             if turtle.forward() then
  492.                 curX = curX - 1
  493.                 Save()
  494.             elseif not turtle.detect() then
  495.                 turtle.attack()
  496.             end
  497.         else
  498.             if curY < toY then
  499.                 turtle.turnLeft()
  500.                 dir = 1
  501.                 Save()
  502.             else
  503.                 turtle.turnRight()
  504.                 dir = 3
  505.                 Save()
  506.             end
  507.         end
  508.     elseif dir == 3 then
  509.         if curY > toY then
  510.             if turtle.forward() then
  511.                 curY = curY - 1
  512.                 Save()
  513.             elseif not turtle.detect() then
  514.                 turtle.attack()
  515.             end
  516.         else
  517.             if curX < toX then
  518.                 turtle.turnRight()
  519.                 dir = 0
  520.                 Save()
  521.             else
  522.                 turtle.turnLeft()
  523.                 dir = 2
  524.                 Save()
  525.             end
  526.         end
  527.     end
  528. end
  529.  
  530. function GoUp(toZ)
  531.     toZ = tonumber(toZ)
  532.     while curZ < toZ do
  533.         tUp()
  534.     end
  535. end
  536.  
  537. function GoDown(toZ)
  538.     toZ = tonumber(toZ)
  539.     while curZ > toZ do
  540.         tDown()
  541.     end
  542. end
  543.  
  544. function GoXY(toX, toY)
  545.     toX = tonumber(toX)
  546.     toY = tonumber(toY)
  547.     while curX ~= toX or curY ~= toY do
  548.         tForward(toX, toY)
  549.     end
  550. end
  551.  
  552. function GoTo(toX, toY, toZ, toDir)
  553.     toX = tonumber(toX)
  554.     toY = tonumber(toY)
  555.     toZ = tonumber(toZ)
  556.     toDir = tonumber(toDir)
  557.    
  558.     if targetZ > 0 then
  559.         GoDown(toZ)
  560.         GoXY(toX,toY)
  561.         GoUp(toZ)
  562.     else
  563.         GoUp(toZ)
  564.         GoXY(toX,toY)
  565.         GoDown(toZ)
  566.     end
  567.    
  568.     while dir ~= toDir do
  569.         if not tPause then
  570.             if dir > toDir then
  571.                 if dir - toDir >= 2 then
  572.                     turtle.turnRight()
  573.                     dir = dir + 1
  574.                 else
  575.                     turtle.turnLeft()
  576.                     dir = dir - 1
  577.                 end
  578.             elseif dir < toDir then
  579.                 if toDir - dir >= 2 then
  580.                     turtle.turnLeft()
  581.                     dir = dir - 1
  582.                 else
  583.                     turtle.turnRight()
  584.                     dir = dir + 1
  585.                 end
  586.             end
  587.             Save()
  588.         else
  589.             sleep(0)
  590.         end
  591.     end
  592. end
  593.  
  594. function Forwards()
  595.     if curZ < 0 or curZ < targetZ then
  596.         if turtle.detectUp() then
  597.             while turtle.detectUp() and turtle.digUp() do
  598.             end
  599.         end
  600.     end
  601.     if curZ > targetZ or curZ > 0 then
  602.         if turtle.detectDown() then
  603.             while turtle.detectDown() and turtle.digDown() do
  604.             end
  605.         end
  606.     end
  607.     if turtle.detect() then
  608.         while turtle.detect() and turtle.dig() do
  609.         end
  610.     end
  611.     if turtle.forward() then
  612.         if dir == 0 then
  613.             curX = curX + 1
  614.         elseif dir == 1 then
  615.             curY = curY + 1
  616.         elseif dir == 2 then
  617.             curX = curX -1
  618.         elseif dir == 3 then
  619.             curY = curY - 1
  620.         end
  621.         lastX = curX
  622.         lastY = curY
  623.         Save()
  624.         if curZ < 0 or curZ < targetZ then
  625.             if turtle.detectUp() then
  626.                 while turtle.detectUp() and turtle.digUp() do
  627.                 end
  628.             end
  629.         end
  630.         if curZ > targetZ or curZ > 0 then
  631.             if turtle.detectDown() then
  632.                 while turtle.detectDown() and turtle.digDown() do
  633.                 end
  634.             end
  635.         end
  636.     else
  637.         if not turtle.detect() then
  638.             turtle.attack()
  639.             turtle.attackUp()
  640.             -- Forwards()
  641.         else
  642.             done = true
  643.             GoTo(0,0,0,2)
  644.         end
  645.     end
  646. end
  647.  
  648. function Downwards()
  649.     if curZ > targetZ + 1 and not done then
  650.         turtle.turnLeft()
  651.         dir = dir - 1
  652.         lastDir = dir
  653.         Save()
  654.         turtle.turnLeft()
  655.         lastDir = dir
  656.         dir = dir - 1
  657.         Save()
  658.         if turtle.down() then
  659.             curZ = curZ - 1
  660.             lastZ = curZ
  661.             Save()
  662.             alternate = not alternate
  663.             if curZ > targetZ then
  664.                 if turtle.detectDown() then
  665.                     turtle.digDown()
  666.                 end
  667.                 if turtle.down() then
  668.                     curZ = curZ -1
  669.                     lastZ = curZ
  670.                     Save()
  671.                     if curZ > targetZ + 1 then
  672.                         if turtle.detectDown() then
  673.                             turtle.digDown()
  674.                         end
  675.                         if turtle.down() then
  676.                             curZ = curZ -1
  677.                             lastZ = curZ
  678.                             Save()
  679.                             if turtle.detectDown() then
  680.                                 turtle.digDown()
  681.                             end
  682.                         end
  683.                     end
  684.                 else
  685.                     done = true
  686.                 end
  687.             end
  688.         else
  689.             if not turtle.detectDown() then
  690.                 turtle.attackDown()
  691.                 -- Downwards()
  692.             else
  693.                 done = true
  694.             end
  695.         end
  696.     else
  697.         done = true
  698.     end
  699. end
  700.  
  701. function Upwards()
  702.     if curZ < targetZ - 1 and not done then
  703.         turtle.turnLeft()
  704.         dir = dir + 1
  705.         lastDir = dir
  706.         Save()
  707.         turtle.turnLeft()
  708.         lastDir = dir
  709.         dir = dir + 1
  710.         Save()
  711.         if turtle.up() then
  712.             curZ = curZ + 1
  713.             lastZ = curZ
  714.             Save()
  715.             alternate = not alternate
  716.             if curZ < targetZ then
  717.                 if turtle.detectUp() then
  718.                     while turtle.detectUp() and turtle.digUp() do
  719.                     end
  720.                 end
  721.                 if turtle.up() then
  722.                     curZ = curZ + 1
  723.                     lastZ = curZ
  724.                     Save()
  725.                     if curZ < targetZ - 1 then
  726.                         if turtle.detectUp() then
  727.                             while turtle.detectUp() and turtle.digUp() do
  728.                             end
  729.                         end
  730.                         if turtle.up() then
  731.                             curZ = curZ + 1
  732.                             lastZ = curZ
  733.                             Save()
  734.                             if turtle.detectUp() then
  735.                                 while turtle.detectUp() and turtle.digUp() do
  736.                                 end
  737.                             end
  738.                         end
  739.                     end
  740.                 end
  741.             end
  742.         else
  743.             if not turtle.detectUp() then
  744.                 turtle.attackUp()
  745.                 -- Upwards()
  746.             else
  747.                 done = true
  748.                 GoTo(0,0,0,2)
  749.             end
  750.         end
  751.     else
  752.         done = true
  753.         GoTo(0,0,0,2)
  754.     end
  755. end
  756.  
  757. function Move()
  758.     if dir == 0 then
  759.         if curX < targetX then
  760.             Forwards()
  761.         elseif not alternate and curY < targetY then
  762.             turtle.turnRight()
  763.             dir = 1
  764.         elseif alternate and curY > 0 then
  765.             turtle.turnLeft()
  766.             dir = 3
  767.         elseif targetZ < 0 then
  768.             Downwards()
  769.         else
  770.             Upwards()
  771.         end
  772.     elseif dir == 1 then
  773.         if curY < targetY then
  774.             Forwards()
  775.             if curX == 0 then
  776.                 turtle.turnLeft()
  777.                 dir = 0
  778.             elseif curX == targetX then
  779.                 turtle.turnRight()
  780.                 dir = 2
  781.             end
  782.         end
  783.     elseif dir == 2 then
  784.         if curX > 0 then
  785.             Forwards()
  786.         elseif not alternate and curY < targetY then
  787.             turtle.turnLeft()
  788.             dir = 1
  789.         elseif alternate and curY > 0 then
  790.             turtle.turnRight()
  791.             dir = 3
  792.         elseif targetZ < 0 then
  793.             Downwards()
  794.         else
  795.             Upwards()
  796.         end
  797.     elseif dir == 3 then
  798.         if curY > 0 then
  799.             Forwards()
  800.             if curX == targetX then
  801.                 turtle.turnLeft()
  802.                 dir = 2
  803.             elseif curX == 0 then
  804.                 turtle.turnRight()
  805.                 dir = 0
  806.             end
  807.         end
  808.     end
  809.    
  810.     lastDir = dir
  811.     Save()
  812. end
  813.  
  814. if #tArgs == 1 then
  815.     if tArgs[1] == "del" then
  816.         fs.delete(filePath)
  817.         return 0
  818.     else
  819.         targetZ = tonumber(tArgs[1])
  820.         if targetZ < 0 then
  821.             targetZ = (targetZ+1) * -1
  822.             targetX = targetZ
  823.             targetY = targetZ
  824.         else
  825.             targetZ = (targetZ-1) * -1
  826.             targetX = targetZ * -1
  827.             targetY = targetZ * -1
  828.         end
  829.         SetStartup()
  830.         CheckFuel()
  831.         FirstMove()
  832.     end
  833. elseif #tArgs == 3 then
  834.     targetX = tonumber(tArgs[1])-1
  835.     targetY = tonumber(tArgs[2])-1
  836.     targetZ = tonumber(tArgs[3])
  837.     if targetZ < 0 then
  838.         targetZ = (targetZ+1) * -1
  839.     else
  840.         targetZ = (targetZ-1) * -1
  841.     end
  842.     SetStartup()
  843.     CheckFuel()
  844.     FirstMove()
  845. else
  846.     if fs.exists(filePath) and fs.getSize(filePath) > 0 and #tArgs == 0 then
  847.         savedFile = fs.open(filePath, "r")
  848.         savedValues = textutils.unserialize(savedFile.readAll())
  849.         if savedValues[15] == "false" or (savedValues[15] == "true" and tonumber(savedValues[1])+tonumber(savedValues[2])-tonumber(savedValues[3]) ~= 0) then
  850.             pmsg("Trying to restart ...")
  851.             curX = tonumber(savedValues[1])
  852.             curY = tonumber(savedValues[2])
  853.             curZ = tonumber(savedValues[3])
  854.             dir = tonumber(savedValues[4])
  855.             lastX = tonumber(savedValues[5])
  856.             lastY = tonumber(savedValues[6])
  857.             lastZ = tonumber(savedValues[7])
  858.             lastDir = tonumber(savedValues[8])
  859.             targetX = tonumber(savedValues[9])
  860.             targetY = tonumber(savedValues[10])
  861.             targetZ = tonumber(savedValues[11])
  862.             alternate = savedValues[12] == "true"
  863.             needFuel = savedValues[13] == "true"
  864.             needClearInventory = savedValues[14] == "true"
  865.             done = savedValues[15] == "true"
  866.             enderChestPlaced = savedValues[16] == "true"
  867.         else
  868.             savedValues = nil
  869.         end
  870.         savedFile.close()
  871.     end
  872.     if savedValues == nil then
  873.         print("help: ccquarry <1> <2> <3>")
  874.         print("      <1> : X Y and Z")
  875.         print("or:")
  876.         print("      <1> : X")
  877.         print("      <2> : Y")
  878.         print("      <3> : Z")
  879.         return 0
  880.     end
  881. end
  882.  
  883. function draw()
  884.     relativeLastZ = lastZ
  885.     if lastZ < 0 then
  886.         relativeLastZ = -lastZ
  887.     end
  888.     relativeTargetZ = targetZ
  889.     if targetZ < 0 then
  890.         relativeTargetZ = -targetZ
  891.     end
  892.     if alternate then
  893.         if dir == 0 or (dir == 3 and (lastY+1)/2 ~= roundTo(lastY/2)) then
  894.             blocsLeftInCurLvl = (((targetX+1)-(lastX+1)) + ((targetX+1)*(lastY)))
  895.         elseif dir == 2 or (dir == 3 and (lastY+1)/2 == roundTo(lastY/2)) then
  896.             blocsLeftInCurLvl = ((lastX) + ((targetX+1)*(lastY)))
  897.         end
  898.     else
  899.         if dir == 0 or (dir == 1 and (lastY+1)/2 ~= roundTo(lastY/2)) then
  900.             blocsLeftInCurLvl = (((targetX+1)-(lastX+1)) + ((targetX+1)*((targetY+1)-(lastY+1))))
  901.         elseif dir == 2 or (dir == 1 and (lastY+1)/2 == roundTo(lastY/2)) then
  902.             blocsLeftInCurLvl = ((lastX) + ((targetX+1)*((targetY+1)-(lastY+1))))
  903.         end
  904.     end
  905.    
  906.     if (relativeTargetZ+1) - (relativeLastZ-1) > 2 then
  907.         blocsLeftInCurLvl = blocsLeftInCurLvl * 3
  908.     elseif (relativeTargetZ+1) - (relativeLastZ+1) > 1 then
  909.         blocsLeftInCurLvl = blocsLeftInCurLvl * 2
  910.     end
  911.    
  912.     monitor.setBackgroundColour(colors.black)
  913.     monitor.setTextColor(colors.white)
  914.    
  915.     -- Blocks Left
  916.     monitor.setCursorPos(1,1)
  917.     monitor.clearLine()
  918.     monitor.write("Blocks left  : " .. (((targetX+1) * (targetY+1) * (relativeTargetZ+1)) - ((targetX+1) * (targetY+1) * (relativeLastZ+2)) + blocsLeftInCurLvl))
  919.     -- Moves Left
  920.     monitor.setCursorPos(1,2)
  921.     monitor.clearLine()
  922.     monitor.write("Moves left  : " .. (((targetX+1) * (targetY+1) * (relativeTargetZ+1)) - ((targetX+1) * (targetY+1) * (relativeLastZ+2)) + blocsLeftInCurLvl)/3)
  923.     -- Fuel Left
  924.     monitor.setCursorPos(1,3)
  925.     monitor.clearLine()
  926.     monitor.write("Fuel left  : " .. turtle.getFuelLevel())
  927.    
  928.     -- Refuel button
  929.     monitor.setBackgroundColour(colors.white)
  930.     monitor.setTextColor(colors.black)
  931.     monitor.setCursorPos(sizeX-5, 1)
  932.     monitor.write("R")
  933.     monitor.setBackgroundColour(colors.black)
  934.     monitor.setTextColor(colors.white)
  935.     monitor.setCursorPos(sizeX-4, 1)
  936.     monitor.write("EFUEL")
  937.    
  938.     -- Pause Button
  939.     if tPause then
  940.         monitor.setCursorPos(sizeX-6, 2)
  941.         monitor.write("UN")
  942.     end
  943.     monitor.setBackgroundColour(colors.white)
  944.     monitor.setTextColor(colors.black)
  945.     monitor.setCursorPos(sizeX-4, 2)
  946.     monitor.write("P")
  947.     monitor.setBackgroundColour(colors.black)
  948.     monitor.setTextColor(colors.white)
  949.     monitor.setCursorPos(sizeX-3, 2)
  950.     monitor.write("AUSE")
  951.    
  952.     -- Coords
  953.     monitor.setCursorPos(1,4)
  954.     monitor.clearLine()
  955.     monitor.write("Target : " .. targetX.." "..targetY.." "..targetZ)
  956.     monitor.setCursorPos(1,5)
  957.     monitor.clearLine()
  958.     monitor.write("Current : " .. curX.." "..curY.." "..curZ.." "..dir)
  959.     monitor.setCursorPos(1,6)
  960.     monitor.clearLine()
  961.     monitor.write("Last : " .. lastX.." "..lastY.." "..lastZ.." "..lastDir)
  962.    
  963.     -- Fuel Slot
  964.     monitor.setCursorPos(1,7)
  965.     monitor.clearLine()
  966.     local itemDetails = ""
  967.     if fuelSlot ~= 0 then turtle.getItemDetail(fuelslot) end
  968.     monitor.write("Fuel Slot : " .. fuelSlot .. " " .. (itemDetails))
  969.    
  970.     -- Ender Slot
  971.     monitor.setCursorPos(1,8)
  972.     monitor.clearLine()
  973.     monitor.write("EnderChest Slot : " .. enderChest)
  974.    
  975.     sleep(0.5)
  976. end
  977.  
  978. draw()
  979.  
  980. function catchEvents()
  981.     if side ~= nil then
  982.         parallel.waitForAny(catchTermEvents, catchRednetEvents)
  983.     else
  984.         catchTermEvents()
  985.     end
  986. end
  987.  
  988. function catchTermEvents()
  989.     --local event, side, xPos, yPos = os.pullEvent("mouse_click")
  990.    
  991.     while true do
  992.         local event, side, xPos, yPos = os.pullEvent()
  993.         if event == "char" then
  994.             if side == "R" or side == "r" then
  995.                 Refuel()
  996.             elseif side == "P" or side == "p"then
  997.                 tPause = not tPause
  998.             end
  999.             break
  1000.         end
  1001.         if event == "mouse_click" then
  1002.             if (xPos >= sizeX-5 and xPos <= sizeX) and (yPos == 1) then
  1003.                 Refuel()
  1004.             elseif (xPos >= sizeX-6 and xPos <= sizeX) and (yPos == 2) then
  1005.                 tPause = not tPause
  1006.             end
  1007.             break
  1008.         end
  1009.     end
  1010. end
  1011.  
  1012. function catchRednetEvents()
  1013.     id, msg = rednet.receive("CCQuarryPortable", 5)
  1014.    
  1015.     if msg == "getAll" then
  1016.         reponse = {
  1017.             [1] = curX,
  1018.             [2] = curY,
  1019.             [3] = curZ,
  1020.             [4] = dir,
  1021.             [5] = lastX,
  1022.             [6] = lastY,
  1023.             [7] = lastZ,
  1024.             [8] = lastDir,
  1025.             [9] = targetX,
  1026.             [10] = targetY,
  1027.             [11] = targetZ,
  1028.             [12] = tostring(alternate),
  1029.             [13] = tostring(needFuel),
  1030.             [14] = tostring(needClearInventory),
  1031.             [15] = tostring(done),
  1032.             [16] = tostring(enderChestPlaced)
  1033.         }
  1034.         rednet.send(id, reponse, "CCQuarry")
  1035.     end
  1036. end
  1037.  
  1038. function mainLoop()
  1039.     while true do
  1040.         sleep(0)
  1041.         if not tPause then
  1042.             CheckInventory()
  1043.             CheckFuel()
  1044.             if not needFuel and not needClearInventory and not done then
  1045.                 Move()
  1046.             end
  1047.            
  1048.             if done and curX == 0 and curY == 0 and curZ == 0 then
  1049.                 Unload()
  1050.                 pmsg("Done !")
  1051.                 fs.delete(filePath)
  1052.                 fs.delete("startup")
  1053.                 if fs.exists("startup.old") then
  1054.                     fs.move("startup.old","startup")
  1055.                 end
  1056.                 return 0
  1057.             elseif done and (curX ~= 0 or curY ~= 0 or curZ ~= 0) then
  1058.                 GoTo(0,0,0,2)
  1059.             end
  1060.         end
  1061.     end
  1062. end
  1063.  
  1064. function drawLoop()
  1065.     while not done do
  1066.         parallel.waitForAny(catchEvents, draw)
  1067.     end
  1068. end
  1069.  
  1070. SearchEnderChest()
  1071. parallel.waitForAll(mainLoop, drawLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement