1 /****************************************************************************
2  Freeciv - Copyright (C) 2004 - The Freeciv Team
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 
21 /* common */
22 #include "fc_types.h"
23 #include "game.h"
24 #include "name_translation.h"
25 
26 #include "disaster.h"
27 
28 static struct disaster_type disaster_types[MAX_DISASTER_TYPES];
29 
30 /****************************************************************************
31   Initialize disaster_type structures.
32 ****************************************************************************/
disaster_types_init(void)33 void disaster_types_init(void)
34 {
35   int i;
36 
37   for (i = 0; i < ARRAY_SIZE(disaster_types); i++) {
38     disaster_types[i].id = i;
39     requirement_vector_init(&disaster_types[i].reqs);
40   }
41 }
42 
43 /****************************************************************************
44   Free the memory associated with disaster types
45 ****************************************************************************/
disaster_types_free(void)46 void disaster_types_free(void)
47 {
48   disaster_type_iterate(pdis) {
49     requirement_vector_free(&pdis->reqs);
50   } disaster_type_iterate_end;
51 }
52 
53 /**************************************************************************
54   Return the disaster id.
55 **************************************************************************/
disaster_number(const struct disaster_type * pdis)56 Disaster_type_id disaster_number(const struct disaster_type *pdis)
57 {
58   fc_assert_ret_val(NULL != pdis, -1);
59 
60   return pdis->id;
61 }
62 
63 /**************************************************************************
64   Return the disaster index.
65 
66   Currently same as disaster_number(), paired with disaster_count()
67   indicates use as an array index.
68 **************************************************************************/
disaster_index(const struct disaster_type * pdis)69 Disaster_type_id disaster_index(const struct disaster_type *pdis)
70 {
71   fc_assert_ret_val(NULL != pdis, -1);
72 
73   return pdis - disaster_types;
74 }
75 
76 /**************************************************************************
77   Return the number of disaster_types.
78 **************************************************************************/
disaster_count(void)79 Disaster_type_id disaster_count(void)
80 {
81   return game.control.num_disaster_types;
82 }
83 
84 /****************************************************************************
85   Return disaster type of given id.
86 ****************************************************************************/
disaster_by_number(Disaster_type_id id)87 struct disaster_type *disaster_by_number(Disaster_type_id id)
88 {
89   fc_assert_ret_val(id >= 0 && id < game.control.num_disaster_types, NULL);
90 
91   return &disaster_types[id];
92 }
93 
94 /****************************************************************************
95   Return translated name of this disaster type.
96 ****************************************************************************/
disaster_name_translation(struct disaster_type * pdis)97 const char *disaster_name_translation(struct disaster_type *pdis)
98 {
99   return name_translation_get(&pdis->name);
100 }
101 
102 /****************************************************************************
103   Return untranslated name of this disaster type.
104 ****************************************************************************/
disaster_rule_name(struct disaster_type * pdis)105 const char *disaster_rule_name(struct disaster_type *pdis)
106 {
107   return rule_name_get(&pdis->name);
108 }
109 
110 /****************************************************************************
111   Check if disaster provides effect
112 ****************************************************************************/
disaster_has_effect(const struct disaster_type * pdis,enum disaster_effect_id effect)113 bool disaster_has_effect(const struct disaster_type *pdis,
114                          enum disaster_effect_id effect)
115 {
116   return BV_ISSET(pdis->effects, effect);
117 }
118 
119 /****************************************************************************
120   Whether disaster can happen in given city.
121 ****************************************************************************/
can_disaster_happen(const struct disaster_type * pdis,const struct city * pcity)122 bool can_disaster_happen(const struct disaster_type *pdis,
123                          const struct city *pcity)
124 {
125   return are_reqs_active(city_owner(pcity), NULL, pcity, NULL,
126                          city_tile(pcity), NULL, NULL, NULL, NULL,
127                          &pdis->reqs, RPT_POSSIBLE);
128 }
129