Freeciv21
Develop your civilization from humble roots to a global empire
view_nations_data.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2023 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 /*
15  * This file contains functions used to gather varying data elements for use
16  * in the nations view (formally known as the plrdlg - player dialog).
17  */
18 
19 // utility
20 #include "fcintl.h"
21 #include "support.h"
22 
23 // common
24 #include "game.h"
25 #include "government.h"
26 #include "nation.h"
27 #include "research.h"
28 
29 // client
30 #include "client_main.h"
31 #include "climisc.h"
32 #include "options.h"
33 #include "text.h"
34 
35 /* client/include */
37 
41 static QString col_name(const struct player *player)
42 {
43  return player_name(player);
44 }
45 
49 static int cmp_name(const struct player *pplayer1,
50  const struct player *pplayer2)
51 {
52  return fc_stricoll(player_name(pplayer1), player_name(pplayer2));
53 }
54 
58 static QString col_username(const struct player *player)
59 {
60  return player->username;
61 }
62 
66 static QString col_nation(const struct player *player)
67 {
69 }
70 
74 static QString col_team(const struct player *player)
75 {
77 }
78 
82 static bool col_ai(const struct player *plr)
83 {
84  /* TODO: Currently is_ai() is a macro so we can't have it
85  * directly as this callback, but once it's a function,
86  * do that. */
87  return is_ai(plr);
88 }
89 
94 static QString col_embassy(const struct player *player)
95 {
97 }
98 
103 static QString col_diplstate(const struct player *player)
104 {
105  static char buf[100];
106  const struct player_diplstate *pds;
107 
108  if (nullptr == client.conn.playing || player == client.conn.playing) {
109  return QStringLiteral("-");
110  } else {
112  if (pds->type == DS_CEASEFIRE || pds->type == DS_ARMISTICE) {
113  fc_snprintf(buf, sizeof(buf), "%s (%d)",
114  diplstate_type_translated_name(pds->type),
115  pds->turns_left);
116  return buf;
117  } else {
118  return diplstate_type_translated_name(pds->type);
119  }
120  }
121 }
122 
129 static int diplstate_value(const struct player *plr)
130 {
131  /* these values are scaled so that adding/subtracting
132  the number of turns left makes sense */
133 
134  static int diplstate_cmp_lookup[DS_LAST];
135  diplstate_cmp_lookup[DS_TEAM] = 1 << 16;
136  diplstate_cmp_lookup[DS_ALLIANCE] = 2 << 16;
137  diplstate_cmp_lookup[DS_PEACE] = 3 << 16;
138  diplstate_cmp_lookup[DS_ARMISTICE] = 4 << 16;
139  diplstate_cmp_lookup[DS_CEASEFIRE] = 5 << 16;
140  diplstate_cmp_lookup[DS_WAR] = 6 << 16;
141  diplstate_cmp_lookup[DS_NO_CONTACT] = 7 << 16;
142 
143  const struct player_diplstate *pds;
144  int ds_value;
145 
146  if (nullptr == client.conn.playing || plr == client.conn.playing) {
147  /* current global player is as close as players get
148  -> return value smaller than for DS_TEAM */
149  return 0;
150  }
151 
153  ds_value = diplstate_cmp_lookup[pds->type];
154 
155  if (pds->type == DS_ARMISTICE || pds->type == DS_CEASEFIRE) {
156  ds_value += pds->turns_left;
157  }
158 
159  return ds_value;
160 }
161 
165 static int cmp_diplstate(const struct player *player1,
166  const struct player *player2)
167 {
168  return diplstate_value(player1) - diplstate_value(player2);
169 }
170 
174 static QString col_love(const struct player *player)
175 {
176  if (nullptr == client.conn.playing || player == client.conn.playing
177  || is_human(player)) {
178  return QStringLiteral("-");
179  } else {
180  return love_text(
182  }
183 }
184 
188 static int cmp_love(const struct player *player1,
189  const struct player *player2)
190 {
191  int love1, love2;
192 
193  if (nullptr == client.conn.playing) {
194  return player_number(player1) - player_number(player2);
195  }
196 
197  if (player1 == client.conn.playing || is_human(player1)) {
198  love1 = MAX_AI_LOVE + 999;
199  } else {
200  love1 = player1->ai_common.love[player_index(client.conn.playing)];
201  }
202 
203  if (player2 == client.conn.playing || is_human(player2)) {
204  love2 = MAX_AI_LOVE + 999;
205  } else {
206  love2 = player2->ai_common.love[player_index(client.conn.playing)];
207  }
208 
209  return love1 - love2;
210 }
211 
215 static QString col_vision(const struct player *player)
216 {
218 }
219 
225 QString plrdlg_col_state(const struct player *plr)
226 {
227  if (!plr->is_alive) {
228  // TRANS: Dead -- Rest In Peace -- Reqia In Pace
229  return _("R.I.P.");
230  } else if (!plr->is_connected) {
231  struct option *opt;
232  bool consider_tb = false;
233 
234  if (is_ai(plr)) {
235  return QLatin1String("");
236  }
237 
238  opt = optset_option_by_name(server_optset, "turnblock");
239  if (opt != nullptr) {
240  consider_tb = option_bool_get(opt);
241  }
242 
243  if (!consider_tb) {
244  // TRANS: No connection
245  return _("noconn");
246  }
247 
248  if (!is_player_phase(plr, game.info.phase)) {
249  return _("waiting");
250  } else if (plr->phase_done) {
251  return _("done");
252  } else {
253  // TRANS: Turnblocking & player not connected
254  return _("blocking");
255  }
256  } else {
257  if (!is_player_phase(plr, game.info.phase)) {
258  return _("waiting");
259  } else if (plr->phase_done) {
260  return _("done");
261  } else {
262  return _("moving");
263  }
264  }
265 }
266 
270 static QString col_idle(const struct player *plr)
271 {
272  int idle;
273  static char buf[100];
274  // nturns_idle >= 0
275  if (plr->nturns_idle == 0) {
276  idle = 0;
277  } else {
278  idle = plr->nturns_idle - 1;
279  }
280  fc_snprintf(buf, sizeof(buf), "%d", idle);
281  return buf;
282 }
283 
287 static int cmp_score(const struct player *player1,
288  const struct player *player2)
289 {
290  return player1->score.game - player2->score.game;
291 }
292 
296 QString col_government(const struct player *them)
297 {
298  if (!them || !them->is_alive) {
299  return _("-");
300  } else if (BV_ISSET(them->client.visible, NI_GOVERNMENT)) {
301  return government_name_for_player(them);
302  } else {
303  return _("?");
304  }
305 }
306 
311 static int cmp_culture(const struct player *player1,
312  const struct player *player2)
313 {
314  return player1->client.culture - player2->client.culture;
315 }
316 
320 QString get_culture_info(const struct player *them)
321 {
322  if (them == nullptr || !them->is_alive) {
323  return _("-");
324  } else if (BV_ISSET(them->client.visible, NI_CULTURE)) {
325  return QString::number(them->client.culture);
326  } else {
327  return _("?");
328  }
329 }
330 
334 QString col_culture(const struct player *pplayer)
335 {
336  return get_culture_info(pplayer);
337 }
338 
343 static int cmp_gold(const struct player *player1,
344  const struct player *player2)
345 {
346  return player1->economic.gold - player2->economic.gold;
347 }
348 
352 QString col_gold(const struct player *them)
353 {
354  if (them == nullptr || !them->is_alive) {
355  return _("-");
356  } else if (BV_ISSET(them->client.visible, NI_GOLD)) {
357  return QString::number(them->economic.gold);
358  } else {
359  return _("?");
360  }
361 }
362 
367 static int cmp_tax(const struct player *player1,
368  const struct player *player2)
369 {
370  return player1->economic.tax - player2->economic.tax;
371 }
372 
376 QString col_tax(const struct player *them)
377 {
378  if (them == nullptr || !them->is_alive) {
379  return _("-");
380  } else if (BV_ISSET(them->client.visible, NI_TAX_RATES)) {
381  return QString::number(them->economic.tax);
382  } else {
383  return _("?");
384  }
385 }
386 
391 static int cmp_science(const struct player *player1,
392  const struct player *player2)
393 {
394  return player1->economic.science - player2->economic.science;
395 }
396 
400 QString col_science(const struct player *them)
401 {
402  if (them == nullptr || !them->is_alive) {
403  return _("-");
404  } else if (BV_ISSET(them->client.visible, NI_TAX_RATES)) {
405  return QString::number(them->economic.science);
406  } else {
407  return _("?");
408  }
409 }
410 
415 static int cmp_luxury(const struct player *player1,
416  const struct player *player2)
417 {
418  return player1->economic.luxury - player2->economic.luxury;
419 }
420 
424 QString col_luxury(const struct player *them)
425 {
426  if (them == nullptr || !them->is_alive) {
427  return _("-");
428  } else if (BV_ISSET(them->client.visible, NI_TAX_RATES)) {
429  return QString::number(them->economic.luxury);
430  } else {
431  return _("?");
432  }
433 }
434 
438 QString col_research(const struct player *them)
439 {
440  if (them == nullptr || !them->is_alive) {
441  return _("-");
442  } else if (BV_ISSET(them->client.visible, NI_TECHS)) {
443  struct research *research = research_get(them);
444  if (research->client.researching_cost == 0) {
445  // Nothing is selected to research
446  return QStringLiteral("%1 (%2/-)")
449  .arg(research->bulbs_researched);
450  } else {
451  // Give the full research string
452  return QStringLiteral("%1 (%2/%3)")
456  .arg(research->client.researching_cost);
457  };
458  } else {
459  // We have no embassy so can't show anything
460  return _("?");
461  }
462 }
467  {true, COL_TEXT, N_("?Player:Name"), col_name, nullptr, cmp_name,
468  "name"},
469  {false, COL_TEXT, N_("Username"), col_username, nullptr, nullptr,
470  "username"},
471  {true, COL_FLAG, N_("Flag"), nullptr, nullptr, nullptr, "flag"},
472  {true, COL_TEXT, N_("Nation"), col_nation, nullptr, nullptr, "nation"},
473  {true, COL_COLOR, N_("Border"), nullptr, nullptr, nullptr, "border"},
474  {true, COL_RIGHT_TEXT, N_("Score"), get_score_text, nullptr, cmp_score,
475  "score"},
476  {true, COL_TEXT, N_("Team"), col_team, nullptr, nullptr, "team"},
477  {true, COL_BOOLEAN, N_("AI"), nullptr, col_ai, nullptr, "ai"},
478  {true, COL_TEXT, N_("Attitude"), col_love, nullptr, cmp_love,
479  "attitude"},
480  {true, COL_TEXT, N_("Embassy"), col_embassy, nullptr, nullptr,
481  "embassy"},
482  {true, COL_TEXT, N_("Dipl.State"), col_diplstate, nullptr, cmp_diplstate,
483  "diplstate"},
484  {true, COL_TEXT, N_("Vision"), col_vision, nullptr, nullptr, "vision"},
485  {true, COL_TEXT, N_("State"), plrdlg_col_state, nullptr, nullptr,
486  "state"},
487  {false, COL_RIGHT_TEXT, N_("?Player_dlg:Idle"), col_idle, nullptr,
488  nullptr, "idle"},
489  {true, COL_GOVERNMENT, N_("Government"), col_government, nullptr,
490  nullptr, "government"},
491  {true, COL_RIGHT_TEXT, N_("Culture"), col_culture, nullptr, cmp_culture,
492  "culture"},
493  {true, COL_RIGHT_TEXT, N_("Gold"), col_gold, nullptr, cmp_gold, "gold"},
494  {true, COL_RIGHT_TEXT, N_("Tax"), col_tax, nullptr, cmp_tax, "tax"},
495  {true, COL_RIGHT_TEXT, N_("Science"), col_science, nullptr, cmp_science,
496  "science"},
497  {true, COL_RIGHT_TEXT, N_("Luxury"), col_luxury, nullptr, cmp_luxury,
498  "luxury"},
499  {true, COL_TEXT, N_("Research"), col_research, nullptr, nullptr,
500  "research"}};
501 
503 
508 {
509  int i;
510 
511  for (i = 0; i < num_player_dlg_columns; i++) {
513  }
514 }
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
struct civclient client
const char * get_vision_status(const struct player *me, const struct player *them)
Return a string indicating one nation's shaed vision status with another.
Definition: climisc.cpp:194
const char * get_embassy_status(const struct player *me, const struct player *them)
Return a string indicating one nation's embassy status with another.
Definition: climisc.cpp:169
#define Q_(String)
Definition: fcintl.h:53
#define _(String)
Definition: fcintl.h:50
#define N_(String)
Definition: fcintl.h:52
struct civ_game game
Definition: game.cpp:47
bool is_player_phase(const struct player *pplayer, int phase)
Return TRUE if it is this player's phase.
Definition: game.cpp:672
const char * government_name_for_player(const struct player *pplayer)
Return the (translated) name of the given government of a player.
Definition: government.cpp:147
const char * nation_adjective_for_player(const struct player *pplayer)
Return the (translated) adjective for the given nation of a player.
Definition: nation.cpp:146
const struct option_set * server_optset
Definition: options.cpp:2430
struct option * optset_option_by_name(const struct option_set *poptset, const char *name)
Returns the option corresponding of the name in this option set.
Definition: options.cpp:110
bool option_bool_get(const struct option *poption)
Returns the current value of this boolean option.
Definition: options.cpp:457
int player_number(const struct player *pplayer)
Return the player index/number/id.
Definition: player.cpp:756
const char * love_text(const int love)
Return a text describing an AI's love for you.
Definition: player.cpp:1283
int player_index(const struct player *pplayer)
Return the player index.
Definition: player.cpp:748
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: player.cpp:288
#define MAX_AI_LOVE
Definition: player.h:546
#define is_ai(plr)
Definition: player.h:227
#define is_human(plr)
Definition: player.h:226
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
QString research_advance_name_translation(const struct research *presearch, Tech_type_id tech)
Store the translated name of the given tech (including A_FUTURE) in 'buf'.
Definition: research.cpp:257
#define ARRAY_SIZE(x)
Definition: shared.h:79
struct packet_game_info info
Definition: game.h:80
struct connection conn
Definition: client_main.h:89
struct player * playing
Definition: connection.h:142
The base class for options.
Definition: options.cpp:209
int love[MAX_NUM_PLAYER_SLOTS]
Definition: player.h:117
enum diplstate_type type
Definition: player.h:193
int game
Definition: player.h:103
Definition: player.h:231
struct player_ai ai_common
Definition: player.h:270
struct player::@65::@68 client
char username[MAX_LEN_NAME]
Definition: player.h:234
bool is_connected
Definition: player.h:278
int nturns_idle
Definition: player.h:247
struct team * team
Definition: player.h:243
bool is_alive
Definition: player.h:250
struct player_economic economic
Definition: player.h:266
struct player_score score
Definition: player.h:265
bool phase_done
Definition: player.h:245
Tech_type_id researching
Definition: research.h:45
struct research::@71::@73 client
int bulbs_researched
Definition: research.h:46
int fc_snprintf(char *str, size_t n, const char *format,...)
See also fc_utf8_snprintf_trunc(), fc_utf8_snprintf_rep().
Definition: support.cpp:537
int fc_stricoll(const char *str0, const char *str1)
Wrapper function for stricoll().
Definition: support.cpp:235
const char * team_name_translation(const struct team *pteam)
Returns the name (translated) of the team.
Definition: team.cpp:393
QString get_score_text(const struct player *pplayer)
Return text giving the score of the player.
Definition: text.cpp:1256
static QString col_team(const struct player *player)
The name of the player's team (or empty) for the plrdlg.
static int cmp_name(const struct player *pplayer1, const struct player *pplayer2)
Compares the names of two players in players dialog.
QString col_tax(const struct player *them)
Show player's tax to me if I am allowed to know it.
static bool col_ai(const struct player *plr)
TRUE if the player is AI-controlled.
static int cmp_culture(const struct player *player1, const struct player *player2)
Compare culture of two players in players dialog, needed to sort column.
static int cmp_diplstate(const struct player *player1, const struct player *player2)
Compares diplomatic status of two players in players dialog.
QString plrdlg_col_state(const struct player *plr)
Returns a translated string giving the player's "state".
static int cmp_score(const struct player *player1, const struct player *player2)
Compare scores of two players in players dialog.
static int cmp_tax(const struct player *player1, const struct player *player2)
Compare tax of two players in players dialog, needed to sort column.
static QString col_username(const struct player *player)
The username (connection name) column of the plrdlg.
static QString col_vision(const struct player *player)
Returns a translated string giving our shared-vision status.
static QString col_love(const struct player *player)
Return a string displaying the AI's love (or not) for you...
QString col_research(const struct player *them)
Show player's research to me if I am allowed to know it.
QString col_culture(const struct player *pplayer)
Player's culture value.
static QString col_embassy(const struct player *player)
Returns a translated string giving the embassy status (none/with us/with them/both).
QString col_gold(const struct player *them)
Show player's gold to me if I am allowed to know it.
static int diplstate_value(const struct player *plr)
Return a numerical value suitable for ordering players by their diplomatic status in the players dial...
static int cmp_luxury(const struct player *player1, const struct player *player2)
Compare luxury of two players in players dialog, needed to sort column.
struct player_dlg_column player_dlg_columns[]
...
static int cmp_gold(const struct player *player1, const struct player *player2)
Compare gold of two players in players dialog, needed to sort column.
static int cmp_science(const struct player *player1, const struct player *player2)
Compare science of two players in players dialog, needed to sort column.
QString col_science(const struct player *them)
Show player's science to me if I am allowed to know it.
void init_player_dlg_common()
Translate all titles.
static QString col_diplstate(const struct player *player)
Returns a translated string giving the diplomatic status ("war" or "ceasefire (5)").
static QString col_idle(const struct player *plr)
Returns a string telling how many turns the player has been idle.
QString col_luxury(const struct player *them)
Show player's luxury to me if I am allowed to know it.
QString col_government(const struct player *them)
The name of the player's government.
QString get_culture_info(const struct player *them)
Show others player's culture to me if I am allowed to know it.
static QString col_name(const struct player *player)
The player-name (aka nation leader) column of the plrdlg.
static QString col_nation(const struct player *player)
The name of the player's nation for the plrdlg.
static int cmp_love(const struct player *player1, const struct player *player2)
Compares ai's attitude toward the player.
const int num_player_dlg_columns
@ COL_TEXT
@ COL_BOOLEAN
@ COL_FLAG
@ COL_RIGHT_TEXT
@ COL_COLOR
@ COL_GOVERNMENT