[Freeciv-Dev] Re: (PR#8528) Move text building functions into one file
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: |
undisclosed-recipients: ; |
Subject: |
[Freeciv-Dev] Re: (PR#8528) Move text building functions into one file |
From: |
"Raimar Falke" <i-freeciv-lists@xxxxxxxxxxxxx> |
Date: |
Tue, 20 Apr 2004 12:11:55 -0700 |
Reply-to: |
rt@xxxxxxxxxxx |
<URL: http://rt.freeciv.org/Ticket/Display.html?id=8528 >
On Tue, Apr 20, 2004 at 11:10:20AM -0700, Raimar Falke wrote:
> I will think about a dynamic allocated buffer. And a static pointer.
It works and also somewhat clean.
Raimar
--
email: rf13@xxxxxxxxxxxxxxxxx
"It is not yet possible to change operating system by writing
to /proc/sys/kernel/ostype." sysctl(2) man page
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/Makefile.am clean/client/Makefile.am
--- real.clean/client/Makefile.am Tue Apr 20 21:00:05 2004
+++ clean/client/Makefile.am Tue Apr 20 20:15:23 2004
@@ -179,6 +179,8 @@
options.h \
repodlgs_common.c \
repodlgs_common.h \
+ text.c \
+ text.h \
tilespec.c \
tilespec.h \
audio.c \
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/climisc.c clean/client/climisc.c
--- real.clean/client/climisc.c Sat Mar 27 15:39:32 2004
+++ clean/client/climisc.c Tue Apr 20 20:15:23 2004
@@ -424,52 +424,6 @@
}
/**************************************************************************
- Concats buf with activity progress text for given tile. Returns
- number of activities.
-**************************************************************************/
-int concat_tile_activity_text(char *buf, int buf_size, int x, int y)
-{
- int activity_total[ACTIVITY_LAST];
- int activity_units[ACTIVITY_LAST];
- int num_activities = 0;
- int remains, turns, i, mr, au;
- struct tile *ptile = map_get_tile(x, y);
-
- memset(activity_total, 0, sizeof(activity_total));
- memset(activity_units, 0, sizeof(activity_units));
-
- unit_list_iterate(ptile->units, punit) {
- mr = get_unit_type(punit->type)->move_rate;
- au = (mr > 0) ? mr / SINGLE_MOVE : 1;
- activity_total[punit->activity] += punit->activity_count;
- if (punit->moves_left > 0) {
- /* current turn */
- activity_total[punit->activity] += au;
- }
- activity_units[punit->activity] += au;
- }
- unit_list_iterate_end;
-
- for (i = 0; i < ACTIVITY_LAST; i++) {
- if (is_build_or_clean_activity(i) && activity_units[i] > 0) {
- if (num_activities > 0)
- (void) mystrlcat(buf, "/", buf_size);
- remains = map_activity_time(i, x, y) - activity_total[i];
- if (remains > 0) {
- turns = 1 + (remains + activity_units[i] - 1) / activity_units[i];
- } else {
- /* activity will be finished this turn */
- turns = 1;
- }
- cat_snprintf(buf, buf_size, "%s(%d)", get_activity_text(i), turns);
- num_activities++;
- }
- }
-
- return num_activities;
-}
-
-/**************************************************************************
...
**************************************************************************/
cid cid_encode(bool is_unit, int id)
@@ -1092,70 +1046,6 @@
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];
- struct unit_type *ptype = unit_type(punit);
-
- 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(buffer4, sizeof(buffer4), _("%s"), ptype->name);
-
- if (ptype->veteran[punit->veteran].name[0] != '\0') {
- sz_strlcat(buffer4, " (");
- sz_strlcat(buffer4, ptype->veteran[punit->veteran].name);
- sz_strlcat(buffer4, ")");
- }
-
- 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;
-}
-
/**************************************************************************
Called when the "Buy" button is pressed in the city report for every
selected city. Checks for coinage and sufficient funds or request the
@@ -1194,58 +1084,6 @@
}
}
-/**************************************************************************
- Returns the text to display in the science dialog.
-**************************************************************************/
-const char *science_dialog_text()
-{
- int turns_to_advance;
- static char text[512];
- struct player *plr = game.player_ptr;
- int ours = 0, theirs = 0;
-
- /* Sum up science */
- players_iterate(pplayer) {
- enum diplstate_type ds = pplayer_get_diplstate(plr, pplayer)->type;
-
- if (plr == pplayer) {
- city_list_iterate(pplayer->cities, pcity) {
- ours += pcity->science_total;
- } city_list_iterate_end;
- } else if (ds == DS_TEAM) {
- theirs += pplayer->research.bulbs_last_turn;
- }
- } players_iterate_end;
-
- if (ours == 0 && theirs == 0) {
- my_snprintf(text, sizeof(text), _("Progress: no research"));
- return text;
- }
- if (ours < 0 || theirs < 0) {
- die("Negative science in science_dialog_text");
- }
- turns_to_advance = (total_bulbs_required(plr) + ours + theirs - 1)
- / (ours + theirs);
- if (theirs == 0) {
- /* Simple version, no techpool */
- my_snprintf(text, sizeof(text),
- PL_("Progress: %d turn/advance (%d pts/turn)",
- "Progress: %d turns/advance (%d pts/turn)",
- turns_to_advance),
- turns_to_advance, ours);
- } else {
- /* Techpool version */
- my_snprintf(text, sizeof(text),
- PL_("Progress: %d turn/advance (%d pts/turn, "
- "%d pts/turn from team)",
- "Progress: %d turns/advance (%d pts/turn, "
- "%d pts/turn from team)",
- turns_to_advance),
- turns_to_advance, ours, theirs);
- }
- return text;
-}
-
void common_taxrates_callback(int i)
{
int tax_end, lux_end, sci_end, tax, lux, sci;
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/climisc.h clean/client/climisc.h
--- real.clean/client/climisc.h Sat Mar 27 15:39:32 2004
+++ clean/client/climisc.h Tue Apr 20 20:15:23 2004
@@ -44,8 +44,6 @@
void center_on_something(void);
-int concat_tile_activity_text(char *buf, int buf_size, int x, int y);
-
/*
* A compound id (cid) can hold all objects a city can build:
* improvements (with wonders) and units. This is achieved by
@@ -119,11 +117,6 @@
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);
-const char *science_dialog_text(void);
void cityrep_buy(struct city *pcity);
void common_taxrates_callback(int i);
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/gui-gtk-2.0/citydlg.c clean/client/gui-gtk-2.0/citydlg.c
--- real.clean/client/gui-gtk-2.0/citydlg.c Mon Apr 12 20:07:23 2004
+++ clean/client/gui-gtk-2.0/citydlg.c Tue Apr 20 20:15:23 2004
@@ -54,6 +54,7 @@
#include "tilespec.h"
#include "wldlg.h"
#include "log.h"
+#include "text.h"
#include "cityicon.ico"
#include "citydlg.h"
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/gui-gtk-2.0/mapctrl.c clean/client/gui-gtk-2.0/mapctrl.c
--- real.clean/client/gui-gtk-2.0/mapctrl.c Sat Mar 27 15:39:33 2004
+++ clean/client/gui-gtk-2.0/mapctrl.c Tue Apr 20 20:15:23 2004
@@ -43,6 +43,7 @@
#include "menu.h"
#include "tilespec.h"
#include "cma_core.h"
+#include "text.h"
#include "mapctrl.h"
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/gui-gtk-2.0/mapview.c clean/client/gui-gtk-2.0/mapview.c
--- real.clean/client/gui-gtk-2.0/mapview.c Fri Apr 16 18:50:36 2004
+++ clean/client/gui-gtk-2.0/mapview.c Tue Apr 20 20:15:23 2004
@@ -47,6 +47,7 @@
#include "mapctrl.h"
#include "options.h"
#include "tilespec.h"
+#include "text.h"
#include "citydlg.h" /* For reset_city_dialogs() */
#include "mapview.h"
@@ -127,23 +128,13 @@
**************************************************************************/
void update_info_label( void )
{
- char buffer [512];
int d;
int sol, flake;
gtk_frame_set_label(GTK_FRAME(main_frame_civ_name),
get_nation_name(game.player_ptr->nation));
- my_snprintf(buffer, sizeof(buffer),
- _("Population: %s\nYear: %s\n"
- "Gold: %d\nTax: %d Lux: %d Sci: %d"),
- population_to_text(civ_population(game.player_ptr)),
- textyear(game.year), game.player_ptr->economic.gold,
- game.player_ptr->economic.tax,
- game.player_ptr->economic.luxury,
- game.player_ptr->economic.science);
-
- gtk_label_set_text(GTK_LABEL(main_label_info), buffer);
+ gtk_label_set_text(GTK_LABEL(main_label_info), get_info_label_text());
sol = client_warming_sprite();
flake = client_cooling_sprite();
@@ -179,32 +170,13 @@
_("Shows your current luxury/science/tax rates;"
"click to toggle them."), "");
- my_snprintf(buffer, sizeof(buffer),
- _("Shows your progress in researching "
- "the current technology.\n"
- "%s: %d/%d."),
- advances[game.player_ptr->research.researching].name,
- game.player_ptr->research.bulbs_researched,
- total_bulbs_required(game.player_ptr));
- gtk_tooltips_set_tip(main_tips, bulb_ebox, buffer, "");
-
- my_snprintf(buffer, sizeof(buffer),
- _("Shows the progress of global warming:\n"
- "%d."),
- sol);
- gtk_tooltips_set_tip(main_tips, sun_ebox, buffer, "");
-
- my_snprintf(buffer, sizeof(buffer),
- _("Shows the progress of nuclear winter:\n"
- "%d."),
- flake);
- gtk_tooltips_set_tip(main_tips, flake_ebox, buffer, "");
-
- my_snprintf(buffer, sizeof(buffer),
- _("Shows your current government:\n"
- "%s."),
- get_government_name(game.player_ptr->government));
- gtk_tooltips_set_tip(main_tips, government_ebox, buffer, "");
+ gtk_tooltips_set_tip(main_tips, bulb_ebox, get_bulb_tooltip(), "");
+ gtk_tooltips_set_tip(main_tips, sun_ebox, get_global_warming_tooltip(),
+ "");
+ gtk_tooltips_set_tip(main_tips, flake_ebox, get_nuclear_winter_tooltip(),
+ "");
+ gtk_tooltips_set_tip(main_tips, government_ebox, get_government_tooltip(),
+ "");
}
/**************************************************************************
@@ -219,34 +191,12 @@
**************************************************************************/
void update_unit_info_label(struct unit *punit)
{
- if(punit) {
- char buffer[512];
- struct city *pcity =
- player_find_city_by_id(game.player_ptr, punit->homecity);
- int infrastructure =
- get_tile_infrastructure_set(map_get_tile(punit->x, punit->y));
- struct unit_type *ptype = unit_type(punit);
-
- my_snprintf(buffer, sizeof(buffer), "%s", ptype->name);
-
- if (ptype->veteran[punit->veteran].name[0] != '\0') {
- sz_strlcat(buffer, " (");
- sz_strlcat(buffer, _(ptype->veteran[punit->veteran].name));
- sz_strlcat(buffer, ")");
- }
-
- gtk_frame_set_label(GTK_FRAME(unit_info_frame), buffer);
-
- my_snprintf(buffer, sizeof(buffer), "%s\n%s\n%s%s%s%s",
- (hover_unit == punit->id) ?
- _("Select destination") : unit_activity_text(punit),
- map_get_tile_info_text(punit->x, punit->y),
- infrastructure ?
- map_get_infrastructure_text(infrastructure) : "",
- infrastructure ? "\n" : "", pcity ? pcity->name : "",
- infrastructure ? "" : "\n");
- gtk_label_set_text(GTK_LABEL(unit_info_label), buffer);
+ gtk_frame_set_label(GTK_FRAME(unit_info_frame),
+ get_unit_info_label_text1(punit));
+ gtk_label_set_text(GTK_LABEL(unit_info_label),
+ get_unit_info_label_text2(punit));
+ if(punit) {
if (hover_unit != punit->id)
set_hover_state(NULL, HOVER_NONE);
@@ -269,8 +219,6 @@
break;
}
} else {
- gtk_frame_set_label( GTK_FRAME(unit_info_frame),"");
- gtk_label_set_text(GTK_LABEL(unit_info_label), "\n\n\n");
gdk_window_set_cursor(root_window, NULL);
}
update_unit_pix_label(punit);
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/gui-gtk-2.0/repodlgs.c clean/client/gui-gtk-2.0/repodlgs.c
--- real.clean/client/gui-gtk-2.0/repodlgs.c Mon Apr 12 20:07:23 2004
+++ clean/client/gui-gtk-2.0/repodlgs.c Tue Apr 20 20:15:23 2004
@@ -45,6 +45,7 @@
#include "options.h"
#include "packhand_gen.h"
#include "control.h"
+#include "text.h"
#include "repodlgs_common.h"
#include "repodlgs.h"
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/mapctrl_common.c clean/client/mapctrl_common.c
--- real.clean/client/mapctrl_common.c Mon Apr 12 20:07:23 2004
+++ clean/client/mapctrl_common.c Tue Apr 20 20:15:23 2004
@@ -711,171 +711,3 @@
qsort(unit_list, i, sizeof(*unit_list), unit_list_compare);
}
-static void add_line(char* buf, size_t bufsz, char *format, ...)
- fc__attribute((format(printf, 3, 4)));
-
-/****************************************************************************
- Add a full line of text to the buffer.
-****************************************************************************/
-static void add_line(char* buf, size_t bufsz, char *format, ...)
-{
- va_list args;
-
- if (buf[0] != '\0') {
- mystrlcat(buf, "\n", bufsz);
- }
-
- va_start(args, format);
- my_vsnprintf(buf + strlen(buf), bufsz - strlen(buf), format, args);
- va_end(args);
-
- assert(strlen(buf) + 1 < bufsz);
-}
-
-/************************************************************************
-Text to popup on middle-click
-************************************************************************/
-char* popup_info_text(int xtile, int ytile)
-{
- static char out[256];
- char buf[64];
- struct city *pcity = map_get_city(xtile, ytile);
- struct tile *ptile = map_get_tile(xtile, ytile);
- struct unit *punit = find_visible_unit(ptile);
- const char *diplo_nation_plural_adjectives[DS_LAST] =
- {Q_("?nation:Neutral"), Q_("?nation:Hostile"),
- "" /* unused, DS_CEASEFIRE*/,
- Q_("?nation:Peaceful"), Q_("?nation:Friendly"),
- Q_("?nation:Mysterious"), Q_("?nation:Friendly(team)")};
- const char *diplo_city_adjectives[DS_LAST] =
- {Q_("?city:Neutral"), Q_("?city:Hostile"),
- "" /*unused, DS_CEASEFIRE */,
- Q_("?city:Peaceful"), Q_("?city:Friendly"), Q_("?city:Mysterious"),
- Q_("?city:Friendly(team)")};
-
- out[0] = '\0';
-#ifdef DEBUG
- add_line(out, sizeof(out), _("Location: (%d, %d) [%d]"),
- xtile, ytile, ptile->continent);
-#endif /*DEBUG*/
- add_line(out, sizeof(out), _("Terrain: %s"),
- map_get_tile_info_text(xtile, ytile));
- add_line(out, sizeof(out), _("Food/Prod/Trade: %s"),
- map_get_tile_fpt_text(xtile, ytile));
- if (tile_has_special(ptile, S_HUT)) {
- add_line(out, sizeof(out), _("Minor Tribe Village"));
- }
- if (game.borders > 0 && !pcity) {
- struct player *owner = map_get_owner(xtile, ytile);
- struct player_diplstate *ds = game.player_ptr->diplstates;
-
- if (owner == game.player_ptr){
- add_line(out, sizeof(out), _("Our Territory"));
- } else if (owner) {
- if (ds[owner->player_no].type == DS_CEASEFIRE) {
- int turns = ds[owner->player_no].turns_left;
-
- add_line(out, sizeof(out), PL_("%s territory (%d turn ceasefire)",
- "%s territory (%d turn ceasefire)",
- turns),
- get_nation_name(owner->nation), turns);
- } else {
- /* TRANS: "Territory of the friendly Polish". See the ?nation:
- * adjectives. */
- add_line(out, sizeof(out), _("Territory of the %s %s"),
- diplo_nation_plural_adjectives[ds[owner->player_no].type],
- get_nation_name_plural(owner->nation));
- }
- } else {
- add_line(out, sizeof(out), _("Unclaimed territory"));
- }
- }
- if (pcity) {
- /* Look at city owner, not tile owner (the two should be the same, if
- * borders are in use). */
- struct player *owner = city_owner(pcity);
- struct player_diplstate *ds = game.player_ptr->diplstates;
- struct unit *apunit;
-
- if (owner == game.player_ptr){
- /* TRANS: "City: <name> (<nation>)" */
- add_line(out, sizeof(out), _("City: %s (%s)"), pcity->name,
- get_nation_name(owner->nation));
- } else if (owner) {
- if (ds[owner->player_no].type == DS_CEASEFIRE) {
- int turns = ds[owner->player_no].turns_left;
-
- /* TRANS: City: Warsaw (Polish, 5 turn ceasefire)" */
- add_line(out, sizeof(out), PL_("City: %s (%s, %d turn ceasefire)",
- "City: %s (%s, %d turn ceasefire)",
- turns),
- pcity->name,
- get_nation_name(owner->nation),
- turns);
- } else {
- /* TRANS: "City: <name> (<nation>,<diplomatic_state>)" */
- add_line(out, sizeof(out), _("City: %s (%s,%s)"), pcity->name,
- get_nation_name(owner->nation),
- diplo_city_adjectives[ds[owner->player_no].type]);
- }
- }
- if (city_got_citywalls(pcity)) {
- sz_strlcat(out, _(" with City Walls"));
- }
-
- if ((apunit = get_unit_in_focus())) {
- struct city *hcity = find_city_by_id(apunit->homecity);
-
- if (unit_flag(apunit, F_TRADE_ROUTE)
- && can_cities_trade(hcity, pcity)
- && can_establish_trade_route(hcity, pcity)) {
- add_line(out, sizeof(out), _("Trade from %s: %d"),
- hcity->name, trade_between_cities(hcity, pcity));
- }
- }
- }
- if (get_tile_infrastructure_set(ptile)) {
- add_line(out, sizeof(out), _("Infrastructure: %s"),
- map_get_infrastructure_text(ptile->special));
- }
- buf[0] = '\0';
- if (concat_tile_activity_text(buf, sizeof(buf), xtile, ytile)) {
- add_line(out, sizeof(out), _("Activity: %s"), buf);
- }
- if (punit && !pcity) {
- char tmp[64] = {0};
- struct unit_type *ptype = unit_type(punit);
- if (punit->owner == game.player_idx) {
- struct city *pcity;
- pcity=player_find_city_by_id(game.player_ptr, punit->homecity);
- if (pcity)
- my_snprintf(tmp, sizeof(tmp), "/%s", pcity->name);
- }
- add_line(out, sizeof(out), _("Unit: %s(%s%s)"), ptype->name,
- get_nation_name(unit_owner(punit)->nation), tmp);
- if (punit->owner != game.player_idx){
- struct unit *apunit;
- if ((apunit = get_unit_in_focus())) {
- /* chance to win when active unit is attacking the selected unit */
- int att_chance = unit_win_chance(apunit, punit) * 100;
-
- /* chance to win when selected unit is attacking the active unit */
- int def_chance = (1.0 - unit_win_chance(punit, apunit)) * 100;
-
- add_line(out, sizeof(out), _("Chance to win: A:%d%% D:%d%%"),
- att_chance, def_chance);
- }
- }
- add_line(out, sizeof(out), _("A:%d D:%d FP:%d HP:%d/%d%s"),
- ptype->attack_strength,
- ptype->defense_strength, ptype->firepower, punit->hp,
- ptype->hp, punit->veteran ? _(" V") : "");
- if (punit->owner == game.player_idx && unit_list_size(&ptile->units) >= 2){
- my_snprintf(buf, sizeof(buf), _(" (%d more)"),
- unit_list_size(&ptile->units) - 1);
- sz_strlcat(out, buf);
- }
- }
- return out;
-}
-
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/mapctrl_common.h clean/client/mapctrl_common.h
--- real.clean/client/mapctrl_common.h Sat Apr 3 09:35:29 2004
+++ clean/client/mapctrl_common.h Tue Apr 20 20:15:23 2004
@@ -54,7 +54,6 @@
void update_turn_done_button_state(void);
void update_line(int canvas_x, int canvas_y);
void overview_update_line(int overview_x, int overview_y);
-char* popup_info_text(int xtile, int ytile);
bool get_chance_to_win(int *att_chance, int *def_chance,
int map_x, int map_y);
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/text.c clean/client/text.c
--- real.clean/client/text.c Thu Jan 1 01:00:00 1970
+++ clean/client/text.c Tue Apr 20 21:04:16 2004
@@ -0,0 +1,509 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 2002 - The Freeciv Poject
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <stdarg.h>
+
+#include "map.h"
+#include "combat.h"
+#include "fcintl.h"
+#include "log.h"
+#include "support.h"
+#include "climisc.h"
+#include "government.h"
+
+#include "control.h"
+
+#include "text.h"
+
+/*
+ * An individual add(_line) have to fit into GROW_TMP_SIZE. One buffer
+ * of GROW_TMP_SIZE size will be allocated for the entire client.
+ */
+#define GROW_TMP_SIZE (1024)
+
+/*
+ * Initial size of the buffer of the various functions.
+ */
+#define START_SIZE 10
+
+static void real_add_line(char **buffer, size_t * buffer_size,
+ const char *format, ...)
+fc__attribute((format(printf, 3, 4)));
+static void real_add(char **buffer, size_t * buffer_size,
+ const char *format, ...)
+fc__attribute((format(printf, 3, 4)));
+
+#define add_line(...) real_add_line(&out,&out_size, __VA_ARGS__)
+#define add(...) real_add(&out,&out_size, __VA_ARGS__)
+
+#define INIT \
+ static char *out = NULL; \
+ static size_t out_size = 0; \
+ if (!out) { \
+ out_size = START_SIZE; \
+ out = fc_malloc(out_size); \
+ } \
+ out[0] = '\0';
+
+#define RETURN return out;
+
+static void grow_printf(char **buffer, size_t * buffer_size,
+ const char *format, va_list ap)
+{
+ size_t old_len = strlen(*buffer),add_len,new_len;
+ static char *buf;
+
+ if (!buf) {
+ buf = fc_malloc(GROW_TMP_SIZE);
+ }
+
+ if (my_vsnprintf(buf, GROW_TMP_SIZE, format, ap) == -1) {
+ die("Formatted string bigger than %d", GROW_TMP_SIZE);
+ }
+ add_len = strlen(buf);
+ new_len = old_len + add_len + 1;
+
+ if (new_len > *buffer_size) {
+ freelog(LOG_VERBOSE, "expand from %d to %d to add '%s'", *buffer_size,
+ new_len, buf);
+ *buffer_size = new_len;
+ *buffer = fc_realloc(*buffer, *buffer_size);
+ }
+ mystrlcat(*buffer, buf, *buffer_size);
+}
+
+/****************************************************************************
+ Add a full line of text to the buffer.
+****************************************************************************/
+static void real_add_line(char **buffer, size_t * buffer_size,
+ const char *format, ...)
+{
+ va_list args;
+
+ if ((*buffer)[0] != '\0') {
+ real_add(buffer, buffer_size, "%s", "\n");
+ }
+
+ va_start(args, format);
+ grow_printf(buffer, buffer_size, format, args);
+ va_end(args);
+}
+
+static void real_add(char **buffer, size_t * buffer_size, const char *format,
+ ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ grow_printf(buffer, buffer_size, format, args);
+ va_end(args);
+}
+
+/************************************************************************
+Text to popup on middle-click
+************************************************************************/
+const char* popup_info_text(int map_x, int map_y)
+{
+ const char *activity_text;
+ struct city *pcity = map_get_city(map_x, map_y);
+ struct tile *ptile = map_get_tile(map_x, map_y);
+ struct unit *punit = find_visible_unit(ptile);
+ const char *diplo_nation_plural_adjectives[DS_LAST] =
+ {Q_("?nation:Neutral"), Q_("?nation:Hostile"),
+ "" /* unused, DS_CEASEFIRE*/,
+ Q_("?nation:Peaceful"), Q_("?nation:Friendly"),
+ Q_("?nation:Mysterious"), Q_("?nation:Friendly(team)")};
+ const char *diplo_city_adjectives[DS_LAST] =
+ {Q_("?city:Neutral"), Q_("?city:Hostile"),
+ "" /*unused, DS_CEASEFIRE */,
+ Q_("?city:Peaceful"), Q_("?city:Friendly"), Q_("?city:Mysterious"),
+ Q_("?city:Friendly(team)")};
+ INIT;
+
+#ifdef DEBUG
+ add_line(_("Location: (%d, %d) [%d]"),
+ map_x, map_y, ptile->continent);
+#endif /*DEBUG*/
+ add_line(_("Terrain: %s"),
+ map_get_tile_info_text(map_x, map_y));
+ add_line(_("Food/Prod/Trade: %s"),
+ map_get_tile_fpt_text(map_x, map_y));
+ if (tile_has_special(ptile, S_HUT)) {
+ add_line(_("Minor Tribe Village"));
+ }
+ if (game.borders > 0 && !pcity) {
+ struct player *owner = map_get_owner(map_x, map_y);
+ struct player_diplstate *ds = game.player_ptr->diplstates;
+
+ if (owner == game.player_ptr){
+ add_line(_("Our Territory"));
+ } else if (owner) {
+ if (ds[owner->player_no].type == DS_CEASEFIRE) {
+ int turns = ds[owner->player_no].turns_left;
+
+ add_line(PL_("%s territory (%d turn ceasefire)",
+ "%s territory (%d turn ceasefire)",
+ turns),
+ get_nation_name(owner->nation), turns);
+ } else {
+ /* TRANS: "Territory of the friendly Polish". See the ?nation:
+ * adjectives. */
+ add_line(_("Territory of the %s %s"),
+ diplo_nation_plural_adjectives[ds[owner->player_no].type],
+ get_nation_name_plural(owner->nation));
+ }
+ } else {
+ add_line(_("Unclaimed territory"));
+ }
+ }
+ if (pcity) {
+ /* Look at city owner, not tile owner (the two should be the same, if
+ * borders are in use). */
+ struct player *owner = city_owner(pcity);
+ struct player_diplstate *ds = game.player_ptr->diplstates;
+ struct unit *apunit;
+
+ if (owner == game.player_ptr){
+ /* TRANS: "City: <name> (<nation>)" */
+ add_line(_("City: %s (%s)"), pcity->name,
+ get_nation_name(owner->nation));
+ } else if (owner) {
+ if (ds[owner->player_no].type == DS_CEASEFIRE) {
+ int turns = ds[owner->player_no].turns_left;
+
+ /* TRANS: City: Warsaw (Polish, 5 turn ceasefire)" */
+ add_line(PL_("City: %s (%s, %d turn ceasefire)",
+ "City: %s (%s, %d turn ceasefire)",
+ turns),
+ pcity->name,
+ get_nation_name(owner->nation),
+ turns);
+ } else {
+ /* TRANS: "City: <name> (<nation>,<diplomatic_state>)" */
+ add_line(_("City: %s (%s,%s)"), pcity->name,
+ get_nation_name(owner->nation),
+ diplo_city_adjectives[ds[owner->player_no].type]);
+ }
+ }
+ if (city_got_citywalls(pcity)) {
+ add("%s",_(" with City Walls"));
+ }
+
+ if ((apunit = get_unit_in_focus())) {
+ struct city *hcity = find_city_by_id(apunit->homecity);
+
+ if (unit_flag(apunit, F_TRADE_ROUTE)
+ && can_cities_trade(hcity, pcity)
+ && can_establish_trade_route(hcity, pcity)) {
+ add_line(_("Trade from %s: %d"),
+ hcity->name, trade_between_cities(hcity, pcity));
+ }
+ }
+ }
+ if (get_tile_infrastructure_set(ptile)) {
+ add_line(_("Infrastructure: %s"),
+ map_get_infrastructure_text(ptile->special));
+ }
+ activity_text = concat_tile_activity_text(map_x, map_y);
+ if (strlen(activity_text) > 0) {
+ add_line(_("Activity: %s"), activity_text);
+ }
+ if (punit && !pcity) {
+ char tmp[64] = {0};
+ struct unit_type *ptype = unit_type(punit);
+ if (punit->owner == game.player_idx) {
+ struct city *pcity;
+ pcity=player_find_city_by_id(game.player_ptr, punit->homecity);
+ if (pcity)
+ my_snprintf(tmp, sizeof(tmp), "/%s", pcity->name);
+ }
+ add_line(_("Unit: %s(%s%s)"), ptype->name,
+ get_nation_name(unit_owner(punit)->nation), tmp);
+ if (punit->owner != game.player_idx){
+ struct unit *apunit;
+ if ((apunit = get_unit_in_focus())) {
+ /* chance to win when active unit is attacking the selected unit */
+ int att_chance = unit_win_chance(apunit, punit) * 100;
+
+ /* chance to win when selected unit is attacking the active unit */
+ int def_chance = (1.0 - unit_win_chance(punit, apunit)) * 100;
+
+ add_line(_("Chance to win: A:%d%% D:%d%%"),
+ att_chance, def_chance);
+ }
+ }
+ add_line(_("A:%d D:%d FP:%d HP:%d/%d%s"),
+ ptype->attack_strength,
+ ptype->defense_strength, ptype->firepower, punit->hp,
+ ptype->hp, punit->veteran ? _(" V") : "");
+ if (punit->owner == game.player_idx && unit_list_size(&ptile->units) >= 2){
+ add(_(" (%d more)"), unit_list_size(&ptile->units) - 1);
+ }
+ }
+ RETURN;
+}
+
+
+/**************************************************************************
+ Concats buf with activity progress text for given tile. Returns
+ number of activities.
+**************************************************************************/
+const char *concat_tile_activity_text(int map_x, int map_y)
+{
+ int activity_total[ACTIVITY_LAST];
+ int activity_units[ACTIVITY_LAST];
+ int num_activities = 0;
+ int remains, turns, i, mr, au;
+ struct tile *ptile = map_get_tile(map_x, map_y);
+ INIT;
+
+ memset(activity_total, 0, sizeof(activity_total));
+ memset(activity_units, 0, sizeof(activity_units));
+
+ unit_list_iterate(ptile->units, punit) {
+ mr = get_unit_type(punit->type)->move_rate;
+ au = (mr > 0) ? mr / SINGLE_MOVE : 1;
+ activity_total[punit->activity] += punit->activity_count;
+ if (punit->moves_left > 0) {
+ /* current turn */
+ activity_total[punit->activity] += au;
+ }
+ activity_units[punit->activity] += au;
+ }
+ unit_list_iterate_end;
+
+ for (i = 0; i < ACTIVITY_LAST; i++) {
+ if (is_build_or_clean_activity(i) && activity_units[i] > 0) {
+ if (num_activities > 0) {
+ add("/");
+ }
+ remains = map_activity_time(i, map_x, map_y) - activity_total[i];
+ if (remains > 0) {
+ turns = 1 + (remains + activity_units[i] - 1) / activity_units[i];
+ } else {
+ /* activity will be finished this turn */
+ turns = 1;
+ }
+ add("%s(%d)", get_activity_text(i), turns);
+ num_activities++;
+ }
+ }
+
+ RETURN;
+}
+
+#define FAR_CITY_SQUARE_DIST (2*(6*6))
+
+/**************************************************************************
+ Fill buf (of size bufsz) with proper nearest city message.
+ Returns buf.
+**************************************************************************/
+const char *get_nearest_city_text(struct city *pcity, int sq_dist)
+{
+ INIT;
+
+ /* just to be sure */
+ if (!pcity) {
+ sq_dist = -1;
+ }
+
+ add((sq_dist >= FAR_CITY_SQUARE_DIST) ? _("far from %s")
+ : (sq_dist > 0) ? _("near %s")
+ : (sq_dist == 0) ? _("in %s")
+ : "%s", pcity ? pcity->name : "");
+
+ RETURN;
+}
+
+/**************************************************************************
+ Returns unit description (as static buffer).
+**************************************************************************/
+const char *unit_description(struct unit *punit)
+{
+ int pcity_near_dist;
+ struct city *pcity =
+ player_find_city_by_id(game.player_ptr, punit->homecity);
+ struct city *pcity_near = get_nearest_city(punit, &pcity_near_dist);
+ struct unit_type *ptype = unit_type(punit);
+ INIT;
+
+ add(_("%s"), ptype->name);
+ if (ptype->veteran[punit->veteran].name[0] != '\0') {
+ add(" (%s)", ptype->veteran[punit->veteran].name);
+ }
+ add("\n");
+ add_line("%s", unit_activity_text(punit));
+
+ if (pcity) {
+ add_line(_("from %s"), pcity->name);
+ } else {
+ add("\n");
+ }
+
+ add_line("%s", get_nearest_city_text(pcity_near, pcity_near_dist));
+
+ RETURN;
+}
+
+/**************************************************************************
+ Returns the text to display in the science dialog.
+**************************************************************************/
+const char *science_dialog_text(void)
+{
+ int turns_to_advance;
+ struct player *plr = game.player_ptr;
+ int ours = 0, theirs = 0;
+ INIT;
+
+ /* Sum up science */
+ players_iterate(pplayer) {
+ enum diplstate_type ds = pplayer_get_diplstate(plr, pplayer)->type;
+
+ if (plr == pplayer) {
+ city_list_iterate(pplayer->cities, pcity) {
+ ours += pcity->science_total;
+ } city_list_iterate_end;
+ } else if (ds == DS_TEAM) {
+ theirs += pplayer->research.bulbs_last_turn;
+ }
+ } players_iterate_end;
+
+ if (ours == 0 && theirs == 0) {
+ add(_("Progress: no research"));
+ RETURN;
+ }
+ if (ours < 0 || theirs < 0) {
+ die("Negative science in science_dialog_text");
+ }
+ turns_to_advance = (total_bulbs_required(plr) + ours + theirs - 1)
+ / (ours + theirs);
+ if (theirs == 0) {
+ /* Simple version, no techpool */
+ add(PL_("Progress: %d turn/advance (%d pts/turn)",
+ "Progress: %d turns/advance (%d pts/turn)",
+ turns_to_advance), turns_to_advance, ours);
+ } else {
+ /* Techpool version */
+ add(PL_("Progress: %d turn/advance (%d pts/turn, "
+ "%d pts/turn from team)",
+ "Progress: %d turns/advance (%d pts/turn, "
+ "%d pts/turn from team)",
+ turns_to_advance), turns_to_advance, ours, theirs);
+ }
+ RETURN;
+}
+
+const char *get_info_label_text(void)
+{
+ INIT;
+
+ add_line(_("Population: %s"),
+ population_to_text(civ_population(game.player_ptr)));
+ add_line(_("Year: %s"), textyear(game.year));
+ add_line(_("Gold: %d"), game.player_ptr->economic.gold);
+ add(_("Tax: %d Lux: %d Sci: %d"), game.player_ptr->economic.tax,
+ game.player_ptr->economic.luxury, game.player_ptr->economic.science);
+ RETURN;
+}
+
+const char *get_unit_info_label_text1(struct unit *punit)
+{
+ INIT;
+
+ if (punit) {
+ struct unit_type *ptype = unit_type(punit);
+
+ add("%s", ptype->name);
+
+ if (ptype->veteran[punit->veteran].name[0] != '\0') {
+ add(" (%s)", _(ptype->veteran[punit->veteran].name));
+ }
+ }
+ RETURN;
+}
+
+const char *get_unit_info_label_text2(struct unit *punit)
+{
+ INIT;
+
+ if (punit) {
+ struct city *pcity =
+ player_find_city_by_id(game.player_ptr, punit->homecity);
+ int infrastructure =
+ get_tile_infrastructure_set(map_get_tile(punit->x, punit->y));
+
+ if (hover_unit == punit->id) {
+ add_line(_("Select destination"));
+ } else {
+ add_line("%s", unit_activity_text(punit));
+ }
+
+ add_line("%s", map_get_tile_info_text(punit->x, punit->y));
+ if (infrastructure) {
+ add_line("%s", map_get_infrastructure_text(infrastructure));
+ }
+ if (pcity) {
+ add("%s", pcity->name);
+ }
+ if (infrastructure) {
+ add("\n");
+ }
+ } else {
+ add("\n\n\n");
+ }
+ RETURN;
+}
+
+const char *get_bulb_tooltip(void)
+{
+ INIT;
+
+ add(_("Shows your progress in researching "
+ "the current technology.\n%s: %d/%d."),
+ advances[game.player_ptr->research.researching].name,
+ game.player_ptr->research.bulbs_researched,
+ total_bulbs_required(game.player_ptr));
+ RETURN;
+}
+
+const char *get_global_warming_tooltip(void)
+{
+ INIT;
+
+ add(_("Shows the progress of global warming:\n%d."),
+ client_warming_sprite());
+ RETURN;
+}
+
+const char *get_nuclear_winter_tooltip(void)
+{
+ INIT;
+
+ add(_("Shows the progress of nuclear winter:\n%d."),
+ client_cooling_sprite());
+ RETURN;
+}
+
+const char *get_government_tooltip(void)
+{
+ INIT;
+
+ add(_("Shows your current government:\n%s."),
+ get_government_name(game.player_ptr->government));
+ RETURN;
+}
diff -Nurd -Xclean/diff_ignore -xdata -x'*.gz' -x'*.sav'
real.clean/client/text.h clean/client/text.h
--- real.clean/client/text.h Thu Jan 1 01:00:00 1970
+++ clean/client/text.h Tue Apr 20 20:15:23 2004
@@ -0,0 +1,30 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 2004 - The Freeciv Poject
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__TEXT_H
+#define FC__TEXT_H
+
+const char* popup_info_text(int xtile, int ytile);
+const char *concat_tile_activity_text(int x, int y);
+const char *get_nearest_city_text(struct city *pcity, int sq_dist);
+const char *unit_description(struct unit *punit);
+const char *science_dialog_text(void);
+const char *get_info_label_text(void);
+const char *get_bulb_tooltip(void);
+const char *get_global_warming_tooltip(void);
+const char *get_nuclear_winter_tooltip(void);
+const char *get_government_tooltip(void);
+const char *get_unit_info_label_text1(struct unit *punit);
+const char *get_unit_info_label_text2(struct unit *punit);
+
+#endif /* FC__TEXT_H */
|
|