Freeciv21
Develop your civilization from humble roots to a global empire
borders.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  /\/\ part of Freeciv21. Freeciv21 is free software: you can
4  \_\ _..._ redistribute it and/or modify it under the terms of the
5  (" )(_..._) GNU General Public License as published by the Free
6  ^^ // \\ Software Foundation, either version 3 of the License,
7  or (at your option) any later version. You should have
8 received a copy of the GNU General Public License along with Freeciv21.
9  If not, see https://www.gnu.org/licenses/.
10  */
11 
12 // utility
13 #include "fcintl.h"
14 #include "log.h"
15 
16 // common
17 #include "game.h"
18 #include "map.h"
19 #include "tile.h"
20 #include "unit.h"
21 
22 #include "borders.h"
23 
28 {
29  struct city *pcity;
30  int radius_sq = 0;
31 
32  if (BORDERS_DISABLED == game.info.borders) {
33  return 0;
34  }
35 
36  pcity = tile_city(ptile);
37 
38  if (pcity) {
39  radius_sq = game.info.border_city_radius_sq;
40  /* Limit the addition due to the city size. A city size of 60 or more is
41  * possible with a city radius of 5 (radius_sq = 26). */
42  radius_sq += MIN(city_size_get(pcity), CITY_MAP_MAX_RADIUS_SQ)
43  * game.info.border_size_effect;
44  } else {
45  extra_type_by_cause_iterate(EC_BASE, pextra)
46  {
47  struct base_type *pbase = extra_base_get(pextra);
48 
49  if (tile_has_extra(ptile, pextra) && territory_claiming_base(pbase)) {
50  radius_sq = pbase->border_sq;
51  break;
52  }
53  }
55  }
56 
57  return radius_sq;
58 }
59 
64 {
65  struct city *pcity;
66  int strength = 0;
67 
68  if (BORDERS_DISABLED == game.info.borders) {
69  return 0;
70  }
71 
72  pcity = tile_city(ptile);
73 
74  if (pcity) {
75  strength = city_size_get(pcity) + 2;
76  } else {
77  extra_type_by_cause_iterate(EC_BASE, pextra)
78  {
79  struct base_type *pbase = extra_base_get(pextra);
80 
81  if (tile_has_extra(ptile, pextra) && territory_claiming_base(pbase)) {
82  strength = 1;
83  break;
84  }
85  }
87  }
88 
89  return strength;
90 }
91 
95 int tile_border_strength(struct tile *ptile, struct tile *source)
96 {
97  int full_strength = tile_border_source_strength(source);
98  int sq_dist = sq_map_distance(ptile, source);
99 
100  if (sq_dist > 0) {
101  return full_strength * full_strength / sq_dist;
102  } else {
103  return FC_INFINITY;
104  }
105 }
106 
110 bool is_border_source(struct tile *ptile)
111 {
112  if (tile_city(ptile)) {
113  return true;
114  }
115 
116  if (extra_owner(ptile) != nullptr) {
117  extra_type_by_cause_iterate(EC_BASE, pextra)
118  {
119  struct base_type *pbase = extra_base_get(pextra);
120 
121  if (tile_has_extra(ptile, pextra) && territory_claiming_base(pbase)) {
122  return true;
123  }
124  }
126  }
127 
128  return false;
129 }
bool territory_claiming_base(const struct base_type *pbase)
Does this base type claim territory?
Definition: base.cpp:196
int tile_border_source_radius_sq(struct tile *ptile)
Border radius sq from given border source tile.
Definition: borders.cpp:27
bool is_border_source(struct tile *ptile)
Is given tile source to borders.
Definition: borders.cpp:110
int tile_border_strength(struct tile *ptile, struct tile *source)
Border source strength at tile.
Definition: borders.cpp:95
int tile_border_source_strength(struct tile *ptile)
Border source strength.
Definition: borders.cpp:63
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
#define CITY_MAP_MAX_RADIUS_SQ
Definition: city.h:59
struct player * extra_owner(const struct tile *ptile)
Who owns extras on tile.
Definition: extras.cpp:1013
#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
@ BORDERS_DISABLED
Definition: fc_types.h:863
struct civ_game game
Definition: game.cpp:47
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Return squared distance between two tiles.
Definition: map.cpp:610
#define MIN(x, y)
Definition: shared.h:49
#define FC_INFINITY
Definition: shared.h:32
Definition: base.h:43
int border_sq
Definition: base.h:46
Definition: city.h:291
struct packet_game_info info
Definition: game.h:80
Definition: tile.h:42
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130