Freeciv-3.1
Loading...
Searching...
No Matches
daimilitary.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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#include <string.h>
19
20/* utility */
21#include "log.h"
22
23/* common */
24#include "combat.h"
25#include "game.h"
26#include "government.h"
27#include "map.h"
28#include "movement.h"
29#include "research.h"
30#include "specialist.h"
31#include "unitlist.h"
32
33/* common/aicore */
34#include "pf_tools.h"
35
36/* server */
37#include "citytools.h"
38#include "cityturn.h"
39#include "srv_log.h"
40#include "srv_main.h"
41
42/* server/advisors */
43#include "advbuilding.h"
44#include "advchoice.h"
45#include "advdata.h"
46#include "advgoto.h"
47#include "advtools.h"
48#include "autosettlers.h"
49#include "infracache.h" /* adv_city */
50
51/* ai */
52#include "aitraits.h"
53#include "difficulty.h"
54#include "handicaps.h"
55
56/* ai/default */
57#include "aiair.h"
58#include "aidata.h"
59#include "aidiplomat.h"
60#include "aiferry.h"
61#include "aihand.h"
62#include "aihunt.h"
63#include "ailog.h"
64#include "aiparatrooper.h"
65#include "aiplayer.h"
66#include "aitech.h"
67#include "aitools.h"
68#include "aiunit.h"
69#include "daicity.h"
70#include "daieffects.h"
71
72#include "daimilitary.h"
73
74/* Size 1 city gets destroyed when conquered. It's still a good thing
75 * stop enemy from having it. */
76#define CITY_CONQUEST_WORTH(_city_, _data_) \
77 (_data_->worth * 0.9 + (city_size_get(_city_) - 0.5) * 10)
78
79static unsigned int assess_danger(struct ai_type *ait, struct city *pcity,
80 const struct civ_map *dmap,
82
84 const struct unit_type *punittype);
86 const struct unit_type *punittype);
87
88/**********************************************************************/
92 struct unit *attacker)
93{
94 struct unit_type *bestunit = NULL;
95 double best = 0;
96 int best_cost = FC_INFINITY;
97 struct player *pplayer = city_owner(pcity);
98 struct civ_map *nmap = &(wld.map);
99
100 simple_ai_unit_type_iterate(punittype) {
101 if (can_city_build_unit_now(pcity, punittype)) {
102 int fpatt, fpdef, defense, attack;
103 double want, loss, cost = utype_build_shield_cost(pcity, NULL, punittype);
104 struct unit *defender;
105 int veteran = get_unittype_bonus(city_owner(pcity), pcity->tile,
106 punittype, NULL,
107 EFT_VETERAN_BUILD);
108
109 defender = unit_virtual_create(pplayer, pcity, punittype, veteran);
110 defense = get_total_defense_power(attacker, defender);
111 attack = get_total_attack_power(attacker, defender);
112 get_modified_firepower(nmap, attacker, defender, &fpatt, &fpdef);
113
114 /* Greg's algorithm. loss is the average number of health lost by
115 * defender. If loss > attacker's hp then we should win the fight,
116 * which is always a good thing, since we avoid shield loss. */
117 loss = (double) defense * punittype->hp * fpdef / (attack * fpatt);
118 want = (loss + MAX(0, loss - attacker->hp)) / cost;
119
120#ifdef NEVER
121 CITY_LOG(LOG_DEBUG, pcity, "desire for %s against %s(%d,%d) is %.2f",
122 unit_name_orig(punittype),
123 unit_name_orig(unit_type_get(attacker)),
124 TILE_XY(attacker->tile), want);
125#endif /* NEVER */
126
127 if (want > best || (ADV_WANTS_EQ(want, best)
128 && cost <= best_cost)) {
129 best = want;
130 bestunit = punittype;
131 best_cost = cost;
132 }
133 unit_virtual_destroy(defender);
134 }
136
137 return bestunit;
138}
139
140/**********************************************************************/
145static struct unit_type *dai_choose_attacker(struct ai_type *ait,
146 struct city *pcity,
147 enum terrain_class tc,
148 bool allow_gold_upkeep)
149{
150 struct unit_type *bestid = NULL;
151 adv_want best = -1;
152 adv_want cur;
153 struct player *pplayer = city_owner(pcity);
154
156 if (!allow_gold_upkeep && utype_upkeep_cost(putype, pplayer, O_GOLD) > 0) {
157 continue;
158 }
159
160 cur = dai_unit_attack_desirability(ait, putype);
161 if ((tc == TC_LAND && utype_class(putype)->adv.land_move != MOVE_NONE)
162 || (tc == TC_OCEAN
163 && utype_class(putype)->adv.sea_move != MOVE_NONE)) {
164 if (can_city_build_unit_now(pcity, putype)
165 && (cur > best
166 || (ADV_WANTS_EQ(cur, best)
167 && utype_build_shield_cost(pcity, NULL, putype)
168 <= utype_build_shield_cost(pcity, NULL, bestid)))) {
169 best = cur;
170 bestid = putype;
171 }
172 }
174
175 return bestid;
176}
177
178/**********************************************************************/
186static struct unit_type *dai_choose_bodyguard(struct ai_type *ait,
187 struct city *pcity,
188 enum terrain_class tc,
189 enum unit_role_id role,
190 bool allow_gold_upkeep)
191{
192 struct unit_type *bestid = NULL;
193 adv_want best = 0;
194 struct player *pplayer = city_owner(pcity);
195
197 /* Only consider units of given role, or any if invalid given */
198 if (unit_role_id_is_valid(role)) {
199 if (!utype_has_role(putype, role)) {
200 continue;
201 }
202 }
203
204 if (!allow_gold_upkeep && utype_upkeep_cost(putype, pplayer, O_GOLD) > 0) {
205 continue;
206 }
207
208 /* Only consider units of same move type */
209 if ((tc == TC_LAND && utype_class(putype)->adv.land_move == MOVE_NONE)
210 || (tc == TC_OCEAN
211 && utype_class(putype)->adv.sea_move == MOVE_NONE)) {
212 continue;
213 }
214
215 /* Now find best */
216 if (can_city_build_unit_now(pcity, putype)) {
217 const adv_want desire = dai_unit_defense_desirability(ait, putype);
218
219 if (desire > best
220 || (ADV_WANTS_EQ(desire, best) && utype_build_shield_cost(pcity, NULL, putype) <=
221 utype_build_shield_cost(pcity, NULL, bestid))) {
222 best = desire;
223 bestid = putype;
224 }
225 }
227
228 return bestid;
229}
230
231/**********************************************************************/
234static int base_assess_defense_unit(struct city *pcity, struct unit *punit,
235 bool igwall, bool quadratic,
236 int wall_value)
237{
238 int defense;
239 int fp;
240
241 if (!is_military_unit(punit)) {
242 return 0;
243 }
244
245 defense = get_fortified_defense_power(NULL, punit) * punit->hp;
247 if (unit_has_type_flag(punit, UTYF_BADCITYDEFENDER)) {
248 /* Attacker firepower doubled, defender firepower set to
249 * game.info.low_firepower_pearl_harbour at max. */
251 defense /= 2;
252 } else {
253 defense *= fp;
254 }
255
256 defense /= POWER_DIVIDER;
257
258 if (quadratic) {
259 defense *= defense;
260 }
261
262 if (pcity != NULL && !igwall && city_got_defense_effect(pcity, NULL)) {
263 /* FIXME: We checked if city got defense effect against *some*
264 * unit type. Sea unit danger might cause us to build defenses
265 * against air units... */
266
267 /* TODO: What about wall_value < 10? Do we really want walls to
268 * result in decrease in the returned value? */
269 defense *= wall_value;
270 defense /= 10;
271 }
272
273 return defense;
274}
275
276/**********************************************************************/
279int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
280{
281 int defense = 0, walls = 0;
282 /* This can be an arg if needed, but we don't need to change it now. */
283 const bool igwall = FALSE;
284 struct ai_city *city_data = def_ai_city_data(pcity, ait);
285
286 /* wallvalue = 10, walls = 10,
287 * wallvalue = 40, walls = 20,
288 * wallvalue = 90, walls = 30 */
289
290 while (walls * walls < city_data->wallvalue * 10) {
291 walls++;
292 }
293
295 defense += base_assess_defense_unit(pcity, punit, igwall, FALSE,
296 walls);
298
299 if (defense > 1<<12) {
300 CITY_LOG(LOG_VERBOSE, pcity, "Overflow danger in assess_defense_quadratic:"
301 " %d", defense);
302 if (defense > 1<<15) {
303 defense = 1<<15; /* more defense than we know what to do with! */
304 }
305 }
306
307 return defense * defense;
308}
309
310/**********************************************************************/
313int assess_defense_unit(struct ai_type *ait, struct city *pcity,
314 struct unit *punit, bool igwall)
315{
316 return base_assess_defense_unit(pcity, punit, igwall, TRUE,
317 def_ai_city_data(pcity, ait)->wallvalue);
318}
319
320/**********************************************************************/
327static int assess_defense_backend(struct ai_type *ait, struct city *pcity,
328 bool igwall)
329{
330 /* Estimate of our total city defensive might */
331 int defense = 0;
332
334 defense += assess_defense_unit(ait, pcity, punit, igwall);
336
337 return defense;
338}
339
340/**********************************************************************/
343int assess_defense(struct ai_type *ait, struct city *pcity)
344{
345 return assess_defense_backend(ait, pcity, FALSE);
346}
347
348/**********************************************************************/
352static int assess_defense_igwall(struct ai_type *ait, struct city *pcity)
353{
354 return assess_defense_backend(ait, pcity, TRUE);
355}
356
357/**********************************************************************/
363static enum fc_tristate
364tactical_req_cb(const struct req_context *context,
365 const struct player *other_player,
366 const struct requirement *req,
367 void *data, int n_data)
368{
369 switch (req->source.kind) {
370 case VUT_IMPROVEMENT:
371 {
372 const struct impr_type *b = req->source.value.building;
373
374 /* FIXME: in actor_reqs, may allow attack _from_ a city with... */
375 if (req->survives || NULL == context->city || is_great_wonder(b)
376 || !city_has_building(context->city, b) || b->sabotage <= 0) {
377 return tri_req_active(context, other_player, req);
378 }
379 /* Else may be sabotaged */
380 }
382 case VUT_UNITSTATE:
383 case VUT_ACTIVITY:
384 case VUT_MINSIZE:
385 case VUT_MAXTILEUNITS:
386 case VUT_MINHP:
387 case VUT_MINMOVES:
388 /* Can be changed back or forth quickly */
389 return TRI_MAYBE;
390 case VUT_MINVETERAN:
391 /* Can be changed forth but not back */
392 return is_req_preventing(context, other_player, req, RPT_POSSIBLE)
394 case VUT_MINFOREIGNPCT:
395 case VUT_NATIONALITY:
396 /* Can be changed back but hardly forth (foreign citizens reduced first) */
397 switch (tri_req_active(context, other_player, req)) {
398 case TRI_NO:
399 return req->present ? TRI_NO : TRI_MAYBE;
400 case TRI_YES:
401 return req->present ? TRI_MAYBE : TRI_YES;
402 default:
403 return TRI_MAYBE;
404 }
405 case VUT_DIPLREL:
406 case VUT_DIPLREL_TILE:
407 case VUT_DIPLREL_TILE_O:
408 case VUT_DIPLREL_UNITANY:
409 case VUT_DIPLREL_UNITANY_O:
410 /* If the attack happens, there is a diplrel that allows it */
411 return TRI_YES;
412 case VUT_AGE:
413 case VUT_MINCALFRAG:
414 case VUT_MINYEAR:
415 /* If it is not near, won't change */
416 return tri_req_active_turns(n_data, 5 /* WAG */,
417 context, other_player, req);
418 case VUT_UTYPE:
419 case VUT_UTFLAG:
420 case VUT_UCLASS:
421 case VUT_UCFLAG:
422 /* FIXME: support converting siege machines (needs hard reqs checked) */
423 case VUT_CITYSTATUS:
424 case VUT_ACTION:
425 case VUT_OTYPE:
426 case VUT_SPECIALIST:
427 case VUT_EXTRAFLAG:
428 case VUT_AI_LEVEL:
429 case VUT_CITYTILE:
430 case VUT_STYLE:
431 case VUT_TOPO:
432 case VUT_SERVERSETTING:
433 case VUT_NATION:
434 case VUT_NATIONGROUP:
435 case VUT_ADVANCE:
436 case VUT_TECHFLAG:
437 case VUT_GOVERNMENT:
438 case VUT_ACHIEVEMENT:
439 case VUT_IMPR_GENUS:
440 case VUT_MINCULTURE:
441 case VUT_MINTECHS:
442 case VUT_ROADFLAG:
443 case VUT_TERRAIN:
444 case VUT_EXTRA:
445 case VUT_GOOD:
446 case VUT_TERRAINCLASS:
447 case VUT_TERRFLAG:
448 case VUT_TERRAINALTER:
449 case VUT_NONE:
450 return tri_req_active(context, other_player, req);
451 case VUT_COUNT:
452 /* Not implemented. */
453 break;
454 }
456
457 return TRI_NO;
458}
459
460/**********************************************************************/
467static bool
469 const struct unit *actor,
470 const struct city *pcity,
471 int turns)
472{
473 const struct player *target_player = city_owner(pcity),
474 *actor_player = unit_owner(actor);
475 const struct unit_type *utype = unit_type_get(actor);
476 const struct req_context target_ctx = {
477 .player = target_player,
478 .city = pcity,
479 .tile = city_tile(pcity)
480 }, actor_ctx = {
481 .player = actor_player,
482 .unit = actor,
483 .unittype = utype
484 };
485
486 if (!utype_can_do_action(utype, wanted_action)) {
487 return FALSE;
488 }
490 enabler) {
491 /* We assume that we could build or move units into the city
492 * that are not present there yet */
493 if (TRI_NO != tri_reqs_cb_active(&actor_ctx, target_player,
494 &enabler->actor_reqs, NULL,
495 tactical_req_cb, NULL, turns)
496 &&
497 TRI_NO != tri_reqs_cb_active(&target_ctx, actor_player,
498 &enabler->target_reqs, NULL,
499 tactical_req_cb, NULL, turns)) {
500 return TRUE;
501 }
503
504 return FALSE;
505}
506
507/**********************************************************************/
510static unsigned int assess_danger_unit(const struct city *pcity,
511 struct pf_reverse_map *pcity_map,
512 const struct unit *punit,
513 int *move_time)
514{
515 struct pf_position pos;
516 const struct unit_type *punittype = unit_type_get(punit);
517 const struct tile *ptile = city_tile(pcity);
518 const struct player *uowner = unit_owner(punit);
519 const struct unit *ferry;
520 unsigned int danger;
521 int amod = -99, dmod;
522 struct civ_map *nmap = &(wld.map);
523 bool attack_danger = FALSE;
524
525 *move_time = PF_IMPOSSIBLE_MC;
526
527 if ((utype_can_do_action_result(punittype, ACTRES_PARADROP)
528 || utype_can_do_action_result(punittype, ACTRES_PARADROP_CONQUER))
529 && 0 < punittype->paratroopers_range) {
530 *move_time = (real_map_distance(ptile, unit_tile(punit))
531 / punittype->paratroopers_range);
532 }
533
534 if (pf_reverse_map_unit_position(pcity_map, punit, &pos)
535 && (PF_IMPOSSIBLE_MC == *move_time
536 || *move_time > pos.turn)) {
537 *move_time = pos.turn;
538 }
539
541 && (ferry = unit_transport_get(punit))
542 && pf_reverse_map_unit_position(pcity_map, ferry, &pos)) {
543 if ((PF_IMPOSSIBLE_MC == *move_time
544 || *move_time > pos.turn)) {
545 *move_time = pos.turn;
546 if (!can_attack_from_non_native(punittype)) {
547 (*move_time)++;
548 }
549 }
550 }
551
552 if (PF_IMPOSSIBLE_MC == *move_time) {
553 return 0;
554 }
555 if (!is_native_tile(punittype, ptile)
556 && !can_attack_non_native(punittype)) {
557 return 0;
558 }
559 if (!is_native_near_tile(nmap, unit_class_get(punit), ptile)) {
560 return 0;
561 }
562
563 /* Find the worst attack action to expect */
564 action_by_result_iterate(paction, ACTRES_ATTACK) {
565 /* Is it possible that punit will do action id to the city? */
566
567 if (action_may_happen_unit_on_city(paction->id, punit, pcity, *move_time)) {
568 int b;
569
570 attack_danger = TRUE;
571
572 b = get_unittype_bonus(uowner, ptile, punittype, paction, EFT_ATTACK_BONUS);
573 if (b > amod) {
574 amod = b;
575 }
576 }
578
579 /* FIXME: it's a dummy support for anti-bombard defense just to do something against
580 * approaching bombarders. Some better logic is needed, see OSDN#41778 */
581 if (!attack_danger) {
582 action_by_result_iterate(paction, ACTRES_BOMBARD) {
583 if (action_may_happen_unit_on_city(paction->id, punit, pcity, *move_time)) {
584 int b;
585
586 attack_danger = TRUE;
587
588 b = get_unittype_bonus(uowner, ptile, punittype, paction, EFT_ATTACK_BONUS);
589 if (b > amod) {
590 amod = b;
591 }
592 }
594 /* Here something should be done cuz the modifier affects
595 * more than one unit but not their full hp, but is not done yet...*/
596 }
597 if (!attack_danger) {
598 /* If the unit is dangerous, it's not about its combat strength */
599 return 0;
600 }
601
602 danger = adv_unit_att_rating(punit);
603 dmod = 100 + get_unittype_bonus(city_owner(pcity), ptile,
604 punittype, NULL, EFT_DEFEND_BONUS);
605 return danger * (amod + 100) / MAX(dmod, 1);
606}
607
608/**********************************************************************/
613void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer,
614 const struct civ_map *dmap)
615{
616 /* Do nothing if game is not running */
617 if (S_S_RUNNING == server_state()) {
618 city_list_iterate(pplayer->cities, pcity) {
619 (void) assess_danger(ait, pcity, dmap, NULL);
621 }
622}
623
624/**********************************************************************/
643static void dai_reevaluate_building(struct city *pcity, adv_want *value,
644 unsigned int urgency, unsigned int danger,
645 int defense)
646{
647 if (*value == 0 || danger <= 0) {
648 return;
649 }
650
651 *value = MAX(*value, 100 + urgency); /* default */
652
653 if (urgency > 0 && danger > defense * 2) {
654 *value += 100;
655 } else if (defense != 0 && danger > defense) {
656 *value = MAX(danger * 100 / defense, *value);
657 }
658}
659
660/**********************************************************************/
678static unsigned int assess_danger(struct ai_type *ait, struct city *pcity,
679 const struct civ_map *dmap,
681{
682 struct player *pplayer = city_owner(pcity);
683 struct tile *ptile = city_tile(pcity);
684 struct ai_city *city_data = def_ai_city_data(pcity, ait);
685 /* How much such danger there is that building would help against. */
686 unsigned int danger_reduced[B_LAST];
687 int i;
688 int defender;
689 unsigned int urgency = 0;
690 int defense;
691 int total_danger = 0;
692
693 /* TODO: Presumably most, or even all, of these arrays
694 * could be of size game.control.num_unit_types instead
695 * of full U_LAST */
696 int defense_bonuses_pct[U_LAST];
697 bool defender_type_handled[U_LAST];
698 int best_non_scramble[U_LAST];
699 bool sth_does_not_scramble = FALSE;
700 int city_def_against[U_LAST];
701 int assess_turns;
702 bool omnimap;
703
705
706 /* Initialize data. */
707 memset(&danger_reduced, 0, sizeof(danger_reduced));
708 if (has_handicap(pplayer, H_DANGER)) {
709 /* Always thinks that city is in grave danger */
710 city_data->grave_danger = 1;
711 } else {
712 city_data->grave_danger = 0;
713 }
714 city_data->diplomat_threat = FALSE;
715 city_data->has_diplomat = FALSE;
716
717 unit_type_iterate(utype) {
718 int idx = utype_index(utype);
719
720 defense_bonuses_pct[idx] = 0;
721 defender_type_handled[idx] = FALSE;
722 best_non_scramble[idx] = -1;
723 /* FIXME: cache it somewhere? */
724 city_def_against[idx]
725 = 100 + get_unittype_bonus(pplayer, ptile, utype, NULL,
726 EFT_DEFEND_BONUS);
727 city_def_against[idx] = MAX(city_def_against[idx], 1);
729
730 /* What flag-specific bonuses do our units have. */
731 /* We value them less than general defense increment */
732 unit_list_iterate(ptile->units, punit) {
733 const struct unit_type *def = unit_type_get(punit);
734
735 if (unit_has_type_flag(punit, UTYF_DIPLOMAT)) {
736 city_data->has_diplomat = TRUE;
737 }
738 if (!defender_type_handled[utype_index(def)]) {
739 /* This is first defender of this type. Check defender type
740 * specific bonuses. */
741
742 /* Skip defenders that have no bonuses at all. Acceptable
743 * side-effect is that we can't consider negative bonuses at
744 * all ("No bonuses" should be better than "negative bonus") */
745 if (def->cache.max_defense_mp_bonus_pct > 0) {
746 unit_type_iterate(utype) {
747 int idx = utype_index(utype);
748 int coeff = def->cache.scramble_coeff[idx];
749 int bonus;
750
751 /* FIXME: consider EFT_FORTIFY_DEFENSE_BONUS */
752 if (coeff) {
753 bonus = coeff / city_def_against[idx] - 100;
754 } else {
755 bonus = def->cache.defense_mp_bonuses_pct[idx];
756 }
757
758 if (bonus > defense_bonuses_pct[idx]) {
759 if (!coeff) {
760 best_non_scramble[idx] = bonus;
761 }
762 defense_bonuses_pct[idx] = bonus;
763 } else if (!coeff) {
764 best_non_scramble[idx] = MAX(best_non_scramble[idx], bonus);
765 }
767 } else {
768 /* Just remember that such a unit exists and hope its bonuses are just 0 */
769 sth_does_not_scramble = TRUE;
770 }
771
772 defender_type_handled[utype_index(def)] = TRUE;
773 }
775 if (sth_does_not_scramble || unit_list_size(ptile->units) <= 0) {
776 /* Scrambling units tend to be expensive. If we have a barenaked city, we'll
777 * need at least a cheap unit. If we have only scramblers,
778 * maybe it's OK. */
779 unit_type_iterate(scrambler_type) {
780 Unit_type_id id = utype_index(scrambler_type);
781
782 best_non_scramble[id] = MAX(best_non_scramble[id], 0);
784 }
785
786 if (player_is_cpuhog(pplayer)) {
787 assess_turns = 6;
788 } else {
789 assess_turns = has_handicap(pplayer, H_ASSESS_DANGER_LIMITED) ? 2 : 3;
790 }
791
792 omnimap = !has_handicap(pplayer, H_MAP);
793
794 /* Check. */
795 players_iterate(aplayer) {
796 struct pf_reverse_map *pcity_map;
797 struct unit_list *units;
798
799 if (!adv_is_player_dangerous(pplayer, aplayer)) {
800 continue;
801 }
802 /* Note that we still consider the units of players we are not (yet)
803 * at war with. */
804
805 pcity_map = pf_reverse_map_new_for_city(pcity, aplayer, assess_turns,
806 omnimap, dmap);
807
808 if (ul_cb != NULL) {
809 units = ul_cb(aplayer);
810 } else {
811 units = aplayer->units;
812 }
813 unit_list_iterate(units, punit) {
814 int move_time;
815 unsigned int vulnerability;
816 int defbonus_pct;
817 const struct unit_type *utype = unit_type_get(punit);
818 struct unit_type_ai *utai = utype_ai_data(utype, ait);
819
820#ifdef FREECIV_WEB
821 /* freeciv-web ignores danger that is far in distance,
822 * no matter how quickly it would reach us; even if
823 * that's *immediately* over a road type that allows
824 * unlimited movement. */
825 int unit_distance = real_map_distance(ptile, unit_tile(punit));
826
827 if (unit_distance > ASSESS_DANGER_MAX_DISTANCE
829 && unit_distance > AI_HANDICAP_DISTANCE_LIMIT)) {
830 /* Too far away. */
831 continue;
832 }
833#endif /* FREECIV_WEB */
834
835 if (!utai->carries_occupiers
836 && !utype_acts_hostile(utype)) {
837 /* Harmless unit. */
838 continue;
839 }
840
841 /* Defender unspecific vulnerability and potential move time */
842 vulnerability = assess_danger_unit(pcity, pcity_map,
843 punit, &move_time);
844
845 if (PF_IMPOSSIBLE_MC == move_time) {
846 continue;
847 }
848
850 /* Even if there is no attack strength,
851 * we need ANY protector for the city */
852 vulnerability = MAX(vulnerability, 1);
853 if (3 >= move_time) {
854 urgency++;
855 if (1 >= move_time) {
856 city_data->grave_danger++;
857 }
858 }
859 }
860
861 defbonus_pct = defense_bonuses_pct[utype_index(utype)];
862 if (defbonus_pct > 100) {
863 defbonus_pct = (defbonus_pct + 100) / 2;
864 }
865 /* Reduce vulnerability for specific bonuses we do have */
866 vulnerability = vulnerability * 100 / (defbonus_pct + 100);
867 /* Pass the order for a new defender type against it
868 * to the scientific advisor, urgency considered */
869 (void) dai_wants_defender_against(ait, pplayer, pcity, utype,
870 vulnerability / MAX(move_time, 1));
871
872 if (utype_acts_hostile(unit_type_get(punit)) && 2 >= move_time) {
873 city_data->diplomat_threat = TRUE;
874 }
875
876 vulnerability *= vulnerability; /* positive feedback */
877 if (1 < move_time) {
878 vulnerability /= move_time;
879 }
880
881 if (unit_can_do_action(punit, ACTION_NUKE)
882 || unit_can_do_action(punit, ACTION_NUKE_CITY)
883 || unit_can_do_action(punit, ACTION_NUKE_UNITS)) {
884 defender = dai_find_source_building(pcity, EFT_NUKE_PROOF, utype);
885 if (defender != B_LAST) {
886 danger_reduced[defender] += vulnerability / MAX(move_time, 1);
887 }
888 } else if (best_non_scramble[utype_index(utype)] >= 0) {
889 /* To consider building a defensive building,
890 * build first a defender that gets any profit of it */
891 defender = dai_find_source_building(pcity, EFT_DEFEND_BONUS, utype);
892 if (defender != B_LAST) {
893 int danred = vulnerability / MAX(move_time, 1);
894 /* Maybe we can build an improvement
895 * that gets normal defender over scrambling one?
896 * Effectively a sqrt-scale because we don't test how high
897 * the effect is. */
898 if (best_non_scramble[utype_index(utype)]
899 < defense_bonuses_pct[utype_index(utype)]) {
900 danred = danred * (100 + best_non_scramble[utype_index(utype)])
901 / (100 + defense_bonuses_pct[utype_index(utype)]);
902 }
903 danger_reduced[defender] += danred;
904 }
905 }
906
907 total_danger += vulnerability;
909
910 pf_reverse_map_destroy(pcity_map);
911
913
914 if (total_danger) {
915 /* If any hostile player has any dangerous unit that can in any time
916 * reach the city, we consider building walls here, if none yet.
917 * FIXME: this value assumes that walls give x3 defense. */
918 city_data->wallvalue = 90;
919 } else {
920 /* No danger.
921 * This is half of the wallvalue of what danger 1 would produce. */
922 city_data->wallvalue = 5;
923 }
924
925 if (0 < city_data->grave_danger) {
926 /* really, REALLY urgent to defend */
927 urgency += 10 * city_data->grave_danger;
928 }
929
930 /* HACK: This needs changing if multiple improvements provide
931 * this effect. */
932 /* FIXME: Accept only buildings helping unit classes we actually use.
933 * Now we consider any land mover helper suitable. */
934 /* Sum of squared defense ratings */
935 defense = assess_defense_igwall(ait, pcity);
936
937 for (i = 0; i < B_LAST; i++) {
938 if (0 < danger_reduced[i]) {
940 urgency, danger_reduced[i], defense);
941 }
942 }
943
944 if (has_handicap(pplayer, H_DANGER) && 0 == total_danger) {
945 /* Has to have some danger
946 * Otherwise grave_danger will be ignored. */
947 city_data->danger = 1;
948 } else {
949 city_data->danger = total_danger;
950 }
951 city_data->urgency = urgency;
952
954
955 return urgency;
956}
957
958/**********************************************************************/
963 const struct unit_type *punittype)
964{
965 adv_want desire = punittype->hp;
966 int attack = punittype->attack_strength;
967 int defense = punittype->defense_strength;
968 int maxbonus_pct = 0;
969 int fp = punittype->firepower;
970
971 /* Sea and helicopters often have their firepower set to low firepower when
972 * defending. We can't have such units as defenders. */
973 if (utype_has_flag(punittype, UTYF_BADCITYDEFENDER)) {
975 }
976 if (((struct unit_type_ai *)utype_ai_data(punittype, ait))->low_firepower) {
978 }
979 desire *= fp;
980 desire *= defense;
981 desire += punittype->move_rate / SINGLE_MOVE;
982 desire += attack;
983
984 maxbonus_pct = punittype->cache.max_defense_mp_bonus_pct;
985 if (maxbonus_pct > 100) {
986 maxbonus_pct = (maxbonus_pct + 100) / 2;
987 }
988 desire += desire * maxbonus_pct / 100;
989 if (utype_has_flag(punittype, UTYF_GAMELOSS)) {
990 desire /= 10; /* But might actually be worth it */
991 }
992
993 return desire;
994}
995
996/**********************************************************************/
1000 const struct unit_type *punittype)
1001{
1002 adv_want desire = punittype->hp;
1003 int attack = punittype->attack_strength;
1004 int defense = punittype->defense_strength;
1005
1006 desire *= punittype->move_rate;
1007 desire *= punittype->firepower;
1008 desire *= attack;
1009 desire += defense;
1010 if (utype_has_flag(punittype, UTYF_IGTER)) {
1011 desire += desire / 2;
1012 }
1013 if (utype_has_flag(punittype, UTYF_GAMELOSS)) {
1014 desire /= 10; /* But might actually be worth it */
1015 }
1016 if (utype_has_flag(punittype, UTYF_CITYBUSTER)) {
1017 desire += desire / 2;
1018 }
1019 if (can_attack_from_non_native(punittype)) {
1020 desire += desire / 4;
1021 }
1022 if (punittype->adv.igwall) {
1023 desire += desire / 4;
1024 }
1025
1026 return desire;
1027}
1028
1029/**********************************************************************/
1034bool dai_process_defender_want(struct ai_type *ait, struct player *pplayer,
1035 struct city *pcity, unsigned int danger,
1036 struct adv_choice *choice, adv_want extra_want)
1037{
1038 const struct research *presearch = research_get(pplayer);
1039 /* FIXME: We check if the city has *some* defensive structure,
1040 * but not whether the city has a defensive structure against
1041 * any specific attacker. The actual danger may not be mitigated
1042 * by the defense selected... */
1043 bool walls = city_got_defense_effect(pcity, NULL);
1044 /* Technologies we would like to have. */
1045 adv_want tech_desire[U_LAST];
1046 /* Our favourite unit. */
1047 adv_want best = -1;
1048 struct unit_type *best_unit_type = NULL;
1049 int best_unit_cost = 1;
1050 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1051 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
1052 adv_want total_want = danger + extra_want;
1053
1054 memset(tech_desire, 0, sizeof(tech_desire));
1055
1056 simple_ai_unit_type_iterate(punittype) {
1057 adv_want desire; /* How much we want the unit? */
1058
1059 /* Only consider proper defenders - otherwise waste CPU and
1060 * bump tech want needlessly. */
1061 if (!utype_has_role(punittype, L_DEFEND_GOOD)
1062 && !utype_has_role(punittype, L_DEFEND_OK)) {
1063 continue;
1064 }
1065
1066 desire = dai_unit_defense_desirability(ait, punittype);
1067
1068 if (!utype_has_role(punittype, L_DEFEND_GOOD)) {
1069 desire /= 2; /* Not good, just ok */
1070 }
1071
1072 if (utype_has_flag(punittype, UTYF_FIELDUNIT)) {
1073 /* Causes unhappiness even when in defense, so not a good
1074 * idea for a defender, unless it is _really_ good.
1075 * Downright counter productive, if we want unit just for
1076 * maintaining peace. */
1077 if (danger == 0) {
1078 desire = -50;
1079 } else {
1080 desire /= 2;
1081 }
1082 }
1083
1084 if (desire > 0) {
1085 desire /= POWER_DIVIDER / 2; /* Good enough, no rounding errors. */
1086 desire *= desire;
1087
1088 if (can_city_build_unit_now(pcity, punittype)) {
1089 /* We can build the unit now... */
1090
1091 int build_cost = utype_build_shield_cost(pcity, NULL, punittype);
1092 int limit_cost = pcity->shield_stock + 40;
1093
1094 if (walls && !utype_has_flag(punittype, UTYF_BADCITYDEFENDER)) {
1095 desire *= city_data->wallvalue;
1096 /* TODO: More use of POWER_FACTOR ! */
1097 desire /= POWER_FACTOR;
1098 }
1099
1100 if ((best_unit_cost > limit_cost
1101 && build_cost < best_unit_cost)
1102 || ((desire > best
1103 || (ADV_WANTS_EQ(desire, best)
1104 && build_cost <= best_unit_cost))
1105 && (best_unit_type == NULL
1106 /* In case all units are more expensive than limit_cost */
1107 || limit_cost <= pcity->shield_stock + 40))) {
1108 best = desire;
1109 best_unit_type = punittype;
1110 best_unit_cost = build_cost;
1111 }
1112 } else if (can_city_build_unit_later(pcity, punittype)) {
1113 /* We first need to develop the tech required by the unit... */
1114
1115 /* Cost (shield equivalent) of gaining these techs. */
1116 /* FIXME? Katvrr advises that this should be weighted more heavily in
1117 * big danger. */
1118 int tech_cost = research_goal_bulbs_required(presearch,
1119 advance_number(punittype->require_advance)) / 4
1120 / city_list_size(pplayer->cities);
1121
1122 /* Contrary to the above, we don't care if walls are actually built
1123 * - we're looking into the future now. */
1124 if (!utype_has_flag(punittype, UTYF_BADCITYDEFENDER)) {
1125 desire *= city_data->wallvalue;
1126 desire /= POWER_FACTOR;
1127 }
1128
1129 /* Yes, there's some similarity with kill_desire(). */
1130 /* TODO: Explain what shield cost has to do with tech want. */
1131 tech_desire[utype_index(punittype)] =
1132 (desire * total_want
1133 / (utype_build_shield_cost(pcity, NULL, punittype) + tech_cost));
1134 }
1135 }
1137
1138 if (best == -1) {
1139 CITY_LOG(LOG_DEBUG, pcity, "Ooops - we cannot build any defender!");
1140 }
1141
1142 if (best_unit_type != NULL) {
1143 if (!walls && !utype_has_flag(best_unit_type, UTYF_BADCITYDEFENDER)) {
1144 best *= city_data->wallvalue;
1145 best /= POWER_FACTOR;
1146 }
1147 } else {
1148 best_unit_cost = 100; /* Building impossible is considered costly.
1149 * This should increase want for tech providing
1150 * first defender type. */
1151 }
1152
1153 if (best <= 0) {
1154 best = 1; /* Avoid division by zero below. */
1155 }
1156
1157 /* Update tech_want for appropriate techs for units we want to build. */
1158 simple_ai_unit_type_iterate(punittype) {
1159 if (tech_desire[utype_index(punittype)] > 0) {
1160 /* TODO: Document or fix the algorithm below. I have no idea why
1161 * it is written this way, and the results seem strange to me. - Per */
1162 adv_want desire = tech_desire[utype_index(punittype)] * best_unit_cost / best;
1163
1164 plr_data->tech_want[advance_index(punittype->require_advance)]
1165 += desire;
1166 TECH_LOG(ait, LOG_DEBUG, pplayer, punittype->require_advance,
1167 "+ " ADV_WANT_PRINTF " for %s to defend %s",
1168 desire,
1169 utype_rule_name(punittype),
1170 city_name_get(pcity));
1171 }
1173
1174 if (!best_unit_type) {
1175 return FALSE;
1176 }
1177
1178 choice->value.utype = best_unit_type;
1179 choice->want = danger; /* FIXME: Not 'total_want' because of the way callers
1180 * are constructed. They may overwrite this value,
1181 * and then the value will NOT contain 'extra_want'.
1182 * Later we may add 'extra_want', and don't want it
1183 * already included in case this was NOT overwritten. */
1184 choice->type = CT_DEFENDER;
1185
1186 return TRUE;
1187}
1188
1189/**********************************************************************/
1206static void process_attacker_want(struct ai_type *ait,
1207 struct city *pcity,
1208 adv_want value,
1209 const struct unit_type *victim_unit_type,
1210 struct player *victim_player,
1211 int veteran, struct tile *ptile,
1212 struct adv_choice *best_choice,
1213 struct pf_map *ferry_map,
1214 struct unit *boat,
1215 const struct unit_type *boattype)
1216{
1217 struct player *pplayer = city_owner(pcity);
1218 const struct research *presearch = research_get(pplayer);
1219 /* The enemy city. acity == NULL means stray enemy unit */
1220 struct city *acity = tile_city(ptile);
1221 struct pf_parameter parameter;
1222 struct pf_map *pfm;
1223 struct pf_position pos;
1224 const struct unit_type *orig_utype = best_choice->value.utype;
1225 int victim_count = 1;
1226 int needferry = 0;
1227 bool unhap = dai_assess_military_unhappiness(pcity);
1228 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
1229 const struct civ_map *nmap = &(wld.map);
1230
1231 /* Has to be initialized to make gcc happy */
1232 struct ai_city *acity_data = NULL;
1233
1234 if (acity != NULL) {
1235 acity_data = def_ai_city_data(acity, ait);
1236 }
1237
1238 if (utype_class(orig_utype)->adv.sea_move == MOVE_NONE
1239 && !boat && boattype) {
1240 /* Cost of ferry */
1241 needferry = utype_build_shield_cost(pcity, NULL, boattype);
1242 }
1243
1244 if (!is_stack_vulnerable(ptile)) {
1245 /* If it is a city, a fortress or an air base,
1246 * we may have to whack it many times */
1247 victim_count += unit_list_size(ptile->units);
1248 }
1249
1250 simple_ai_unit_type_iterate(punittype) {
1251 Tech_type_id tech_req = advance_number(punittype->require_advance);
1252 int tech_dist = research_goal_unknown_techs(presearch, tech_req);
1253
1254 if (dai_can_unit_type_follow_unit_type(punittype, orig_utype, ait)
1255 && is_native_near_tile(&(wld.map), utype_class(punittype), ptile)
1256 && (U_NOT_OBSOLETED == punittype->obsoleted_by
1257 || !can_city_build_unit_direct(pcity, punittype->obsoleted_by))
1258 && punittype->attack_strength > 0 /* or we'll get SIGFPE */) {
1259 /* Values to be computed */
1260 adv_want desire;
1261 adv_want want;
1262 int move_time;
1263 int vuln;
1264 int veteran_level
1266 &(const struct req_context) {
1267 .player = pplayer,
1268 .city = pcity,
1269 .tile = city_tile(pcity),
1270 .unittype = punittype,
1271 },
1272 NULL,
1273 EFT_VETERAN_BUILD);
1274 /* Cost (shield equivalent) of gaining these techs. */
1275 /* FIXME? Katvrr advises that this should be weighted more heavily in big
1276 * danger. */
1277 int tech_cost = research_goal_bulbs_required(presearch, tech_req) / 4
1278 / city_list_size(pplayer->cities);
1279 int bcost_balanced = build_cost_balanced(punittype);
1280 /* See description of kill_desire() for info about this variables. */
1281 int bcost = utype_build_shield_cost(pcity, NULL, punittype);
1284 punittype->hp);
1285
1286 /* Take into account reinforcements strength */
1287 if (acity) {
1288 attack += acity_data->attack;
1289 }
1290
1291 if (attack == 0) {
1292 /* Yes, it can happen that a military unit has attack = 1,
1293 * for example militia with HP = 1 (in civ1 ruleset). */
1294 continue;
1295 }
1296
1297 attack *= attack;
1298
1299 pft_fill_utype_parameter(&parameter, nmap, punittype, city_tile(pcity),
1300 pplayer);
1301 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1302 pfm = pf_map_new(&parameter);
1303
1304 /* Set the move_time appropriately. */
1305 move_time = -1;
1306 if (NULL != ferry_map) {
1307 struct tile *dest_tile;
1308
1309 if (find_beachhead(pplayer, ferry_map, ptile, punittype,
1310 &dest_tile, NULL)
1311 && pf_map_position(ferry_map, dest_tile, &pos)) {
1312 move_time = pos.turn;
1313 dest_tile = pf_map_parameter(ferry_map)->start_tile;
1314 pf_map_tiles_iterate(pfm, atile, FALSE) {
1315 if (atile == dest_tile) {
1317 move_time += pos.turn;
1318 break;
1319 } else if (atile == ptile) {
1320 /* Reaching directly seems better. */
1322 move_time = pos.turn;
1323 break;
1324 }
1326 }
1327 }
1328
1329 if (-1 == move_time) {
1330 if (pf_map_position(pfm, ptile, &pos)) {
1331 move_time = pos.turn;
1332 } else {
1333 pf_map_destroy(pfm);
1334 continue;
1335 }
1336 }
1337 pf_map_destroy(pfm);
1338
1339 /* Estimate strength of the enemy. */
1340
1341 if (victim_unit_type) {
1342 vuln = unittype_def_rating_squared(punittype, victim_unit_type,
1343 victim_player,
1344 ptile, FALSE, veteran);
1345 } else {
1346 vuln = 0;
1347 }
1348
1349 /* Not bothering to s/!vuln/!pdef/ here for the time being. -- Syela
1350 * (this is noted elsewhere as terrible bug making warships yoyoing)
1351 * as the warships will go to enemy cities hoping that the enemy builds
1352 * something for them to kill. */
1353 if (vuln == 0
1354 && (utype_class(punittype)->adv.land_move == MOVE_NONE
1355 || 0 < utype_fuel(punittype))) {
1356 desire = 0;
1357
1358 } else {
1359 if (acity
1360 && utype_can_take_over(punittype)
1361 && acity_data->invasion.attack > 0
1362 && acity_data->invasion.occupy == 0) {
1363 int owner_size = city_list_size(city_owner(acity)->cities);
1364 float finishing_factor = 1;
1365
1366 if (owner_size <= FINISH_HIM_CITY_COUNT) {
1367 finishing_factor = (2 - (float)owner_size / FINISH_HIM_CITY_COUNT);
1368 }
1369 desire = CITY_CONQUEST_WORTH(acity, acity_data) * 10 * finishing_factor;
1370 } else {
1371 desire = 0;
1372 }
1373
1374 if (!acity) {
1375 desire = kill_desire(value, attack, bcost, vuln, victim_count);
1376 } else {
1377 adv_want kd;
1378 int city_attack = acity_data->attack * acity_data->attack;
1379
1380 /* See aiunit.c:find_something_to_kill() for comments. */
1381 kd = kill_desire(value, attack,
1382 (bcost + acity_data->bcost), vuln,
1383 victim_count);
1384
1385 if (value * city_attack > acity_data->bcost * vuln) {
1386 kd -= kill_desire(value, city_attack,
1387 acity_data->bcost, vuln,
1388 victim_count);
1389 }
1390
1391 desire = MAX(desire, kd);
1392 }
1393 }
1394
1395 desire -= tech_cost * SHIELD_WEIGHTING;
1396 /* We can be possibly making some people of our homecity unhappy - then
1397 * we don't want to travel too far away to our victims. */
1398 /* TODO: Unify the 'unhap' dependency to some common function. */
1399 desire -= move_time * (unhap ? SHIELD_WEIGHTING + 2 * TRADE_WEIGHTING
1401
1402 want = military_amortize(pplayer, pcity, desire, MAX(1, move_time),
1403 bcost_balanced + needferry);
1404
1405 if (want > 0) {
1406 if (tech_dist > 0) {
1407 /* This is a future unit, tell the scientist how much we need it */
1408 plr_data->tech_want[advance_index(punittype->require_advance)]
1409 += want;
1410 TECH_LOG(ait, LOG_DEBUG, pplayer, punittype->require_advance,
1411 "+ " ADV_WANT_PRINTF " for %s vs %s(%d,%d)",
1412 want,
1413 utype_rule_name(punittype),
1414 (acity ? city_name_get(acity) : utype_rule_name(victim_unit_type)),
1415 TILE_XY(ptile));
1416 } else if (want > best_choice->want) {
1417 const struct impr_type *impr_req;
1418
1419 if (can_city_build_unit_now(pcity, punittype)) {
1420 /* This is a real unit and we really want it */
1421
1422 CITY_LOG(LOG_DEBUG, pcity, "overriding %s(" ADV_WANT_PRINTF
1423 ") with %s(" ADV_WANT_PRINTF ")"
1424 " [attack=%d,value=" ADV_WANT_PRINTF
1425 ",move_time=%d,vuln=%d,bcost=%d]",
1426 utype_rule_name(best_choice->value.utype),
1427 best_choice->want,
1428 utype_rule_name(punittype),
1429 want,
1430 attack, value, move_time, vuln, bcost);
1431
1432 best_choice->value.utype = punittype;
1433 best_choice->want = want;
1434 best_choice->type = CT_ATTACKER;
1435 } else if (!((impr_req = utype_needs_improvement(punittype,
1436 pcity)))) {
1437 CITY_LOG(LOG_DEBUG, pcity, "cannot build unit %s",
1438 utype_rule_name(punittype));
1439 } else if (can_city_build_improvement_now(pcity, impr_req)) {
1440 /* Building this unit requires a specific type of improvement.
1441 * So we build this improvement instead. This may not be the
1442 * best behavior. */
1443 CITY_LOG(LOG_DEBUG, pcity, "building %s to build unit %s",
1444 improvement_rule_name(impr_req),
1445 utype_rule_name(punittype));
1446 best_choice->value.building = impr_req;
1447 best_choice->want = want;
1448 best_choice->type = CT_BUILDING;
1449 } else {
1450 /* This should never happen? */
1451 CITY_LOG(LOG_DEBUG, pcity, "cannot build %s or unit %s",
1452 improvement_rule_name(impr_req),
1453 utype_rule_name(punittype));
1454 }
1455 }
1456 }
1457 }
1459}
1460
1461/**********************************************************************/
1472static struct adv_choice *kill_something_with(struct ai_type *ait,
1473 struct player *pplayer,
1474 struct city *pcity, struct unit *myunit,
1475 struct adv_choice *choice)
1476{
1477 /* Our attack rating (with reinforcements) */
1478 int attack;
1479 /* Benefit from fighting the target */
1480 adv_want benefit;
1481 /* Defender of the target city/tile */
1482 struct unit *pdef;
1483 const struct unit_type *def_type;
1484 struct player *def_owner;
1485 int def_vet; /* Is the defender veteran? */
1486 /* Target coordinates */
1487 struct tile *ptile;
1488 /* Our transport */
1489 struct unit *ferryboat;
1490 /* Our target */
1491 struct city *acity;
1492 /* Type of the boat (real or a future one) */
1493 const struct unit_type *boattype;
1494 struct pf_map *ferry_map = NULL;
1495 int move_time;
1496 struct adv_choice *best_choice;
1497 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1498 struct ai_city *acity_data;
1499 struct civ_map *nmap = &(wld.map);
1500
1501 best_choice = adv_new_choice();
1502 best_choice->value.utype = unit_type_get(myunit);
1503 best_choice->type = CT_ATTACKER;
1504 adv_choice_set_use(best_choice, "attacker");
1505
1506 fc_assert_ret_val(is_military_unit(myunit) && !utype_fuel(unit_type_get(myunit)), choice);
1507
1508 if (city_data->danger != 0 && assess_defense(ait, pcity) == 0) {
1509 /* Defense comes first! */
1510 goto cleanup;
1511 }
1512
1513 best_choice->want = find_something_to_kill(ait, pplayer, myunit, &ptile, NULL,
1514 &ferry_map, &ferryboat,
1515 &boattype, &move_time);
1516 if (NULL == ptile
1517 || ptile == unit_tile(myunit)
1518 || !can_unit_attack_tile(myunit, NULL, ptile)) {
1519 goto cleanup;
1520 }
1521
1522 acity = tile_city(ptile);
1523
1524 if (myunit->id != 0) {
1525 log_error("%s(): non-virtual unit!", __FUNCTION__);
1526 goto cleanup;
1527 }
1528
1529 attack = adv_unit_att_rating(myunit);
1530 if (acity) {
1531 acity_data = def_ai_city_data(acity, ait);
1532 attack += acity_data->attack;
1533 }
1534 attack *= attack;
1535
1536 if (NULL != acity) {
1537 /* Rating of enemy defender */
1538 int vulnerability;
1539
1540 if (!POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, city_owner(acity))) {
1541 /* Not a valid target */
1542 goto cleanup;
1543 }
1544
1545 def_type = dai_choose_defender_versus(acity, myunit);
1546 def_owner = city_owner(acity);
1547 if (1 < move_time && def_type) {
1548 def_vet = city_production_unit_veteran_level(acity, def_type);
1549 vulnerability = unittype_def_rating_squared(unit_type_get(myunit), def_type,
1550 city_owner(acity), ptile,
1551 FALSE, def_vet);
1552 benefit = utype_build_shield_cost_base(def_type);
1553 } else {
1554 vulnerability = 0;
1555 benefit = 0;
1556 def_vet = 0;
1557 }
1558
1559 pdef = get_defender(nmap, myunit, ptile);
1560 if (pdef) {
1562 city_owner(acity), ptile, FALSE,
1563 pdef->veteran);
1564 if (vulnerability < m) {
1565 benefit = unit_build_shield_cost_base(pdef);
1566 def_vet = pdef->veteran;
1567 def_type = unit_type_get(pdef);
1568 def_owner = unit_owner(pdef);
1569 }
1570 }
1571 if (unit_can_take_over(myunit) || acity_data->invasion.occupy > 0) {
1572 /* Bonus for getting the city */
1573 int owner_size = city_list_size(city_owner(acity)->cities);
1574 float finishing_factor = 1;
1575
1576 if (owner_size <= FINISH_HIM_CITY_COUNT) {
1577 finishing_factor = (2 - (float)owner_size / FINISH_HIM_CITY_COUNT);
1578 }
1579 benefit += CITY_CONQUEST_WORTH(acity, acity_data) * finishing_factor / 3;
1580 }
1581
1582 /* end dealing with cities */
1583 } else {
1584
1585 if (NULL != ferry_map) {
1586 pf_map_destroy(ferry_map);
1587 ferry_map = NULL;
1588 }
1589
1590 pdef = get_defender(nmap, myunit, ptile);
1591 if (!pdef) {
1592 /* Nobody to attack! */
1593 goto cleanup;
1594 }
1595
1596 benefit = unit_build_shield_cost_base(pdef);
1597
1598 def_type = unit_type_get(pdef);
1599 def_vet = pdef->veteran;
1600 def_owner = unit_owner(pdef);
1601 /* end dealing with units */
1602 }
1603
1604 if (NULL == ferry_map) {
1605 process_attacker_want(ait, pcity, benefit, def_type, def_owner,
1606 def_vet, ptile,
1607 best_choice, NULL, NULL, NULL);
1608 } else {
1609 /* Attract a boat to our city or retain the one that's already here */
1610 fc_assert_ret_val(unit_class_get(myunit)->adv.sea_move != MOVE_FULL, choice);
1611 best_choice->need_boat = TRUE;
1612 process_attacker_want(ait, pcity, benefit, def_type, def_owner,
1613 def_vet, ptile,
1614 best_choice, ferry_map, ferryboat, boattype);
1615 }
1616
1617 if (best_choice->want > choice->want) {
1618 /* We want attacker more than what we have selected before */
1619 adv_free_choice(choice);
1620 choice = best_choice;
1621 CITY_LOG(LOG_DEBUG, pcity, "kill_something_with()"
1622 " %s has chosen attacker, %s, want=" ADV_WANT_PRINTF,
1623 city_name_get(pcity),
1624 utype_rule_name(best_choice->value.utype),
1625 best_choice->want);
1626
1627 if (NULL != ferry_map && !ferryboat) { /* need a new ferry */
1628 /* We might need a new boat even if there are boats free,
1629 * if they are blockaded or in inland seas*/
1630 fc_assert_ret_val(unit_class_get(myunit)->adv.sea_move != MOVE_FULL, choice);
1631 if (dai_choose_role_unit(ait, pplayer, pcity, choice, CT_ATTACKER,
1632 L_FERRYBOAT, choice->want, TRUE)
1633 && dai_is_ferry_type(choice->value.utype, ait)) {
1634#ifdef FREECIV_DEBUG
1635 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, NULL);
1636
1637 log_debug("kill_something_with() %s has chosen attacker ferry, "
1638 "%s, want=" ADV_WANT_PRINTF ", %d of %d free",
1639 city_name_get(pcity),
1640 utype_rule_name(choice->value.utype),
1641 choice->want,
1642 ai->stats.available_boats, ai->stats.boats);
1643#endif /* FREECIV_DEBUG */
1644
1645 adv_choice_set_use(choice, "attacker ferry");
1646 } /* else can not build ferries yet */
1647 }
1648 }
1649
1650cleanup:
1651 if (best_choice != choice) {
1652 /* It was not taken to use.
1653 * This hackery needed since 'goto cleanup:' might skip
1654 * sensible points to do adv_free_choice(). */
1655 adv_free_choice(best_choice);
1656 }
1657 if (NULL != ferry_map) {
1658 pf_map_destroy(ferry_map);
1659 }
1660
1661 return choice;
1662}
1663
1664/**********************************************************************/
1670static void dai_unit_consider_bodyguard(struct ai_type *ait,
1671 struct city *pcity,
1672 struct unit_type *punittype,
1673 struct adv_choice *choice)
1674{
1675 if (choice->want < DAI_WANT_MILITARY_EMERGENCY) {
1676 struct player *pplayer = city_owner(pcity);
1677 struct unit *aunit = NULL;
1678 struct city *acity = NULL;
1679 struct unit *virtualunit
1680 = unit_virtual_create(pplayer, pcity, punittype,
1682 punittype));
1683 const adv_want want = look_for_charge(ait, pplayer, virtualunit,
1684 &aunit, &acity);
1685
1686 if (want > choice->want) {
1687 choice->want = want;
1688 choice->value.utype = punittype;
1689 choice->type = CT_DEFENDER;
1690 adv_choice_set_use(choice, "bodyguard");
1691 }
1692
1693 unit_virtual_destroy(virtualunit);
1694 }
1695}
1696
1697/**********************************************************************/
1705static void adjust_ai_unit_choice(struct city *pcity,
1706 struct adv_choice *choice)
1707{
1709
1710 /* Sanity */
1711 if (!is_unit_choice_type(choice->type)
1712 || utype_has_flag(choice->value.utype, UTYF_CIVILIAN)
1713 || city_production_unit_veteran_level(pcity, choice->value.utype)) {
1714 return;
1715 }
1716
1717 /* N.B.: have to check that we haven't already built the building --mck */
1718 if ((id = dai_find_source_building(pcity, EFT_VETERAN_BUILD,
1719 choice->value.utype)) != B_LAST
1720 && !city_has_building(pcity, improvement_by_number(id))) {
1721 choice->value.building = improvement_by_number(id);
1722 choice->want = choice->want * (0.5 + (ai_trait_get_value(TRAIT_BUILDER,
1723 city_owner(pcity))
1724 / TRAIT_DEFAULT_VALUE / 2));
1725 choice->type = CT_BUILDING;
1726 adv_choice_set_use(choice, "veterancy building");
1727 }
1728}
1729
1730/**********************************************************************/
1736 struct player *pplayer,
1737 struct city *pcity,
1738 const struct civ_map *mamap,
1740{
1741 struct adv_data *ai = adv_data_get(pplayer, NULL);
1742 struct unit_type *punittype;
1743 unsigned int our_def, urgency;
1744 struct tile *ptile = pcity->tile;
1745 struct unit *virtualunit;
1746 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1747 adv_want martial_value = 0;
1748 bool martial_need = FALSE;
1749 struct adv_choice *choice = adv_new_choice();
1750 bool allow_gold_upkeep;
1751
1752 urgency = assess_danger(ait, pcity, mamap, ul_cb);
1753 /* Changing to quadratic to stop AI from building piles
1754 * of small units -- Syela */
1755 /* It has to be AFTER assess_danger thanks to wallvalue. */
1756 our_def = assess_defense_quadratic(ait, pcity);
1757
1758 dai_choose_diplomat_defensive(ait, pplayer, pcity, choice, our_def);
1759
1761 + pcity->feel[CITIZEN_ANGRY][FEELING_NATIONALITY] > 0) {
1762 martial_need = TRUE;
1763 }
1764
1765 if (!martial_need) {
1767 if (pcity->specialists[sp] > 0
1768 && get_specialist_output(pcity, sp, O_LUXURY) > 0) {
1769 martial_need = TRUE;
1770 break;
1771 }
1773 }
1774
1775 if (martial_need
1776 && unit_list_size(pcity->tile->units) < get_city_bonus(pcity, EFT_MARTIAL_LAW_MAX)) {
1777 martial_value = dai_content_effect_value(pplayer, pcity,
1778 get_city_bonus(pcity, EFT_MARTIAL_LAW_EACH),
1779 1, FEELING_FINAL);
1780 }
1781
1782 /* Otherwise no need to defend yet */
1783 if (city_data->danger != 0 || martial_value > 0) {
1784 struct impr_type *pimprove;
1785 int num_defenders = unit_list_size(ptile->units);
1786 int wall_id, danger;
1787 bool build_walls = TRUE;
1788 bool def_unit_selected = FALSE;
1789 int qdanger = city_data->danger * city_data->danger;
1790
1791 if (qdanger <= 0) {
1792 /* We only need these defenders because of Martial Law value */
1793 danger = 0;
1794 build_walls = FALSE; /* Walls don't provide Martial Law */
1795 } else {
1796 /* First determine the danger. It is measured in percents of our
1797 * defensive strength, capped at 200 + urgency */
1798 if (qdanger >= our_def) {
1799 if (urgency == 0) {
1800 /* Don't waste money */
1801 danger = 100;
1802 } else if (our_def == 0) {
1803 danger = 200 + urgency;
1804 } else {
1805 danger = MIN(200, 100 * qdanger / our_def) + urgency;
1806 }
1807 } else {
1808 danger = 100 * qdanger / our_def;
1809 }
1810
1811 if (pcity->surplus[O_SHIELD] <= 0 && our_def != 0) {
1812 /* Won't be able to support anything */
1813 danger = 0;
1814 }
1815 }
1816
1817 CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d urgency=%d danger=%d num_def=%d "
1818 "our_def=%d", urgency, danger, num_defenders, our_def);
1819
1820 if (our_def == 0 && danger > 0) {
1821 /* Build defensive unit first! Walls will help nothing if there's
1822 * nobody behind them. */
1823 if (dai_process_defender_want(ait, pplayer, pcity, danger, choice,
1824 martial_value)) {
1825 choice->want = DAI_WANT_BELOW_MIL_EMERGENCY + danger;
1826 adv_choice_set_use(choice, "first defender");
1827 build_walls = FALSE;
1828 def_unit_selected = TRUE;
1829
1830 CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants first defender with " ADV_WANT_PRINTF,
1831 choice->want);
1832 }
1833 }
1834 if (build_walls) {
1835 /* FIXME: 1. Does not consider what kind of future danger is likely, so
1836 * may build SAM batteries when enemy has only land units. */
1837 /* We will build walls if we can and want and (have "enough" defenders or
1838 * can just buy the walls straight away) */
1839
1840 /* HACK: This needs changing if multiple improvements provide
1841 * this effect. */
1842 wall_id = dai_find_source_building(pcity, EFT_DEFEND_BONUS, NULL);
1843 pimprove = improvement_by_number(wall_id);
1844
1845 if (wall_id != B_LAST
1846 && pcity->server.adv->building_want[wall_id] != 0 && our_def != 0
1847 && can_city_build_improvement_now(pcity, pimprove)
1848 && (danger < 101 || num_defenders > 1
1849 || (city_data->grave_danger == 0
1850 && pplayer->economic.gold
1851 > impr_buy_gold_cost(pcity, pimprove, pcity->shield_stock)))
1852 && ai_fuzzy(pplayer, TRUE)) {
1853 if (pcity->server.adv->building_want[wall_id] > 0) {
1854 /* NB: great wall is under domestic */
1855 choice->value.building = pimprove;
1856 /* building_want is hacked by assess_danger */
1857 choice->want = pcity->server.adv->building_want[wall_id];
1858 choice->want = choice->want * (0.5 + (ai_trait_get_value(TRAIT_BUILDER, pplayer)
1859 / TRAIT_DEFAULT_VALUE / 2));
1860 if (urgency == 0 && choice->want > DAI_WANT_BELOW_MIL_EMERGENCY) {
1862 }
1863 choice->type = CT_BUILDING;
1864 adv_choice_set_use(choice, "defense building");
1865 CITY_LOG(LOG_DEBUG, pcity,
1866 "m_a_c_d wants defense building with " ADV_WANT_PRINTF,
1867 choice->want);
1868 } else {
1869 build_walls = FALSE;
1870 }
1871 } else {
1872 build_walls = FALSE;
1873 }
1874 }
1875
1876 /* If our choice isn't defender unit already, consider one */
1877 if (!def_unit_selected) {
1878 if ((danger > 0 && num_defenders <= urgency) || martial_value > 0) {
1879 struct adv_choice uchoice;
1880
1881 adv_init_choice(&uchoice);
1882
1883 /* Consider building defensive units */
1884 if (dai_process_defender_want(ait, pplayer, pcity, danger, &uchoice,
1885 martial_value)) {
1886 /* Potential defender found */
1887 if (urgency == 0
1888 && uchoice.value.utype->defense_strength == 1) {
1889 /* FIXME: check other reqs (unit class?) */
1890 if (get_city_bonus(pcity, EFT_HP_REGEN) > 0) {
1891 /* Unlikely */
1892 uchoice.want = MIN(49, danger);
1893 } else {
1894 uchoice.want = MIN(25, danger);
1895 }
1896 } else {
1897 uchoice.want = danger;
1898 }
1899
1900 uchoice.want += martial_value;
1901 if (danger > 0) {
1902 adv_choice_set_use(&uchoice, "defender");
1903 } else {
1904 adv_choice_set_use(&uchoice, "police");
1905 }
1906
1907 if (!build_walls || uchoice.want > choice->want) {
1908 adv_choice_copy(choice, &uchoice);
1909 }
1910 adv_deinit_choice(&uchoice);
1911
1912 CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants %s with desire " ADV_WANT_PRINTF,
1913 utype_rule_name(choice->value.utype),
1914 choice->want);
1915 } else {
1916 CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d cannot select defender");
1917 }
1918 } else {
1919 CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d does not want defenders");
1920 }
1921 }
1922 } /* ok, don't need to defend */
1923
1924 if (pcity->surplus[O_SHIELD] <= 0
1926 || pcity->id == ai->wonder_city) {
1927 /* Things we consider below are not life-saving so we don't want to
1928 * build them if our populace doesn't feel like it */
1929 return choice;
1930 }
1931
1932 if (pplayer->economic.tax <= 50 || city_total_unit_gold_upkeep(pcity) <= 0) {
1933 /* Always allow one unit with real gold upkeep (after EFT_UNIT_UPKEEP_FREE_PER_CITY)
1934 * Allow more if economics is so strong that we have not increased taxes. */
1935 allow_gold_upkeep = TRUE;
1936 } else {
1937 allow_gold_upkeep = FALSE;
1938 }
1939
1940 /* Consider making a land bodyguard */
1941 punittype = dai_choose_bodyguard(ait, pcity, TC_LAND, L_DEFEND_GOOD,
1942 allow_gold_upkeep);
1943 if (punittype) {
1944 dai_unit_consider_bodyguard(ait, pcity, punittype, choice);
1945 }
1946
1947 /* If we are in severe danger, don't consider attackers. This is probably
1948 too general. In many cases we will want to buy attackers to counterattack.
1949 -- Per */
1950 if (choice->want - martial_value >= DAI_WANT_MILITARY_EMERGENCY
1951 && city_data->grave_danger > 0) {
1952 CITY_LOG(LOGLEVEL_BUILD, pcity,
1953 "severe danger (want " ADV_WANT_PRINTF "), force defender",
1954 choice->want);
1955 return choice;
1956 }
1957
1958 /* Consider making an offensive diplomat */
1959 dai_choose_diplomat_offensive(ait, pplayer, pcity, choice);
1960
1961 /* Consider making a sea bodyguard */
1962 punittype = dai_choose_bodyguard(ait, pcity, TC_OCEAN, L_DEFEND_GOOD,
1963 allow_gold_upkeep);
1964 if (punittype) {
1965 dai_unit_consider_bodyguard(ait, pcity, punittype, choice);
1966 }
1967
1968 /* Consider making an airplane */
1969 (void) dai_choose_attacker_air(ait, pplayer, pcity, choice, allow_gold_upkeep);
1970
1971 /* Consider making a paratrooper */
1972 dai_choose_paratrooper(ait, pplayer, pcity, choice, allow_gold_upkeep);
1973
1974 /* Check if we want a sailing attacker. Have to put sailing first
1975 before we mung the seamap */
1976 punittype = dai_choose_attacker(ait, pcity, TC_OCEAN, allow_gold_upkeep);
1977 if (punittype) {
1978 virtualunit = unit_virtual_create(
1979 pplayer, pcity, punittype,
1980 city_production_unit_veteran_level(pcity, punittype));
1981 choice = kill_something_with(ait, pplayer, pcity, virtualunit, choice);
1982 unit_virtual_destroy(virtualunit);
1983 }
1984
1985 /* Consider a land attacker or a ferried land attacker
1986 * (in which case, we might want a ferry before an attacker)
1987 */
1988 punittype = dai_choose_attacker(ait, pcity, TC_LAND, allow_gold_upkeep);
1989 if (punittype) {
1990 virtualunit = unit_virtual_create(pplayer, pcity, punittype, 1);
1991 choice = kill_something_with(ait, pplayer, pcity, virtualunit, choice);
1992 unit_virtual_destroy(virtualunit);
1993 }
1994
1995 /* Consider a hunter */
1996 dai_hunter_choice(ait, pplayer, pcity, choice, allow_gold_upkeep);
1997
1998 /* Consider veteran level enhancing buildings before non-urgent units */
1999 adjust_ai_unit_choice(pcity, choice);
2000
2001 if (choice->want <= 0) {
2002 CITY_LOG(LOGLEVEL_BUILD, pcity, "military advisor has no advice");
2003 } else {
2004 CITY_LOG(LOGLEVEL_BUILD, pcity,
2005 "military advisor choice: %s (want " ADV_WANT_PRINTF ")",
2006 adv_choice_rule_name(choice),
2007 choice->want);
2008 }
2009
2010 return choice;
2011}
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:2475
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:488
#define action_enabler_list_iterate_end
Definition actions.h:457
#define action_by_result_iterate_end
Definition actions.h:492
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:455
#define TRADE_WEIGHTING
Definition advbuilding.h:21
#define SHIELD_WEIGHTING
Definition advbuilding.h:20
void adv_deinit_choice(struct adv_choice *choice)
Definition advchoice.c:46
void adv_init_choice(struct adv_choice *choice)
Definition advchoice.c:31
void adv_free_choice(struct adv_choice *choice)
Definition advchoice.c:71
struct adv_choice * adv_new_choice(void)
Definition advchoice.c:59
bool is_unit_choice_type(enum choice_type type)
Definition advchoice.c:115
const char * adv_choice_rule_name(const struct adv_choice *choice)
Definition advchoice.c:124
#define adv_choice_set_use(_choice, _use)
Definition advchoice.h:85
static void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src)
Definition advchoice.h:79
@ CT_DEFENDER
Definition advchoice.h:41
@ CT_ATTACKER
Definition advchoice.h:40
@ CT_BUILDING
Definition advchoice.h:38
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:593
bool adv_is_player_dangerous(struct player *pplayer, struct player *aplayer)
Definition advdata.c:1087
int adv_unittype_att_rating(const struct unit_type *punittype, int veteran, int moves_left, int hp)
Definition advgoto.c:374
int adv_unit_att_rating(const struct unit *punit)
Definition advgoto.c:385
#define POWER_DIVIDER
Definition advtools.h:32
#define ADV_WANTS_EQ(_w1, _w2)
Definition advtools.h:23
bool dai_choose_attacker_air(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
Definition aiair.c:584
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition aidata.c:308
void dai_choose_diplomat_defensive(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, int def)
Definition aidiplomat.c:115
void dai_choose_diplomat_offensive(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice)
Definition aidiplomat.c:164
bool dai_is_ferry_type(const struct unit_type *pferry, struct ai_type *ait)
Definition aiferry.c:151
void dai_hunter_choice(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
Definition aihunt.c:253
#define TECH_LOG(ait, loglevel, pplayer, padvance, msg,...)
Definition ailog.h:36
void dai_choose_paratrooper(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, bool allow_gold_upkeep)
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
struct unit_type * dai_wants_defender_against(struct ai_type *ait, struct player *pplayer, struct city *pcity, const struct unit_type *att, int want)
Definition aitech.c:388
bool dai_assess_military_unhappiness(struct city *pcity)
Definition aitools.c:1397
bool dai_choose_role_unit(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, enum choice_type type, int role, int want, bool need_boat)
Definition aitools.c:1326
adv_want military_amortize(struct player *pplayer, struct city *pcity, adv_want value, int delay, int build_cost)
Definition aitools.c:118
#define DAI_WANT_BELOW_MIL_EMERGENCY
Definition aitools.h:31
#define DAI_WANT_MILITARY_EMERGENCY
Definition aitools.h:32
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Definition aitraits.c:68
adv_want kill_desire(adv_want benefit, int attack, int loss, int vuln, int victim_count)
Definition aiunit.c:340
int unittype_def_rating_squared(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Definition aiunit.c:302
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition aiunit.c:2894
int build_cost_balanced(const struct unit_type *punittype)
Definition aiunit.c:248
adv_want look_for_charge(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
Definition aiunit.c:713
adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile **pdest_tile, struct pf_path **ppath, struct pf_map **pferrymap, struct unit **pferryboat, const struct unit_type **pboattype, int *pmove_time)
Definition aiunit.c:1134
bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
Definition aiunit.c:833
bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map, struct tile *dest_tile, const struct unit_type *cargo_type, struct tile **ferry_dest, struct tile **beachhead_tile)
Definition aiunit.c:1059
#define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer)
Definition aiunit.h:72
#define simple_ai_unit_type_iterate_end
Definition aiunit.h:151
#define simple_ai_unit_type_iterate(_ut)
Definition aiunit.h:145
bool can_city_build_unit_later(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:946
bool city_has_building(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:1212
const char * city_name_get(const struct city *pcity)
Definition city.c:1111
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:789
bool can_city_build_improvement_now(const struct city *pcity, const struct impr_type *pimprove)
Definition city.c:839
bool city_got_defense_effect(const struct city *pcity, const struct unit_type *attacker)
Definition city.c:1565
bool can_city_build_unit_direct(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:888
int city_total_unit_gold_upkeep(const struct city *pcity)
Definition city.c:1194
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:928
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
@ CITIZEN_ANGRY
Definition city.h:263
@ CITIZEN_UNHAPPY
Definition city.h:262
#define city_owner(_pcity_)
Definition city.h:543
#define city_list_iterate_end
Definition city.h:490
@ FEELING_EFFECT
Definition city.h:273
@ FEELING_FINAL
Definition city.h:276
@ FEELING_NATIONALITY
Definition city.h:274
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:928
int get_fortified_defense_power(const struct unit *attacker, struct unit *defender)
Definition combat.c:726
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
Definition combat.c:712
int get_total_attack_power(const struct unit *attacker, const struct unit *defender)
Definition combat.c:567
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:781
void get_modified_firepower(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp)
Definition combat.c:369
#define POWER_FACTOR
Definition combat.h:32
Impr_type_id dai_find_source_building(struct city *pcity, enum effect_type effect_type, const struct unit_type *utype)
Definition daicity.c:2150
adv_want dai_content_effect_value(const struct player *pplayer, const struct city *pcity, int amount, int num_cities, int happiness_step)
Definition daieffects.c:77
void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer, const struct civ_map *dmap)
struct unit_type * dai_choose_defender_versus(struct city *pcity, struct unit *attacker)
Definition daimilitary.c:91
int assess_defense(struct ai_type *ait, struct city *pcity)
static adv_want dai_unit_attack_desirability(struct ai_type *ait, const struct unit_type *punittype)
static bool action_may_happen_unit_on_city(const action_id wanted_action, const struct unit *actor, const struct city *pcity, int turns)
static void dai_reevaluate_building(struct city *pcity, adv_want *value, unsigned int urgency, unsigned int danger, int defense)
static void process_attacker_want(struct ai_type *ait, struct city *pcity, adv_want value, const struct unit_type *victim_unit_type, struct player *victim_player, int veteran, struct tile *ptile, struct adv_choice *best_choice, struct pf_map *ferry_map, struct unit *boat, const struct unit_type *boattype)
static void dai_unit_consider_bodyguard(struct ai_type *ait, struct city *pcity, struct unit_type *punittype, struct adv_choice *choice)
static struct adv_choice * kill_something_with(struct ai_type *ait, struct player *pplayer, struct city *pcity, struct unit *myunit, struct adv_choice *choice)
static void adjust_ai_unit_choice(struct city *pcity, struct adv_choice *choice)
static unsigned int assess_danger(struct ai_type *ait, struct city *pcity, const struct civ_map *dmap, player_unit_list_getter ul_cb)
static struct unit_type * dai_choose_bodyguard(struct ai_type *ait, struct city *pcity, enum terrain_class tc, enum unit_role_id role, bool allow_gold_upkeep)
static adv_want dai_unit_defense_desirability(struct ai_type *ait, const struct unit_type *punittype)
#define CITY_CONQUEST_WORTH(_city_, _data_)
Definition daimilitary.c:76
static unsigned int assess_danger_unit(const struct city *pcity, struct pf_reverse_map *pcity_map, const struct unit *punit, int *move_time)
static enum fc_tristate tactical_req_cb(const struct req_context *context, const struct player *other_player, const struct requirement *req, void *data, int n_data)
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
bool dai_process_defender_want(struct ai_type *ait, struct player *pplayer, struct city *pcity, unsigned int danger, struct adv_choice *choice, adv_want extra_want)
static int base_assess_defense_unit(struct city *pcity, struct unit *punit, bool igwall, bool quadratic, int wall_value)
static int assess_defense_backend(struct ai_type *ait, struct city *pcity, bool igwall)
static struct unit_type * dai_choose_attacker(struct ai_type *ait, struct city *pcity, enum terrain_class tc, bool allow_gold_upkeep)
int assess_defense_unit(struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
struct adv_choice * military_advisor_choose_build(struct ai_type *ait, struct player *pplayer, struct city *pcity, const struct civ_map *mamap, player_unit_list_getter ul_cb)
static int assess_defense_igwall(struct ai_type *ait, struct city *pcity)
struct unit_list *() player_unit_list_getter(struct player *pplayer)
Definition daimilitary.h:34
#define FINISH_HIM_CITY_COUNT
Definition daimilitary.h:32
static void attack(QVariant data1, QVariant data2)
Definition dialogs.cpp:2917
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit * actor
Definition dialogs_g.h:72
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
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 cost
Definition dialogs_g.h:73
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Definition difficulty.c:339
int int id
Definition editgui_g.h:28
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:691
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:789
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, const struct action *paction, enum effect_type effect_type)
Definition effects.c:957
float adv_want
Definition fc_types.h:1206
int Tech_type_id
Definition fc_types.h:347
int Impr_type_id
Definition fc_types.h:346
@ RPT_POSSIBLE
Definition fc_types.h:585
int action_id
Definition fc_types.h:359
#define ADV_WANT_PRINTF
Definition fc_types.h:1207
int Unit_type_id
Definition fc_types.h:352
@ O_SHIELD
Definition fc_types.h:91
@ O_LUXURY
Definition fc_types.h:91
@ O_GOLD
Definition fc_types.h:91
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
static struct tile * pos
Definition finddlg.c:53
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_MAP
Definition handicaps.h:28
@ H_DANGER
Definition handicaps.h:32
@ H_ASSESS_DANGER_LIMITED
Definition handicaps.h:36
struct impr_type * improvement_by_number(const Impr_type_id id)
const char * improvement_rule_name(const struct impr_type *pimprove)
int impr_buy_gold_cost(const struct city *pcity, const struct impr_type *pimprove, int shields_in_stock)
bool is_great_wonder(const struct impr_type *pimprove)
#define B_LAST
Definition improvement.h:42
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_debug(message,...)
Definition log.h:115
@ LOG_DEBUG
Definition log.h:34
@ LOG_VERBOSE
Definition log.h:33
#define log_error(message,...)
Definition log.h:103
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:628
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:316
bool can_attack_from_non_native(const struct unit_type *utype)
Definition movement.c:215
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:450
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:202
#define SINGLE_MOVE
Definition movement.h:24
struct city_list * cities
Definition packhand.c:117
bool pf_reverse_map_unit_position(struct pf_reverse_map *pfrm, const struct unit *punit, struct pf_position *pos)
struct pf_reverse_map * pf_reverse_map_new_for_city(const struct city *pcity, const struct player *attacker, int max_turns, bool omniscient, const struct civ_map *nmap)
const struct pf_parameter * pf_map_parameter(const struct pf_map *pfm)
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
bool pf_map_position(struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
void pf_map_iter_position(struct pf_map *pfm, struct pf_position *pos)
void pf_reverse_map_destroy(struct pf_reverse_map *pfrm)
void pf_map_destroy(struct pf_map *pfm)
#define PF_IMPOSSIBLE_MC
#define pf_map_tiles_iterate(ARG_pfm, NAME_tile, COND_from_start)
#define pf_map_tiles_iterate_end
void pft_fill_utype_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer)
Definition pf_tools.c:826
#define players_iterate_end
Definition player.h:535
#define players_iterate(_pplayer)
Definition player.h:530
static bool player_is_cpuhog(const struct player *pplayer)
Definition player.h:573
enum fc_tristate tri_req_active_turns(int pass, int period, const struct req_context *context, const struct player *other_player, const struct requirement *req)
enum fc_tristate tri_reqs_cb_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, struct requirement_vector *maybe_reqs, req_tester_cb tester, void *data, int n_data)
enum fc_tristate tri_req_active(const struct req_context *context, const struct player *other_player, const struct requirement *req)
enum req_unchanging_status is_req_preventing(const struct req_context *context, const struct player *other_player, const struct requirement *req, enum req_problem_type prob_type)
@ REQUCH_CTRL
int research_goal_unknown_techs(const struct research *presearch, Tech_type_id goal)
Definition research.c:747
int research_goal_bulbs_required(const struct research *presearch, Tech_type_id goal)
Definition research.c:769
struct research * research_get(const struct player *pplayer)
Definition research.c:126
fc_tristate
Definition shared.h:46
@ TRI_YES
Definition shared.h:46
@ TRI_NO
Definition shared.h:46
@ TRI_MAYBE
Definition shared.h:46
#define MIN(x, y)
Definition shared.h:55
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c:217
#define specialist_type_iterate_end
Definition specialist.h:79
#define specialist_type_iterate(sp)
Definition specialist.h:73
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
@ AIT_DANGER
Definition srv_log.h:52
#define LOGLEVEL_BUILD
Definition srv_log.h:34
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
enum server_states server_state(void)
Definition srv_main.c:323
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
adv_want building_want[B_LAST]
Definition infracache.h:32
int wonder_city
Definition advdata.h:50
bool has_diplomat
Definition daicity.h:59
unsigned int danger
Definition daicity.h:47
int bcost
Definition daicity.h:44
unsigned int urgency
Definition daicity.h:49
bool diplomat_threat
Definition daicity.h:58
int wallvalue
Definition daicity.h:51
int attack
Definition daicity.h:44
struct ai_invasion invasion
Definition daicity.h:43
unsigned int grave_danger
Definition daicity.h:48
int attack
Definition daicity.h:30
int occupy
Definition daicity.h:31
struct ai_plr::@267 stats
int boats
Definition aidata.h:79
adv_want tech_want[A_LAST+1]
Definition aidata.h:103
int available_boats
Definition aidata.h:80
Definition ai.h:50
Definition city.h:309
int surplus[O_LAST]
Definition city.h:343
int id
Definition city.h:315
struct adv_city * adv
Definition city.h:435
citizens feel[CITIZEN_LAST][FEELING_LAST]
Definition city.h:321
citizens specialists[SP_MAX]
Definition city.h:324
struct tile * tile
Definition city.h:311
int shield_stock
Definition city.h:355
struct city::@17::@19 server
struct packet_game_info info
Definition game.h:89
int low_firepower_pearl_harbour
int low_firepower_combat_bonus
struct tile * start_tile
struct city_list * cities
Definition player.h:281
struct player_economic economic
Definition player.h:284
struct adv_data * adv
Definition player.h:334
const struct player * player
const struct city * city
struct universal source
Definition tile.h:49
struct unit_list * units
Definition tile.h:57
enum move_level sea_move
Definition unittype.h:150
bool carries_occupiers
Definition aiunit.h:54
bool low_firepower
Definition aiunit.h:51
int max_defense_mp_bonus_pct
Definition unittype.h:560
struct unit_type::@87 adv
int defense_strength
Definition unittype.h:496
int firepower
Definition unittype.h:506
int paratroopers_range
Definition unittype.h:522
int move_rate
Definition unittype.h:497
int scramble_coeff[U_LAST]
Definition unittype.h:564
struct unit_type::@88 cache
bool igwall
Definition unittype.h:555
int attack_strength
Definition unittype.h:495
int defense_mp_bonuses_pct[U_LAST]
Definition unittype.h:563
Definition unit.h:138
int id
Definition unit.h:145
int hp
Definition unit.h:151
struct tile * tile
Definition unit.h:140
int veteran
Definition unit.h:152
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define fc__fallthrough
Definition support.h:109
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
tech_req
Definition tech.h:110
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define TILE_XY(ptile)
Definition tile.h:42
#define TRAIT_DEFAULT_VALUE
Definition traits.h:32
const struct unit_type * utype
Definition fc_types.h:604
const struct impr_type * building
Definition fc_types.h:598
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2427
bool is_military_unit(const struct unit *punit)
Definition unit.c:322
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:331
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1619
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1715
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2411
#define unit_tile(_pu)
Definition unit.h:388
#define unit_owner(_pu)
Definition unit.h:387
#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_has_role(const struct unit_type *punittype, int role)
Definition unittype.c:193
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1520
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1536
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2546
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:459
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:258
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Definition unittype.c:2741
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:270
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1490
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:515
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
#define utype_class(_t_)
Definition unittype.h:736
#define utype_fuel(ptype)
Definition unittype.h:822
@ MOVE_FULL
Definition unittype.h:131
@ MOVE_NONE
Definition unittype.h:131
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:604
#define unit_type_iterate(_p)
Definition unittype.h:838
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:845
#define U_NOT_OBSOLETED
Definition unittype.h:509