Complete.Org: Mailing Lists: Archives: freeciv-dev: June 2004:
[Freeciv-Dev] Re: (PR#8906) save shuffled_players
Home

[Freeciv-Dev] Re: (PR#8906) save shuffled_players

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#8906) save shuffled_players
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 6 Jun 2004 09:57:46 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8906 >

Gregory Berkolaiko wrote:

>>>Anyway, your patch is a step in right direction.  Is set_shuffled_players
>>>used more than once? I think you should kill it.  Set the players as you
>>>load them.
>>
>>This is possible but it means the shuffled players variables must be
>>made global (currently they are static).  I preferred to just add a new
>>function to the interface.  But I can do it the other way if you like.
>>Is it worth it?
> 
> Ah, I see.  No, definitely not worth it.  Although you could make a
> smaller function set_shuffled_player to set just one player and call it in
> the loop, I think this is worth it.  And definitely write in the header of
> the function that it is essentially an accessor to the static variables.

This is possible (patch attached).  Only problem with this is with 
shuffled_nplayers.  This is set when the players are shuffled so that if 
the number of players change they'll be reshuffled (although I suspect 
this is impossible).  In theory it's like a boolean "is_shuffled" value.

And if we set values one at a time we could end up with only some 
positions shuffled, while shuffled_nplayers has been set (since of 
course we have to set it on _every_ call to set_shuffled_player).

jason

? error.gz
? orig
? test.gz
Index: server/plrhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.c,v
retrieving revision 1.311
diff -u -r1.311 plrhand.c
--- server/plrhand.c    28 May 2004 06:52:30 -0000      1.311
+++ server/plrhand.c    6 Jun 2004 16:45:44 -0000
@@ -1628,11 +1628,27 @@
     shuffled_plr[pos] = tmp_plr;
   }
 
+#ifdef DEBUG
+  for (i = 0; i < game.nplayers; i++) {
+    freelog(LOG_DEBUG, "Shuffling player %d as %d.",
+           i, shuffled_plr[i]->player_no);
+  }
+#endif
+
   /* Record how many players there were when shuffled: */
   shuffled_nplayers = game.nplayers;
 }
 
 /**************************************************************************
+  Initialize the shuffled players list (as from a loaded savegame).
+**************************************************************************/
+void set_shuffled_player(int index, int shuffled_player)
+{
+  shuffled_plr[index] = get_player(shuffled_player);
+  shuffled_nplayers = game.nplayers;
+}
+
+/**************************************************************************
   Return i'th shuffled player.  If number of players has grown between
   re-shuffles, added players are given in unshuffled order at the end.
   Number of players should not have shrunk.
Index: server/plrhand.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.h,v
retrieving revision 1.64
diff -u -r1.64 plrhand.h
--- server/plrhand.h    28 May 2004 06:52:30 -0000      1.64
+++ server/plrhand.h    6 Jun 2004 16:45:44 -0000
@@ -78,6 +78,7 @@
 void do_conquer_cost(struct player *pplayer);
 
 void shuffle_players(void);
+void set_shuffled_player(int index, int shuffled_player);
 struct player *shuffled_player(int i);
 
 #define shuffled_players_iterate(pplayer)                                   \
Index: server/savegame.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/savegame.c,v
retrieving revision 1.157
diff -u -r1.157 savegame.c
--- server/savegame.c   31 May 2004 06:41:11 -0000      1.157
+++ server/savegame.c   6 Jun 2004 16:45:44 -0000
@@ -2401,6 +2401,19 @@
     game.nplayers = 0;
   }
 
+  if (secfile_lookup_int_default(file, -1,
+                                "game.shuffled_player_%d", 0) >= 0) {
+    for (i = 0; i < game.nplayers; i++) {
+      int shuffled_player
+       = secfile_lookup_int(file, "game.shuffled_player_%d", i);
+      set_shuffled_player(i, shuffled_player);
+    }
+  } else {
+    /* No shuffled players included, so shuffle them (this may include
+     * scenarios). */
+    shuffle_players();
+  }
+
   if (!game.is_new_game) {
     /* Set active city improvements/wonders and their effects */
     improvements_update_obsolete();
@@ -2600,5 +2613,10 @@
     players_iterate(pplayer) {
       player_save(pplayer, pplayer->player_no, file);
     } players_iterate_end;
+
+    for (i = 0; i < game.nplayers; i++) {
+      secfile_insert_int(file, shuffled_player(i)->player_no,
+                        "game.shuffled_player_%d", i);
+    }
   }
 }
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.166
diff -u -r1.166 srv_main.c
--- server/srv_main.c   4 Jun 2004 15:49:59 -0000       1.166
+++ server/srv_main.c   6 Jun 2004 16:45:44 -0000
@@ -457,9 +457,10 @@
     }
   }
 
-  /* FIXME: player shuffling shouldn't be repeated unless is_new_turn. */
-  freelog(LOG_DEBUG, "Shuffleplayers");
-  shuffle_players();
+  if (is_new_turn) {
+    freelog(LOG_DEBUG, "Shuffleplayers");
+    shuffle_players();
+  }
 
   sanity_check();
 }

[Prev in Thread] Current Thread [Next in Thread]