CometWolf

Attacker turtle

Apr 2nd, 2013
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. -- Player ignore table
  2. tPlayers = {"Lil122j", "CometWolf"}
  3. -- Mob ignore table
  4. tMobs = {}
  5. -- range settings
  6. -- program start
  7. os.loadAPI"cTurtle"
  8. os.pullEvent = os.pullEventRaw
  9. os.loadAPI"ocs/apis/sensor"
  10. cTurtle.locate()
  11. -- locates sensors
  12. local tSides = rs.getSides()
  13. local tSensors = {}
  14. for i=1,#tSides do
  15.   if peripheral.getType(tSides[i]) == "sensor" then
  16.     tSensors[#tSensors+1] = sensor.wrap(tSides[i])
  17.   end
  18. end
  19. -- no sensors found
  20. if not tSensors[1] then
  21.   error"ERROR: No sensors found!"
  22. elseif tSensors[2] then
  23.   error"ERROR: Multiple sensors found!"
  24. end
  25. -- sensor loop
  26. function detect()
  27.   while true do
  28.     local tEntities = {}
  29.     local tDetails = {}
  30.     -- entity detection
  31.     for name,table in pairs(tSensors[1].getTargets()) do
  32.       tDetails[#tEntities+1] = tSensors[1].getTargetDetails(name)
  33.       tEntities[#tEntities+1] = name
  34.     end
  35.     -- ignore check
  36.     for i=1,#tEntities do
  37.       if not tDetails[i] then
  38.         break
  39.       end
  40.       local ignoreMob = false
  41.       local ignorePlayer = false
  42.       local ignore = false
  43.       local entity = ""
  44.       -- players
  45.       if tDetails[i]["Name"] == "Player" then
  46.         entity = "player"
  47.         for t=1,#tPlayers do
  48.           if tDetails[i]["Username"] == tPlayers[t] then
  49.             ignorePlayer = true
  50.             break
  51.           end
  52.         end
  53.       -- mobs
  54.       else
  55.         for t=1,#tMobs do
  56.           entity = "mob"
  57.           if tDetails[i]["Name"] == tMobs[t] then
  58.             ignoreMob = true
  59.             break
  60.           end
  61.         end
  62.       end
  63.       if not ignorePlayer and entity == "player" then
  64.       -- un-ignored player detected
  65.         -- action
  66.         distX = tDetails[i].Position.X
  67.         distZ = tDetails[i].Position.Z
  68.         distY = tDetails[i].Position.Y
  69.         term.clear()
  70.         term.setCursorPos(1,1)
  71.         print("X: "..distX)
  72.         print("Z: "..distZ)
  73.         print("Y: "..distY)
  74.         return
  75.       elseif not ignoreMob and entity == "mob" then
  76.       -- un-ignored mob detected
  77.       else
  78.       -- any entity detected
  79.       end
  80.     end
  81.     sleep(0.1)
  82.   end
  83. end
  84.  
  85. function action()
  86. -- dist checks
  87.   local tDist = {}
  88. -- X dist
  89.   if distX < -1.2 then
  90.     cTurtle.move"X-"
  91.     return
  92.   elseif distX > 1.2 then
  93.     cTurtle.move"X+"
  94.     return
  95.   else
  96.     tDist["X"] = true
  97.   end
  98. -- Z dist
  99.   if distZ < -1.2 then
  100.     cTurtle.move"Z-"
  101.     return
  102.   elseif distZ > 1.2 then
  103.     cTurtle.move"Z+"
  104.     return
  105.   else
  106.     tDist["Z"] = true
  107.   end
  108. -- Y dist  
  109.   if distY < -2 then
  110.     cTurtle.move"down"
  111.     return
  112.   elseif distY > 0 then
  113.     cTurtle.move"up"
  114.     return
  115.   else
  116.     tDist["Y"] = true
  117.   end
  118. -- dist ok
  119.   if tDist["X"] and tDist["Z"] and tDist["Y"] then
  120.     targetDir = math.max(math.abs(distX),math.abs(distZ))
  121.     if targetDir == math.abs(distX) then
  122.       if distX > 0 then
  123.         cTurtle.turn"X+"
  124.         if not turtle.attack() then
  125.           cTurtle.move"forward"
  126.         end
  127.       else
  128.         cTurtle.turn"X-"
  129.         if not turtle.attack() then
  130.           cTurtle.move"forward"
  131.         end
  132.       end
  133.     end
  134.     if targetDir == math.abs(distZ) then
  135.       if distZ > 0 then
  136.         cTurtle.turn"Z+"
  137.         if not turtle.attack() then
  138.           cTurtle.move"forward"
  139.         end
  140.       else
  141.         cTurtle.turn"Z-"
  142.         if not turtle.attack() then
  143.           cTurtle.move"forward"
  144.         end
  145.       end
  146.     end
  147.   end
  148. end
  149.  
  150. while true do
  151.   detect()
  152.   action()
  153. end
Advertisement
Add Comment
Please, Sign In to add comment