Advertisement
Guest User

Untitled

a guest
Oct 5th, 2010
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1.  --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3.  
  4. function gadget:GetInfo()
  5.    return {
  6.       name      = "Laser Designator",
  7.       desc      = "",
  8.       author    = "quantum",
  9.       date      = "May 2010",
  10.       license   = "GNU GPL, v2 or later",
  11.       layer     = 5,
  12.       enabled   = true
  13.    }
  14. end
  15.  
  16. --------------------------------------------------------------------------------
  17. --------------------------------------------------------------------------------
  18.  
  19. if gadgetHandler:IsSyncedCode() then
  20.  
  21.  
  22.  
  23. --------------------------------------------------------------------------------
  24. --------------------------------------------------------------------------------
  25.  
  26. local Spring, pairs, ipairs = Spring, pairs, ipairs
  27.  
  28. --------------------------------------------------------------------------------
  29. --------------------------------------------------------------------------------
  30.  
  31. local damageMult = 1
  32. local duration = 0.5 * 30 -- in frames
  33.  
  34.  
  35. GG.targets = {}
  36. local targets = GG.targets
  37. local designatorWeaponIDs = {}
  38.  
  39. for i = 1, #WeaponDefs do
  40.   if WeaponDefs[i].customParams.designator then
  41.     designatorWeaponIDs[WeaponDefs[i].id] = true
  42.   end
  43. end
  44.  
  45. --[[
  46. local targetingLinks = {}
  47. local targetDesignators = {}
  48. --]]
  49.  
  50. --------------------------------------------------------------------------------
  51. --------------------------------------------------------------------------------
  52.  
  53. local function raiseLaserEvent(eventArgs)
  54.   _G.laserEventArgs = eventArgs
  55.   SendToUnsynced("LaserEvent")
  56.   _G.laserEventArgs = nil
  57. end
  58.  
  59.  
  60. function gadget:UnitPreDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)
  61.   if weaponID and designatorWeaponIDs[weaponID] and GG.creeps[unitID] then -- hit by a laser designator
  62.     if not targets[unitID] then
  63.       targets[unitID] = {expiration = Spring.GetGameFrame() + duration}
  64.       raiseLaserEvent{addTarget = true, unitID = unitID}
  65.       for turretID in pairs(GG.turrets) do -- make every turret attack the designated unit
  66.         Spring.SetUnitTarget(turretID, unitID)
  67.       end
  68.     end
  69.   end
  70.   return damage
  71. end
  72.  
  73.  
  74. function gadget:GameFrame(frame)
  75.   if frame % 23 == 0 then
  76.     for unitID, unitInfo in pairs(targets) do
  77.       if unitInfo.expiration < frame then
  78.         targets[unitID] = nil
  79.         raiseLaserEvent{removeTarget = true, unitID = unitID}
  80.       end
  81.     end
  82.   end
  83. end
  84.  
  85.  
  86. function gadget:UnitDestroyed(unitID)
  87.   if targets[unitID] then
  88.     targets[unitID] = nil
  89.     raiseLaserEvent{removeTarget = true, unitID = unitID}
  90.   end
  91. end
  92.  
  93. --------------------------------------------------------------------------------
  94. --------------------------------------------------------------------------------
  95. else
  96. --------------------------------------------------------------------------------
  97. --------------------------------------------------------------------------------
  98. --- Unsynced
  99. --------------------------------------------------------------------------------
  100.  
  101.  
  102. local function drawTarget()
  103.   gl.Color(1, 1, 1, 1)
  104.   gl.PushMatrix()
  105.   gl.Billboard()
  106.   gl.Texture("bitmaps/gpl/point2.png")
  107.   gl.TexRect(-8, -8, 8, 8)
  108.   gl.PopMatrix()
  109. end
  110.  
  111. function gadget:Initialize()
  112.   gadgetHandler:AddSyncAction("LaserEvent", HandleLaserEvent)
  113. end
  114.  
  115. local targets = {}
  116.  
  117. function HandleLaserEvent()
  118.   local eventArgs = {}
  119.   for k, v in spairs(SYNCED.laserEventArgs) do
  120.     eventArgs[k] = v
  121.   end
  122.   if eventArgs.addTarget then
  123.     targets[eventArgs.unitID] = true
  124.   elseif eventArgs.removeTarget then
  125.     targets[eventArgs.unitID] = nil
  126.   end
  127. end
  128.  
  129.  
  130. function gadget:DrawWorld()
  131.   for unitID in pairs(targets) do
  132.     gl.DrawFuncAtUnit(unitID, false, drawTarget)
  133.   end
  134. end
  135.  
  136.  
  137.  
  138. --------------------------------------------------------------------------------
  139. --------------------------------------------------------------------------------
  140.  
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement