rdalkire

Vein Miner 1.2.1

Dec 23rd, 2015
1,986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.71 KB | None | 0 0
  1. --[[ Mines a contiguous aggregation of
  2. resource blocks. Meant for trees or
  3. veins of ore.
  4. 1  Place the turtle so it faces the
  5.    material you want.
  6. 2  Refuel the turtle if applicable.
  7. 3  Run this script.  
  8. 3a Note, if you want it to dig *all*
  9.    blocks around any matching one, to
  10.    give the spaces a neater
  11.    appearance, use 'a' as an argument.
  12.    For example, if the script is
  13.    called vMiner:
  14.    vMiner a
  15. 3b Even neater but using more time
  16.    and more fuel: To dig out the whole
  17.    rectangular prism surrounding the
  18.    vein, use 'r' argument:
  19.    vMiner r
  20.  
  21. Copyright (c) 2015 - 2016
  22. Robert David Alkire II, IGN ian_xw
  23. Distributed under the MIT License.
  24. (See accompanying file LICENSE or copy
  25. at http://opensource.org/licenses/MIT)
  26. ]]
  27.  
  28. --- assigns fake/real turtle
  29. --  toggle turtle before deployment
  30. --  TODO remember deploy-time toggle
  31. t = turtle
  32. -- t = require "mockTurtle"
  33.  
  34. Locus = {}
  35. Locus.__index = Locus
  36. Locus.x = 0
  37. Locus.y = 0
  38. Locus.z = 0
  39. --- A constructor which sets the
  40. -- current location
  41. Locus.new= function( x, y, z )
  42.   local self = setmetatable({}, Locus)
  43.   self.x = x
  44.   self.y = y
  45.   self.z = z
  46.   return self
  47. end
  48.  
  49. local deadReckoner = {}
  50. local dr = deadReckoner
  51.  
  52. --- relative to turtle heading at start
  53. deadReckoner.FORE = 0
  54. deadReckoner.STARBOARD = 1
  55. deadReckoner.AFT = 2
  56. deadReckoner.PORT = 3
  57. deadReckoner.WAYS = {}
  58. dr.WAYS[deadReckoner.FORE] = "FORE"
  59. dr.WAYS[deadReckoner.STARBOARD]=
  60.     "STARBOARD"
  61. dr.WAYS[deadReckoner.AFT] = "AFT"
  62. dr.WAYS[deadReckoner.PORT] = "PORT"
  63.  
  64. deadReckoner.heading=deadReckoner.FORE
  65.  
  66. --- Current position relative to start
  67. deadReckoner.place= Locus.new(0, 0, 0)
  68.  
  69. --- Maximum x, y, z relative to start,
  70. -- located or dug
  71. deadReckoner.placeMAX=Locus.new(0,0,0)
  72.  
  73. --- Minimum x, y, z relative to start,
  74. -- located or dug
  75. deadReckoner.placeMIN=Locus.new(0,0,0)
  76.  
  77. --- forward regardless of heading
  78. deadReckoner.AHEAD = 4
  79. deadReckoner.UP = 5
  80. deadReckoner.DOWN = 6
  81.  
  82. --- Turns as needed to face the
  83. -- target direction indicated
  84. -- @param target must be dr.FORE,
  85. -- dr.STARBOARD, dr.AFT, or dr.PORT
  86. deadReckoner.bearTo= function(target)
  87.  
  88.   local trnsRght =
  89.       target - deadReckoner.heading
  90.  
  91.   local trns = math.abs( trnsRght )
  92.   if trns ~= 0 then
  93.    
  94.     if trns== 3 then
  95.       trns= 1
  96.       trnsRght= trnsRght/-3
  97.     end
  98.    
  99.     local i = 0
  100.     while i < trns do
  101.       if trnsRght >= 0 then
  102.         t.turnRight()
  103.       else
  104.         t.turnLeft()
  105.       end -- which way
  106.       i = i + 1
  107.     end -- turn loop
  108.   end -- there were any turns
  109.  
  110.   deadReckoner.heading = target
  111. end
  112.  
  113. --- If way is fore, starboard, aft or
  114. -- port, then bear to that direction
  115. -- @param way can any of the heading
  116. -- constants: FORE, STARBOARD, AFT,
  117. -- PORT, UP, DOWN or even AHEAD
  118. -- @return way is AHEAD if the param
  119. -- had been a horizontal direction
  120. -- (FORE, AFT, PORT, STARBOARD).
  121. -- Otherwise it's the same as the input
  122. -- param.
  123. deadReckoner.correctHeading=
  124.     function(way)
  125.    
  126.   if way < 4 then
  127.     dr.bearTo( way )
  128.     way = dr.AHEAD
  129.   end
  130.  
  131.   return way
  132.  
  133. end
  134.  
  135. --- Adjusts placeMAX and placeMIN as
  136. -- applicable.
  137. deadReckoner.setMaxMin=function(x,y,z)
  138.  
  139.   if x > dr.placeMAX.x then
  140.     dr.placeMAX.x = x
  141.   elseif x < dr.placeMIN.x then
  142.     dr.placeMIN.x = x
  143.   end
  144.  
  145.   if y > dr.placeMAX.y then
  146.     dr.placeMAX.y = y
  147.   elseif y < dr.placeMIN.y then
  148.     dr.placeMIN.y = y
  149.   end
  150.  
  151.   if z > dr.placeMAX.z then
  152.     dr.placeMAX.z = z
  153.   elseif z < dr.placeMIN.z then
  154.     dr.placeMIN.z = z
  155.   end
  156.  
  157. end
  158.  
  159. --- Gets the coordinates of the block
  160. -- currently next to the turtle,
  161. -- depending on which way one would
  162. -- look.
  163. -- @param way must be deadReckoner's
  164. -- (dr's) AFT, FORE, PORT, STARBOARD,
  165. -- UP, DOWN or AHEAD.
  166. -- @return x, y, z coordinates of the
  167. -- adjacent block.
  168. deadReckoner.getTargetCoords=
  169.     function(way)
  170.  
  171.   local ix = dr.place.x
  172.   local iy = dr.place.y
  173.   local iz = dr.place.z
  174.  
  175.   if way == dr.AHEAD then
  176.     way = dr.heading
  177.   end
  178.  
  179.   if way== dr.AFT then
  180.     iz= iz - 1
  181.   elseif way== dr.FORE then
  182.     iz= iz + 1
  183.   elseif way== dr.PORT then
  184.     ix= ix- 1
  185.   elseif way== dr.STARBOARD then
  186.     ix= ix+ 1
  187.   elseif way== dr.UP then
  188.     iy= iy + 1
  189.   elseif way== dr.DOWN then
  190.     iy= iy - 1
  191.   end
  192.  
  193.   return ix, iy, iz
  194.  
  195. end
  196.  
  197.  
  198. --- Finds the distance between the
  199. -- current location and some other
  200. -- place, without diagonal travel
  201. -- @param x, y, z are the coords of
  202. -- the other place
  203. -- @return the distance
  204. deadReckoner.howFarFrom=function(x,y,z)
  205.   local dx= math.abs( dr.place.x- x )
  206.   local dy= math.abs( dr.place.y- y )
  207.   local dz= math.abs( dr.place.z- z )
  208.   return dx + dy + dz
  209. end
  210.  
  211. --- Calculates distance from starting
  212. -- place, considering that turtles
  213. -- do not move diagonally in their
  214. -- present form.
  215. -- @return number of moves to get back
  216. deadReckoner.howFarFromHome=function()
  217.   return math.abs(dr.place.x)+
  218.       math.abs(dr.place.y)+
  219.       math.abs(dr.place.z)
  220. end
  221.  
  222. --- Inspects the given direction, and
  223. -- also calls to evaluate the target
  224. -- block for max and min coords.
  225. -- @param way FORE, UP, AHEAD etc
  226. -- @return boolean success, table
  227. -- data/string error message
  228. deadReckoner.inspect= function(way)
  229.  
  230.   way = dr.correctHeading(way)
  231.   local ok, item
  232.   if way== dr.AHEAD then
  233.     ok, item= t.inspect()
  234.   elseif way== dr.UP then
  235.     ok, item= t.inspectUp()
  236.   else
  237.     ok, item= t.inspectDown()
  238.   end
  239.  
  240.   local ix, iy, iz =
  241.       dr.getTargetCoords(way)
  242.  
  243.   dr.setMaxMin(ix, iy, iz)
  244.   return ok, item
  245. end
  246.  
  247. --- Digs.
  248. -- @param way must be dr.FORE,
  249. -- dr.STARBOARD, dr.FORE, dr.AFT
  250. -- dr.AHEAD, dr.UP or dr.DOWN
  251. -- @return isAble true if it really
  252. -- was able to dig
  253. -- @return whyNot if isAble, nil. Else,
  254. -- reason why not.
  255. deadReckoner.dig= function( way )
  256.  
  257.   way = dr.correctHeading( way )
  258.  
  259.   local dug= false
  260.   local whyNot
  261.   if way== dr.AHEAD then
  262.     dug, whyNot= t.dig()
  263.   elseif way== dr.UP then
  264.     dug, whyNot= t.digUp()
  265.   elseif way== dr.DOWN then
  266.     dug, whyNot= t.digDown()
  267.   end
  268.  
  269.   return dug, whyNot
  270. end
  271.  
  272. --- Tries to move ahead, up or down.
  273. -- If successful,
  274. -- it updates its current location
  275. -- relative to where it started and
  276. -- returns true.
  277. -- Else, it returns false and the
  278. -- reason why not.
  279. -- @param way is either dr.AHEAD, dr.UP
  280. -- or dr.DOWN, where dr is deadReckoner
  281. deadReckoner.move= function( way )
  282.  
  283.   -- where way is dr.AHEAD, UP or DOWN
  284.   local isAble, whynot
  285.   if way== dr.AHEAD then
  286.     isAble, whynot = t.forward()
  287.    
  288.     if isAble then
  289.       if dr.heading== dr.AFT then
  290.         dr.place.z= dr.place.z - 1
  291.       elseif dr.heading== dr.FORE then
  292.         dr.place.z= dr.place.z + 1
  293.       elseif dr.heading== dr.PORT then
  294.         dr.place.x= dr.place.x- 1
  295.       else
  296.         dr.place.x= dr.place.x+ 1
  297.       end
  298.      
  299.     end -- isAble
  300.   elseif way== dr.UP then
  301.     isAble, whynot = t.up()
  302.     if isAble then
  303.       dr.place.y = dr.place.y + 1
  304.     end
  305.   elseif way== dr.DOWN then
  306.     isAble, whynot = t.down()
  307.     if isAble then
  308.       dr.place.y = dr.place.y - 1
  309.     end
  310.   end -- AHEAD, UP or DOWN
  311.  
  312.   if isAble then
  313.     dr.setMaxMin( dr.place.x,
  314.         dr.place.y, dr.place.z )
  315.   end
  316.  
  317.   return isAble, whynot
  318. end
  319.  
  320. --- Comparing destination with current
  321. -- location, this finds the dominant
  322. -- direction and distance in that
  323. -- direction. X - Z plane gets
  324. -- priority.
  325. -- @param dest destination coordinates
  326. -- @return direction: up, down, fore,
  327. -- aft, port or starboard
  328. -- @return distance
  329. deadReckoner.furthestWay =
  330.     function(dest)
  331.  
  332.   -- Dest - Current: +Srbrd -Port
  333.   local direction = 0
  334.   local dist = dest.x - dr.place.x
  335.   if dist >= 0 then
  336.     direction= dr.STARBOARD
  337.   else
  338.     direction= dr.PORT
  339.   end
  340.  
  341.   -- Find Z diff +fore -aft
  342.   local zDist = dest.z - dr.place.z
  343.   if math.abs(zDist)>math.abs(dist)then
  344.     dist= zDist
  345.     if dist >= 0 then
  346.       direction= dr.FORE
  347.     else
  348.       direction= dr.AFT
  349.     end
  350.   end
  351.  
  352.   -- Y:  +up -down
  353.   local yDist = dest.y - dr.place.y
  354.   if math.abs(yDist)>math.abs(dist)then
  355.     dist= yDist
  356.     if dist >= 0 then
  357.       direction= dr.UP
  358.     else
  359.       direction= dr.DOWN
  360.     end
  361.   end
  362.  
  363.   return direction, math.abs(dist)
  364.  
  365. end
  366.  
  367. local veinMiner = {}
  368. local vm = veinMiner
  369.  
  370. --- For estimating fuel need
  371. veinMiner.MOVESPERCUBE = 15
  372.  
  373. --- If part of larger task, from caller
  374. veinMiner.previousDistance = 0
  375.  
  376. --- The kind of block it's looking for
  377. veinMiner.targetBlockName = ""
  378.  
  379. --- Defines cube's surrounding loci, in
  380.   -- the order in which they are to
  381.   -- be inspected.
  382. veinMiner.cubeInspectionSequence = {
  383.   {0, 1, 0}, {0, -1, 0}, {0, 0, 1},
  384.   {0, 1, 1}, {0, -1, 1}, {1, 0, 1},
  385.   {1, 1, 1}, {1, -1, 1}, {1, 0, 0},
  386.   -- 10
  387.   {1, 1, 0}, {1, -1, 0}, {1, 0,-1},
  388.   {1, 1,-1}, {1, -1,-1}, {0, 0,-1},
  389.   {0, 1,-1}, {0, -1,-1}, {-1,0,-1},
  390.   -- 19
  391.   {-1,1,-1}, {-1,-1,-1}, {-1,0, 0},
  392.   {-1, 1,0}, {-1,-1, 0}, {-1,0, 1},
  393.   {-1, 1,1}, {-1,-1, 1}
  394. }
  395.  
  396. --- Collection of "cubes", which are
  397. -- spaces to explore, defined by
  398. -- central locations.
  399. veinMiner.cubeStack = {}
  400.  
  401. --- Array of locations which have been
  402. -- inspected.
  403. veinMiner.inspected = {}
  404.  
  405. --- Count of blocks that have been
  406. -- marked as inspected
  407. veinMiner.inspectedCount= 0
  408.  
  409. --- Count of inspections skipped due
  410. -- to logic with the Check function
  411. -- (when the turtle was right next to
  412. -- it)
  413. veinMiner.inspectedSkipped= 0
  414.  
  415. --- Count of times that coordinates
  416. -- to goLookAt were actually for the
  417. -- robot's own location
  418. veinMiner.inspectSelfAvoidance = 0
  419.  
  420. --- If true, then the miner is supposed
  421. -- to *dig* all the blocks from around
  422. -- the target block, even the ones that
  423. -- don't match the target blocks.
  424. veinMiner.isAll = false
  425.  
  426. --- Count of goLookAt calls skipped due
  427. -- to logic within inspectACube,
  428. -- therefore avoiding extra movements
  429. veinMiner.goLookSkipped= 0
  430.  
  431. --- Records a location as having been
  432. -- inspected
  433. veinMiner.setInspected=function(x,y,z)
  434.   vm.inspected[
  435.       string.format("%d,%d,%d",x,y,z)]=
  436.       true
  437.   vm.inspectedCount=vm.inspectedCount+1
  438. end
  439.  
  440. veinMiner.isInspected=function(x,y,z)
  441.   local indx= string.format(
  442.       "%d,%d,%d", x, y, z )
  443.   local val= vm.inspected[indx]
  444.   local isInspctd = not ( val == nil )
  445.   return isInspctd
  446. end
  447.  
  448. --- Determines whether the mining/
  449. -- felling task is complete.
  450. veinMiner.isVeinExplored= function()
  451.   local isExplored = false
  452.   local cubeCount=
  453.       table.maxn(vm.cubeStack)
  454.   if cubeCount== 0 then
  455.     isExplored = true
  456.     print("Vein is explored")
  457. --  else
  458. --    print( "unexplored cubeCount: "..
  459. --        cubeCount )
  460.   end
  461.  
  462.   return isExplored
  463. end
  464.  
  465. --- If there's enough fuel to explore
  466. -- one last cube and move back to the
  467. -- original place.
  468. veinMiner.isFuelOK4Cube = function()
  469.   local isOK = false
  470.   local fuel = t.getFuelLevel()
  471.   if fuel == "unlimited" then
  472.     isOK = true
  473.   else
  474.     local fuelNeed =
  475.         veinMiner.previousDistance +
  476.         dr.howFarFromHome() +
  477.         vm.MOVESPERCUBE
  478.     if fuel > fuelNeed then
  479.       isOK = true
  480.     else
  481.       print("Fuel too low: ".. fuel )
  482.     end
  483.   end
  484.   return isOK
  485. end
  486.  
  487. --- See's if there's enough fuel to get
  488. -- to the destination then get home
  489. -- @param x, y, z coords for dstnation
  490. -- @return true if fuel OK or unlimited
  491. veinMiner.isFuelOK4Dest=
  492.     function(x, y, z)
  493.   local isOK = false
  494.   local fuel = t.getFuelLevel()
  495.   if fuel == "unlimited" then
  496.     isOK = true
  497.   else
  498.     local destToHome = math.abs(x)+
  499.         math.abs(y)+ math.abs(z)
  500.     local fuelNeed =
  501.         dr.howFarFrom(x,y,z)+
  502.         destToHome+ vm.previousDistance
  503.     if fuel >= fuelNeed then
  504.       isOK = true
  505.     else
  506.       print("Not enough fuel:".. fuel)
  507.     end
  508.   end
  509.   return isOK
  510. end
  511.  
  512. --- If the target has not already been
  513. -- inspected it gets checked here.
  514. -- If so it gets added to the inspected
  515. -- array, to avoid redundancy.
  516. -- If inspection shows block is wanted,
  517. -- its location gets added to the
  518. -- cubeStack
  519. -- @param way is must be dr.FORE,
  520. -- dr.STARBOARD, dr.FORE, dr.AFT
  521. -- dr.AHEAD, dr.UP or dr.DOWN
  522. -- @return true if target block matches
  523. -- what was wanted.
  524. veinMiner.check= function(way)
  525.   local isWanted = false
  526.  
  527.   local ix = 0
  528.   local iy = 0
  529.   local iz = 0
  530.   ix, iy, iz= dr.getTargetCoords(way)
  531.  
  532.   way = dr.correctHeading(way)
  533.  
  534.   -- If it still needs to be inspected,
  535.   if vm.isInspected(ix,iy,iz) then
  536.     vm.inspectedSkipped =
  537.         vm.inspectedSkipped + 1
  538.   else
  539.     local ok, item
  540.     ok, item= dr.inspect(way)
  541.    
  542.     if ok then
  543.       if item.name==vm.targetBlockName
  544.           then
  545.         isWanted = true
  546.         local locus= Locus.new( ix,
  547.             iy, iz)
  548.         table.insert( vm.cubeStack,
  549.             locus)
  550.       end -- match
  551.     end -- ok
  552.    
  553.     -- adds to the inspected array.
  554.     vm.setInspected(ix, iy, iz )
  555.   end
  556.  
  557.   return isWanted
  558. end
  559.  
  560. --- Moves, checks, and pushes to stack
  561. -- when applicable.
  562. -- @param way is either dr.AHEAD,
  563. -- STARBOARD, PORT, FORE, or AFT
  564. -- dr.UP, or dr.DOWN where dr is
  565. -- deadReckoner
  566. -- @param moves
  567. -- @return isAble true if it really was
  568. -- able to move and dig.
  569. -- @return whyNot if isAble, nil. Else,
  570. -- reason why not.
  571. veinMiner.explore= function(way, moves)
  572.  
  573.   way = dr.correctHeading(way)
  574.  
  575.   local isAble, whynot
  576.  
  577.   for i = 1, moves do
  578.     isAble, whynot = dr.move(way)
  579.    
  580.     -- if cannot, because obstructed
  581.     if not isAble then
  582.    
  583.       if whynot=="Movement obstructed"
  584.           then
  585.         vm.check( way )
  586.         isAble, whynot = dr.dig( way )
  587.         isAble, whynot= dr.move( way )
  588.       else
  589.         print( "Stuck. ".. whynot )
  590.       end
  591.      
  592.     end
  593.    
  594.   end -- end moves loop
  595.  
  596.   return isAble, whynot
  597. end
  598.  
  599. --- Moves starboard or port
  600. -- depending on where dest is compared
  601. -- to the robot's current location.  
  602. -- Inspects and/or breaks when needed.
  603. -- @param dest a target location on
  604. -- the same x/z plane
  605. veinMiner.exploreToX= function( dest )
  606.  
  607.   local diff = dest.x - dr.place.x
  608.   local moves = math.abs(diff)
  609.   local way = 0
  610.   if diff > 0 then
  611.     way = dr.STARBOARD
  612.   elseif diff < 0 then
  613.     way = dr.PORT
  614.   end
  615.  
  616.   vm.explore( way, moves)
  617.  
  618. end
  619.  
  620. --- Up or down
  621. veinMiner.exploreToY= function( dest )
  622.  
  623.   local diff = dest.y - dr.place.y
  624.   local moves = math.abs(diff)
  625.   local way = 0
  626.   if diff > 0 then
  627.     way = dr.UP
  628.   elseif diff < 0 then
  629.     way = dr.DOWN
  630.   end
  631.  
  632.   vm.explore( way, moves)
  633.  
  634. end
  635.  
  636. --- Fore or aft
  637. veinMiner.exploreToZ= function( dest )
  638.   local diff = dest.z - dr.place.z
  639.   local moves = math.abs(diff)
  640.   local way = 0
  641.   if diff > 0 then
  642.     way = dr.FORE
  643.   elseif diff < 0 then
  644.     way = dr.AFT
  645.   end
  646.  
  647.   vm.explore( way, moves)
  648. end
  649.  
  650. --- Moves to the location, inspecting,
  651. -- pushing and breaking when needed
  652. -- @param place is location relative
  653. -- to turtle's original location
  654. veinMiner.exploreTo= function( place )
  655.   vm.exploreToX( place )
  656.   vm.exploreToZ( place )
  657.   vm.exploreToY( place )
  658. end
  659.  
  660. --- Inspects block at the given
  661. -- coordinates, moving if needed.
  662. -- Pushes matching loci via check()
  663. -- Digs matching loci via dig()
  664. veinMiner.goLookAt= function(x, y, z)
  665.  
  666.   local dest = {x, y, z}
  667.  
  668.   local diffs= {x- dr.place.x,
  669.                 y- dr.place.y,
  670.                 z- dr.place.z }
  671.  
  672.   -- count of non-zero dimensions
  673.   local nzCount= 0
  674.   for i = 1, 3 do
  675.     if diffs[i] ~= 0 then
  676.       nzCount= nzCount+ 1
  677.     end
  678.   end
  679.  
  680.   local direction = 0
  681.   local dist = 0
  682.   local dest = Locus.new(x,y,z)
  683.  
  684.   -- All three coordinates differ
  685.   if nzCount > 2 then
  686.     -- Explore the farthest way
  687.     direction, dist=
  688.         dr.furthestWay(dest)
  689.     vm.explore(direction, dist)    
  690.   end
  691.  
  692.   -- Two or three coords were different
  693.   if nzCount > 1 then
  694.     -- Explore (what is now) the
  695.     -- farthest way
  696.     direction, dist=
  697.         dr.furthestWay(dest)
  698.     vm.explore(direction, dist)
  699.   end
  700.  
  701.   -- Unless it's to inspect self
  702.   if nzCount > 0 then
  703.     -- Explore *Up To* dest, farthest
  704.     direction, dist=
  705.         dr.furthestWay(dest)
  706.     dist = dist - 1
  707.     vm.explore(direction, dist)
  708.     -- Check it
  709.     if direction <= dr.PORT then
  710.       direction= dr.AHEAD
  711.     end
  712.     if vm.check(direction) or
  713.         vm.isAll then
  714.       dr.dig(direction)
  715.     end
  716.   else -- Coords were turtle's place
  717.     vm.inspectSelfAvoidance=
  718.         vm.inspectSelfAvoidance + 1
  719.     vm.setInspected(x,y,z)
  720.   end
  721.  
  722. end
  723.  
  724. --- Pulls a location from the stack,
  725. -- inspects it surrounding blocks. Each
  726. -- matching location gets added to the
  727. -- stack.
  728. veinMiner.inspectACube= function()
  729.  
  730.   -- Pops one
  731.   local cube=table.remove(vm.cubeStack)
  732.  
  733.   -- shorthand
  734.   local cis= vm.cubeInspectionSequence
  735.  
  736.   -- For each surrounding locus sl
  737.   local maxOfSequence = table.maxn(
  738.       vm.cubeInspectionSequence )
  739.   for sl= 1, maxOfSequence do
  740.     local x= cube.x + cis[sl][1]
  741.     local y= cube.y + cis[sl][2]
  742.     local z= cube.z + cis[sl][3]
  743.     -- If not already inspected
  744.     if vm.isInspected(x,y,z) then
  745.       vm.inspectedSkipped =
  746.           vm.inspectedSkipped + 1
  747.       vm.goLookSkipped =
  748.           vm.goLookSkipped + 1
  749.     else
  750.       vm.goLookAt( x, y, z )
  751.     end -- if not inspected
  752.   end
  753. end
  754.  
  755. --- Sees if there's enough space in
  756. -- the inventory for another cube
  757. -- of target material
  758. veinMiner.isInvtrySpaceAvail=function()
  759.  
  760.   local isAvail = false
  761.   local frSpace = 0
  762.   for i = 1, 16 do
  763.     local itmCount = t.getItemCount(i)
  764.    
  765.     if itmCount == 0 then
  766.       frSpace= frSpace+ 64
  767.     else
  768.       local slName=
  769.           t.getItemDetail(i).name
  770.          
  771.       if slName==
  772.           vm.targetBlockName then
  773.         frSpace= frSpace+ 64- itmCount
  774.       end -- match
  775.      
  776.     end -- count zero-else
  777.    
  778.   end -- inventory loop
  779.  
  780.   -- Assuming a cube could be no more
  781.   -- than 25 blocks, realistically
  782.   if frSpace >= 25 then
  783.     isAvail = true
  784.   else
  785.     print( "There might not be "..
  786.     "enough inventory \nspace to "..
  787.     "hold the target material" )
  788.   end
  789.   return isAvail
  790. end
  791.  
  792. --- Digs out the remaining blocks
  793. -- between the minimum, maximum
  794. -- coordinates so far
  795. veinMiner.clearRectangle= function()
  796.  
  797.   local fulOK = true
  798.   local tx = dr.placeMIN.x
  799.   while tx<= dr.placeMAX.x and fulOK do
  800.     local ty = dr.placeMIN.y
  801.     while ty<= dr.placeMAX.y and
  802.         fulOK do
  803.       local tz = dr.placeMIN.z
  804.       while tz<= dr.placeMAX.z and
  805.           fulOK do
  806.        
  807.         if not vm.isInspected(tx,ty,tz)
  808.             then
  809.           fulOK= vm.isFuelOK4Dest(
  810.               tx,ty,tz )
  811.           if fulOK then
  812.             vm.exploreTo(
  813.                 Locus.new(tx, ty, tz))
  814.           end
  815.         end
  816.        
  817.         tz = tz + 1
  818.       end
  819.       ty = ty + 1
  820.     end
  821.     tx = tx + 1
  822.   end
  823.  
  824. end
  825.  
  826. --- The main function: Inspects the
  827. -- block in front of it and sets its
  828. -- name of that as the target material,
  829. -- then mines for that until the vein
  830. -- has been explored, the fuel is gone
  831. -- or there isn't any more room.
  832. veinMiner.mine= function( args )
  833.  
  834.   local isRectangle = false
  835.   local isArgOK = true
  836.   if table.getn( args ) > 0 then
  837.     if args[1] == "a" then
  838.       vm.isAll = true
  839.     elseif args[1] == "r" then
  840.       vm.isAll = true
  841.       isRectangle = true
  842.     else
  843.       print( "Unknown argument: \"" ..
  844.         args[1] .. "\". \n"..
  845.         "Acceptable arguments are "..
  846.         "a or r.  \n"..
  847.         "Edit script for details." )
  848.       isArgOK = false
  849.     end
  850.   end
  851.  
  852.   local isBlockOK = false
  853.   local block = {}
  854.  
  855.   isBlockOK, block =
  856.       dr.inspect( dr.AHEAD )
  857.  
  858.   if isBlockOK and isArgOK then
  859.    
  860.     veinMiner.targetBlockName =
  861.         block.name
  862.     print("target block: "..
  863.         block.name)
  864.    
  865.     vm.check( dr.AHEAD )
  866.    
  867.     while vm.isFuelOK4Cube() and
  868.         vm.isInvtrySpaceAvail() and
  869.         (not vm.isVeinExplored()) do
  870.       vm.inspectACube()
  871.     end
  872.    
  873.     if isRectangle then
  874.       vm.clearRectangle()
  875.     end
  876.    
  877.     -- Comes back and faces forward
  878.     vm.exploreTo( Locus.new(0,0,0) )
  879.     dr.bearTo( dr.FORE )
  880.    
  881.     print( "inspected count: "..
  882.         vm.inspectedCount )
  883.     print( "inspections skipped: "..
  884.         vm.inspectedSkipped )
  885.     print( "goLookAt calls skipped: "..
  886.         vm.goLookSkipped )
  887.     print( "inspect-self avoidance: "..
  888.         vm.inspectSelfAvoidance )
  889.    
  890.     -- Possibly more found on way back
  891.     -- If so, report it.
  892.     local cubesYet=
  893.         table.maxn(vm.cubeStack)
  894.     if cubesYet > 0 then
  895.       print( "There are still "..
  896.           cubesYet..
  897.           " cubes to be explored." )
  898.     end
  899.   elseif not isBlockOK then
  900.     print( "To start, there must \n"..
  901.         "be a block of interest \n"..
  902.         "in front of the turtle." )
  903.   end
  904. end
  905.  
  906. vm.mine({...})
Advertisement
Add Comment
Please, Sign In to add comment