Advertisement
BombBloke

Hunter (ComputerCraft)

Sep 12th, 2014
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. -- Hunter
  2. -- ----------------------------------------------------------
  3.  
  4. -- By Jeffrey Alexander, aka Bomb Bloke.
  5. -- Travels around my base, waving a sword.
  6.  
  7. -- ----------------------------------------------------------
  8.  
  9. -- Initialise important values:
  10.  
  11. -- ----------------------------------------------------------
  12.  
  13. local facing = {"north","east","south","west","up","down"}
  14. local hunting, direction, x, y, z, node
  15.  
  16. -- ----------------------------------------------------------
  17.  
  18. -- Functions and stuff:
  19.  
  20. -- ----------------------------------------------------------
  21.  
  22. -- Returns true if the turtle is at the specified node.
  23. local function atNode(tnode) return (x == node[tnode][1] and y == node[tnode][2] and z == node[tnode][3]) end
  24.  
  25. -- Accepts strings representing compass-facings to turn the turtle.
  26. local function faceDirection(targetdirection)
  27.     local tardir
  28.     for i=1,4 do if targetdirection == facing[i] then
  29.         tardir = i
  30.         break
  31.     end end
  32.    
  33.     if tardir < direction then
  34.         if tardir == 1 and direction == 4 then while not turtle.turnRight() do end
  35.         else for i=1,direction-tardir do while not turtle.turnLeft() do end end end
  36.     elseif tardir > direction then
  37.         if tardir == 4 and direction == 1 then while not turtle.turnLeft() do end
  38.         else for i=1,tardir-direction do while not turtle.turnRight() do end end end
  39.     end
  40.    
  41.     direction = tardir
  42.    
  43.     turtle.attackUp()
  44.     turtle.attack()
  45.     turtle.attackDown()
  46. end
  47.  
  48. -- Travel to a co-ordinate. Returns after every movement.
  49. local function goToPos(targetx,targety,targetz)
  50.     if turtle.getFuelLevel() == 0 then while turtle.getFuelLevel() < 5000 do
  51.         print("Out of fuel ("..turtle.getFuelLevel().."/5000). Insert consumables to continue...\n")
  52.        
  53.         os.pullEvent("turtle_inventory")
  54.        
  55.         for i=1,16 do if turtle.getItemCount(i) > 0 then
  56.             turtle.select(i)
  57.             turtle.refuel()
  58.         end end
  59.     end end
  60.    
  61.     local oldx, oldy, oldz = x, y, z
  62.    
  63.     if z > targetz then if turtle.down() then z = z - 1 end
  64.     elseif z < targetz then if turtle.up() then z = z + 1 end end
  65.  
  66.     turtle.attackUp()
  67.     turtle.attack()
  68.     turtle.attackDown()
  69.  
  70.     if x > targetx then
  71.         if direction ~= 4 then faceDirection("west") end
  72.         if turtle.forward() then x = x - 1 end
  73.     elseif x < targetx then
  74.         if direction ~= 2 then faceDirection("east") end
  75.         if turtle.forward() then x = x + 1 end
  76.     end
  77.  
  78.     turtle.attackUp()
  79.     turtle.attack()
  80.     turtle.attackDown()
  81.  
  82.     if y > targety then
  83.         if direction ~= 1 then faceDirection("north") end
  84.         if turtle.forward() then y = y - 1 end
  85.     elseif y < targety then
  86.         if direction ~= 3 then faceDirection("south") end
  87.         if turtle.forward() then y = y + 1 end
  88.     end
  89.  
  90.     turtle.attackUp()
  91.     turtle.attack()
  92.     turtle.attackDown()
  93.    
  94.     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
  95. end
  96.  
  97. -- ----------------------------------------------------------
  98.  
  99. -- I've just booted up. Where am I? Who am I? etc...
  100.  
  101. -- ----------------------------------------------------------
  102.  
  103. print("I've been supplied with "..turtle.getFuelLevel().." fuel.")
  104.  
  105. do
  106.     while true do
  107.         x, y, z = gps.locate(5)
  108.        
  109.         if z then break end
  110.        
  111.         print("GPS cluster not answering; pinging again...")
  112.         sleep(5)
  113.     end
  114.  
  115.     local moved, goingUp = false, true
  116.  
  117.     while not moved do
  118.         for i=1,4 do if turtle.forward() then
  119.             moved = true
  120.             break
  121.         else turtle.turnLeft() end end
  122.  
  123.         if not moved then if goingUp then
  124.             if not turtle.up() then
  125.                 goingUp = false
  126.                 turtle.down()
  127.             end
  128.         elseif not turtle.down() then
  129.             print("Cannot determine facing: I'm boxed in.")
  130.             print("I'M BOXED IN.")
  131.             print("CAN'T BREATH!")
  132.             print("AAAAUUUUUGGGHHH")
  133.             error("Suffocated")
  134.         end end
  135.     end
  136.  
  137.     local tempx, tempy, tempz = gps.locate(5)
  138.  
  139.     if x > tempx then direction = 4
  140.     elseif x < tempx then direction = 2
  141.     elseif y > tempy then direction = 1
  142.     else direction = 3 end
  143.    
  144.     turtle.back()
  145.  
  146.     print("I'm at "..x..","..y..","..z.." and I'm facing "..facing[direction]..".")
  147. end
  148.  
  149. if fs.exists("config") then
  150.     local file = fs.open("config","r")
  151.     node = textutils.unserialize(file.readAll())
  152.     file.close()
  153. else
  154.     node = {{x,y,z}}
  155.    
  156.     repeat turtle.attack() until turtle.forward()
  157.     repeat turtle.attackUp() until turtle.up()
  158.     repeat turtle.attackUp() until turtle.up()
  159.    
  160.     x, y, z = gps.locate(5)
  161.     node[2] = {x,y,z}
  162.    
  163.     repeat turtle.attackDown() until turtle.down()
  164.     repeat turtle.attackDown() until turtle.down()
  165.     repeat until turtle.back()
  166.    
  167.     x, y, z = unpack(node[1])
  168.    
  169.     local file = fs.open("config","w")
  170.     file.write(textutils.serialize(node))
  171.     file.close()
  172. end
  173.  
  174. rednet.open("right")
  175.  
  176. print("\nHunter turtle ready to engage...\n")
  177.  
  178. -- ----------------------------------------------------------
  179.  
  180. -- Main program loops:
  181.  
  182. -- ----------------------------------------------------------
  183.  
  184. local function main()
  185.     while true do
  186.         if hunting then
  187.             print("I'm'a hunt "..hunting.."!\n")
  188.             if z < node[1][3] + 2 then while not atNode(2) do goToPos(node[2][1],node[2][2],node[2][3]) end end    
  189.            
  190.             while hunting do
  191.                 if atNode(3) then
  192.                     turtle.attackUp()
  193.                     turtle.attack()
  194.                     turtle.attackDown()
  195.                     sleep(2)
  196.                 else goToPos(node[3][1],node[3][2],node[3][3]) end
  197.             end
  198.         else
  199.             print("Hunt called off; returning...\n")
  200.             if z > node[1][3] then while not atNode(2) do goToPos(node[2][1],node[2][2],node[2][3]) end end
  201.            
  202.             while not atNode(1) do goToPos(node[1][1],node[1][2],node[1][3]) end
  203.            
  204.             rs.setOutput("top",true)
  205.             while not hunting do os.pullEvent("newCommand") end
  206.             rs.setOutput("top",false)
  207.         end
  208.     end    
  209. end
  210.  
  211. local function comms()
  212.     local myMessage
  213.    
  214.     while true do
  215.         myMessage = {rednet.receive()}
  216.        
  217.         if type(myMessage[2]) == "table" and myMessage[2][1] == "basepatrol" and myMessage[2][2] ~= "BombBloke" and myMessage[2][2] ~= "eliteaccendo" then
  218.             hunting = myMessage[2][2]
  219.             node[3] = myMessage[2][3]
  220.            
  221.             os.queueEvent("newCommand")
  222.         elseif myMessage[2] == "update" then
  223.             fs.delete("startup")
  224.             shell.run("pastebin get 7pbWTBjw startup")
  225.             os.reboot()
  226.         end
  227.     end
  228. end
  229.  
  230. parallel.waitForAny(main, comms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement