Freeciv21
Develop your civilization from humble roots to a global empire
aisupport.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 GNU
5  ( oo ) General Public License as published by the Free Software
6  \__/ Foundation, either version 3 of the License, or (at your
7  option) any later version. You should have received
8  a copy of the GNU General Public License along with Freeciv21. If not,
9  see https://www.gnu.org/licenses/.
10  */
11 
12 // utility
13 #include "log.h"
14 #include "shared.h"
15 #include "support.h"
16 
17 // common
18 #include "city.h"
19 #include "game.h"
20 #include "map.h"
21 #include "player.h"
22 #include "spaceship.h"
23 #include "unitlist.h"
24 #include "victory.h"
25 
26 #include "aisupport.h"
27 
32 {
33  struct player *best = nullptr;
34  int best_arrival = FC_INFINITY;
35  enum spaceship_state best_state = SSHIP_NONE;
36 
38  return nullptr;
39  }
40 
41  players_iterate_alive(pplayer)
42  {
43  struct player_spaceship *ship = &pplayer->spaceship;
44  int arrival = static_cast<int>(ship->travel_time) + ship->launch_year;
45 
46  if (is_barbarian(pplayer) || ship->state == SSHIP_NONE) {
47  continue;
48  }
49 
50  if (ship->state != SSHIP_LAUNCHED && ship->state > best_state) {
51  best_state = ship->state;
52  best = pplayer;
53  } else if (ship->state == SSHIP_LAUNCHED && arrival < best_arrival) {
54  best_state = ship->state;
55  best_arrival = arrival;
56  best = pplayer;
57  }
58  }
60 
61  return best;
62 }
63 
68 int player_distance_to_player(struct player *pplayer, struct player *target)
69 {
70  int cities = 0;
71  int dists = 0;
72 
73  if (pplayer == target || !target->is_alive || !pplayer->is_alive
74  || city_list_size(pplayer->cities) == 0
75  || city_list_size(target->cities) == 0) {
76  return 1;
77  }
78 
79  // For all our cities, find the closest distance to an enemy city.
80  city_list_iterate(pplayer->cities, pcity)
81  {
82  int min_dist = FC_INFINITY;
83 
84  city_list_iterate(target->cities, c2)
85  {
86  int dist = real_map_distance(c2->tile, pcity->tile);
87 
88  if (min_dist > dist) {
89  min_dist = dist;
90  }
91  }
93  dists += min_dist;
94  cities++;
95  }
97 
98  return MAX(dists / qMax(cities, 1), 1);
99 }
100 
104 int city_gold_worth(struct city *pcity)
105 {
106  struct player *pplayer = city_owner(pcity);
107  int worth = 0, i;
108  struct unit_type *u = nullptr;
109 
110  if (!game.scenario.prevent_new_cities) {
112  action_id_get_role(ACTION_FOUND_CITY));
113  }
114 
115  if (u != nullptr) {
116  worth += utype_buy_gold_cost(pcity, u, 0); // cost of settler
117  }
118  for (i = 1; i < city_size_get(pcity); i++) {
119  worth += city_granary_size(i); // cost of growing city
120  }
121  output_type_iterate(o) { worth += pcity->prod[o] * 10; }
123  unit_list_iterate(pcity->units_supported, punit)
124  {
125  if (same_pos(unit_tile(punit), pcity->tile)) {
126  const struct unit_type *punittype = unit_type_get(punit)->obsoleted_by;
127 
128  if (punittype && can_city_build_unit_direct(pcity, punittype)) {
129  // obsolete, candidate for disbanding
130  worth += unit_shield_value(punit, unit_type_get(punit),
131  action_by_number(ACTION_RECYCLE_UNIT));
132  } else {
133  worth += unit_build_shield_cost(pcity, punit); // good stuff
134  }
135  }
136  }
138  city_built_iterate(pcity, pimprove)
139  {
140  if (improvement_obsolete(pplayer, pimprove, pcity)) {
141  worth += impr_sell_gold(pimprove); // obsolete, candidate for selling
142  } else if (!is_wonder(pimprove)) {
143  // Buy cost, with nonzero shield amount
144  worth += impr_build_shield_cost(pcity, pimprove) * 2;
145  } else {
146  worth += impr_build_shield_cost(pcity, pimprove) * 4;
147  }
148  }
150  if (city_unhappy(pcity)) {
151  worth *= 0.75;
152  }
153  return worth;
154 }
struct action * action_by_number(action_id act_id)
Return the action with the given id.
Definition: actions.cpp:1149
#define action_id_get_role(act_id)
Definition: actions.h:580
int player_distance_to_player(struct player *pplayer, struct player *target)
Calculate average distances to other players.
Definition: aisupport.cpp:68
int city_gold_worth(struct city *pcity)
Rough calculation of the worth of pcity in gold.
Definition: aisupport.cpp:104
struct player * player_leading_spacerace()
Find who is leading the space race.
Definition: aisupport.cpp:31
int city_granary_size(int city_size)
Generalized formula used to calculate granary size.
Definition: city.cpp:2034
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
Definition: city.cpp:1083
bool city_unhappy(const struct city *pcity)
Return TRUE iff the city is unhappy.
Definition: city.cpp:1544
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit, ignoring whether unit is obsolete.
Definition: city.cpp:860
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
#define city_list_iterate(citylist, pcity)
Definition: city.h:482
#define output_type_iterate(output)
Definition: city.h:764
#define city_list_iterate_end
Definition: city.h:484
#define city_built_iterate(_pcity, _p)
Definition: city.h:752
#define city_built_iterate_end
Definition: city.h:759
#define output_type_iterate_end
Definition: city.h:771
@ VC_SPACERACE
Definition: fc_types.h:1083
struct civ_game game
Definition: game.cpp:47
int impr_sell_gold(const struct impr_type *pimprove)
Returns the amount of gold received when this improvement is sold.
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
Returns the number of shields it takes to build this improvement.
bool is_wonder(const struct impr_type *pimprove)
Returns whether improvement is some kind of wonder.
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
Returns TRUE if the improvement or wonder is obsolete.
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
Definition: map.cpp:887
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
Definition: map.cpp:599
struct city_list * cities
Definition: packhand.cpp:122
static bool is_barbarian(const struct player *pplayer)
Definition: player.h:474
#define players_iterate_alive_end
Definition: player.h:532
#define players_iterate_alive(_pplayer)
Definition: player.h:526
#define FC_INFINITY
Definition: shared.h:32
#define MAX(x, y)
Definition: shared.h:48
spaceship_state
Definition: spaceship.h:77
@ SSHIP_LAUNCHED
Definition: spaceship.h:80
@ SSHIP_NONE
Definition: spaceship.h:78
Definition: city.h:291
struct tile * tile
Definition: city.h:293
int prod[O_LAST]
Definition: city.h:327
struct unit_list * units_supported
Definition: city.h:377
struct packet_scenario_info scenario
Definition: game.h:78
double travel_time
Definition: spaceship.h:113
enum spaceship_state state
Definition: spaceship.h:105
Definition: player.h:231
struct city_list * cities
Definition: player.h:263
bool is_alive
Definition: player.h:250
const struct unit_type * obsoleted_by
Definition: unittype.h:494
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Returns how many shields the unit (type) is worth.
Definition: unit.cpp:204
#define unit_tile(_pu)
Definition: unit.h:371
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
int utype_buy_gold_cost(const struct city *pcity, const struct unit_type *punittype, int shields_in_stock)
Returns the amount of gold it takes to rush this unit.
Definition: unittype.cpp:1193
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
Definition: unittype.cpp:114
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Returns the number of shields it takes to build this unit.
Definition: unittype.cpp:1176
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Return "best" unit the player can build, with given role/flag.
Definition: unittype.cpp:1950
bool victory_enabled(enum victory_condition_type victory)
Whether victory condition is enabled.
Definition: victory.cpp:20