Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2003:
[Freeciv-Dev] Re: (PR#6305) New server command: debug
Home

[Freeciv-Dev] Re: (PR#6305) New server command: debug

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#6305) New server command: debug
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Sat, 27 Sep 2003 16:55:29 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Attached patch implements PLAYER_LOG and changes a lot of freelogs in
advdiplomacy.c into those. Makes debugging the AI diplomacy code a lot
easier. I also found one (very small) mistake: The timer was often set
almost the same as the countdown. Fix in this patch.

  - Per

Index: ai/advdiplomacy.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advdiplomacy.c,v
retrieving revision 1.7
diff -u -r1.7 advdiplomacy.c
--- ai/advdiplomacy.c   22 Sep 2003 20:25:56 -0000      1.7
+++ ai/advdiplomacy.c   27 Sep 2003 23:51:39 -0000
@@ -44,6 +44,7 @@
 #include "settlers.h"  /* amortize */
 
 #include "aidata.h"
+#include "ailog.h"
 #include "aitools.h"
 #include "advmilitary.h"
 
@@ -511,7 +512,7 @@
 
     i = MIN(i, ai->diplomacy.love_incr * 150);
     adip->love += i;
-    freelog(LOG_DIPL2, "%s's gift to %s increased love by %d",
+    PLAYER_LOG(LOG_DIPL2, pplayer, ai, "%s's gift to %s increased love by %d",
             aplayer->name, pplayer->name, i);
   }
 }
@@ -659,17 +660,17 @@
         && !adip->at_war_with_ally
         && adip->ally_patience >= 0) {
       adip->love += ai->diplomacy.love_incr;
-      freelog(LOG_DEBUG, "(%s ai diplo) Increased love for %s (now %d)",
-              pplayer->name, aplayer->name, adip->love);
+      PLAYER_LOG(LOG_DEBUG, pplayer, ai, "Increased love for %s (now %d)",
+                 aplayer->name, adip->love);
     } else if (pplayer->diplstates[aplayer->player_no].type == DS_WAR) {
       adip->love -= ai->diplomacy.love_incr;
-      freelog(LOG_DEBUG, "(%s ai diplo) Reduced love for %s (now %d) ",
-              pplayer->name, aplayer->name, adip->love);
+      PLAYER_LOG(LOG_DEBUG, pplayer, ai, "Reduced love for %s (now %d) ",
+                 aplayer->name, adip->love);
     } else if (pplayer->diplstates[a].has_reason_to_cancel) {
       /* Provoked in time of peace */
       if (adip->love > 0) {
-        freelog(LOG_DEBUG, "(%s ai diplo) Provoked by %s! Love halved (was 
%d)",
-                pplayer->name, aplayer->name, adip->love);
+        PLAYER_LOG(LOG_DEBUG, pplayer, ai, "Provoked by %s! Love halved "
+                   "(was %d)", aplayer->name, adip->love);
         adip->love /= 2;
       }
       adip->love -= ai->diplomacy.love_incr;
@@ -681,8 +682,8 @@
 
   /* Stop war against a dead player */
   if (ai->diplomacy.target && !ai->diplomacy.target->is_alive) {
-    freelog(LOG_DIPL2, "(%s ai diplo) Target player %s is dead! Victory!",
-            pplayer->name, ai->diplomacy.target->name);
+    PLAYER_LOG(LOG_DIPL2, pplayer, ai, "Target player %s is dead! Victory!",
+               ai->diplomacy.target->name);
     ai->diplomacy.timer = 0;
     ai->diplomacy.countdown = 0;
     ai->diplomacy.target = NULL;
@@ -743,9 +744,8 @@
     if (pplayers_non_attack(pplayer, aplayer)) {
       war_desire[aplayer->player_no] /= 2;
     }
-    freelog(LOG_DEBUG, "(%s ai diplo) Against %s we have war desire "
-            "%d ", pplayer->name, aplayer->name,
-            war_desire[aplayer->player_no]);
+    PLAYER_LOG(LOG_DEBUG, pplayer, ai, "Against %s we have war desire "
+            "%d ", aplayer->name, war_desire[aplayer->player_no]);
 
     /* Find best target */
     if (war_desire[aplayer->player_no] > best_desire) {
@@ -755,17 +755,15 @@
   } players_iterate_end;
 
   if (!target) {
-    freelog(LOG_DEBUG, "(%s ai diplo) Found no target.", pplayer->name);
+    PLAYER_LOG(LOG_DEBUG, pplayer, ai, "Found no target.");
     ai->diplomacy.target = NULL;
     return;
   }
 
   /* Switch to target */
   if (target != ai->diplomacy.target) {
-    freelog(LOG_DIPL, "(%s ai diplo) Setting target to %s",
-            pplayer->name, target->name);
+    PLAYER_LOG(LOG_DIPL, pplayer, ai, "Setting target to %s", target->name);
     ai->diplomacy.target = target;
-    ai->diplomacy.timer = myrand(6) + 6; /* Don't reevaluate too often. */
     if (ai->diplomacy.strategy == WIN_CAPITAL) {
       ai->diplomacy.countdown = 1; /* Quickly!! */
     } else if (pplayer->diplstates[target->player_no].has_reason_to_cancel > 
1) {
@@ -776,6 +774,8 @@
     } else {
       ai->diplomacy.countdown = 6; /* Take the time we need - WAG */
     }
+    /* Don't reevaluate too often. */
+    ai->diplomacy.timer = myrand(6) + 6 + ai->diplomacy.countdown;
     players_iterate(aplayer) {
       ai->diplomacy.player_intel[aplayer->player_no].ally_patience = 0;
     } players_iterate_end;
@@ -855,8 +855,8 @@
     if (ai->diplomacy.acceptable_reputation > aplayer->reputation
         && ai->diplomacy.player_intel[aplayer->player_no].love < 0
         && pplayer->diplstates[aplayer->player_no].has_reason_to_cancel >= 2) {
-      freelog(LOG_DIPL2, "(%s ai diplo) Declaring war on %s in revenge",
-              pplayer->name, target->name);
+      PLAYER_LOG(LOG_DIPL2, pplayer, ai, "Declaring war on %s in revenge",
+                 target->name);
       notify(target, _("*%s (AI)* I will NOT accept such behaviour! This "
              "means WAR!"), pplayer->name);
       ai_go_to_war(pplayer, ai, aplayer);
@@ -909,11 +909,14 @@
 
   /*** Declare war - against target ***/
 
+  if (target && pplayers_at_war(pplayer, target)) {
+    ai->diplomacy.countdown = 0; /* cosmetic */
+  }
   if (target && !pplayers_at_war(pplayer, target)
-      && ai->diplomacy.countdown-- <= 0) {
+      && ai->diplomacy.countdown <= 0) {
     if (pplayers_allied(pplayer, target)) {
-      freelog(LOG_ERROR, "%s: Went to war against %s, who is an ally!",
-              pplayer->name, target->name); /* Oh, my. */
+      PLAYER_LOG(LOG_ERROR, pplayer, ai, "Went to war against %s, who is "
+                 "an ally!", target->name); /* Oh, my. */
     }
     if (pplayer->diplstates[target->player_no].has_reason_to_cancel > 0) {
       /* We have good reason */
@@ -1015,8 +1018,8 @@
         break;
       }
       if (target && pplayer->ai.control) {
-        freelog(LOG_DIPL2, "%s: Ally %s not at war with enemy %s "
-                "(patience %d, %s %s)", pplayer->name, aplayer->name, 
+        PLAYER_LOG(LOG_DIPL2, pplayer, ai, "Ally %s not at war with enemy %s "
+                "(patience %d, %s %s)", aplayer->name, 
                 target->name, adip->ally_patience, adip->at_war_with_ally
                 ? "war_with_ally" : "", adip->is_allied_with_ally ? 
                 "allied_with_ally" : "");
@@ -1038,8 +1041,8 @@
                  "alliance, and yet you remain at peace with our mortal "
                  "enemy, %s! This is unacceptable, our alliance is no "
                  "more!"), pplayer->name, target->name);
-          freelog(LOG_DIPL2, "(%s ai diplo) breaking useless alliance with %s",
-                  pplayer->name, aplayer->name);
+          PLAYER_LOG(LOG_DIPL2, pplayer, ai, "breaking useless alliance with ",
+                     "%s", aplayer->name);
           packet.id = aplayer->player_no;
           packet.value1 = CLAUSE_ALLIANCE;
           handle_player_cancel_pact(pplayer, &packet); /* to peace */
Index: ai/ailog.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/ailog.c,v
retrieving revision 1.7
diff -u -r1.7 ailog.c
--- ai/ailog.c  17 Jul 2003 18:56:50 -0000      1.7
+++ ai/ailog.c  27 Sep 2003 23:51:39 -0000
@@ -25,9 +25,51 @@
 
 #include "gotohand.h"
 
+#include "aidata.h"
 #include "ailog.h"
 
 /* General AI logging functions */
+
+/**************************************************************************
+  Log player messages, they will appear like this
+    2: 
+**************************************************************************/
+void PLAYER_LOG(int level, struct player *pplayer, struct ai_data *ai, 
+                const char *msg, ...)
+{
+  char targetbuffer[250];
+  char buffer[500];
+  char buffer2[500];
+  va_list ap;
+  int minlevel = MIN(LOGLEVEL_CITY, level);
+
+  if (pplayer->debug) {
+    minlevel = LOG_NORMAL;
+  } else if (minlevel > fc_log_level) {
+    return;
+  }
+
+  if (ai->diplomacy.target) {
+    my_snprintf(targetbuffer, sizeof(targetbuffer), "[ti%d co%d lo%d %s] ",
+                ai->diplomacy.timer, ai->diplomacy.countdown,
+              ai->diplomacy.player_intel[ai->diplomacy.target->player_no].love,
+                ai->diplomacy.target->name);
+  }
+  my_snprintf(buffer, sizeof(buffer), "%s %s%s%s ", pplayer->name,
+              ai->diplomacy.target ? targetbuffer : "",
+              ai->diplomacy.spacerace_leader &&
+              ai->diplomacy.spacerace_leader->player_no == pplayer->player_no 
? 
+                "(spacelead) " : "",
+              ai->diplomacy.alliance_leader->player_no == pplayer->player_no ?
+                "(*)" : "");
+
+  va_start(ap, msg);
+  my_vsnprintf(buffer2, sizeof(buffer2), msg, ap);
+  va_end(ap);
+
+  cat_snprintf(buffer, sizeof(buffer), buffer2);
+  freelog(minlevel, buffer);
+}
 
 /**************************************************************************
   Log city messages, they will appear like this
Index: ai/ailog.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/ailog.h,v
retrieving revision 1.3
diff -u -r1.3 ailog.h
--- ai/ailog.h  15 Nov 2002 22:15:01 -0000      1.3
+++ ai/ailog.h  27 Sep 2003 23:51:39 -0000
@@ -28,6 +28,8 @@
 #define LOGLEVEL_CITY LOG_DEBUG
 #define LOGLEVEL_BUILD LOG_DEBUG
 
+void PLAYER_LOG(int level, struct player *pplayer, struct ai_data *ai,
+                const char *msg, ...);
 void CITY_LOG(int level, struct city *pcity, const char *msg, ...);
 void UNIT_LOG(int level, struct unit *punit, const char *msg, ...);
 void GOTO_LOG(int level, struct unit *punit, enum goto_result result,

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