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)
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 ***********************************************************************/
15 #include <fc_config.h>
23 #include "string_vector.h"
31 #include "government.h"
33 struct government
*governments
= NULL
;
35 /****************************************************************************
36 Returns the government that has the given (translated) name.
37 Returns NULL if none match.
38 ****************************************************************************/
39 struct government
*government_by_translated_name(const char *name
)
41 governments_iterate(gov
) {
42 if (0 == strcmp(government_name_translation(gov
), name
)) {
45 } governments_iterate_end
;
50 /****************************************************************************
51 Returns the government that has the given (untranslated) rule name.
52 Returns NULL if none match.
53 ****************************************************************************/
54 struct government
*government_by_rule_name(const char *name
)
56 const char *qname
= Qn_(name
);
58 governments_iterate(gov
) {
59 if (0 == fc_strcasecmp(government_rule_name(gov
), qname
)) {
62 } governments_iterate_end
;
67 /**************************************************************************
68 Return the number of governments.
69 **************************************************************************/
70 Government_type_id
government_count(void)
72 return game
.control
.government_count
;
75 /**************************************************************************
76 Return the government index.
78 Currently same as government_number(), paired with government_count()
79 indicates use as an array index.
80 **************************************************************************/
81 Government_type_id
government_index(const struct government
*pgovern
)
83 fc_assert_ret_val(NULL
!= pgovern
, -1);
84 return pgovern
- governments
;
87 /**************************************************************************
88 Return the government index.
89 **************************************************************************/
90 Government_type_id
government_number(const struct government
*pgovern
)
92 fc_assert_ret_val(NULL
!= pgovern
, -1);
93 return pgovern
->item_number
;
96 /****************************************************************************
97 Return the government with the given index.
99 This function returns NULL for an out-of-range index (some callers
101 ****************************************************************************/
102 struct government
*government_by_number(const Government_type_id gov
)
104 if (gov
< 0 || gov
>= game
.control
.government_count
) {
107 return &governments
[gov
];
110 /****************************************************************************
111 Return the government of a player.
112 ****************************************************************************/
113 struct government
*government_of_player(const struct player
*pplayer
)
115 fc_assert_ret_val(NULL
!= pplayer
, NULL
);
116 return pplayer
->government
;
119 /****************************************************************************
120 Return the government of the player who owns the city.
121 ****************************************************************************/
122 struct government
*government_of_city(const struct city
*pcity
)
124 fc_assert_ret_val(NULL
!= pcity
, NULL
);
125 return government_of_player(city_owner(pcity
));
128 /****************************************************************************
129 Return the (untranslated) rule name of the government.
130 You don't have to free the return pointer.
131 ****************************************************************************/
132 const char *government_rule_name(const struct government
*pgovern
)
134 fc_assert_ret_val(NULL
!= pgovern
, NULL
);
135 return rule_name_get(&pgovern
->name
);
138 /****************************************************************************
139 Return the (translated) name of the given government.
140 You don't have to free the return pointer.
141 ****************************************************************************/
142 const char *government_name_translation(const struct government
*pgovern
)
144 fc_assert_ret_val(NULL
!= pgovern
, NULL
);
146 return name_translation_get(&pgovern
->name
);
149 /****************************************************************************
150 Return the (translated) name of the given government of a player.
151 You don't have to free the return pointer.
152 ****************************************************************************/
153 const char *government_name_for_player(const struct player
*pplayer
)
155 return government_name_translation(government_of_player(pplayer
));
158 /***************************************************************
159 Can change to government if appropriate tech exists, and one of:
160 - no required tech (required is A_NONE)
161 - player has required tech
162 - we have an appropriate wonder
163 Returns FALSE if pplayer is NULL (used for observers).
164 ***************************************************************/
165 bool can_change_to_government(struct player
*pplayer
,
166 const struct government
*gov
)
168 fc_assert_ret_val(NULL
!= gov
, FALSE
);
174 if (get_player_bonus(pplayer
, EFT_ANY_GOVERNMENT
) > 0) {
175 /* Note, this may allow govs that are on someone else's "tech tree". */
179 return are_reqs_active(pplayer
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
180 NULL
, NULL
, NULL
, &gov
->reqs
, RPT_CERTAIN
);
184 /****************************************************************************
186 ****************************************************************************/
188 const struct nation_type
*pnation
;
189 struct name_translation male
;
190 struct name_translation female
;
193 /****************************************************************************
195 ****************************************************************************/
196 static genhash_val_t
nation_hash_val(const struct nation_type
*pnation
)
198 return NULL
!= pnation
? nation_number(pnation
) : nation_count();
201 /****************************************************************************
203 ****************************************************************************/
204 static bool nation_hash_comp(const struct nation_type
*pnation1
,
205 const struct nation_type
*pnation2
)
207 return pnation1
== pnation2
;
210 /****************************************************************************
211 Create a new ruler title.
212 ****************************************************************************/
213 static struct ruler_title
*ruler_title_new(const struct nation_type
*pnation
,
215 const char *ruler_male_title
,
216 const char *ruler_female_title
)
218 struct ruler_title
*pruler_title
= fc_malloc(sizeof(*pruler_title
));
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
);
227 /****************************************************************************
229 ****************************************************************************/
230 static void ruler_title_destroy(struct ruler_title
*pruler_title
)
235 /****************************************************************************
236 Return TRUE if the ruler title is valid.
237 ****************************************************************************/
238 static bool ruler_title_check(const struct ruler_title
*pruler_title
)
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
));
250 log_error("\"%s\" male ruler title is not a format. "
251 "It should match \"%%s\"",
252 rule_name_get(&pruler_title
->male
));
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
));
265 log_error("\"%s\" female ruler title is not a format. "
266 "It should match \"%%s\"",
267 rule_name_get(&pruler_title
->female
));
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
));
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
));
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
));
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
));
309 /****************************************************************************
310 Returns all ruler titles for a government type.
311 ****************************************************************************/
312 const struct ruler_title_hash
*
313 government_ruler_titles(const struct government
*pgovern
)
315 fc_assert_ret_val(NULL
!= pgovern
, NULL
);
316 return pgovern
->ruler_titles
;
319 /****************************************************************************
320 Add a new ruler title for the nation. Pass NULL for pnation for defining
322 ****************************************************************************/
324 government_ruler_title_new(struct government
*pgovern
,
325 const struct nation_type
*pnation
,
326 const char *ruler_male_title
,
327 const char *ruler_female_title
)
329 const char *domain
= NULL
;
330 struct ruler_title
*pruler_title
;
332 if (pnation
!= NULL
) {
333 domain
= pnation
->translation_domain
;
336 ruler_title_new(pnation
, domain
, ruler_male_title
, ruler_female_title
);
338 if (!ruler_title_check(pruler_title
)) {
339 ruler_title_destroy(pruler_title
);
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
),
349 nation_rule_name(pnation
), nation_number(pnation
));
351 log_error("Default ruler title for government \"%s\" (nb %d) "
352 "was set twice.", government_rule_name(pgovern
),
353 government_number(pgovern
));
360 /****************************************************************************
361 Return the nation of the rule title. Returns NULL if this is default.
362 ****************************************************************************/
363 const struct nation_type
*
364 ruler_title_nation(const struct ruler_title
*pruler_title
)
366 return pruler_title
->pnation
;
369 /****************************************************************************
370 Return the male rule title name.
371 ****************************************************************************/
373 ruler_title_male_untranslated_name(const struct ruler_title
*pruler_title
)
375 return untranslated_name(&pruler_title
->male
);
378 /****************************************************************************
379 Return the female rule title name.
380 ****************************************************************************/
382 ruler_title_female_untranslated_name(const struct ruler_title
*pruler_title
)
384 return untranslated_name(&pruler_title
->female
);
387 /****************************************************************************
388 Return the ruler title of the player (translated).
389 ****************************************************************************/
390 const char *ruler_title_for_player(const struct player
*pplayer
,
391 char *buf
, size_t buf_len
)
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
;
397 fc_assert_ret_val(NULL
!= buf
, NULL
);
398 fc_assert_ret_val(0 < buf_len
, NULL
);
400 /* Try specific nation rule title. */
401 if (!ruler_title_hash_lookup(pgovern
->ruler_titles
,
402 pnation
, &pruler_title
)
403 /* Try default rule 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
),
409 nation_rule_name(pnation
), nation_number(pnation
));
410 if (pplayer
->is_male
) {
411 fc_snprintf(buf
, buf_len
, _("Mr. %s"), player_name(pplayer
));
413 fc_snprintf(buf
, buf_len
, _("Ms. %s"), player_name(pplayer
));
416 fc_snprintf(buf
, buf_len
,
417 name_translation_get(pplayer
->is_male
418 ? &pruler_title
->male
419 : &pruler_title
->female
),
420 player_name(pplayer
));
427 /****************************************************************************
429 ****************************************************************************/
430 struct government_iter
{
431 struct iterator vtable
;
432 struct government
*p
, *end
;
434 #define GOVERNMENT_ITER(p) ((struct government_iter *) (p))
436 /****************************************************************************
437 Implementation of iterator 'sizeof' function.
438 ****************************************************************************/
439 size_t government_iter_sizeof(void)
441 return sizeof(struct government_iter
);
444 /****************************************************************************
445 Implementation of iterator 'next' function.
446 ****************************************************************************/
447 static void government_iter_next(struct iterator
*iter
)
449 GOVERNMENT_ITER(iter
)->p
++;
452 /****************************************************************************
453 Implementation of iterator 'get' function.
454 ****************************************************************************/
455 static void *government_iter_get(const struct iterator
*iter
)
457 return GOVERNMENT_ITER(iter
)->p
;
460 /****************************************************************************
461 Implementation of iterator 'valid' function.
462 ****************************************************************************/
463 static bool government_iter_valid(const struct iterator
*iter
)
465 struct government_iter
*it
= GOVERNMENT_ITER(iter
);
466 return it
->p
< it
->end
;
469 /****************************************************************************
470 Implementation of iterator 'init' function.
471 ****************************************************************************/
472 struct iterator
*government_iter_init(struct government_iter
*it
)
474 it
->vtable
.next
= government_iter_next
;
475 it
->vtable
.get
= government_iter_get
;
476 it
->vtable
.valid
= government_iter_valid
;
478 it
->end
= governments
+ government_count();
483 /****************************************************************************
484 Allocate resources associated with the given government.
485 ****************************************************************************/
486 static inline void government_init(struct government
*pgovern
)
488 memset(pgovern
, 0, sizeof(*pgovern
));
490 pgovern
->item_number
= pgovern
- governments
;
491 pgovern
->ruler_titles
=
492 ruler_title_hash_new_full(nation_hash_val
, nation_hash_comp
,
493 NULL
, NULL
, NULL
, ruler_title_destroy
);
494 requirement_vector_init(&pgovern
->reqs
);
495 pgovern
->changed_to_times
= 0;
496 pgovern
->disabled
= FALSE
;
499 /****************************************************************************
500 De-allocate resources associated with the given government.
501 ****************************************************************************/
502 static inline void government_free(struct government
*pgovern
)
504 ruler_title_hash_destroy(pgovern
->ruler_titles
);
505 pgovern
->ruler_titles
= NULL
;
507 if (NULL
!= pgovern
->helptext
) {
508 strvec_destroy(pgovern
->helptext
);
509 pgovern
->helptext
= NULL
;
512 requirement_vector_free(&pgovern
->reqs
);
515 /****************************************************************************
516 Allocate the governments.
517 ****************************************************************************/
518 void governments_alloc(int num
)
522 fc_assert(NULL
== governments
);
523 governments
= fc_malloc(sizeof(*governments
) * num
);
524 game
.control
.government_count
= num
;
526 for (i
= 0; i
< game
.control
.government_count
; i
++) {
527 government_init(governments
+ i
);
531 /****************************************************************************
532 De-allocate the currently allocated governments.
533 ****************************************************************************/
534 void governments_free(void)
538 if (NULL
== governments
) {
542 for (i
= 0; i
< game
.control
.government_count
; i
++) {
543 government_free(governments
+ i
);
548 game
.control
.government_count
= 0;
551 /****************************************************************************
552 Is it possible to start a revolution without specifying the target
553 government in the current game?
554 ****************************************************************************/
555 bool untargeted_revolution_allowed(void)
557 if (game
.info
.revolentype
== REVOLEN_QUICKENING
558 || game
.info
.revolentype
== REVOLEN_RANDQUICK
) {
559 /* We need to know the target government at the onset of the revolution
560 * in order to know how long anarchy will last. */