Freeciv-3.4
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
34struct government *governments = nullptr;
35
37
38/**********************************************************************/
43{
45 if (0 == strcmp(government_name_translation(gov), name)) {
46 return gov;
47 }
49
50 return nullptr;
51}
52
53/**********************************************************************/
58{
59 const char *qname = Qn_(name);
60
63 return gov;
64 }
66
67 return nullptr;
68}
69
70/**********************************************************************/
77
78/**********************************************************************/
88
89/**********************************************************************/
96
97/**********************************************************************/
104{
106 return nullptr;
107 }
108
109 return &governments[gov];
110}
111
112/**********************************************************************/
115struct government *government_of_player(const struct player *pplayer)
116{
117 return pplayer->government;
118}
119
120/**********************************************************************/
124{
126}
127
128/**********************************************************************/
132const char *government_rule_name(const struct government *pgovern)
133{
134 return rule_name_get(&pgovern->name);
135}
136
137/**********************************************************************/
142{
143 return name_translation_get(&pgovern->name);
144}
145
146/**********************************************************************/
150const char *government_name_for_player(const struct player *pplayer)
151{
152 if (!pplayer->is_alive) {
153 return "-";
154 }
155
157}
158
159/**********************************************************************/
166bool can_change_to_government(struct player *pplayer,
167 const struct government *gov)
168{
169 if (pplayer == nullptr) {
170 return FALSE;
171 }
172
173 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
174 /* Note, this may allow govs that are on someone else's "tech tree". */
175 return TRUE;
176 }
177
178 return are_reqs_active(&(const struct req_context) { .player = pplayer },
179 nullptr, &gov->reqs, RPT_CERTAIN);
180}
181
182
183/**************************************************************************
184 Ruler titles.
185**************************************************************************/
191
192/**********************************************************************/
195static genhash_val_t nation_hash_val(const struct nation_type *pnation)
196{
197 return pnation != nullptr ? nation_number(pnation) : nation_count();
198}
199
200/**********************************************************************/
203static bool nation_hash_comp(const struct nation_type *pnation1,
204 const struct nation_type *pnation2)
205{
206 return pnation1 == pnation2;
207}
208
209/**********************************************************************/
212static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
213 const char *domain,
214 const char *ruler_male_title,
215 const char *ruler_female_title)
216{
218
219 pruler_title->pnation = pnation;
222
223 return pruler_title;
224}
225
226/**********************************************************************/
230{
232}
233
234/**********************************************************************/
238{
239 bool ret = TRUE;
240
241 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
242 if (pruler_title->pnation != nullptr) {
243 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
244 "is not a format. It should match \"%%s\"",
247 nation_number(pruler_title->pnation));
248 } else {
249 log_error("\"%s\" male ruler title is not a format. "
250 "It should match \"%%s\"",
252 }
253 ret = FALSE;
254 }
255
256 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
257 if (pruler_title->pnation != nullptr) {
258 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
259 "is not a format. It should match \"%%s\"",
260 rule_name_get(&pruler_title->female),
262 nation_number(pruler_title->pnation));
263 } else {
264 log_error("\"%s\" female ruler title is not a format. "
265 "It should match \"%%s\"",
266 rule_name_get(&pruler_title->female));
267 }
268 ret = FALSE;
269 }
270
271 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
272 if (pruler_title->pnation != nullptr) {
273 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
274 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
277 nation_number(pruler_title->pnation),
279 } else {
280 log_error("Translation of \"%s\" male ruler title is not a format "
281 "(\"%s\"). It should match \"%%s\"",
284 }
285 ret = FALSE;
286 }
287
288 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
289 if (pruler_title->pnation != nullptr) {
290 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
291 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
292 rule_name_get(&pruler_title->female),
294 nation_number(pruler_title->pnation),
296 } else {
297 log_error("Translation of \"%s\" female ruler title is not a format "
298 "(\"%s\"). It should match \"%%s\"",
299 rule_name_get(&pruler_title->female),
301 }
302 ret = FALSE;
303 }
304
305 return ret;
306}
307
308/**********************************************************************/
311const struct ruler_title_hash *
313{
314 return pgovern->ruler_titles;
315}
316
317/**********************************************************************/
321struct ruler_title *
323 const struct nation_type *pnation,
324 const char *ruler_male_title,
325 const char *ruler_female_title)
326{
327 const char *domain = nullptr;
329
330 if (pnation != nullptr) {
332 }
335
338 return nullptr;
339 }
340
341 if (ruler_title_hash_replace(pgovern->ruler_titles,
343 if (pnation != nullptr) {
344 log_error("Ruler title for government \"%s\" (nb %d) and "
345 "nation \"%s\" (nb %d) was set twice.",
348 } else {
349 log_error("Default ruler title for government \"%s\" (nb %d) "
350 "was set twice.", government_rule_name(pgovern),
352 }
353 }
354
355 return pruler_title;
356}
357
358/**********************************************************************/
361const struct nation_type *
363{
364 return pruler_title->pnation;
365}
366
367/**********************************************************************/
370const char *
375
376/**********************************************************************/
379const char *
384
385/**********************************************************************/
388const char *ruler_title_for_player(const struct player *pplayer,
389 char *buf, size_t buf_len)
390{
391 const struct government *pgovern = government_of_player(pplayer);
392 const struct nation_type *pnation = nation_of_player(pplayer);
394
395 fc_assert_ret_val(0 < buf_len, nullptr);
396
397 /* Try specific nation ruler title. */
398 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
400 /* Try default ruler title. */
401 && !ruler_title_hash_lookup(pgovern->ruler_titles,
402 nullptr, &pruler_title)) {
403 log_error("Missing title for government \"%s\" (nb %d) "
404 "nation \"%s\" (nb %d).",
408 } else {
411 ? &pruler_title->male
412 : &pruler_title->female),
413 player_name(pplayer));
414 }
415
416 return buf;
417}
418
419/**********************************************************************/
422const char *default_title_for_player(const struct player *pplayer,
423 char *buf, size_t buf_len)
424{
425 if (pplayer->is_male) {
426 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
427 } else {
428 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
429 }
430
431 return buf;
432}
433
434/**************************************************************************
435 Government iterator.
436**************************************************************************/
439 struct government *p, *end;
440};
441#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
442
443/**********************************************************************/
447{
448 return sizeof(struct government_iter);
449}
450
451/**********************************************************************/
455{
456 GOVERNMENT_ITER(iter)->p++;
457}
458
459/**********************************************************************/
462static void *government_iter_get(const struct iterator *iter)
463{
464 return GOVERNMENT_ITER(iter)->p;
465}
466
467/**********************************************************************/
470static bool government_iter_valid(const struct iterator *iter)
471{
473 return it->p < it->end;
474}
475
476/**********************************************************************/
488
489/**********************************************************************/
492static inline void government_init(struct government *pgovern)
493{
494 memset(pgovern, 0, sizeof(*pgovern));
495
496 pgovern->item_number = pgovern - governments;
499 nullptr, nullptr, nullptr, ruler_title_destroy);
501 pgovern->changed_to_times = 0;
502 pgovern->ruledit_disabled = FALSE;
503 pgovern->ruledit_dlg = nullptr;
504}
505
506/**********************************************************************/
509static inline void government_free(struct government *pgovern)
510{
511 ruler_title_hash_destroy(pgovern->ruler_titles);
512 pgovern->ruler_titles = nullptr;
513
514 if (pgovern->helptext != nullptr) {
515 strvec_destroy(pgovern->helptext);
516 pgovern->helptext = nullptr;
517 }
518
520}
521
522/**********************************************************************/
525void governments_alloc(int num)
526{
527 int i;
528
529 fc_assert(governments == nullptr);
530
531 governments = fc_malloc(sizeof(*governments) * num);
533
534 for (i = 0; i < game.control.government_count; i++) {
536 }
537}
538
539/**********************************************************************/
543{
544 int i;
545
546 if (governments == nullptr) {
547 return;
548 }
549
550 for (i = 0; i < game.control.government_count; i++) {
552 }
553
555 governments = nullptr;
557}
558
559/**********************************************************************/
564{
567 /* We need to know the target government at the onset of the revolution
568 * in order to know how long anarchy will last. */
569 return FALSE;
570 }
571
572 return TRUE;
573}
574
575/**********************************************************************/
579 enum gov_flag_id flag)
580{
582
583 return BV_ISSET(pgov->flags, flag);
584}
585
586
587/************************************************************************/
591{
592 int i;
593
594 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
596 }
597}
598
599/************************************************************************/
603{
604 int i;
605
606 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
608 }
609}
610
611/************************************************************************/
614void set_user_gov_flag_name(enum gov_flag_id id, const char *name,
615 const char *helptxt)
616{
617 int gfid = id - GOVF_USER_FLAG_1;
618
620
622 user_gov_flags[gfid].name = nullptr;
623
624 if (name && name[0] != '\0') {
626 }
627
628 free(user_gov_flags[gfid].helptxt);
629 user_gov_flags[gfid].helptxt = nullptr;
630
631 if (helptxt && helptxt[0] != '\0') {
633 }
634}
635
636/************************************************************************/
639const char *gov_flag_id_name_cb(enum gov_flag_id flag)
640{
642 return nullptr;
643 }
644
645 return user_gov_flags[flag - GOVF_USER_FLAG_1].name;
646}
647
648/************************************************************************/
651const char *gov_flag_helptxt(enum gov_flag_id id)
652{
654
656}
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
#define city_owner(_pcity_)
Definition city.h:564
char * incite_cost
Definition comments.c:77
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:824
@ RPT_CERTAIN
Definition fc_types.h:516
@ REVOLEN_RANDQUICK
Definition fc_types.h:982
@ REVOLEN_QUICKENING
Definition fc_types.h:981
int Government_type_id
Definition fc_types.h:242
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
void user_flag_init(struct user_flag *flag)
Definition game.c:834
void user_flag_free(struct user_flag *flag)
Definition game.c:843
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:141
const char * gov_flag_id_name_cb(enum gov_flag_id flag)
Definition government.c:639
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:422
bool government_has_flag(const struct government *pgov, enum gov_flag_id flag)
Definition government.c:578
void governments_free(void)
Definition government.c:542
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:362
bool untargeted_revolution_allowed(void)
Definition government.c:563
static void government_init(struct government *pgovern)
Definition government.c:492
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:322
static void government_iter_next(struct iterator *iter)
Definition government.c:454
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:388
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:479
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:380
Government_type_id government_count(void)
Definition government.c:73
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:212
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:470
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:115
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:150
static bool nation_hash_comp(const struct nation_type *pnation1, const struct nation_type *pnation2)
Definition government.c:203
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:371
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:229
static void government_free(struct government *pgovern)
Definition government.c:509
struct government * government_of_city(const struct city *pcity)
Definition government.c:123
const char * gov_flag_helptxt(enum gov_flag_id id)
Definition government.c:651
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:195
void governments_alloc(int num)
Definition government.c:525
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:166
void user_gov_flags_init(void)
Definition government.c:590
void gov_flags_free(void)
Definition government.c:602
Government_type_id government_number(const struct government *pgovern)
Definition government.c:92
static struct user_flag user_gov_flags[MAX_NUM_USER_GOVERNMENT_FLAGS]
Definition government.c:36
const struct ruler_title_hash * government_ruler_titles(const struct government *pgovern)
Definition government.c:312
Government_type_id government_index(const struct government *pgovern)
Definition government.c:84
size_t government_iter_sizeof(void)
Definition government.c:446
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:237
#define GOVERNMENT_ITER(p)
Definition government.c:441
const char * government_rule_name(const struct government *pgovern)
Definition government.c:132
static void * government_iter_get(const struct iterator *iter)
Definition government.c:462
struct government * government_by_translated_name(const char *name)
Definition government.c:42
struct government * government_by_rule_name(const char *name)
Definition government.c:57
void set_user_gov_flag_name(enum gov_flag_id id, const char *name, const char *helptxt)
Definition government.c:614
#define governments_iterate(NAME_pgov)
Definition government.h:136
#define GOVF_LAST_USER_FLAG
Definition government.h:28
#define governments_iterate_end
Definition government.h:139
#define MAX_NUM_USER_GOVERNMENT_FLAGS
Definition government.h:29
const char * name
Definition inputfile.c:127
#define ITERATOR(p)
Definition iterator.h:37
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_error(message,...)
Definition log.h:104
#define fc_strdup(str)
Definition mem.h:43
#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:503
Nation_type_id nation_number(const struct nation_type *pnation)
Definition nation.c:484
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
const char * player_name(const struct player *pplayer)
Definition player.c:885
bool are_reqs_active(const struct req_context *context, const struct req_context *other_context, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
bool formats_match(const char *format1, const char *format2)
Definition shared.c:2455
void strvec_destroy(struct strvec *psv)
Definition city.h:318
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
struct government * p
Definition government.c:439
struct iterator vtable
Definition government.c:438
struct government * end
Definition government.c:439
struct requirement_vector reqs
Definition government.h:64
Government_type_id item_number
Definition government.h:55
struct ruler_title_hash * ruler_titles
Definition government.h:65
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:257
struct government * government
Definition player.h:258
bool is_alive
Definition player.h:268
struct name_translation male
Definition government.c:188
struct name_translation female
Definition government.c:189
const struct nation_type * pnation
Definition government.c:187
char * name
Definition game.h:74
char * helptxt
Definition game.h:75
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47