Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2004:
[Freeciv-Dev] Re: (PR#10336) citymap cleanup
Home

[Freeciv-Dev] Re: (PR#10336) citymap cleanup

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#10336) citymap cleanup
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 28 Sep 2004 21:20:22 -0700
Reply-to: rt@xxxxxxxxxxx

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

Here is a new patch.

In the current code citymap_init_turn is never called at all!  You can 
see this with the first attached patch.  Unless you are using 
H_EXPERIMENTAL the initialization is never done.  But the citymap is 
always used by the ai.

So this patch:

- Moves the inline functions into citymap.c.

- Changes the citymap variable to be a static (local) pointer to a 
dynamically allocated array.

- Allocates and initializes the array in citymap_turn_init().

- Makes sure citymap_turn_init is always called for AI players.

- Adds a missing #include to citymap.h.

- Edits the documentation of the citymap.

- Fixes the weird ++ line.

Per: the part of the documentation that isn't clear is what the ID value 
means.  Is this a city id or a unit id?

jason


? diff
? common/diff
Index: common/aicore/citymap.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/aicore/citymap.c,v
retrieving revision 1.4
diff -u -r1.4 citymap.c
--- common/aicore/citymap.c     29 Sep 2004 02:24:23 -0000      1.4
+++ common/aicore/citymap.c     29 Sep 2004 04:17:58 -0000
@@ -37,17 +37,19 @@
  * The citymap is a large int double array that corresponds to
  * the freeciv main map. For each tile, it stores three different 
  * and exclusive values in a single int: A positive int tells you
- * how many cities that can use it, a crowdedness indicator. A 
+ * how many cities can use this tile (a crowdedness inidicator). A 
  * value of zero indicates that the tile is presently unused and 
  * available. A negative value means that this tile is occupied 
- * and reserved by some city or unit.
+ * and reserved by some city or unit: in this case the value gives
+ * the negative of the ID of the city or unit that has reserved the
+ * tile.
  *
  * Code that uses the citymap should modify its behaviour based on
  * positive values encountered, and never attempt to steal a tile
  * which has a negative value.
  */
 
-int citymap[MAP_MAX_WIDTH * MAP_MAX_HEIGHT];
+static int *citymap;
 
 #define LOG_CITYMAP LOG_DEBUG
 
@@ -57,14 +59,19 @@
 **************************************************************************/
 void citymap_turn_init(struct player *pplayer)
 {
-  memset(citymap, 0, sizeof(citymap));
+  /* The citymap is reinitialized at the start of ever turn.  This includes
+   * a call to realloc, which only really matters if this is the first turn
+   * of the game (but it's easier than a separate function to do this). */
+  citymap = fc_realloc(citymap, MAX_MAP_INDEX * sizeof(*citymap));
+  memset(citymap, 0, MAX_MAP_INDEX * sizeof(*citymap));
+
   players_iterate(pplayer) {
     city_list_iterate(pplayer->cities, pcity) {
       map_city_radius_iterate(pcity->tile, ptile) {
         if (ptile->worked) {
           citymap[ptile->index] = -(ptile->worked->id);
         } else {
-          citymap[ptile->index] = citymap[ptile->index]++;
+         citymap[ptile->index]++;
         }
       } map_city_radius_iterate_end;
     } city_list_iterate_end;
@@ -135,3 +142,25 @@
 
   citymap[ptile->index] = -id;
 }
+
+/**************************************************************************
+  Returns a positive value if within a city radius, which is 1 x number of
+  cities you are within the radius of, or zero or less if not. A negative
+  value means this tile is reserved by a city and should not be taken.
+**************************************************************************/
+int citymap_read(struct tile *ptile)
+{
+  return citymap[ptile->index];
+}
+
+/**************************************************************************
+  A tile is reserved if it contains a city or unit id, or a worker is
+  assigned to it.
+**************************************************************************/
+bool citymap_is_reserved(struct tile *ptile)
+{
+  if (ptile->worked || ptile->city) {
+    return TRUE;
+  }
+  return (citymap[ptile->index] < 0);
+}
Index: common/aicore/citymap.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/aicore/citymap.h,v
retrieving revision 1.4
diff -u -r1.4 citymap.h
--- common/aicore/citymap.h     29 Sep 2004 02:24:23 -0000      1.4
+++ common/aicore/citymap.h     29 Sep 2004 04:17:58 -0000
@@ -13,33 +13,14 @@
 #ifndef FC__CITYMAP_H
 #define FC__CITYMAP_H
 
-extern int citymap[MAP_MAX_WIDTH * MAP_MAX_HEIGHT]; /* FIXME */
+#include "fcintl.h"
 
+void citymap_init_map(void);
 void citymap_turn_init(struct player *pplayer);
 void citymap_reserve_city_spot(struct tile *ptile, int id);
 void citymap_free_city_spot(struct tile *ptile, int id);
 void citymap_reserve_tile(struct tile *ptile, int id);
-
-/**************************************************************************
-  Returns a positive value if within a city radius, which is 1 x number of
-  cities you are within the radius of, or zero or less if not. A negative
-  value means this tile is reserved by a city and should not be taken.
-**************************************************************************/
-static inline int citymap_read(struct tile *ptile)
-{
-  return citymap[ptile->index];
-}
-
-/**************************************************************************
-  A tile is reserved if it contains a city or unit id, or a worker is
-  assigned to it.
-**************************************************************************/
-static inline bool citymap_is_reserved(struct tile *ptile)
-{
-  if (ptile->worked || ptile->city) {
-    return TRUE;
-  }
-  return (citymap[ptile->index] < 0);
-}
+int citymap_read(struct tile *ptile);
+bool citymap_is_reserved(struct tile *ptile);
 
 #endif
Index: server/settlers.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/settlers.c,v
retrieving revision 1.204
diff -u -r1.204 settlers.c
--- server/settlers.c   29 Sep 2004 02:24:24 -0000      1.204
+++ server/settlers.c   29 Sep 2004 04:17:59 -0000
@@ -1275,7 +1275,7 @@
 
   t = renew_timer_start(t, TIMER_CPU, TIMER_DEBUG);
 
-  if (pplayer->ai.control && ai_handicap(pplayer, H_EXPERIMENTAL)) {
+  if (pplayer->ai.control) {
     /* Set up our city map. */
     citymap_turn_init(pplayer);
   }

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