Diff file attached to this message.

       
____________________________________________________________________________________
Looking for a deal? Find great prices on flights and hotels with Yahoo! 
FareChase.
http://farechase.yahoo.com/
diff -r -u freeciv-original/server/score.c freeciv/server/score.c
--- freeciv-original/server/score.c	2007-09-22 10:37:30.000000000 -0400
+++ freeciv/server/score.c	2007-09-22 10:44:13.000000000 -0400
@@ -544,3 +544,136 @@
 
   fclose(fp);
 }
+
+/**************************************************************************
+This function will output a number of statistics to the debug log, 
+using freelog() at the given log level.  If the current log level
+is does not match that in the argument, the function will exit immediately
+without using CPU.
+
+The statistics will be entered into the log such that they can be extracted
+from the log using grep, and will be readable as a table.
+
+
+
+**************************************************************************/
+void log_player_statistics(int log_level) {
+
+  /* if the log level is not high enough, exit now, to save CPU */
+  if(log_get_level() < log_level) {
+    return;
+  }
+
+  int n = game.info.nplayers;
+  int buflen = 256 + 64*n;
+  char pname[64];
+  memset(pname, 0, 64);
+
+  /* define the value of each shield as 1.5 units of trade.  
+     This is a bit arbitrary, but, the two can be exchanged at about this rate:
+     Coinage produces one gold per mfg; buildings can be rushed for
+     two gold per mfg. The average is 1.5 gold per mfg. */
+  const float MFG_VALUE = 1.5;
+  
+  char* buf = (char*)fc_malloc(buflen);
+  memset(buf, 0, buflen);
+
+  /* The total population of the world.  I.e., if there are five size-2 cities,
+     total_pop = 10 */
+  int total_pop = 0;
+  
+  /* The total economic output of the world, where 3 units of bnp equal 2 of mfg */
+  float total_economy = 0;
+  
+  /* The relative share of the global population and economy held by a player */
+  float economy_share = 0.0;
+  float pop_share = 0.0;
+
+  /* Calculate global population, global economic output */
+  players_iterate(pplayer) {
+    total_pop += total_player_citizens(pplayer);
+    total_economy += (float)(pplayer->score.bnp) + (MFG_VALUE * (float)(pplayer->score.mfg));
+  } players_iterate_end;
+  
+  /* print out each player's share of global population */
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    pop_share = (float)total_player_citizens(pplayer) / (float)total_pop;
+    sprintf(buf+index, "%s: %2.2f%%, ", pname, pop_share*100.0);
+  } players_iterate_end;  
+  freelog(log_level, "(YEAR=%d) POP SHARE: %s", game.info.year, buf);
+
+  /* print out each player's share of global economic output */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    economy_share = ((float)(pplayer->score.bnp) + (MFG_VALUE * (float)(pplayer->score.mfg))) / (float)total_economy;
+    sprintf(buf+index, "%s: %2.2f%%, ", pname, economy_share*100.0);
+  } players_iterate_end;  
+  freelog(log_level, "(YEAR=%d) ECON SHARE: %s", game.info.year, buf);
+
+  /* print out each player's share of global population */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    pop_share = (float)total_player_citizens(pplayer) / (float)(pplayer->score.cities);
+    sprintf(buf+index, "%s: %2.2f, ", pname, pop_share);
+  } players_iterate_end;
+  freelog(LOG_NORMAL, "(YEAR=%d) POP PER CITY: %s", game.info.year, buf);
+
+  /* for each player, print out average economic output per city */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    economy_share = ((float)(pplayer->score.bnp) + (MFG_VALUE * (float)(pplayer->score.mfg))) / (float)(pplayer->score.cities);
+    sprintf(buf+index, "%s: %2.2f, ", pname, economy_share);
+  } players_iterate_end;
+  freelog(log_level, "(YEAR=%d) ECON PER CITY: %s", game.info.year, buf);
+
+  /* for each player, print out the number of military units */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    sprintf(buf+index, "%s: %d, ", pname, pplayer->score.units);
+  } players_iterate_end;  
+  freelog(LOG_NORMAL, "(YEAR=%d) MILITARY UNITS: %s", game.info.year, buf);
+
+  /* for each player, print out the number of techs discovered */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    sprintf(buf+index, "%s: %d, ", pname, pplayer->score.techs);
+  } players_iterate_end;
+  freelog(LOG_NORMAL, "(YEAR=%d) TECHS: %s", game.info.year, buf);
+
+  /* for each player, print out the number of wonders constructed */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    sprintf(buf+index, "%s: %d, ", pname, pplayer->score.wonders);
+  } players_iterate_end;
+  freelog(LOG_NORMAL, "(YEAR=%d) WONDERS: %s", game.info.year, buf);  
+
+  /* for each player, print out the overall game score */
+  memset(buf, 0, buflen);
+  players_iterate(pplayer) {
+    int index = strlen(buf);
+    sprintf(pname, "P%2d", pplayer->player_no);
+    sprintf(buf+index, "%s: %d, ", pname, pplayer->score.game);
+  } players_iterate_end;
+  freelog(log_level, "(YEAR=%d) GAME SCORE: %s", game.info.year, buf);
+
+
+  /* free the buffer used for the output to the log file */
+  free(buf);
+
+
+}
+
diff -r -u freeciv-original/server/score.h freeciv/server/score.h
--- freeciv-original/server/score.h	2007-09-22 10:37:30.000000000 -0400
+++ freeciv/server/score.h	2007-09-22 10:44:17.000000000 -0400
@@ -23,4 +23,6 @@
 
 void rank_users(void);
 
+void log_player_statistics(int log_level);
+
 #endif /* FC__SCORE_H */
diff -r -u freeciv-original/server/srv_main.c freeciv/server/srv_main.c
--- freeciv-original/server/srv_main.c	2007-09-22 10:37:32.000000000 -0400
+++ freeciv/server/srv_main.c	2007-09-22 10:43:46.000000000 -0400
@@ -576,6 +576,10 @@
     } players_iterate_end;
   }
 
+  if(is_new_turn) {
+    log_player_statistics(LOG_VERBOSE);
+  }
+
   /* find out if users attached to players have been attached to those players
    * for long enough. The first user to do so becomes "associated" to that
    * player for ranking purposes. */
diff -r -u freeciv-original/utility/log.c freeciv/utility/log.c
--- freeciv-original/utility/log.c	2007-09-22 10:37:35.000000000 -0400
+++ freeciv/utility/log.c	2007-09-22 10:45:07.000000000 -0400
@@ -201,6 +201,14 @@
 }
 
 /**************************************************************************
+Retrieve the logging level after initial log_init() and log_set_level().
+**************************************************************************/
+int log_get_level()
+{
+  return fc_log_level;
+}
+
+/**************************************************************************
 Adjust the callback function after initial log_init().
 **************************************************************************/
 void log_set_callback(log_callback_fn callback)
diff -r -u freeciv-original/utility/log.h freeciv/utility/log.h
--- freeciv-original/utility/log.h	2007-09-22 10:37:35.000000000 -0400
+++ freeciv/utility/log.h	2007-09-22 10:45:03.000000000 -0400
@@ -55,6 +55,9 @@
 void log_init(const char *filename, int initial_level,
 	      log_callback_fn callback);
 void log_set_level(int level);
+
+int log_get_level();
+
 void log_set_callback(log_callback_fn callback);
 
 void real_freelog(int level, const char *message, ...)
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to