Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2003:
[Freeciv-Dev] (PR#3751) Common class patch
Home

[Freeciv-Dev] (PR#3751) Common class patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3751) Common class patch
From: "ms209290@xxxxxxxxxxxxxxxxxxx" <ms209290@xxxxxxxxxxxxxxxxxxx>
Date: Tue, 18 Mar 2003 23:20:18 -0800
Reply-to: rt@xxxxxxxxxxxxxx

This patch makes the AI select a nation from the same class as 
human-players
I.E. If I choose English, AI won't play Mordorians.
--
mateusz
--- freeorig/server/srv_main.c  2003-02-13 07:41:32.000000000 +0100
+++ freeciv/server/srv_main.c   2003-03-18 16:56:17.000000000 +0100
@@ -1546,6 +1546,65 @@
 }
 
 /**************************************************************************
+  If all players have chosen the same nation class, return
+  this class, otherwise return NULL.
+**************************************************************************/  
+static char* find_common_class(void) 
+{
+  char* class = NULL;
+  struct nation_type* nation;
+
+  players_iterate(pplayer) {
+    if (pplayer->nation == MAX_NUM_NATIONS) {
+      /* still undecided */
+      continue;  
+    }
+    nation = get_nation_by_idx(pplayer->nation);
+    if (class == NULL) {
+      class = nation->class;
+    } else {
+      if (nation->class != NULL && strcmp(nation->class, class)) {
+        return NULL;
+      }
+    }
+  } players_iterate_end;
+  return class;
+}
+
+/**************************************************************************
+  Selects random nation from nation_avail[], but if it is possible
+  consider only the given class
+**************************************************************************/
+static Nation_Type_id select_random_nation(char* class)
+{
+  Nation_Type_id* nations, selected;
+  struct nation_type* nation;
+  int i, j;
+  
+  if (class == NULL) {
+    return nations_avail[myrand(num_nations_avail)];
+  }
+  
+  nations = fc_calloc(num_nations_avail, sizeof(Nation_Type_id));
+  for (j = i = 0; i < num_nations_avail; i++) {
+    nation = get_nation_by_idx(nations_avail[i]);
+    if (nation->class == NULL) continue;
+    if (strcmp(nation->class, class) == 0) {
+      nations[j++] = nations_avail[i];
+    }
+  }
+
+  if (j == 0) {
+    free(nations);
+    return nations_avail[myrand(num_nations_avail)];
+  } else {
+    selected = nations[myrand(j)];
+    free(nations);
+    return selected;
+  }
+}
+
+/**************************************************************************
 generate_ai_players() - Selects a nation for players created with
    server's "create <PlayerName>" command.  If <PlayerName> matches
    one of the leader names for some nation, we choose that nation.
@@ -1557,6 +1616,10 @@
    than the number of players currently connected.  If so, we create the
    appropriate number of players (game.aifill - game.nplayers) from
    scratch, choosing a random nation and appropriate name for each.
+   
+   When we choose a nation randomly we try to consider only nations
+   that are in the same class as nations choosen by other players.
+   (I.e. If human player decides to play English, AI won't use Mordorians.)
 
    If the AI player name is one of the leader names for the AI player's
    nation, the player sex is set to the sex for that leader, else it
@@ -1569,10 +1632,12 @@
   char player_name[MAX_LEN_NAME];
   struct player *pplayer;
   int i, old_nplayers;
+  char* common_class;
 
   /* Select nations for AI players generated with server
    * 'create <name>' command
    */
+  common_class = find_common_class();
   for (i=0; i<game.nplayers; i++) {
     pplayer = &game.players[i];
     
@@ -1607,13 +1672,21 @@
 
     if (nation == game.playable_nation_count) {
       pplayer->nation =
-       mark_nation_as_used(nations_avail[myrand(num_nations_avail)]);
+       mark_nation_as_used(select_random_nation(common_class));
       pplayer->city_style = get_nation_city_style(pplayer->nation);
       pplayer->is_male = (myrand(2) == 1);
     }
 
     announce_ai_player(pplayer);
   }
+  
+  /* We do this again, because user could type:
+   * >create Hammurabi
+   * >set aifill 5
+   * Now we are sure that all AI-players will use historical class
+   */
+
+  common_class = find_common_class();
 
   /* Create and pick nation and name for AI players needed to bring the
    * total number of players to equal game.aifill
@@ -1634,7 +1707,7 @@
   }
 
   for(;game.nplayers < game.aifill;) {
-    nation = mark_nation_as_used(nations_avail[myrand(num_nations_avail)]);
+    nation = mark_nation_as_used(select_random_nation(common_class));
     pick_ai_player_name(nation, player_name);
 
     old_nplayers = game.nplayers;

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