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)
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 ***********************************************************************/
15 #include <fc_config.h>
19 #include "bitvector.h"
28 #include "government.h"
41 #include "barbarian.h"
42 #include "citytools.h"
49 #include "unittools.h"
55 #include "autosettlers.h"
56 #include "infracache.h" /* adv_city */
59 #include "handicaps.h"
69 #include "daimilitary.h"
73 /**************************************************************************
74 Return the (untranslated) rule name of the ai_unit_task.
75 You don't have to free the return pointer.
76 **************************************************************************/
77 const char *dai_unit_task_rule_name(const enum ai_unit_task task
)
82 case AIUNIT_AUTO_SETTLER
:
83 return "Auto settler";
84 case AIUNIT_BUILD_CITY
:
86 case AIUNIT_DEFEND_HOME
:
103 /* no default, ensure all types handled somehow */
104 log_error("Unsupported ai_unit_task %d.", task
);
108 /**************************************************************************
109 Return the (untranslated) rule name of the adv_choice.
110 You don't have to free the return pointer.
111 **************************************************************************/
112 const char *dai_choice_rule_name(const struct adv_choice
*choice
)
114 switch (choice
->type
) {
118 return improvement_rule_name(choice
->value
.building
);
122 return utype_rule_name(choice
->value
.utype
);
126 /* no default, ensure all types handled somehow */
127 log_error("Unsupported ai_unit_task %d.", choice
->type
);
131 /**************************************************************************
132 Amortize a want modified by the shields (build_cost) we risk losing.
133 We add the build time of the unit(s) we risk to amortize delay. The
134 build time is calculated as the build cost divided by the production
135 output of the unit's homecity or the city where we want to produce
136 the unit. If the city has less than average shield output, we
137 instead use the average, to encourage long-term thinking.
138 **************************************************************************/
139 int military_amortize(struct player
*pplayer
, struct city
*pcity
,
140 int value
, int delay
, int build_cost
)
142 struct adv_data
*ai
= adv_data_get(pplayer
, NULL
);
143 int city_output
= (pcity
? pcity
->surplus
[O_SHIELD
] : 1);
144 int output
= MAX(city_output
, ai
->stats
.average_production
);
145 int build_time
= build_cost
/ MAX(output
, 1);
151 return amortize(value
, delay
+ build_time
);
154 /**********************************************************************
155 There are some signs that a player might be dangerous: We are at
156 war with him, he has done lots of ignoble things to us, he is an
157 ally of one of our enemies (a ticking bomb to be sure), he is
158 our war target, we don't like him, diplomatic state is neutral
159 or we have case fire.
160 This function is used for example to check if pplayer can leave
161 his city undefended when aplayer's units are near it.
162 ***********************************************************************/
163 void dai_consider_plr_dangerous(struct ai_type
*ait
, struct player
*plr1
,
165 enum override_bool
*result
)
167 struct ai_dip_intel
*adip
;
169 adip
= dai_diplomacy_get(ait
, plr1
, plr2
);
171 if (adip
->countdown
>= 0) {
172 /* Don't trust our war target */
173 *result
= OVERRIDE_TRUE
;
177 /****************************************************************************
178 A helper function for ai_gothere. Estimates the dangers we will
179 be facing at our destination and tries to find/request a bodyguard if
181 ****************************************************************************/
182 static bool dai_gothere_bodyguard(struct ai_type
*ait
,
183 struct unit
*punit
, struct tile
*dest_tile
)
185 struct player
*pplayer
= unit_owner(punit
);
186 unsigned int danger
= 0;
188 struct unit
*guard
= aiguard_guard_of(ait
, punit
);
189 const struct veteran_level
*vlevel
;
190 bool bg_needed
= FALSE
;
192 if (is_barbarian(unit_owner(punit
))) {
193 /* barbarians must have more courage (ie less brains) */
194 aiguard_clear_guard(ait
, punit
);
198 /* Estimate enemy attack power. */
199 unit_list_iterate(dest_tile
->units
, aunit
) {
200 if (POTENTIALLY_HOSTILE_PLAYER(ait
, pplayer
, unit_owner(aunit
))) {
201 danger
+= adv_unit_att_rating(aunit
);
203 } unit_list_iterate_end
;
204 dcity
= tile_city(dest_tile
);
205 if (dcity
&& POTENTIALLY_HOSTILE_PLAYER(ait
, pplayer
, city_owner(dcity
))) {
206 /* Assume enemy will build another defender, add it's attack strength */
207 struct unit_type
*d_type
= dai_choose_defender_versus(dcity
, punit
);
210 /* Enemy really can build something */
212 adv_unittype_att_rating(d_type
, do_make_unit_veteran(dcity
, d_type
),
213 SINGLE_MOVE
, d_type
->hp
);
216 danger
*= POWER_DIVIDER
;
218 /* If we are fast, there is less danger.
219 * FIXME: that assumes that most units have move_rate == SINGLE_MOVE;
220 * not true for all rulesets */
221 danger
/= (unit_type_get(punit
)->move_rate
/ SINGLE_MOVE
);
222 if (unit_has_type_flag(punit
, UTYF_IGTER
)) {
226 vlevel
= utype_veteran_level(unit_type_get(punit
), punit
->veteran
);
227 fc_assert_ret_val(vlevel
!= NULL
, FALSE
);
229 /* We look for the bodyguard where we stand. */
230 if (guard
== NULL
|| unit_tile(guard
) != unit_tile(punit
)) {
231 int my_def
= (punit
->hp
* unit_type_get(punit
)->defense_strength
232 * POWER_FACTOR
* vlevel
->power_fact
/ 100);
234 if (danger
>= my_def
) {
235 UNIT_LOG(LOGLEVEL_BODYGUARD
, punit
,
236 "want bodyguard @(%d, %d) danger=%d, my_def=%d",
237 TILE_XY(dest_tile
), danger
, my_def
);
238 aiguard_request_guard(ait
, punit
);
241 aiguard_clear_guard(ait
, punit
);
244 } else if (guard
!= NULL
) {
248 /* What if we have a bodyguard, but don't need one? */
253 #define LOGLEVEL_GOTHERE LOG_DEBUG
254 /****************************************************************************
255 This is ferry-enabled goto. Should not normally be used for non-ferried
256 units (i.e. planes or ships), use dai_unit_goto instead.
258 Return values: TRUE if got to or next to our destination, FALSE otherwise.
260 TODO: A big one is rendezvous points. When this is implemented, we won't
261 have to be at the coast to ask for a boat to come to us.
262 ****************************************************************************/
263 bool dai_gothere(struct ai_type
*ait
, struct player
*pplayer
,
264 struct unit
*punit
, struct tile
*dest_tile
)
269 if (same_pos(dest_tile
, unit_tile(punit
)) || punit
->moves_left
<= 0) {
274 /* See if we need a bodyguard at our destination */
275 /* FIXME: If bodyguard is _really_ necessary, don't go anywhere */
276 bg_needed
= dai_gothere_bodyguard(ait
, punit
, dest_tile
);
278 if (unit_transported(punit
)
279 || !goto_is_sane(punit
, dest_tile
)) {
280 /* Must go by boat, call an aiferryboat function */
281 if (!aiferry_gobyboat(ait
, pplayer
, punit
, dest_tile
,
287 /* Go where we should be going if we can, and are at our destination
288 * if we are on a ferry */
289 if (goto_is_sane(punit
, dest_tile
) && punit
->moves_left
> 0) {
290 punit
->goto_tile
= dest_tile
;
291 UNIT_LOG(LOGLEVEL_GOTHERE
, punit
, "Walking to (%d,%d)", TILE_XY(dest_tile
));
292 if (!dai_unit_goto(ait
, punit
, dest_tile
)) {
296 /* liable to bump into someone that will kill us. Should avoid? */
298 UNIT_LOG(LOGLEVEL_GOTHERE
, punit
, "Not moving");
302 if (def_ai_unit_data(punit
, ait
)->ferryboat
> 0
303 && !unit_transported(punit
)) {
304 /* We probably just landed, release our boat */
305 aiferry_clear_boat(ait
, punit
);
308 /* Dead unit shouldn't reach this point */
311 return (same_pos(unit_tile(punit
), dest_tile
)
312 || is_tiles_adjacent(unit_tile(punit
), dest_tile
));
315 /**************************************************************************
316 Returns the destination for a unit moving towards a given final destination.
317 That is, it gives a suitable way-point, if necessary.
318 For example, aircraft need these way-points to refuel.
319 **************************************************************************/
320 struct tile
*immediate_destination(struct unit
*punit
,
321 struct tile
*dest_tile
)
323 if (!same_pos(unit_tile(punit
), dest_tile
)
324 && utype_fuel(unit_type_get(punit
))) {
325 struct pf_parameter parameter
;
327 struct pf_path
*path
;
329 struct player
*pplayer
= unit_owner(punit
);
331 pft_fill_unit_parameter(¶meter
, punit
);
332 parameter
.omniscience
= !has_handicap(pplayer
, H_MAP
);
333 pfm
= pf_map_new(¶meter
);
334 path
= pf_map_path(pfm
, punit
->goto_tile
);
337 for (i
= 1; i
< path
->length
; i
++) {
338 if (path
->positions
[i
].tile
== path
->positions
[i
- 1].tile
) {
339 /* The path-finding code advices us to wait there to refuel. */
340 struct tile
*ptile
= path
->positions
[i
].tile
;
342 pf_path_destroy(path
);
347 pf_path_destroy(path
);
349 /* Seems it's the immediate destination */
350 return punit
->goto_tile
;
354 log_verbose("Did not find an air-route for "
355 "%s %s[%d] (%d,%d)->(%d,%d)",
356 nation_rule_name(nation_of_unit(punit
)),
357 unit_rule_name(punit
),
359 TILE_XY(unit_tile(punit
)),
361 /* Prevent take off */
362 return unit_tile(punit
);
365 /* else does not need way-points */
369 /**************************************************************************
370 Log the cost of travelling a path.
371 **************************************************************************/
372 void dai_log_path(struct unit
*punit
,
373 struct pf_path
*path
, struct pf_parameter
*parameter
)
375 const struct pf_position
*last
= pf_path_last_position(path
);
376 const int cc
= PF_TURN_FACTOR
* last
->total_MC
377 + parameter
->move_rate
* last
->total_EC
;
378 const int tc
= cc
/ (PF_TURN_FACTOR
*parameter
->move_rate
);
380 UNIT_LOG(LOG_DEBUG
, punit
, "path L=%d T=%d(%d) MC=%d EC=%d CC=%d",
381 path
->length
- 1, last
->turn
, tc
,
382 last
->total_MC
, last
->total_EC
, cc
);
385 /**************************************************************************
386 Go to specified destination, subject to given PF constraints,
387 but do not disturb existing role or activity
388 and do not clear the role's destination. Return FALSE iff we died.
390 parameter: the PF constraints on the computed path. The unit will move
391 as far along the computed path is it can; the movement code will impose
392 all the real constraints (ZoC, etc).
393 **************************************************************************/
394 bool dai_unit_goto_constrained(struct ai_type
*ait
, struct unit
*punit
,
396 struct pf_parameter
*parameter
)
400 struct pf_path
*path
;
402 UNIT_LOG(LOG_DEBUG
, punit
, "constrained goto to %d,%d", TILE_XY(ptile
));
404 ptile
= immediate_destination(punit
, ptile
);
406 UNIT_LOG(LOG_DEBUG
, punit
, "constrained goto: let's go to %d,%d",
409 if (same_pos(unit_tile(punit
), ptile
)) {
410 /* Not an error; sometimes immediate_destination instructs the unit
411 * to stay here. For example, to refuel.*/
412 UNIT_LOG(LOG_DEBUG
, punit
, "constrained goto: already there!");
413 send_unit_info(NULL
, punit
);
415 } else if (!goto_is_sane(punit
, ptile
)) {
416 UNIT_LOG(LOG_DEBUG
, punit
, "constrained goto: 'insane' goto!");
417 punit
->activity
= ACTIVITY_IDLE
;
418 send_unit_info(NULL
, punit
);
420 } else if(punit
->moves_left
== 0) {
421 UNIT_LOG(LOG_DEBUG
, punit
, "constrained goto: no moves left!");
422 send_unit_info(NULL
, punit
);
426 pfm
= pf_map_new(parameter
);
427 path
= pf_map_path(pfm
, ptile
);
430 dai_log_path(punit
, path
, parameter
);
431 UNIT_LOG(LOG_DEBUG
, punit
, "constrained goto: following path.");
432 alive
= adv_follow_path(punit
, path
, ptile
);
434 UNIT_LOG(LOG_DEBUG
, punit
, "no path to destination");
437 pf_path_destroy(path
);
443 /****************************************************************************
444 Use pathfinding to determine whether a GOTO is possible, considering all
445 aspects of the unit being moved and the terrain under consideration.
446 Don't bother with pathfinding if the unit is already there.
447 ****************************************************************************/
448 bool goto_is_sane(struct unit
*punit
, struct tile
*ptile
)
450 bool can_get_there
= FALSE
;
452 if (same_pos(unit_tile(punit
), ptile
)) {
453 can_get_there
= TRUE
;
455 struct pf_parameter parameter
;
458 pft_fill_unit_attack_param(¶meter
, punit
);
459 pfm
= pf_map_new(¶meter
);
461 if (pf_map_move_cost(pfm
, ptile
) != PF_IMPOSSIBLE_MC
) {
462 can_get_there
= TRUE
;
466 return can_get_there
;
470 * The length of time, in turns, which is long enough to be optimistic
471 * that enemy units will have moved from their current position.
475 /**************************************************************************
476 Set up the constraints on a path for an AI unit.
481 auxiliary data used by the constraints (output)
483 the destination of the unit.
484 For ferries, the destination may be a coastal land tile,
485 in which case the ferry should stop on an adjacent tile.
486 **************************************************************************/
487 void dai_fill_unit_param(struct ai_type
*ait
, struct pf_parameter
*parameter
,
488 struct adv_risk_cost
*risk_cost
,
489 struct unit
*punit
, struct tile
*ptile
)
491 const bool long_path
= LONG_TIME
< (map_distance(unit_tile(punit
),
494 / unit_type_get(punit
)->move_rate
);
495 const bool barbarian
= is_barbarian(unit_owner(punit
));
497 struct unit_ai
*unit_data
= def_ai_unit_data(punit
, ait
);
498 struct player
*pplayer
= unit_owner(punit
);
500 /* This function is now always omniscient and should not be used
501 * for human players any more. */
502 fc_assert(pplayer
->ai_controlled
);
504 /* If a unit is hunting, don't expect it to be a ferry. */
505 is_ferry
= (unit_data
->task
!= AIUNIT_HUNTER
506 && dai_is_ferry(punit
, ait
));
509 /* The destination may be a coastal land tile,
510 * in which case the ferry should stop on an adjacent tile. */
511 pft_fill_unit_overlap_param(parameter
, punit
);
512 } else if (!utype_fuel(unit_type_get(punit
))
513 && is_military_unit(punit
)
514 && (unit_data
->task
== AIUNIT_DEFEND_HOME
515 || unit_data
->task
== AIUNIT_ATTACK
516 || unit_data
->task
== AIUNIT_ESCORT
517 || unit_data
->task
== AIUNIT_HUNTER
)) {
518 /* Use attack movement for defenders and escorts so they can
519 * make defensive attacks */
520 pft_fill_unit_attack_param(parameter
, punit
);
522 pft_fill_unit_parameter(parameter
, punit
);
524 parameter
->omniscience
= !has_handicap(pplayer
, H_MAP
);
526 /* Should we use the risk avoidance code?
527 * The risk avoidance code uses omniscience, so do not use for
528 * human-player units under temporary AI control.
529 * Barbarians bravely/stupidly ignore risks
531 if (!uclass_has_flag(unit_class_get(punit
), UCF_UNREACHABLE
)
533 adv_avoid_risks(parameter
, risk_cost
, punit
, NORMAL_STACKING_FEARFULNESS
);
536 /* Should we absolutely forbid ending a turn on a dangerous tile?
537 * Do not annoy human players by killing their units for them.
538 * For AI units be optimistic; allows attacks across dangerous terrain,
539 * and polar settlements.
540 * TODO: This is compatible with old code,
541 * but probably ought to be more cautious for non military units
543 if (!is_ferry
&& !utype_fuel(unit_type_get(punit
))) {
544 parameter
->get_moves_left_req
= NULL
;
548 /* Move as far along the path to the destination as we can;
549 * that is, ignore the presence of enemy units when computing the
551 * Hopefully, ai_avoid_risks will have produced a path that avoids enemy
552 * ZoCs. Ignoring ZoCs allows us to move closer to a destination
553 * for which there is not yet a clear path.
554 * That is good if the destination is several turns away,
555 * so we can reasonably expect blocking enemy units to move or
556 * be destroyed. But it can be bad if the destination is one turn away
557 * or our destination is far but there are enemy units near us and on the
558 * shortest path to the destination.
560 parameter
->get_zoc
= NULL
;
563 if (unit_has_type_flag(punit
, UTYF_SETTLERS
)) {
564 parameter
->get_TB
= no_fights
;
565 } else if (long_path
&& unit_is_cityfounder(punit
)) {
566 /* Default tile behaviour;
567 * move as far along the path to the destination as we can;
568 * that is, ignore the presence of enemy units when computing the
571 } else if (unit_is_cityfounder(punit
)) {
573 parameter
->get_TB
= no_fights
;
574 } else if (unit_has_type_role(punit
, L_BARBARIAN_LEADER
)) {
576 parameter
->get_TB
= no_fights
;
577 } else if (is_ferry
) {
578 /* Ferries are not warships */
579 parameter
->get_TB
= no_fights
;
580 } else if (is_losing_hp(punit
)) {
581 /* Losing hitpoints over time (helicopter in default rules) */
582 /* Default tile behaviour */
583 } else if (is_military_unit(punit
)
584 || utype_may_act_at_all(unit_type_get(punit
))) {
585 switch (unit_data
->task
) {
586 case AIUNIT_AUTO_SETTLER
:
587 case AIUNIT_BUILD_CITY
:
588 /* Strange, but not impossible */
589 parameter
->get_TB
= no_fights
;
591 case AIUNIT_DEFEND_HOME
:
592 case AIUNIT_ATTACK
: /* Includes spy actions */
597 parameter
->get_TB
= no_intermediate_fights
;
601 parameter
->get_TB
= no_fights
;
604 /* Default tile behaviour */
608 /* Probably an explorer */
609 parameter
->get_TB
= no_fights
;
613 /* Show the destination in the client when watching an AI: */
614 punit
->goto_tile
= ptile
;
618 /**************************************************************************
619 Go to specified destination but do not disturb existing role or activity
620 and do not clear the role's destination. Return FALSE iff we died.
621 **************************************************************************/
622 bool dai_unit_goto(struct ai_type
*ait
, struct unit
*punit
, struct tile
*ptile
)
624 struct pf_parameter parameter
;
625 struct adv_risk_cost risk_cost
;
627 UNIT_LOG(LOG_DEBUG
, punit
, "dai_unit_goto to %d,%d", TILE_XY(ptile
));
628 dai_fill_unit_param(ait
, ¶meter
, &risk_cost
, punit
, ptile
);
630 return dai_unit_goto_constrained(ait
, punit
, ptile
, ¶meter
);
633 /**************************************************************************
634 Adviser task for unit has been changed.
635 **************************************************************************/
636 void dai_unit_new_adv_task(struct ai_type
*ait
, struct unit
*punit
,
637 enum adv_unit_task task
, struct tile
*ptile
)
639 /* Keep ai_unit_task in sync with adv task */
641 case AUT_AUTO_SETTLER
:
642 dai_unit_new_task(ait
, punit
, AIUNIT_AUTO_SETTLER
, ptile
);
645 dai_unit_new_task(ait
, punit
, AIUNIT_BUILD_CITY
, ptile
);
648 dai_unit_new_task(ait
, punit
, AIUNIT_NONE
, ptile
);
653 /**************************************************************************
654 Ensure unit sanity by telling charge that we won't bodyguard it anymore,
655 tell bodyguard it can roam free if our job is done, add and remove city
656 spot reservation, and set destination. If we set a unit to hunter, also
657 reserve its target, and try to load it with cruise missiles or nukes
659 **************************************************************************/
660 void dai_unit_new_task(struct ai_type
*ait
, struct unit
*punit
,
661 enum ai_unit_task task
, struct tile
*ptile
)
663 struct unit
*bodyguard
= aiguard_guard_of(ait
, punit
);
664 struct unit_ai
*unit_data
= def_ai_unit_data(punit
, ait
);
666 /* If the unit is under (human) orders we shouldn't control it.
667 * Allow removal of old role with AIUNIT_NONE. */
668 fc_assert_ret(!unit_has_orders(punit
) || task
== AIUNIT_NONE
);
670 UNIT_LOG(LOG_DEBUG
, punit
, "changing task from %s to %s",
671 dai_unit_task_rule_name(unit_data
->task
),
672 dai_unit_task_rule_name(task
));
674 /* Free our ferry. Most likely it has been done already. */
675 if (task
== AIUNIT_NONE
|| task
== AIUNIT_DEFEND_HOME
) {
676 aiferry_clear_boat(ait
, punit
);
679 if (punit
->activity
== ACTIVITY_GOTO
) {
680 /* It would indicate we're going somewhere otherwise */
681 unit_activity_handling(punit
, ACTIVITY_IDLE
);
684 if (unit_data
->task
== AIUNIT_BUILD_CITY
) {
685 if (punit
->goto_tile
) {
686 citymap_free_city_spot(punit
->goto_tile
, punit
->id
);
688 /* Print error message instead of crashing in citymap_free_city_spot()
689 * This probably means that some city spot reservation has not been
690 * properly cleared; bad for the AI, as it will leave that area
692 log_error("%s was on city founding mission without target tile.",
693 unit_rule_name(punit
));
697 if (unit_data
->task
== AIUNIT_HUNTER
) {
698 /* Clear victim's hunted bit - we're no longer chasing. */
699 struct unit
*target
= game_unit_by_number(unit_data
->target
);
702 BV_CLR(def_ai_unit_data(target
, ait
)->hunted
,
703 player_index(unit_owner(punit
)));
704 UNIT_LOG(LOGLEVEL_HUNT
, target
, "no longer hunted (new task %d, old %d)",
705 task
, unit_data
->task
);
709 aiguard_clear_charge(ait
, punit
);
710 /* Record the city to defend; our goto may be to transport. */
711 if (task
== AIUNIT_DEFEND_HOME
&& ptile
&& tile_city(ptile
)) {
712 aiguard_assign_guard_city(ait
, tile_city(ptile
), punit
);
715 unit_data
->task
= task
;
717 /* Verify and set the goto destination. Eventually this can be a lot more
718 * stringent, but for now we don't want to break things too badly. */
719 punit
->goto_tile
= ptile
; /* May be NULL. */
721 if (unit_data
->task
== AIUNIT_NONE
&& bodyguard
) {
722 dai_unit_new_task(ait
, bodyguard
, AIUNIT_NONE
, NULL
);
725 /* Reserve city spot, _unless_ we want to add ourselves to a city. */
726 if (unit_data
->task
== AIUNIT_BUILD_CITY
&& !tile_city(ptile
)) {
727 citymap_reserve_city_spot(ptile
, punit
->id
);
729 if (unit_data
->task
== AIUNIT_HUNTER
) {
730 /* Set victim's hunted bit - the hunt is on! */
731 struct unit
*target
= game_unit_by_number(unit_data
->target
);
733 fc_assert_ret(target
!= NULL
);
734 BV_SET(def_ai_unit_data(target
, ait
)->hunted
, player_index(unit_owner(punit
)));
735 UNIT_LOG(LOGLEVEL_HUNT
, target
, "is being hunted");
737 /* Grab missiles lying around and bring them along */
738 unit_list_iterate(unit_tile(punit
)->units
, missile
) {
739 if (unit_owner(missile
) == unit_owner(punit
)
740 && def_ai_unit_data(missile
, ait
)->task
!= AIUNIT_ESCORT
741 && !unit_transported(missile
)
742 && unit_owner(missile
) == unit_owner(punit
)
743 && uclass_has_flag(unit_class_get(missile
), UCF_MISSILE
)
744 && can_unit_load(missile
, punit
)) {
745 UNIT_LOG(LOGLEVEL_HUNT
, missile
, "loaded on hunter");
746 dai_unit_new_task(ait
, missile
, AIUNIT_ESCORT
, unit_tile(target
));
747 unit_transport_load_send(missile
, punit
);
749 } unit_list_iterate_end
;
752 /* Map ai tasks to advisor tasks. For most ai tasks there is
753 no advisor, so AUT_NONE is set. */
754 switch(unit_data
->task
) {
755 case AIUNIT_AUTO_SETTLER
:
756 punit
->server
.adv
->task
= AUT_AUTO_SETTLER
;
758 case AIUNIT_BUILD_CITY
:
759 punit
->server
.adv
->task
= AUT_BUILD_CITY
;
762 punit
->server
.adv
->task
= AUT_NONE
;
767 /**************************************************************************
768 Try to make pcity our new homecity. Fails if we can't upkeep it. Assumes
770 **************************************************************************/
771 bool dai_unit_make_homecity(struct unit
*punit
, struct city
*pcity
)
774 fc_assert_ret_val(unit_owner(punit
) == city_owner(pcity
), TRUE
);
776 if (punit
->homecity
== 0 && !unit_has_type_role(punit
, L_EXPLORER
)) {
777 /* This unit doesn't pay any upkeep while it doesn't have a homecity,
778 * so it would be stupid to give it one. There can also be good reasons
779 * why it doesn't have a homecity. */
780 /* However, until we can do something more useful with them, we
781 will assign explorers to a city so that they can be disbanded for
782 the greater good -- Per */
785 if (pcity
->surplus
[O_SHIELD
] >= unit_type_get(punit
)->upkeep
[O_SHIELD
]
786 && pcity
->surplus
[O_FOOD
] >= unit_type_get(punit
)->upkeep
[O_FOOD
]) {
787 handle_unit_change_homecity(unit_owner(punit
), punit
->id
, pcity
->id
);
793 /**************************************************************************
794 Move a bodyguard along with another unit. We assume that unit has already
795 been moved to ptile which is a valid, safe tile, and that our
796 bodyguard has not. This is an ai_unit_* auxiliary function, do not use
798 **************************************************************************/
799 static void dai_unit_bodyguard_move(struct ai_type
*ait
,
800 struct unit
*bodyguard
, struct tile
*ptile
)
803 struct player
*pplayer
;
805 fc_assert_ret(bodyguard
!= NULL
);
806 pplayer
= unit_owner(bodyguard
);
807 fc_assert_ret(pplayer
!= NULL
);
808 punit
= aiguard_charge_unit(ait
, bodyguard
);
809 fc_assert_ret(punit
!= NULL
);
811 CHECK_GUARD(ait
, bodyguard
);
812 CHECK_CHARGE_UNIT(ait
, punit
);
814 if (!is_tiles_adjacent(ptile
, unit_tile(bodyguard
))) {
818 if (bodyguard
->moves_left
<= 0) {
819 /* should generally should not happen */
820 BODYGUARD_LOG(ait
, LOG_DEBUG
, bodyguard
, "was left behind by charge");
824 unit_activity_handling(bodyguard
, ACTIVITY_IDLE
);
825 (void) dai_unit_move(ait
, bodyguard
, ptile
);
828 /**************************************************************************
829 Move and attack with an ai unit. We do not wait for server reply.
830 **************************************************************************/
831 bool dai_unit_attack(struct ai_type
*ait
, struct unit
*punit
, struct tile
*ptile
)
833 struct unit
*bodyguard
= aiguard_guard_of(ait
, punit
);
834 int sanity
= punit
->id
;
838 fc_assert_ret_val(unit_owner(punit
)->ai_controlled
, TRUE
);
839 fc_assert_ret_val(is_tiles_adjacent(unit_tile(punit
), ptile
), TRUE
);
841 unit_activity_handling(punit
, ACTIVITY_IDLE
);
842 /* Regular attack, Occupy City or other move. */
843 (void) unit_move_handling(punit
, ptile
, FALSE
, TRUE
, NULL
);
844 alive
= (game_unit_by_number(sanity
) != NULL
);
846 if (alive
&& same_pos(ptile
, unit_tile(punit
))
847 && bodyguard
!= NULL
&& def_ai_unit_data(bodyguard
, ait
)->charge
== punit
->id
) {
848 dai_unit_bodyguard_move(ait
, bodyguard
, ptile
);
849 /* Clumsy bodyguard might trigger an auto-attack */
850 alive
= (game_unit_by_number(sanity
) != NULL
);
856 /**************************************************************************
857 Ai unit moving function called from AI interface.
858 **************************************************************************/
859 void dai_unit_move_or_attack(struct ai_type
*ait
, struct unit
*punit
,
860 struct tile
*ptile
, struct pf_path
*path
, int step
)
862 if (step
== path
->length
- 1) {
863 (void) dai_unit_attack(ait
, punit
, ptile
);
865 (void) dai_unit_move(ait
, punit
, ptile
);
869 /**************************************************************************
870 Move a unit. Do not attack. Do not leave bodyguard.
873 This function returns only when we have a reply from the server and
874 we can tell the calling function what happened to the move request.
875 (Right now it is not a big problem, since we call the server directly.)
876 **************************************************************************/
877 bool dai_unit_move(struct ai_type
*ait
, struct unit
*punit
, struct tile
*ptile
)
879 struct unit
*bodyguard
;
880 int sanity
= punit
->id
;
881 struct player
*pplayer
= unit_owner(punit
);
882 const bool is_ai
= pplayer
->ai_controlled
;
886 fc_assert_ret_val_msg(is_tiles_adjacent(unit_tile(punit
), ptile
), FALSE
,
887 "Tiles not adjacent: Unit = %d, "
888 "from = (%d, %d]) to = (%d, %d).",
889 punit
->id
, TILE_XY(unit_tile(punit
)),
892 /* if enemy, stop and give a chance for the ai attack function
893 * to handle this case */
894 if (is_enemy_unit_tile(ptile
, pplayer
)
895 || is_enemy_city_tile(ptile
, pplayer
)) {
896 UNIT_LOG(LOG_DEBUG
, punit
, "movement halted due to enemy presence");
900 /* barbarians shouldn't enter huts */
901 if (is_barbarian(pplayer
) && tile_has_cause_extra(ptile
, EC_HUT
)) {
905 /* don't leave bodyguard behind */
907 && (bodyguard
= aiguard_guard_of(ait
, punit
))
908 && same_pos(unit_tile(punit
), unit_tile(bodyguard
))
909 && bodyguard
->moves_left
== 0) {
910 UNIT_LOG(LOGLEVEL_BODYGUARD
, punit
, "does not want to leave "
915 /* Try not to end move next to an enemy if we can avoid it by waiting */
916 mcost
= map_move_cost_unit(punit
, ptile
);
917 if (punit
->moves_left
<= mcost
918 && unit_move_rate(punit
) > mcost
919 && adv_danger_at(punit
, ptile
)
920 && !adv_danger_at(punit
, unit_tile(punit
))) {
921 UNIT_LOG(LOG_DEBUG
, punit
, "ending move early to stay out of trouble");
926 unit_activity_handling(punit
, ACTIVITY_IDLE
);
928 (void) unit_move_handling(punit
, ptile
, FALSE
, TRUE
, NULL
);
930 /* handle the results */
931 if (game_unit_by_number(sanity
) && same_pos(ptile
, unit_tile(punit
))) {
932 bodyguard
= aiguard_guard_of(ait
, punit
);
934 if (is_ai
&& bodyguard
!= NULL
935 && def_ai_unit_data(bodyguard
, ait
)->charge
== punit
->id
) {
936 dai_unit_bodyguard_move(ait
, bodyguard
, ptile
);
943 /**************************************************************************
944 Calculate the value of the target unit including the other units which
945 will die in a successful attack
946 **************************************************************************/
947 int stack_cost(struct unit
*pattacker
, struct unit
*pdefender
)
949 struct tile
*ptile
= unit_tile(pdefender
);
952 if (is_stack_vulnerable(ptile
)) {
953 /* lotsa people die */
954 unit_list_iterate(ptile
->units
, aunit
) {
955 if (unit_attack_unit_at_tile_result(pattacker
, aunit
, ptile
) == ATT_OK
) {
956 victim_cost
+= unit_build_shield_cost(aunit
);
958 } unit_list_iterate_end
;
959 } else if (unit_attack_unit_at_tile_result(pattacker
, pdefender
, ptile
) == ATT_OK
) {
960 /* Only one unit dies if attack is successful */
961 victim_cost
= unit_build_shield_cost(pdefender
);
967 /**************************************************************************
968 Change government, pretty fast...
969 **************************************************************************/
970 void dai_government_change(struct player
*pplayer
, struct government
*gov
)
972 if (gov
== government_of_player(pplayer
)) {
976 handle_player_change_government(pplayer
, government_number(gov
));
978 city_list_iterate(pplayer
->cities
, pcity
) {
979 auto_arrange_workers(pcity
); /* update cities */
980 } city_list_iterate_end
;
983 /**************************************************************************
984 Credits the AI wants to have in reserves. We need some gold to bribe
987 "I still don't trust this function" -- Syela
988 **************************************************************************/
989 int dai_gold_reserve(struct player
*pplayer
)
991 int i
= total_player_citizens(pplayer
)*2;
992 return MAX(pplayer
->ai_common
.maxbuycost
, i
);
995 /**************************************************************************
996 Adjust want for choice to 'value' percent
997 **************************************************************************/
998 void adjust_choice(int value
, struct adv_choice
*choice
)
1000 choice
->want
= (choice
->want
*value
)/100;
1003 /**************************************************************************
1004 After call to this function best is better of cur and best
1005 **************************************************************************/
1006 void copy_if_better_choice(struct adv_choice
*cur
, struct adv_choice
*best
)
1008 if (best
->want
< cur
->want
) {
1009 /* This simple minded copy works for now.
1011 * TODO: We should get rid of this function. Choice structures should
1012 * be accessed via pointers, and those pointers should be updated
1013 * instead of copying whole structures.
1019 /**************************************************************************
1020 Does choice type refer to unit
1021 **************************************************************************/
1022 bool is_unit_choice_type(enum choice_type type
)
1024 return type
== CT_CIVILIAN
|| type
== CT_ATTACKER
|| type
== CT_DEFENDER
;
1027 /**************************************************************************
1028 Calls dai_wants_role_unit to choose the best unit with the given role and
1029 set tech wants. Sets choice->value.utype when we can build something.
1030 **************************************************************************/
1031 bool dai_choose_role_unit(struct ai_type
*ait
, struct player
*pplayer
,
1032 struct city
*pcity
, struct adv_choice
*choice
,
1033 enum choice_type type
, int role
, int want
,
1036 struct unit_type
*iunit
= dai_wants_role_unit(ait
, pplayer
, pcity
, role
, want
);
1038 if (iunit
!= NULL
) {
1039 choice
->type
= type
;
1040 choice
->value
.utype
= iunit
;
1041 choice
->want
= want
;
1043 choice
->need_boat
= need_boat
;
1051 /**************************************************************************
1052 Consider overriding building target selected by common advisor code.
1053 **************************************************************************/
1054 void dai_build_adv_override(struct ai_type
*ait
, struct city
*pcity
,
1055 struct adv_choice
*choice
)
1057 struct impr_type
*chosen
;
1060 if (choice
->type
== CT_NONE
) {
1064 want
= choice
->want
;
1065 chosen
= choice
->value
.building
;
1068 improvement_iterate(pimprove
) {
1069 /* Advisor code did not consider wonders, let's do it here */
1070 if (is_wonder(pimprove
)) {
1071 if (pcity
->server
.adv
->building_want
[improvement_index(pimprove
)] > want
1072 && can_city_build_improvement_now(pcity
, pimprove
)) {
1073 want
= pcity
->server
.adv
->building_want
[improvement_index(pimprove
)];
1077 } improvement_iterate_end
;
1079 choice
->want
= want
;
1080 choice
->value
.building
= chosen
;
1083 choice
->type
= CT_BUILDING
; /* In case advisor had not chosen anything */
1085 CITY_LOG(LOG_DEBUG
, pcity
, "ai wants most to build %s at %d",
1086 improvement_rule_name(chosen
),
1091 /**********************************************************************
1092 "The following evaluates the unhappiness caused by military units
1093 in the field (or aggressive) at a city when at Republic or
1096 Now generalised somewhat for government rulesets, though I'm not
1097 sure whether it is fully general for all possible parameters/
1098 combinations." --dwp
1099 **********************************************************************/
1100 bool dai_assess_military_unhappiness(struct city
*pcity
)
1102 int free_unhappy
= get_city_bonus(pcity
, EFT_MAKE_CONTENT_MIL
);
1105 /* bail out now if happy_cost is 0 */
1106 if (get_player_bonus(city_owner(pcity
), EFT_UNHAPPY_FACTOR
) == 0) {
1110 unit_list_iterate(pcity
->units_supported
, punit
) {
1111 int happy_cost
= city_unit_unhappiness(punit
, &free_unhappy
);
1113 if (happy_cost
> 0) {
1114 unhap
+= happy_cost
;
1116 } unit_list_iterate_end
;