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: Fri, 3 Jan 2003 16:44:53 -0800
Reply-to: rt@xxxxxxxxxxxxxx



On Fri, 3 Jan 2003, Stepan Roh wrote:

> On Fri, 3 Jan 2003, Jason Short via RT wrote:
>
> > On Fri, 2003-01-03 at 06:49, Stepan Roh via RT wrote:
> >
> > > Looks good. I'll code a patch this weekend (create get_nearest_*()
> > > functions in climisc.[hc] and use them in gui-gtk's new unit_description()
> > > function (and possibly mark unit_description() in common/ as deprecated)).
> >
> > If you make GUI changes, please make them for all GUIs.
> >
> > This is one argument in favor of keeping unit_description :-).
>
> Oh well, I'll try.

Patch attached.

Features:

- introduced methods get_nearest_city() and get_nearest_city_text()
(located in client/climisc.[hc])

- 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

- added "from " in front of home city text

- text visible before nearest city name is now better translatable (I
hope)

Tested only with gui-gtk.

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 00:33:01
@@ -1231,3 +1231,56 @@
   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;
+}
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 00:33:01
@@ -121,4 +121,7 @@
 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);
+
 #endif  /* FC__CLIMISC_H */
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
--- client/gui-gtk/citydlg.c    2003/01/01 11:51:32     1.150
+++ client/gui-gtk/citydlg.c    2003/01/04 00:33:04
@@ -316,6 +317,8 @@
 static void close_callback(GtkWidget * w, gpointer data);
 static void switch_city_callback(GtkWidget * w, gpointer data);
 
+static const char *unit_description(struct unit *punit);
+
 /****************************************************************
   Called to set the dimensions of the city dialog, both on
   startup and if the tileset is changed.
@@ -3644,4 +3662,33 @@
   set_cityopt_values(pdialog); /* need not be in refresh_city_dialog */
   refresh_city_dialog(pdialog->pcity);
   select_impr_list_callback(NULL, 0, 0, NULL, pdialog); /* unselects clist */
+}
+
+/**************************************************************************
+ ...
+**************************************************************************/
+static const char *unit_description(struct unit *punit)
+{
+  struct city *pcity, *pcity_near;
+  int pcity_near_dist;
+  static char buffer[512];
+  static char buffer2[64];
+  static 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/gui-gtk-2.0/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/citydlg.c,v
retrieving revision 1.22
diff -u -r1.22 citydlg.c
--- client/gui-gtk-2.0/citydlg.c        2002/12/31 03:57:46     1.22
+++ client/gui-gtk-2.0/citydlg.c        2003/01/04 00:33:08
@@ -307,6 +307,8 @@
 static void close_callback(GtkWidget * w, gpointer data);
 static void switch_city_callback(GtkWidget * w, gpointer data);
 
+static const char *unit_description(struct unit *punit);
+
 /****************************************************************
   Called to set the dimensions of the city dialog, both on
   startup and if the tileset is changed.
@@ -3538,4 +3540,33 @@
   set_cityopt_values(pdialog); /* need not be in refresh_city_dialog */
   refresh_city_dialog(pdialog->pcity);
   select_impr_list_callback(NULL, 0, 0, NULL, pdialog); /* unselects clist */
+}
+
+/**************************************************************************
+ ...
+**************************************************************************/
+static const char *unit_description(struct unit *punit)
+{
+  struct city *pcity, *pcity_near;
+  int pcity_near_dist;
+  static char buffer[512];
+  static char buffer2[64];
+  static 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/gui-sdl/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/citydlg.c,v
retrieving revision 1.4
diff -u -r1.4 citydlg.c
--- client/gui-sdl/citydlg.c    2002/12/30 21:40:49     1.4
+++ client/gui-sdl/citydlg.c    2003/01/04 00:33:10
@@ -165,6 +165,8 @@
 static void rebuild_citydlg_title_str(struct GUI *pWindow,
                                      struct city *pCity);
 
+static const char *unit_description(struct unit *punit);
+
 /* ======================================================================= */
 
 
@@ -5074,4 +5076,33 @@
     return TRUE;
 
   return FALSE;
+}
+
+/**************************************************************************
+ ...
+**************************************************************************/
+static const char *unit_description(struct unit *punit)
+{
+  struct city *pcity, *pcity_near;
+  int pcity_near_dist;
+  static char buffer[512];
+  static char buffer2[64];
+  static 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/gui-win32/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/citydlg.c,v
retrieving revision 1.44
diff -u -r1.44 citydlg.c
--- client/gui-win32/citydlg.c  2002/12/30 15:36:03     1.44
+++ client/gui-win32/citydlg.c  2003/01/04 00:33:11
@@ -159,9 +159,8 @@
   PAGE_CONFIG
 };
 
+static const char *unit_description(struct unit *punit);
 
-
-
 /**************************************************************************
 ...
 **************************************************************************/
@@ -2336,4 +2335,33 @@
       return DefWindowProc(win,message,wParam,lParam);
     }
   return 0;
+}
+
+/**************************************************************************
+ ...
+**************************************************************************/
+static const char *unit_description(struct unit *punit)
+{
+  struct city *pcity, *pcity_near;
+  int pcity_near_dist;
+  static char buffer[512];
+  static char buffer2[64];
+  static 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/gui-xaw/citydlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/citydlg.c,v
retrieving revision 1.86
diff -u -r1.86 citydlg.c
--- client/gui-xaw/citydlg.c    2002/12/15 22:43:47     1.86
+++ client/gui-xaw/citydlg.c    2003/01/04 00:33:13
@@ -189,6 +189,8 @@
                             XtPointer call_data);
 static void popdown_cityopt_dialog(void);
 
+static const char *unit_description(struct unit *punit);
+
 /****************************************************************
 ...
 *****************************************************************/
@@ -2743,4 +2745,33 @@
     XtDestroyWidget(cityopt_shell);
     cityopt_shell = 0;
   }
+}
+
+/**************************************************************************
+ ...
+**************************************************************************/
+static const char *unit_description(struct unit *punit)
+{
+  struct city *pcity, *pcity_near;
+  int pcity_near_dist;
+  static char buffer[512];
+  static char buffer2[64];
+  static 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: 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 00:33:15
@@ -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 00:33:15
@@ -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]