Freeciv21
Develop your civilization from humble roots to a global empire
base.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 // common
13 #include "extras.h"
14 #include "game.h"
15 #include "map.h"
16 #include "tile.h"
17 #include "unit.h"
18 
19 #include "base.h"
20 
24 bool base_has_flag(const struct base_type *pbase, enum base_flag_id flag)
25 {
26  return BV_ISSET(pbase->flags, flag);
27 }
28 
33 bool is_base_flag_card_near(const struct tile *ptile, enum base_flag_id flag)
34 {
35  extra_type_by_cause_iterate(EC_BASE, pextra)
36  {
37  if (base_has_flag(extra_base_get(pextra), flag)) {
38  cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile)
39  {
40  if (tile_has_extra(adjc_tile, pextra)) {
41  return true;
42  }
43  }
45  }
46  }
48 
49  return false;
50 }
51 
56 bool is_base_flag_near_tile(const struct tile *ptile, enum base_flag_id flag)
57 {
58  extra_type_by_cause_iterate(EC_BASE, pextra)
59  {
60  if (base_has_flag(extra_base_get(pextra), flag)) {
61  adjc_iterate(&(wld.map), ptile, adjc_tile)
62  {
63  if (tile_has_extra(adjc_tile, pextra)) {
64  return true;
65  }
66  }
68  }
69  }
71 
72  return false;
73 }
74 
78 bool base_flag_is_retired(enum base_flag_id flag)
79 {
80  Q_UNUSED(flag)
81  /* No new flags retired in 3.1. Flags that were retired in 3.0 are already
82  * completely removed. */
83  return false;
84 }
85 
90 bool base_has_flag_for_utype(const struct base_type *pbase,
91  enum base_flag_id flag,
92  const struct unit_type *punittype)
93 {
94  return base_has_flag(pbase, flag)
95  && is_native_extra_to_utype(base_extra_get(pbase), punittype);
96 }
97 
101 bool can_build_base(const struct unit *punit, const struct base_type *pbase,
102  const struct tile *ptile)
103 {
104  struct extra_type *pextra = base_extra_get(pbase);
105  struct player *pplayer = unit_owner(punit);
106 
107  if (!can_build_extra_base(pextra, pplayer, ptile)) {
108  return false;
109  }
110 
111  return are_reqs_active(pplayer, tile_owner(ptile), nullptr, nullptr, ptile,
112  punit, unit_type_get(punit), nullptr, nullptr,
113  nullptr, &pextra->reqs, RPT_CERTAIN);
114 }
115 
120 {
121  struct extra_type_list *bases;
122 
123  bases = extra_type_list_by_cause(EC_BASE);
124 
125  if (bases == nullptr || id < 0 || id >= extra_type_list_size(bases)) {
126  return nullptr;
127  }
128 
129  return extra_base_get(extra_type_list_get(bases, id));
130 }
131 
135 Base_type_id base_number(const struct base_type *pbase)
136 {
137  fc_assert_ret_val(nullptr != pbase, 0);
138  return pbase->item_number;
139 }
140 
144 struct extra_type *base_extra_get(const struct base_type *pbase)
145 {
146  return pbase->self;
147 }
148 
152 Base_type_id base_count() { return game.control.num_base_types; }
153 
157 void base_type_init(struct extra_type *pextra, int idx)
158 {
159  auto *pbase = new base_type;
160 
161  pextra->data.base = pbase;
162 
163  pbase->item_number = idx;
164  pbase->self = pextra;
165 }
166 
171 
175 struct base_type *get_base_by_gui_type(enum base_gui_type type,
176  const struct unit *punit,
177  const struct tile *ptile)
178 {
179  extra_type_by_cause_iterate(EC_BASE, pextra)
180  {
181  struct base_type *pbase = extra_base_get(pextra);
182 
183  if (type == pbase->gui_type
184  && (punit == nullptr || can_build_base(punit, pbase, ptile))) {
185  return pbase;
186  }
187  }
189 
190  return nullptr;
191 }
192 
196 bool territory_claiming_base(const struct base_type *pbase)
197 {
198  return pbase->border_sq >= 0;
199 }
bool base_flag_is_retired(enum base_flag_id flag)
Returns TRUE iff the given flag is retired.
Definition: base.cpp:78
Base_type_id base_number(const struct base_type *pbase)
Return the base index.
Definition: base.cpp:135
void base_type_init(struct extra_type *pextra, int idx)
Initialize base_type structures.
Definition: base.cpp:157
bool base_has_flag(const struct base_type *pbase, enum base_flag_id flag)
Check if base provides effect.
Definition: base.cpp:24
bool is_base_flag_card_near(const struct tile *ptile, enum base_flag_id flag)
Returns TRUE iff any cardinally adjacent tile contains a base with the given flag (does not check pti...
Definition: base.cpp:33
void base_types_free()
Free the memory associated with base types.
Definition: base.cpp:170
struct extra_type * base_extra_get(const struct base_type *pbase)
Return extra that base is.
Definition: base.cpp:144
bool base_has_flag_for_utype(const struct base_type *pbase, enum base_flag_id flag, const struct unit_type *punittype)
Base provides base flag for unit? Checks if base provides flag and if base is native to unit.
Definition: base.cpp:90
struct base_type * base_by_number(const Base_type_id id)
Returns base_type entry for an ID value.
Definition: base.cpp:119
bool can_build_base(const struct unit *punit, const struct base_type *pbase, const struct tile *ptile)
Can unit build base to given tile?
Definition: base.cpp:101
Base_type_id base_count()
Return the number of base_types.
Definition: base.cpp:152
struct base_type * get_base_by_gui_type(enum base_gui_type type, const struct unit *punit, const struct tile *ptile)
Get best gui_type base for given parameters.
Definition: base.cpp:175
bool is_base_flag_near_tile(const struct tile *ptile, enum base_flag_id flag)
Returns TRUE iff any adjacent tile contains a base with the given flag (does not check ptile itself)
Definition: base.cpp:56
bool territory_claiming_base(const struct base_type *pbase)
Does this base type claim territory?
Definition: base.cpp:196
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
char * bases
Definition: comments.cpp:35
bool can_build_extra_base(const struct extra_type *pextra, const struct player *pplayer, const struct tile *ptile)
Tells if player can build extra to tile with suitable unit.
Definition: extras.cpp:371
bool is_native_extra_to_utype(const struct extra_type *pextra, const struct unit_type *punittype)
Is extra native to unit type?
Definition: extras.cpp:770
struct extra_type_list * extra_type_list_by_cause(enum extra_cause cause)
Returns extra type for given cause.
Definition: extras.cpp:224
#define extra_base_get(_e_)
Definition: extras.h:170
#define extra_type_by_cause_iterate_end
Definition: extras.h:307
#define extra_type_by_cause_iterate(_cause, _extra)
Definition: extras.h:299
@ RPT_CERTAIN
Definition: fc_types.h:568
int Base_type_id
Definition: fc_types.h:300
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define adjc_iterate_end
Definition: map.h:358
#define cardinal_adjc_iterate_end
Definition: map.h:384
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition: map.h:351
#define cardinal_adjc_iterate(nmap, center_tile, itr_tile)
Definition: map.h:380
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.
Definition: base.h:43
int border_sq
Definition: base.h:46
enum base_gui_type gui_type
Definition: base.h:45
bv_base_flags flags
Definition: base.h:51
struct extra_type * self
Definition: base.h:53
Base_type_id item_number
Definition: base.h:44
struct packet_ruleset_control control
Definition: game.h:74
struct extra_type::@22 data
struct requirement_vector reqs
Definition: extras.h:90
struct base_type * base
Definition: extras.h:134
Definition: player.h:231
Definition: tile.h:42
Definition: unit.h:134
struct civ_map map
Definition: world_object.h:21
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
#define tile_owner(_tile)
Definition: tile.h:78
#define unit_owner(_pu)
Definition: unit.h:370
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114