Freeciv-3.3
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
36/**********************************************************************/
41{
43 if (0 == strcmp(government_name_translation(gov), name)) {
44 return gov;
45 }
47
48 return NULL;
49}
50
51/**********************************************************************/
56{
57 const char *qname = Qn_(name);
58
61 return gov;
62 }
64
65 return NULL;
66}
67
68/**********************************************************************/
75
76/**********************************************************************/
87
88/**********************************************************************/
92{
94 return pgovern->item_number;
95}
96
97/**********************************************************************/
104{
106 return NULL;
107 }
108 return &governments[gov];
109}
110
111/**********************************************************************/
114struct government *government_of_player(const struct player *pplayer)
115{
116 fc_assert_ret_val(NULL != pplayer, NULL);
117 return pplayer->government;
118}
119
120/**********************************************************************/
128
129/**********************************************************************/
133const char *government_rule_name(const struct government *pgovern)
134{
136 return rule_name_get(&pgovern->name);
137}
138
139/**********************************************************************/
144{
146
147 return name_translation_get(&pgovern->name);
148}
149
150/**********************************************************************/
154const char *government_name_for_player(const struct player *pplayer)
155{
156 const struct government *pgov;
157
158 if (!pplayer->is_alive) {
159 return "-";
160 }
161
162 pgov = government_of_player(pplayer);
163
164 if (!pgov) {
165 /* Can happen on client side when we have
166 * no contact to the player. */
167 return "?";
168 }
169
171}
172
173/**********************************************************************/
180bool can_change_to_government(struct player *pplayer,
181 const struct government *gov)
182{
184
185 if (!pplayer) {
186 return FALSE;
187 }
188
189 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
190 /* Note, this may allow govs that are on someone else's "tech tree". */
191 return TRUE;
192 }
193
194 return are_reqs_active(&(const struct req_context) { .player = pplayer },
195 NULL, &gov->reqs, RPT_CERTAIN);
196}
197
198
199/**************************************************************************
200 Ruler titles.
201**************************************************************************/
207
208/**********************************************************************/
211static genhash_val_t nation_hash_val(const struct nation_type *pnation)
212{
213 return NULL != pnation ? nation_number(pnation) : nation_count();
214}
215
216/**********************************************************************/
219static bool nation_hash_comp(const struct nation_type *pnation1,
220 const struct nation_type *pnation2)
221{
222 return pnation1 == pnation2;
223}
224
225/**********************************************************************/
228static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
229 const char *domain,
230 const char *ruler_male_title,
231 const char *ruler_female_title)
232{
234
235 pruler_title->pnation = pnation;
238
239 return pruler_title;
240}
241
242/**********************************************************************/
246{
248}
249
250/**********************************************************************/
254{
255 bool ret = TRUE;
256
257 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
258 if (NULL != pruler_title->pnation) {
259 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
260 "is not a format. It should match \"%%s\"",
263 nation_number(pruler_title->pnation));
264 } else {
265 log_error("\"%s\" male ruler title is not a format. "
266 "It should match \"%%s\"",
268 }
269 ret = FALSE;
270 }
271
272 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
273 if (NULL != pruler_title->pnation) {
274 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
275 "is not a format. It should match \"%%s\"",
276 rule_name_get(&pruler_title->female),
278 nation_number(pruler_title->pnation));
279 } else {
280 log_error("\"%s\" female ruler title is not a format. "
281 "It should match \"%%s\"",
282 rule_name_get(&pruler_title->female));
283 }
284 ret = FALSE;
285 }
286
287 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
288 if (NULL != pruler_title->pnation) {
289 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
290 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
293 nation_number(pruler_title->pnation),
295 } else {
296 log_error("Translation of \"%s\" male ruler title is not a format "
297 "(\"%s\"). It should match \"%%s\"",
300 }
301 ret = FALSE;
302 }
303
304 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
305 if (NULL != pruler_title->pnation) {
306 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
307 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
308 rule_name_get(&pruler_title->female),
310 nation_number(pruler_title->pnation),
312 } else {
313 log_error("Translation of \"%s\" female ruler title is not a format "
314 "(\"%s\"). It should match \"%%s\"",
315 rule_name_get(&pruler_title->female),
317 }
318 ret = FALSE;
319 }
320
321 return ret;
322}
323
324/**********************************************************************/
327const struct ruler_title_hash *
329{
331 return pgovern->ruler_titles;
332}
333
334/**********************************************************************/
338struct ruler_title *
340 const struct nation_type *pnation,
341 const char *ruler_male_title,
342 const char *ruler_female_title)
343{
344 const char *domain = NULL;
346
347 if (pnation != NULL) {
349 }
352
355 return NULL;
356 }
357
358 if (ruler_title_hash_replace(pgovern->ruler_titles,
360 if (NULL != pnation) {
361 log_error("Ruler title for government \"%s\" (nb %d) and "
362 "nation \"%s\" (nb %d) was set twice.",
365 } else {
366 log_error("Default ruler title for government \"%s\" (nb %d) "
367 "was set twice.", government_rule_name(pgovern),
369 }
370 }
371
372 return pruler_title;
373}
374
375/**********************************************************************/
378const struct nation_type *
380{
381 return pruler_title->pnation;
382}
383
384/**********************************************************************/
387const char *
392
393/**********************************************************************/
396const char *
401
402/**********************************************************************/
405const char *ruler_title_for_player(const struct player *pplayer,
406 char *buf, size_t buf_len)
407{
408 const struct government *pgovern = government_of_player(pplayer);
409 const struct nation_type *pnation = nation_of_player(pplayer);
411
414
415 /* Try specific nation ruler title. */
416 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
418 /* Try default ruler title. */
419 && !ruler_title_hash_lookup(pgovern->ruler_titles,
420 NULL, &pruler_title)) {
421 log_error("Missing title for government \"%s\" (nb %d) "
422 "nation \"%s\" (nb %d).",
426 } else {
429 ? &pruler_title->male
430 : &pruler_title->female),
431 player_name(pplayer));
432 }
433
434 return buf;
435}
436
437/**********************************************************************/
440const char *default_title_for_player(const struct player *pplayer,
441 char *buf, size_t buf_len)
442{
443 if (pplayer->is_male) {
444 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
445 } else {
446 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
447 }
448
449 return buf;
450}
451
452/**************************************************************************
453 Government iterator.
454**************************************************************************/
457 struct government *p, *end;
458};
459#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
460
461/**********************************************************************/
465{
466 return sizeof(struct government_iter);
467}
468
469/**********************************************************************/
473{
474 GOVERNMENT_ITER(iter)->p++;
475}
476
477/**********************************************************************/
480static void *government_iter_get(const struct iterator *iter)
481{
482 return GOVERNMENT_ITER(iter)->p;
483}
484
485/**********************************************************************/
488static bool government_iter_valid(const struct iterator *iter)
489{
491 return it->p < it->end;
492}
493
494/**********************************************************************/
506
507/**********************************************************************/
510static inline void government_init(struct government *pgovern)
511{
512 memset(pgovern, 0, sizeof(*pgovern));
513
514 pgovern->item_number = pgovern - governments;
519 pgovern->changed_to_times = 0;
520 pgovern->ruledit_disabled = FALSE;
521 pgovern->ruledit_dlg = NULL;
522}
523
524/**********************************************************************/
527static inline void government_free(struct government *pgovern)
528{
529 ruler_title_hash_destroy(pgovern->ruler_titles);
530 pgovern->ruler_titles = NULL;
531
532 if (NULL != pgovern->helptext) {
533 strvec_destroy(pgovern->helptext);
534 pgovern->helptext = NULL;
535 }
536
538}
539
540/**********************************************************************/
543void governments_alloc(int num)
544{
545 int i;
546
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 (NULL == governments) {
564 return;
565 }
566
567 for (i = 0; i < game.control.government_count; i++) {
569 }
570
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 return TRUE;
589}
#define city_owner(_pcity_)
Definition city.h:560
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:77
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:824
@ RPT_CERTAIN
Definition fc_types.h:534
@ REVOLEN_RANDQUICK
Definition fc_types.h:994
@ REVOLEN_QUICKENING
Definition fc_types.h:993
int Government_type_id
Definition fc_types.h:241
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
struct civ_game game
Definition game.c:61
unsigned int genhash_val_t
Definition genhash.h:32
const char * government_name_translation(const struct government *pgovern)
Definition government.c:143
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:440
void governments_free(void)
Definition government.c:559
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:379
bool untargeted_revolution_allowed(void)
Definition government.c:580
static void government_init(struct government *pgovern)
Definition government.c:510
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:339
static void government_iter_next(struct iterator *iter)
Definition government.c:472
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:405
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:497
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:397
Government_type_id government_count(void)
Definition government.c:71
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:228
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:488
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:114
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:154
static bool nation_hash_comp(const struct nation_type *pnation1, const struct nation_type *pnation2)
Definition government.c:219
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:388
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:245
static void government_free(struct government *pgovern)
Definition government.c:527
struct government * government_of_city(const struct city *pcity)
Definition government.c:123
struct government * governments
Definition government.c:34
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:211
void governments_alloc(int num)
Definition government.c:543
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:180
Government_type_id government_number(const struct government *pgovern)
Definition government.c:91
const struct ruler_title_hash * government_ruler_titles(const struct government *pgovern)
Definition government.c:328
Government_type_id government_index(const struct government *pgovern)
Definition government.c:82
size_t government_iter_sizeof(void)
Definition government.c:464
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:253
#define GOVERNMENT_ITER(p)
Definition government.c:459
const char * government_rule_name(const struct government *pgovern)
Definition government.c:133
static void * government_iter_get(const struct iterator *iter)
Definition government.c:480
struct government * government_by_translated_name(const char *name)
Definition government.c:40
struct government * government_by_rule_name(const char *name)
Definition government.c:55
#define governments_iterate(NAME_pgov)
Definition government.h:124
#define governments_iterate_end
Definition government.h:127
const char * name
Definition inputfile.c:127
#define ITERATOR(p)
Definition iterator.h:37
#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_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:507
Nation_type_id nation_number(const struct nation_type *pnation)
Definition nation.c:486
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
const char * player_name(const struct player *pplayer)
Definition player.c:895
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:317
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
struct government * p
Definition government.c:457
struct iterator vtable
Definition government.c:456
struct government * end
Definition government.c:457
struct requirement_vector reqs
Definition government.h:62
struct ruler_title_hash * ruler_titles
Definition government.h:63
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:204
struct name_translation female
Definition government.c:205
const struct nation_type * pnation
Definition government.c:203
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