Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2005:
[Freeciv-Dev] (PR#12939) create global observer
Home

[Freeciv-Dev] (PR#12939) create global observer

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12939) create global observer
From: "Mike Kaufman" <kaufman@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 29 Apr 2005 22:10:13 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12939 >

here's a patch that takes the creation of a global observer out of
observe_command in stdinhand.c and into its own function in plrhand.c
I also added some comments to make some things a bit more clear.

-mike

Index: server/plrhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.c,v
retrieving revision 1.366
diff -u -r1.366 plrhand.c
--- server/plrhand.c    15 Apr 2005 05:22:52 -0000      1.366
+++ server/plrhand.c    30 Apr 2005 05:03:10 -0000
@@ -1893,6 +1893,65 @@
 }
 
 /**********************************************************************
+ Create a player with is_observer = TRUE and return it.
+ If a global observer has already been created, return that player.
+ If there are no player slots available return NULL.
+***********************************************************************/
+struct player *create_global_observer(void)
+{
+  struct player *pplayer = NULL;
+
+  /* check if a global observer already exists. If so, return it. */
+  players_iterate(aplayer) {
+    if (aplayer->is_observer) {
+      return aplayer;
+    }
+  } players_iterate_end
+
+  /* if we're here, we couldn't find an observer, check if we have
+   * a slot available to create one */
+  if (game.nplayers >= MAX_NUM_PLAYERS) {
+    notify_player(NULL, _("A global observer cannot be created: too "
+                          "many regular players."));
+    return NULL;
+  }
+
+  /* alright, we can create an observer. go for it. */
+  pplayer = &game.players[game.nplayers];
+
+  /* only allocate a player map is the game is running or a game is loaded
+   * in pregame. This is because a game map might not be created otherwise. */
+  server_player_init(pplayer,
+                     (server_state == RUN_GAME_STATE) || !game.is_new_game);
+
+  sz_strlcpy(pplayer->name, OBSERVER_NAME);
+  sz_strlcpy(pplayer->username, ANON_USER_NAME);
+  pplayer->is_connected = FALSE;
+  pplayer->is_observer = TRUE;
+  pplayer->capital = TRUE;     /* is this necessary? maybe for client... */
+  pplayer->phase_done = TRUE;
+  pplayer->embassy = 0;                /* no embassies */
+  pplayer->is_alive = FALSE;
+  pplayer->was_created = FALSE;        /* doesn't really matter */
+
+  /* don't do this otherwise, because a game map might not have been created */
+  if ((server_state == RUN_GAME_STATE) || !game.is_new_game) {
+    pplayer->nation = OBSERVER_NATION;
+    init_tech(pplayer, 0);
+    map_know_and_see_all(pplayer);
+  }
+
+  game.nplayers++;
+
+  /* tell everyone that game.nplayers has been updated */
+  send_game_info(NULL);
+  send_player_info(pplayer, NULL);
+  notify_player(NULL, _("A global observer has been created"));
+
+  return pplayer;
+}
+
+/**********************************************************************
 This function creates a new player and copies all of it's science
 research etc.  Players are both thrown into anarchy and gold is
 split between both players.
Index: server/plrhand.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.h,v
retrieving revision 1.70
diff -u -r1.70 plrhand.h
--- server/plrhand.h    23 Feb 2005 03:34:06 -0000      1.70
+++ server/plrhand.h    30 Apr 2005 05:03:10 -0000
@@ -81,6 +81,7 @@
 void shuffle_players(void);
 void set_shuffled_players(int *shuffled_players);
 struct player *shuffled_player(int i);
+struct player *create_global_observer(void);
 
 #define shuffled_players_iterate(pplayer)                                   \
 {                                                                           \
Index: server/stdinhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/stdinhand.c,v
retrieving revision 1.400
diff -u -r1.400 stdinhand.c
--- server/stdinhand.c  29 Apr 2005 18:14:40 -0000      1.400
+++ server/stdinhand.c  30 Apr 2005 05:03:12 -0000
@@ -2701,49 +2701,8 @@
 
   /* if we have no pplayer, it means that we want to be a global observer */
   if (!pplayer) {
-    /* check if a global  observer has already been created */
-    players_iterate(aplayer) {
-      if (aplayer->is_observer) {
-        pplayer = aplayer;
-        break;
-      }
-    } players_iterate_end;
-
-    /* we need to create a new player */
-    if (!pplayer) {
-      if (game.nplayers >= MAX_NUM_PLAYERS) {
-        notify_player(NULL, _("A global observer cannot be created: too "
-                              "many regular players."));
-        goto end;
-      }
-
-      pplayer = &game.players[game.nplayers];
-      server_player_init(pplayer, 
-                         (server_state == RUN_GAME_STATE) || 
!game.is_new_game);
-      sz_strlcpy(pplayer->name, OBSERVER_NAME);
-      sz_strlcpy(pplayer->username, ANON_USER_NAME);
-
-      pplayer->is_connected = FALSE;
-      pplayer->is_observer = TRUE;
-      pplayer->capital = TRUE;
-      pplayer->phase_done = TRUE;
-      pplayer->embassy = 0;   /* no embassys */
-      pplayer->is_alive = FALSE;
-      pplayer->was_created = FALSE;
-
-      if ((server_state == RUN_GAME_STATE) || !game.is_new_game) {
-        pplayer->nation = OBSERVER_NATION;
-        init_tech(pplayer, 0);
-        map_know_and_see_all(pplayer);
-      }
-
-      game.nplayers++;
-
-      /* tell everyone that game.nplayers has been updated */
-      send_game_info(NULL);
-      send_player_info(pplayer, NULL);
-
-      notify_player(NULL, _("A global observer has been created"));
+    if (!(pplayer = create_global_observer())) {
+      goto end; /* couldn't create an observer */ 
     }
   }
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12939) create global observer, Mike Kaufman <=