hilburn

Non-Recursive Vein Miner

Jul 25th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. local sides={[0]="north",[1]="east",[2]="south",[3]="west",[4]="up",[5]="down"}
  2. local move_vect={[0]=vector.new(0,0,-1),[1]=vector.new(1,0,0),[2]=vector.new(0,0,1),[3]=vector.new(-1,0,0),[4]=vector.new(0,1,0),[5]=vector.new(0,-1,0)}
  3. local facing=0
  4. local mining={vector.new(0,0,0)}
  5. local junkslots={1,2,3,4}
  6.  
  7. local function left()
  8.     if turtle.turnLeft() then
  9.         facing=(facing-1)%4
  10.         return true
  11.     else
  12.         return false
  13.     end
  14. end
  15.  
  16. local function right()
  17.     if turtle.turnRight() then
  18.         facing=(facing+1)%4
  19.         return true
  20.     else
  21.         return false
  22.     end
  23. end
  24.  
  25. local function turnTo(side)
  26.     local diff=(side-facing)%4
  27.     local turn=right
  28.     if diff>2 then
  29.         turn=left
  30.         diff=4-diff
  31.     end
  32.     for i=1,diff do
  33.         turn()
  34.     end
  35. end
  36.  
  37. local function compareBlock(side)
  38.     local compare=turtle.compare
  39.     local detect=turtle.detect
  40.     if side<4 then
  41.         turnTo(side)
  42.     elseif side==4 then
  43.         compare=turtle.compareUp
  44.         detect=turtle.detectUp
  45.     else
  46.         compare=turtle.compareDown
  47.         detect=turtle.detectDown
  48.     end
  49.     if detect then
  50.         for i,j in pairs(junkslots) do
  51.             turn.select(j)
  52.             if compare() then return false end
  53.         end
  54.         return true
  55.     else
  56.         return false
  57.     end
  58. end
  59.    
  60. local function scanSides()
  61.     for i,j in pairs(sides) do
  62.         if compareBlock(i) then return i end
  63.     end
  64.     return "no ore"
  65. end
  66.  
  67. local function whichWay(here,there)
  68.     local move=there:sub(here)
  69.     for i,j in ipairs(move_vect)
  70.         if move==j then return i end
  71.     end
  72. end
  73.  
  74. local function go(direction)
  75.     if go<4 then
  76.         turnTo(go)
  77.         turtle.dig()
  78.         return turtle.forward()
  79.     elseif go==4 then
  80.         turtle.digUp()
  81.         return turtle.up()
  82.     else
  83.         turtle.digDown()
  84.         return turtle.down()
  85.     end
  86. end
  87.  
  88. local function notRecursive
  89. repeat
  90.     local mine=scanSides()
  91.     if move_vect[mine] == nil then --#if no ore found
  92.         if #mining>1 then --#if not at start
  93.             if go(whichWay(mining[#mining],mining[#mining-1]) then --#try to go back 1
  94.                 table.remove(mining) --#remove where we came from if successful
  95.             else
  96.                 --#something has gone wrong
  97.             end
  98.         end
  99.     else    --#if ore found
  100.         if go(mine) then    --#dig it and move there
  101.             table.insert(mining,mining[#mining]:add(move_vect[mine])) --#update position
  102.         else
  103.             --#something has gone wrong, possibly gravel, haven't put and handling in for that
  104.         end
  105.     end
  106. until #mining==1
  107. end
Advertisement
Add Comment
Please, Sign In to add comment