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
35
37
38/**********************************************************************/
43{
45 if (0 == strcmp(government_name_translation(gov), name)) {
46 return gov;
47 }
49
50 return NULL;
51}
52
53/**********************************************************************/
58{
59 const char *qname = Qn_(name);
60
63 return gov;
64 }
66
67 return NULL;
68}
69
70/**********************************************************************/
77
78/**********************************************************************/
89
90/**********************************************************************/
94{
96 return pgovern->item_number;
97}
98
99/**********************************************************************/
106{
108 return NULL;
109 }
110 return &governments[gov];
111}
112
113/**********************************************************************/
116struct government *government_of_player(const struct player *pplayer)
117{
118 fc_assert_ret_val(NULL != pplayer, NULL);
119 return pplayer->government;
120}
121
122/**********************************************************************/
130
131/**********************************************************************/
135const char *government_rule_name(const struct government *pgovern)
136{
138 return rule_name_get(&pgovern->name);
139}
140
141/**********************************************************************/
146{
148
149 return name_translation_get(&pgovern->name);
150}
151
152/**********************************************************************/
156const char *government_name_for_player(const struct player *pplayer)
157{
159}
160
161/**********************************************************************/
168bool can_change_to_government(struct player *pplayer,
169 const struct government *gov)
170{
172
173 if (!pplayer) {
174 return FALSE;
175 }
176
177 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
178 /* Note, this may allow govs that are on someone else's "tech tree". */
179 return TRUE;
180 }
181
182 return are_reqs_active(&(const struct req_context) { .player = pplayer },
183 NULL, &gov->reqs, RPT_CERTAIN);
184}
185
186
187/**************************************************************************
188 Ruler titles.
189**************************************************************************/
195
196/**********************************************************************/
199static genhash_val_t nation_hash_val(const struct nation_type *pnation)
200{
201 return NULL != pnation ? nation_number(pnation) : nation_count();
202}
203
204/**********************************************************************/
207static bool nation_hash_comp(const struct nation_type *pnation1,
208 const struct nation_type *pnation2)
209{
210 return pnation1 == pnation2;
211}
212
213/**********************************************************************/
216static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
217 const char *domain,
218 const char *ruler_male_title,
219 const char *ruler_female_title)
220{
222
223 pruler_title->pnation = pnation;
226
227 return pruler_title;
228}
229
230/**********************************************************************/
234{
236}
237
238/**********************************************************************/
242{
243 bool ret = TRUE;
244
245 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
246 if (NULL != pruler_title->pnation) {
247 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
248 "is not a format. It should match \"%%s\"",
251 nation_number(pruler_title->pnation));
252 } else {
253 log_error("\"%s\" male ruler title is not a format. "
254 "It should match \"%%s\"",
256 }
257 ret = FALSE;
258 }
259
260 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
261 if (NULL != pruler_title->pnation) {
262 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
263 "is not a format. It should match \"%%s\"",
264 rule_name_get(&pruler_title->female),
266 nation_number(pruler_title->pnation));
267 } else {
268 log_error("\"%s\" female ruler title is not a format. "
269 "It should match \"%%s\"",
270 rule_name_get(&pruler_title->female));
271 }
272 ret = FALSE;
273 }
274
275 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
276 if (NULL != pruler_title->pnation) {
277 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
278 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
281 nation_number(pruler_title->pnation),
283 } else {
284 log_error("Translation of \"%s\" male ruler title is not a format "
285 "(\"%s\"). It should match \"%%s\"",
288 }
289 ret = FALSE;
290 }
291
292 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
293 if (NULL != pruler_title->pnation) {
294 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
295 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
296 rule_name_get(&pruler_title->female),
298 nation_number(pruler_title->pnation),
300 } else {
301 log_error("Translation of \"%s\" female ruler title is not a format "
302 "(\"%s\"). It should match \"%%s\"",
303 rule_name_get(&pruler_title->female),
305 }
306 ret = FALSE;
307 }
308
309 return ret;
310}
311
312/**********************************************************************/
315const struct ruler_title_hash *
317{
319 return pgovern->ruler_titles;
320}
321
322/**********************************************************************/
326struct ruler_title *
328 const struct nation_type *pnation,
329 const char *ruler_male_title,
330 const char *ruler_female_title)
331{
332 const char *domain = NULL;
334
335 if (pnation != NULL) {
337 }
340
343 return NULL;
344 }
345
346 if (ruler_title_hash_replace(pgovern->ruler_titles,
348 if (NULL != pnation) {
349 log_error("Ruler title for government \"%s\" (nb %d) and "
350 "nation \"%s\" (nb %d) was set twice.",
353 } else {
354 log_error("Default ruler title for government \"%s\" (nb %d) "
355 "was set twice.", government_rule_name(pgovern),
357 }
358 }
359
360 return pruler_title;
361}
362
363/**********************************************************************/
366const struct nation_type *
368{
369 return pruler_title->pnation;
370}
371
372/**********************************************************************/
375const char *
380
381/**********************************************************************/
384const char *
389
390/**********************************************************************/
393const char *ruler_title_for_player(const struct player *pplayer,
394 char *buf, size_t buf_len)
395{
396 const struct government *pgovern = government_of_player(pplayer);
397 const struct nation_type *pnation = nation_of_player(pplayer);
399
402
403 /* Try specific nation ruler title. */
404 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
406 /* Try default ruler title. */
407 && !ruler_title_hash_lookup(pgovern->ruler_titles,
408 NULL, &pruler_title)) {
409 log_error("Missing title for government \"%s\" (nb %d) "
410 "nation \"%s\" (nb %d).",
414 } else {
417 ? &pruler_title->male
418 : &pruler_title->female),
419 player_name(pplayer));
420 }
421
422 return buf;
423}
424
425/**********************************************************************/
428const char *default_title_for_player(const struct player *pplayer,
429 char *buf, size_t buf_len)
430{
431 if (pplayer->is_male) {
432 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
433 } else {
434 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
435 }
436
437 return buf;
438}
439
440/**************************************************************************
441 Government iterator.
442**************************************************************************/
445 struct government *p, *end;
446};
447#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
448
449/**********************************************************************/
453{
454 return sizeof(struct government_iter);
455}
456
457/**********************************************************************/
461{
462 GOVERNMENT_ITER(iter)->p++;
463}
464
465/**********************************************************************/
468static void *government_iter_get(const struct iterator *iter)
469{
470 return GOVERNMENT_ITER(iter)->p;
471}
472
473/**********************************************************************/
476static bool government_iter_valid(const struct iterator *iter)
477{
479 return it->p < it->end;
480}
481
482/**********************************************************************/
494
495/**********************************************************************/
498static inline void government_init(struct government *pgovern)
499{
500 memset(pgovern, 0, sizeof(*pgovern));
501
502 pgovern->item_number = pgovern - governments;
507 pgovern->changed_to_times = 0;
508 pgovern->ruledit_disabled = FALSE;
509 pgovern->ruledit_dlg = NULL;
510}
511
512/**********************************************************************/
515static inline void government_free(struct government *pgovern)
516{
517 ruler_title_hash_destroy(pgovern->ruler_titles);
518 pgovern->ruler_titles = NULL;
519
520 if (NULL != pgovern->helptext) {
521 strvec_destroy(pgovern->helptext);
522 pgovern->helptext = NULL;
523 }
524
526}
527
528/**********************************************************************/
531void governments_alloc(int num)
532{
533 int i;
534
536 governments = fc_malloc(sizeof(*governments) * num);
538
539 for (i = 0; i < game.control.government_count; i++) {
541 }
542}
543
544/**********************************************************************/
548{
549 int i;
550
551 if (NULL == governments) {
552 return;
553 }
554
555 for (i = 0; i < game.control.government_count; i++) {
557 }
558
562}
563
564/**********************************************************************/
569{
572 /* We need to know the target government at the onset of the revolution
573 * in order to know how long anarchy will last. */
574 return FALSE;
575 }
576
577 return TRUE;
578}
579
580/**********************************************************************/
584 enum gov_flag_id flag)
585{
587
588 return BV_ISSET(pgov->flags, flag);
589}
590
591
592/************************************************************************/
596{
597 int i;
598
599 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
601 }
602}
603
604/************************************************************************/
608{
609 int i;
610
611 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
613 }
614}
615
616/************************************************************************/
619void set_user_gov_flag_name(enum gov_flag_id id, const char *name,
620 const char *helptxt)
621{
622 int gfid = id - GOVF_USER_FLAG_1;
623
625
627 user_gov_flags[gfid].name = nullptr;
628
629 if (name && name[0] != '\0') {
631 }
632
633 free(user_gov_flags[gfid].helptxt);
634 user_gov_flags[gfid].helptxt = nullptr;
635
636 if (helptxt && helptxt[0] != '\0') {
638 }
639}
640
641/************************************************************************/
644const char *gov_flag_id_name_cb(enum gov_flag_id flag)
645{
647 return nullptr;
648 }
649
650 return user_gov_flags[flag - GOVF_USER_FLAG_1].name;
651}
652
653/************************************************************************/
656const char *gov_flag_helptxt(enum gov_flag_id id)
657{
659
661}
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
#define city_owner(_pcity_)
Definition city.h:563
char * incite_cost
Definition comments.c:76
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:515
@ REVOLEN_RANDQUICK
Definition fc_types.h:980
@ REVOLEN_QUICKENING
Definition fc_types.h:979
int Government_type_id
Definition fc_types.h:241
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
void user_flag_init(struct user_flag *flag)
Definition game.c:832
void user_flag_free(struct user_flag *flag)
Definition game.c:841
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:145
const char * gov_flag_id_name_cb(enum gov_flag_id flag)
Definition government.c:644
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:428
bool government_has_flag(const struct government *pgov, enum gov_flag_id flag)
Definition government.c:583
void governments_free(void)
Definition government.c:547
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:367
bool untargeted_revolution_allowed(void)
Definition government.c:568
static void government_init(struct government *pgovern)
Definition government.c:498
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:327
static void government_iter_next(struct iterator *iter)
Definition government.c:460
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:393
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:485
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:385
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:216
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:476
struct government * government_by_number(const Government_type_id gov)
Definition government.c:105
struct government * government_of_player(const struct player *pplayer)
Definition government.c:116
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:156
static bool nation_hash_comp(const struct nation_type *pnation1, const struct nation_type *pnation2)
Definition government.c:207
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:376
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:233
static void government_free(struct government *pgovern)
Definition government.c:515
struct government * government_of_city(const struct city *pcity)
Definition government.c:125
const char * gov_flag_helptxt(enum gov_flag_id id)
Definition government.c:656
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:199
void governments_alloc(int num)
Definition government.c:531
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:168
void user_gov_flags_init(void)
Definition government.c:595
void gov_flags_free(void)
Definition government.c:607
Government_type_id government_number(const struct government *pgovern)
Definition government.c:93
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:316
Government_type_id government_index(const struct government *pgovern)
Definition government.c:84
size_t government_iter_sizeof(void)
Definition government.c:452
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:241
#define GOVERNMENT_ITER(p)
Definition government.c:447
const char * government_rule_name(const struct government *pgovern)
Definition government.c:135
static void * government_iter_get(const struct iterator *iter)
Definition government.c:468
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:619
#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:528
Nation_type_id nation_number(const struct nation_type *pnation)
Definition nation.c:505
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:458
const char * player_name(const struct player *pplayer)
Definition player.c:900
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:2446
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:445
struct iterator vtable
Definition government.c:444
struct government * end
Definition government.c:445
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
struct name_translation male
Definition government.c:192
struct name_translation female
Definition government.c:193
const struct nation_type * pnation
Definition government.c:191
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