Advertisement
qwertyMAN_rus

turret

Jan 31st, 2016 (edited)
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. local com           = require("component")
  2. local gpu           = com.gpu
  3. local turret        = com.os_energyturret
  4. local radar         = com.openperipheral_sensor
  5. local autors        = {"qwertyMAN"}
  6. local version       = "0.9"
  7. local Black_List    = {}
  8. local White_List    = {}
  9. -- относительные координаты пушки от сканера
  10. local correct = {
  11.     x = 0,
  12.     y = 4,
  13.     z = 0
  14. }
  15.  
  16. local function kostil(x)
  17.     while true do
  18.         if x>=360 then
  19.             x = x - 360
  20.         elseif x<0 then
  21.             x = x + 360
  22.         else
  23.             break
  24.         end
  25.     end
  26.     return x
  27. end
  28.  
  29. local function pointer(x,y,z)
  30.     local distXY = math.sqrt(x^2+z^2)
  31.     local distDY = math.sqrt(y^2+distXY^2)
  32.     local outX = math.deg(math.acos(x/distXY))+90
  33.     local outY = 90-math.deg(math.acos(y/distDY))
  34.     if z<0 then
  35.         outX = kostil(180-outX)
  36.     end
  37.     return outX,outY
  38. end    
  39.                
  40. while true do
  41.     os.sleep(0)
  42.     local target = false
  43.     local scan=radar.getPlayers()
  44.     if #scan>0 then
  45.         if #White_List>0 then
  46.             for i=1, #autors do
  47.                 White_List[#White_List+1] = autors[i]
  48.             end
  49.             for i=1, #scan do
  50.                 local swich = true
  51.                 for j=1, #White_List do
  52.                     if scan[i].name==White_List[j] then
  53.                         swich = false
  54.                     end
  55.                 end
  56.                 if swich then
  57.                     target = scan[i].name
  58.                     break
  59.                 end
  60.             end
  61.         elseif #Black_List>0 then
  62.             for i=#Black_List, 1, -1 do
  63.                 for j=1, #autors do
  64.                     if Black_List[i] == autors[j] then
  65.                         table.remove(Black_List,i)
  66.                     end
  67.                 end
  68.             end
  69.             for i=1, #scan do
  70.                 local swich = false
  71.                 for j=1, #Black_List do
  72.                     if scan[i].name==Black_List[j] then
  73.                         swich = true
  74.                     end
  75.                 end
  76.                 if swich then
  77.                     target = scan[i].name
  78.                     break
  79.                 end
  80.             end
  81.         else
  82.             if #autors>0 then
  83.                 for i=1, #autors do
  84.                     White_List[#White_List+1] = autors[i]
  85.                 end
  86.             else
  87.                 target = scan[1].name
  88.             end
  89.         end
  90.         if target then
  91.             if radar.getPlayerByName(target) then
  92.                 target=radar.getPlayerByName(target).all()
  93.                 local x,y,z = target.position.x-0.5-correct.x, target.position.y+0.3-correct.y, target.position.z-0.5-correct.z
  94.                 local vx,vy = pointer(x,y,z)
  95.                 turret.moveTo(vx,vy)
  96.                 if turret.isOnTarget() then
  97.                     turret.fire()
  98.                 end
  99.             end
  100.         end
  101.     end
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement