Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] (PR#9548) rewrite upgrade_building_prod
Home

[Freeciv-Dev] (PR#9548) rewrite upgrade_building_prod

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9548) rewrite upgrade_building_prod
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 29 Jul 2004 13:08:31 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9548 >

This patch attempts to rewrite upgrade_building_prod in 
server/cityturn.c.  It is rewritten to correspond closely to 
upgrade_unit_prod.

However I did not use can_build_improvement_direct, I just used 
can_build_improvement.  For two reasons:

- There is no can_build_improvement_direct yet, and city.c is too ugly 
for me to write one immediately.

- It looks like can_build_improvement does the exact same thing as 
can_build_improvement_direct should do.  Probably this is because there 
is no full building upgrading in CVS currently.

This patch is untested and I'm not sure if it's ready for inclusion.

jason

Index: server/cityturn.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v
retrieving revision 1.255
diff -u -r1.255 cityturn.c
--- server/cityturn.c   20 Jul 2004 16:27:08 -0000      1.255
+++ server/cityturn.c   29 Jul 2004 20:06:18 -0000
@@ -67,11 +67,11 @@
 static bool city_build_building(struct player *pplayer, struct city *pcity);
 static bool city_build_unit(struct player *pplayer, struct city *pcity);
 static bool city_build_stuff(struct player *pplayer, struct city *pcity);
-static int improvement_upgrades_to(struct city *pcity, int imp);
+static Impr_Type_id improvement_upgrades_to(const struct city *pcity,
+                                           Impr_Type_id imp);
 static void upgrade_building_prod(struct city *pcity);
 static Unit_Type_id unit_upgrades_to(struct city *pcity, Unit_Type_id id);
 static void upgrade_unit_prod(struct city *pcity);
-static void obsolete_building_test(struct city *pcity, int b1, int b2);
 static void pay_for_buildings(struct player *pplayer, struct city *pcity);
 
 static bool disband_city(struct city *pcity);
@@ -759,45 +759,48 @@
 }
 
 /**************************************************************************
-...
+  If the improvement is obsolete, return the improvement that _can_ be
+  built that lead to imp's obsolesence.
 **************************************************************************/
-static void obsolete_building_test(struct city *pcity, int b1, int b2)
+static Impr_Type_id improvement_upgrades_to(const struct city *pcity,
+                                           Impr_Type_id id)
 {
-  if (pcity->currently_building == b1
-      && !pcity->is_building_unit
-      && can_build_improvement(pcity, b2)) {
-    pcity->currently_building = b2;
+  Impr_Type_id check = id, latest_ok = id;
+
+  if (!can_build_improvement_direct(pcity, check)) {
+    return -1;
   }
-}
 
-/**************************************************************************
-  If imp is obsolete, return the improvement that _can_ be built that
-  lead to imp's obsolesence.
-  !!! Note:  I hear that the building ruleset code is going to be
-  overhauled soon.  If this happens, then this function should be updated
-  to follow the new model.  This function will probably look a lot like
-  unit_upgrades_to().
-**************************************************************************/
-static int improvement_upgrades_to(struct city *pcity, int imp)
-{
-  if (imp == B_BARRACKS && can_build_improvement(pcity, B_BARRACKS3))
-    return B_BARRACKS3;
-  else if (imp == B_BARRACKS && can_build_improvement(pcity, B_BARRACKS2))
-    return B_BARRACKS2;
-  else if (imp == B_BARRACKS2 && can_build_improvement(pcity, B_BARRACKS3))
-    return B_BARRACKS3;
-  else
-    return imp;
+  while (improvement_exists(check = improvement_types[check].replaced_by)) {
+    if (can_build_improvement_direct(pcity, check)) {
+      latest_ok = check;
+    }
+  }
+
+  if (latest_ok == id) {
+    return -1; /* Can't upgrade. */
+  }
+  return latest_ok;
 }
 
 /**************************************************************************
-...
+  Upgrade the building production in the city.
 **************************************************************************/
 static void upgrade_building_prod(struct city *pcity)
 {
-  obsolete_building_test(pcity, B_BARRACKS,B_BARRACKS3);
-  obsolete_building_test(pcity, B_BARRACKS,B_BARRACKS2);
-  obsolete_building_test(pcity, B_BARRACKS2,B_BARRACKS3);
+  struct player *pplayer = city_owner(pcity);
+  Impr_Type_id id = pcity->currently_building, id2;
+  
+  assert(!pcity->is_building_unit);
+  id2 = building_upgrades_to(pcity, id);
+
+  if (can_build_improvement(pcity, id2)) {
+    pcity->currently_building = id2;
+    notify_player_ex(pplayer, pcity->x, pcity->y, E_UNIT_UPGRADED,
+                    _("Game: Production of %s is upgraded to %s in %s."),
+                    get_improvement_type(id)->name,
+                    get_improvement_type(id2)->name, pcity->name);
+  }
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9548) rewrite upgrade_building_prod, Jason Short <=