Freeciv-3.1
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 "player.h"
29#include "tech.h"
30
31#include "government.h"
32
33struct government *governments = NULL;
34
35/**********************************************************************/
40{
42 if (0 == strcmp(government_name_translation(gov), name)) {
43 return gov;
44 }
46
47 return NULL;
48}
49
50/**********************************************************************/
55{
56 const char *qname = Qn_(name);
57
59 if (0 == fc_strcasecmp(government_rule_name(gov), qname)) {
60 return gov;
61 }
63
64 return NULL;
65}
66
67/**********************************************************************/
74
75/**********************************************************************/
82{
83 fc_assert_ret_val(NULL != pgovern, -1);
84 return pgovern - governments;
85}
86
87/**********************************************************************/
91{
92 fc_assert_ret_val(NULL != pgovern, -1);
93 return pgovern->item_number;
94}
95
96/**********************************************************************/
103{
104 if (gov < 0 || gov >= game.control.government_count) {
105 return NULL;
106 }
107 return &governments[gov];
108}
109
110/**********************************************************************/
113struct government *government_of_player(const struct player *pplayer)
114{
115 fc_assert_ret_val(NULL != pplayer, NULL);
116 return pplayer->government;
117}
118
119/**********************************************************************/
122struct government *government_of_city(const struct city *pcity)
123{
124 fc_assert_ret_val(NULL != pcity, NULL);
125 return government_of_player(city_owner(pcity));
126}
127
128/**********************************************************************/
132const char *government_rule_name(const struct government *pgovern)
133{
134 fc_assert_ret_val(NULL != pgovern, NULL);
135 return rule_name_get(&pgovern->name);
136}
137
138/**********************************************************************/
142const char *government_name_translation(const struct government *pgovern)
143{
144 fc_assert_ret_val(NULL != pgovern, NULL);
145
146 return name_translation_get(&pgovern->name);
147}
148
149/**********************************************************************/
153const char *government_name_for_player(const struct player *pplayer)
154{
156}
157
158/**********************************************************************/
165bool can_change_to_government(struct player *pplayer,
166 const struct government *gov)
167{
168 fc_assert_ret_val(NULL != gov, FALSE);
169
170 if (!pplayer) {
171 return FALSE;
172 }
173
174 if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
175 /* Note, this may allow govs that are on someone else's "tech tree". */
176 return TRUE;
177 }
178
179 return are_reqs_active(&(const struct req_context) { .player = pplayer },
180 NULL, &gov->reqs, RPT_CERTAIN);
181}
182
183
184/**************************************************************************
185 Ruler titles.
186**************************************************************************/
192
193/**********************************************************************/
196static genhash_val_t nation_hash_val(const struct nation_type *pnation)
197{
198 return NULL != pnation ? nation_number(pnation) : nation_count();
199}
200
201/**********************************************************************/
204static bool nation_hash_comp(const struct nation_type *pnation1,
205 const struct nation_type *pnation2)
206{
207 return pnation1 == pnation2;
208}
209
210/**********************************************************************/
213static struct ruler_title *ruler_title_new(const struct nation_type *pnation,
214 const char *domain,
215 const char *ruler_male_title,
216 const char *ruler_female_title)
217{
218 struct ruler_title *pruler_title = fc_malloc(sizeof(*pruler_title));
219
220 pruler_title->pnation = pnation;
221 name_set(&pruler_title->male, domain, ruler_male_title);
222 name_set(&pruler_title->female, domain, ruler_female_title);
223
224 return pruler_title;
225}
226
227/**********************************************************************/
230static void ruler_title_destroy(struct ruler_title *pruler_title)
231{
232 free(pruler_title);
233}
234
235/**********************************************************************/
238static bool ruler_title_check(const struct ruler_title *pruler_title)
239{
240 bool ret = TRUE;
241
242 if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
243 if (NULL != pruler_title->pnation) {
244 log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
245 "is not a format. It should match \"%%s\"",
246 rule_name_get(&pruler_title->male),
247 nation_rule_name(pruler_title->pnation),
248 nation_number(pruler_title->pnation));
249 } else {
250 log_error("\"%s\" male ruler title is not a format. "
251 "It should match \"%%s\"",
252 rule_name_get(&pruler_title->male));
253 }
254 ret = FALSE;
255 }
256
257 if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
258 if (NULL != pruler_title->pnation) {
259 log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
260 "is not a format. It should match \"%%s\"",
261 rule_name_get(&pruler_title->female),
262 nation_rule_name(pruler_title->pnation),
263 nation_number(pruler_title->pnation));
264 } else {
265 log_error("\"%s\" female ruler title is not a format. "
266 "It should match \"%%s\"",
267 rule_name_get(&pruler_title->female));
268 }
269 ret = FALSE;
270 }
271
272 if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
273 if (NULL != pruler_title->pnation) {
274 log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
275 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
276 rule_name_get(&pruler_title->male),
277 nation_rule_name(pruler_title->pnation),
278 nation_number(pruler_title->pnation),
279 name_translation_get(&pruler_title->male));
280 } else {
281 log_error("Translation of \"%s\" male ruler title is not a format "
282 "(\"%s\"). It should match \"%%s\"",
283 rule_name_get(&pruler_title->male),
284 name_translation_get(&pruler_title->male));
285 }
286 ret = FALSE;
287 }
288
289 if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
290 if (NULL != pruler_title->pnation) {
291 log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
292 "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
293 rule_name_get(&pruler_title->female),
294 nation_rule_name(pruler_title->pnation),
295 nation_number(pruler_title->pnation),
296 name_translation_get(&pruler_title->female));
297 } else {
298 log_error("Translation of \"%s\" female ruler title is not a format "
299 "(\"%s\"). It should match \"%%s\"",
300 rule_name_get(&pruler_title->female),
301 name_translation_get(&pruler_title->female));
302 }
303 ret = FALSE;
304 }
305
306 return ret;
307}
308
309/**********************************************************************/
312const struct ruler_title_hash *
314{
315 fc_assert_ret_val(NULL != pgovern, NULL);
316 return pgovern->ruler_titles;
317}
318
319/**********************************************************************/
323struct ruler_title *
325 const struct nation_type *pnation,
326 const char *ruler_male_title,
327 const char *ruler_female_title)
328{
329 const char *domain = NULL;
330 struct ruler_title *pruler_title;
331
332 if (pnation != NULL) {
333 domain = pnation->translation_domain;
334 }
335 pruler_title =
336 ruler_title_new(pnation, domain, ruler_male_title, ruler_female_title);
337
338 if (!ruler_title_check(pruler_title)) {
339 ruler_title_destroy(pruler_title);
340 return NULL;
341 }
342
343 if (ruler_title_hash_replace(pgovern->ruler_titles,
344 pnation, pruler_title)) {
345 if (NULL != pnation) {
346 log_error("Ruler title for government \"%s\" (nb %d) and "
347 "nation \"%s\" (nb %d) was set twice.",
348 government_rule_name(pgovern), government_number(pgovern),
350 } else {
351 log_error("Default ruler title for government \"%s\" (nb %d) "
352 "was set twice.", government_rule_name(pgovern),
353 government_number(pgovern));
354 }
355 }
356
357 return pruler_title;
358}
359
360/**********************************************************************/
363const struct nation_type *
364ruler_title_nation(const struct ruler_title *pruler_title)
365{
366 return pruler_title->pnation;
367}
368
369/**********************************************************************/
372const char *
374{
375 return untranslated_name(&pruler_title->male);
376}
377
378/**********************************************************************/
381const char *
383{
384 return untranslated_name(&pruler_title->female);
385}
386
387/**********************************************************************/
390const char *ruler_title_for_player(const struct player *pplayer,
391 char *buf, size_t buf_len)
392{
393 const struct government *pgovern = government_of_player(pplayer);
394 const struct nation_type *pnation = nation_of_player(pplayer);
395 struct ruler_title *pruler_title;
396
397 fc_assert_ret_val(NULL != buf, NULL);
398 fc_assert_ret_val(0 < buf_len, NULL);
399
400 /* Try specific nation ruler title. */
401 if (!ruler_title_hash_lookup(pgovern->ruler_titles,
402 pnation, &pruler_title)
403 /* Try default ruler title. */
404 && !ruler_title_hash_lookup(pgovern->ruler_titles,
405 NULL, &pruler_title)) {
406 log_error("Missing title for government \"%s\" (nb %d) "
407 "nation \"%s\" (nb %d).",
408 government_rule_name(pgovern), government_number(pgovern),
410 default_title_for_player(pplayer, buf, buf_len);
411 } else {
412 fc_snprintf(buf, buf_len,
414 ? &pruler_title->male
415 : &pruler_title->female),
416 player_name(pplayer));
417 }
418
419 return buf;
420}
421
422/**********************************************************************/
425const char *default_title_for_player(const struct player *pplayer,
426 char *buf, size_t buf_len)
427{
428 if (pplayer->is_male) {
429 fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
430 } else {
431 fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
432 }
433
434 return buf;
435}
436
437/**************************************************************************
438 Government iterator.
439**************************************************************************/
442 struct government *p, *end;
443};
444#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
445
446/**********************************************************************/
450{
451 return sizeof(struct government_iter);
452}
453
454/**********************************************************************/
457static void government_iter_next(struct iterator *iter)
458{
459 GOVERNMENT_ITER(iter)->p++;
460}
461
462/**********************************************************************/
465static void *government_iter_get(const struct iterator *iter)
466{
467 return GOVERNMENT_ITER(iter)->p;
468}
469
470/**********************************************************************/
473static bool government_iter_valid(const struct iterator *iter)
474{
475 struct government_iter *it = GOVERNMENT_ITER(iter);
476 return it->p < it->end;
477}
478
479/**********************************************************************/
491
492/**********************************************************************/
495static inline void government_init(struct government *pgovern)
496{
497 memset(pgovern, 0, sizeof(*pgovern));
498
499 pgovern->item_number = pgovern - governments;
500 pgovern->ruler_titles =
501 ruler_title_hash_new_full(nation_hash_val, nation_hash_comp,
502 NULL, NULL, NULL, ruler_title_destroy);
503 requirement_vector_init(&pgovern->reqs);
504 pgovern->changed_to_times = 0;
505 pgovern->ruledit_disabled = FALSE;
506 pgovern->ruledit_dlg = NULL;
507}
508
509/**********************************************************************/
512static inline void government_free(struct government *pgovern)
513{
514 ruler_title_hash_destroy(pgovern->ruler_titles);
515 pgovern->ruler_titles = NULL;
516
517 if (NULL != pgovern->helptext) {
518 strvec_destroy(pgovern->helptext);
519 pgovern->helptext = NULL;
520 }
521
522 requirement_vector_free(&pgovern->reqs);
523}
524
525/**********************************************************************/
528void governments_alloc(int num)
529{
530 int i;
531
532 fc_assert(NULL == governments);
533 governments = fc_malloc(sizeof(*governments) * num);
535
536 for (i = 0; i < game.control.government_count; i++) {
538 }
539}
540
541/**********************************************************************/
545{
546 int i;
547
548 if (NULL == governments) {
549 return;
550 }
551
552 for (i = 0; i < game.control.government_count; i++) {
554 }
555
556 free(governments);
557 governments = NULL;
559}
560
561/**********************************************************************/
566{
569 /* We need to know the target government at the onset of the revolution
570 * in order to know how long anarchy will last. */
571 return FALSE;
572 }
573 return TRUE;
574}
#define city_owner(_pcity_)
Definition city.h:543
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:771
@ RPT_CERTAIN
Definition fc_types.h:586
@ REVOLEN_RANDQUICK
Definition fc_types.h:1138
@ REVOLEN_QUICKENING
Definition fc_types.h:1137
int Government_type_id
Definition fc_types.h:351
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
struct civ_game game
Definition game.c:57
unsigned int genhash_val_t
Definition genhash.h:32
const char * government_name_translation(const struct government *pgovern)
Definition government.c:142
const char * default_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:425
void governments_free(void)
Definition government.c:544
const struct nation_type * ruler_title_nation(const struct ruler_title *pruler_title)
Definition government.c:364
bool untargeted_revolution_allowed(void)
Definition government.c:565
static void government_init(struct government *pgovern)
Definition government.c:495
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:324
static void government_iter_next(struct iterator *iter)
Definition government.c:457
const char * ruler_title_for_player(const struct player *pplayer, char *buf, size_t buf_len)
Definition government.c:390
struct iterator * government_iter_init(struct government_iter *it)
Definition government.c:482
const char * ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:382
Government_type_id government_count(void)
Definition government.c:70
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:213
static bool government_iter_valid(const struct iterator *iter)
Definition government.c:473
struct government * government_by_number(const Government_type_id gov)
Definition government.c:102
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
const char * government_name_for_player(const struct player *pplayer)
Definition government.c:153
static bool nation_hash_comp(const struct nation_type *pnation1, const struct nation_type *pnation2)
Definition government.c:204
const char * ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
Definition government.c:373
static void ruler_title_destroy(struct ruler_title *pruler_title)
Definition government.c:230
static void government_free(struct government *pgovern)
Definition government.c:512
struct government * government_of_city(const struct city *pcity)
Definition government.c:122
struct government * governments
Definition government.c:33
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
Definition government.c:196
void governments_alloc(int num)
Definition government.c:528
bool can_change_to_government(struct player *pplayer, const struct government *gov)
Definition government.c:165
Government_type_id government_number(const struct government *pgovern)
Definition government.c:90
const struct ruler_title_hash * government_ruler_titles(const struct government *pgovern)
Definition government.c:313
Government_type_id government_index(const struct government *pgovern)
Definition government.c:81
size_t government_iter_sizeof(void)
Definition government.c:449
static bool ruler_title_check(const struct ruler_title *pruler_title)
Definition government.c:238
#define GOVERNMENT_ITER(p)
Definition government.c:444
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:465
struct government * government_by_translated_name(const char *name)
Definition government.c:39
struct government * government_by_rule_name(const char *name)
Definition government.c:54
#define governments_iterate(NAME_pgov)
Definition government.h:121
#define governments_iterate_end
Definition government.h:124
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:137
Nation_type_id nation_count(void)
Definition nation.c:506
Nation_type_id nation_number(const struct nation_type *pnation)
Definition nation.c:485
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:886
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:2433
void strvec_destroy(struct strvec *psv)
Definition city.h:309
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
struct government * p
Definition government.c:442
struct iterator vtable
Definition government.c:441
struct government * end
Definition government.c:442
struct strvec * helptext
Definition government.h:62
struct requirement_vector reqs
Definition government.h:59
void * ruledit_dlg
Definition government.h:56
bool ruledit_disabled
Definition government.h:55
Government_type_id item_number
Definition government.h:53
int changed_to_times
Definition government.h:61
struct name_translation name
Definition government.h:54
struct ruler_title_hash * ruler_titles
Definition government.h:60
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:99
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:189
struct name_translation female
Definition government.c:190
const struct nation_type * pnation
Definition government.c:188
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
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