Advertisement
ArchReplicator

stripmine.lua

Jun 26th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.08 KB | None | 0 0
  1. --[[ This program strip mines 1 wide x 3 high tunnels, 3 blocks apart
  2.      Shafts are connected at both ends to simplify return to base logic
  3.      Torches are placed on the floor of the shafts
  4.      Two chests needed for setup, one to receive output, one to supply torches
  5.      
  6.      Assumes a robot with a generator, inventory manager, diamond pick equipped
  7.      Torches in slot 16, Fuel in slot 15
  8.      
  9.      The robot tries to make the tunnels safe to walk.  Cobble in slot 1 is used for this.
  10.      
  11.      Author: ArchReplicator
  12.      
  13.      Invocation
  14.      
  15.      stripmine length numstrips [left | right]
  16.      
  17.      length is how far to mine, default 100
  18.      numstrips is how many tunnels, default 5
  19.      left/right is direction to work, default left
  20.      
  21.      Setup
  22.      
  23.      T=tunnel I=input chest O=output chest R=robot
  24.      
  25.      We start with an 8 meter pre-dug tunnel to ensure lave doesn't eat the chests
  26.      
  27.         T
  28.         T
  29.         T
  30.         T
  31.         T
  32.         T
  33.         T
  34.         T
  35.         RI
  36.         O
  37.          ]]
  38.          
  39.  local component = require("component")
  40.  local shell = require("shell")
  41.  local generator, ic
  42.  
  43.  -- check required equipment
  44.  
  45.  if not component.isAvailable("robot") then
  46.    io.stderr:write("This program must run on a robot.")
  47.    return
  48. end
  49.  
  50. if not component.isAvailable("generator") then
  51.     io.stderr:write("The robot needs a generator.")
  52.     return
  53. else
  54.   generator = component.getPrimary("generator")
  55. end
  56.  
  57. if not component.isAvailable("inventory_controller") then
  58.     io.stderr:write("The robot needs an inventory controller.")
  59.     return
  60. else
  61.    ic = component.getPrimary("inventory_controller")
  62. end
  63.  
  64. local robot = require("robot")
  65.  
  66. -- get arguments
  67.  
  68. local args, options = shell.parse(...)
  69.  
  70. -- "global" variables (defined as local to this file which contains the whole thing)
  71.  
  72. local minedir = 1 -- valid values are 1(->chest),2(left),3(away),4(right)
  73. local dx = 0      -- start at 0 x,y,z displacement
  74. local dy = 0
  75. local dz = 0
  76.  
  77. local savedx, savedy, savedz, savedir
  78.  
  79. local diglen, numtun, direct
  80.  
  81. -- function definitions
  82. --[[
  83. movement functions
  84. keeps track of direction and movement in the dx,dy,dz variables
  85. direction vectors x,z,y
  86. 1 vector -1,0,0
  87. 2 vector 0,-1,0
  88. 3 vector 1,0,0
  89. 4 vector 0,1,0
  90. up vector 0,0,-1
  91. down vector 0,0,1
  92. ]]
  93.  
  94. local deltas = { -- entries are X, Z, Y (note order!!!)
  95.     {-1, 0, 0},
  96.     {0, -1, 0},
  97.     {1, 0, 0},
  98.     {0, 1, 0},
  99.     {0, 0, -1}, -- up
  100.     {0, 0, 1}  -- down
  101.     }
  102.    
  103. function movefwd()
  104.     local ok, reason = robot.forward()
  105.     if ok then
  106.         dx = dx + deltas[minedir][1]
  107.         dz = dz + deltas[minedir][2]
  108.         dy = dy + deltas[minedir][3]
  109.     else
  110.         io.stderr:write("Can't move forward: " .. reason)
  111.     end
  112.     return ok, reason
  113. end
  114.  
  115. function moveup()
  116.     local ok, reason = robot.up()
  117.     if ok then
  118.         dx = dx + deltas[5][1]
  119.         dz = dz + deltas[5][2]
  120.         dy = dy + deltas[5][3]
  121.     else
  122.         io.stderr:write("Can't move up: " .. reason)
  123.     end
  124.     return ok,reason
  125. end
  126.  
  127. function movedown()
  128.     local ok, reason = robot.down()
  129.     if ok then
  130.         dx = dx + deltas[6][1]
  131.         dz = dz + deltas[6][2]
  132.         dy = dy + deltas[6][3]
  133.     else
  134.         io.stderr:write("Can't move down: " .. reason)
  135.     end
  136.     return ok,reason
  137. end
  138.  
  139. function turnleft()
  140.     robot.turnLeft()
  141.     if minedir == 1 then
  142.         minedir = 4
  143.     else
  144.         minedir = minedir - 1
  145.     end
  146. end
  147.  
  148. function turnright()
  149.     robot.turnRight()
  150.     if minedir == 4 then
  151.         minedir = 1
  152.     else
  153.         minedir = minedir + 1
  154.     end
  155. end
  156.  
  157. function turnaround()
  158.     turnleft()
  159.     turnleft()
  160. end
  161.  
  162. function turnto(tgtdir)
  163.     if tgtdir == minedir then
  164.         return
  165.     elseif tgtdir < minedir then
  166.     do
  167.         local num = minedir - tgtdir
  168.         for i = 1,num do turnleft() end
  169.     end
  170.     else do
  171.         local num = tgtdir - minedir
  172.         for i = 1,num do turnright() end
  173.     end
  174.     end
  175. end
  176.  
  177. function printpos()
  178.     io.stdout:write(dx, " ", dz, " ", dy, " ", minedir, "\n")
  179. end
  180.  
  181. function xmoveto(tgtx)
  182.  
  183.     local done = false
  184.     local lsavedir = minedir
  185.     if tgtx == dx then
  186.         return
  187.     end
  188.     if tgtx < dx then
  189.         turnto(1)
  190.     else
  191.         turnto(3)
  192.     end
  193.     while not done do
  194.         if tgtx == dx then
  195.             done = true
  196.         else
  197.             movefwd()
  198.         end
  199.     end
  200.     turnto(lsavedir)
  201. end
  202.  
  203. function zmoveto(tgtz)
  204.  
  205.     local done = false
  206.     local lsavedir = minedir
  207.     if tgtz == dz then
  208.         return
  209.     end
  210.    
  211.     if tgtz < dz then
  212.         turnto(2)
  213.     else
  214.         turnto(4)
  215.     end
  216.     while not done do
  217.         if tgtz == dz then
  218.             done = true
  219.         else
  220.             movefwd()
  221.         end
  222.     end
  223.     turnto(lsavedir)
  224. end
  225.  
  226. function ymoveto(tgty)
  227.  
  228.     local done = false
  229.     while not done do
  230.         if dy == tgty then
  231.             done = true
  232.         elseif dy > tgty then
  233.             moveup()
  234.         else
  235.             movedown()
  236.         end
  237.     end -- while
  238. end
  239.    
  240. function rtb() -- return to base
  241.  
  242.     savedx = dx; savedy = dy; savedz = dz; savedir = minedir
  243.     -- get Y to zero
  244.     ymoveto(0)
  245.  
  246.     -- go to a cross tunnel
  247.     local tgtx
  248.     if (minedir == 1) or (minedir == 3) then
  249.         if minedir == 1 then    -- current direction toward chest
  250.             tgtx = diglen + 8   -- go back to far end of current tunnel
  251.         else                    -- away from chest
  252.             tgtx = 8            -- go to near end of current tunnel
  253.         end
  254.  
  255.         turnaround()
  256.         xmoveto(tgtx)
  257.  
  258.     end -- done moving to end of tunnel
  259.    
  260.     -- go to the initial tunnel
  261.     zmoveto(0)
  262.  
  263.     -- go to start pos
  264.     turnto(1)
  265.     xmoveto(0)
  266.  
  267. end -- rtb
  268.    
  269. function resumedig() -- back to digging after a return to base
  270.  
  271.     -- go to the side tunnel needed to reach old position
  272.     turnto(3) -- away
  273.     if (savedir == 1) or (savedx == diglen+8) then  -- old direction toward, or in far side tunnel
  274.         xmoveto(diglen+8)
  275.     else                                            -- old direction away, or in near side tunnel
  276.         xmoveto(8)
  277.     end
  278.  
  279.     -- go to the old z location (which mining tunnel or side tunnel depth)
  280.  
  281.     zmoveto(savedz)
  282.  
  283.     -- go to the mining location within the tunnel
  284.  
  285.     turnto(savedir)
  286.     xmoveto(savedx)
  287.     ymoveto(savedy)
  288.  
  289. end -- resumedig
  290.    
  291.  
  292. -- mainline code
  293.  
  294. diglen = args[1] or 100
  295. numtun = args[2] or 5
  296. direct = args[3] or "left"
  297.  
  298. if (direct ~= "left") and (direct ~= "right") then
  299.     io.stderr:write("Invalid direction, left assumed")
  300.     direct = "left"
  301. end
  302.  
  303. -- put main call here
  304.  
  305. -- 1st test: directions
  306.  
  307. --printpos()
  308. --turnaround()
  309. --printpos()
  310. --for i=1,5 do movefwd() end
  311. --turnleft()
  312. --for i=1,3 do movefwd() end
  313. --for i=1,2 do moveup() end
  314. --printpos()
  315. --rtb()
  316. --printpos()
  317. --resumedig()
  318. --printpos()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement