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/**********************************************************************/
85{
86 fc_assert_ret_val(pgovern != nullptr, -1);
87
88 return pgovern - governments;
89}
90
91/**********************************************************************/
95{
96 fc_assert_ret_val(pgovern != nullptr, -1);
97
98 return pgovern->item_number;
99}
100
101/**********************************************************************/
108{
110 return nullptr;
111 }
112
113 return &governments[gov];
114}
115
116/**********************************************************************/
119struct government *government_of_player(const struct player *pplayer)
120{
121 fc_assert_ret_val(pplayer != nullptr, nullptr);
122
123 return pplayer->government;
124}
125
126/**********************************************************************/
130{
131 fc_assert_ret_val(pcity != nullptr, nullptr);
132
134}
135
136/**********************************************************************/
140const char *government_rule_name(const struct government *pgovern)
141{
142 fc_assert_ret_val(pgovern != nullptr, nullptr);
143
144 return rule_name_get(&pgovern->name);
145}
146
147/**********************************************************************/
152{
153 fc_assert_ret_val(pgovern != nullptr, nullptr);
154
155 return name_translation_get(&pgovern->name);
156}
157
158/**********************************************************************/
162const char *government_name_for_player(const struct player *pplayer)
163{
164 if (!pplayer->is_alive) {
165 return "-";
166 }
167
169}
170
171/**********************************************************************/
178bool can_change_to_government(struct player *pplayer,
179 const struct government *gov)
180{
181 fc_assert_ret_val(gov != nullptr, FALSE);
182
183 if (!pplayer) {
184 return FALSE;
185 }
186
187 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
188 /* Note, this may allow govs that are on someone else's "tech tree". */
189 return TRUE;
190 }
191
192 return are_reqs_active(&(const struct req_context) { .player = pplayer },
193 nullptr, &gov->reqs, RPT_CERTAIN);
194}
195
196
197/**************************************************************************
198 Ruler titles.
199**************************************************************************/
205
206/**********************************************************************/
209static genhash_val_t nation_hash_val(const struct nation_type *pnation)
210{
211 return pnation != nullptr ? nation_number(pnation) : nation_count();
212}
213
214/**********************************************************************/
217static bool nation_hash_comp(const struct nation_type *pnation1,
218 const struct nation_type *pnation2)
219{
220 return pnation1 == pnation2;
221}
222
223/**********************************************************************/
226static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
227 const char *domain,
228 const char *ruler_male_title,
229 const char *ruler_female_title)
230{
232
233 pruler_title->pnation = pnation;
236
237 return pruler_title;
238}
239
240/**********************************************************************/
244{
246}
247
248/**********************************************************************/
252{
253 bool ret = TRUE;
254
255 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
256 if (pruler_title->pnation != nullptr) {
257 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
258 "is not a format. It should match \"%%s\"",
261 nation_number(pruler_title->pnation));
262 } else {
263 log_error("\"%s\" male ruler title is not a format. "
264 "It should match \"%%s\"",
266 }
267 ret = FALSE;
268 }
269
270 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
271 if (pruler_title->pnation != nullptr) {
272 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
273 "is not a format. It should match \"%%s\"",
274 rule_name_get(&pruler_title->female),
276 nation_number(pruler_title->pnation));
277 } else {
278 log_error("\"%s\" female ruler title is not a format. "
279 "It should match \"%%s\"",
280 rule_name_get(&pruler_title->female));
281 }
282 ret = FALSE;
283 }
284
285 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
286 if (pruler_title->pnation != nullptr) {
287 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
288 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
291 nation_number(pruler_title->pnation),
293 } else {
294 log_error("Translation of \"%s\" male ruler title is not a format "
295 "(\"%s\"). It should match \"%%s\"",
298 }
299 ret = FALSE;
300 }
301
302 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
303 if (pruler_title->pnation != nullptr) {
304 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
305 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
306 rule_name_get(&pruler_title->female),
308 nation_number(pruler_title->pnation),
310 } else {
311 log_error("Translation of \"%s\" female ruler title is not a format "
312 "(\"%s\"). It should match \"%%s\"",
313 rule_name_get(&pruler_title->female),
315 }
316 ret = FALSE;
317 }
318
319 return ret;
320}
321
322/**********************************************************************/
325const struct ruler_title_hash *
327{
328 fc_assert_ret_val(pgovern != nullptr, nullptr);
329
330 return pgovern->ruler_titles;
331}
332
333/**********************************************************************/
337struct ruler_title *
339 const struct nation_type *pnation,
340 const char *ruler_male_title,
341 const char *ruler_female_title)
342{
343 const char *domain = nullptr;
345
346 if (pnation != nullptr) {
348 }
351
354 return nullptr;
355 }
356
357 if (ruler_title_hash_replace(pgovern->ruler_titles,
359 if (pnation != nullptr) {
360 log_error("Ruler title for government \"%s\" (nb %d) and "
361 "nation \"%s\" (nb %d) was set twice.",
364 } else {
365 log_error("Default ruler title for government \"%s\" (nb %d) "
366 "was set twice.", government_rule_name(pgovern),
368 }
369 }
370
371 return pruler_title;
372}
373
374/**********************************************************************/
377const struct nation_type *
379{
380 return pruler_title->pnation;
381}
382
383/**********************************************************************/
386const char *
391
392/**********************************************************************/
395const char *
400
401/**********************************************************************/
404const char *ruler_title_for_player(const struct player *pplayer,
405 char *buf, size_t buf_len)
406{
407 const struct government *pgovern = government_of_player(pplayer);
408 const struct nation_type *pnation = nation_of_player(pplayer);
410
411 fc_assert_ret_val(buf != nullptr, nullptr);
412 fc_assert_ret_val(0 < buf_len, nullptr);
413
414 /* Try specific nation ruler title. */
415 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
417 /* Try default ruler title. */
418 && !ruler_title_hash_lookup(pgovern->ruler_titles,
419 nullptr, &pruler_title)) {
420 log_error("Missing title for government \"%s\" (nb %d) "
421 "nation \"%s\" (nb %d).",
425 } else {
428 ? &pruler_title->male
429 : &pruler_title->female),
430 player_name(pplayer));
431 }
432
433 return buf;
434}
435
436/**********************************************************************/
439const char *default_title_for_player(const struct player *pplayer,
440 char *buf, size_t buf_len)
441{
442 if (pplayer->is_male) {
443 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
444 } else {
445 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
446 }
447
448 return buf;
449}
450
451/**************************************************************************
452 Government iterator.
453**************************************************************************/
456 struct government *p, *end;
457};
458#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
459
460/**********************************************************************/
464{
465 return sizeof(struct government_iter);
466}
467
468/**********************************************************************/
472{
473 GOVERNMENT_ITER(iter)->p++;
474}
475
476/**********************************************************************/
479static void *government_iter_get(const struct iterator *iter)
480{
481 return GOVERNMENT_ITER(iter)->p;
482}
483
484/**********************************************************************/
487static bool government_iter_valid(const struct iterator *iter)
488{
490 return it->p < it->end;
491}
492
493/**********************************************************************/
505
506/**********************************************************************/
509static inline void government_init(struct government *pgovern)
510{
511 memset(pgovern, 0, sizeof(*pgovern));
512
513 pgovern->item_number = pgovern - governments;
516 nullptr, nullptr, nullptr, ruler_title_destroy);
518 pgovern->changed_to_times = 0;
519 pgovern->ruledit_disabled = FALSE;
520 pgovern->ruledit_dlg = nullptr;
521}
522
523/**********************************************************************/
526static inline void government_free(struct government *pgovern)
527{
528 ruler_title_hash_destroy(pgovern->ruler_titles);
529 pgovern->ruler_titles = nullptr;
530
531 if (pgovern->helptext != nullptr) {
532 strvec_destroy(pgovern->helptext);
533 pgovern->helptext = nullptr;
534 }
535
537}
538
539/**********************************************************************/
542void governments_alloc(int num)
543{
544 int i;
545
546 fc_assert(governments == nullptr);
547
548 governments = fc_malloc(sizeof(*governments) * num);
550
551 for (i = 0; i < game.control.government_count; i++) {
553 }
554}
555
556/**********************************************************************/
560{
561 int i;
562
563 if (governments == nullptr) {
564 return;
565 }
566
567 for (i = 0; i < game.control.government_count; i++) {
569 }
570
572 governments = nullptr;
574}
575
576/**********************************************************************/
581{
584 /* We need to know the target government at the onset of the revolution
585 * in order to know how long anarchy will last. */
586 return FALSE;
587 }
588
589 return TRUE;
590}
591
592/**********************************************************************/
596 enum gov_flag_id flag)
597{
599
600 return BV_ISSET(pgov->flags, flag);
601}
602
603
604/************************************************************************/
608{
609 int i;
610
611 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
613 }
614}
615
616/************************************************************************/
620{
621 int i;
622
623 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
625 }
626}
627
628/************************************************************************/
631void set_user_gov_flag_name(enum gov_flag_id id, const char *name,
632 const char *helptxt)
633{
634 int gfid = id - GOVF_USER_FLAG_1;
635
637
639 user_gov_flags[gfid].name = nullptr;
640
641 if (name && name[0] != '\0') {
643 }
644
645 free(user_gov_flags[gfid].helptxt);
646 user_gov_flags[gfid].helptxt = nullptr;
647
648 if (helptxt && helptxt[0] != '\0') {
650 }
651}
652
653/************************************************************************/
656const char *gov_flag_id_name_cb(enum gov_flag_id flag)
657{
659 return nullptr;
660 }
661
662 return user_gov_flags[flag - GOVF_USER_FLAG_1].name;
663}
664
665/************************************************************************/
668const char *gov_flag_helptxt(enum gov_flag_id id)
669{
671
673}
#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:151
const char * gov_flag_id_name_cb(enum gov_flag_id flag)
Definition government.c:656
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:439
bool government_has_flag(const struct government *pgov, enum gov_flag_id flag)
Definition government.c:595
void governments_free(void)
Definition government.c:559
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:378
bool untargeted_revolution_allowed(void)
Definition government.c:580
static void government_init(struct government *pgovern)
Definition government.c:509
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:338
static void government_iter_next(struct iterator *iter)
Definition government.c:471
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:404
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:496
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:396
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:226
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:487
struct government * government_by_number(const Government_type_id gov)
Definition government.c:107
struct government * government_of_player(const struct player *pplayer)
Definition government.c:119
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:162
static bool nation_hash_comp(const struct nation_type *pnation1, const struct nation_type *pnation2)
Definition government.c:217
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:387
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:243
static void government_free(struct government *pgovern)
Definition government.c:526
struct government * government_of_city(const struct city *pcity)
Definition government.c:129
const char * gov_flag_helptxt(enum gov_flag_id id)
Definition government.c:668
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:209
void governments_alloc(int num)
Definition government.c:542
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:178
void user_gov_flags_init(void)
Definition government.c:607
void gov_flags_free(void)
Definition government.c:619
Government_type_id government_number(const struct government *pgovern)
Definition government.c:94
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:326
Government_type_id government_index(const struct government *pgovern)
Definition government.c:84
size_t government_iter_sizeof(void)
Definition government.c:463
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:251
#define GOVERNMENT_ITER(p)
Definition government.c:458
const char * government_rule_name(const struct government *pgovern)
Definition government.c:140
static void * government_iter_get(const struct iterator *iter)
Definition government.c:479
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:631
#define governments_iterate(NAME_pgov)
Definition government.h:127
#define GOVF_LAST_USER_FLAG
Definition government.h:28
#define governments_iterate_end
Definition government.h:130
#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:456
struct iterator vtable
Definition government.c:455
struct government * end
Definition government.c:456
struct requirement_vector reqs
Definition government.h:64
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:202
struct name_translation female
Definition government.c:203
const struct nation_type * pnation
Definition government.c:201
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