(PR#10170) MAX_AI_LOVE

"Mateusz Stefek" <mstefek-IjDXvh/[email protected]>
Newsgroups gmane.games.freeciv.ai
Message-ID <[email protected]>
<URL: http://rt.freeciv.org/Ticket/Display.html?id=10170 >

This patch adds new constant: MAX_AI_LOVE
It also simplifies greed() function - It was some kind of
spline-aproximation of x^2/50, new we use exactly x^2/50.
--
mateusz
max_ai_love.diff (text/x-patch, 6.9 KB)
? freeciv.spec
? ai/test
? client/civgame-3950.sav.gz
? server/civgame-3700.sav.gz
? server/civgame-3900.sav.gz
? server/civgame-3950.sav.gz
? server/freeciv_user_database
Index: ai/advdiplomacy.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdiplomacy.c,v
retrieving revision 1.42
diff -u -r1.42 advdiplomacy.c
--- ai/advdiplomacy.c	17 Sep 2004 08:35:14 -0000	1.42
+++ ai/advdiplomacy.c	17 Sep 2004 20:44:41 -0000
@@ -100,28 +100,17 @@
   lust for gold.
 ***********************************************************************/
 static int greed(int missing_love)
-#define NUM_BANDS 5
 {
-  int band_incr[NUM_BANDS] = {50, 50, 100, 200, 300};
-  int band_rate[NUM_BANDS+1] = {1, 3, 5, 10, 25, 50};
-  int greed = 0;
-  int i = 0;
-  int n_love = -missing_love;
-
-  while(i < NUM_BANDS && n_love > 0) {
-    int band_love = MIN(band_incr[i], n_love);
-
-    greed += band_love * band_rate[i];
-    n_love -= band_incr[i];
-    i++;
-  }
-
-  if (n_love > 0) {
-    greed += n_love * band_rate[NUM_BANDS];
+  if (missing_love > 0) {
+    return 0;
+  } else {
+    /* Don't change the operation order here.
+     * We do not want integer overflows */
+    return ((missing_love * MAX_AI_LOVE) / 1000) * 
+           ((missing_love * MAX_AI_LOVE) / 1000) /
+	   50;
   }
-  return (-greed);
 }
-#undef NUM_BANDS
 
 /********************************************************************** 
   How much is a tech worth to player measured in gold
@@ -175,7 +164,7 @@
   return (ai1->diplomacy.target != player2 && 
           (player1 == ai1->diplomacy.alliance_leader ||
            !pplayers_at_war(player2, ai1->diplomacy.alliance_leader)) &&
-	  player1->ai.love[player2->player_no] > -400 &&
+	  player1->ai.love[player2->player_no] > - (MAX_AI_LOVE * 4 / 10)  &&
 	  (ai1->diplomacy.target == NULL || 
 	   !pplayers_allied(ai1->diplomacy.target, player2)));
 }
@@ -652,7 +641,7 @@
   /* Modify by love. Increase the divisor to make ai go to war earlier */
   kill_desire -= MAX(0, kill_desire 
                         * pplayer->ai.love[aplayer->player_no] 
-                        / 2000);
+                        / (2 * MAX_AI_LOVE));
 
   /* Amortize by distance */
   return amortize(kill_desire, adip->distance);
@@ -720,7 +709,7 @@
       if (ai->diplomacy.target != aplayer && 
           pplayer->ai.love[aplayer->player_no] < 0) {
         /* Give him a better chance for a cease fire */
-        pplayer->ai.love[aplayer->player_no] += 30;
+        pplayer->ai.love[aplayer->player_no] += (MAX_AI_LOVE) * 3 / 100;
       }
       PLAYER_LOG(LOG_DEBUG, pplayer, ai, "Reduced love for %s (now %d) ",
                  aplayer->name, pplayer->ai.love[aplayer->player_no]);
@@ -737,18 +726,19 @@
     /* Reduce love by number of units in our territory.
      * AI is so naive, that we have to count it even if players are allied */
     pplayer->ai.love[aplayer->player_no] -=
-      MIN(player_in_territory(pplayer, aplayer) * 10,
+      MIN(player_in_territory(pplayer, aplayer) * (MAX_AI_LOVE / 100),
           pplayers_allied(aplayer, pplayer) ? 
-	    ai->diplomacy.love_incr - 1 : 500);
+	    ai->diplomacy.love_incr - 1 : (MAX_AI_LOVE / 2));
 	  
     /* Massage our numbers to keep love and its opposite on the ground. 
      * Gravitate towards zero. */
     pplayer->ai.love[aplayer->player_no] -= 
        (pplayer->ai.love[aplayer->player_no] * ai->diplomacy.love_coeff / 100);
        
-    /* ai love should always be in range [-1000..1000] */
+    /* ai love should always be in range [-MAX_AI_LOVE..MAX_AI_LOVE] */
     pplayer->ai.love[aplayer->player_no] = 
-      MAX(-1000, MIN(1000, pplayer->ai.love[aplayer->player_no]));
+      MAX(-MAX_AI_LOVE,
+          MIN(MAX_AI_LOVE, pplayer->ai.love[aplayer->player_no]));
   } players_iterate_end;
 
   /* Stop war against a dead player */
Index: ai/aidata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.c,v
retrieving revision 1.39
diff -u -r1.39 aidata.c
--- ai/aidata.c	14 Sep 2004 09:12:21 -0000	1.39
+++ ai/aidata.c	17 Sep 2004 20:44:44 -0000
@@ -442,9 +442,9 @@
   ai->diplomacy.timer = 0;
   ai->diplomacy.countdown = 0;
   ai->diplomacy.love_coeff = 4; /* 4% */
-  ai->diplomacy.love_incr = 40;
-  ai->diplomacy.req_love_for_peace = 80;
-  ai->diplomacy.req_love_for_alliance = 160;
+  ai->diplomacy.love_incr = MAX_AI_LOVE * 4 / 100;
+  ai->diplomacy.req_love_for_peace = MAX_AI_LOVE * 8 / 100;
+  ai->diplomacy.req_love_for_alliance = MAX_AI_LOVE * 16 / 100;
   ai->diplomacy.req_love_for_ceasefire = 0;
   ai->diplomacy.alliance_leader = pplayer;
 
Index: common/player.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/player.c,v
retrieving revision 1.155
diff -u -r1.155 player.c
--- common/player.c	17 Sep 2004 08:35:14 -0000	1.155
+++ common/player.c	17 Sep 2004 20:44:50 -0000
@@ -566,28 +566,28 @@
 **************************************************************************/
 const char *love_text(const int love)
 {
-  if (love <= -950) {
+  if (love <= - MAX_AI_LOVE * 90 / 100) {
     return Q_("?attitude:Genocidal");
-  } else if (love >= -949 && love <= -701) {
+  } else if (love <= - MAX_AI_LOVE * 70 / 100) {
     return Q_("?attitude:Belligerent");
-  } else if (love >= -700 && love <= -501) {
+  } else if (love <= - MAX_AI_LOVE * 50 / 100) {
     return Q_("?attitude:Hostile");
-  } else if (love >= -500 && love <= -251) {
+  } else if (love <= - MAX_AI_LOVE * 25 / 100) {
     return Q_("?attitude:Uncooperative");
-  } else if (love >= -250 && love <= -101) {
+  } else if (love <= - MAX_AI_LOVE * 10 / 100) {
     return Q_("?attitude:Uneasy");
-  } else if (love >= -100 && love <= 100) {
+  } else if (love <= MAX_AI_LOVE * 10 / 100) {
     return Q_("?attitude:Neutral");
-  } else if (love >= 101 && love <= 250) {
+  } else if (love <= MAX_AI_LOVE * 25 / 100) {
     return Q_("?attitude:Respectful");
-  } else if (love >= 251 && love <= 500) {
+  } else if (love <= MAX_AI_LOVE * 50 / 100) {
     return Q_("?attitude:Helpful");
-  } else if (love >= 501 && love <= 700) {
+  } else if (love <= MAX_AI_LOVE * 70 / 100) {
     return Q_("?attitude:Enthusiastic");
-  } else if (love >= 701 && love <= 949) {
+  } else if (love <= MAX_AI_LOVE * 90 / 100) {
     return Q_("?attitude:Admiring");
   } else {
-    assert(love >= 950);
+    assert(love > MAX_AI_LOVE * 90 / 100);
     return Q_("?attitude:Worshipful");
   }
 }
Index: common/player.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/player.h,v
retrieving revision 1.126
diff -u -r1.126 player.h
--- common/player.h	17 Sep 2004 08:35:14 -0000	1.126
+++ common/player.h	17 Sep 2004 20:44:51 -0000
@@ -303,4 +303,7 @@
   }                                                                           \
 }
 
+/* ai love values should be in range [-MAX_AI_LOVE..MAX_AI_LOVE] */
+#define MAX_AI_LOVE 1000
+
 #endif  /* FC__PLAYER_H */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.