Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ This program strip mines 1 wide x 3 high tunnels, 3 blocks apart
- Shafts are connected at both ends to simplify return to base logic
- Torches are placed on the floor of the shafts
- Two chests needed for setup, one to receive output, one to supply torches
- Assumes a robot with a generator, inventory manager, diamond pick equipped
- Torches in slot 16, Fuel in slot 15
- The robot tries to make the tunnels safe to walk. Cobble in slot 1 is used for this.
- Author: ArchReplicator
- Invocation
- stripmine length numstrips [left | right]
- length is how far to mine, default 100
- numstrips is how many tunnels, default 5
- left/right is direction to work, default left
- Setup
- T=tunnel I=input chest O=output chest R=robot
- We start with an 8 meter pre-dug tunnel to ensure lave doesn't eat the chests
- T
- T
- T
- T
- T
- T
- T
- T
- RI
- O
- ]]
- local component = require("component")
- local shell = require("shell")
- local generator, ic
- -- check required equipment
- if not component.isAvailable("robot") then
- io.stderr:write("This program must run on a robot.")
- return
- end
- if not component.isAvailable("generator") then
- io.stderr:write("The robot needs a generator.")
- return
- else
- generator = component.getPrimary("generator")
- end
- if not component.isAvailable("inventory_controller") then
- io.stderr:write("The robot needs an inventory controller.")
- return
- else
- ic = component.getPrimary("inventory_controller")
- end
- local robot = require("robot")
- -- get arguments
- local args, options = shell.parse(...)
- -- "global" variables (defined as local to this file which contains the whole thing)
- local minedir = 1 -- valid values are 1(->chest),2(left),3(away),4(right)
- local dx = 0 -- start at 0 x,y,z displacement
- local dy = 0
- local dz = 0
- local savedx, savedy, savedz, savedir
- local diglen, numtun, direct
- -- function definitions
- --[[
- movement functions
- keeps track of direction and movement in the dx,dy,dz variables
- direction vectors x,z,y
- 1 vector -1,0,0
- 2 vector 0,-1,0
- 3 vector 1,0,0
- 4 vector 0,1,0
- up vector 0,0,-1
- down vector 0,0,1
- ]]
- local deltas = { -- entries are X, Z, Y (note order!!!)
- {-1, 0, 0},
- {0, -1, 0},
- {1, 0, 0},
- {0, 1, 0},
- {0, 0, -1}, -- up
- {0, 0, 1} -- down
- }
- function movefwd()
- local ok, reason = robot.forward()
- if ok then
- dx = dx + deltas[minedir][1]
- dz = dz + deltas[minedir][2]
- dy = dy + deltas[minedir][3]
- else
- io.stderr:write("Can't move forward: " .. reason)
- end
- return ok, reason
- end
- function moveup()
- local ok, reason = robot.up()
- if ok then
- dx = dx + deltas[5][1]
- dz = dz + deltas[5][2]
- dy = dy + deltas[5][3]
- else
- io.stderr:write("Can't move up: " .. reason)
- end
- return ok,reason
- end
- function movedown()
- local ok, reason = robot.down()
- if ok then
- dx = dx + deltas[6][1]
- dz = dz + deltas[6][2]
- dy = dy + deltas[6][3]
- else
- io.stderr:write("Can't move down: " .. reason)
- end
- return ok,reason
- end
- function turnleft()
- robot.turnLeft()
- if minedir == 1 then
- minedir = 4
- else
- minedir = minedir - 1
- end
- end
- function turnright()
- robot.turnRight()
- if minedir == 4 then
- minedir = 1
- else
- minedir = minedir + 1
- end
- end
- function turnaround()
- turnleft()
- turnleft()
- end
- function turnto(tgtdir)
- if tgtdir == minedir then
- return
- elseif tgtdir < minedir then
- do
- local num = minedir - tgtdir
- for i = 1,num do turnleft() end
- end
- else do
- local num = tgtdir - minedir
- for i = 1,num do turnright() end
- end
- end
- end
- function printpos()
- io.stdout:write(dx, " ", dz, " ", dy, " ", minedir, "\n")
- end
- function xmoveto(tgtx)
- local done = false
- local lsavedir = minedir
- if tgtx == dx then
- return
- end
- if tgtx < dx then
- turnto(1)
- else
- turnto(3)
- end
- while not done do
- if tgtx == dx then
- done = true
- else
- movefwd()
- end
- end
- turnto(lsavedir)
- end
- function zmoveto(tgtz)
- local done = false
- local lsavedir = minedir
- if tgtz == dz then
- return
- end
- if tgtz < dz then
- turnto(2)
- else
- turnto(4)
- end
- while not done do
- if tgtz == dz then
- done = true
- else
- movefwd()
- end
- end
- turnto(lsavedir)
- end
- function ymoveto(tgty)
- local done = false
- while not done do
- if dy == tgty then
- done = true
- elseif dy > tgty then
- moveup()
- else
- movedown()
- end
- end -- while
- end
- function rtb() -- return to base
- savedx = dx; savedy = dy; savedz = dz; savedir = minedir
- -- get Y to zero
- ymoveto(0)
- -- go to a cross tunnel
- local tgtx
- if (minedir == 1) or (minedir == 3) then
- if minedir == 1 then -- current direction toward chest
- tgtx = diglen + 8 -- go back to far end of current tunnel
- else -- away from chest
- tgtx = 8 -- go to near end of current tunnel
- end
- turnaround()
- xmoveto(tgtx)
- end -- done moving to end of tunnel
- -- go to the initial tunnel
- zmoveto(0)
- -- go to start pos
- turnto(1)
- xmoveto(0)
- end -- rtb
- function resumedig() -- back to digging after a return to base
- -- go to the side tunnel needed to reach old position
- turnto(3) -- away
- if (savedir == 1) or (savedx == diglen+8) then -- old direction toward, or in far side tunnel
- xmoveto(diglen+8)
- else -- old direction away, or in near side tunnel
- xmoveto(8)
- end
- -- go to the old z location (which mining tunnel or side tunnel depth)
- zmoveto(savedz)
- -- go to the mining location within the tunnel
- turnto(savedir)
- xmoveto(savedx)
- ymoveto(savedy)
- end -- resumedig
- -- mainline code
- diglen = args[1] or 100
- numtun = args[2] or 5
- direct = args[3] or "left"
- if (direct ~= "left") and (direct ~= "right") then
- io.stderr:write("Invalid direction, left assumed")
- direct = "left"
- end
- -- put main call here
- -- 1st test: directions
- --printpos()
- --turnaround()
- --printpos()
- --for i=1,5 do movefwd() end
- --turnleft()
- --for i=1,3 do movefwd() end
- --for i=1,2 do moveup() end
- --printpos()
- --rtb()
- --printpos()
- --resumedig()
- --printpos()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement