Freeciv-3.1
Loading...
Searching...
No Matches
aiparatrooper.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2005 The Freeciv Team
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
21/* common */
22#include "city.h"
23#include "game.h"
24#include "player.h"
25#include "research.h"
26#include "unit.h"
27#include "unitlist.h"
28
29/* common/aicore */
30#include "pf_tools.h"
31
32/* server */
33#include "citytools.h"
34#include "maphand.h"
35#include "srv_log.h"
36#include "unithand.h"
37#include "unittools.h"
38
39/* server/advisors */
40#include "advdata.h"
41
42/* server/generator */
43#include "mapgen_utils.h"
44
45/* ai */
46#include "handicaps.h"
47
48/* ai/default */
49#include "aidata.h"
50#include "ailog.h"
51#include "aiplayer.h"
52#include "aiunit.h"
53#include "aitools.h"
54#include "daicity.h"
55
56#include "aiparatrooper.h"
57
58
59#define LOGLEVEL_PARATROOPER LOG_DEBUG
60
61/*************************************************************************/
64static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait,
65 struct unit *punit)
66{
67 int best = 0;
68 int val;
69 struct tile* best_tile = NULL;
71 struct city* acity;
72 struct player* pplayer = unit_owner(punit);
73 struct civ_map *nmap = &(wld.map);
74
75 /* First, we search for undefended cities in danger */
76 square_iterate(nmap, unit_tile(punit), range, ptile) {
77 if (!map_is_known(ptile, pplayer)) {
78 continue;
79 }
80
81 acity = tile_city(ptile);
82 if (acity && city_owner(acity) == unit_owner(punit)
83 && unit_list_size(ptile->units) == 0) {
84 val = city_size_get(acity) * def_ai_city_data(acity, ait)->urgency;
85 if (val > best) {
86 best = val;
87 best_tile = ptile;
88 }
89 }
91
92 if (best_tile != NULL) {
93 acity = tile_city(best_tile);
95 "Choose to jump in order to protect allied city %s (%d %d). "
96 "Benefit: %d",
97 city_name_get(acity), TILE_XY(best_tile), best);
98 return best_tile;
99 }
100
101 /* Second, we search for undefended enemy cities */
102 square_iterate(nmap, unit_tile(punit), range, ptile) {
103 acity = tile_city(ptile);
104 if (acity && pplayers_at_war(unit_owner(punit), city_owner(acity))
105 && (unit_list_size(ptile->units) == 0)) {
106 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)
107 && has_handicap(pplayer, H_FOG)) {
108 continue;
109 }
110 /* Prefer big cities on other continents */
111 val = city_size_get(acity)
113 if (val > best) {
114 best = val;
115 best_tile = ptile;
116 }
117 }
119
120 if (best_tile != NULL) {
121 acity = tile_city(best_tile);
123 "Choose to jump into enemy city %s (%d %d). Benefit: %d",
124 city_name_get(acity), TILE_XY(best_tile), best);
125 return best_tile;
126 }
127
128 /* Jump to kill adjacent units */
129 square_iterate(nmap, unit_tile(punit), range, ptile) {
130 struct terrain *pterrain = tile_terrain(ptile);
131 if (is_ocean(pterrain)) {
132 continue;
133 }
134 if (has_handicap(pplayer, H_FOG)) {
135 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
136 continue;
137 }
138 } else {
139 if (!map_is_known(ptile, pplayer)) {
140 continue;
141 }
142 }
143 acity = tile_city(ptile);
144 if (acity && !pplayers_allied(city_owner(acity), pplayer)) {
145 continue;
146 }
147 if (!acity && unit_list_size(ptile->units) > 0) {
148 continue;
149 }
150
151 /* Iterate over adjacent tile to find good victim */
152 adjc_iterate(nmap, ptile, target) {
153 if (unit_list_size(target->units) == 0
154 || !can_unit_attack_tile(punit, NULL, target)
155 || is_ocean_tile(target)
156 || (has_handicap(pplayer, H_FOG)
157 && !map_is_known_and_seen(target, pplayer, V_MAIN))) {
158 continue;
159 }
160 val = 0;
161 if (is_stack_vulnerable(target)) {
162 unit_list_iterate(target->units, victim) {
163 if ((!has_handicap(pplayer, H_FOG)
164 || can_player_see_unit(pplayer, victim))
166 victim, target)
167 == ATT_OK)) {
168 val += victim->hp * 100;
169 }
171 } else {
172 val += get_defender(nmap, punit, target)->hp * 100;
173 }
174 val *= unit_win_chance(nmap, punit,
175 get_defender(nmap, punit, target));
176 val += pterrain->defense_bonus / 10;
177 val -= punit->hp * 100;
178
179 if (val > best) {
180 best = val;
181 best_tile = ptile;
182 }
185
186 if (best_tile != NULL) {
187 UNIT_LOG(LOG_DEBUG, punit, "Choose to jump at (%d, %d) to attack "
188 "adjacent units. Benefit: %d",
189 TILE_XY(best_tile), best);
190 }
191
192 return best_tile;
193}
194
195/*************************************************************************/
198void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer,
199 struct unit *punit)
200{
201 struct city *pcity = tile_city(unit_tile(punit));
202 struct tile *ptile_dest = NULL;
203 const struct civ_map *nmap = &(wld.map);
204 int sanity = punit->id;
205
206 /* Defend attacking (and be opportunistic too) */
209 /* Dead */
210 return;
211 }
212
213 /* Check to recover hit points */
214 if ((punit->hp < unit_type_get(punit)->hp / 2) && pcity) {
215 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Recovering hit points.");
216 return;
217 }
218
219 /* Nothing to do! */
220 if (punit->moves_left == 0) {
221 return;
222 }
223
224 if (pcity && unit_list_size(unit_tile(punit)->units) == 1) {
225 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Defending the city.");
226 return;
227 }
228
229 if (can_unit_paradrop(nmap, punit)) {
230 ptile_dest = find_best_tile_to_paradrop_to(ait, punit);
231
232 if (ptile_dest) {
233 bool jump_performed = FALSE;
234
235 action_iterate(act_id) {
236 struct action *paction = action_by_number(act_id);
237 bool possible;
238
239 if (!(action_has_result(paction, ACTRES_PARADROP)
240 || action_has_result(paction, ACTRES_PARADROP_CONQUER))) {
241 /* Not relevant. */
242 continue;
243 }
244
245 if (has_handicap(pplayer, H_FOG)) {
246 possible = action_prob_possible(
247 action_prob_unit_vs_tgt(nmap, paction, punit,
248 tile_city(ptile_dest), NULL,
249 ptile_dest, NULL));
250 } else {
251 possible = is_action_enabled_unit_on_tile(nmap, paction->id, punit,
252 ptile_dest, NULL);
253 }
254
255 if (possible) {
256 jump_performed
258 tile_index(ptile_dest), NO_TARGET, "",
259 paction->id, ACT_REQ_PLAYER);
260 }
261
262 if (jump_performed || NULL == game_unit_by_number(sanity)) {
263 /* Finished or finished. */
264 break;
265 }
267
268 if (jump_performed) {
269 /* Successful! */
270 if (NULL == game_unit_by_number(sanity)) {
271 /* The unit did not survive the move */
272 return;
273 }
274 /* We attack the target */
277 }
278 }
279 } else {
280 /* We can't paradrop :-( */
281 struct city *acity = NULL;
282
283 /* We are in a city, so don't try to find another */
284 if (pcity) {
286 "waiting in a city for next turn.");
287 return;
288 }
289
290 /* Find a city to go to recover and paradrop from */
292
293 if (acity) {
294 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Going to %s", city_name_get(acity));
295 if (!dai_unit_goto(ait, punit, acity->tile)) {
296 /* Died or unsuccessful move */
297 return;
298 }
299 } else {
301 "didn't find city to go and recover.");
302 dai_manage_military(ait, nmap, pplayer, punit);
303 }
304 }
305}
306
307/*************************************************************************/
312 struct tile *ptile_city)
313{
314 const struct unit_type* u_type = unit_type_get(punit);
315 int range = u_type->paratroopers_range;
316 int profit = 0;
317 struct player* pplayer = unit_owner(punit);
318 int total, total_cities;
319 struct civ_map *nmap = &(wld.map);
320
321 profit += u_type->defense_strength
322 + u_type->move_rate
323 + u_type->attack_strength;
324
325 square_iterate(nmap, ptile_city, range, ptile) {
326 int multiplier;
327 struct city *pcity = tile_city(ptile);
328
329 if (!pcity) {
330 continue;
331 }
332
333 if (!map_is_known(ptile, pplayer)) {
334 continue;
335 }
336
337 /* We prefer jumping to other continents. On the same continent we
338 * can fight traditionally.
339 * FIXME: Handle ocean cities we can attack. */
340 if (!is_ocean_tile(ptile)
341 && tile_continent(ptile_city) != tile_continent(ptile)) {
342 if (get_continent_size(tile_continent(ptile)) < 3) {
343 /* Tiny island are hard to conquer with traditional units */
344 multiplier = 10;
345 } else {
346 multiplier = 5;
347 }
348 } else {
349 multiplier = 1;
350 }
351
352 /* There are lots of units, the city will be safe against paratroopers. */
353 if (unit_list_size(ptile->units) > 2) {
354 continue;
355 }
356
357 /* Prefer long jumps.
358 * If a city is near we can take/protect it with normal units */
359 if (pplayers_allied(pplayer, city_owner(pcity))) {
360 profit += city_size_get(pcity)
361 * multiplier * real_map_distance(ptile_city, ptile) / 2;
362 } else {
363
364 profit += city_size_get(pcity) * multiplier
365 * real_map_distance(ptile_city, ptile);
366 }
368
369 total = adv_data_get(pplayer, NULL)->stats.units.paratroopers;
370 total_cities = city_list_size(pplayer->cities);
371
372 if (total > total_cities) {
373 profit = profit * total_cities / total;
374 }
375
376 return profit;
377}
378
379/*************************************************************************/
383 struct player *pplayer, struct city *pcity,
384 struct adv_choice *choice, bool allow_gold_upkeep)
385{
386 const struct research *presearch;
387 int profit;
389 Tech_type_id requirements[A_LAST];
390 int num_requirements = 0;
391 int i;
392 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
393 const struct civ_map *nmap = &(wld.map);
394
395 /* military_advisor_choose_build() does something idiotic,
396 * this function should not be called if there is danger... */
397 if (choice->want >= 100 && choice->type != CT_ATTACKER) {
398 return;
399 }
400
401 unit_type_iterate(u_type) {
402 struct unit *virtual_unit;
403
404 if (!utype_can_do_action(u_type, ACTION_PARADROP)
405 && !utype_can_do_action(u_type, ACTION_PARADROP_CONQUER)) {
406 continue;
407 }
408 if (A_NEVER == u_type->require_advance) {
409 continue;
410 }
411
412 if (!allow_gold_upkeep && utype_upkeep_cost(u_type, pplayer, O_GOLD) > 0) {
413 continue;
414 }
415
416 /* Temporary hack before we are smart enough to return such units */
417 if (!utype_is_consumed_by_action_result(ACTRES_ATTACK, u_type)
418 && 1 == utype_fuel(u_type)) {
419 continue;
420 }
421
422 /* Assign tech for paratroopers */
423 tech_req = advance_index(u_type->require_advance);
424 if (tech_req != A_NONE && tech_req != A_UNSET) {
425 for (i = 0; i < num_requirements; i++) {
426 if (requirements[i] == tech_req) {
427 break;
428 }
429 }
430 if (i == num_requirements) {
431 requirements[num_requirements++] = tech_req;
432 }
433 }
434
435 /* we only update choice struct if we can build it! */
436 if (!can_city_build_unit_now(nmap, pcity, u_type)) {
437 continue;
438 }
439
440 /* it's worth building that unit? */
441 virtual_unit = unit_virtual_create(
442 pplayer, pcity, u_type,
444 profit = calculate_want_for_paratrooper(virtual_unit, pcity->tile);
445 unit_virtual_destroy(virtual_unit);
446
447 /* update choice struct if it's worth */
448 if (profit > choice->want) {
449 /* Update choice */
450 choice->want = profit;
451 choice->value.utype = u_type;
452 choice->type = CT_ATTACKER;
453 choice->need_boat = FALSE;
454 adv_choice_set_use(choice, "paratrooper");
455 log_base(LOGLEVEL_PARATROOPER, "%s wants to build %s (want=%d)",
456 city_name_get(pcity), utype_rule_name(u_type), profit);
457 }
459
460 /* we raise want if the required tech is not known */
461 presearch = research_get(pplayer);
462 for (i = 0; i < num_requirements; i++) {
463 tech_req = requirements[i];
464 plr_data->tech_want[tech_req] += 2;
465 log_base(LOGLEVEL_PARATROOPER, "Raising tech want in city %s for %s "
466 "stimulating %s with %d (" ADV_WANT_PRINTF ") and req",
467 city_name_get(pcity),
468 player_name(pplayer),
470 2,
471 plr_data->tech_want[tech_req]);
472
473 /* now, we raise want for prerequisites */
475 if (research_goal_tech_req(presearch, tech_req, k)) {
476 plr_data->tech_want[k] += 1;
477 }
479 }
480 return;
481}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:6703
bool is_action_enabled_unit_on_tile(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:4996
struct act_prob action_prob_unit_vs_tgt(const struct civ_map *nmap, const struct action *paction, const struct unit *act_unit, const struct city *tgt_city, const struct unit *tgt_unit, const struct tile *tgt_tile, const struct extra_type *extra_tgt)
Definition actions.c:6399
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
#define action_has_result(_act_, _res_)
Definition actions.h:448
#define action_iterate_end
Definition actions.h:472
#define action_iterate(_act_)
Definition actions.h:467
#define adv_choice_set_use(_choice, _use)
Definition advchoice.h:85
@ CT_ATTACKER
Definition advchoice.h:40
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:593
static struct tile * find_best_tile_to_paradrop_to(struct ai_type *ait, struct unit *punit)
void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer, struct unit *punit)
void dai_choose_paratrooper(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
#define LOGLEVEL_PARATROOPER
static int calculate_want_for_paratrooper(struct unit *punit, struct tile *ptile_city)
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition aiplayer.h:54
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition aiplayer.h:42
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition aitools.c:606
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition aiunit.c:2496
struct city * find_nearest_safe_city(struct unit *punit)
Definition aiunit.c:1629
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition aiunit.c:600
#define RAMPAGE_ANYTHING
Definition aiunit.h:85
#define RAMPAGE_FREE_CITY_OR_BETTER
Definition aiunit.h:87
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:789
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:927
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
#define city_owner(_pcity_)
Definition city.h:543
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:930
enum unit_attack_result unit_attack_unit_at_tile_result(const struct unit *punit, const struct action *paction, const struct unit *pdefender, const struct tile *dest_tile)
Definition combat.c:122
double unit_win_chance(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender)
Definition combat.c:438
bool can_unit_attack_tile(const struct unit *punit, const struct action *paction, const struct tile *dest_tile)
Definition combat.c:271
struct unit * get_defender(const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile)
Definition combat.c:783
@ ATT_OK
Definition combat.h:35
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
#define NO_TARGET
Definition fc_types.h:324
int Tech_type_id
Definition fc_types.h:347
#define ADV_WANT_PRINTF
Definition fc_types.h:1207
@ O_GOLD
Definition fc_types.h:91
struct world wld
Definition game.c:58
struct unit * game_unit_by_number(int id)
Definition game.c:111
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_FOG
Definition handicaps.h:26
#define log_base(level, message,...)
Definition log.h:94
@ LOG_DEBUG
Definition log.h:34
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:628
#define adjc_iterate_end
Definition map.h:427
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:385
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:422
#define square_iterate_end
Definition map.h:388
int get_continent_size(Continent_id id)
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:886
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:900
const char * player_name(const struct player *pplayer)
Definition player.c:886
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1084
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1364
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
bool research_goal_tech_req(const struct research *presearch, Tech_type_id goal, Tech_type_id tech)
Definition research.c:797
struct research * research_get(const struct player *pplayer)
Definition research.c:126
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
action_id id
Definition actions.h:380
enum choice_type type
Definition advchoice.h:46
adv_want want
Definition advchoice.h:48
universals_u value
Definition advchoice.h:47
bool need_boat
Definition advchoice.h:49
struct adv_data::@92 stats
struct adv_data::@92::@95 units
int paratroopers
Definition advdata.h:83
unsigned int urgency
Definition daicity.h:49
adv_want tech_want[A_LAST+1]
Definition aidata.h:103
Definition ai.h:50
Definition city.h:309
struct tile * tile
Definition city.h:311
struct city_list * cities
Definition player.h:281
int defense_bonus
Definition terrain.h:193
Definition tile.h:49
int defense_strength
Definition unittype.h:496
int paratroopers_range
Definition unittype.h:522
int move_rate
Definition unittype.h:497
int attack_strength
Definition unittype.h:495
Definition unit.h:138
int moves_left
Definition unit.h:150
int id
Definition unit.h:145
int hp
Definition unit.h:151
struct civ_map map
#define FALSE
Definition support.h:47
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:107
const char * advance_rule_name(const struct advance *padvance)
Definition tech.c:299
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
#define A_NEVER
Definition tech.h:51
#define advance_index_iterate_end
Definition tech.h:248
tech_req
Definition tech.h:110
#define A_FIRST
Definition tech.h:44
#define A_NONE
Definition tech.h:43
#define A_UNSET
Definition tech.h:48
#define A_LAST
Definition tech.h:45
#define advance_index_iterate(_start, _index)
Definition tech.h:244
#define is_ocean(pterrain)
Definition terrain.h:287
#define is_ocean_tile(ptile)
Definition terrain.h:289
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:87
#define tile_terrain(_tile)
Definition tile.h:109
#define TILE_XY(ptile)
Definition tile.h:42
#define tile_continent(_tile)
Definition tile.h:91
const struct unit_type * utype
Definition fc_types.h:604
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1617
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:826
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1713
#define unit_tile(_pu)
Definition unit.h:395
#define unit_owner(_pu)
Definition unit.h:394
bool unit_perform_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id_incoming, const char *name, const action_id action_type, const enum action_requester requester)
Definition unithand.c:3175
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Definition unittype.c:132
bool utype_is_consumed_by_action_result(enum action_result result, const struct unit_type *utype)
Definition unittype.c:1281
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
#define utype_fuel(ptype)
Definition unittype.h:825
#define unit_type_iterate(_p)
Definition unittype.h:841
#define unit_type_iterate_end
Definition unittype.h:848