foxygit / doom Log in
commit f0c7aa035a81c84e26edd0cd9aafd13f00e0a2fe
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Thu Aug 9 00:04:03 2007 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Thu Aug 9 00:04:03 2007 +0000

    Don't crash when all players have quit.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 955
---
 src/am_map.c | 12 ++++++++++--
 src/d_net.c  | 32 +++++++++++++++++++++++++++++---
 2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/src/am_map.c b/src/am_map.c
index 347d592f..cad46a70 100644
--- a/src/am_map.c
+++ b/src/am_map.c
@@ -472,13 +472,21 @@ void AM_initVariables(void)
     m_w = FTOM(f_w);
     m_h = FTOM(f_h);

+    plr = &players[0];
+
     // find player to center on initially
     if (!playeringame[pnum = consoleplayer])
+    {
 	for (pnum=0;pnum<MAXPLAYERS;pnum++)
+        {
 	    if (playeringame[pnum])
+            {
+                plr = &players[pnum];
 		break;
-
-    plr = &players[pnum];
+            }
+        }
+    }
+
     m_x = plr->mo->x - m_w/2;
     m_y = plr->mo->y - m_h/2;
     AM_changeWindowLoc();
diff --git a/src/d_net.c b/src/d_net.c
index b8ecd645..4e87a813 100644
--- a/src/d_net.c
+++ b/src/d_net.c
@@ -207,7 +207,6 @@ void NetUpdate (void)
         }

 #endif
-
         netcmds[consoleplayer][maketic % BACKUPTICS] = cmd;

 	++maketic;
@@ -408,6 +407,23 @@ void D_QuitNetGame (void)

 }

+// Returns true if there are currently any players in the game.
+
+static boolean PlayersInGame(void)
+{
+    int i;
+
+    for (i=0; i<MAXPLAYERS; ++i)
+    {
+        if (playeringame[i])
+        {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 static int GetLowTic(void)
 {
     int i;
@@ -454,7 +470,7 @@ void TryRunTics (void)
     int realtics;
     int	availabletics;
     int	counts;
-
+
     // get real tics
     entertic = I_GetTime() / ticdup;
     realtics = entertic - oldentertics;
@@ -539,7 +555,8 @@ void TryRunTics (void)
 	counts = 1;

     // wait for new tics if needed
-    while (lowtic < gametic/ticdup + counts)
+
+    while (!PlayersInGame() || lowtic < gametic/ticdup + counts)
     {
 	NetUpdate ();

@@ -564,6 +581,14 @@ void TryRunTics (void)
     {
 	for (i=0 ; i<ticdup ; i++)
 	{
+            // check that there are players in the game.  if not, we cannot
+            // run a tic.
+
+            if (!PlayersInGame())
+            {
+                return;
+            }
+
 	    if (gametic/ticdup > lowtic)
 		I_Error ("gametic>lowtic");
 	    if (advancedemo)
@@ -592,3 +617,4 @@ void TryRunTics (void)
 	NetUpdate ();	// check for new console commands
     }
 }
+