rdalkire

wallSherpa

Jul 25th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.74 KB | None | 0 0
  1. -- Starting at top of hole/cliff/wall,
  2. -- this places ladders and torches
  3. -- downward so you can climb safetly
  4.  
  5. -- Run with no args or -h for usage
  6.  
  7. --[[ wallSherpa
  8. Copyright (c) 2016
  9. Robert David Alkire II, AKA rdalkire,
  10. IGN ian_xw
  11. (some parts contributed by others. I
  12. credit them at the top of those parts)
  13. Distributed under the MIT License.
  14. (See accompanying file LICENSE or copy
  15. at http://opensource.org/licenses/MIT)
  16. ]]
  17.  
  18. -- when 'compiling', use native turtle
  19. local t = turtle
  20.  
  21. Locus = {}
  22. Locus.__index = Locus
  23. Locus.x = 0
  24. Locus.y = 0
  25. Locus.z = 0
  26. --- A constructor which sets the
  27. -- current location
  28. Locus.new= function( x, y, z )
  29.   local self = setmetatable({}, Locus)
  30.   self.x = x
  31.   self.y = y
  32.   self.z = z
  33.   return self
  34. end
  35.  
  36. local deadReckoner = {}
  37. local dr = deadReckoner
  38.  
  39. --- relative to turtle heading at start
  40. deadReckoner.FORE = 0
  41. deadReckoner.STARBOARD = 1
  42. deadReckoner.AFT = 2
  43. deadReckoner.PORT = 3
  44.  
  45.  
  46. deadReckoner.WAYS = {}
  47. dr.WAYS[deadReckoner.FORE] = "FORE"
  48. dr.WAYS[deadReckoner.STARBOARD]=
  49.     "STARBOARD"
  50. dr.WAYS[deadReckoner.AFT] = "AFT"
  51. dr.WAYS[deadReckoner.PORT] = "PORT"
  52.  
  53. deadReckoner.heading=deadReckoner.FORE
  54.  
  55. --- Current position relative to start
  56. deadReckoner.place= Locus.new(0, 0, 0)
  57.  
  58. --- Maximum x, y, z relative to start,
  59. -- located or dug
  60. deadReckoner.placeMAX=Locus.new(0,0,0)
  61.  
  62. --- Minimum x, y, z relative to start,
  63. -- located or dug
  64. deadReckoner.placeMIN=Locus.new(0,0,0)
  65.  
  66. --- forward regardless of heading
  67. deadReckoner.AHEAD = 4
  68.  
  69. deadReckoner.UP = 5
  70. deadReckoner.DOWN = 6
  71.  
  72. --- again regardless of heading
  73. deadReckoner.BACK = 7
  74.  
  75. --- Turns as needed to face the
  76. -- target direction indicated
  77. -- @param target must be dr.FORE,
  78. -- dr.STARBOARD, dr.AFT, or dr.PORT
  79. deadReckoner.bearTo= function(target)
  80.  
  81.   local trnsRght =
  82.       target - deadReckoner.heading
  83.  
  84.   local trns = math.abs( trnsRght )
  85.   if trns ~= 0 then
  86.    
  87.     if trns== 3 then
  88.       trns= 1
  89.       trnsRght= trnsRght/-3
  90.     end
  91.    
  92.     local i = 0
  93.     while i < trns do
  94.       if trnsRght >= 0 then
  95.         t.turnRight()
  96.       else
  97.         t.turnLeft()
  98.       end -- which way
  99.       i = i + 1
  100.     end -- turn loop
  101.   end -- there were any turns
  102.  
  103.   deadReckoner.heading = target
  104. end
  105.  
  106. --- If way is fore, starboard, aft or
  107. -- port, then bear to that direction,
  108. -- unless this is called for movement
  109. -- purposes and turtle was facing the
  110. -- opposite way to begin with.
  111. -- @param way can any of the heading
  112. -- constants: FORE, STARBOARD, AFT,
  113. -- PORT, UP, DOWN or even AHEAD
  114. -- @param [isForMovement] true if the
  115. -- caller is using this in order to
  116. -- move.
  117. -- @return way is AHEAD if the param
  118. -- had been a horizontal direction
  119. -- (FORE, AFT, PORT, STARBOARD).
  120. -- Otherwise it's the same as the input
  121. -- param. If isForMovement, and the
  122. -- turtle is facing opposite way, then
  123. -- this will return BACK
  124. deadReckoner.correctHeading=
  125.     function(way, isForMovement)
  126.    
  127.   if way < 4 then
  128.     if isForMovement and
  129.         way ~= dr.heading and
  130.         (way - dr.heading) % 2== 0 then
  131.       way = dr.BACK
  132.     else
  133.       dr.bearTo( way )
  134.       way = dr.AHEAD
  135.     end
  136.   end
  137.  
  138.   return way
  139.  
  140. end
  141.  
  142. --- Adjusts placeMAX and placeMIN as
  143. -- applicable.
  144. deadReckoner.setMaxMin=function(x,y,z)
  145.  
  146.   if x > dr.placeMAX.x then
  147.     dr.placeMAX.x = x
  148.   elseif x < dr.placeMIN.x then
  149.     dr.placeMIN.x = x
  150.   end
  151.  
  152.   if y > dr.placeMAX.y then
  153.     dr.placeMAX.y = y
  154.   elseif y < dr.placeMIN.y then
  155.     dr.placeMIN.y = y
  156.   end
  157.  
  158.   if z > dr.placeMAX.z then
  159.     dr.placeMAX.z = z
  160.   elseif z < dr.placeMIN.z then
  161.     dr.placeMIN.z = z
  162.   end
  163.  
  164. end
  165.  
  166. --- Gets the coordinates of the block
  167. -- currently next to the turtle,
  168. -- depending on which way one would
  169. -- look.
  170. -- @param way must be deadReckoner's
  171. -- (dr's) AFT, FORE, PORT, STARBOARD,
  172. -- UP, DOWN or AHEAD.
  173. -- @return x, y, z coordinates of the
  174. -- adjacent block.
  175. deadReckoner.getTargetCoords=
  176.     function(way)
  177.  
  178.   local ix = dr.place.x
  179.   local iy = dr.place.y
  180.   local iz = dr.place.z
  181.  
  182.   if way == dr.AHEAD then
  183.     way = dr.heading
  184.   end
  185.  
  186.   if way== dr.AFT then
  187.     iz= iz - 1
  188.   elseif way== dr.FORE then
  189.     iz= iz + 1
  190.   elseif way== dr.PORT then
  191.     ix= ix- 1
  192.   elseif way== dr.STARBOARD then
  193.     ix= ix+ 1
  194.   elseif way== dr.UP then
  195.     iy= iy + 1
  196.   elseif way== dr.DOWN then
  197.     iy= iy - 1
  198.   end
  199.  
  200.   return ix, iy, iz
  201.  
  202. end
  203.  
  204.  
  205. --- Finds the distance between the
  206. -- current location and some other
  207. -- place, without diagonal travel
  208. -- @param x, y, z are the coords of
  209. -- the other place
  210. -- @return the distance
  211. deadReckoner.howFarFrom=function(x,y,z)
  212.   local dx= math.abs( dr.place.x- x )
  213.   local dy= math.abs( dr.place.y- y )
  214.   local dz= math.abs( dr.place.z- z )
  215.   return dx + dy + dz
  216. end
  217.  
  218. --- Calculates distance from starting
  219. -- place, considering that turtles
  220. -- do not move diagonally in their
  221. -- present form.
  222. -- @return number of moves to get back
  223. deadReckoner.howFarFromHome=function()
  224.   return math.abs(dr.place.x)+
  225.       math.abs(dr.place.y)+
  226.       math.abs(dr.place.z)
  227. end
  228.  
  229. --- Inspects the given direction, and
  230. -- also calls to evaluate the target
  231. -- block for max and min coords.
  232. -- @param way FORE, UP, AHEAD etc
  233. -- @return boolean success, table
  234. -- data/string error message
  235. deadReckoner.inspect= function(way)
  236.  
  237.   way = dr.correctHeading(way)
  238.   local ok, item
  239.   if way== dr.AHEAD then
  240.     ok, item= t.inspect()
  241.   elseif way== dr.UP then
  242.     ok, item= t.inspectUp()
  243.   else
  244.     ok, item= t.inspectDown()
  245.   end
  246.  
  247.   local ix, iy, iz =
  248.       dr.getTargetCoords(way)
  249.  
  250.   dr.setMaxMin(ix, iy, iz)
  251.   return ok, item
  252. end
  253.  
  254. --- Digs.
  255. -- @param way must be dr.FORE,
  256. -- dr.STARBOARD, dr.FORE, dr.AFT
  257. -- dr.AHEAD, dr.UP or dr.DOWN
  258. -- @return isAble true if it really
  259. -- was able to dig
  260. -- @return whyNot if isAble, nil. Else,
  261. -- reason why not.
  262. deadReckoner.dig= function( way )
  263.  
  264.   way = dr.correctHeading( way )
  265.  
  266.   local dug= false
  267.   local whyNot
  268.   if way== dr.AHEAD then
  269.     dug, whyNot= t.dig()
  270.   elseif way== dr.UP then
  271.     dug, whyNot= t.digUp()
  272.   elseif way== dr.DOWN then
  273.     dug, whyNot= t.digDown()
  274.   end
  275.  
  276.   return dug, whyNot
  277. end
  278.  
  279. --- Tries to move laterally, up or down.
  280. -- If successful,
  281. -- it updates its current location
  282. -- relative to where it started and
  283. -- returns true.
  284. -- Else, it returns false and the
  285. -- reason why not.
  286. -- @param way is dr.FORE, dr.STARBOARD,
  287. -- dr.AFT, dr.PORT, dr.AHEAD, dr.UP
  288. -- or dr.DOWN, where dr is deadReckoner
  289. -- @return isAble, whyNot
  290. deadReckoner.move= function( way )
  291.  
  292.   way = dr.correctHeading(way, true)
  293.  
  294.   -- where way is dr.AHEAD, UP or DOWN
  295.   local isAble, whynot
  296.   if way==dr.AHEAD or way==dr.BACK then
  297.     local forwardness = 1
  298.     if way== dr.AHEAD then
  299.       isAble, whynot = t.forward()
  300.     else
  301.       isAble, whynot = t.back()
  302.       forwardness = -1
  303.     end
  304.    
  305.     if isAble then
  306.       if dr.heading== dr.AFT then
  307.         dr.place.z=
  308.             dr.place.z - forwardness
  309.       elseif dr.heading== dr.FORE then
  310.         dr.place.z=
  311.             dr.place.z + forwardness
  312.       elseif dr.heading== dr.PORT then
  313.         dr.place.x=
  314.             dr.place.x- forwardness
  315.       else
  316.         dr.place.x=
  317.             dr.place.x+ forwardness
  318.       end
  319.      
  320.     end -- isAble
  321.   elseif way== dr.UP then
  322.     isAble, whynot = t.up()
  323.     if isAble then
  324.       dr.place.y = dr.place.y + 1
  325.     end
  326.   elseif way== dr.DOWN then
  327.     isAble, whynot = t.down()
  328.     if isAble then
  329.       dr.place.y = dr.place.y - 1
  330.     end
  331.   end -- AHEAD, UP or DOWN
  332.  
  333.   if isAble then
  334.     dr.setMaxMin( dr.place.x,
  335.         dr.place.y, dr.place.z )
  336.   end
  337.  
  338.   return isAble, whynot
  339. end
  340.  
  341. --- Places.
  342. -- @param way must be dr.FORE,
  343. -- dr.STARBOARD, dr.FORE, dr.AFT
  344. -- dr.AHEAD, dr.UP or dr.DOWN
  345. -- @return isAble true if it really
  346. -- was able to place the item
  347. -- @return whyNot if isAble, nil. Else,
  348. -- reason why not.
  349. deadReckoner.placeItem = function(way)
  350.  
  351.   way = dr.correctHeading( way )
  352.  
  353.   local placed= false
  354.   local whyNot
  355.   if way== dr.AHEAD then
  356.     placed, whyNot= t.place()
  357.   elseif way== dr.UP then
  358.     placed, whyNot= t.placeUp()
  359.   elseif way== dr.DOWN then
  360.     placed, whyNot= t.placeDown()
  361.   end
  362.  
  363.   return placed, whyNot
  364. end
  365.  
  366. --- Comparing destination with current
  367. -- location, this finds the dominant
  368. -- direction and distance in that
  369. -- direction. X - Z plane gets
  370. -- priority.
  371. -- @param dest destination coordinates
  372. -- @return direction: up, down, fore,
  373. -- aft, port or starboard
  374. -- @return distance
  375. deadReckoner.furthestWay =
  376.     function(dest)
  377.  
  378.   -- Dest - Current: +Srbrd -Port
  379.   local direction = 0
  380.   local dist = dest.x - dr.place.x
  381.   if dist >= 0 then
  382.     direction= dr.STARBOARD
  383.   else
  384.     direction= dr.PORT
  385.   end
  386.  
  387.   -- Find Z diff +fore -aft
  388.   local zDist = dest.z - dr.place.z
  389.   if math.abs(zDist)>math.abs(dist)then
  390.     dist= zDist
  391.     if dist >= 0 then
  392.       direction= dr.FORE
  393.     else
  394.       direction= dr.AFT
  395.     end
  396.   end
  397.  
  398.   -- Y:  +up -down
  399.   local yDist = dest.y - dr.place.y
  400.   if math.abs(yDist)>math.abs(dist)then
  401.     dist= yDist
  402.     if dist >= 0 then
  403.       direction= dr.UP
  404.     else
  405.       direction= dr.DOWN
  406.     end
  407.   end
  408.  
  409.   return direction, math.abs(dist)
  410.  
  411. end
  412.  
  413. --[[ BEGIN IN-LINE RELEASE getopt
  414. Copyright (c) 2016
  415. Authors: Admicos, dalkire.
  416. (See accompanying file LICENSE or copy
  417. at http://opensource.org/licenses/MIT)
  418. ]]
  419. local getopt = {}
  420.  
  421. local g_name
  422. local g_desc
  423. local g_options
  424.  
  425. --- displays help
  426. getopt.help= function()
  427.  
  428.   local help= g_name.. ": "..
  429.       g_desc.."\n".."USAGE: "..g_name..
  430.       " [options] [arg(s)]\n"..
  431.       "Options:\n"
  432.      
  433.   for key, val in pairs(g_options) do
  434.     help = help .. "--" .. key ..
  435.         " (-" .. val[2] .. ")"
  436.  
  437.     if val[3] ~= nil then
  438.       help = help ..
  439.           " [" .. val[3] .. "]"
  440.     end
  441.    
  442.     help= help.. ": ".. val[1].. "\n"
  443.   end
  444.  
  445.   if textutils then
  446.     textutils.pagedPrint(help)
  447.   else
  448.     print(help)
  449.   end
  450.  
  451. end
  452.  
  453. --- Initializes
  454. -- @param name program name
  455. -- @param desc program description
  456. -- @param options a table of options,
  457. --  **not** including --help or -h
  458. --  because getopt creates them for
  459. --  you.
  460. -- @param args the arguments passed in
  461. -- @return table or nil
  462. getopt.init= function(name, desc,
  463.     options, args)
  464.  
  465.   g_name = name
  466.   g_desc = desc
  467.   g_options = options
  468.  
  469.   local _resTbl = {}
  470.   local _isArg = false
  471.   local _optCnt = 1
  472.  
  473.   for indx, val in ipairs(args) do
  474.  
  475.     if val== "-h" or val=="--help" then
  476.       _resTbl = {}
  477.  
  478.       getopt.help()
  479.  
  480.       return nil
  481.     end
  482.  
  483.     if val:sub( 1, 1 ) == "-" then
  484.    
  485.       for nme, trio in pairs(options)do
  486.      
  487.         if val== "--".. nme or
  488.             val== "-".. trio[2] then
  489.            
  490.           if trio[3] ~= nil then
  491.             _resTbl[nme]= args[indx+ 1]
  492.             _isArg = true
  493.           else
  494.             _resTbl[nme] = true
  495.           end
  496.          
  497.         end -- if arg
  498.        
  499.       end -- options loop
  500.      
  501.     elseif not _isArg then
  502.       _resTbl["opt-" .. _optCnt] = val
  503.       _optCnt = _optCnt + 1
  504.     else
  505.       _isArg = false
  506.     end
  507.    
  508.   end -- args loop
  509.  
  510.   return _resTbl
  511. end
  512. -- END IN-LINE RELEASE getopt
  513.  
  514. -- wallSherpa ITSELF BEGINS HERE
  515.  
  516. local FILL_MIN = 12
  517.  
  518. local ITM_FILL="minecraft:cobblestone"
  519. local ITM_LADDER = "minecraft:ladder"
  520. local ITM_TORCH = "minecraft:torch"
  521.  
  522. local g_stay = false
  523. local g_height = 0
  524. local g_bed = true -- stops at bedrock
  525. local g_up = false
  526. local g_gap = false -- stops at gap
  527.  
  528. local g_torchInterval = 5
  529.  
  530. --- estimates and inventories torches,
  531. -- ladders and cobbles
  532. -- @return how many torches, ladders,
  533. -- and cobbles are needed to make up,
  534. -- and how many cobbles you actually
  535. -- have
  536. local function lddrsNTrchsDiff()
  537.  
  538.   local n = g_height
  539.   if g_bed then
  540.     n = n - 5
  541.   end
  542.  
  543.   local ladderReq = n * 2
  544.   local torchReq= math.ceil( n/
  545.       g_torchInterval )
  546.      
  547.   local cobbleReq= n * 3
  548.  
  549.   -- inventories ladders & torches
  550.   local laddersHave = 0
  551.   local torchesHave = 0
  552.   local cobbleHave = 0
  553.   for iSlot = 1, 16 do
  554.     local sltDt= t.getItemDetail(iSlot)
  555.     if t.getItemCount(iSlot)==0 then
  556.       -- no op
  557.     elseif sltDt.name==
  558.         ITM_LADDER then
  559.       laddersHave= laddersHave +
  560.           sltDt.count
  561.     elseif sltDt.name==
  562.         ITM_TORCH then
  563.       torchesHave = torchesHave +
  564.           sltDt.count
  565.     elseif sltDt.name== ITM_FILL then
  566.       cobbleHave=cobbleHave+sltDt.count
  567.     end -- slotname if
  568.   end -- slots loop
  569.  
  570.   local ladderDif = math.max(0,
  571.       ladderReq - laddersHave )
  572.   local torchDif = math.max( 0,
  573.       torchReq - torchesHave )
  574.   local cobbleDif = math.max( 0,
  575.       cobbleReq - cobbleHave )
  576.      
  577.   return ladderDif,torchDif, cobbleDif,
  578.       cobbleHave
  579.  
  580. end
  581.  
  582. --- Finds how many sticks needed for
  583. -- ladders, and how many sticks for
  584. -- torches
  585. local function lddrNTrchSticks(
  586.     ladderDif, torchDif )
  587.  
  588.   local trchStcks = 0
  589.   if torchDif > 0 then
  590.     print("== need ".. torchDif..
  591.         " more torches")
  592.     -- Nearest higher factor of 4
  593.     torchDif= math.ceil(torchDif/4)*4
  594.     print("Craft ".. torchDif..
  595.         " torches" )
  596.     local coalCnt= math.ceil(
  597.         torchDif / 4 )
  598.     trchStcks= coalCnt
  599.     print("coal & sticks needed for "..
  600.         "torches: ".. coalCnt )
  601.   end
  602.  
  603.   local lddrStcks = 0
  604.   if ladderDif > 0 then
  605.     print("== more ladders needed: "..
  606.         ladderDif )
  607.     -- Nearest higher factor of 3
  608.     ladderDif= math.ceil(ladderDif/3)*3
  609.     print("Ladders to craft: "..
  610.         ladderDif )
  611.     lddrStcks= math.ceil(
  612.         ladderDif * 7 / 3 )
  613.     print("ladder-sticks needed: "..
  614.         lddrStcks )
  615.   end
  616.  
  617.   return lddrStcks, trchStcks
  618.  
  619. end
  620.  
  621. --- @return true if fuel is enough
  622. local function checkFuel()
  623.   local isGood = false
  624.   local fuel = t.getFuelLevel()
  625.   if fuel == "unlimited" then
  626.     isGood = true
  627.   else
  628.  
  629.     local d = g_height
  630.     if g_bed then
  631.       d = d - 5
  632.     end
  633.    
  634.     -- returning fuel
  635.     local rtn = d
  636.     if g_stay then
  637.       rtn = 0
  638.     end
  639.    
  640.     local needed = d * 2 + -- ladders
  641.                   (d/5)*2+ -- torches
  642.                    rtn +   -- returning
  643.                    20      -- buffer
  644.                    
  645.     local diff = math.max(0,
  646.         needed - fuel )
  647.    
  648.     if diff == 0 then
  649.       isGood = true
  650.     else
  651.       print("fuel is too low: ".. fuel)
  652.       print("needs at least: "..needed)
  653.       print("difference: ".. diff)
  654.     end  
  655.   end
  656.   return isGood
  657. end
  658.  
  659. --- Finds out if there are enough
  660. -- ladders and torches.  If not it
  661. -- displays a manifest of what's needed
  662. local function checkSupplies()
  663.   local areOK = false
  664.  
  665.   local ladderDif, torchDif, cobbleDif,
  666.       cobbleHave = lddrsNTrchsDiff()
  667.  
  668.   local lddrStcks, trchStcks =
  669.       lddrNTrchSticks( ladderDif,
  670.           torchDif )
  671.  
  672.   if ladderDif + torchDif == 0 then
  673.     areOK = true
  674.   else
  675.     local stickCnt= lddrStcks +
  676.         trchStcks
  677.    
  678.     -- Nearest higher factor of 4
  679.     stickCnt= math.ceil(stickCnt/4 )*4
  680.     print( "total sticks to craft: "..
  681.         stickCnt )
  682.    
  683.     local plankCnt= math.ceil(
  684.         stickCnt * 2 / 4 )
  685.    
  686.     plankCnt= math.ceil(plankCnt/4)*4
  687.    
  688.     print(
  689.         "planks to craft for sticks "..
  690.         plankCnt )
  691.    
  692.     local logCnt = math.ceil(
  693.         plankCnt / 4 )
  694.     print("logs for planks: "..
  695.         logCnt )
  696.    
  697.   end
  698.  
  699.   if cobbleHave < FILL_MIN then
  700.     areOK = false
  701.    
  702.     -- TODO parse fill item for warning
  703.     print("You should have at least "..
  704.         FILL_MIN.. " cobbles")
  705.     print("If building whole wall "..
  706.         "you'd need ".. cobbleDif..
  707.         " more cobblestone blocks")
  708.   end
  709.  
  710.   if not checkFuel() then
  711.     areOK = false
  712.   end
  713.  
  714.   return areOK
  715. end
  716.  
  717. --- Manages the input arguments.
  718. -- Validates and parses them.
  719. -- @param targs the arguments passed
  720. -- in when starting the program
  721. local function mngArgs(targs)
  722.   local _optionsExample = {
  723.       ["stay"] = {
  724.           "Stay and don't come back.",
  725.           "s", nill },
  726.       ["bedrock"] = {
  727.           "go all the way to "..
  728.           "Bedrock (true if not "..
  729.           "specified and going "..
  730.           "down.  If going up, "..
  731.           "this isn't applicable).",
  732.           "b", "<true|false>"},
  733.       ["up"] = {
  734.           "go Up instead.",
  735.           "u", nill },
  736.       ["gap"] = {
  737.           "stop at the first Gap "..
  738.           "(If not specified, "..
  739.           "defaults true going up,"..
  740.           " false going down).",
  741.           "g", "<true|false>"},
  742.       ["torchInterval"] = {
  743.           "Define Interval between "..
  744.           "torches. Defaults to 5",
  745.           "i", "<num>"}
  746.   }
  747.  
  748.   local isOK = true
  749.  
  750.   local tbl= getopt.init("wallSherpa",
  751.       "This is for after you've "..
  752.       "used the excavate script, "..
  753.       "and you have a deep hole in"..
  754.       " front of you.  Finds out "..
  755.       "how many ladders and torches "..
  756.       "you need, and places them. "..
  757.       "\nUse turtle's Y coord "..
  758.       "as the argument to go all "..
  759.       "the way to bedrock",
  760.       _optionsExample, targs )
  761.  
  762.   if tbl== nil then
  763.     -- user had used -h as option
  764.     isOK = false
  765.   elseif next(tbl)== nil then
  766.     -- no options or args specified
  767.     isOK = false
  768.     getopt.help()
  769.   else
  770.  
  771.     if tbl["stay"] then
  772.       g_stay = true
  773.     end
  774.    
  775.     if tbl["up"] then
  776.       g_up = true
  777.       g_bed = false
  778.       g_gap = true
  779.     end
  780.    
  781.     if tbl["bedrock"] then
  782.    
  783.       local bedTxt = string.lower(
  784.           tbl["bedrock"] )
  785.      
  786.       if "true" == bedTxt then
  787.         g_bed = true
  788.       elseif "false" == bedTxt then
  789.         g_bed = false
  790.       elseif g_up then
  791.         isOK = false
  792.         print("Going up, so bedrock"..
  793.             "doesn't really apply.")
  794.       else
  795.         isOK = false
  796.         print("For Bedrock, you "..
  797.             "must specify true or "..
  798.             "false.")
  799.       end
  800.      
  801.     end
  802.    
  803.     if tbl["gap"] then
  804.       local gapTxt = string.lower(
  805.           tbl["gap"])
  806.          
  807.       if "true" == gapTxt then
  808.         g_gap = true
  809.       elseif "false" == gapTxt then
  810.         g_gap = false
  811.       else
  812.         print("Gap option requires"..
  813.             " true|false argument.")
  814.         isOK = false
  815.       end
  816.     end
  817.    
  818.     if tbl["torchInterval"] then
  819.       g_torchInterval = tonumber(
  820.           tbl["torchInterval"] )
  821.       if g_torchInterval == nil then
  822.         isOK = false
  823.         print("TorchInterval option"..
  824.             " requires number.")
  825.       end
  826.     end
  827.    
  828.     if tbl["opt-1"] then
  829.       g_height= tonumber( tbl["opt-1"])
  830.       if g_height== nil then
  831.         isOK = false
  832.         print("Arg must be numeric.")
  833.       elseif g_bed and g_height< 6 then
  834.         isOK = false
  835.         print("Arg must be 6 or more.")
  836.       end
  837.     elseif isOK then
  838.       isOK = false
  839.       print("Arg required for height.")
  840.     end -- opt-1
  841.   end
  842.  
  843.   return isOK
  844. end
  845.  
  846. --- Manages prerequisites. Validates
  847. -- and parses the options, arguments,
  848. -- and inventory supplies.
  849. -- @param targs the raw arguments
  850. -- @return true if prerequisites
  851. -- are OK
  852. local function mngPrereqs(targs)
  853.   local isOK = true
  854.  
  855.     isOK = mngArgs(targs)
  856.     if isOK then
  857.       isOK = checkSupplies()
  858.     end
  859.    
  860.   return isOK
  861. end
  862.  
  863. --- Select the slot that has the item
  864. -- @param itmName the item to match
  865. -- @return true if item is found
  866. local function selectSltWthItm(itmName)
  867.   local itm = t.getItemDetail()
  868.   local isFound = false
  869.   local nme
  870.   if itm ~= nill then
  871.     nme = itm.name
  872.     isFound = itmName == nme
  873.   end
  874.  
  875.   if not isFound then
  876.     local slt = t.getSelectedSlot()
  877.     local i = 1
  878.     while not isFound and i <= 16 do
  879.       if i ~= slt then
  880.         itm = t.getItemDetail(i)
  881.         if itm ~= nill then
  882.           nme = itm.name
  883.           if nme == itmName then
  884.             isFound = true
  885.             t.select(i)
  886.           end
  887.         end -- not nill
  888.       end -- not current slot
  889.       i = i + 1
  890.     end -- while
  891.   end -- not yet found
  892.   return isFound
  893. end
  894.  
  895. ---Meant for placing torches or ladders
  896. -- AFT if going down, or FORE if going
  897. -- up. When encountering a gap, it will
  898. -- depend on whether it's supposed to
  899. -- stop at gaps.  If so (g_gap, default
  900. -- true going up), it will return
  901. -- false.  Otherwise will fill in the
  902. -- gap with ITM_FILL and continue.
  903. -- @param itmName
  904. -- @return true if successful
  905. local function placeItem( itmName )
  906.   local isAble=selectSltWthItm(itmName)
  907.   local whyNt = nil
  908.   local way = dr.AFT
  909.   if g_up then
  910.     way = dr.FORE
  911.   end
  912.   if isAble then
  913.    
  914.     dr.dig(way)
  915.     isAble, whyNt=dr.placeItem(way)
  916.  
  917.     if not isAble and not g_gap then
  918.      
  919.       -- go back and place filler
  920.      
  921.       isAble, whyNt = dr.move(way)
  922.      
  923.       if isAble then
  924.         isAble = selectSltWthItm(
  925.             ITM_FILL )
  926.        
  927.         if isAble then
  928.           -- Could be abandoned mine
  929.           -- with fencing or cobweb
  930.           dr.dig(way)
  931.          
  932.           isAble, whyNt = dr.placeItem(
  933.               way )
  934.  
  935.           dr.move(dr.BACK)
  936.          
  937.           -- try again
  938.           selectSltWthItm(itmName)
  939.           isAble, whyNt = dr.placeItem(
  940.               way )
  941.         else
  942.           -- TODO parse fill item
  943.           whyNt = "out of ".. itmName
  944.         end -- if there's cobble
  945.       end -- able to move aft
  946.     elseif not isAble then
  947.       print("Not filling gap")
  948.       print(
  949.         "isAble: "..tostring(isAble)..
  950.         " g_gap: "..tostring(g_gap))
  951.     end -- wasn't able to place
  952.   else
  953.     whyNt = "Out of ".. itmName
  954.   end
  955.  
  956.   if not isAble then
  957.     print("unable to place; ".. whyNt)
  958.   end
  959.  
  960.   return isAble
  961. end
  962.  
  963. --- if fifth, move starboard,
  964. --  place a torch and come back
  965. --  @param d is distance down so far
  966. local function placeNthTrchStrbrd(d)
  967.   local rtrn = true
  968.   if d % g_torchInterval == 0 then
  969.     dr.move(dr.STARBOARD)
  970.     rtrn = placeItem(ITM_TORCH)
  971.     dr.move(dr.PORT)
  972.   end
  973.   return rtrn
  974. end
  975.  
  976. --- Moves down the shaft wall, placing
  977. -- ladders and torches.
  978. -- @return vertical distance traveled
  979. local function goPlaceThings()
  980.  
  981.   local lmt = g_height
  982.   if g_bed then
  983.     lmt = lmt - 5
  984.   end
  985.  
  986.   local actlDst = 0
  987.   local keepGoing = true
  988.  
  989.   local vert = dr.DOWN
  990.   if g_up then
  991.     vert = dr.UP
  992.   end
  993.  
  994.   local isFirstGoingUp = g_up
  995.  
  996.   while keepGoing and actlDst < lmt do
  997.    
  998.     if isFirstGoingUp then
  999.       isFirstGoingUp = false
  1000.     else
  1001.       keepGoing = dr.move(vert)
  1002.     end
  1003.    
  1004.     if keepGoing then
  1005.      
  1006.       if actlDst % 2 == 0 then --even
  1007.        
  1008.         keepGoing =
  1009.             placeItem( ITM_LADDER) and
  1010.             dr.move(dr.STARBOARD ) and
  1011.             placeItem( ITM_LADDER) and
  1012.             placeNthTrchStrbrd(actlDst)
  1013.        
  1014.       else -- odd
  1015.        
  1016.         keepGoing =
  1017.             placeNthTrchStrbrd(actlDst)
  1018.             and
  1019.             placeItem( ITM_LADDER) and
  1020.             dr.move(dr.PORT) and
  1021.             placeItem( ITM_LADDER )
  1022.        
  1023.       end -- even/odd
  1024.       actlDst = actlDst + 1
  1025.     end -- if keepgoing
  1026.   end -- while
  1027.   return actlDst
  1028. end -- function
  1029.  
  1030. --- Moves the turtle to the original
  1031. -- location. First left/right, then
  1032. -- up/down (in this app's case up, to
  1033. -- be realistic), then for/aft
  1034. local function comeBack()
  1035.  
  1036.   -- left/right
  1037.   local x = dr.place.x
  1038.   if x < 0 then
  1039.     -- move right abs x places
  1040.     for m = 1, math.abs(x) do
  1041.       dr.move(dr.STARBOARD)
  1042.     end
  1043.   elseif x > 0 then
  1044.     -- move left x times
  1045.     for m = 1, x do
  1046.       dr.move(dr.PORT)
  1047.     end
  1048.   end
  1049.  
  1050.   -- up/down
  1051.   local y = dr.place.y
  1052.   if y < 0 then
  1053.     for m = 1, math.abs(y) do
  1054.       dr.move(dr.UP)
  1055.     end
  1056.   elseif y > 0 then
  1057.     for m = 1, y do
  1058.       dr.move(dr.DOWN)
  1059.     end
  1060.   end
  1061.  
  1062.   -- fore/aft
  1063.   local z = dr.place.z
  1064.   if z < 0 then
  1065.     for m = 1, math.abs(z) do
  1066.       dr.move(dr.FORE)
  1067.     end
  1068.   elseif z > 0 then
  1069.     for m = 1, z do
  1070.       dr.move(dr.AFT)
  1071.     end
  1072.   end
  1073. end
  1074.  
  1075. local function adjStartLocation()
  1076.  
  1077.   if g_up then
  1078.     -- If up against wall, back up
  1079.     -- so that a ladder can be placed
  1080.     if t.inspect() then
  1081.       dr.move(dr.BACK)
  1082.     end
  1083.   else
  1084.     -- so turtle can start 1 from edge
  1085.     -- afterward, trtl is 1 away from
  1086.     -- wall, giving room to place
  1087.     if t.inspectDown() then
  1088.       dr.move(dr.AHEAD)
  1089.     end
  1090.     dr.move(dr.AHEAD)
  1091.     dr.bearTo(dr.AFT);
  1092.   end
  1093. end
  1094.  
  1095. local function main( targs )
  1096.  
  1097.   -- Check prereqs; warn as applicable
  1098.   if mngPrereqs(targs) then
  1099.     print("prereqs OK")
  1100.    
  1101.     adjStartLocation()
  1102.    
  1103.     -- Go down, placing things
  1104.     local d = goPlaceThings()
  1105.  
  1106.     if not g_stay then
  1107.       comeBack()
  1108.     end
  1109.    
  1110.     dr.bearTo(dr.FORE)
  1111.    
  1112.   end
  1113.  
  1114. end
  1115.  
  1116. local tArgs = {...}
  1117. main(tArgs)
Advertisement
Add Comment
Please, Sign In to add comment