Advertisement
Alarindris

Untitled

Oct 20th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.79 KB | None | 0 0
  1. --Designate inventory slots
  2. STONE_SLOT          = 1
  3. COBBLE_SLOT         = 2
  4. GRAVEL_SLOT         = 3
  5. DIRT_SLOT           = 4
  6. SAND_SLOT           = 5
  7. TORCH_SLOT          = 6
  8. ENDER_CHEST_SLOT    = 7
  9.  
  10. INV_COUNT           = 7                     --total number of inventory slots      
  11. FUEL_SLOT           = 16
  12. TOTAL_SLOTS         = 16                    --number of slots in the turtle
  13.  
  14. --Set globals
  15. MIN_FUEL_COUNT      = 16                    --minimum number of fuel items
  16. TORCH_SPACE         = 10                    --number of block before placing a torch
  17.  
  18. --Instantiate random shit
  19. CURRENT_SLOT        = STONE_SLOT            --if the turtle is not facing stone
  20. LAST_SLOT           = STONE_SLOT            --then you are doing it wrong
  21.  
  22. DISTANCE            = 0                     --tracks total spaces moved forward
  23. TORCHES_USED        = 0                    
  24. FUEL_USED           = 0                     --tracks units of fuel used, not blocks used
  25. LAST_FUEL           = 0                     --previous FUEL_USED
  26. ORE                 = 0                     --total amount of ore mined
  27. TOTAL_BLOCKS        = 0                     --total amount of blocks dug
  28. TOTAL_SHAFTS        = 4
  29. SHAFT_WIDTH         = 2
  30. SHAFT_LENGTH        = 5
  31.  
  32. --Hilbert variablesr
  33. H_REPETITION        = 2                     --Hilbert depth
  34. H_SEG_LENGTH        = 3                     --Length of Hilbert segment
  35.  
  36. -- Hilbert function
  37. function A(depth)  
  38.    if depth < 1 then return end
  39.    turtle.turnLeft()
  40.    B(depth - 1)
  41.    TunnelForward(H_SEG_LENGTH)
  42.    turtle.turnRight()
  43.    A(depth - 1)
  44.    TunnelForward(H_SEG_LENGTH)
  45.    A(depth - 1)
  46.    turtle.turnRight()
  47.    TunnelForward(H_SEG_LENGTH)
  48.    B(depth - 1)
  49.    turtle.turnLeft()
  50. end
  51.  
  52. function B(depth)
  53.  
  54.    if depth < 1 then return end
  55.    turtle.turnRight()
  56.    A(depth - 1)
  57.    TunnelForward(H_SEG_LENGTH)
  58.    turtle.turnLeft()
  59.    B(depth - 1)
  60.    TunnelForward(H_SEG_LENGTH)
  61.    B(depth - 1)
  62.    turtle.turnLeft()
  63.    TunnelForward(H_SEG_LENGTH)
  64.    A(depth - 1)
  65.    turtle.turnRight()
  66.  
  67. end
  68.  
  69. function BlockHandler( here )              
  70.     if Detect(here) then
  71.         if IsOre(here) then MineOre(here) end
  72.     end
  73. end
  74.  
  75. function CheckInventory( onInit )
  76.     print("Checking inventory...") 
  77.     local n = 0
  78.     local invThresh = 13                        --dump inventory after this many slots are full
  79.     local slotReorg = 14                                --number of slots to reorgainze
  80.    
  81.     for i = INV_COUNT+1, TOTAL_SLOTS - 1 do     --first consolidate fuel items into one slot
  82.         Select(FUEL_SLOT)
  83.         if turtle.compareTo(i) then
  84.             turtle.select(i)
  85.             CURRENT_SLOT = i
  86.             turtle.transferTo(FUEL_SLOT)   
  87.         end
  88.         Select(LAST_SLOT)
  89.     end
  90.    
  91.     for i = 1, slotReorg do
  92.         if turtle.getItemCount(i) > 0 then      --slot used, reorganize, checking for duplicate slots
  93.             n = n + 1
  94.             CheckSlotDupes(i)
  95.         end
  96.     end
  97.    
  98.     result = n
  99.    
  100.     if n > invThresh then                       --dump inventory into a chest after threshold level
  101.         result = DumpInventory("forward")
  102.     end
  103.     print("Done")
  104.     return result
  105. end
  106.  
  107. function CheckSlotDupes ( index )   --check inventory slots for duplicate items, condense
  108.     local i
  109.     local begin
  110.     if index < INV_COUNT+1 then begin = INV_COUNT+1
  111.     else begin = index + 1 end
  112.     Select(index)
  113.     for i = begin, TOTAL_SLOTS - 1 do
  114.         if turtle.compareTo(i) then
  115.             turtle.select(i)
  116.             CURRENT_SLOT = i
  117.             turtle.transferTo(index)
  118.         end
  119.     end
  120.     Select(LAST_SLOT)
  121. end
  122.    
  123. function Compare( here )    --compare selected slot to block
  124.     if here == "up" then return turtle.compareUp()
  125.     elseif here == "down" then return turtle.compareDown()
  126.     elseif here == "forward" then return turtle.compare() end return false end
  127.  
  128. function Detect ( here )    --detect if block is air/water/lava or mineable
  129.     if here == "up" then return turtle.detectUp()
  130.     elseif here == "down" then return turtle.detectDown()
  131.     elseif here == "forward" then return turtle.detect() end return false end
  132.  
  133. function Drop ( thisSlot, thisMany )
  134.     turtle.select(thisSlot)             --if thisMany == nil then everything will be dropped
  135.     turtle.drop(thisMany)
  136. end
  137.    
  138. function DumpInventory( here ) --drops chest and fills it
  139.     local i, g, result
  140.     result = -1
  141.     print("Placing chest...")
  142.     RemoveBlock("forward")
  143.     Place(ENDER_CHEST_SLOT, here)
  144.     for i = INV_COUNT+1, TOTAL_SLOTS - 1 do
  145.         print(i)
  146.         Drop(i, 64)
  147.         sleep(.1)
  148.     end
  149.     Select(LAST_SLOT)
  150.     FuelCheck()
  151.     g = turtle.getItemCount(FUEL_SLOT)      --dump fuel items if we have too many
  152.     if g > MIN_FUEL_COUNT then
  153.         Drop(FUEL_SLOT, g - MIN_FUEL_COUNT)
  154.         Select(LAST_SLOT)
  155.     end
  156.     DigAndMove("forward")
  157.     BlockHandler("forward")
  158.     BlockHandler("up")
  159.     BlockHandler("down")
  160.     turtle.turnRight()
  161.     BlockHandler("forward")
  162.     turtle.turnLeft()
  163.     turtle.turnLeft()
  164.     BlockHandler("forward")
  165.     turtle.turnRight()
  166.     turtle.back()
  167.     return result
  168. end
  169.  
  170. function Dig( here )
  171.     TOTAL_BLOCKS = TOTAL_BLOCKS + 1  --stats
  172.     if here == "up" then return turtle.digUp()
  173.     elseif here == "down" then return turtle.digDown()
  174.     elseif here == "forward" then return turtle.dig() end end
  175.  
  176. function DigAndMove ( here )
  177.     RemoveBlock(here)
  178.     if here == "forward" then return turtle.forward()
  179.     elseif here == "up" then return turtle.up()
  180.     elseif here == "down" then return turtle.down()
  181.     end
  182. end
  183.    
  184. function FuelCheck()                    --checks fuel level
  185.     local GC = turtle.getItemCount
  186.     print("Fuel: " .. turtle.getFuelLevel())
  187.     Stats("fuel")
  188.     if turtle.getFuelLevel() < 100 then
  189.         Select(FUEL_SLOT)
  190.         print("Refueling " .. (GC(FUEL_SLOT) - 1) .. " units")
  191.         return turtle.refuel(GC(FUEL_SLOT) - 1)
  192.     end
  193.     Select(LAST_SLOT)
  194. end
  195.        
  196. function InitFuel()                     --used on first run
  197.     print("Checking fuel")
  198.     LAST_FUEL = turtle.getFuelLevel()
  199.     return FuelCheck() end
  200.    
  201. function IsFull ( )                     --checks to see if inventory is full
  202.     local n, i
  203.     for i = INV_COUNT+1, TOTAL_SLOTS-1 do
  204.         if turtle.getItemCount(i) > 0 then
  205.         else
  206.             return false
  207.         end
  208.     end
  209.     return true
  210. end
  211.    
  212. function IsOre(here)                    --cycles through inventory slots and compares to a block
  213.                        
  214.     local i
  215.     local result = nil
  216.     if Compare(here) then              
  217.         if CURRENT_SLOT > INV_COUNT then result = true
  218.         else result = false
  219.         end
  220.     else
  221.         for i = 1, TOTAL_SLOTS - 1 do
  222.             turtle.select(i)
  223.             CURRENT_SLOT = i
  224.             if Compare(here) then
  225.                 if i > INV_COUNT then result = true
  226.                     break
  227.                 else result = false
  228.                     break
  229.                 end
  230.             end
  231.         end
  232.     end
  233.     print(result)
  234.     if result == nil then result = true end
  235.     return result
  236. end
  237.  
  238. function Left( doThis, here )           --turn left, call a function, turn right   
  239.     local result
  240.     local doIt = doThis
  241.     turtle.turnLeft()
  242.     result = doIt(here)
  243.     turtle.turnRight()
  244.     return result
  245. end
  246.  
  247. function MineOre( here )                --remove surrounding vein  
  248.     local m = MineOre                       --declaring functions locally is supposed to be
  249.     local a = 0                             --much faster in lua
  250.     local RB = RemoveBlock
  251.     local M = Move
  252.     local R = Right
  253.     local BH = BlockHandler
  254.     local RH = RotateH
  255.     local TS = Select
  256.     local TB = turtle.back
  257.     local P = Place
  258.     local D = turtle.down
  259.     local U = turtle.up
  260.    
  261.     RB(here)                        --get ore and move in  
  262.     M(here)
  263.     if here == "forward" then               --send surrounding area to the BlockHandler
  264.         R(BH, "forward")
  265.         BH("up")
  266.         Left(BH, "forward")
  267.         BH("down")
  268.         BH("forward")  
  269.     else
  270.         RH(BH, "forward")
  271.         BH(here)
  272.     end
  273.  
  274.     if here == "forward" then               --covering up our tracks
  275.         TB()
  276.         P(COBBLE_SLOT, here)
  277.     elseif here == "up" then
  278.         D()
  279.         P(COBBLE_SLOT, here)
  280.     elseif here == "down" then
  281.         U()
  282.         P(COBBLE_SLOT, here)
  283.     end
  284. end
  285.  
  286. function Move ( here )
  287.     if here == "up" then return turtle.up()
  288.     elseif here == "forward" then return turtle.forward()
  289.     elseif here == "down" then return turtle.down()
  290.     end
  291. end
  292.  
  293. function Place( thisSlot, here )                    --places block up, forward or down.  fills in with gravel below
  294.     Select(thisSlot)
  295.     if here == "forward" then return turtle.place()
  296.     elseif here == "down" then
  297.         local t = turtle.getItemCount(GRAVEL_SLOT)     
  298.         turtle.placeDown() 
  299.         Select(GRAVEL_SLOT)                                            
  300.         if not turtle.detectDown() then
  301.             while i < t do
  302.                 Place("down")                      
  303.                 if t == 1 then return true
  304.                 else return false
  305.                 end
  306.                 t = t + 1
  307.             end
  308.         else turtle.placeDown()
  309.         end
  310.     elseif here == "up" then return turtle.placeUp()
  311.     end
  312.     Select(LAST_SLOT)
  313.     return false
  314. end
  315.  
  316. function RemoveBlock( here )            --removes block and any gravel/sand (sand/gravel from above falls down when mined)
  317.     local failsafe = 0 
  318.     if here == "down" then Dig("down")
  319.     else
  320.         if Dig(here) == true then Select(GRAVEL_SLOT)
  321.             if Compare(here) == true then failsafe = 0
  322.                 while Dig(here) and (failsafe < 16) do  --infinite loop protection
  323.                     sleep(.5)
  324.                     failsafe = failsafe + 1
  325.                 end
  326.             end
  327.             Select(LAST_SLOT)
  328.         end
  329.     end
  330. end
  331.  
  332. function RotateH( doThis, here )        --rotates and calls a function
  333.     local result
  334.     local doIt = doThis
  335.     turtle.turnRight()
  336.     doIt(here)
  337.     turtle.turnRight()
  338.     doIt(here)
  339.     turtle.turnRight()
  340.     doIt(here)
  341.     turtle.turnRight() 
  342.     doIt(here)
  343. end
  344.    
  345.  
  346. function Stats ( this, that )           --prints various stats
  347.     FUEL_USED = FUEL_USED + LAST_FUEL - turtle.getFuelLevel()
  348.     LAST_FUEL = turtle.getFuelLevel()
  349.     if this == "distance" then
  350.         DISTANCE = DISTANCE + 1
  351.         if DISTANCE % 5 == 0 then  
  352.             print(DISTANCE .. "m tunneled.")
  353.             print(((TOTAL_BLOCKS + ORE) / DISTANCE) .. " blocks mined per tunnel section.")
  354.             print((FUEL_USED/(TOTAL_BLOCKS + ORE)).. " fuel per block.")return true
  355.         end
  356.     elseif this == "torch" then TORCHES_USED = TORCHES_USED + 1
  357.         return true
  358.     elseif this == "fuel" then FUEL_USED = FUEL_USED + (LAST_FUEL - turtle.getFuelLevel())
  359.         print(FUEL_USED .. " total fuel used.")
  360.         print((FUEL_USED / DISTANCE) .. " fuel per m.")
  361.         print((FUEL_USED / ORE) .. " fuel per extra ore.")
  362.         print((turtle.getFuelLevel() /(FUEL_USED/DISTANCE)) .. "m's of fuel left.")
  363.         return true
  364.     elseif this == "ore" then ORE = ORE + 1
  365.         print(ORE .. " extra ore mined.")
  366.         print((FUEL_USED / ORE) .. " per ore.") return true
  367.     elseif this == "fuelchunks" then end return false end  
  368.  
  369. function Select ( slot )
  370.     LAST_SLOT = CURRENT_SLOT
  371.     turtle.select(slot)
  372.     CURRENT_SLOT = slot
  373. end
  374.    
  375. function TorchCheck()                   --checks to see if torch needs to be placed
  376.     local BH = BlockHandler
  377.     if (DISTANCE % TORCH_SPACE) == 0 and turtle.getItemCount(TORCH_SLOT) > 0 then
  378.         Stats("torch") 
  379.         RemoveBlock("forward")
  380.         turtle.forward()
  381.         if turtle.detect() then BH("forward")
  382.         else Place(COBBLE_SLOT, "forward")
  383.         end
  384.         if turtle.detectDown() then BH("down")
  385.         else Place(COBBLE_SLOT, "down")
  386.         end
  387.         turtle.back()
  388.         Place(TORCH_SLOT, "forward")
  389.     end
  390. end
  391.  
  392. function TunnelForward( thisFar )       --mine a 1x2 tunnel forward a given number of units
  393.     local i
  394.     for i = 1, thisFar do                    
  395.         if IsFull() then CheckInventory() end
  396.         FuelCheck()
  397.                                             --Coords         Facing
  398.         --turtle.now                        -- 0,0,0            N                              
  399.         DigAndMove("forward")               -- 0,1,0            N
  400.             BlockHandler("down")
  401.             if not Detect("down") then Place(COBBLE_SLOT, "down") end
  402.         turtle.turnLeft()                   -- 0,1,0            W
  403.             BlockHandler("forward")
  404.             if not Detect("forward") then Place(COBBLE_SLOT, "forward") end
  405.         DigAndMove("up")                    -- 0,1,1            W
  406.             BlockHandler("up")
  407.             if not Detect("up") then Place(COBBLE_SLOT, "up") end
  408.             BlockHandler("forward")
  409.             if not Detect("forward") then Place(COBBLE_SLOT, "forward") end
  410.         turtle.turnRight()                  -- 0,1,1            N
  411.         turtle.turnRight()                  -- 0,1,1            E
  412.             BlockHandler("forward")
  413.             if not Detect("forward") then Place(COBBLE_SLOT, "forward") end
  414.         turtle.down()                       -- 0,1,0            E
  415.             BlockHandler("forward")
  416.             if not Detect("forward") then Place(COBBLE_SLOT, "forward") end
  417.             TorchCheck()
  418.         turtle.turnLeft()                   -- 0,1,0            N
  419.         Stats("distance")                   --add one to distance travelled and display stats
  420.     end
  421. end
  422.  
  423. function main()
  424.     InitFuel()
  425.     TunnelForward(SHAFT_LENGTH+2)
  426.     turtle.turnRight()
  427.     TunnelForward((TOTAL_SHAFTS-1)*3)
  428.     turtle.turnRight()
  429.     TunnelForward(SHAFT_LENGTH+1)
  430.     turtle.turnRight()
  431.     TunnelForward((TOTAL_SHAFTS-2)*3)
  432.     turtle.turnRight()
  433.    
  434.     for i = 1, SHAFTS / 2 do
  435.         TunnelForward(SHAFT_LENGTH+1)
  436.         turtle.turnRight()
  437.         TunnelForward(SHAFT_WIDTH+1)
  438.         turtle.turnRight()
  439.         TunnelForward(SHAFT_LENGTH+1)
  440.         turtle.turnLeft()
  441.         TunnelForward(SHAFT_WIDTH+1)
  442.         turtle.turnLeft()
  443.     end
  444.    
  445. end
  446.  
  447. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement