Author: sveinung
Date: Wed Mar 19 18:11:41 2014
New Revision: 24699

URL: http://svn.gna.org/viewcvs/freeciv?rev=24699&view=rev
Log:
AI: consider how a building affects a city as a potential action target

Take any significant change in what actions a city can be the target of into
account when deciding how much the city should value a building. To be seen as
significant the change must be between never possible and sometimes possible.

All actions that currently are controlled by action enablers are negative for
the target city. The value of being a target of an action is therefore given as
negative utility. ("How bad is it?")

The negative utility of being a potential victim of Incite City is taken from
the No_Incite effect. The negative utility of being a potential victim of the
other actions are set to be low (max 10) but with enough granularity to make the
AI prioritize what actions to block.

See patch #4622

Modified:
    trunk/ai/default/aicity.c
    trunk/common/actions.c

Modified: trunk/ai/default/aicity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aicity.c?rev=24699&r1=24698&r2=24699&view=diff
==============================================================================
--- trunk/ai/default/aicity.c   (original)
+++ trunk/ai/default/aicity.c   Wed Mar 19 18:11:41 2014
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/**********************************************************************
  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@
 #include "registry.h"
 
 /* common */
+#include "actions.h"
 #include "game.h"
 #include "government.h"
 #include "specialist.h"
@@ -1088,6 +1089,55 @@
   }
 
   return v;
+}
+
+/**************************************************************************
+  How undesirable for the owner of a particular city is the fact that it
+  can be a target of a particular action?
+
+  The negative utility (how undesirable it is) is given as a positive
+  number. If it is desirable to be the target of an action make the
+  negative utility a negative number since double negative is positive.
+
+  Examples:
+   * action_target_neg_util(Add to population) = -50
+   * action_target_neg_util(Subtract from population) = 50
+**************************************************************************/
+static int action_target_neg_util(int action_id,
+                                  const struct city *pcity)
+{
+  fc_assert_ret_val_msg(action_get_target_kind(action_id) == ATK_CITY,
+                        0, "Action not aimed at cities");
+
+  switch (action_id) {
+  case ACTION_SPY_INCITE_CITY:
+    /* Copied from the evaluation of the No_Incite effect */
+    return MAX((game.server.diplchance * 2
+                - game.server.incite_total_factor) / 2
+               - game.server.incite_improvement_factor * 5
+               - game.server.incite_unit_factor * 5, 0);
+
+  /* Bad for the city */
+  case ACTION_SPY_POISON:
+  case ACTION_SPY_SABOTAGE_CITY:
+  case ACTION_SPY_TARGETED_SABOTAGE_CITY:
+    /* TODO: Individual and well balanced values */
+    return 10;
+
+  /* Good for an enemy */
+  case ACTION_SPY_STEAL_TECH:
+  case ACTION_SPY_TARGETED_STEAL_TECH:
+    /* TODO: Individual and well balanced values */
+    return 8;
+
+  /* Could be worse */
+  case ACTION_ESTABLISH_EMBASSY:
+  case ACTION_SPY_INVESTIGATE_CITY:
+    /* TODO: Individual and well balanced values */
+    return 1;
+  }
+
+  return 0;
 }
 
 /**************************************************************************
@@ -1781,7 +1831,7 @@
 **************************************************************************/
 static void adjust_improvement_wants_by_effects(struct ai_type *ait,
                                                 struct player *pplayer,
-                                                struct city *pcity, 
+                                                struct city *pcity,
                                                 struct impr_type *pimprove,
                                                 const bool already)
 {
@@ -1939,6 +1989,75 @@
 
     tech_vector_free(&needed_techs);
   } effect_list_iterate_end;
+
+  /* Can the city be the target of an action? */
+  action_iterate (action_id) {
+    bool isPossible;
+    bool willBePossible;
+    int act_neg_util;
+
+    /* Is the action relevant? */
+    if (action_get_target_kind(action_id) != ATK_CITY) {
+      continue;
+    }
+
+    /* Is is possible to do the action to the city right now?
+     *
+     * (DiplRel requirements are ignored since actor_player is NULL) */
+    isPossible = is_action_possible_on_city(action_id, NULL, pcity);
+
+    /* Will it be possible to do the action to the city if the building is
+     * built? */
+    /* TODO: Support caring about ranges if the price is acceptable.
+     * The price: Must look at all action enablers. */
+    action_enabler_list_iterate(action_enablers_for_action(action_id),
+                                enabler) {
+      bool active = TRUE;
+
+      requirement_vector_iterate(&(enabler->target_reqs), preq) {
+        if (VUT_IMPROVEMENT == preq->source.kind
+            && preq->source.value.building == pimprove) {
+          /* Pretend the building is there */
+          if (preq->present) {
+            continue;
+          } else {
+            active = FALSE;
+            break;
+          }
+        }
+
+        if (!is_req_active(pplayer, NULL, pcity, pimprove,
+                           city_tile(pcity), NULL, NULL, NULL, preq,
+                           RPT_POSSIBLE)) {
+          active = FALSE;
+          break;
+        }
+      } requirement_vector_iterate_end;
+
+      if (active) {
+        willBePossible = TRUE;
+        /* One enabler is enough */
+        break;
+      }
+    } action_enabler_list_iterate_end;
+
+    /* Will the building significantly change the ability to target
+     * the city? */
+    if (isPossible == willBePossible) {
+      continue;
+    }
+
+    /* How undersireable is it that the city may be a target? */
+    act_neg_util = action_target_neg_util(action_id, pcity);
+
+    /* Consider the utility of being a potential target.
+     * Remember: act_util is the negative utility of being a target. */
+    if (willBePossible) {
+      v -= act_neg_util;
+    } else {
+      v += act_neg_util;
+    }
+  } action_iterate_end;
 
   if (already) {
     /* Discourage research of the technology that would make this building

Modified: trunk/common/actions.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.c?rev=24699&r1=24698&r2=24699&view=diff
==============================================================================
--- trunk/common/actions.c      (original)
+++ trunk/common/actions.c      Wed Mar 19 18:11:41 2014
@@ -736,7 +736,7 @@
     if (are_reqs_active(target_player, actor_player, target_city,
                         target_building, target_tile, target_unittype,
                         target_output, target_specialist,
-                        &enabler->target_reqs, RPT_CERTAIN)) {
+                        &enabler->target_reqs, RPT_POSSIBLE)) {
       return TRUE;
     }
   } action_enabler_list_iterate_end;


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to