Freeciv-3.3
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 "daicity.h"
50#include "daidata.h"
51#include "daiplayer.h"
52#include "daitools.h"
53#include "daiunit.h"
54
55#include "aiparatrooper.h"
56
57
58#define LOGLEVEL_PARATROOPER LOG_DEBUG
59
60/*************************************************************************/
63static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait,
64 struct unit *punit)
65{
66 int best = 0;
67 int val;
68 struct tile* best_tile = NULL;
70 struct city* acity;
71 struct player* pplayer = unit_owner(punit);
72 struct civ_map *nmap = &(wld.map);
73
74 /* First, we search for undefended cities in danger */
76 if (!map_is_known(ptile, pplayer)) {
77 continue;
78 }
79
80 acity = tile_city(ptile);
82 && unit_list_size(ptile->units) == 0) {
84 if (val > best) {
85 best = val;
86 best_tile = ptile;
87 }
88 }
90
91 if (best_tile != NULL) {
94 "Choose to jump in order to protect allied city %s (%d %d). "
95 "Benefit: %d",
97 return best_tile;
98 }
99
100 /* Second, we search for undefended enemy cities */
102 acity = tile_city(ptile);
104 && (unit_list_size(ptile->units) == 0)) {
105 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)
106 && has_handicap(pplayer, H_FOG)) {
107 continue;
108 }
109 /* Prefer big cities on other continents */
110 val = city_size_get(acity)
112 if (val > best) {
113 best = val;
114 best_tile = ptile;
115 }
116 }
118
119 if (best_tile != NULL) {
122 "Choose to jump into enemy city %s (%d %d). Benefit: %d",
124 return best_tile;
125 }
126
127 /* Jump to kill adjacent units */
129 struct terrain *pterrain = tile_terrain(ptile);
130 if (is_ocean(pterrain)) {
131 continue;
132 }
133 if (has_handicap(pplayer, H_FOG)) {
134 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
135 continue;
136 }
137 } else {
138 if (!map_is_known(ptile, pplayer)) {
139 continue;
140 }
141 }
142 acity = tile_city(ptile);
143 if (acity && !pplayers_allied(city_owner(acity), pplayer)) {
144 continue;
145 }
146 if (!acity && unit_list_size(ptile->units) > 0) {
147 continue;
148 }
149
150 /* Iterate over adjacent tile to find good victim */
151 adjc_iterate(nmap, ptile, target) {
152 if (unit_list_size(target->units) == 0
153 || !can_unit_attack_tile(punit, NULL, target)
154 || is_ocean_tile(target)
155 || (has_handicap(pplayer, H_FOG)
156 && !map_is_known_and_seen(target, pplayer, V_MAIN))) {
157 continue;
158 }
159 val = 0;
160 if (is_stack_vulnerable(target)) {
161 unit_list_iterate(target->units, victim) {
162 if ((!has_handicap(pplayer, H_FOG)
163 || can_player_see_unit(pplayer, victim))
165 victim, target)
166 == ATT_OK)) {
167 val += victim->hp * 100;
168 }
170 } else {
171 val += get_defender(nmap, punit, target, NULL)->hp * 100;
172 }
173 val *= unit_win_chance(nmap, punit,
174 get_defender(nmap, punit, target, NULL),
175 NULL);
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
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
241 /* Not relevant. */
242 continue;
243 }
244
245 if (has_handicap(pplayer, H_FOG)) {
249 ptile_dest, NULL));
250 } else {
253 }
254
255 if (possible) {
260 }
261
263 /* Finished or finished. */
264 break;
265 }
267
268 if (jump_performed) {
269 /* Successful! */
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) {
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, 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
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)
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)
362 } else {
363
366 }
368
369 total = adv_data_get(pplayer, NULL)->stats.units.paratroopers;
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 = research_get(pplayer);
387 int profit;
388 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
389
390 /* military_advisor_choose_build() does something idiotic,
391 * this function should not be called if there is danger... */
392 if (choice->want >= 100 && choice->type != CT_ATTACKER) {
393 return;
394 }
395
397 struct unit *virtual_unit;
398
401 continue;
402 }
403
404 if (!allow_gold_upkeep && utype_upkeep_cost(u_type, pplayer, O_GOLD) > 0) {
405 continue;
406 }
407
408 /* Temporary hack before we are smart enough to return such units */
410 && 1 == utype_fuel(u_type)) {
411 continue;
412 }
413
414 /* Assign tech for paratroopers */
416 /* We raise want if the required tech is not known */
418
420 plr_data->tech_want[tech_req] += 2;
421 log_base(LOGLEVEL_PARATROOPER, "Raising tech want in city %s for %s "
422 "stimulating %s with %d (" ADV_WANT_PRINTF ") and req",
423 city_name_get(pcity),
424 player_name(pplayer),
426 2,
427 plr_data->tech_want[tech_req]);
428
429 /* Now, we raise want for prerequisites */
432 plr_data->tech_want[k] += 1;
433 }
435 }
437
438 /* we only update choice struct if we can build it! */
439 if (!can_city_build_unit_now(pcity, u_type)) {
440 continue;
441 }
442
443 /* it's worth building that unit? */
445 pplayer, pcity, u_type,
449
450 /* update choice struct if it's worth */
451 if (profit > choice->want) {
452 /* Update choice */
453 choice->want = profit;
454 choice->value.utype = u_type;
455 choice->type = CT_ATTACKER;
456 choice->need_boat = FALSE;
457 adv_choice_set_use(choice, "paratrooper");
458 log_base(LOGLEVEL_PARATROOPER, "%s wants to build %s (want=%d)",
460 }
462
463 return;
464}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5044
struct act_prob action_prob_unit_vs_tgt(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:4739
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:3293
static struct action * action_by_number(action_id act_id)
Definition actions.h:390
#define action_has_result(_act_, _res_)
Definition actions.h:175
#define action_iterate_end
Definition actions.h:209
#define action_iterate(_act_)
Definition actions.h:205
#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:604
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)
const char * city_name_get(const struct city *pcity)
Definition city.c:1133
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:804
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:948
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define city_owner(_pcity_)
Definition city.h:563
double unit_win_chance(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, const struct action *paction)
Definition combat.c:480
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:992
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:123
struct unit * get_defender(const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile, const struct action *paction)
Definition combat.c:839
bool can_unit_attack_tile(const struct unit *punit, const struct action *paction, const struct tile *dest_tile)
Definition combat.c:312
@ ATT_OK
Definition combat.h:35
char * incite_cost
Definition comments.c:74
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition daiplayer.h:54
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition daiplayer.h:42
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition daitools.c:603
void dai_manage_military(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2504
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1635
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:600
#define RAMPAGE_ANYTHING
Definition daiunit.h:85
#define RAMPAGE_FREE_CITY_OR_BETTER
Definition daiunit.h:87
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
#define NO_TARGET
Definition fc_types.h:358
int Tech_type_id
Definition fc_types.h:381
#define ADV_WANT_PRINTF
Definition fc_types.h:1336
@ O_GOLD
Definition fc_types.h:101
struct world wld
Definition game.c:62
struct unit * game_unit_by_number(int id)
Definition game.c:115
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:636
#define adjc_iterate_end
Definition map.h:419
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:377
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:414
#define square_iterate_end
Definition map.h:380
int get_continent_size(Continent_id id)
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:901
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:927
const char * player_name(const struct player *pplayer)
Definition player.c:895
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1104
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
bool research_goal_tech_req(const struct research *presearch, Tech_type_id goal, Tech_type_id tech)
Definition research.c:807
struct research * research_get(const struct player *pplayer)
Definition research.c:128
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:619
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
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::@91::@94 units
struct adv_data::@91 stats
int paratroopers
Definition advdata.h:89
unsigned int urgency
Definition daicity.h:49
Definition ai.h:50
Definition city.h:320
struct tile * tile
Definition city.h:322
struct city_list * cities
Definition player.h:281
int defense_bonus
Definition terrain.h:93
Definition tile.h:50
int paratroopers_range
Definition unittype.h:548
Definition unit.h:140
int moves_left
Definition unit.h:152
int id
Definition unit.h:147
int hp
Definition unit.h:153
struct tile * tile
Definition unit.h:142
struct civ_map map
#define FALSE
Definition support.h:47
const char * advance_rule_name(const struct advance *padvance)
Definition tech.c:309
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
#define advance_index_iterate_end
Definition tech.h:249
tech_req
Definition tech.h:110
#define A_FIRST
Definition tech.h:44
#define advance_index_iterate(_start, _index)
Definition tech.h:245
#define is_ocean(pterrain)
Definition terrain.h:194
#define is_ocean_tile(ptile)
Definition terrain.h:196
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:89
#define tile_terrain(_tile)
Definition tile.h:111
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_continent(_tile)
Definition tile.h:93
const struct unit_type * utype
Definition fc_types.h:698
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1662
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:847
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1767
#define unit_tile(_pu)
Definition unit.h:397
#define unit_owner(_pu)
Definition unit.h:396
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:3337
#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:1229
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1578
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371
#define utype_fuel(ptype)
Definition unittype.h:843
#define unit_tech_reqs_iterate_end
Definition unittype.h:885
#define unit_tech_reqs_iterate(_utype_, _p)
Definition unittype.h:879
#define unit_type_iterate(_p)
Definition unittype.h:859
#define unit_type_iterate_end
Definition unittype.h:866