Freeciv-3.2
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 "aitools.h"
50#include "daicity.h"
51#include "daidata.h"
52#include "daiplayer.h"
53
54#include "aiparatrooper.h"
55
56
57#define LOGLEVEL_PARATROOPER LOG_DEBUG
58
59/*************************************************************************/
62static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait,
63 struct unit *punit)
64{
65 int best = 0;
66 int val;
67 struct tile* best_tile = NULL;
69 struct city* acity;
70 struct player* pplayer = unit_owner(punit);
71 struct civ_map *nmap = &(wld.map);
72
73 /* First, we search for undefended cities in danger */
75 if (!map_is_known(ptile, pplayer)) {
76 continue;
77 }
78
79 acity = tile_city(ptile);
81 && unit_list_size(ptile->units) == 0) {
83 if (val > best) {
84 best = val;
85 best_tile = ptile;
86 }
87 }
89
90 if (best_tile != NULL) {
93 "Choose to jump in order to protect allied city %s (%d %d). "
94 "Benefit: %d",
96 return best_tile;
97 }
98
99 /* Second, we search for undefended enemy cities */
101 acity = tile_city(ptile);
103 && (unit_list_size(ptile->units) == 0)) {
104 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)
105 && has_handicap(pplayer, H_FOG)) {
106 continue;
107 }
108 /* Prefer big cities on other continents */
109 val = city_size_get(acity)
111 if (val > best) {
112 best = val;
113 best_tile = ptile;
114 }
115 }
117
118 if (best_tile != NULL) {
121 "Choose to jump into enemy city %s (%d %d). Benefit: %d",
123 return best_tile;
124 }
125
126 /* Jump to kill adjacent units */
128 struct terrain *pterrain = tile_terrain(ptile);
129 if (is_ocean(pterrain)) {
130 continue;
131 }
132 if (has_handicap(pplayer, H_FOG)) {
133 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
134 continue;
135 }
136 } else {
137 if (!map_is_known(ptile, pplayer)) {
138 continue;
139 }
140 }
141 acity = tile_city(ptile);
142 if (acity && !pplayers_allied(city_owner(acity), pplayer)) {
143 continue;
144 }
145 if (!acity && unit_list_size(ptile->units) > 0) {
146 continue;
147 }
148
149 /* Iterate over adjacent tile to find good victim */
150 adjc_iterate(nmap, ptile, target) {
151 if (unit_list_size(target->units) == 0
152 || !can_unit_attack_tile(punit, NULL, target)
153 || is_ocean_tile(target)
154 || (has_handicap(pplayer, H_FOG)
155 && !map_is_known_and_seen(target, pplayer, V_MAIN))) {
156 continue;
157 }
158 val = 0;
159 if (is_stack_vulnerable(target)) {
160 unit_list_iterate(target->units, victim) {
161 if ((!has_handicap(pplayer, H_FOG)
162 || can_player_see_unit(pplayer, victim))
164 victim, target)
165 == ATT_OK)) {
166 val += victim->hp * 100;
167 }
169 } else {
170 val += get_defender(nmap, punit, target, NULL)->hp * 100;
171 }
172 val *= unit_win_chance(nmap, punit,
173 get_defender(nmap, punit, target, NULL),
174 NULL);
175 val += pterrain->defense_bonus / 10;
176 val -= punit->hp * 100;
177
178 if (val > best) {
179 best = val;
180 best_tile = ptile;
181 }
184
185 if (best_tile != NULL) {
186 UNIT_LOG(LOG_DEBUG, punit, "Choose to jump at (%d, %d) to attack "
187 "adjacent units. Benefit: %d",
188 TILE_XY(best_tile), best);
189 }
190
191 return best_tile;
192}
193
194/*************************************************************************/
197void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer,
198 struct unit *punit)
199{
200 struct city *pcity = tile_city(unit_tile(punit));
201 struct tile *ptile_dest = NULL;
202 const struct civ_map *nmap = &(wld.map);
203 int sanity = punit->id;
204
205 /* Defend attacking (and be opportunistic too) */
208 /* Dead */
209 return;
210 }
211
212 /* Check to recover hit points */
213 if ((punit->hp < unit_type_get(punit)->hp / 2) && pcity) {
214 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Recovering hit points.");
215 return;
216 }
217
218 /* Nothing to do! */
219 if (punit->moves_left == 0) {
220 return;
221 }
222
223 if (pcity && unit_list_size(unit_tile(punit)->units) == 1) {
224 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Defending the city.");
225 return;
226 }
227
230
231 if (ptile_dest) {
232 bool jump_performed = FALSE;
233
234 action_iterate(act_id) {
235 struct action *paction = action_by_number(act_id);
236 bool possible;
237
240 /* Not relevant. */
241 continue;
242 }
243
244 if (has_handicap(pplayer, H_FOG)) {
248 ptile_dest, NULL));
249 } else {
252 }
253
254 if (possible) {
259 }
260
262 /* Finished or finished. */
263 break;
264 }
266
267 if (jump_performed) {
268 /* Successful! */
270 /* The unit did not survive the move */
271 return;
272 }
273 /* We attack the target */
276 }
277 }
278 } else {
279 /* We can't paradrop :-( */
280 struct city *acity = NULL;
281
282 /* We are in a city, so don't try to find another */
283 if (pcity) {
285 "waiting in a city for next turn.");
286 return;
287 }
288
289 /* Find a city to go to recover and paradrop from */
291
292 if (acity) {
294 if (!dai_unit_goto(ait, punit, acity->tile)) {
295 /* Died or unsuccessful move */
296 return;
297 }
298 } else {
300 "didn't find city to go and recover.");
301 dai_manage_military(ait, pplayer, punit);
302 }
303 }
304}
305
306/*************************************************************************/
311 struct tile *ptile_city)
312{
313 const struct unit_type* u_type = unit_type_get(punit);
314 int range = u_type->paratroopers_range;
315 int profit = 0;
316 struct player* pplayer = unit_owner(punit);
317 int total, total_cities;
318 struct civ_map *nmap = &(wld.map);
319
320 profit += u_type->defense_strength
321 + u_type->move_rate
322 + u_type->attack_strength;
323
325 int multiplier;
326 struct city *pcity = tile_city(ptile);
327
328 if (!pcity) {
329 continue;
330 }
331
332 if (!map_is_known(ptile, pplayer)) {
333 continue;
334 }
335
336 /* We prefer jumping to other continents. On the same continent we
337 * can fight traditionally.
338 * FIXME: Handle ocean cities we can attack. */
339 if (!is_ocean_tile(ptile)
341 if (get_continent_size(tile_continent(ptile)) < 3) {
342 /* Tiny island are hard to conquer with traditional units */
343 multiplier = 10;
344 } else {
345 multiplier = 5;
346 }
347 } else {
348 multiplier = 1;
349 }
350
351 /* There are lots of units, the city will be safe against paratroopers. */
352 if (unit_list_size(ptile->units) > 2) {
353 continue;
354 }
355
356 /* Prefer long jumps.
357 * If a city is near we can take/protect it with normal units */
358 if (pplayers_allied(pplayer, city_owner(pcity))) {
359 profit += city_size_get(pcity)
361 } else {
362
365 }
367
368 total = adv_data_get(pplayer, NULL)->stats.units.paratroopers;
370
371 if (total > total_cities) {
372 profit = profit * total_cities / total;
373 }
374
375 return profit;
376}
377
378/*************************************************************************/
382 struct player *pplayer, struct city *pcity,
383 struct adv_choice *choice, bool allow_gold_upkeep)
384{
385 const struct research *presearch = research_get(pplayer);
386 int profit;
387 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
388 const struct civ_map *nmap = &(wld.map);
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(nmap, 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:5821
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:4082
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:5517
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define action_has_result(_act_, _res_)
Definition actions.h:431
#define action_iterate_end
Definition actions.h:465
#define action_iterate(_act_)
Definition actions.h:461
#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:605
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)
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition aitools.c:606
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
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 civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
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:478
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:989
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
struct unit * get_defender(const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile, const struct action *paction)
Definition combat.c:836
bool can_unit_attack_tile(const struct unit *punit, const struct action *paction, const struct tile *dest_tile)
Definition combat.c:311
@ ATT_OK
Definition combat.h:35
char * incite_cost
Definition comments.c:75
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
void dai_manage_military(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2499
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1632
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:601
#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:354
int Tech_type_id
Definition fc_types.h:377
#define ADV_WANT_PRINTF
Definition fc_types.h:1355
@ O_GOLD
Definition fc_types.h:101
struct world wld
Definition game.c:63
struct unit * game_unit_by_number(int id)
Definition game.c:116
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:630
#define adjc_iterate_end
Definition map.h:433
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:391
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:428
#define square_iterate_end
Definition map.h:394
int get_continent_size(Continent_id id)
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:894
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:920
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:1405
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:279
int defense_bonus
Definition terrain.h:200
Definition tile.h:50
int paratroopers_range
Definition unittype.h:541
Definition unit.h:138
int moves_left
Definition unit.h:150
int id
Definition unit.h:145
int hp
Definition unit.h:151
struct tile * tile
Definition unit.h:140
struct civ_map map
#define FALSE
Definition support.h:47
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 advance_index_iterate_end
Definition tech.h:248
tech_req
Definition tech.h:110
#define A_FIRST
Definition tech.h:44
#define advance_index_iterate(_start, _index)
Definition tech.h:244
#define is_ocean(pterrain)
Definition terrain.h:301
#define is_ocean_tile(ptile)
Definition terrain.h:303
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:88
#define tile_terrain(_tile)
Definition tile.h:110
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_continent(_tile)
Definition tile.h:92
const struct unit_type * utype
Definition fc_types.h:721
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1624
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:822
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1729
#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:3313
#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:839
#define unit_tech_reqs_iterate_end
Definition unittype.h:881
#define unit_tech_reqs_iterate(_utype_, _p)
Definition unittype.h:875
#define unit_type_iterate(_p)
Definition unittype.h:855
#define unit_type_iterate_end
Definition unittype.h:862