Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1.  
  2. function gadget:GetInfo()
  3.     return {
  4.         name = "Game Over",
  5.         desc = "Manage Game Over",
  6.         author = "zwzsg",
  7.         date = "2nd of July 2011",
  8.         license = "Free",
  9.         layer = 0,
  10.         enabled = true
  11.     }
  12. end
  13.  
  14. local mokey="gamemode" -- Mod Option Key
  15.  
  16. if (gadgetHandler:IsSyncedCode()) then
  17. --SYNCED
  18.  
  19.     local RemainingTeamList={}
  20.     local LifeSustainerDefIdList={}
  21.     local CheckForGameOver=false
  22.     local GameModeKey=1
  23.     -- 0: Kill everything
  24.     -- 1: Kill all factories
  25.     -- 2: Kill homebase
  26.     -- 3: Never ends
  27.  
  28.     local function isMiniFacFunc(UnitDef)
  29.         return UnitDef.isCommander -- Change this to adapt to your own game
  30.     end
  31.  
  32.     local function isHomeBaseFunc(UnitDef)
  33.         return UnitDef.isCommander and math.max(UnitDef.xsize,UnitDef.zsize)>=12 -- Change this to adapt to your own game
  34.     end
  35.  
  36.     function gadget:Initialize()
  37.         if not Spring.GameOver then
  38.             gadgetHandler:RemoveGadget()
  39.             return
  40.         else
  41.             if Spring.GetModOptions and Spring.GetModOptions() then
  42.                 GameModeKey=tonumber(Spring.GetModOptions()[mokey] or 1) or 1
  43.                 if GameModeKey~=0 and GameModeKey~=1 and GameModeKey~=2 and GameModeKey~=3 then
  44.                     GameModeKey=1
  45.                 end
  46.             end
  47.             if GameModeKey==3 then
  48.                 gadgetHandler:RemoveGadget()
  49.                 return
  50.             elseif GameModeKey==1 or GameModeKey==2 then
  51.                 local isLifeSustainerFunc = GameModeKey==1 and isMiniFacFunc or isHomeBaseFunc
  52.                 LifeSustainerDefIdList={}
  53.                 for _,ud in pairs(UnitDefs) do
  54.                     if isLifeSustainerFunc(ud) then
  55.                         table.insert(LifeSustainerDefIdList,ud.id)
  56.                     end
  57.                 end
  58.             end
  59.             RemainingTeamList={}
  60.             for _,team in ipairs(Spring.GetTeamList()) do
  61.                 if team~=Spring.GetGaiaTeamID() then
  62.                     table.insert(RemainingTeamList,team)
  63.                 end
  64.             end
  65.         end
  66.     end
  67.  
  68.     function gadget:GameFrame(f)
  69.         -- Every second (don't do at first frame, cause no units then)
  70.         if f%30==25 then
  71.             -- Check game over condition
  72.             if CheckForGameOver then
  73.                 --Spring.Echo("Checking if game is over")
  74.                 CheckForGameOver=false
  75.                 -- Check if there's a pair of opposing team
  76.                 areEnemiesAlive=false
  77.                 for _,team in ipairs(RemainingTeamList) do
  78.                     for _,otherTeam in ipairs(RemainingTeamList) do
  79.                         if (not Spring.AreTeamsAllied(team,otherTeam)) and (not Spring.AreTeamsAllied(otherTeam,team)) then
  80.                             areEnemiesAlive=true
  81.                         end
  82.                     end
  83.                 end
  84.                 -- If no, list the winning ally team and declare game to be over
  85.                 if not areEnemiesAlive then
  86.                     local WinningAllies={}
  87.                     local isWinningAlly={}
  88.                     for _,team in ipairs(RemainingTeamList) do
  89.                         local _,_,_,_,_,allyTeam=Spring.GetTeamInfo(team)
  90.                         --Spring.Echo("team["..team.."] of allegiance["..allyTeam.."] is amongst the winners!")
  91.                         if not isWinningAlly[team] then
  92.                             isWinningAlly[allyTeam]=true
  93.                             table.insert(WinningAllies,allyTeam)
  94.                         end
  95.                     end
  96.                     --Spring.Echo("WinningAllies={"..table.concat(WinningAllies).."}")
  97.                     Spring.GameOver(WinningAllies)
  98.                 end
  99.             end
  100.             -- Check if teams are still alive
  101.             for idx,team in ipairs(RemainingTeamList) do
  102.                 if (GameModeKey==0 and #Spring.GetTeamUnits(team) or #Spring.GetTeamUnitsByDefs(team,LifeSustainerDefIdList))==0 then
  103.                     Spring.KillTeam(team)
  104.                     table.remove(RemainingTeamList,idx)
  105.                     CheckForGameOver=true
  106.                 end
  107.             end
  108.         end
  109.     end
  110.  
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement