Freeciv-3.2
Loading...
Searching...
No Matches
government.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "fcintl.h"
20#include "log.h"
21#include "mem.h"
22#include "shared.h"
23#include "string_vector.h"
24#include "support.h"
25
26/* common */
27#include "game.h"
28#include "nation.h"
29#include "player.h"
30#include "tech.h"
31
32#include "government.h"
33
35
36/**********************************************************************/
41{
43 if (0 == strcmp(government_name_translation(gov), name)) {
44 return gov;
45 }
47
48 return NULL;
49}
50
51/**********************************************************************/
56{
57 const char *qname = Qn_(name);
58
61 return gov;
62 }
64
65 return NULL;
66}
67
68/**********************************************************************/
75
76/**********************************************************************/
87
88/**********************************************************************/
92{
94 return pgovern->item_number;
95}
96
97/**********************************************************************/
104{
106 return NULL;
107 }
108 return &governments[gov];
109}
110
111/**********************************************************************/
114struct government *government_of_player(const struct player *pplayer)
115{
116 fc_assert_ret_val(NULL != pplayer, NULL);
117 return pplayer->government;
118}
119
120/**********************************************************************/
123struct government *government_of_city(const struct city *pcity)
124{
125 fc_assert_ret_val(NULL != pcity, NULL);
126 return government_of_player(city_owner(pcity));
127}
128
129/**********************************************************************/
133const char *government_rule_name(const struct government *pgovern)
134{
136 return rule_name_get(&pgovern->name);
137}
138
139/**********************************************************************/
144{
146
147 return name_translation_get(&pgovern->name);
148}
149
150/**********************************************************************/
154const char *government_name_for_player(const struct player *pplayer)
155{
156 if (!pplayer->is_alive) {
157 return "-";
158 }
159
161}
162
163/**********************************************************************/
170bool can_change_to_government(struct player *pplayer,
171 const struct government *gov)
172{
174
175 if (!pplayer) {
176 return FALSE;
177 }
178
179 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
180 /* Note, this may allow govs that are on someone else's "tech tree". */
181 return TRUE;
182 }
183
184 return are_reqs_active(&(const struct req_context) { .player = pplayer },
185 NULL, &gov->reqs, RPT_CERTAIN);
186}
187
188
189/**************************************************************************
190 Ruler titles.
191**************************************************************************/
197
198/**********************************************************************/
201static genhash_val_t nation_hash_val(const struct nation_type *pnation)
202{
203 return NULL != pnation ? nation_number(pnation) : nation_count();
204}
205
206/**********************************************************************/
209static bool nation_hash_comp(const struct nation_type *pnation1,
210 const struct nation_type *pnation2)
211{
212 return pnation1 == pnation2;
213}
214
215/**********************************************************************/
218static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
219 const char *domain,
220 const char *ruler_male_title,
221 const char *ruler_female_title)
222{
224
225 pruler_title->pnation = pnation;
228
229 return pruler_title;
230}
231
232/**********************************************************************/
236{
238}
239
240/**********************************************************************/
244{
245 bool ret = TRUE;
246
247 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
248 if (NULL != pruler_title->pnation) {
249 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
250 "is not a format. It should match \"%%s\"",
253 nation_number(pruler_title->pnation));
254 } else {
255 log_error("\"%s\" male ruler title is not a format. "
256 "It should match \"%%s\"",
258 }
259 ret = FALSE;
260 }
261
262 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
263 if (NULL != pruler_title->pnation) {
264 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
265 "is not a format. It should match \"%%s\"",
266 rule_name_get(&pruler_title->female),
268 nation_number(pruler_title->pnation));
269 } else {
270 log_error("\"%s\" female ruler title is not a format. "
271 "It should match \"%%s\"",
272 rule_name_get(&pruler_title->female));
273 }
274 ret = FALSE;
275 }
276
277 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
278 if (NULL != pruler_title->pnation) {
279 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
280 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
283 nation_number(pruler_title->pnation),
285 } else {
286 log_error("Translation of \"%s\" male ruler title is not a format "
287 "(\"%s\"). It should match \"%%s\"",
290 }
291 ret = FALSE;
292 }
293
294 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
295 if (NULL != pruler_title->pnation) {
296 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
297 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
298 rule_name_get(&pruler_title->female),
300 nation_number(pruler_title->pnation),
302 } else {
303 log_error("Translation of \"%s\" female ruler title is not a format "
304 "(\"%s\"). It should match \"%%s\"",
305 rule_name_get(&pruler_title->female),
307 }
308 ret = FALSE;
309 }
310
311 return ret;
312}
313
314/**********************************************************************/
317const struct ruler_title_hash *
319{
321 return pgovern->ruler_titles;
322}
323
324/**********************************************************************/
328struct ruler_title *
330 const struct nation_type *pnation,
331 const char *ruler_male_title,
332 const char *ruler_female_title)
333{
334 const char *domain = NULL;
336
337 if (pnation != NULL) {
339 }
342
345 return NULL;
346 }
347
348 if (ruler_title_hash_replace(pgovern->ruler_titles,
350 if (NULL != pnation) {
351 log_error("Ruler title for government \"%s\" (nb %d) and "
352 "nation \"%s\" (nb %d) was set twice.",
355 } else {
356 log_error("Default ruler title for government \"%s\" (nb %d) "
357 "was set twice.", government_rule_name(pgovern),
359 }
360 }
361
362 return pruler_title;
363}
364
365/**********************************************************************/
368const struct nation_type *
370{
371 return pruler_title->pnation;
372}
373
374/**********************************************************************/
377const char *
382
383/**********************************************************************/
386const char *
391
392/**********************************************************************/
395const char *ruler_title_for_player(const struct player *pplayer,
396 char *buf, size_t buf_len)
397{
398 const struct government *pgovern = government_of_player(pplayer);
399 const struct nation_type *pnation = nation_of_player(pplayer);
401
404
405 /* Try specific nation ruler title. */
406 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
408 /* Try default ruler title. */
409 && !ruler_title_hash_lookup(pgovern->ruler_titles,
410 NULL, &pruler_title)) {
411 log_error("Missing title for government \"%s\" (nb %d) "
412 "nation \"%s\" (nb %d).",
416 } else {
419 ? &pruler_title->male
420 : &pruler_title->female),
421 player_name(pplayer));
422 }
423
424 return buf;
425}
426
427/**********************************************************************/
430const char *default_title_for_player(const struct player *pplayer,
431 char *buf, size_t buf_len)
432{
433 if (pplayer->is_male) {
434 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
435 } else {
436 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
437 }
438
439 return buf;
440}
441
442/**************************************************************************
443 Government iterator.
444**************************************************************************/
447 struct government *p, *end;
448};
449#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
450
451/**********************************************************************/
455{
456 return sizeof(struct government_iter);
457}
458
459/**********************************************************************/
463{
464 GOVERNMENT_ITER(iter)->p++;
465}
466
467/**********************************************************************/
470static void *government_iter_get(const struct iterator *iter)
471{
472 return GOVERNMENT_ITER(iter)->p;
473}
474
475/**********************************************************************/
478static bool government_iter_valid(const struct iterator *iter)
479{
481 return it->p < it->end;
482}
483
484/**********************************************************************/
496
497/**********************************************************************/
500static inline void government_init(struct government *pgovern)
501{
502 memset(pgovern, 0, sizeof(*pgovern));
503
504 pgovern->item_number = pgovern - governments;
509 pgovern->changed_to_times = 0;
510 pgovern->ruledit_disabled = FALSE;
511 pgovern->ruledit_dlg = NULL;
512}
513
514/**********************************************************************/
517static inline void government_free(struct government *pgovern)
518{
519 ruler_title_hash_destroy(pgovern->ruler_titles);
520 pgovern->ruler_titles = NULL;
521
522 if (NULL != pgovern->helptext) {
523 strvec_destroy(pgovern->helptext);
524 pgovern->helptext = NULL;
525 }
526
528}
529
530/**********************************************************************/
533void governments_alloc(int num)
534{
535 int i;
536
538 governments = fc_malloc(sizeof(*governments) * num);
540
541 for (i = 0; i < game.control.government_count; i++) {
543 }
544}
545
546/**********************************************************************/
550{
551 int i;
552
553 if (NULL == governments) {
554 return;
555 }
556
557 for (i = 0; i < game.control.government_count; i++) {
559 }
560
564}
565
566/**********************************************************************/
571{
574 /* We need to know the target government at the onset of the revolution
575 * in order to know how long anarchy will last. */
576 return FALSE;
577 }
578 return TRUE;
579}
#define city_owner(_pcity_)
Definition city.h:563
char * incite_cost
Definition comments.c:75
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:828
@ RPT_CERTAIN
Definition fc_types.h:701
@ REVOLEN_RANDQUICK
Definition fc_types.h:1286
@ REVOLEN_QUICKENING
Definition fc_types.h:1285
int Government_type_id
Definition fc_types.h:381
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
struct civ_game game
Definition game.c:62
unsigned int genhash_val_t
Definition genhash.h:32
const char * government_name_translation(const struct government *pgovern)
Definition government.c:143
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:430
void governments_free(void)
Definition government.c:549
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:369
bool untargeted_revolution_allowed(void)
Definition government.c:570
static void government_init(struct government *pgovern)
Definition government.c:500
struct ruler_title * government_ruler_title_new(struct government *pgovern, const struct nation_type *pnation, const char *ruler_male_title, const char *ruler_female_title)
Definition government.c:329
static void government_iter_next(struct iterator *iter)
Definition government.c:462
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:395
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:487
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:387
Government_type_id government_count(void)
Definition government.c:71
static struct ruler_title * ruler_title_new(const struct nation_type *pnation, const char *domain, const char *ruler_male_title, const char *ruler_female_title)
Definition government.c:218
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:478
struct government * government_by_number(const Government_type_id gov)
Definition government.c:103
struct government * government_of_player(const struct player *pplayer)
Definition government.c:114
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:154
static bool nation_hash_comp(const struct nation_type *pnation1, const struct nation_type *pnation2)
Definition government.c:209
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:378
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:235
static void government_free(struct government *pgovern)
Definition government.c:517
struct government * government_of_city(const struct city *pcity)
Definition government.c:123
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:201
void governments_alloc(int num)
Definition government.c:533
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:170
Government_type_id government_number(const struct government *pgovern)
Definition government.c:91
const struct ruler_title_hash * government_ruler_titles(const struct government *pgovern)
Definition government.c:318
Government_type_id government_index(const struct government *pgovern)
Definition government.c:82
size_t government_iter_sizeof(void)
Definition government.c:454
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:243
#define GOVERNMENT_ITER(p)
Definition government.c:449
const char * government_rule_name(const struct government *pgovern)
Definition government.c:133
static void * government_iter_get(const struct iterator *iter)
Definition government.c:470
struct government * government_by_translated_name(const char *name)
Definition government.c:40
struct government * government_by_rule_name(const char *name)
Definition government.c:55
#define governments_iterate(NAME_pgov)
Definition government.h:124
#define governments_iterate_end
Definition government.h:127
const char * name
Definition inputfile.c:127
#define ITERATOR(p)
Definition iterator.h:37
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_error(message,...)
Definition log.h:103
#define fc_malloc(sz)
Definition mem.h:34
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
static const char * untranslated_name(const struct name_translation *ptrans)
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
Nation_type_id nation_count(void)
Definition nation.c:507
Nation_type_id nation_number(const struct nation_type *pnation)
Definition nation.c:486
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
const char * player_name(const struct player *pplayer)
Definition player.c:895
bool are_reqs_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
bool formats_match(const char *format1, const char *format2)
Definition shared.c:2445
void strvec_destroy(struct strvec *psv)
Definition city.h:320
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
struct government * p
Definition government.c:447
struct iterator vtable
Definition government.c:446
struct government * end
Definition government.c:447
struct requirement_vector reqs
Definition government.h:62
struct ruler_title_hash * ruler_titles
Definition government.h:63
bool(* valid)(const struct iterator *it)
Definition iterator.h:34
void *(* get)(const struct iterator *it)
Definition iterator.h:33
void(* next)(struct iterator *it)
Definition iterator.h:32
char * translation_domain
Definition nation.h:100
enum revolen_type revolentype
bool is_male
Definition player.h:255
struct government * government
Definition player.h:256
bool is_alive
Definition player.h:266
struct name_translation male
Definition government.c:194
struct name_translation female
Definition government.c:195
const struct nation_type * pnation
Definition government.c:193
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47