Advertisement
Guest User

MW3-like Controllable Turret

a guest
Feb 23rd, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.18 KB | None | 0 0
  1. -------------------------
  2. --- GENERAL FUNCTIONS ---
  3. -------------------------
  4.  
  5. function contains(table, element)
  6.     for _, value in ipairs(table) do
  7.         if value == element then
  8.             return true
  9.         end
  10.     end
  11.     return false
  12. end
  13.  
  14. function table.remove(table, element)
  15.     if #table > 1 then
  16.         for key, value in ipairs(table) do
  17.             if value == element then
  18.                 table[key] = nil
  19.             end
  20.         end
  21.     else
  22.         table = {}
  23.     end
  24. end
  25.  
  26. function table.insert2(table, element)
  27.     if #table > 0 then
  28.         for key, value in ipairs(table) do
  29.             if value == nil then
  30.                 table[key] = element
  31.             end
  32.         end
  33.     else
  34.         table[1]=element
  35.     end
  36. end
  37.  
  38. function angledir(rot,x,y,factor)
  39.     if rot<-90 then rot=rot+360 end
  40.     local angle=math.rad(math.abs(rot+90))-math.pi
  41.     local cx=x+(math.cos(angle)*factor)
  42.     local cy=y+(math.sin(angle)*factor)
  43.     return cx,cy
  44. end
  45.  
  46. function distance(x1,y1,x2,y2)
  47.     local x=x1-x2
  48.     local y=y1-y2
  49.     return math.sqrt(x^2+y^2)
  50. end
  51.  
  52. ------------------------------
  53. ---- SCRIPT-RELATED STUFF ----
  54. ------------------------------
  55.  
  56. ------ VARIABLES AND TABLES ------
  57.  
  58. turret={}
  59. plydata={}
  60.  
  61. plydata.cimg = "gfx/sprites/block.bmp" -- image of the player controlling the turret; modify this variable to change
  62.  
  63. turret.hp = 250 -- health of any given turret; modify this variable to change
  64. turret.bimg = "gfx/sprites/block.bmp" -- image of any given turret base; modify this variable to change
  65. turret.gimg = "gfx/weapons/turret.bmp" -- image of any given turret gun; modify this variable to change
  66. turret.aux = {} -- auxiliary functions table (e.g. say functions, switch functions etc.)
  67. turret.controlling = {} -- table of players that are controlling turrets
  68.  
  69. ------ TURRET-RELATED FUNCTIONS ------
  70.  
  71. function turret.new(ply,x,y) -- create new turret; x y are tiles!
  72.     table.insert2(turret,{
  73.         owner = ply,
  74.         pos = {x,y},
  75.         health = turret.hp,
  76.         bimg = image(turret.bimg,x*32+16,y*32+16,3),
  77.         gimg = image(turret.gimg,x*32+16,y*32+16,3),
  78.     })
  79.     local id = #turret
  80.     if not plydata[ply] then plydata[ply]={} end
  81.     plydata[ply].turret = id
  82.     return id
  83. end
  84.  
  85. function turret.kill(id) -- destroy a turret
  86.     local ply = turret[id].owner
  87.     if (turret[id].controlled) then
  88.         turret.scontrol(ply)
  89.     end
  90.     parse("explosion "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 128 20 "..ply)
  91.     freeimage(turret[id].bimg)
  92.     freeimage(turret[id].gimg)
  93.     turret[id] = nil
  94.     return
  95. end
  96.  
  97. function turret.hit(id,dmg) -- hit a turret
  98.     if (turret[id].health > dmg) then
  99.         turret[id].health = turret[id].health - dmg
  100.         parse("effect \"flare\" "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 3 8 255 255 0")
  101.     else
  102.         turret.kill(id)
  103.         return
  104.     end
  105. end
  106.  
  107. ------ PLAYER-RELATED FUNCTIONS ------
  108.  
  109. function turret.control(ply) -- assume control of a turret
  110.     plydata[ply].controlling = true
  111.     local id = plydata[ply].turret
  112.     turret[id].controlled = true
  113.     table.insert2(turret.controlling,ply)
  114.     plydata[ply].cimg = image(plydata.cimg,player(ply,"x"),player(ply,"y"),0)
  115.     imagepos(plydata[ply].cimg,player(ply,"x"),player(ply,"y"),player(ply,"rot"))
  116.     plydata[ply].armor = player(ply,"armor") -- old armor to revert to after destruction or control stop
  117.     plydata[ply].pos = {player(ply,"x"),player(ply,"y")} -- old position to teleport to
  118.     plydata[ply].speed = player(ply,"speedmod") -- old speedmod to revert to
  119.     parse("setarmor "..ply.." 206")
  120.     parse("setpos "..ply.." "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16))
  121.     if not contains(playerweapons(ply),40) then -- if a player doesn't already have a machine gun then it's equipped, leave it otherwise
  122.         parse("equip "..id.." 40")
  123.         plydata[ply].meq=true
  124.     else
  125.         plydata[ply].meq=false
  126.     end
  127.     parse("speedmod "..ply.." -100")
  128.     parse("setweapon "..ply.." 40")
  129. end
  130.  
  131. function turret.scontrol(ply) -- cease control of a turret
  132.     plydata[ply].controlling = false
  133.     local id = plydata[ply].turret
  134.     turret[id].controlled = false
  135.     parse("setarmor "..ply.." "..plydata[ply].armor)
  136.     if (plydata[ply].meq) then
  137.         parse("strip "..ply.." 40")
  138.     end
  139.     parse("setpos "..ply.." "..plydata[ply].pos[1].." "..plydata[ply].pos[2])
  140.     parse("speedmod "..ply.." "..plydata[ply].speed)
  141.     freeimage(plydata[ply].cimg)
  142.     table.remove(turret.controlling,ply)
  143.     plydata[ply].cimg = nil
  144.     plydata[ply].armor = nil
  145.     plydata[ply].pos = nil
  146.     plydata[ply].speed = nil
  147. end
  148.  
  149. function turret.acquire(ply)
  150.     if not plydata[ply] then plydata[ply]={} end
  151.     plydata[ply].tpacked = true -- player has turret packed
  152.     msg2(ply,"You have acquired a remote controlled turret.")
  153. end
  154.  
  155. ------ AUXILIARY FUNCTIONS ------
  156.  
  157. function turret.aux.update()
  158.     for id=1,#turret do
  159.         if (turret[id].controlled) then
  160.             imagepos(turret[id].gimg,player(turret[id].owner,"x"),player(turret[id].owner,"y"),player(turret[id].owner,"rot"))
  161.         end
  162.     end
  163. end
  164.  
  165. function turret.aux.ms100()
  166.     for id=1,#turret do
  167.         if (turret[id].health<=turret.hp/2) then
  168.             parse("effect \"smoke\" "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 3 4 255 255 0")
  169.             if (turret[id].health<=turret.hp/4) then
  170.                 parse("effect \"fire\" "..(turret[id].pos[1]*32+16).." "..(turret[id].pos[2]*32+16).." 3 4 255 255 0")
  171.             end
  172.         end
  173.     end
  174. end
  175.  
  176. function turret.aux.say(ply,t)
  177.     if (t:sub(1,8) == "!control") then
  178.         if (plydata[ply].turret) then
  179.             turret.control(ply)
  180.         end
  181.         return 1
  182.     end
  183.     if (t:sub(1,5) == "!stop") then
  184.         if (plydata[ply].controlling) then
  185.             turret.scontrol(ply)
  186.         end
  187.         return 1
  188.     end
  189.     if (t:sub(1,7) == "!deploy") then
  190.         if (plydata[ply].tpacked) then
  191.             turret.new(ply,player(ply,"tilex"),player(ply,"tiley"))
  192.         end
  193.         return 1
  194.     end
  195. end
  196.  
  197. function turret.aux.check(x1,y1,x2,y2) -- check path between two points (tiles)
  198.     for x=math.max(x1,x2),math.min(x1,x2) do
  199.         for y=math.max(y1,y2),math.min(y1,y2) do
  200.             if not (tile(x,y,"walkable")) then
  201.                 return false
  202.             end
  203.         end
  204.     end
  205.     return true
  206. end
  207.  
  208. function turret.aux.hit(ply,plys,_,hpdmg)
  209.     if (plydata[ply] and plydata[ply].controlling) then
  210.         turret.hit(plydata[ply].turret,hpdmg)
  211.         return 1
  212.     end
  213.     if (plydata[plys] and plydata[plys].controlling and player(ply,"health")<=hpdmg) then
  214.         parse("customkill "..plys.." \"RC Turret\" "..ply)
  215.         return 1
  216.     end
  217. end
  218.  
  219. function turret.aux.shoot(ply)
  220.     for i=1,#turret.controlling do
  221.         local ply2=turret.controlling[i]
  222.         if (turret.aux.check(player(ply,"tilex"),player(ply,"tiley"),player(ply2,"tilex"),player(ply2,"tiley"))) then
  223.             for i=1,640 do
  224.                 local x,y=angledir(player(ply,"rot"),player(ply,"x"),player(ply,"y"),i)
  225.                 if (plydata[ply2].pos and distance(plydata[ply2].pos[1],plydata[ply2].pos[2],x,y)<=16) then
  226.                     msg(x)
  227.                     msg(y)
  228.                     msg(plydata[ply2].pos[1])
  229.                     msg(plydata[ply2].pos[2])
  230.                     if (player(ply2,"health")>itemtype(player(ply,"weapon"),"dmg")) then
  231.                         parse("sethealth "..ply2.." "..player(ply2,"health")-itemtype(player(ply,"weapon"),"dmg"))
  232.                     else
  233.                         parse("customkill "..ply.." "..itemtype(player(ply,"weapon"),"name").." "..ply2)
  234.                     end
  235.                     break
  236.                 end
  237.             end
  238.         end
  239.     end
  240.     for id=1,#turret do
  241.         if (not turret[id].controlled and turret.aux.check(player(ply,"tilex"),player(ply,"tiley"),turret[id].pos[1],turret[id].pos[2])) then
  242.             for i=32,640 do
  243.                 local x,y=angledir(player(ply,"rot"),player(ply,"x"),player(ply,"y"),i)
  244.                 if (distance(turret[id].pos[1]*32+16,turret[id].pos[2]*32+16,x,y)<=16) then
  245.                     turret.hit(id,itemtype(player(ply,"weapon"),"dmg"))
  246.                     break
  247.                 end
  248.             end
  249.         end
  250.     end
  251. end
  252.  
  253. function turret.aux.select(ply,type)
  254.     if (plydata[ply].controlling) then
  255.         if (type ~= 40) then
  256.             parse("setweapon "..ply.." 40")
  257.         end
  258.     end
  259. end
  260.  
  261. function turret.aux.die(ply)
  262.     if (plydata[ply] and plydata[ply].controlling) then
  263.         plydata[ply].spos = plydata[ply].pos
  264.         turret.scontrol(ply)
  265.     end
  266. end
  267.  
  268. function turret.aux.spawn(ply)
  269.     if (plydata[ply] and plydata[ply].spos) then
  270.         parse("setpos "..ply.." "..plydata[ply].spos[1].." "..plydata[ply].spos[2])
  271.         plydata[ply].spos = nil
  272.     end
  273. end
  274.  
  275. ------ HOOKS ------
  276.  
  277. addhook("always","turret.aux.update")
  278. addhook("ms100","turret.aux.ms100")
  279. addhook("attack","turret.aux.shoot")
  280. addhook("hit","turret.aux.hit")
  281. addhook("say","turret.aux.say")
  282. addhook("select","turret.aux.select")
  283. addhook("die","turret.aux.die")
  284. addhook("spawn","turret.aux.spawn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement