Freeciv21
Develop your civilization from humble roots to a global empire
disaster.cpp
Go to the documentation of this file.
1 /*
2 _ ._ Copyright (c) 1996-2021 Freeciv21 and Freeciv contributors.
3  \ | This file is part of Freeciv21. Freeciv21 is free software: you
4  \_| can redistribute it and/or modify it under the terms of the
5  .' '. GNU General Public License as published by the Free
6  :O O: Software Foundation, either version 3 of the License,
7  '/ \' or (at your option) any later version. You should have
8  :X: received a copy of the GNU General Public License along with
9  :X: Freeciv21. If not, see https://www.gnu.org/licenses/.
10  */
11 
12 // utility
13 #include "fcintl.h"
14 
15 // common
16 #include "fc_types.h"
17 #include "game.h"
18 #include "name_translation.h"
19 
20 #include "disaster.h"
21 
23 
28 {
29  int i;
30 
31  for (i = 0; i < ARRAY_SIZE(disaster_types); i++) {
32  disaster_types[i].id = i;
33  requirement_vector_init(&disaster_types[i].reqs);
34  }
35 }
36 
41 {
42  disaster_type_iterate(pdis) { requirement_vector_free(&pdis->reqs); }
44 }
45 
50 {
51  fc_assert_ret_val(nullptr != pdis, 0);
52 
53  return pdis->id;
54 }
55 
63 {
64  fc_assert_ret_val(nullptr != pdis, 0);
65 
66  return pdis - disaster_types;
67 }
68 
73 {
74  fc_assert_ret_val(id >= 0 && id < game.control.num_disaster_types,
75  nullptr);
76 
77  return &disaster_types[id];
78 }
79 
83 const char *disaster_name_translation(struct disaster_type *pdis)
84 {
85  return name_translation_get(&pdis->name);
86 }
87 
91 const char *disaster_rule_name(struct disaster_type *pdis)
92 {
93  return rule_name_get(&pdis->name);
94 }
95 
99 bool disaster_has_effect(const struct disaster_type *pdis,
100  enum disaster_effect_id effect)
101 {
102  return BV_ISSET(pdis->effects, effect);
103 }
104 
108 bool can_disaster_happen(const struct disaster_type *pdis,
109  const struct city *pcity)
110 {
111  return are_reqs_active(city_owner(pcity), nullptr, pcity, nullptr,
112  city_tile(pcity), nullptr, nullptr, nullptr,
113  nullptr, nullptr, &pdis->reqs, RPT_POSSIBLE);
114 }
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
struct disaster_type * disaster_by_number(Disaster_type_id id)
Return disaster type of given id.
Definition: disaster.cpp:72
bool can_disaster_happen(const struct disaster_type *pdis, const struct city *pcity)
Whether disaster can happen in given city.
Definition: disaster.cpp:108
const char * disaster_rule_name(struct disaster_type *pdis)
Return untranslated name of this disaster type.
Definition: disaster.cpp:91
static struct disaster_type disaster_types[MAX_DISASTER_TYPES]
Definition: disaster.cpp:22
Disaster_type_id disaster_number(const struct disaster_type *pdis)
Return the disaster id.
Definition: disaster.cpp:49
Disaster_type_id disaster_index(const struct disaster_type *pdis)
Return the disaster index.
Definition: disaster.cpp:62
bool disaster_has_effect(const struct disaster_type *pdis, enum disaster_effect_id effect)
Check if disaster provides effect.
Definition: disaster.cpp:99
void disaster_types_init()
Initialize disaster_type structures.
Definition: disaster.cpp:27
void disaster_types_free()
Free the memory associated with disaster types.
Definition: disaster.cpp:40
const char * disaster_name_translation(struct disaster_type *pdis)
Return translated name of this disaster type.
Definition: disaster.cpp:83
#define disaster_type_iterate(_p)
Definition: disaster.h:70
#define disaster_type_iterate_end
Definition: disaster.h:76
struct @19::@20 reqs
#define MAX_DISASTER_TYPES
Definition: fc_types.h:46
@ RPT_POSSIBLE
Definition: fc_types.h:567
int Disaster_type_id
Definition: fc_types.h:302
struct civ_game game
Definition: game.cpp:47
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
bool are_reqs_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement_vector *reqs, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement(s) to see if they are active on the given target.
#define ARRAY_SIZE(x)
Definition: shared.h:79
Definition: city.h:291
struct packet_ruleset_control control
Definition: game.h:74
struct name_translation name
Definition: disaster.h:40
struct requirement_vector reqs
Definition: disaster.h:42
bv_disaster_effects effects
Definition: disaster.h:49