Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/aicore/aisupport.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
aisupport.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2003 - The Freeciv Project
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 "log.h"
20#include "shared.h"
21#include "support.h"
22
23/* common */
24#include "city.h"
25#include "game.h"
26#include "map.h"
27#include "player.h"
28#include "spaceship.h"
29#include "tech.h"
30#include "unitlist.h"
31#include "victory.h"
32
33#include "aisupport.h"
34
35/*******************************************************************/
38 struct player *player_leading_spacerace(void)
39{
40 struct player *best = NULL;
41 int best_arrival = FC_INFINITY;
42 enum spaceship_state best_state = SSHIP_NONE;
43
44 if (!victory_enabled(VC_SPACERACE)) {
45 return NULL;
46 }
47
48 players_iterate_alive(pplayer) {
49 struct player_spaceship *ship = &pplayer->spaceship;
50 int arrival = (int) ship->travel_time + ship->launch_year;
51
52 if (is_barbarian(pplayer) || ship->state == SSHIP_NONE) {
53 continue;
54 }
55
56 if (ship->state != SSHIP_LAUNCHED
57 && ship->state > best_state) {
58 best_state = ship->state;
59 best = pplayer;
60 } else if (ship->state == SSHIP_LAUNCHED
61 && arrival < best_arrival) {
62 best_state = ship->state;
63 best_arrival = arrival;
64 best = pplayer;
65 }
66 } players_iterate_alive_end;
67
68 return best;
69}
70
71/*******************************************************************/
75 int player_distance_to_player(struct player *pplayer, struct player *target)
76{
77 int cities = city_list_size(pplayer->cities);
78 int dists = 0;
79
80 if (pplayer == target
81 || !target->is_alive
82 || !pplayer->is_alive
83 || cities == 0
84 || city_list_size(target->cities) == 0) {
85 return 1;
86 }
87
88 /* For all our cities, find the closest distance to an enemy city. */
89 city_list_iterate(pplayer->cities, pcity) {
90 int min_dist = FC_INFINITY;
91
92 city_list_iterate(target->cities, c2) {
93 int dist = real_map_distance(c2->tile, pcity->tile);
94
95 if (min_dist > dist) {
96 min_dist = dist;
97 }
98 } city_list_iterate_end;
99 dists += min_dist;
100 } city_list_iterate_end;
101
102 return MAX(dists / cities, 1);
103}
104
105/*******************************************************************/
108 int city_gold_worth(struct city *pcity)
109{
110 struct player *pplayer = city_owner(pcity);
111 int worth = 0, i;
112 struct unit_type *u = NULL;
113
114 if (!game.scenario.prevent_new_cities) {
115 u = best_role_unit_for_player(city_owner(pcity),
116 action_id_get_role(ACTION_FOUND_CITY));
117 }
118
119 if (u != NULL) {
120 worth += utype_buy_gold_cost(pcity, u, 0); /* cost of settler */
121 }
122 for (i = 1; i < city_size_get(pcity); i++) {
123 worth += city_granary_size(i); /* cost of growing city */
124 }
125 output_type_iterate(o) {
126 worth += pcity->prod[o] * 10;
127 } output_type_iterate_end;
128 unit_list_iterate(pcity->units_supported, punit) {
129 if (same_pos(unit_tile(punit), pcity->tile)) {
130 const struct unit_type *punittype = unit_type_get(punit)->obsoleted_by;
131
132 if (punittype && can_city_build_unit_direct(pcity, punittype)) {
133 /* obsolete, candidate for disbanding */
134 worth += unit_shield_value(punit, unit_type_get(punit),
135 action_by_number(ACTION_DISBAND_UNIT_RECOVER));
136 } else {
137 worth += unit_build_shield_cost(pcity, punit); /* good stuff */
138 }
139 }
140 } unit_list_iterate_end;
141 city_built_iterate(pcity, pimprove) {
142 if (improvement_obsolete(pplayer, pimprove, pcity)) {
143 worth += impr_sell_gold(pimprove); /* obsolete, candidate for selling */
144 } else if (!is_wonder(pimprove)) {
145 /* Buy cost, with nonzero shield amount */
146 worth += impr_build_shield_cost(pcity, pimprove) * 2;
147 } else {
148 worth += impr_build_shield_cost(pcity, pimprove) * 4;
149 }
150 } city_built_iterate_end;
151 if (city_unhappy(pcity)) {
152 worth *= 0.75;
153 }
154 return worth;
155}
action_by_number
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
action_id_get_role
#define action_id_get_role(act_id)
Definition actions.h:708
player_distance_to_player
int player_distance_to_player(struct player *pplayer, struct player *target)
Definition aisupport.c:75
city_gold_worth
int city_gold_worth(struct city *pcity)
Definition aisupport.c:108
player_leading_spacerace
struct player * player_leading_spacerace(void)
Definition aisupport.c:38
aisupport.h
city_granary_size
int city_granary_size(int city_size)
Definition city.c:2095
city_unhappy
bool city_unhappy(const struct city *pcity)
Definition city.c:1591
can_city_build_unit_direct
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:887
city.h
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:488
city_size_get
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
output_type_iterate
#define output_type_iterate(output)
Definition city.h:809
city_owner
#define city_owner(_pcity_)
Definition city.h:543
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:490
city_built_iterate
#define city_built_iterate(_pcity, _p)
Definition city.h:798
city_built_iterate_end
#define city_built_iterate_end
Definition city.h:804
output_type_iterate_end
#define output_type_iterate_end
Definition city.h:815
punit
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
VC_SPACERACE
@ VC_SPACERACE
Definition fc_types.h:1123
game
struct civ_game game
Definition game.c:57
game.h
impr_sell_gold
int impr_sell_gold(const struct impr_type *pimprove)
Definition improvement.c:332
impr_build_shield_cost
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
Definition improvement.c:292
is_wonder
bool is_wonder(const struct impr_type *pimprove)
Definition improvement.c:341
improvement_obsolete
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
Definition improvement.c:409
log.h
same_pos
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
real_map_distance
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:628
map.h
cities
struct city_list * cities
Definition packhand.c:117
player.h
is_barbarian
static bool is_barbarian(const struct player *pplayer)
Definition player.h:488
players_iterate_alive_end
#define players_iterate_alive_end
Definition player.h:545
players_iterate_alive
#define players_iterate_alive(_pplayer)
Definition player.h:540
shared.h
FC_INFINITY
#define FC_INFINITY
Definition shared.h:36
MAX
#define MAX(x, y)
Definition shared.h:54
spaceship.h
spaceship_state
spaceship_state
Definition spaceship.h:84
SSHIP_LAUNCHED
@ SSHIP_LAUNCHED
Definition spaceship.h:85
SSHIP_NONE
@ SSHIP_NONE
Definition spaceship.h:84
city
Definition city.h:309
city::tile
struct tile * tile
Definition city.h:311
city::prod
int prod[O_LAST]
Definition city.h:346
city::units_supported
struct unit_list * units_supported
Definition city.h:391
civ_game::scenario
struct packet_scenario_info scenario
Definition game.h:87
packet_scenario_info::prevent_new_cities
bool prevent_new_cities
Definition packets_gen.h:1611
player_spaceship
Definition spaceship.h:94
player_spaceship::travel_time
double travel_time
Definition spaceship.h:116
player_spaceship::state
enum spaceship_state state
Definition spaceship.h:108
player_spaceship::launch_year
int launch_year
Definition spaceship.h:109
player
Definition player.h:249
player::cities
struct city_list * cities
Definition player.h:281
player::is_alive
bool is_alive
Definition player.h:268
unit_type
Definition unittype.h:481
unit_type::obsoleted_by
const struct unit_type * obsoleted_by
Definition unittype.h:510
support.h
tech.h
unit_shield_value
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:219
unit_tile
#define unit_tile(_pu)
Definition unit.h:388
unitlist.h
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33
utype_buy_gold_cost
int utype_buy_gold_cost(const struct city *pcity, const struct unit_type *punittype, int shields_in_stock)
Definition unittype.c:1544
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
unit_build_shield_cost
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Definition unittype.c:1528
best_role_unit_for_player
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2345
victory_enabled
bool victory_enabled(enum victory_condition_type victory)
Definition victory.c:26
victory.h