DeliciousJaffa

Turtle attack program

Feb 12th, 2013
6,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. -- Jaffa's attack turtle program
  2. -- Version: 1.0.1.0
  3. local hits = 0
  4. local tries = 0
  5. local movemode = false
  6. local moveallowed = false
  7. local tstop
  8. local ignoredkeys = {["2"]=true, ["5"]=true, ["8"]=true}
  9. local waittext = " - Wait"
  10.  
  11. local args = { ... }
  12. if args[1] == "movemodeon" then movemode = true end
  13. if movemode == true then
  14. local tx, ty = term.getCursorPos()
  15. term.setCursorPos(1 ,ty - 1)
  16. term.clearLine()
  17. print("Jaffa's attack program")
  18. end
  19.  
  20. function cleanInv ()
  21.   for i = 1, 16 do
  22.     if turtle.getItemCount(i) then
  23.       turtle.select(i)
  24.       turtle.drop()
  25.     end
  26.   end
  27.   turtle.select(1)
  28. end
  29.  
  30. function checkInv ()
  31.   cleanInv()
  32.   while not tstop do
  33.     sleep(1)
  34.     if turtle.getItemCount(4) > 0 then
  35.       cleanInv()
  36.     end
  37.   end
  38.   cleanInv()
  39. end
  40.  
  41. function keywait ()
  42.   while not tstop do
  43.     local event,key = os.pullEvent()
  44.       if (event == "key") and (key == 203) then
  45.         if not turtle.turnLeft() then print("Error turning left") end
  46.       elseif (event == "key") and (key == 205) then
  47.         if not turtle.turnRight() then print("Error turning right") end
  48.       elseif (event == "key") and (key == 200) and (moveallowed == true) then
  49.         if not turtle.forward() then print("Error moving forward") end
  50.       elseif (event == "key") and (key == 208) and (moveallowed == true) then
  51.         if not turtle.back() then print("Error moving back") end
  52.       elseif (event == "key") and (key == 76) and (movemode == true) then
  53.         if moveallowed == true then
  54.           moveallowed = false
  55.           print("Move lock enabled")
  56.         else
  57.           moveallowed = true
  58.           print("Move lock disbled")
  59.         end
  60.       elseif (event == "key") and (key == 72) and (moveallowed == true) then
  61.         if not turtle.up() then print("Error moving up") end
  62.       elseif (event == "key") and (key == 80) and (moveallowed == true) then
  63.         if not turtle.down() then print("Error moving down") end
  64. --      elseif (event == "char") and (not ignoredkeys[key]) then
  65. --        tstop = true
  66.       elseif (event == "char") and (key == "s") then
  67.         tstop = true
  68.        write(waittext)
  69.       elseif (event == "char") and (key == "d") then
  70.         cleanInv()
  71.       end
  72.   end
  73. end
  74.  
  75. function attack ()
  76.   while not tstop do
  77.     if turtle.attack() then
  78.       hits = hits + 1
  79.     else
  80.       sleep(0.15)
  81.     end
  82.     tries = tries + 1
  83.    end
  84. end
  85.  
  86. function stats ()
  87.   local tx,ty = term.getCursorPos()
  88.   while not tstop do
  89.     sleep(2)
  90.     local percentage = (hits/tries) * 100
  91.     term.setCursorPos(1,ty)
  92.     term.clearLine()
  93.     term.setCursorPos(1,ty)
  94.     write("Hits: ")
  95.     write(hits)
  96.     write("/")
  97.     write(tries)
  98.     write(" (")
  99.     write(percentage)
  100.     write("%)")
  101.   end
  102.   write(waittext)
  103. end
  104.  
  105. turtle.select(1)
  106. print("Press S to stop, D to drop")
  107. parallel.waitForAll(keywait,attack,stats,checkInv)
  108. local mx,my = term.getCursorPos()
  109. term.setCursorPos((mx - string.len(waittext)),my)
  110. print(" \\e/   ")
Advertisement
Add Comment
Please, Sign In to add comment