Freeciv-3.2
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/**********************************************************************/
123struct government *government_of_city(const struct city *pcity)
124{
125 fc_assert_ret_val(NULL != pcity, NULL);
126 return government_of_player(city_owner(pcity));
127}
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{
157}
158
159/**********************************************************************/
166bool can_change_to_government(struct player *pplayer,
167 const struct government *gov)
168{
170
171 if (!pplayer) {
172 return FALSE;
173 }
174
175 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
176 /* Note, this may allow govs that are on someone else's "tech tree". */
177 return TRUE;
178 }
179
180 return are_reqs_active(&(const struct req_context) { .player = pplayer },
181 NULL, &gov->reqs, RPT_CERTAIN);
182}
183
184
185/**************************************************************************
186 Ruler titles.
187**************************************************************************/
193
194/**********************************************************************/
197static genhash_val_t nation_hash_val(const struct nation_type *pnation)
198{
199 return NULL != pnation ? nation_number(pnation) : nation_count();
200}
201
202/**********************************************************************/
205static bool nation_hash_comp(const struct nation_type *pnation1,
206 const struct nation_type *pnation2)
207{
208 return pnation1 == pnation2;
209}
210
211/**********************************************************************/
214static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
215 const char *domain,
216 const char *ruler_male_title,
217 const char *ruler_female_title)
218{
220
221 pruler_title->pnation = pnation;
224
225 return pruler_title;
226}
227
228/**********************************************************************/
232{
234}
235
236/**********************************************************************/
240{
241 bool ret = TRUE;
242
243 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
244 if (NULL != pruler_title->pnation) {
245 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
246 "is not a format. It should match \"%%s\"",
249 nation_number(pruler_title->pnation));
250 } else {
251 log_error("\"%s\" male ruler title is not a format. "
252 "It should match \"%%s\"",
254 }
255 ret = FALSE;
256 }
257
258 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
259 if (NULL != pruler_title->pnation) {
260 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
261 "is not a format. It should match \"%%s\"",
262 rule_name_get(&pruler_title->female),
264 nation_number(pruler_title->pnation));
265 } else {
266 log_error("\"%s\" female ruler title is not a format. "
267 "It should match \"%%s\"",
268 rule_name_get(&pruler_title->female));
269 }
270 ret = FALSE;
271 }
272
273 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
274 if (NULL != pruler_title->pnation) {
275 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
276 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
279 nation_number(pruler_title->pnation),
281 } else {
282 log_error("Translation of \"%s\" male ruler title is not a format "
283 "(\"%s\"). It should match \"%%s\"",
286 }
287 ret = FALSE;
288 }
289
290 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
291 if (NULL != pruler_title->pnation) {
292 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
293 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
294 rule_name_get(&pruler_title->female),
296 nation_number(pruler_title->pnation),
298 } else {
299 log_error("Translation of \"%s\" female ruler title is not a format "
300 "(\"%s\"). It should match \"%%s\"",
301 rule_name_get(&pruler_title->female),
303 }
304 ret = FALSE;
305 }
306
307 return ret;
308}
309
310/**********************************************************************/
313const struct ruler_title_hash *
315{
317 return pgovern->ruler_titles;
318}
319
320/**********************************************************************/
324struct ruler_title *
326 const struct nation_type *pnation,
327 const char *ruler_male_title,
328 const char *ruler_female_title)
329{
330 const char *domain = NULL;
332
333 if (pnation != NULL) {
335 }
338
341 return NULL;
342 }
343
344 if (ruler_title_hash_replace(pgovern->ruler_titles,
346 if (NULL != pnation) {
347 log_error("Ruler title for government \"%s\" (nb %d) and "
348 "nation \"%s\" (nb %d) was set twice.",
351 } else {
352 log_error("Default ruler title for government \"%s\" (nb %d) "
353 "was set twice.", government_rule_name(pgovern),
355 }
356 }
357
358 return pruler_title;
359}
360
361/**********************************************************************/
364const struct nation_type *
366{
367 return pruler_title->pnation;
368}
369
370/**********************************************************************/
373const char *
378
379/**********************************************************************/
382const char *
387
388/**********************************************************************/
391const char *ruler_title_for_player(const struct player *pplayer,
392 char *buf, size_t buf_len)
393{
394 const struct government *pgovern = government_of_player(pplayer);
395 const struct nation_type *pnation = nation_of_player(pplayer);
397
400
401 /* Try specific nation ruler title. */
402 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
404 /* Try default ruler title. */
405 && !ruler_title_hash_lookup(pgovern->ruler_titles,
406 NULL, &pruler_title)) {
407 log_error("Missing title for government \"%s\" (nb %d) "
408 "nation \"%s\" (nb %d).",
412 } else {
415 ? &pruler_title->male
416 : &pruler_title->female),
417 player_name(pplayer));
418 }
419
420 return buf;
421}
422
423/**********************************************************************/
426const char *default_title_for_player(const struct player *pplayer,
427 char *buf, size_t buf_len)
428{
429 if (pplayer->is_male) {
430 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
431 } else {
432 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
433 }
434
435 return buf;
436}
437
438/**************************************************************************
439 Government iterator.
440**************************************************************************/
443 struct government *p, *end;
444};
445#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
446
447/**********************************************************************/
451{
452 return sizeof(struct government_iter);
453}
454
455/**********************************************************************/
459{
460 GOVERNMENT_ITER(iter)->p++;
461}
462
463/**********************************************************************/
466static void *government_iter_get(const struct iterator *iter)
467{
468 return GOVERNMENT_ITER(iter)->p;
469}
470
471/**********************************************************************/
474static bool government_iter_valid(const struct iterator *iter)
475{
477 return it->p < it->end;
478}
479
480/**********************************************************************/
492
493/**********************************************************************/
496static inline void government_init(struct government *pgovern)
497{
498 memset(pgovern, 0, sizeof(*pgovern));
499
500 pgovern->item_number = pgovern - governments;
505 pgovern->changed_to_times = 0;
506 pgovern->ruledit_disabled = FALSE;
507 pgovern->ruledit_dlg = NULL;
508}
509
510/**********************************************************************/
513static inline void government_free(struct government *pgovern)
514{
515 ruler_title_hash_destroy(pgovern->ruler_titles);
516 pgovern->ruler_titles = NULL;
517
518 if (NULL != pgovern->helptext) {
519 strvec_destroy(pgovern->helptext);
520 pgovern->helptext = NULL;
521 }
522
524}
525
526/**********************************************************************/
529void governments_alloc(int num)
530{
531 int i;
532
534 governments = fc_malloc(sizeof(*governments) * num);
536
537 for (i = 0; i < game.control.government_count; i++) {
539 }
540}
541
542/**********************************************************************/
546{
547 int i;
548
549 if (NULL == governments) {
550 return;
551 }
552
553 for (i = 0; i < game.control.government_count; i++) {
555 }
556
560}
561
562/**********************************************************************/
567{
570 /* We need to know the target government at the onset of the revolution
571 * in order to know how long anarchy will last. */
572 return FALSE;
573 }
574 return TRUE;
575}
#define city_owner(_pcity_)
Definition city.h:563
char * incite_cost
Definition comments.c:75
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:828
@ RPT_CERTAIN
Definition fc_types.h:701
@ REVOLEN_RANDQUICK
Definition fc_types.h:1286
@ REVOLEN_QUICKENING
Definition fc_types.h:1285
int Government_type_id
Definition fc_types.h:381
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
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:143
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:426
void governments_free(void)
Definition government.c:545
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:365
bool untargeted_revolution_allowed(void)
Definition government.c:566
static void government_init(struct government *pgovern)
Definition government.c:496
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:325
static void government_iter_next(struct iterator *iter)
Definition government.c:458
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:391
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:483
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:383
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:214
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:474
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:205
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:374
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:231
static void government_free(struct government *pgovern)
Definition government.c:513
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:197
void governments_alloc(int num)
Definition government.c:529
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:166
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:314
Government_type_id government_index(const struct government *pgovern)
Definition government.c:82
size_t government_iter_sizeof(void)
Definition government.c:450
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:239
#define GOVERNMENT_ITER(p)
Definition government.c:445
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:466
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:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_error(message,...)
Definition log.h:103
#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 player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
bool formats_match(const char *format1, const char *format2)
Definition shared.c:2445
void strvec_destroy(struct strvec *psv)
Definition city.h:320
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
struct government * p
Definition government.c:443
struct iterator vtable
Definition government.c:442
struct government * end
Definition government.c:443
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:255
struct government * government
Definition player.h:256
struct name_translation male
Definition government.c:190
struct name_translation female
Definition government.c:191
const struct nation_type * pnation
Definition government.c:189
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47