From 6b04139c5b49623f6c3467ac2faa0e374bde5d6f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 13 Mar 2016 10:51:01 +1100 Subject: [PATCH] Modularise core zombie code some more, poll player positions every 50ms instead of every 500ms, also rounds start after 30 seconds not two minutes. --- Commands/Fun/CmdZombieGame.cs | 2 +- Games/ZombieSurvival/ZombieGame.Core.cs | 133 ++++++++++++------------ Games/ZombieSurvival/ZombieGame.cs | 17 +-- 3 files changed, 78 insertions(+), 74 deletions(-) diff --git a/Commands/Fun/CmdZombieGame.cs b/Commands/Fun/CmdZombieGame.cs index b2e773c4f..952ab1b2e 100644 --- a/Commands/Fun/CmdZombieGame.cs +++ b/Commands/Fun/CmdZombieGame.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands Player.SendMessage(p, "There is a one-time Zombie Survival game currently in progress."); return; case 3: - Player.SendMessage(p, "There is a Zombie Survival game currently in progress with a " + Server.zombie.limitRounds + " amount of rounds."); + Player.SendMessage(p, "There is a Zombie Survival game currently in progress with a " + Server.zombie.MaxRounds + " amount of rounds."); return; case 4: Player.SendMessage(p, "There is a Zombie Survival game currently in progress, scheduled to stop after this round."); diff --git a/Games/ZombieSurvival/ZombieGame.Core.cs b/Games/ZombieSurvival/ZombieGame.Core.cs index b5d228af7..8e00a59bd 100644 --- a/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/Games/ZombieSurvival/ZombieGame.Core.cs @@ -37,14 +37,14 @@ namespace MCGalaxy { while (true) { zombieRound = false; - amountOfRounds++; + RoundsDone++; if (gameStatus == 0) { return; } else if (gameStatus == 1) { DoRound(); if (ChangeLevels) ChangeLevel();} else if (gameStatus == 2) { DoRound(); if (ChangeLevels) ChangeLevel(); gameStatus = 0; return; } else if (gameStatus == 3) { - if (limitRounds == amountOfRounds) { ResetState(); return; } + if (RoundsDone == MaxRounds) { ResetState(); return; } else { DoRound(); if (ChangeLevels) ChangeLevel(); } } else if (gameStatus == 4) { ResetState(); return; } @@ -54,79 +54,91 @@ namespace MCGalaxy { void DoRound() { if (gameStatus == 0) return; - GoBack: Player.GlobalMessage("%4Round Start:%f 2:00"); - Thread.Sleep(60000); if (!Server.ZombieModeOn) return; - Player.GlobalMessage("%4Round Start:%f 1:00"); - Thread.Sleep(55000); if (!Server.ZombieModeOn) return; - Server.s.Log(Convert.ToString(ChangeLevels) + " " + Convert.ToString(Server.ZombieOnlyServer) + " " + Convert.ToString(UseLevelList) + " " + string.Join(",", LevelList.ToArray())); - Player.GlobalMessage("%4Round Start:%f 5..."); - Thread.Sleep(1000); if (!Server.ZombieModeOn) return; - Player.GlobalMessage("%4Round Start:%f 4..."); - Thread.Sleep(1000); if (!Server.ZombieModeOn) return; - Player.GlobalMessage("%4Round Start:%f 3..."); - Thread.Sleep(1000); if (!Server.ZombieModeOn) return; - Player.GlobalMessage("%4Round Start:%f 2..."); - Thread.Sleep(1000); if (!Server.ZombieModeOn) return; - Player.GlobalMessage("%4Round Start:%f 1..."); - Thread.Sleep(1000); if (!Server.ZombieModeOn) return; - zombieRound = true; - int playerscountminusref = 0; List players = new List(); - - Player[] online = PlayerInfo.Online; - foreach (Player p in online) { - if (p.referee) { - p.color = p.group.color; - } else if (p.level.name == currentLevelName) { - p.color = p.group.color; - players.Add(p); - playerscountminusref++; - } - } - if (playerscountminusref < 2) { - Player.GlobalMessage(Colors.red + "ERROR: Need more than 2 players to play"); - goto GoBack; - } + List players = DoRoundCountdown(); theEnd: Random random = new Random(); int firstinfect = random.Next(players.Count()); Player player = null; - if (queZombie) - player = PlayerInfo.Find(nextZombie); - else - player = players[firstinfect]; + if (queZombie) player = PlayerInfo.Find(nextZombie); + else player = players[firstinfect]; if (player.level.name != currentLevelName) goto theEnd; - Player.GlobalMessage(player.color + player.name + Server.DefaultColor + " started the infection!"); + Player.GlobalMessage(player.color + player.name + " %Sstarted the infection!"); player.infected = true; player.color = Colors.red; Player.GlobalDespawn(player, false); Player.GlobalSpawn(player, player.pos[0], player.pos[1], player.pos[2], player.rot[0], player.rot[1], false); zombieRound = true; - int roundMins = random.Next(5, 12); + int roundMins = random.Next(5, 8); Player.GlobalMessage("The round will last for " + roundMins + " minutes!"); timer = new System.Timers.Timer(roundMins * 60 * 1000); timer.Elapsed += new ElapsedEventHandler(EndRound); timer.Enabled = true; - online = PlayerInfo.Online; - foreach (Player p in online) - { - if(p != player) + Player[] online = PlayerInfo.Online; + foreach (Player p in online) { + if (p != player) alive.Add(p); } infectd.Clear(); - if (queZombie) - infectd.Add(PlayerInfo.Find(nextZombie)); - else - infectd.Add(player); + if (queZombie) infectd.Add(PlayerInfo.Find(nextZombie)); + else infectd.Add(player); aliveCount = alive.Count; - - while (aliveCount > 0) - { + DoCoreGame(players, random); + + if (gameStatus == 0) { + gameStatus = 4; return; + } else { + HandOutRewards(); + } + } + + List DoRoundCountdown() { + while (true) { + string logMessage = Convert.ToString(ChangeLevels) + " " + Convert.ToString(Server.ZombieOnlyServer) + + " " + Convert.ToString(UseLevelList) + " " + string.Join(",", LevelList.ToArray()); + Server.s.Log(logMessage); + + Player.GlobalMessage("%4Round Start:%f 30..."); + Thread.Sleep(45000); if (!Server.ZombieModeOn) return null; + Player.GlobalMessage("%4Round Start:%f 10..."); + Thread.Sleep(10000); if (!Server.ZombieModeOn) return null; + Player.GlobalMessage("%4Round Start:%f 5..."); + Thread.Sleep(1000); if (!Server.ZombieModeOn) return null; + Player.GlobalMessage("%4Round Start:%f 4..."); + Thread.Sleep(1000); if (!Server.ZombieModeOn) return null; + Player.GlobalMessage("%4Round Start:%f 3..."); + Thread.Sleep(1000); if (!Server.ZombieModeOn) return null; + Player.GlobalMessage("%4Round Start:%f 2..."); + Thread.Sleep(1000); if (!Server.ZombieModeOn) return null; + Player.GlobalMessage("%4Round Start:%f 1..."); + Thread.Sleep(1000); if (!Server.ZombieModeOn) return null; + zombieRound = true; + int nonRefPlayers = 0; + List players = new List(); + + Player[] online = PlayerInfo.Online; + foreach (Player p in online) { + if (p.referee) { + p.color = p.group.color; + } else if (p.level.name == currentLevelName) { + p.color = p.group.color; + players.Add(p); + nonRefPlayers++; + } + } + + if (nonRefPlayers >= 2) return players; + Player.GlobalMessage(Colors.red + "ERROR: Need 2 or more players to play"); + } + } + + void DoCoreGame(List players, Random random) { + while (aliveCount > 0) { aliveCount = alive.Count; infectd.ForEach(delegate(Player pKiller) { @@ -186,26 +198,16 @@ namespace MCGalaxy { pKiller.playersInfected = pKiller.playersInfected++; Player.GlobalDespawn(pAlive, false); Player.GlobalSpawn(pAlive, pAlive.pos[0], pAlive.pos[1], pAlive.pos[2], pAlive.rot[0], pAlive.rot[1], false); - Thread.Sleep(500); + Thread.Sleep(50); } } }); }); - Thread.Sleep(500); - } - if (gameStatus == 0) - { - gameStatus = 4; - return; - } - else - { - HandOutRewards(); + Thread.Sleep(50); } } - public void EndRound(object sender, ElapsedEventArgs e) - { + public void EndRound(object sender, ElapsedEventArgs e) { if (gameStatus == 0) return; Player.GlobalMessage("%4Round End:%f 5"); Thread.Sleep(1000); Player.GlobalMessage("%4Round End:%f 4"); Thread.Sleep(1000); @@ -215,8 +217,7 @@ namespace MCGalaxy { HandOutRewards(); } - public void HandOutRewards() - { + public void HandOutRewards() { zombieRound = false; if (gameStatus == 0) return; Player.GlobalMessage(Colors.lime + "The game has ended!"); diff --git a/Games/ZombieSurvival/ZombieGame.cs b/Games/ZombieSurvival/ZombieGame.cs index f9fdf9d0a..670bb44f9 100644 --- a/Games/ZombieSurvival/ZombieGame.cs +++ b/Games/ZombieSurvival/ZombieGame.cs @@ -35,10 +35,13 @@ namespace MCGalaxy { } } - public sealed partial class ZombieGame - { - public int amountOfRounds = 0; - public int limitRounds = 0; + public sealed partial class ZombieGame { + /// The number of rounds that have been played in this game so far. + public int RoundsDone = 0; + + /// The maximum number of rounds that can be played before the game ends. + public int MaxRounds = 0; + public int aliveCount = 0; public string currentZombieLevel = ""; public static System.Timers.Timer timer; @@ -79,8 +82,8 @@ namespace MCGalaxy { gameStatus = status; zombieRound = false; initialChangeLevel = false; - limitRounds = amount + 1; - amountOfRounds = 0; + MaxRounds = amount + 1; + RoundsDone = 0; //SET ALL THE VARIABLES?!? //Start the main Zombie thread @@ -204,7 +207,7 @@ namespace MCGalaxy { public void ResetState() { gameStatus = 0; - limitRounds = 0; + MaxRounds = 0; initialChangeLevel = false; Server.ZombieModeOn = false; zombieRound = false;