Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Hunter
- -- ----------------------------------------------------------
- -- By Jeffrey Alexander, aka Bomb Bloke.
- -- Travels around my base, waving a sword.
- -- ----------------------------------------------------------
- -- Initialise important values:
- -- ----------------------------------------------------------
- local facing = {"north","east","south","west","up","down"}
- local hunting, direction, x, y, z, node
- -- ----------------------------------------------------------
- -- Functions and stuff:
- -- ----------------------------------------------------------
- -- Returns true if the turtle is at the specified node.
- local function atNode(tnode) return (x == node[tnode][1] and y == node[tnode][2] and z == node[tnode][3]) end
- -- Accepts strings representing compass-facings to turn the turtle.
- local function faceDirection(targetdirection)
- local tardir
- for i=1,4 do if targetdirection == facing[i] then
- tardir = i
- break
- end end
- if tardir < direction then
- if tardir == 1 and direction == 4 then while not turtle.turnRight() do end
- else for i=1,direction-tardir do while not turtle.turnLeft() do end end end
- elseif tardir > direction then
- if tardir == 4 and direction == 1 then while not turtle.turnLeft() do end
- else for i=1,tardir-direction do while not turtle.turnRight() do end end end
- end
- direction = tardir
- turtle.attackUp()
- turtle.attack()
- turtle.attackDown()
- end
- -- Travel to a co-ordinate. Returns after every movement.
- local function goToPos(targetx,targety,targetz)
- if turtle.getFuelLevel() == 0 then while turtle.getFuelLevel() < 5000 do
- print("Out of fuel ("..turtle.getFuelLevel().."/5000). Insert consumables to continue...\n")
- os.pullEvent("turtle_inventory")
- for i=1,16 do if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.refuel()
- end end
- end end
- local oldx, oldy, oldz = x, y, z
- if z > targetz then if turtle.down() then z = z - 1 end
- elseif z < targetz then if turtle.up() then z = z + 1 end end
- turtle.attackUp()
- turtle.attack()
- turtle.attackDown()
- if x > targetx then
- if direction ~= 4 then faceDirection("west") end
- if turtle.forward() then x = x - 1 end
- elseif x < targetx then
- if direction ~= 2 then faceDirection("east") end
- if turtle.forward() then x = x + 1 end
- end
- turtle.attackUp()
- turtle.attack()
- turtle.attackDown()
- if y > targety then
- if direction ~= 1 then faceDirection("north") end
- if turtle.forward() then y = y - 1 end
- elseif y < targety then
- if direction ~= 3 then faceDirection("south") end
- if turtle.forward() then y = y + 1 end
- end
- turtle.attackUp()
- turtle.attack()
- turtle.attackDown()
- if oldx == x and oldy == y and oldz == z then return goToPos(targetx+math.random(3)-2,targety+math.random(3)-2,targetz+math.random(2)-1) end
- end
- -- ----------------------------------------------------------
- -- I've just booted up. Where am I? Who am I? etc...
- -- ----------------------------------------------------------
- print("I've been supplied with "..turtle.getFuelLevel().." fuel.")
- do
- while true do
- x, y, z = gps.locate(5)
- if z then break end
- print("GPS cluster not answering; pinging again...")
- sleep(5)
- end
- local moved, goingUp = false, true
- while not moved do
- for i=1,4 do if turtle.forward() then
- moved = true
- break
- else turtle.turnLeft() end end
- if not moved then if goingUp then
- if not turtle.up() then
- goingUp = false
- turtle.down()
- end
- elseif not turtle.down() then
- print("Cannot determine facing: I'm boxed in.")
- print("I'M BOXED IN.")
- print("CAN'T BREATH!")
- print("AAAAUUUUUGGGHHH")
- error("Suffocated")
- end end
- end
- local tempx, tempy, tempz = gps.locate(5)
- if x > tempx then direction = 4
- elseif x < tempx then direction = 2
- elseif y > tempy then direction = 1
- else direction = 3 end
- turtle.back()
- print("I'm at "..x..","..y..","..z.." and I'm facing "..facing[direction]..".")
- end
- if fs.exists("config") then
- local file = fs.open("config","r")
- node = textutils.unserialize(file.readAll())
- file.close()
- else
- node = {{x,y,z}}
- repeat turtle.attack() until turtle.forward()
- repeat turtle.attackUp() until turtle.up()
- repeat turtle.attackUp() until turtle.up()
- x, y, z = gps.locate(5)
- node[2] = {x,y,z}
- repeat turtle.attackDown() until turtle.down()
- repeat turtle.attackDown() until turtle.down()
- repeat until turtle.back()
- x, y, z = unpack(node[1])
- local file = fs.open("config","w")
- file.write(textutils.serialize(node))
- file.close()
- end
- rednet.open("right")
- print("\nHunter turtle ready to engage...\n")
- -- ----------------------------------------------------------
- -- Main program loops:
- -- ----------------------------------------------------------
- local function main()
- while true do
- if hunting then
- print("I'm'a hunt "..hunting.."!\n")
- if z < node[1][3] + 2 then while not atNode(2) do goToPos(node[2][1],node[2][2],node[2][3]) end end
- while hunting do
- if atNode(3) then
- turtle.attackUp()
- turtle.attack()
- turtle.attackDown()
- sleep(2)
- else goToPos(node[3][1],node[3][2],node[3][3]) end
- end
- else
- print("Hunt called off; returning...\n")
- if z > node[1][3] then while not atNode(2) do goToPos(node[2][1],node[2][2],node[2][3]) end end
- while not atNode(1) do goToPos(node[1][1],node[1][2],node[1][3]) end
- rs.setOutput("top",true)
- while not hunting do os.pullEvent("newCommand") end
- rs.setOutput("top",false)
- end
- end
- end
- local function comms()
- local myMessage
- while true do
- myMessage = {rednet.receive()}
- if type(myMessage[2]) == "table" and myMessage[2][1] == "basepatrol" and myMessage[2][2] ~= "BombBloke" and myMessage[2][2] ~= "eliteaccendo" then
- hunting = myMessage[2][2]
- node[3] = myMessage[2][3]
- os.queueEvent("newCommand")
- elseif myMessage[2] == "update" then
- fs.delete("startup")
- shell.run("pastebin get 7pbWTBjw startup")
- os.reboot()
- end
- end
- end
- parallel.waitForAny(main, comms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement