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 03:02:44 -0800
Reply-to: rt@xxxxxxxxxxxxxx

[patch attached]

On Fri, 3 Jan 2003, Jason Short via RT wrote:

> > - moved unit_description() to client/gui-*/citydlg.c (if used by that gui
> > = gtk, gtk-2.0, xaw, sdl, win32) and changed it to use new
> > get_nearest_city*() methods
>
> Don't put identical copies of the function into each GUI.  Just put one
> copy into climisc; it's okay if not all GUIs use it.
>
> For now I don't suggest changing the users of unit_description.  Later
> some GUIs may want to do their own formatting, and some may wish to add
> this information to the mapview panel information for the active unit.
> But these are separate issues.

Citation:

"And perhaps this function is overkill to begin with - it should be left
up to the GUI code to generate the string (or do whatever layout it wants
with the data), and all the client-common code should do is provide a
helper function to find the nearest city."

So I provided only get_nearest_city*() functions and distributed
unit_description() in other GUIs. Moved to climisc.[hc] now.

> > - added "from " in front of home city text
>
> Seems good to me.  Other opinions?
>
> > - text visible before nearest city name is now better translatable (I
> > hope)
>
> I don't see any problems with this, but I'd like to get a translator's
> opinion.  Remember that some languages may follow entirely different
> grammar rules.

I know, I'm Czech. That's why there is _("in %s") (and similar) so others
may translate whole sentence in some neutral way (necause city name must
be intact).

> > Index: client/gui-gtk/citydlg.c
> > ===================================================================
> > RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/citydlg.c,v
> > retrieving revision 1.150
> > diff -u -r1.150 citydlg.c
>
> > +  static char buffer[512];
> > +  static char buffer2[64];
> > +  static char buffer3[64];
>
> buffer2 and buffer3 need not be static.

Sure.

> As a side note, we could avoid the use of a static array here by passing
> a buffer in to the function.  But that's probably a separate issue.

Sure. I don't want to rewrite whole gui code I just want one tiny useful
feature :-) What about two my other patches (2680, 2692)?

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 10:58:02
@@ -1231,3 +1231,85 @@
   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];
+
+  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;
+  }
+  my_snprintf(buffer, sizeof(buffer), "%s%s\n%s\n%s\n%s", 
+         unit_type(punit)->name, 
+         punit->veteran ? _(" (veteran)") : "",
+         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 10:58:02
@@ -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 10:58:08
@@ -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 10:58:08
@@ -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]