Freeciv-3.2
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/*******************************************************************/
39{
40 struct player *best = NULL;
43
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;
64 best = pplayer;
65 }
67
68 return best;
69}
70
71/*******************************************************************/
75int 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) {
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 }
99 dists += min_dist;
101
102 return MAX(dists / cities, 1);
103}
104
105/*******************************************************************/
108int 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 const struct civ_map *nmap = &(wld.map);
114
118 }
119
120 if (u != NULL) {
121 worth += utype_buy_gold_cost(pcity, u, 0); /* Cost of settler */
122 }
123 for (i = 1; i < city_size_get(pcity); i++) {
124 worth += city_granary_size(i); /* Cost of growing city */
125 }
127 worth += pcity->prod[o] * 10;
130 if (same_pos(unit_tile(punit), pcity->tile)) {
132
134 /* Obsolete, candidate for disbanding */
137 } else {
138 worth += unit_build_shield_cost(pcity, punit); /* Good stuff */
139 }
140 }
142 city_built_iterate(pcity, pimprove) {
143 if (improvement_obsolete(pplayer, pimprove, pcity)) {
144 worth += impr_sell_gold(pimprove); /* Obsolete, candidate for selling */
145 } else if (!is_wonder(pimprove)) {
146 /* Buy cost, with nonzero shield amount */
147 worth += impr_build_shield_cost(pcity, pimprove) * 2;
148 } else {
149 worth += impr_build_shield_cost(pcity, pimprove) * 4;
150 }
152 if (city_unhappy(pcity)) {
153 worth *= 0.75;
154 }
155
156 return worth;
157}
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define action_id_get_role(act_id)
Definition actions.h:696
int player_distance_to_player(struct player *pplayer, struct player *target)
Definition aisupport.c:75
int city_gold_worth(struct city *pcity)
Definition aisupport.c:108
struct player * player_leading_spacerace(void)
Definition aisupport.c:38
int city_granary_size(int city_size)
Definition city.c:2132
bool city_unhappy(const struct city *pcity)
Definition city.c:1626
bool can_city_build_unit_direct(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:903
#define city_list_iterate(citylist, pcity)
Definition city.h:508
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define output_type_iterate(output)
Definition city.h:845
#define city_owner(_pcity_)
Definition city.h:563
#define city_list_iterate_end
Definition city.h:510
#define city_built_iterate(_pcity, _p)
Definition city.h:834
#define city_built_iterate_end
Definition city.h:840
#define output_type_iterate_end
Definition city.h:851
char * incite_cost
Definition comments.c:75
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:74
@ VC_SPACERACE
Definition fc_types.h:1271
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
int impr_sell_gold(const struct impr_type *pimprove)
int impr_build_shield_cost(const struct city *pcity, const struct impr_type *pimprove)
bool is_wonder(const struct impr_type *pimprove)
bool improvement_obsolete(const struct player *pplayer, const struct impr_type *pimprove, const struct city *pcity)
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:940
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
struct city_list * cities
Definition packhand.c:119
static bool is_barbarian(const struct player *pplayer)
Definition player.h:489
#define players_iterate_alive_end
Definition player.h:547
#define players_iterate_alive(_pplayer)
Definition player.h:542
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
spaceship_state
Definition spaceship.h:84
@ SSHIP_LAUNCHED
Definition spaceship.h:85
@ SSHIP_NONE
Definition spaceship.h:84
Definition city.h:320
struct tile * tile
Definition city.h:322
int prod[O_LAST]
Definition city.h:358
struct unit_list * units_supported
Definition city.h:406
struct packet_scenario_info scenario
Definition game.h:87
struct city_list * cities
Definition player.h:279
bool is_alive
Definition player.h:266
const struct unit_type * obsoleted_by
Definition unittype.h:529
struct civ_map map
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:214
#define unit_tile(_pu)
Definition unit.h:397
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
int utype_buy_gold_cost(const struct city *pcity, const struct unit_type *punittype, int shields_in_stock)
Definition unittype.c:1492
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
Definition unittype.c:1476
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2298
bool victory_enabled(enum victory_condition_type victory)
Definition victory.c:26