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{
158 if (!pplayer->is_alive) {
159 return "-";
160 }
161
163}
164
165/**********************************************************************/
172bool can_change_to_government(struct player *pplayer,
173 const struct government *gov)
174{
176
177 if (!pplayer) {
178 return FALSE;
179 }
180
181 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
182 /* Note, this may allow govs that are on someone else's "tech tree". */
183 return TRUE;
184 }
185
186 return are_reqs_active(&(const struct req_context) { .player = pplayer },
187 NULL, &gov->reqs, RPT_CERTAIN);
188}
189
190
191/**************************************************************************
192 Ruler titles.
193**************************************************************************/
199
200/**********************************************************************/
203static genhash_val_t nation_hash_val(const struct nation_type *pnation)
204{
205 return NULL != pnation ? nation_number(pnation) : nation_count();
206}
207
208/**********************************************************************/
211static bool nation_hash_comp(const struct nation_type *pnation1,
212 const struct nation_type *pnation2)
213{
214 return pnation1 == pnation2;
215}
216
217/**********************************************************************/
220static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
221 const char *domain,
222 const char *ruler_male_title,
223 const char *ruler_female_title)
224{
226
227 pruler_title->pnation = pnation;
230
231 return pruler_title;
232}
233
234/**********************************************************************/
238{
240}
241
242/**********************************************************************/
246{
247 bool ret = TRUE;
248
249 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
250 if (NULL != pruler_title->pnation) {
251 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
252 "is not a format. It should match \"%%s\"",
255 nation_number(pruler_title->pnation));
256 } else {
257 log_error("\"%s\" male ruler title is not a format. "
258 "It should match \"%%s\"",
260 }
261 ret = FALSE;
262 }
263
264 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
265 if (NULL != pruler_title->pnation) {
266 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
267 "is not a format. It should match \"%%s\"",
268 rule_name_get(&pruler_title->female),
270 nation_number(pruler_title->pnation));
271 } else {
272 log_error("\"%s\" female ruler title is not a format. "
273 "It should match \"%%s\"",
274 rule_name_get(&pruler_title->female));
275 }
276 ret = FALSE;
277 }
278
279 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
280 if (NULL != pruler_title->pnation) {
281 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
282 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
285 nation_number(pruler_title->pnation),
287 } else {
288 log_error("Translation of \"%s\" male ruler title is not a format "
289 "(\"%s\"). It should match \"%%s\"",
292 }
293 ret = FALSE;
294 }
295
296 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
297 if (NULL != pruler_title->pnation) {
298 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
299 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
300 rule_name_get(&pruler_title->female),
302 nation_number(pruler_title->pnation),
304 } else {
305 log_error("Translation of \"%s\" female ruler title is not a format "
306 "(\"%s\"). It should match \"%%s\"",
307 rule_name_get(&pruler_title->female),
309 }
310 ret = FALSE;
311 }
312
313 return ret;
314}
315
316/**********************************************************************/
319const struct ruler_title_hash *
321{
323 return pgovern->ruler_titles;
324}
325
326/**********************************************************************/
330struct ruler_title *
332 const struct nation_type *pnation,
333 const char *ruler_male_title,
334 const char *ruler_female_title)
335{
336 const char *domain = NULL;
338
339 if (pnation != NULL) {
341 }
344
347 return NULL;
348 }
349
350 if (ruler_title_hash_replace(pgovern->ruler_titles,
352 if (NULL != pnation) {
353 log_error("Ruler title for government \"%s\" (nb %d) and "
354 "nation \"%s\" (nb %d) was set twice.",
357 } else {
358 log_error("Default ruler title for government \"%s\" (nb %d) "
359 "was set twice.", government_rule_name(pgovern),
361 }
362 }
363
364 return pruler_title;
365}
366
367/**********************************************************************/
370const struct nation_type *
372{
373 return pruler_title->pnation;
374}
375
376/**********************************************************************/
379const char *
384
385/**********************************************************************/
388const char *
393
394/**********************************************************************/
397const char *ruler_title_for_player(const struct player *pplayer,
398 char *buf, size_t buf_len)
399{
400 const struct government *pgovern = government_of_player(pplayer);
401 const struct nation_type *pnation = nation_of_player(pplayer);
403
406
407 /* Try specific nation ruler title. */
408 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
410 /* Try default ruler title. */
411 && !ruler_title_hash_lookup(pgovern->ruler_titles,
412 NULL, &pruler_title)) {
413 log_error("Missing title for government \"%s\" (nb %d) "
414 "nation \"%s\" (nb %d).",
418 } else {
421 ? &pruler_title->male
422 : &pruler_title->female),
423 player_name(pplayer));
424 }
425
426 return buf;
427}
428
429/**********************************************************************/
432const char *default_title_for_player(const struct player *pplayer,
433 char *buf, size_t buf_len)
434{
435 if (pplayer->is_male) {
436 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
437 } else {
438 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
439 }
440
441 return buf;
442}
443
444/**************************************************************************
445 Government iterator.
446**************************************************************************/
449 struct government *p, *end;
450};
451#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
452
453/**********************************************************************/
457{
458 return sizeof(struct government_iter);
459}
460
461/**********************************************************************/
465{
466 GOVERNMENT_ITER(iter)->p++;
467}
468
469/**********************************************************************/
472static void *government_iter_get(const struct iterator *iter)
473{
474 return GOVERNMENT_ITER(iter)->p;
475}
476
477/**********************************************************************/
480static bool government_iter_valid(const struct iterator *iter)
481{
483 return it->p < it->end;
484}
485
486/**********************************************************************/
498
499/**********************************************************************/
502static inline void government_init(struct government *pgovern)
503{
504 memset(pgovern, 0, sizeof(*pgovern));
505
506 pgovern->item_number = pgovern - governments;
511 pgovern->changed_to_times = 0;
512 pgovern->ruledit_disabled = FALSE;
513 pgovern->ruledit_dlg = NULL;
514}
515
516/**********************************************************************/
519static inline void government_free(struct government *pgovern)
520{
521 ruler_title_hash_destroy(pgovern->ruler_titles);
522 pgovern->ruler_titles = NULL;
523
524 if (NULL != pgovern->helptext) {
525 strvec_destroy(pgovern->helptext);
526 pgovern->helptext = NULL;
527 }
528
530}
531
532/**********************************************************************/
535void governments_alloc(int num)
536{
537 int i;
538
540 governments = fc_malloc(sizeof(*governments) * num);
542
543 for (i = 0; i < game.control.government_count; i++) {
545 }
546}
547
548/**********************************************************************/
552{
553 int i;
554
555 if (NULL == governments) {
556 return;
557 }
558
559 for (i = 0; i < game.control.government_count; i++) {
561 }
562
566}
567
568/**********************************************************************/
573{
576 /* We need to know the target government at the onset of the revolution
577 * in order to know how long anarchy will last. */
578 return FALSE;
579 }
580
581 return TRUE;
582}
583
584/**********************************************************************/
588 enum gov_flag_id flag)
589{
591
592 return BV_ISSET(pgov->flags, flag);
593}
594
595
596/************************************************************************/
600{
601 int i;
602
603 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
605 }
606}
607
608/************************************************************************/
612{
613 int i;
614
615 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
617 }
618}
619
620/************************************************************************/
623void set_user_gov_flag_name(enum gov_flag_id id, const char *name,
624 const char *helptxt)
625{
626 int gfid = id - GOVF_USER_FLAG_1;
627
629
631 user_gov_flags[gfid].name = nullptr;
632
633 if (name && name[0] != '\0') {
635 }
636
637 free(user_gov_flags[gfid].helptxt);
638 user_gov_flags[gfid].helptxt = nullptr;
639
640 if (helptxt && helptxt[0] != '\0') {
642 }
643}
644
645/************************************************************************/
648const char *gov_flag_id_name_cb(enum gov_flag_id flag)
649{
651 return nullptr;
652 }
653
654 return user_gov_flags[flag - GOVF_USER_FLAG_1].name;
655}
656
657/************************************************************************/
660const char *gov_flag_helptxt(enum gov_flag_id id)
661{
663
665}
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
#define city_owner(_pcity_)
Definition city.h:563
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: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:648
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:432
bool government_has_flag(const struct government *pgov, enum gov_flag_id flag)
Definition government.c:587
void governments_free(void)
Definition government.c:551
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:371
bool untargeted_revolution_allowed(void)
Definition government.c:572
static void government_init(struct government *pgovern)
Definition government.c:502
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:331
static void government_iter_next(struct iterator *iter)
Definition government.c:464
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:397
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:489
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:389
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:220
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:480
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:211
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:380
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:237
static void government_free(struct government *pgovern)
Definition government.c:519
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:660
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:203
void governments_alloc(int num)
Definition government.c:535
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:172
void user_gov_flags_init(void)
Definition government.c:599
void gov_flags_free(void)
Definition government.c:611
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:320
Government_type_id government_index(const struct government *pgovern)
Definition government.c:84
size_t government_iter_sizeof(void)
Definition government.c:456
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:245
#define GOVERNMENT_ITER(p)
Definition government.c:451
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:472
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:623
#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: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:449
struct iterator vtable
Definition government.c:448
struct government * end
Definition government.c:449
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:196
struct name_translation female
Definition government.c:197
const struct nation_type * pnation
Definition government.c:195
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