Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2005:
[Freeciv-Dev] (PR#12647) AI should select nations from the same group as
Home

[Freeciv-Dev] (PR#12647) AI should select nations from the same group as

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12647) AI should select nations from the same group as user did
From: "Mateusz Stefek" <mstefek@xxxxxxxxx>
Date: Fri, 25 Mar 2005 11:20:50 -0800
Reply-to: bugs@xxxxxxxxxxx

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

This patch brings back the feature that the AI chooses nations which
goes well together with users' ones. It is much better now.
Nation groups can be marked as those we like having their members
together in a game.
I choosed these: ancient, mediaval, modern and fictional.
--
mateusz
? aicity.c
? shared.c
? data/ft
? po/pl.back
Index: common/nation.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.c,v
retrieving revision 1.43
diff -u -r1.43 nation.c
--- common/nation.c     25 Mar 2005 18:03:16 -0000      1.43
+++ common/nation.c     25 Mar 2005 19:12:47 -0000
@@ -446,6 +446,7 @@
     die("Too many groups of nations");
   }
   sz_strlcpy(nation_groups[i].name, name);
+  nation_groups[i].match = 0;
   return &nation_groups[num_nation_groups++];
 }
 
Index: common/nation.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.h,v
retrieving revision 1.36
diff -u -r1.36 nation.h
--- common/nation.h     25 Mar 2005 18:03:16 -0000      1.36
+++ common/nation.h     25 Mar 2005 19:12:47 -0000
@@ -69,7 +69,9 @@
 
 struct nation_group {
   char name[MAX_LEN_NAME];
-  /* ... */
+  
+  /* How much the AI will try to select a nation in the same group */
+  int match;
 };
 
 struct nation_type {
Index: data/default/nations.ruleset
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/default/nations.ruleset,v
retrieving revision 1.73
diff -u -r1.73 nations.ruleset
--- data/default/nations.ruleset        23 Jan 2005 02:11:09 -0000      1.73
+++ data/default/nations.ruleset        25 Mar 2005 19:13:01 -0000
@@ -11,6 +11,23 @@
 description="Default nations data for Freeciv"
 options="1.9"
 
+; Descriptions of nation groups
+; name                 = name of the group
+; match                = How much the AI will try to select a nation in the 
same
+;                 group
+[ngroup_ancient]
+name="Ancient"
+match=1
+[ngroup_mediaval]
+name="Mediaval"
+match=1
+[ngroup_modern]
+name="Modern"
+match=1
+[ngroup_fictional]
+name="Fictional"
+match=2
+
 ; Below: nations data in sections [nation_*] for
 ; all nations available. If you want to have more
 ; nations just add one at the end.
Index: server/ruleset.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/ruleset.c,v
retrieving revision 1.236
diff -u -r1.236 ruleset.c
--- server/ruleset.c    25 Mar 2005 18:03:17 -0000      1.236
+++ server/ruleset.c    25 Mar 2005 19:13:28 -0000
@@ -1985,12 +1985,21 @@
   char *bad_leader, *g;
   struct nation_type *pl;
   struct government *gov;
-  int dim, i, j, k, nval;
+  int dim, i, j, k, nval, numgroups;
   char temp_name[MAX_LEN_NAME];
   char **leaders, **sec, **civilwar_nations, **groups;
+  char* name;
   const char *filename = secfile_filename(file);
 
   (void) check_ruleset_capabilities(file, "+1.9", filename);
+  
+  groups = secfile_get_secnames_prefix(file, "ngroup", &numgroups);
+  for (i = 0; i < numgroups; i++) {
+    struct nation_group* group;
+    name = secfile_lookup_str(file, "%s.name", groups[i]);
+    group = add_new_nation_group(name);
+    group->match = secfile_lookup_int_default(file, 0, "%s.match", groups[i]);
+  }
 
   sec = secfile_get_secnames_prefix(file, "nation", &nval);
 
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.238
diff -u -r1.238 srv_main.c
--- server/srv_main.c   25 Mar 2005 18:03:17 -0000      1.238
+++ server/srv_main.c   25 Mar 2005 19:13:39 -0000
@@ -1240,6 +1240,22 @@
   }
 }
 
+
+/**************************************************************************
+  Returns how much two nations looks good in the same game
+**************************************************************************/
+static int nations_match(struct nation_type* n1, struct nation_type* n2)
+{
+  int i;
+  int sum = 0;
+  for (i = 0; i < n1->num_groups; i++) {
+    if (nation_in_group(n2, n1->groups[i]->name)) {
+      sum += n1->groups[i]->match;
+    }
+  }
+  return sum;
+}
+
 /**************************************************************************
   Select a random available nation.
 **************************************************************************/
@@ -1247,17 +1263,36 @@
 {
   Nation_Type_id i, available[game.playable_nation_count];
   int count = 0;
+  int V[game.playable_nation_count];
+  int sum = 0;
+  int x;
   
   /* Determine which nations are available. */
   for (i = 0; i < game.playable_nation_count; i++) {
     if (nations_available[i]) {
       available[count] = i;
+      
+      /* Increase the probablity of selecting those which have higher
+       * values of nations_match() */
+      players_iterate(aplayer) {
+        if (aplayer->nation == NO_NATION_SELECTED) {
+         continue;
+       }
+       sum+= nations_match(get_nation_by_idx(aplayer->nation),
+                           get_nation_by_idx(i)) * 100;
+      } players_iterate_end;
+      sum++;
+      V[count] = sum;
       count++;
     }
   }
-  
-  /* Then pick one. */
-  return available[myrand(count)];
+
+  /* Then pick one */  
+  x = myrand(sum);
+  for (i = 0; i < count; i++) {
+    if (V[i] >= x) break;
+  }
+  return available[i];
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12647) AI should select nations from the same group as user did, Mateusz Stefek <=