Advertisement
LJLim

Bombs gadget

Apr 14th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.77 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3. function gadget:GetInfo()
  4.     return {
  5.         name = "Bombs (adapted from KotH)",
  6.         desc = "*BOOOOOM* Terrorists win!",
  7.         author = "Alchemist, Licho, KingRaptor",
  8.         date = "April 2009",
  9.         license = "Public domain",
  10.         layer = 1,
  11.         enabled = true
  12.     }
  13. end
  14. --------------------------------------------------------------------------------
  15. --------------------------------------------------------------------------------
  16. if(gadgetHandler:IsSyncedCode()) then
  17. --------------------------------------------------------------------------------
  18. --SYNCED
  19. --------------------------------------------------------------------------------
  20. local spGetUnitsInCylinder  = Spring.GetUnitsInCylinder
  21. local spGetUnitTeam     = Spring.GetUnitTeam
  22. local spGetUnitAllyTeam     = Spring.GetUnitAllyTeam
  23. local spGetUnitDefID        = Spring.GetUnitDefID
  24. --------------------------------------------------------------------------------
  25. --------------------------------------------------------------------------------
  26. local blockedDefs = {
  27.     [ UnitDefNames['terraunit'].id ] = true,
  28.     [ UnitDefNames['wolverine_mine'].id ] = true,
  29.     [ UnitDefNames['pw_dropfac'].id ] = true,
  30.     [ UnitDefNames['pw_dropdepot'].id ] = true,
  31.     [ UnitDefNames['fakeunit_los'].id ] = true,
  32. }
  33.  
  34. local points = {
  35.     {pos = {568, 915, 2831}, radius = 250, controlStrength = 0},
  36.     {pos = {1224, 915, 3672}, radius = 250, controlStrength = 0},
  37.     {pos = {536, 878, 5432}, radius = 300, controlStrength = 0},
  38. }
  39.  
  40. local teams = {
  41.     [0] = 0,
  42.     [3] = 0,
  43. }
  44. local allyTeams = {
  45.     [0] = 0,
  46. }
  47.  
  48. local captures = 0
  49. local triggers = {"Bomb Defused 1", "Bomb Defused 2", "Bomb Defused 3"}
  50.  
  51. local UPDATE_PERIOD = 15
  52.  
  53. local captureSpeed = 0.05*(UPDATE_PERIOD/30)    -- 20 seconds to fully cap
  54. local decaptureSpeed = 0    --0.1*(UPDATE_PERIOD/30)    -- 10 seconds to fully uncap
  55.  
  56. local grace = 0
  57. local graceLength = 0
  58.  
  59. _G.points = points
  60. --------------------------------------------------------------------------------
  61. --------------------------------------------------------------------------------
  62. local function GetSetCount(set)
  63.   local count = 0
  64.   for _ in pairs(set) do
  65.     count = count + 1
  66.   end
  67.   return count
  68. end
  69.  
  70. local function ProcessPoint(index)
  71.     local point = points[index]
  72.     local soleAllyTeam  -- may not actually be the sole ally team
  73.     local contested = false
  74.     local present = {}
  75.    
  76.     local units = spGetUnitsInCylinder(point.pos[1], point.pos[3], point.radius)
  77.     for i=1,#units do
  78.         local unitID = units[i]
  79.         local team = spGetUnitTeam(unitID)
  80.         local allyTeam = spGetUnitAllyTeam(unitID)
  81.         local unitDefID = spGetUnitDefID(unitID)
  82.         if teams[team] and not blockedDefs[unitDefID] then
  83.             present[allyTeam] = true
  84.             soleAllyTeam = allyTeam
  85.         end
  86.     end
  87.    
  88.     -- possible situations:
  89.     -- neutral, uncontested,
  90.     -- neutral, contested,
  91.     -- neutral, being captured
  92.     -- captured, uncontested
  93.     -- captured, contested
  94.     -- captured, being decaptured
  95.    
  96.     local numAllyTeams = GetSetCount(present)
  97.     local controlStrength = point.controlStrength
  98.     local capturingAllyTeam = point.capturingAllyTeam
  99.    
  100.     if not point.controllingAllyTeam then   -- neutral
  101.         if numAllyTeams == 0 and capturingAllyTeam then -- unoccupied
  102.             -- bleed current progress
  103.             --[[
  104.             controlStrength = controlStrength - captureSpeed
  105.             if controlStrength <= 0 then
  106.                 capturingAllyTeam = nil
  107.                 controlStrength = 0
  108.             end
  109.             ]]
  110.         elseif numAllyTeams == 1 then   -- occupied by one team
  111.             if soleAllyTeam ~= capturingAllyTeam then   -- decapture
  112.                 controlStrength = controlStrength - decaptureSpeed
  113.                 if controlStrength <= 0 then
  114.                     capturingAllyTeam = soleAllyTeam
  115.                     controlStrength = 0
  116.                 end
  117.             else    -- capture
  118.                 controlStrength = controlStrength + captureSpeed
  119.                 if controlStrength >= 1 then
  120.                     point.controllingAllyTeam = soleAllyTeam
  121.                     controlStrength = 1
  122.                     capturingAllyTeam = nil
  123.                 end
  124.             end
  125.         else    -- contested
  126.             -- do nothing?
  127.         end
  128.     else    -- controlled by one allyTeam
  129.         if numAllyTeams == 0 then   -- unoccupied
  130.             -- recover to full if necessary
  131.             controlStrength = controlStrength + captureSpeed
  132.             if controlStrength >= 1 then
  133.                 controlStrength = 1
  134.                 capturingAllyTeam = nil
  135.             end
  136.         elseif numAllyTeams == 1 then   -- occupied by one team
  137.             if soleAllyTeam ~= controllingAllyTeam then -- decapture
  138.                 controlStrength = controlStrength - decaptureSpeed
  139.                 if controlStrength <= 0 then
  140.                     point.controllingAllyTeam = nil
  141.                     capturingAllyTeam = soleAllyTeam
  142.                     controlStrength = 0
  143.                 end
  144.             else    -- recover to full if necessary (at increased speed)
  145.                 controlStrength = controlStrength + decaptureSpeed
  146.                 if controlStrength >= 1 then
  147.                     controlStrength = 1
  148.                     capturingAllyTeam = nil
  149.                 end
  150.             end
  151.         else    -- contested
  152.             -- do nothing?
  153.         end
  154.     end
  155.     point.controlStrength = controlStrength
  156.     point.capturingAllyTeam = capturingAllyTeam
  157.    
  158.     if point.controllingAllyTeam then
  159.         return true -- defuse the bomb
  160.     end
  161. end
  162.  
  163. local function ProcessPoints()
  164.     local toRemove = {}
  165.     for i=1,#points do
  166.         local defused = ProcessPoint(i)
  167.         if defused then
  168.             toRemove[#toRemove+1] = i
  169.         end
  170.     end
  171.     for i=1,#toRemove do
  172.         local index = toRemove[i]
  173.         local point = points[index]
  174.         local units = spGetUnitsInCylinder(point.pos[1], point.pos[3], point.radius)
  175.         for i=1,#units do
  176.             local unitID = units[i]
  177.             GG.mission.RemoveUnitGroup(unitID, "Bomb")
  178.         end
  179.        
  180.         table.remove(points,index)
  181.         captures = captures + 1
  182.         GG.mission.ExecuteTriggerByName(triggers[captures])
  183.     end
  184.     if #points == 0 then
  185.         gadgetHandler:RemoveGadget()
  186.     end
  187. end
  188. --------------------------------------------------------------------------------
  189. --------------------------------------------------------------------------------
  190. function gadget:Initialize()
  191.     --[[
  192.     local goalTime = (Spring.GetModOptions().hilltime or 0) * 60
  193.    
  194.     graceLength = Spring.GetModOptions().gracetime
  195.     if graceLength then
  196.         grace = graceLength * 60
  197.     else
  198.         grace = 0
  199.     end
  200.     for allyTeamID in pairs(allyTeams) do
  201.         allyTeams[allyTeamID] = goalTime
  202.     end
  203.     ]]
  204. end
  205.  
  206. function gadget:GameFrame(f)
  207.     --[[
  208.     if(f%30 == 0 and f < grace * 30 + graceLength*30*60) then
  209.         grace = grace - 1
  210.         _G.grace = grace
  211.     end
  212.     if(f == grace*30 + graceLength*30*60) then
  213.         _G.grace = grace
  214.         --Spring.Echo("Grace period is over. GET THE HILL!")
  215.     end
  216.     ]]
  217.     if(f % UPDATE_PERIOD == 0) then
  218.         ProcessPoints()
  219.     end
  220. end
  221.  
  222. --------------------------------------------------------------------------------
  223. --------------------------------------------------------------------------------
  224. else
  225. --------------------------------------------------------------------------------
  226. --UNSYNCED
  227. --------------------------------------------------------------------------------
  228. local glBeginEnd = gl.BeginEnd
  229. local glPushMatrix = gl.PushMatrix
  230. local glPopMatrix = gl.PopMatrix
  231. local glColor = gl.Color
  232. local glTranslate = gl.Translate
  233. local glVertex = gl.Vertex
  234. local glText = gl.Text
  235. local glDepthTest = gl.DepthTest
  236.  
  237. local allyTeams = { [0] = {0,1,0.5}, [1] = {1,0,0} }
  238. local circleDivs = 65   -- circle resolution
  239. local innersize = 0.8
  240. local outersize = 1 -- outer radius size compared to hill radius
  241.  
  242. local function MakeRealTable(proxy)
  243.     if not proxy then return end
  244.     local proxyLocal = proxy
  245.     local ret = {}
  246.     for i,v in spairs(proxyLocal) do
  247.         if type(v) == "table" then
  248.             ret[i] = MakeRealTable(v)
  249.         else
  250.             ret[i] = v
  251.         end
  252.     end
  253.     return ret
  254. end
  255.  
  256.  
  257. local function DrawCircleInside(circleDivs, r, g, b, alpha, radius)
  258.     local radstep = (2.0 * math.pi) / circleDivs
  259.     for i = 1, circleDivs do
  260.         local a1 = (i * radstep)
  261.         local a2 = ((i+1) * radstep)
  262.         glColor(r, g, b, 0)
  263.         glVertex(0, 0, 0)
  264.         glColor(r, g, b, alpha)
  265.         glVertex(math.sin(a1)*radius, 0, math.cos(a1)*radius)
  266.         glVertex(math.sin(a2)*radius, 0, math.cos(a2)*radius)
  267.     end
  268. end
  269.  
  270. local function DrawCircleRim(circleDivs, numSlices, r, g, b, alpha, fadealpha, radius)
  271.     local radstep = (2.0 * math.pi) / circleDivs
  272.     for i = 1, numSlices do
  273.         local a1 = (i * radstep)
  274.         local a2 = ((i+1) * radstep)
  275.         glColor(r, g, b, fadealpha)
  276.         glVertex(math.sin(a1)* radius * innersize, 0, math.cos(a1)*radius * innersize)
  277.         glVertex(math.sin(a2)* radius * innersize, 0, math.cos(a2)*radius * innersize)
  278.         glColor(r, g, b, alpha)
  279.         glVertex(math.sin(a2) * radius * outersize, 0, math.cos(a2) * radius * outersize)
  280.         glVertex(math.sin(a1) * radius * outersize, 0, math.cos(a1) * radius * outersize)
  281.     end
  282. end
  283.  
  284. local function DrawPointCircle(point)
  285.     local owner = point.owningAllyTeam
  286.     local capper = point.capturingAllyTeam
  287.     local innerColor = {1,1,1}
  288.     if owner then
  289.         innerColor = allyTeams[owner]
  290.     end
  291.     local r1, g1, b1 = unpack(innerColor)
  292.     local outerColor = innerColor
  293.     if capper and (not owner) then
  294.         outerColor = allyTeams[capper]
  295.     end
  296.     local r2, g2, b2 = unpack(outerColor)
  297.    
  298.     local alpha = 0.5
  299.     local fadealpha = 0.3
  300.     if (r == b) and (r == g) then  -- increased alphas for greys/b/w
  301.         alpha = 0.7
  302.         fadealpha = 0.5
  303.     end
  304.     local radius = point.radius
  305.     local numSlices = math.floor(point.controlStrength*circleDivs)
  306.    
  307.     glPushMatrix()
  308.     glTranslate(point.pos[1], point.pos[2] + 10, point.pos[3])
  309.     glBeginEnd(GL.TRIANGLES, DrawCircleInside, circleDivs, r1, g1, b1, fadealpha, radius)
  310.     glBeginEnd(GL.QUADS, DrawCircleRim, circleDivs, numSlices, r2, g2, b2, alpha, fadealpha, radius)
  311.     glPopMatrix()
  312. end
  313.  
  314. function gadget:DrawWorldPreUnit()
  315.     if not Spring.IsGUIHidden() then
  316.         local points = SYNCED.points
  317.         --glDepthTest(true)
  318.         for _,v in spairs(points) do
  319.             DrawPointCircle(v)
  320.         end
  321.         glColor(1,1,1,1)
  322.         --glDepthTest(false)
  323.     end
  324. end
  325. --------------------------------------------------------------------------------
  326. --------------------------------------------------------------------------------
  327. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement