shadowndacorner

Rounds system

Jan 6th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.88 KB | None | 0 0
  1. GAMEMODE.Rounds={}
  2. GAMEMODE.Rounds.CurRound=0
  3. GAMEMODE.Rounds.Paused=false
  4. GAMEMODE.Rounds.SqrtVal=1
  5. GAMEMODE.Rounds.TimeStarted=CurTime()
  6. GAMEMODE.Rounds.TimeToEnd=-1
  7. GAMEMODE.Rounds.Waiting=false
  8. GAMEMODE.Rounds.HasSpawnedBoss=false
  9. GAMEMODE.Rounds.SpecialRounds={}
  10. GAMEMODE.Rounds.BossBattles={}
  11. local nextSpawnTime=0
  12. function AddSpecialRound(num, spawnfunc) -- Adds to spawning code, or overrides if a value is returned
  13.     if !num or !spawnfunc then return end
  14.     num=math.floor(num)
  15.     GAMEMODE.Rounds.SpecialRounds[num]=function()
  16.         spawnfunc()
  17.         hook.Call("SpecialRoundStarted", GM, GetCurrentRound())
  18.     end
  19. end
  20.  
  21. AddSpecialRound(5, function()
  22.     if CountEnemies()<(GetMaxEnemies()*2) and nextSpawnTime<CurTime() then
  23.         nextSpawnTime=CurTime()+0.5
  24.         local spawnpos=PickRandomSpawnpoint()
  25.         if spawnpos then
  26.             CreateZombie(spawnpos)
  27.             print("Spawning zombie...")
  28.         end
  29.         return true
  30.     end
  31. end)
  32.  
  33. AddSpecialRound(10, function()
  34.     if CountEnemies()<(GetMaxEnemies()*2) and nextSpawnTime<CurTime() then
  35.         nextSpawnTime=CurTime()+0.5
  36.         local spawnpos=PickRandomSpawnpoint()
  37.         if spawnpos then
  38.             CreateAntlion(spawnpos)
  39.             print("Spawning antlion...")
  40.         end
  41.         return true
  42.     end
  43. end)
  44.  
  45. AddSpecialRound(15, function()
  46.     if (GetCurrentRound()==10) and !GAMEMODE.Rounds.HasSpawnedBoss then
  47.         nextSpawnTime=CurTime()+0.5
  48.         local spawnpos=PickRandomStriderSpawnpoint()
  49.         GAMEMODE.Rounds.HasSpawnedBoss=true
  50.         if spawnpos then
  51.             CreateStrider(spawnpos)
  52.         end
  53.     end
  54. end)
  55.  
  56. function ResetRound()
  57.     GAMEMODE.Rounds.CurRound=0
  58.     GAMEMODE.Rounds.SqrtVal=1
  59.     GAMEMODE.Rounds.TimeStarted=CurTime()
  60.     GAMEMODE.Rounds.TimeToEnd=-1
  61.     GAMEMODE.Rounds.Waiting=false
  62.     GAMEMODE.Rounds.HasSpawnedBoss=false
  63.     for f, v in pairs(player.GetAll()) do
  64.         v:SetTeam(TEAM_REBEL)
  65.         v:Spawn()
  66.     end
  67.     if GAMEMODE.Rounds.CurRound==0 and GAMEMODE.Rounds.SqrtVal==1 and GAMEMODE.Rounds.TimeToEnd==-1 and !GAMEMODE.Rounds.Waiting then
  68.         return true;
  69.     else
  70.         return false;
  71.     end
  72. end
  73.  
  74. function StartRound(num, cl)
  75.     if table.Count(player.GetAll())==0 then
  76.         return ResetRound();
  77.     end
  78.     if !cl then
  79.         EndWaiting()
  80.         GAMEMODE.Rounds.HasSpawnedBoss=false
  81.         for f, v in pairs(player.GetAll()) do
  82.             v:SetHealth(100)
  83.         end
  84.         num=num or GAMEMODE.Rounds.CurRound+1
  85.         num=math.floor(num)
  86.         GAMEMODE.Rounds.SqrtVal=math.sqrt(num)
  87.         print("Starting round "..num.."!")
  88.         GAMEMODE.Rounds.Waiting=false
  89.         GAMEMODE.Rounds.CurRound=num
  90.         GAMEMODE.Rounds.TimeStarted=CurTime()
  91.         GAMEMODE.Rounds.TimeToEnd=CurTime()+30*(math.floor(GAMEMODE.Rounds.SqrtVal))
  92.         hook.Call("RoundStarted", GM, num)
  93.     end
  94.    
  95.     if ValidEntity(cl) then
  96.         umsg.Start("roundData", cl)
  97.     else
  98.         umsg.Start("roundData")
  99.     end
  100.         umsg.Long(GAMEMODE.Rounds.CurRound)
  101.         umsg.Long(GAMEMODE.Rounds.TimeStarted)
  102.         umsg.Long(GAMEMODE.Rounds.TimeToEnd)
  103.     umsg.End()
  104. end
  105.  
  106. function CleanupEnemies()
  107.     for f, v in pairs(GAMEMODE.Enemies) do
  108.         for k, ent in pairs(v) do
  109.             if ValidEntity(ent) then
  110.                 if ent.Kill then
  111.                     ent:Kill()
  112.                 else
  113.                     ent:Remove()
  114.                 end
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. function GetCurrentRound()
  121.     return GAMEMODE.Rounds.CurRound
  122. end
  123.  
  124. function GetRoundStartTime()
  125.     return GAMEMODE.Rounds.TimeStarted
  126. end
  127.  
  128. function GetRoundEndTime()
  129.     return GAMEMODE.Rounds.TimeToEnd
  130. end
  131.  
  132. function GetMaxEnemies()
  133.     return math.floor(GAMEMODE.Rounds.SqrtVal*10)
  134. end
  135.  
  136. function CheckRoundOver()
  137.     return (GAMEMODE.Rounds.TimeToEnd!=(-1)) and CurTime()>GAMEMODE.Rounds.TimeToEnd
  138. end
  139.  
  140. function GetWaiting()
  141.     return GAMEMODE.Rounds.Waiting
  142. end
  143.  
  144. function StartWaiting(num)
  145.     for f, v in pairs(player.GetAll()) do
  146.         v:SetHealth(100)
  147.     end
  148.     if num then
  149.         GAMEMODE.Rounds.CurRound=num-1
  150.     end
  151.     GAMEMODE.Rounds.Waiting=true
  152.     GAMEMODE.Rounds.TimeStarted=CurTime()+60
  153.     SendUserMessage("roundWaiting", RecipientFilter():AddAllPlayers(), true, GAMEMODE.Rounds.TimeStarted)
  154. end
  155.  
  156. function EndWaiting()
  157.     GAMEMODE.Rounds.Waiting=false
  158.     SendUserMessage("roundWaiting", RecipientFilter():AddAllPlayers(), false, GAMEMODE.Rounds.TimeStarted)
  159. end
  160.  
  161. function GetPaused()
  162.     return GAMEMODE.Rounds.IsPaused or false
  163. end
  164.  
  165. function SetPaused(bool)
  166.     if bool or bool==false then
  167.         GAMEMODE.Rounds.IsPaused=bool or false
  168.     end
  169. end
  170.  
  171. concommand.Add("cs_pause", function(ply)
  172.     if !ply:IsPlayer() or ply:IsAdmin() then
  173.         SetPaused(true)
  174.     end
  175. end)
  176.  
  177. concommand.Add("cs_resume", function(ply)
  178.     if !ply:IsPlayer() or ply:IsAdmin() then
  179.         SetPaused(false)
  180.         StartWaiting()
  181.     end
  182. end)
  183.  
  184. /*
  185. if (GetCurrentRound()==5) then
  186.         if CountEnemies()<(GetMaxEnemies()*2) and nextSpawnTime<CurTime() then
  187.             nextSpawnTime=CurTime()+0.5
  188.             local spawnpos=PickRandomSpawnpoint()
  189.             if spawnpos then
  190.                 CreateZombie(spawnpos)
  191.                 print("Spawning zombie...")
  192.             end
  193.         end
  194.         return
  195.     end
  196.     if (GetCurrentRound()==10) and !GAMEMODE.Rounds.HasSpawnedBoss then
  197.         nextSpawnTime=CurTime()+0.5
  198.         local spawnpos=PickRandomStriderSpawnpoint()
  199.         GAMEMODE.Rounds.HasSpawnedBoss=true
  200.         if spawnpos then
  201.             CreateStrider(spawnpos)
  202.         end
  203.     end
  204. */
  205. function SpawnEnemies()
  206.     if !GAMEMODE.Rounds.SpecialRounds[GetCurrentRound()] or !GAMEMODE.Rounds.SpecialRounds[GetCurrentRound()]() then
  207.         if CountEnemies()<GetMaxEnemies() and nextSpawnTime<CurTime() then
  208.             nextSpawnTime=CurTime()+0.5
  209.             local spawnpos=PickRandomSpawnpoint()
  210.             local shouldSpawn=true
  211.             for f, v in pairs(player.GetAll()) do
  212.                 local tracedata = {}
  213.                 tracedata.start = v:EyePos()
  214.                 tracedata.endpos = spawnpos
  215.                 tracedata.filter = player.GetAll()
  216.                 local trace=util.TraceLine(tracedata)
  217.                 --PrintTable(trace)
  218.                 if trace.Hit and trace.HitPos:Distance(spawnpos)<10 then
  219.                     shouldSpawn=false
  220.                 end
  221.                 print(shouldSpawn)
  222.             end
  223.             if spawnpos and shouldSpawn then
  224.                 local perc=math.random(0, 100)
  225.                 if perc>80 then
  226.                     CreateScanner(spawnpos)
  227.                     print("Spawning a scanner!")
  228.                 elseif perc>60 then
  229.                     CreateManhack(spawnpos)
  230.                     print("Spawning a manhack!")
  231.                 else
  232.                     CreateCombineSoldier(spawnpos)
  233.                     print("Spawning a soldier!")
  234.                 end
  235.             end
  236.         end
  237.     end
  238. end
  239.  
  240. hook.Add("Think", "runRoundCode", function()
  241.     if table.Count(team.GetPlayers(TEAM_REBEL))==0 then return end
  242.     if GAMEMODE.Rounds.Waiting and CurTime()>GetRoundStartTime() then
  243.         if GetPaused() then
  244.             if !GetWaiting() then
  245.                 StartWaiting()
  246.             end
  247.             return
  248.         else
  249.             EndWaiting()
  250.             StartRound()
  251.         end
  252.     end
  253.     if CurTime()>GetRoundEndTime() and !GetWaiting() and CountEnemies()<1 then
  254.         hook.Call("RoundEnded", GM, GetCurrentRound())
  255.         StartWaiting()
  256.     elseif CurTime()<GetRoundEndTime() and !(GetWaiting()) then
  257.         SpawnEnemies()
  258.     elseif (GetRoundStartTime()+120)<CurTime() and !(!GAMEMODE.Rounds.SpecialRounds[GetCurrentRound()] or !GAMEMODE.Rounds.SpecialRounds[GetCurrentRound()]()) then
  259.         CleanupEnemies()
  260.         StartRound()
  261.     end
  262. end)
  263.  
  264. concommand.Add("cs_cleanup", function(ply, cmd, args)
  265.     if !ply:IsPlayer() or ply:IsAdmin() then
  266.         CleanupEnemies()
  267.     end
  268. end)
  269.  
  270. concommand.Add("cs_forcestartround", function(ply, cmd, args)
  271.     if !ply:IsPlayer() or ply:IsAdmin() then
  272.         CleanupEnemies()
  273.         for f, v in pairs(player.GetAll()) do
  274.             v:SendLua("game.RemoveRagdolls()")
  275.         end
  276.         if args[1] and tonumber(args[1]) then
  277.             StartRound(tonumber(args[1]))
  278.         else
  279.             StartRound()
  280.         end
  281.     end
  282. end)
  283.  
  284. /*hook.Add("PlayerDeath", "checkAllPlayersDead", function(ply, inf, att)
  285.     ply:SetTeam(TEAM_SPECTATOR)
  286.     ply:Spectate(OBS_MODE_ROAMING)
  287.     timer.Simple(1, function()
  288.         if table.Count(team.GetPlayers(TEAM_REBEL))==0 then
  289.             game.CleanUpMap()
  290.             for f, v in pairs(player.GetAll()) do
  291.                 v:UnSpectate()
  292.                 v:SetTeam(TEAM_REBEL)
  293.                 v:Spawn()
  294.                 StartWaiting(1)
  295.             end
  296.         end
  297.     end)
  298. end)
  299.  
  300. hook.Add("CharacterLoaded", "StartRoundWhenLoaded", function(ply)
  301.     if table.Count(player.GetAll()==1) then
  302.         StartWaiting(1)
  303.     else
  304.         StartRound(1, ply)
  305.     end
  306. end)*/
  307.  
  308. hook.Add("PlayerDisconnected", "cleanupOnDisconnect", function(ply)
  309.     if table.Count(player.GetAll())==0 then
  310.         game.CleanUpMap()
  311.         ResetRound()
  312.         StartWaiting()
  313.     end
  314. end)
Advertisement
Add Comment
Please, Sign In to add comment