florensie

Computercraft Intelligent Mining Turtle

Dec 22nd, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Created by makeo. All rights reserved.
  2.  
  3. --DO NOT EDIT
  4. local ENDER_CHEST_SLOT = 1
  5. local DIG_SLOT = 2
  6.  
  7. --save movements
  8. function move(move, detect, dig, attack)
  9.     if needsPitstop() then
  10.         doPitstop()
  11.     end
  12.  
  13.     local first = true
  14.     while not move() do
  15.         if detect() then
  16.             turtle.select(DIG_SLOT)
  17.             dig()
  18.         else
  19.             attack()
  20.         end
  21.        
  22.         if not first then
  23.             sleep(0.5)
  24.         end
  25.         first = false
  26.     end
  27. end
  28.  
  29. function moveForward()
  30.     move(turtle.forward, turtle.detect, turtle.dig, turtle.attack)
  31. end
  32.  
  33. function moveBack()
  34.     if not turtle.back() then
  35.         turn180()
  36.         moveForward()
  37.         turn180()
  38.     end
  39. end
  40.  
  41. function moveUp()
  42.     move(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp)
  43. end
  44.  
  45. function moveDown()
  46.     move(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown)
  47. end
  48.  
  49. function turn180()
  50.     turtle.turnLeft()
  51.     turtle.turnLeft()
  52. end
  53.  
  54. --pitstop
  55. function doPitstop()
  56.     if turtle.getItemDetail(ENDER_CHEST_SLOT) == nil then
  57.         if peripheral.getType("bottom") ~= "ender_chest" then
  58.             error("No ender chest found.")
  59.         end
  60.     else
  61.         if turtle.detectDown() then
  62.             turtle.select(DIG_SLOT)
  63.             turtle.digDown()
  64.         end
  65.        
  66.         turtle.select(ENDER_CHEST_SLOT)
  67.         turtle.placeDown()
  68.         sleep(0.5)
  69.     end
  70.  
  71.     local enderChest = peripheral.wrap("bottom")
  72.    
  73.     if enderChest == nil then
  74.         error("Failed to wrap ender chest.")
  75.     end
  76.    
  77.    
  78.     if (not hasSpace()) or getMovingHome() then
  79.         for i = 3, 16, 1 do
  80.             while turtle.getItemCount(i) > 0 do
  81.                 local moved = false
  82.                 for j = 2, enderChest.getInventorySize(), 1 do
  83.                     if enderChest.pullItem("up", i, 64, j) ~= 0 then
  84.                         moved = true
  85.                     end
  86.                 end
  87.                 sleep(0.5)
  88.                
  89.                 if not moved then
  90.                     sleep(2)
  91.                 end
  92.             end
  93.         end
  94.     end
  95.    
  96.     turtle.select(ENDER_CHEST_SLOT)
  97.     turtle.digDown()
  98. end
  99.  
  100. function needsPitstop()
  101.     return (not hasSpace())
  102. end
  103.  
  104. function hasSpace()
  105.     local count = 0
  106.     for i = 3, 16, 1 do
  107.         if turtle.getItemCount(i) == 0 then
  108.             count = count + 1
  109.         end
  110.     end
  111.     return count > 1
  112. end
  113.  
  114. function readNumber(minVal)
  115.     local num = -1
  116.     repeat
  117.         if num ~= -1 then
  118.             print("Text is not a number.")
  119.         end
  120.         num = tonumber(read())
  121.        
  122.         if num ~= nil and num < minVal then
  123.             print("Number is out of bound.")
  124.             num = nil
  125.         end
  126.        
  127.     until num ~= nil
  128.     return num
  129. end
  130.  
  131. --ore stuff
  132. function isOre()
  133.     local success, data = turtle.inspect()
  134.     return checkOre(success, data)
  135. end
  136.  
  137. function isOreUp()
  138.     local success, data = turtle.inspectUp()
  139.     return checkOre(success, data)
  140. end
  141.  
  142. function isOreDown()
  143.     local success, data = turtle.inspectDown()
  144.     return checkOre(success, data)
  145. end
  146.  
  147. function checkOre(success, data)
  148.     if success then
  149.         local name = data["name"]
  150.         if name ~= nil then
  151.             --Alternative name bypass
  152.             if name == "Forestry:resources" or name == "TConstruct:SearedBrick"
  153.                     or name == "denseores:block0" or name == "rftools:dimensionalShardBlock"
  154.                     or name == "harvestcraft:salt" then
  155.                 return true
  156.             end
  157.        
  158.             local index = string.find(name, ":")
  159.             if index ~= nil then
  160.                 local part = string.lower(string.sub(name, index))
  161.                 local isOre = string.find(part, "ore")
  162.                 return isOre ~= nil
  163.             end
  164.         end
  165.     end
  166.     return false
  167. end
  168.  
  169. --vein stuff
  170. function mineVein()
  171.     while true do
  172.         if isOre() then
  173.             veinMoveForward()
  174.         elseif isOreUp() then
  175.             veinMoveUp()
  176.         elseif isOreDown() then
  177.             veinMoveDown()
  178.         else
  179.             local success = false
  180.             for i = 1, 3, 1 do
  181.                 veinMoveLeft()
  182.                 if isOre() then
  183.                     veinMoveForward()
  184.                     success = true
  185.                     break
  186.                 end
  187.             end
  188.            
  189.             if not success then
  190.                 if not popVeinMovment() then
  191.                     return
  192.                 end
  193.             end
  194.         end
  195.         sleep(0.5)
  196.     end
  197. end
  198.  
  199. function veinMoveForward()
  200.     moveForward()
  201.     pushToVeinStack(1)
  202. end
  203.  
  204. function veinMoveUp()
  205.     moveUp()
  206.     pushToVeinStack(2)
  207. end
  208.  
  209. function veinMoveDown()
  210.     moveDown()
  211.     pushToVeinStack(3)
  212. end
  213.  
  214. function veinMoveLeft()
  215.     turtle.turnLeft()
  216.     pushToVeinStack(4)
  217. end
  218.  
  219. function veinMoveRight()
  220.     turtle.turnRight()
  221.     pushToVeinStack(5)
  222. end
  223.  
  224. function veinMoveBack()
  225.     moveBack()
  226.     pushToVeinStack(6)
  227. end
  228.  
  229. function popVeinMovment()
  230.     local direction = getFromVeinStack()
  231.    
  232.     if direction == nil then
  233.         return false
  234.     end
  235.    
  236.     if direction == 1 then
  237.         moveBack()
  238.     elseif direction == 2 then
  239.         moveDown()
  240.     elseif direction == 3 then
  241.         moveUp()
  242.     elseif direction == 4 then
  243.         turtle.turnRight()
  244.         removeLastVeinStack()
  245.         return popVeinMovment()
  246.     elseif direction == 5 then
  247.         turtle.turnLeft()
  248.         removeLastVeinStack()
  249.         return popVeinMovment()
  250.     elseif direction == 6 then
  251.         moveForward()
  252.     end
  253.    
  254.     removeLastVeinStack()
  255.    
  256.     return true;
  257. end
  258.  
  259. function pushToVeinStack(direction)
  260.     local stack = getVeinStack()
  261.    
  262.     if stack == nil then
  263.         stack = {}
  264.     end
  265.    
  266.     stack[#stack + 1] = direction
  267.    
  268.     stack = optimizeVeinStack(stack)
  269.     saveVeinStack(stack, #stack)
  270. end
  271.  
  272. function getFromVeinStack()
  273.     local data = getVeinStack()
  274.    
  275.     if data ~= nil then
  276.         return data[#data]
  277.     end
  278.     return nil
  279. end
  280.  
  281. function optimizeVeinStack(data)
  282.     local lastAction = 0
  283.     local actionCount = 0
  284.    
  285.     local i = 1
  286.     while i <= #data do
  287.         --turn right and then left is somewhat useless
  288.         if (data[i] == 4 and lastAction == 5) or
  289.             (data[i] == 5 and lastAction == 4) then
  290.             data = moveArrayContent(data, i + 1, 2)
  291.             if #data < 1 then
  292.                 break
  293.             end
  294.            
  295.             i = 1
  296.             lastAction = 0
  297.             actionCount = 0
  298.         end
  299.    
  300.         if data[i] ~= lastAction then
  301.             lastAction = 0
  302.             actionCount = 0
  303.         end
  304.    
  305.         if data[i] == 4 then
  306.             lastAction = 4
  307.             actionCount = actionCount + 1
  308.         elseif data[i] == 5 then
  309.             lastAction = 5
  310.             actionCount = actionCount + 1
  311.         end
  312.        
  313.         if actionCount == 3 then
  314.             local newAction = 4
  315.             if lastAction == 4 then
  316.                 newAction = 5
  317.             end
  318.             data = moveArrayContent(data, i + 1, 2)
  319.             data[i - 2] = newAction
  320.            
  321.             i = 0
  322.             lastAction = 0
  323.             actionCount = 0
  324.         end
  325.         i = i + 1
  326.     end
  327.     return data
  328. end
  329.  
  330. function moveArrayContent(array, startIndex, amount)
  331.     local size = #array
  332.     for i = startIndex, size + amount, 1 do
  333.         array[i - amount] = array[i]
  334.         array[i] = nil
  335.     end
  336.     return array
  337. end
  338.  
  339. function removeLastVeinStack()
  340.     local data = getVeinStack()
  341.     saveVeinStack(data, #data - 1)
  342. end
  343.  
  344. function saveVeinStack(data, length)
  345.     if data ~= nil then
  346.         local dataLeng = #data
  347.         for i = length + 1, dataLeng, 1 do
  348.             data[i] = nil
  349.         end
  350.     end
  351.    
  352.     if #data < 1 then
  353.         data = nil
  354.     end
  355.     setVeinStack(data)
  356. end
  357.  
  358. function getVeinStack()
  359.     return getVariable("veinStack")
  360. end
  361.  
  362. function setVeinStack(value)
  363.     setVariable("veinStack", value)
  364. end
  365.  
  366. function getVariable(name)
  367.     local vars = getVariables()
  368.     if vars ~= nil then
  369.         return vars[name]
  370.     end
  371.     return nil
  372. end
  373.  
  374. function setVariable(name, value)
  375.     local vars = getVariables()
  376.    
  377.     if vars == nil then
  378.         vars = {}
  379.     end
  380.    
  381.     vars[name] = value
  382.    
  383.     local handle = fs.open("Vars.dat", "w")
  384.     handle.writeLine(textutils.serialize(vars))
  385.     handle.close()
  386. end
  387.  
  388. function getVariables()
  389.     local handle = fs.open("Vars.dat", "r")
  390.    
  391.     if handle ~= nil then  
  392.         local raw = handle.readAll()
  393.         handle.close()
  394.        
  395.         if raw ~= nil then
  396.             return textutils.unserialize(raw)
  397.         end
  398.     end
  399.     return nil
  400. end
  401.  
  402. function getStripOnGround()
  403.     return getVariable("stripGround")
  404. end
  405.  
  406. function setStripOnGround(value)
  407.     return setVariable("stripGround", value)
  408. end
  409.  
  410. function getStripHasDug()
  411.         return getVariable("stripHasDug")
  412. end
  413.  
  414. function setStripHasDug(value)
  415.         return setVariable("stripHasDug", value)
  416. end
  417.  
  418. function digStrip(gotoStart)
  419.  
  420.     if true then
  421.         while not getStripHasDug() do
  422.             mineVein()
  423.            
  424.             if getStripHasDug() then
  425.                
  426.                 moveForward()
  427.                 setStripHasDug(false)
  428.             else
  429.                 if getStripOnGround() then
  430.                     moveUp()
  431.                     setStripOnGround(false)
  432.                 else
  433.                     moveDown()
  434.                     setStripOnGround(true)
  435.                 end
  436.                 setStripHasDug(true)
  437.             end
  438.         end
  439.     end
  440.    
  441.     mineVein()
  442.    
  443.     if not getStripOnGround() then
  444.         moveDown()
  445.         setStripOnGround(true)
  446.         mineVein()
  447.     end
  448. end
  449.  
  450. while true do
  451.     digStrip(true)
  452. end
Add Comment
Please, Sign In to add comment