Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2003:
[Freeciv-Dev] Re: (PR#2690) [PATCH] Show unit location in its descriptio
Home

[Freeciv-Dev] Re: (PR#2690) [PATCH] Show unit location in its descriptio

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#2690) [PATCH] Show unit location in its description
From: "Stepan Roh via RT" <rt@xxxxxxxxxxxxxx>
Date: Sat, 4 Jan 2003 08:00:25 -0800
Reply-to: rt@xxxxxxxxxxxxxx

(Is CC'ing freeciv-dev needed?)

On Sat, 4 Jan 2003, Jason Short via RT wrote:

> So, I have one last request: build that line's string into another
> independent buffer first.  Use _("%s (veteran)") or "%s" as the format
> string.  (See below.)

Done. Patch attached.

Have a nice day.

Stepan Roh

Index: client/climisc.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
retrieving revision 1.107
diff -u -r1.107 climisc.c
--- client/climisc.c    2003/01/01 11:51:32     1.107
+++ client/climisc.c    2003/01/04 15:55:39
@@ -1231,3 +1231,90 @@
   report_dialogs_force_thaw();
   output_window_force_thaw();
 }
+
+/**************************************************************************
+  Find city nearest to given unit and optionally return squared city distance
+  Parameter sq_dist may be NULL. Returns NULL only if no city is known.
+  Favors punit owner's cities over other cities if equally distant.
+**************************************************************************/
+struct city*get_nearest_city(struct unit *punit, int *sq_dist)
+{
+  struct city *pcity_near;
+  int pcity_near_dist;
+
+  if ((pcity_near = map_get_city(punit->x, punit->y))) {
+    pcity_near_dist = 0;
+  } else {
+    pcity_near = NULL;
+    pcity_near_dist = -1;
+    players_iterate(pplayer) {
+      city_list_iterate(pplayer->cities, pcity_current) {
+        int dist = sq_map_distance(pcity_current->x, pcity_current->y, 
punit->x, punit->y);
+        if ((pcity_near_dist == -1) || (dist < pcity_near_dist) || ((dist == 
pcity_near_dist) && (punit->owner == pcity_current->owner))) {
+          pcity_near = pcity_current;
+          pcity_near_dist = dist;
+        }
+      } city_list_iterate_end;
+    } players_iterate_end;
+  }
+  
+  if (sq_dist) {
+    *sq_dist = pcity_near_dist;
+  }
+
+  return pcity_near;
+}
+
+#define FAR_CITY_SQUARE_DIST (2*(6*6))
+
+/**************************************************************************
+  Fill buf (of size bufsz) with proper nearest city message.
+  Returns buf.
+**************************************************************************/
+char *get_nearest_city_text(struct city *pcity, int sq_dist, char *buf, size_t 
bufsz)
+{
+  /* just to be sure */
+  if (!pcity) {
+    sq_dist = -1;
+  }
+  
+  my_snprintf(buf, bufsz, 
+         (sq_dist >= FAR_CITY_SQUARE_DIST) ? _("far from %s") : (sq_dist > 0) 
? _("near %s") : (sq_dist == 0) ? _("in %s") : "%s",
+         pcity ? pcity->name : "");
+
+  return buf;
+}
+
+/**************************************************************************
+  Returns unit description (as static buffer).
+**************************************************************************/
+const char *unit_description(struct unit *punit)
+{
+  struct city *pcity, *pcity_near;
+  int pcity_near_dist;
+  static char buffer[512];
+  char buffer2[64];
+  char buffer3[64];
+  char buffer4[64];
+
+  pcity = player_find_city_by_id(game.player_ptr, punit->homecity);
+  pcity_near = get_nearest_city(punit, &pcity_near_dist);
+
+  if (pcity) {
+    my_snprintf(buffer3, sizeof(buffer3), _("from %s"), pcity->name);
+  } else {
+    buffer3[0] = 0;
+  }
+  if (punit->veteran) {
+    my_snprintf(buffer4, sizeof(buffer4), _("%s (veteran)"), 
unit_type(punit)->name);
+  } else {
+    sz_strlcpy(buffer4, unit_type(punit)->name);
+  }
+  my_snprintf(buffer, sizeof(buffer), "%s\n%s\n%s\n%s", 
+          buffer4,
+          unit_activity_text(punit), 
+          buffer3,
+          get_nearest_city_text(pcity_near, pcity_near_dist, buffer2, 
sizeof(buffer2)));
+
+  return buffer;
+}
Index: client/climisc.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.h,v
retrieving revision 1.40
diff -u -r1.40 climisc.h
--- client/climisc.h    2002/11/15 22:15:01     1.40
+++ client/climisc.h    2003/01/04 15:55:40
@@ -121,4 +121,9 @@
 void reports_thaw(void);
 void reports_force_thaw(void);
 
+struct city*get_nearest_city(struct unit *punit, int *sq_dist);
+char *get_nearest_city_text(struct city *pcity, int sq_dist, char *buf, size_t 
bufsz);
+
+const char *unit_description(struct unit *punit);
+
 #endif  /* FC__CLIMISC_H */
Index: common/unit.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.c,v
retrieving revision 1.167
diff -u -r1.167 unit.c
--- common/unit.c       2002/12/18 17:36:19     1.167
+++ common/unit.c       2003/01/04 15:55:46
@@ -844,25 +844,6 @@
 /**************************************************************************
  ...
 **************************************************************************/
-const char *unit_description(struct unit *punit)
-{
-  struct city *pcity;
-  static char buffer[512];
-
-  pcity = player_find_city_by_id(game.player_ptr, punit->homecity);
-
-  my_snprintf(buffer, sizeof(buffer), "%s%s\n%s\n%s", 
-         unit_type(punit)->name, 
-         punit->veteran ? _(" (veteran)") : "",
-         unit_activity_text(punit), 
-         pcity ? pcity->name : "");
-
-  return buffer;
-}
-
-/**************************************************************************
- ...
-**************************************************************************/
 const char *unit_activity_text(struct unit *punit)
 {
   static char text[64];
Index: common/unit.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unit.h,v
retrieving revision 1.92
diff -u -r1.92 unit.h
--- common/unit.h       2002/12/18 17:36:19     1.92
+++ common/unit.h       2003/01/04 15:55:46
@@ -221,7 +221,6 @@
 bool kills_citizen_after_attack(struct unit *punit);
 
 const char *unit_activity_text(struct unit *punit);
-const char *unit_description(struct unit *punit);
 int ground_unit_transporter_capacity(int x, int y, struct player *pplayer);
 int get_transporter_capacity(struct unit *punit);
 bool is_ground_units_transport(struct unit *punit);

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