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 const struct government *pgov;
153
154 if (!pplayer->is_alive) {
155 return "-";
156 }
157
158 pgov = government_of_player(pplayer);
159
160 if (!pgov) {
161 /* Can happen on client side when we have
162 * no contact to the player. */
163 return "?";
164 }
165
167}
168
169/**********************************************************************/
176bool can_change_to_government(struct player *pplayer,
177 const struct government *gov)
178{
179 if (pplayer == nullptr) {
180 return FALSE;
181 }
182
183 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
184 /* Note, this may allow govs that are on someone else's "tech tree". */
185 return TRUE;
186 }
187
188 return are_reqs_active(&(const struct req_context) { .player = pplayer },
189 nullptr, &gov->reqs, RPT_CERTAIN);
190}
191
192
193/**************************************************************************
194 Ruler titles.
195**************************************************************************/
201
202/**********************************************************************/
205static genhash_val_t nation_hash_val(const struct nation_type *pnation)
206{
207 return pnation != nullptr ? nation_number(pnation) : nation_count();
208}
209
210/**********************************************************************/
213static bool nation_hash_comp(const struct nation_type *pnation1,
214 const struct nation_type *pnation2)
215{
216 return pnation1 == pnation2;
217}
218
219/**********************************************************************/
222static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
223 const char *domain,
224 const char *ruler_male_title,
225 const char *ruler_female_title)
226{
228
229 pruler_title->pnation = pnation;
232
233 return pruler_title;
234}
235
236/**********************************************************************/
240{
242}
243
244/**********************************************************************/
248{
249 bool ret = TRUE;
250
251 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
252 if (pruler_title->pnation != nullptr) {
253 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
254 "is not a format. It should match \"%%s\"",
257 nation_number(pruler_title->pnation));
258 } else {
259 log_error("\"%s\" male ruler title is not a format. "
260 "It should match \"%%s\"",
262 }
263 ret = FALSE;
264 }
265
266 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
267 if (pruler_title->pnation != nullptr) {
268 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
269 "is not a format. It should match \"%%s\"",
270 rule_name_get(&pruler_title->female),
272 nation_number(pruler_title->pnation));
273 } else {
274 log_error("\"%s\" female ruler title is not a format. "
275 "It should match \"%%s\"",
276 rule_name_get(&pruler_title->female));
277 }
278 ret = FALSE;
279 }
280
281 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
282 if (pruler_title->pnation != nullptr) {
283 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
284 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
287 nation_number(pruler_title->pnation),
289 } else {
290 log_error("Translation of \"%s\" male ruler title is not a format "
291 "(\"%s\"). It should match \"%%s\"",
294 }
295 ret = FALSE;
296 }
297
298 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
299 if (pruler_title->pnation != nullptr) {
300 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
301 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
302 rule_name_get(&pruler_title->female),
304 nation_number(pruler_title->pnation),
306 } else {
307 log_error("Translation of \"%s\" female ruler title is not a format "
308 "(\"%s\"). It should match \"%%s\"",
309 rule_name_get(&pruler_title->female),
311 }
312 ret = FALSE;
313 }
314
315 return ret;
316}
317
318/**********************************************************************/
321const struct ruler_title_hash *
323{
324 return pgovern->ruler_titles;
325}
326
327/**********************************************************************/
331struct ruler_title *
333 const struct nation_type *pnation,
334 const char *ruler_male_title,
335 const char *ruler_female_title)
336{
337 const char *domain = nullptr;
339
340 if (pnation != nullptr) {
342 }
345
348 return nullptr;
349 }
350
351 if (ruler_title_hash_replace(pgovern->ruler_titles,
353 if (pnation != nullptr) {
354 log_error("Ruler title for government \"%s\" (nb %d) and "
355 "nation \"%s\" (nb %d) was set twice.",
358 } else {
359 log_error("Default ruler title for government \"%s\" (nb %d) "
360 "was set twice.", government_rule_name(pgovern),
362 }
363 }
364
365 return pruler_title;
366}
367
368/**********************************************************************/
371const struct nation_type *
373{
374 return pruler_title->pnation;
375}
376
377/**********************************************************************/
380const char *
385
386/**********************************************************************/
389const char *
394
395/**********************************************************************/
398const char *ruler_title_for_player(const struct player *pplayer,
399 char *buf, size_t buf_len)
400{
401 const struct government *pgovern = government_of_player(pplayer);
402 const struct nation_type *pnation = nation_of_player(pplayer);
404
405 fc_assert_ret_val(0 < buf_len, nullptr);
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 nullptr, &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;
509 nullptr, nullptr, nullptr, ruler_title_destroy);
511 pgovern->changed_to_times = 0;
512 pgovern->ruledit_disabled = FALSE;
513 pgovern->ruledit_dlg = nullptr;
514}
515
516/**********************************************************************/
519static inline void government_free(struct government *pgovern)
520{
521 ruler_title_hash_destroy(pgovern->ruler_titles);
522 pgovern->ruler_titles = nullptr;
523
524 if (pgovern->helptext != nullptr) {
525 strvec_destroy(pgovern->helptext);
526 pgovern->helptext = nullptr;
527 }
528
530}
531
532/**********************************************************************/
535void governments_alloc(int num)
536{
537 int i;
538
539 fc_assert(governments == nullptr);
540
541 governments = fc_malloc(sizeof(*governments) * num);
543
544 for (i = 0; i < game.control.government_count; i++) {
546 }
547}
548
549/**********************************************************************/
553{
554 int i;
555
556 if (governments == nullptr) {
557 return;
558 }
559
560 for (i = 0; i < game.control.government_count; i++) {
562 }
563
565 governments = nullptr;
567}
568
569/**********************************************************************/
574{
577 /* We need to know the target government at the onset of the revolution
578 * in order to know how long anarchy will last. */
579 return FALSE;
580 }
581
582 return TRUE;
583}
584
585/**********************************************************************/
589 enum gov_flag_id flag)
590{
592
593 return BV_ISSET(pgov->flags, flag);
594}
595
596
597/************************************************************************/
601{
602 int i;
603
604 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
606 }
607}
608
609/************************************************************************/
613{
614 int i;
615
616 for (i = 0; i < MAX_NUM_USER_GOVERNMENT_FLAGS; i++) {
618 }
619}
620
621/************************************************************************/
624void set_user_gov_flag_name(enum gov_flag_id id, const char *name,
625 const char *helptxt)
626{
627 int gfid = id - GOVF_USER_FLAG_1;
628
630
632 user_gov_flags[gfid].name = nullptr;
633
634 if (name && name[0] != '\0') {
636 }
637
638 free(user_gov_flags[gfid].helptxt);
639 user_gov_flags[gfid].helptxt = nullptr;
640
641 if (helptxt && helptxt[0] != '\0') {
643 }
644}
645
646/************************************************************************/
649const char *gov_flag_id_name_cb(enum gov_flag_id flag)
650{
652 return nullptr;
653 }
654
655 return user_gov_flags[flag - GOVF_USER_FLAG_1].name;
656}
657
658/************************************************************************/
661const char *gov_flag_helptxt(enum gov_flag_id id)
662{
664
666}
#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:77
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:649
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:588
void governments_free(void)
Definition government.c:552
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:372
bool untargeted_revolution_allowed(void)
Definition government.c:573
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:332
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:398
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:390
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:222
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: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:213
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:381
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:239
static void government_free(struct government *pgovern)
Definition government.c:519
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:661
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:205
void governments_alloc(int num)
Definition government.c:535
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:176
void user_gov_flags_init(void)
Definition government.c:600
void gov_flags_free(void)
Definition government.c:612
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:322
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:247
#define GOVERNMENT_ITER(p)
Definition government.c:451
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: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:624
#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:449
struct iterator vtable
Definition government.c:448
struct government * end
Definition government.c:449
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:198
struct name_translation female
Definition government.c:199
const struct nation_type * pnation
Definition government.c:197
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:925
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