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

In June, I wrote:
> Madeline Book writes:
> > and for 2.2+ to expand the effect system to allow support for
> > auto-irrigation and auto-farmland of the city center.
> 
> I'll see if I can find time to have a go at this.

I've finally found a bit of time. Draft attached. It continues to be a
can of worms.

I've realised I need _another_ universal type which will allow me to
restrict the auto-farmland effect to irrigable terrains. Having a
source which allows the effects system to use this part of the terrain
ruleset seems like a generally useful thing, though, so I've done
that. The result (VUT_TERRAINALTER) is somewhat clumsy -- its name,
for one thing -- but appears to work. Suggestions for improvement
welcome.

The universal I've added for target-city-is-on-target-tile
(VUT_CITYCENTER), while suitable for the job in hand, feels a bit wrong
-- it has no parameters and only one valid range. I can't think of any
way to generalise it, though. I've wondered about instead extending the
existing VUT_MINSIZE to allow use in tile output contexts (here we'd use
MinSize=1); would that be unnacceptably gross?

On the plus side, once all the bits are in place, it becomes easier to
move various bits of hard-coded stuff from city_tile_output() to the
effects system, if that's desirable:

 - auto-irrigation of city centres (requires new effect
   "Apply_Irrigation_Bonus" or similar)

 - min_city_center_output[] (requires new effect "Output_Min_Tile")

(I don't intend to actually take these steps yet, however.)

Testing: I've done some ad-hoc testing to check it works as desired
and haven't spotted any bad side effects. I also did some
before-and-after autogame testing (with the new bit of effects.ruleset
commented out), which diverged after 107 turns for no reason I can
work out (it's not around the discovery of Refrigeration or anything
like that). 

I expect this will need some beating up before it can be committed. I
should be able to respond to comments for the next two weeks.

Index: doc/README.effects
===================================================================
--- doc/README.effects  (revision 15124)
+++ doc/README.effects  (working copy)
@@ -40,9 +40,11 @@
 
 A requirement type is the type of the requirement and can be one of "None" 
 (default), "Tech", "Gov", "Building", "Special", "Terrain", "UnitType", 
-"UnitFlag", "UnitClass", "Nation", "OutputType", "MinSize", "AI" and
-"TerrainClass". MinSize is the minimum size of a city required. AI is ai
-player difficulty level. TerrainClass is either "Land" or "Oceanic".
+"UnitFlag", "UnitClass", "Nation", "OutputType", "MinSize", "AI",
+"TerrainClass", "TerrainAlter", or "CityCenter". MinSize is the minimum
+size of a city required. AI is ai player difficulty level. TerrainClass
+is either "Land" or "Oceanic". CityCenter takes no argument and is true
+if the target city is on the target tile.
 
 Effect types 
 ============
Index: server/cityturn.c
===================================================================
--- server/cityturn.c   (revision 15124)
+++ server/cityturn.c   (working copy)
@@ -1046,6 +1046,8 @@
            case VUT_UCFLAG:
            case VUT_OTYPE:
            case VUT_SPECIALIST:
+           case VUT_TERRAINALTER: /* XXX could do this in principle */
+           case VUT_CITYCENTER:
              /* Will only happen with a bogus ruleset. */
              freelog(LOG_ERROR, "worklist_change_build_target()"
                      " has bogus preq");
Index: server/ruleset.c
===================================================================
--- server/ruleset.c    (revision 15124)
+++ server/ruleset.c    (working copy)
@@ -3733,6 +3733,8 @@
      case VUT_MINSIZE: /* Breaks nothing, but has no sense either */
      case VUT_AI_LEVEL:
      case VUT_TERRAINCLASS:
+     case VUT_TERRAINALTER: /* Local range only */
+     case VUT_CITYCENTER:
        /* There can be only one requirement of these types (with current
         * range limitations)
         * Requirements might be identical, but we consider multiple
Index: data/default/effects.ruleset
===================================================================
--- data/default/effects.ruleset        (revision 15124)
+++ data/default/effects.ruleset        (working copy)
@@ -1543,6 +1543,17 @@
       "OutputType", "Food", "Local"
     }
 
+[effect_supermarket_2]
+name   = "Output_Per_Tile"
+value  = 50
+reqs   =
+    { "type", "name", "range"
+      "CityCenter", "", "Local"
+      "TerrainAlter", "CanIrrigate", "Local"
+      "Building", "Supermarket", "City"
+      "OutputType", "Food", "Local"
+    }
+
 [effect_temple]
 name   = "Make_Content"
 value  = 1
Index: common/city.c
===================================================================
--- common/city.c       (revision 15124)
+++ common/city.c       (working copy)
@@ -710,7 +710,6 @@
 int city_tile_output(const struct city *pcity, const struct tile *ptile,
                     bool is_celebrating, Output_type_id otype)
 {
-  struct tile tile;
   int prod;
   struct terrain *pterrain = tile_terrain(ptile);
 
@@ -727,38 +726,27 @@
     prod += tile_resource(ptile)->output[otype];
   }
 
-  /* create dummy tile which has the city center bonuses. */
-  tile.terrain = pterrain;
-  tile.special = tile_specials(ptile);
-
-  if (NULL != pcity
-      && is_city_center(pcity, ptile)
-      && pterrain == pterrain->irrigation_result
-      && terrain_control.may_irrigate) {
-    /* The center tile is auto-irrigated. */
-    tile_set_special(&tile, S_IRRIGATION);
-
-    if (player_knows_techs_with_flag(city_owner(pcity), TF_FARMLAND)) {
-      tile_set_special(&tile, S_FARMLAND);
-    }
-  }
-
   switch (otype) {
   case O_SHIELD:
-    if (contains_special(tile.special, S_MINE)) {
+    if (tile_has_special(ptile, S_MINE)) {
       prod += pterrain->mining_shield_incr;
     }
     break;
   case O_FOOD:
-    if (contains_special(tile.special, S_IRRIGATION)) {
+    /* The city center tile is auto-irrigated. */
+    if (tile_has_special(ptile, S_IRRIGATION)
+        || (NULL != pcity
+            && is_city_center(pcity, ptile)
+            && pterrain == pterrain->irrigation_result
+            && terrain_control.may_irrigate)) {
       prod += pterrain->irrigation_food_incr;
     }
     break;
   case O_TRADE:
-    if (contains_special(tile.special, S_RIVER) && !is_ocean(tile.terrain)) {
+    if (tile_has_special(ptile, S_RIVER) && !is_ocean_tile(ptile)) {
       prod += terrain_control.river_trade_incr;
     }
-    if (contains_special(tile.special, S_ROAD)) {
+    if (tile_has_special(ptile, S_ROAD)) {
       prod += pterrain->road_trade_incr;
     }
     break;
@@ -769,7 +757,7 @@
     break;
   }
 
-  if (contains_special(tile.special, S_RAILROAD)) {
+  if (tile_has_special(ptile, S_RAILROAD)) {
     prod += (prod * terrain_control.rail_tile_bonus[otype]) / 100;
   }
 
@@ -799,11 +787,11 @@
     }
   }
 
-  if (contains_special(tile.special, S_POLLUTION)) {
+  if (tile_has_special(ptile, S_POLLUTION)) {
     prod -= (prod * terrain_control.pollution_tile_penalty[otype]) / 100;
   }
 
-  if (contains_special(tile.special, S_FALLOUT)) {
+  if (tile_has_special(ptile, S_FALLOUT)) {
     prod -= (prod * terrain_control.fallout_tile_penalty[otype]) / 100;
   }
 
Index: common/terrain.c
===================================================================
--- common/terrain.c    (revision 15124)
+++ common/terrain.c    (working copy)
@@ -46,6 +46,12 @@
   N_("Oceanic")
 };
 
+static const char *terrain_alteration_names[] = {
+  N_("CanIrrigate"),
+  N_("CanMine"),
+  N_("CanRoad")
+};
+
 /* T_UNKNOWN isn't allowed here. */
 #define SANITY_CHECK_TERRAIN(pterrain)                                 \
   assert((pterrain)->item_number >= 0                                  \
@@ -891,3 +897,76 @@
 
   return _(terrain_class_names[tclass]);
 }
+
+/****************************************************************************
+  Can terrain support given infrastructure?
+****************************************************************************/
+bool terrain_can_support_alteration(const struct terrain *pterrain,
+                                    enum terrain_alteration alter)
+{
+  switch (alter) {
+   case TA_CAN_IRRIGATE:
+     return (terrain_control.may_irrigate
+          && (pterrain == pterrain->irrigation_result));
+   case TA_CAN_MINE:
+     return (terrain_control.may_mine
+          && (pterrain == pterrain->mining_result));
+   case TA_CAN_ROAD:
+     return (terrain_control.may_road && (pterrain->road_time > 0));
+   case TA_LAST:
+   default:
+     break;
+  }
+
+  assert(FALSE);
+  return FALSE;
+}
+
+/****************************************************************************
+  Return the terrain alteration possibility value matching name, or TA_LAST
+  if none matches.
+****************************************************************************/
+enum terrain_alteration find_terrain_alteration_by_rule_name(const char *name)
+{
+  int i;
+
+  for (i = 0; i < TA_LAST; i++) {
+    if (0 == strcmp(terrain_alteration_names[i], name)) {
+      return i;
+    }
+  }
+
+  return TA_LAST;
+}
+
+/****************************************************************************
+  Return the (untranslated) rule name of the given terrain alteration.
+  You don't have to free the return pointer.
+****************************************************************************/
+const char *terrain_alteration_rule_name(enum terrain_alteration talter)
+{
+  if (talter < 0 || talter >= TA_LAST) {
+    return NULL;
+  }
+
+  return terrain_alteration_names[talter];
+}
+
+/****************************************************************************
+  Return the (translated) name of the infrastructure (e.g., "Irrigation").
+  You don't have to free the return pointer.
+****************************************************************************/
+const char *terrain_alteration_name_translation(enum terrain_alteration talter)
+{
+  switch(talter) {
+   case TA_CAN_IRRIGATE:
+     return special_name_translation(S_IRRIGATION);
+   case TA_CAN_MINE:
+     return special_name_translation(S_MINE);
+   case TA_CAN_ROAD:
+     return special_name_translation(S_ROAD);
+   case TA_LAST:
+   default:
+     return NULL;
+  }
+}
Index: common/terrain.h
===================================================================
--- common/terrain.h    (revision 15124)
+++ common/terrain.h    (working copy)
@@ -140,6 +140,16 @@
   MG_LAST
 };
 
+/* Types of alterations available to terrain.
+ * This enum is only used in the effects system; the relevant information
+ * is encoded in other members of the terrain structure. */
+enum terrain_alteration {
+  TA_CAN_IRRIGATE,     /* Can build irrigation without changing terrain */
+  TA_CAN_MINE,         /* Can build mine without changing terrain */
+  TA_CAN_ROAD,         /* Can build roads and/or railroads */
+  TA_LAST
+};
+
 /*
  * This struct gives data about each terrain type.  There are many ways
  * it could be extended.
@@ -302,6 +312,13 @@
                               enum terrain_class tclass);
 bool is_terrain_class_near_tile(const struct tile *ptile, enum terrain_class 
tclass);
 
+/* Functions to deal with possible terrain alterations. */
+enum terrain_alteration find_terrain_alteration_by_rule_name(const char *name);
+const char *terrain_alteration_rule_name(enum terrain_alteration talter);
+const char *terrain_alteration_name_translation(enum terrain_alteration 
talter);
+bool terrain_can_support_alteration(const struct terrain *pterrain,
+                                    enum terrain_alteration talter);
+
 /* Initialization and iteration */
 struct resource *resource_array_first(void);
 const struct resource *resource_array_last(void);
Index: common/fc_types.h
===================================================================
--- common/fc_types.h   (revision 15124)
+++ common/fc_types.h   (working copy)
@@ -241,6 +241,7 @@
   int minsize;
   Output_type_id outputtype;
   int terrainclass;                    /* enum terrain_class */
+  int terrainalter;                    /* enum terrain_alteration */
   int special;                         /* enum tile_special_type */
   int unitclassflag;                   /* enum unit_class_flag_id */
   int unitflag;                                /* enum unit_flag_id */
@@ -266,6 +267,8 @@
   VUT_MINSIZE,         /* Minimum size: at city range means city size */
   VUT_AI_LEVEL,                /* AI level of the player */
   VUT_TERRAINCLASS,    /* More generic terrain type, currently "Land" or 
"Ocean" */
+  VUT_TERRAINALTER,    /* Terrain alterations that are possible */
+  VUT_CITYCENTER,      /* Target tile is occupied by target city */
   VUT_LAST
 };
 
Index: common/requirements.c
===================================================================
--- common/requirements.c       (revision 15124)
+++ common/requirements.c       (working copy)
@@ -46,7 +46,9 @@
   "Specialist",
   "MinSize",
   "AI",
-  "TerrainClass"
+  "TerrainClass",
+  "TerrainAlter",
+  "CityCenter"
 };
 
 /* Names of requirement ranges. These must correspond to enum req_range in
@@ -200,6 +202,14 @@
       return source;
     }
     break;
+  case VUT_TERRAINALTER:
+    source.value.terrainalter = find_terrain_alteration_by_rule_name(value);
+    if (source.value.terrainalter != TA_LAST) {
+      return source;
+    }
+    break;
+  case VUT_CITYCENTER:
+    return source;
   case VUT_LAST:
   default:
     break;
@@ -291,6 +301,11 @@
   case VUT_TERRAINCLASS:
     source.value.terrainclass = value;
     return source;
+  case VUT_TERRAINALTER:
+    source.value.terrainalter = value;
+    return source;
+  case VUT_CITYCENTER:
+    return source;
   case VUT_LAST:
     return source;
   default:
@@ -352,6 +367,10 @@
     return source->value.ai_level;
   case VUT_TERRAINCLASS:
     return source->value.terrainclass;
+  case VUT_TERRAINALTER:
+    return source->value.terrainalter;
+  case VUT_CITYCENTER:
+    return 0;
   case VUT_LAST:
   default:
     break;
@@ -396,6 +415,8 @@
     case VUT_OTYPE:
     case VUT_SPECIALIST:
     case VUT_TERRAINCLASS:
+    case VUT_TERRAINALTER:
+    case VUT_CITYCENTER:
       req.range = REQ_RANGE_LOCAL;
       break;
     case VUT_MINSIZE:
@@ -448,6 +469,8 @@
   case VUT_UCFLAG:
   case VUT_OTYPE:
   case VUT_SPECIALIST:
+  case VUT_TERRAINALTER: /* XXX could in principle support ADJACENT */
+  case VUT_CITYCENTER:
     invalid = (req.range != REQ_RANGE_LOCAL);
     break;
   case VUT_NONE:
@@ -798,6 +821,35 @@
 }
 
 /****************************************************************************
+  Is there a terrain which can support the specified infrastructure
+  within range of the target?
+****************************************************************************/
+static bool is_terrain_alteration_in_range(const struct tile *target_tile,
+                                           enum req_range range, bool survives,
+                                           enum terrain_alteration alteration)
+{
+  if (!target_tile) {
+    return FALSE;
+  }
+
+  switch (range) {
+  case REQ_RANGE_LOCAL:
+    return terrain_can_support_alteration(tile_terrain(target_tile),
+                                          alteration);
+  case REQ_RANGE_ADJACENT: /* XXX Could in principle support ADJACENT. */
+  case REQ_RANGE_CITY:
+  case REQ_RANGE_CONTINENT:
+  case REQ_RANGE_PLAYER:
+  case REQ_RANGE_WORLD:
+  case REQ_RANGE_LAST:
+    break;
+  }
+
+  assert(0);
+  return FALSE;
+}
+
+/****************************************************************************
   Is there a nation within range of the target?
 ****************************************************************************/
 static bool is_nation_in_range(const struct player *target_player,
@@ -1001,6 +1053,15 @@
                                      req->range, req->survives,
                                      req->source.value.terrainclass);
     break;
+  case VUT_TERRAINALTER:
+    eval = is_terrain_alteration_in_range(target_tile,
+                                          req->range, req->survives,
+                                          req->source.value.terrainalter);
+    break;
+  case VUT_CITYCENTER:
+    eval = target_city && target_tile
+      && is_city_center(target_city, target_tile);
+    break;
   case VUT_LAST:
     assert(0);
     return FALSE;
@@ -1065,6 +1126,7 @@
   case VUT_OTYPE:
   case VUT_SPECIALIST: /* Only so long as it's at local range only */
   case VUT_AI_LEVEL:
+  case VUT_CITYCENTER:
     return TRUE;
   case VUT_ADVANCE:
   case VUT_GOVERNMENT:
@@ -1078,6 +1140,7 @@
   case VUT_SPECIAL:
   case VUT_TERRAIN:
   case VUT_TERRAINCLASS:
+  case VUT_TERRAINALTER:
     /* Terrains and specials aren't really unchanging; in fact they're
      * practically guaranteed to change.  We return TRUE here for historical
      * reasons and so that the AI doesn't get confused (since the AI
@@ -1133,6 +1196,10 @@
     return psource1->value.ai_level == psource2->value.ai_level;
   case VUT_TERRAINCLASS:
     return psource1->value.terrainclass == psource2->value.terrainclass;
+  case VUT_TERRAINALTER:
+    return psource1->value.terrainalter == psource2->value.terrainalter;
+  case VUT_CITYCENTER:
+    return TRUE;
   case VUT_LAST:
     break;
   }
@@ -1158,6 +1225,7 @@
 {
   switch (psource->kind) {
   case VUT_NONE:
+  case VUT_CITYCENTER:
     /* TRANS: missing value */
     return N_("(none)");
   case VUT_ADVANCE:
@@ -1190,6 +1258,8 @@
     return ai_level_name(psource->value.ai_level);
   case VUT_TERRAINCLASS:
     return terrain_class_rule_name(psource->value.terrainclass);
+  case VUT_TERRAINALTER:
+    return terrain_alteration_rule_name(psource->value.terrainalter);
   case VUT_LAST:
   default:
     assert(0);
@@ -1267,6 +1337,14 @@
     cat_snprintf(buf, bufsz, _("%s terrain"),
                  terrain_class_name_translation(psource->value.terrainclass));
     break;
+  case VUT_TERRAINALTER:
+    /* TRANS: "Irrigation possible" */
+    cat_snprintf(buf, bufsz, _("%s possible"),
+                 
terrain_alteration_name_translation(psource->value.terrainalter));
+    break;
+  case VUT_CITYCENTER:
+    mystrlcat(buf, _("City center tile"), bufsz);
+    break;
   case VUT_LAST:
     assert(0);
     break;
Index: client/helpdata.c
===================================================================
--- client/helpdata.c   (revision 15124)
+++ client/helpdata.c   (working copy)
@@ -232,6 +232,13 @@
      cat_snprintf(buf, bufsz, _("Requires %s terrain.\n\n"),
                   
terrain_class_name_translation(req->source.value.terrainclass));
      return;
+  case VUT_TERRAINALTER:
+     cat_snprintf(buf, bufsz, _("Requires terrain on which %s can be 
built.\n\n"),
+                  
terrain_alteration_name_translation(req->source.value.terrainalter));
+     return;
+  case VUT_CITYCENTER:
+     cat_snprintf(buf, bufsz, _("Applies only to city centers.\n\n"));
+     return;
   case VUT_LAST:
   default:
     break;
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to