Freeciv-3.3
Loading...
Searching...
No Matches
daiparadrop.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 "map.h"
25#include "player.h"
26#include "research.h"
27#include "unit.h"
28#include "unitlist.h"
29
30/* common/aicore */
31#include "pf_tools.h"
32
33/* server */
34#include "citytools.h"
35#include "maphand.h"
36#include "srv_log.h"
37#include "unithand.h"
38#include "unittools.h"
39
40/* server/advisors */
41#include "advdata.h"
42
43/* ai */
44#include "handicaps.h"
45
46/* ai/default */
47#include "daicity.h"
48#include "daidata.h"
49#include "daiplayer.h"
50#include "daitools.h"
51#include "daiunit.h"
52
53#include "daiparadrop.h"
54
55
56#define LOGLEVEL_PARATROOPER LOG_DEBUG
57
58/*************************************************************************/
61static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait,
62 struct unit *punit)
63{
64 int best = 0;
65 int val;
66 struct tile* best_tile = NULL;
68 struct city* acity;
69 struct player* pplayer = unit_owner(punit);
70 struct civ_map *nmap = &(wld.map);
71
72 /* First, we search for undefended cities in danger */
74 if (!map_is_known(ptile, pplayer)) {
75 continue;
76 }
77
78 acity = tile_city(ptile);
80 && unit_list_size(ptile->units) == 0) {
82 if (val > best) {
83 best = val;
84 best_tile = ptile;
85 }
86 }
88
89 if (best_tile != NULL) {
92 "Choose to jump in order to protect allied city %s (%d %d). "
93 "Benefit: %d",
95 return best_tile;
96 }
97
98 /* Second, we search for undefended enemy cities */
100 acity = tile_city(ptile);
102 && (unit_list_size(ptile->units) == 0)) {
103 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)
104 && has_handicap(pplayer, H_FOG)) {
105 continue;
106 }
107 /* Prefer big cities on other continents */
108 val = city_size_get(acity)
110 if (val > best) {
111 best = val;
112 best_tile = ptile;
113 }
114 }
116
117 if (best_tile != NULL) {
120 "Choose to jump into enemy city %s (%d %d). Benefit: %d",
122 return best_tile;
123 }
124
125 /* Jump to kill adjacent units */
127 struct terrain *pterrain = tile_terrain(ptile);
128 if (is_ocean(pterrain)) {
129 continue;
130 }
131 if (has_handicap(pplayer, H_FOG)) {
132 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
133 continue;
134 }
135 } else {
136 if (!map_is_known(ptile, pplayer)) {
137 continue;
138 }
139 }
140 acity = tile_city(ptile);
141 if (acity && !pplayers_allied(city_owner(acity), pplayer)) {
142 continue;
143 }
144 if (!acity && unit_list_size(ptile->units) > 0) {
145 continue;
146 }
147
148 /* Iterate over adjacent tile to find good victim */
149 adjc_iterate(nmap, ptile, target) {
150 if (unit_list_size(target->units) == 0
151 || !can_unit_attack_tile(punit, NULL, target)
152 || is_ocean_tile(target)
153 || (has_handicap(pplayer, H_FOG)
154 && !map_is_known_and_seen(target, pplayer, V_MAIN))) {
155 continue;
156 }
157 val = 0;
158 if (is_stack_vulnerable(target)) {
159 unit_list_iterate(target->units, victim) {
160 if ((!has_handicap(pplayer, H_FOG)
161 || can_player_see_unit(pplayer, victim))
163 victim, target)
164 == ATT_OK)) {
165 val += victim->hp * 100;
166 }
168 } else {
169 val += get_defender(nmap, punit, target, NULL)->hp * 100;
170 }
171 val *= unit_win_chance(nmap, punit,
172 get_defender(nmap, punit, target, NULL),
173 NULL);
174 val += pterrain->defense_bonus / 10;
175 val -= punit->hp * 100;
176
177 if (val > best) {
178 best = val;
179 best_tile = ptile;
180 }
183
184 if (best_tile != NULL) {
185 UNIT_LOG(LOG_DEBUG, punit, "Choose to jump at (%d, %d) to attack "
186 "adjacent units. Benefit: %d",
187 TILE_XY(best_tile), best);
188 }
189
190 return best_tile;
191}
192
193/*************************************************************************/
196void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer,
197 struct unit *punit)
198{
199 struct city *pcity = tile_city(unit_tile(punit));
200 struct tile *ptile_dest = NULL;
201 const struct civ_map *nmap = &(wld.map);
202 int sanity = punit->id;
203
204 /* Defend attacking (and be opportunistic too) */
207 /* Dead */
208 return;
209 }
210
211 /* Check to recover hit points */
212 if ((punit->hp < unit_type_get(punit)->hp / 2) && pcity) {
213 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Recovering hit points.");
214 return;
215 }
216
217 /* Nothing to do! */
218 if (punit->moves_left == 0) {
219 return;
220 }
221
222 if (pcity && unit_list_size(unit_tile(punit)->units) == 1) {
223 UNIT_LOG(LOGLEVEL_PARATROOPER, punit, "Defending the city.");
224 return;
225 }
226
229
230 if (ptile_dest) {
231 bool jump_performed = FALSE;
232
233 action_iterate(act_id) {
234 struct action *paction = action_by_number(act_id);
235 bool possible;
236
239 /* Not relevant. */
240 continue;
241 }
242
243 if (has_handicap(pplayer, H_FOG)) {
247 ptile_dest, NULL));
248 } else {
251 }
252
253 if (possible) {
258 }
259
261 /* Finished or finished. */
262 break;
263 }
265
266 if (jump_performed) {
267 /* Successful! */
269 /* The unit did not survive the move */
270 return;
271 }
272 /* We attack the target */
275 }
276 }
277 } else {
278 /* We can't paradrop :-( */
279 struct city *acity = NULL;
280
281 /* We are in a city, so don't try to find another */
282 if (pcity) {
284 "waiting in a city for next turn.");
285 return;
286 }
287
288 /* Find a city to go to recover and paradrop from */
290
291 if (acity) {
293 if (!dai_unit_goto(ait, punit, acity->tile)) {
294 /* Died or unsuccessful move */
295 return;
296 }
297 } else {
299 "didn't find city to go and recover.");
300 dai_manage_military(ait, pplayer, punit);
301 }
302 }
303}
304
305/*************************************************************************/
310 struct tile *ptile_city)
311{
312 const struct unit_type* u_type = unit_type_get(punit);
313 int range = u_type->paratroopers_range;
314 int profit = 0;
315 struct player* pplayer = unit_owner(punit);
316 int total, total_cities;
317 struct civ_map *nmap = &(wld.map);
318
319 profit += u_type->defense_strength
320 + u_type->move_rate
321 + u_type->attack_strength;
322
324 int multiplier;
325 struct city *pcity = tile_city(ptile);
326
327 if (!pcity) {
328 continue;
329 }
330
331 if (!map_is_known(ptile, pplayer)) {
332 continue;
333 }
334
335 /* We prefer jumping to other continents. On the same continent we
336 * can fight traditionally.
337 * FIXME: Handle ocean cities we can attack. */
338 if (!is_ocean_tile(ptile)
340 if (get_continent_size(tile_continent(ptile)) < 3) {
341 /* Tiny island are hard to conquer with traditional units */
342 multiplier = 10;
343 } else {
344 multiplier = 5;
345 }
346 } else {
347 multiplier = 1;
348 }
349
350 /* There are lots of units, the city will be safe against paratroopers. */
351 if (unit_list_size(ptile->units) > 2) {
352 continue;
353 }
354
355 /* Prefer long jumps.
356 * If a city is near we can take/protect it with normal units */
357 if (pplayers_allied(pplayer, city_owner(pcity))) {
360 } else {
361
364 }
366
367 total = adv_data_get(pplayer, NULL)->stats.units.paratroopers;
369
370 if (total > total_cities) {
371 profit = profit * total_cities / total;
372 }
373
374 return profit;
375}
376
377/*************************************************************************/
381 struct player *pplayer, struct city *pcity,
382 struct adv_choice *choice, bool allow_gold_upkeep)
383{
384 const struct research *presearch = research_get(pplayer);
385 int profit;
386 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
387 const struct civ_map *nmap = &(wld.map);
388
389 /* military_advisor_choose_build() does something idiotic,
390 * this function should not be called if there is danger... */
391 if (choice->want >= 100 && choice->type != CT_ATTACKER) {
392 return;
393 }
394
396 struct unit *virtual_unit;
397
400 continue;
401 }
402
403 if (!allow_gold_upkeep && utype_upkeep_cost(u_type, pplayer, O_GOLD) > 0) {
404 continue;
405 }
406
407 /* Temporary hack before we are smart enough to return such units */
409 && 1 == utype_fuel(u_type)) {
410 continue;
411 }
412
413 /* Assign tech for paratroopers */
415 /* We raise want if the required tech is not known */
417
419 plr_data->tech_want[tech_req] += 2;
420 log_base(LOGLEVEL_PARATROOPER, "Raising tech want in city %s for %s "
421 "stimulating %s with %d (" ADV_WANT_PRINTF ") and req",
423 player_name(pplayer),
425 2,
426 plr_data->tech_want[tech_req]);
427
428 /* Now, we raise want for prerequisites */
431 plr_data->tech_want[k] += 1;
432 }
434 }
436
437 /* we only update choice struct if we can build it! */
439 continue;
440 }
441
442 /* it's worth building that unit? */
444 pplayer, pcity, u_type,
448
449 /* update choice struct if it's worth */
450 if (profit > choice->want) {
451 /* Update choice */
452 choice->want = profit;
453 choice->value.utype = u_type;
454 choice->type = CT_ATTACKER;
455 choice->need_boat = FALSE;
456 adv_choice_set_use(choice, "paratrooper");
457 log_base(LOGLEVEL_PARATROOPER, "%s wants to build %s (want=%d)",
459 }
461
462 return;
463}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5091
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:3332
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:4787
static struct action * action_by_number(action_id act_id)
Definition actions.h:396
#define action_has_result(_act_, _res_)
Definition actions.h:180
#define action_iterate_end
Definition actions.h:214
#define action_iterate(_act_)
Definition actions.h:210
#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: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:566
#define city_owner(_pcity_)
Definition city.h:560
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:994
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:841
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:76
static struct tile * find_best_tile_to_paradrop_to(struct ai_type *ait, struct unit *punit)
Definition daiparadrop.c:61
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
Definition daiparadrop.c:56
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 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:605
void dai_manage_military(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2505
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1636
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
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 int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
#define NO_TARGET
Definition fc_types.h:213
int Tech_type_id
Definition fc_types.h:236
#define ADV_WANT_PRINTF
Definition fc_types.h:1064
@ 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:95
@ LOG_DEBUG
Definition log.h:35
int get_continent_size(Continent_id id)
Definition map.c:819
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:675
#define adjc_iterate_end
Definition map.h:430
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:388
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:425
#define square_iterate_end
Definition map.h:391
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:899
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:925
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::@94::@97 units
int paratroopers
Definition advdata.h:89
struct adv_data::@94 stats
unsigned int urgency
Definition daicity.h:49
Definition ai.h:50
Definition city.h:317
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:244
tech_req
Definition tech.h:105
#define A_FIRST
Definition tech.h:44
#define advance_index_iterate(_start, _index)
Definition tech.h:240
#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:553
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1661
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:846
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1766
#define unit_tile(_pu)
Definition unit.h:404
#define unit_owner(_pu)
Definition unit.h:403
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:3358
#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:1235
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1584
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:377
#define utype_fuel(ptype)
Definition unittype.h:846
#define unit_tech_reqs_iterate_end
Definition unittype.h:888
#define unit_tech_reqs_iterate(_utype_, _p)
Definition unittype.h:882
#define unit_type_iterate(_p)
Definition unittype.h:862
#define unit_type_iterate_end
Definition unittype.h:869