File: government.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (563 lines) | stat: -rw-r--r-- 20,548 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/***********************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "fcintl.h"
#include "log.h"
#include "mem.h"
#include "shared.h"
#include "string_vector.h"
#include "support.h"

/* common */
#include "game.h"
#include "player.h"
#include "tech.h"

#include "government.h"

struct government *governments = NULL;

/****************************************************************************
  Returns the government that has the given (translated) name.
  Returns NULL if none match.
****************************************************************************/
struct government *government_by_translated_name(const char *name)
{
  governments_iterate(gov) {
    if (0 == strcmp(government_name_translation(gov), name)) {
      return gov;
    }
  } governments_iterate_end;

  return NULL;
}

/****************************************************************************
  Returns the government that has the given (untranslated) rule name.
  Returns NULL if none match.
****************************************************************************/
struct government *government_by_rule_name(const char *name)
{
  const char *qname = Qn_(name);

  governments_iterate(gov) {
    if (0 == fc_strcasecmp(government_rule_name(gov), qname)) {
      return gov;
    }
  } governments_iterate_end;

  return NULL;
}

/**************************************************************************
  Return the number of governments.
**************************************************************************/
Government_type_id government_count(void)
{
  return game.control.government_count;
}

/**************************************************************************
  Return the government index.

  Currently same as government_number(), paired with government_count()
  indicates use as an array index.
**************************************************************************/
Government_type_id government_index(const struct government *pgovern)
{
  fc_assert_ret_val(NULL != pgovern, -1);
  return pgovern - governments;
}

/**************************************************************************
  Return the government index.
**************************************************************************/
Government_type_id government_number(const struct government *pgovern)
{
  fc_assert_ret_val(NULL != pgovern, -1);
  return pgovern->item_number;
}

/****************************************************************************
  Return the government with the given index.

  This function returns NULL for an out-of-range index (some callers
  rely on this).
****************************************************************************/
struct government *government_by_number(const Government_type_id gov)
{
  if (gov < 0 || gov >= game.control.government_count) {
    return NULL;
  }
  return &governments[gov];
}

/****************************************************************************
  Return the government of a player.
****************************************************************************/
struct government *government_of_player(const struct player *pplayer)
{
  fc_assert_ret_val(NULL != pplayer, NULL);
  return pplayer->government;
}

/****************************************************************************
  Return the government of the player who owns the city.
****************************************************************************/
struct government *government_of_city(const struct city *pcity)
{
  fc_assert_ret_val(NULL != pcity, NULL);
  return government_of_player(city_owner(pcity));
}

/****************************************************************************
  Return the (untranslated) rule name of the government.
  You don't have to free the return pointer.
****************************************************************************/
const char *government_rule_name(const struct government *pgovern)
{
  fc_assert_ret_val(NULL != pgovern, NULL);
  return rule_name_get(&pgovern->name);
}

/****************************************************************************
  Return the (translated) name of the given government. 
  You don't have to free the return pointer.
****************************************************************************/
const char *government_name_translation(const struct government *pgovern)
{
  fc_assert_ret_val(NULL != pgovern, NULL);

  return name_translation_get(&pgovern->name);
}

/****************************************************************************
  Return the (translated) name of the given government of a player. 
  You don't have to free the return pointer.
****************************************************************************/
const char *government_name_for_player(const struct player *pplayer)
{
  return government_name_translation(government_of_player(pplayer));
}

/***************************************************************
  Can change to government if appropriate tech exists, and one of:
   - no required tech (required is A_NONE)
   - player has required tech
   - we have an appropriate wonder
  Returns FALSE if pplayer is NULL (used for observers).
***************************************************************/
bool can_change_to_government(struct player *pplayer,
                              const struct government *gov)
{
  fc_assert_ret_val(NULL != gov, FALSE);

  if (!pplayer) {
    return FALSE;
  }

  if (get_player_bonus(pplayer, EFT_ANY_GOVERNMENT) > 0) {
    /* Note, this may allow govs that are on someone else's "tech tree". */
    return TRUE;
  }

  return are_reqs_active(pplayer, NULL, NULL, NULL, NULL, NULL, NULL,
                         NULL, NULL, &gov->reqs, RPT_CERTAIN);
}


/****************************************************************************
  Ruler titles.
****************************************************************************/
struct ruler_title {
  const struct nation_type *pnation;
  struct name_translation male;
  struct name_translation female;
};

/****************************************************************************
  Hash function.
****************************************************************************/
static genhash_val_t nation_hash_val(const struct nation_type *pnation)
{
  return NULL != pnation ? nation_number(pnation) : nation_count();
}

/****************************************************************************
  Hash function.
****************************************************************************/
static bool nation_hash_comp(const struct nation_type *pnation1,
                             const struct nation_type *pnation2)
{
  return pnation1 == pnation2;
}

/****************************************************************************
  Create a new ruler title.
****************************************************************************/
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)
{
  struct ruler_title *pruler_title = fc_malloc(sizeof(*pruler_title));

  pruler_title->pnation = pnation;
  name_set(&pruler_title->male, domain, ruler_male_title);
  name_set(&pruler_title->female, domain, ruler_female_title);

  return pruler_title;
}

/****************************************************************************
  Free a ruler title.
****************************************************************************/
static void ruler_title_destroy(struct ruler_title *pruler_title)
{
  free(pruler_title);
}

/****************************************************************************
  Return TRUE if the ruler title is valid.
****************************************************************************/
static bool ruler_title_check(const struct ruler_title *pruler_title)
{
  bool ret = TRUE;

  if (!formats_match(rule_name_get(&pruler_title->male), "%s")) {
    if (NULL != pruler_title->pnation) {
      log_error("\"%s\" male ruler title for nation \"%s\" (nb %d) "
                "is not a format. It should match \"%%s\"",
                rule_name_get(&pruler_title->male),
                nation_rule_name(pruler_title->pnation),
                nation_number(pruler_title->pnation));
    } else {
      log_error("\"%s\" male ruler title is not a format. "
                "It should match \"%%s\"",
                rule_name_get(&pruler_title->male));
    }
    ret = FALSE;
  }

  if (!formats_match(rule_name_get(&pruler_title->female), "%s")) {
    if (NULL != pruler_title->pnation) {
      log_error("\"%s\" female ruler title for nation \"%s\" (nb %d) "
                "is not a format. It should match \"%%s\"",
                rule_name_get(&pruler_title->female),
                nation_rule_name(pruler_title->pnation),
                nation_number(pruler_title->pnation));
    } else {
      log_error("\"%s\" female ruler title is not a format. "
                "It should match \"%%s\"",
                rule_name_get(&pruler_title->female));
    }
    ret = FALSE;
  }

  if (!formats_match(name_translation_get(&pruler_title->male), "%s")) {
    if (NULL != pruler_title->pnation) {
      log_error("Translation of \"%s\" male ruler title for nation \"%s\" "
                "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
                rule_name_get(&pruler_title->male),
                nation_rule_name(pruler_title->pnation),
                nation_number(pruler_title->pnation),
                name_translation_get(&pruler_title->male));
    } else {
      log_error("Translation of \"%s\" male ruler title is not a format "
                "(\"%s\"). It should match \"%%s\"",
                rule_name_get(&pruler_title->male),
                name_translation_get(&pruler_title->male));
    }
    ret = FALSE;
  }

  if (!formats_match(name_translation_get(&pruler_title->female), "%s")) {
    if (NULL != pruler_title->pnation) {
      log_error("Translation of \"%s\" female ruler title for nation \"%s\" "
                "(nb %d) is not a format (\"%s\"). It should match \"%%s\"",
                rule_name_get(&pruler_title->female),
                nation_rule_name(pruler_title->pnation),
                nation_number(pruler_title->pnation),
                name_translation_get(&pruler_title->female));
    } else {
      log_error("Translation of \"%s\" female ruler title is not a format "
                "(\"%s\"). It should match \"%%s\"",
                rule_name_get(&pruler_title->female),
                name_translation_get(&pruler_title->female));
    }
    ret = FALSE;
  }

  return ret;
}

/****************************************************************************
  Returns all ruler titles for a government type.
****************************************************************************/
const struct ruler_title_hash *
government_ruler_titles(const struct government *pgovern)
{
  fc_assert_ret_val(NULL != pgovern, NULL);
  return pgovern->ruler_titles;
}

/****************************************************************************
  Add a new ruler title for the nation. Pass NULL for pnation for defining
  the default title.
****************************************************************************/
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)
{
  const char *domain = NULL;
  struct ruler_title *pruler_title;

  if (pnation != NULL) {
    domain = pnation->translation_domain;
  }
  pruler_title =
    ruler_title_new(pnation, domain, ruler_male_title, ruler_female_title);

  if (!ruler_title_check(pruler_title)) {
    ruler_title_destroy(pruler_title);
    return NULL;
  }

  if (ruler_title_hash_replace(pgovern->ruler_titles,
                               pnation, pruler_title)) {
    if (NULL != pnation) {
      log_error("Ruler title for government \"%s\" (nb %d) and "
                "nation \"%s\" (nb %d) was set twice.",
                government_rule_name(pgovern), government_number(pgovern),
                nation_rule_name(pnation), nation_number(pnation));
    } else {
      log_error("Default ruler title for government \"%s\" (nb %d) "
                "was set twice.", government_rule_name(pgovern),
                government_number(pgovern));
    }
  }

  return pruler_title;
}

/****************************************************************************
  Return the nation of the rule title. Returns NULL if this is default.
****************************************************************************/
const struct nation_type *
ruler_title_nation(const struct ruler_title *pruler_title)
{
  return pruler_title->pnation;
}

/****************************************************************************
  Return the male rule title name.
****************************************************************************/
const char *
ruler_title_male_untranslated_name(const struct ruler_title *pruler_title)
{
  return untranslated_name(&pruler_title->male);
}

/****************************************************************************
  Return the female rule title name.
****************************************************************************/
const char *
ruler_title_female_untranslated_name(const struct ruler_title *pruler_title)
{
  return untranslated_name(&pruler_title->female);
}

/****************************************************************************
  Return the ruler title of the player (translated).
****************************************************************************/
const char *ruler_title_for_player(const struct player *pplayer,
                                   char *buf, size_t buf_len)
{
  const struct government *pgovern = government_of_player(pplayer);
  const struct nation_type *pnation = nation_of_player(pplayer);
  struct ruler_title *pruler_title;

  fc_assert_ret_val(NULL != buf, NULL);
  fc_assert_ret_val(0 < buf_len, NULL);

  /* Try specific nation rule title. */
  if (!ruler_title_hash_lookup(pgovern->ruler_titles,
                               pnation, &pruler_title)
      /* Try default rule title. */
      && !ruler_title_hash_lookup(pgovern->ruler_titles,
                                  NULL, &pruler_title)) {
    log_error("Missing title for government \"%s\" (nb %d) "
              "nation \"%s\" (nb %d).",
              government_rule_name(pgovern), government_number(pgovern),
              nation_rule_name(pnation), nation_number(pnation));
    if (pplayer->is_male) {
      fc_snprintf(buf, buf_len, _("Mr. %s"), player_name(pplayer));
    } else {
      fc_snprintf(buf, buf_len, _("Ms. %s"), player_name(pplayer));
    }
  } else {
    fc_snprintf(buf, buf_len,
                name_translation_get(pplayer->is_male
                                     ? &pruler_title->male
                                     : &pruler_title->female),
                player_name(pplayer));
  }

  return buf;
}


/****************************************************************************
  Government iterator.
****************************************************************************/
struct government_iter {
  struct iterator vtable;
  struct government *p, *end;
};
#define GOVERNMENT_ITER(p) ((struct government_iter *) (p))

/****************************************************************************
  Implementation of iterator 'sizeof' function.
****************************************************************************/
size_t government_iter_sizeof(void)
{
  return sizeof(struct government_iter);
}

/****************************************************************************
  Implementation of iterator 'next' function.
****************************************************************************/
static void government_iter_next(struct iterator *iter)
{
  GOVERNMENT_ITER(iter)->p++;
}

/****************************************************************************
  Implementation of iterator 'get' function.
****************************************************************************/
static void *government_iter_get(const struct iterator *iter)
{
  return GOVERNMENT_ITER(iter)->p;
}

/****************************************************************************
  Implementation of iterator 'valid' function.
****************************************************************************/
static bool government_iter_valid(const struct iterator *iter)
{
  struct government_iter *it = GOVERNMENT_ITER(iter);
  return it->p < it->end;
}

/****************************************************************************
  Implementation of iterator 'init' function.
****************************************************************************/
struct iterator *government_iter_init(struct government_iter *it)
{
  it->vtable.next = government_iter_next;
  it->vtable.get = government_iter_get;
  it->vtable.valid = government_iter_valid;
  it->p = governments;
  it->end = governments + government_count();
  return ITERATOR(it);
}


/****************************************************************************
  Allocate resources associated with the given government.
****************************************************************************/
static inline void government_init(struct government *pgovern)
{
  memset(pgovern, 0, sizeof(*pgovern));

  pgovern->item_number = pgovern - governments;
  pgovern->ruler_titles =
      ruler_title_hash_new_full(nation_hash_val, nation_hash_comp,
                                NULL, NULL, NULL, ruler_title_destroy);
  requirement_vector_init(&pgovern->reqs);
  pgovern->changed_to_times = 0;
}

/****************************************************************************
  De-allocate resources associated with the given government.
****************************************************************************/
static inline void government_free(struct government *pgovern)
{
  ruler_title_hash_destroy(pgovern->ruler_titles);
  pgovern->ruler_titles = NULL;

  if (NULL != pgovern->helptext) {
    strvec_destroy(pgovern->helptext);
    pgovern->helptext = NULL;
  }

  requirement_vector_free(&pgovern->reqs);
}

/****************************************************************************
  Allocate the governments.
****************************************************************************/
void governments_alloc(int num)
{
  int i;

  fc_assert(NULL == governments);
  governments = fc_malloc(sizeof(*governments) * num);
  game.control.government_count = num;

  for (i = 0; i < game.control.government_count; i++) {
    government_init(governments + i);
  }
}

/****************************************************************************
  De-allocate the currently allocated governments.
****************************************************************************/
void governments_free(void)
{
  int i;

  if (NULL == governments) {
    return;
  }

  for (i = 0; i < game.control.government_count; i++) {
    government_free(governments + i);
  }

  free(governments);
  governments = NULL;
  game.control.government_count = 0;
}

/****************************************************************************
  Is it possible to start a revolution without specifying the target
  government in the current game?
****************************************************************************/
bool untargeted_revolution_allowed(void)
{
  if (game.info.revolentype == REVOLEN_QUICKENING
      || game.info.revolentype == REVOLEN_RANDQUICK) {
    /* We need to know the target government at the onset of the revolution
     * in order to know how long anarchy will last. */
    return FALSE;
  }
  return TRUE;
}