71 #define LOGLEVEL_RECOVERY LOG_DEBUG
72 #define LOG_CARAVAN LOG_DEBUG
73 #define LOG_CARAVAN2 LOG_DEBUG
74 #define LOG_CARAVAN3 LOG_DEBUG
96 const struct unit *pdef);
125 const struct player *pplayer)
127 struct city *neediest_city =
nullptr;
135 if (pcity->airlift) {
136 if (city_data->
urgency > most_urgent) {
137 most_urgent = city_data->
urgency;
138 neediest_city = pcity;
139 }
else if (0 == most_urgent
140 && city_data->
danger > most_danger) {
141 most_danger = city_data->
danger;
142 neediest_city = pcity;
148 return neediest_city;
159 struct city *most_needed;
161 struct unit *transported;
166 transported =
nullptr;
189 comparison = city_data->
danger;
271 const struct unit *defender)
276 * (attacker->
id != 0 ? defender->
hp : def_type->
hp)
284 const struct unit *defender)
298 struct player *def_player,
299 struct tile *ptile,
bool fortified,
340 desire = ((benefit *
attack - loss * vuln) * victim_count
360 return static_cast<int>(((benefit + loss) * chance - loss)
369 struct tile *ptile0,
int *value,
486 return MAX(0, desire);
488 }
else if (0 == unit_list_size(ptile->
units)) {
555 thresh = (move_needed ? thresh_move : thresh_adj);
556 want = (want < 0 ? -want : want);
558 if (want > max_want && want > thresh) {
604 ((thresh_move - thresh_adj) *
game.
server.occupychance / 100);
606 while (count-- > 0 && punit->
moves_left > 0) {
645 if (me2goal < me2them
646 || (me2goal / punit_mv_rate < them2goal / aunit_mv_rate
647 && me2goal / punit_mv_rate < me2them / punit_mv_rate
648 && punit_mv_rate > aunit_mv_rate)) {
708 struct unit *punit,
struct unit **aunit,
714 struct ai_city *data, *best_data =
nullptr;
716 int def, best_def = -1;
723 if (0 == toughness) {
734 if (move_cost > max_move_cost) {
769 def >>= move_cost / notzero;
771 if (def > best_def) {
780 if (
ai_fuzzy(pplayer,
true) &&
nullptr != pcity
783 if (
nullptr != best_data
800 if (def > best_def &&
ai_fuzzy(pplayer,
true)) {
813 __FUNCTION__, best_def * 100 / toughness,
828 return ((best_def * 100) / toughness);
843 if (pcharge == followee) {
884 struct ai_city *city_data =
nullptr;
886 if (acity !=
nullptr) {
907 || punit->
hp < punittype->
hp * 0.25) {
998 bool dest,
int radius,
int which)
1038 struct tile *dest_tile,
1040 struct tile **ferry_dest,
struct tile **beachhead_tile)
1045 struct tile *best_tile =
nullptr;
1048 if (
nullptr != beachhead_tile) {
1049 *beachhead_tile = dest_tile;
1056 && (
nullptr == best_tile || cost < best_cost)) {
1063 if (
nullptr != ferry_dest) {
1064 *ferry_dest = best_tile;
1070 struct tile *best_tile =
nullptr, *best_beach =
nullptr;
1071 struct tile_list *checked_tiles = tile_list_new();
1074 tile_list_append(checked_tiles, dest_tile);
1081 if (!tile_list_search(checked_tiles, ptile)
1083 tile_list_append(checked_tiles, ptile);
1086 && (
nullptr == best_tile || cost < best_cost)) {
1098 tile_list_destroy(checked_tiles);
1100 if (
nullptr != beachhead_tile) {
1101 *beachhead_tile = best_beach;
1103 if (
nullptr != ferry_dest) {
1104 *ferry_dest = best_tile;
1119 struct unit *punit,
struct tile **pdest_tile,
1121 struct unit **pferryboat,
1127 struct pf_map *punit_map, *ferry_map;
1133 const struct unit_type *boattype =
nullptr;
1134 struct unit *ferryboat =
nullptr;
1137 int bcost, bcost_bal;
1140 bool harbor =
false;
1144 struct unit *pdefender;
1157 struct tile *goto_dest_tile =
nullptr;
1161 *pdest_tile = punit_tile;
1162 if (
nullptr != pferrymap) {
1163 *pferrymap =
nullptr;
1165 if (
nullptr != pferryboat) {
1166 *pferryboat =
nullptr;
1168 if (
nullptr != pboattype) {
1169 *pboattype =
nullptr;
1171 if (
nullptr != pmove_time) {
1174 if (!ppath->
empty()) {
1178 if (0 == attack_value) {
1219 if (aunit == punit) {
1227 if (aunit->activity == ACTIVITY_GOTO) {
1229 ait, aunit,
true, 0,
1231 if ((pcity =
tile_city(aunit->goto_tile))) {
1244 if (aunit->activity == ACTIVITY_GOTO) {
1261 if (
nullptr != pcity && (0 == punit->
id || pcity->
id == punit->
homecity)) {
1279 if (ferryboat ==
nullptr || !
is_boat_free(ait, ferryboat, punit, 0)) {
1281 ferryboat =
nullptr;
1284 if (
nullptr == ferryboat) {
1296 if (
nullptr != ferryboat) {
1303 if (
nullptr == boattype) {
1307 if (
nullptr != boattype && harbor) {
1314 ferry_map =
nullptr;
1349 move_time = pos.
turn;
1350 }
else if (
nullptr == ferry_map) {
1353 struct tile *dest, *beach;
1355 if (!
find_beachhead(pplayer, ferry_map, atile, punit_type, &dest,
1363 move_time = pos.
turn;
1364 if (dest != beach) {
1367 if (
nullptr != ferryboat &&
unit_tile(ferryboat) != punit_tile) {
1369 move_time += pos.
turn;
1382 pdefender =
nullptr;
1387 if (1 < move_time) {
1393 punit_type, def_type, aplayer, atile,
false,
1395 if (v > vulnerability) {
1406 (acity_data->
invasion.
attack - unit_list_size(acity->tile->units));
1408 if (punit->
id == 0) {
1413 if (0 < reserves && (can_occupy || 0 < acity_data->
invasion.
occupy)) {
1416 benefit += acity_data->
worth * reserves / 5;
1426 victim_count = unit_list_size(atile->
units);
1428 if (!can_occupy &&
nullptr == pdefender) {
1432 }
else if (10 < move_time) {
1441 vulnerability, victim_count + 1);
1447 needferry = (go_by_boat &&
nullptr == ferryboat
1453 want,
MAX(1, move_time), bcost_bal + needferry);
1456 if (0 >= want && 0 == punit->
id && 0 == best) {
1460 bcost_bal + needferry);
1463 *pdest_tile = atile;
1464 if (
nullptr != pferrymap) {
1465 *pferrymap = go_by_boat ? ferry_map :
nullptr;
1467 if (
nullptr != pferryboat) {
1468 *pferryboat = go_by_boat ? ferryboat :
nullptr;
1470 if (
nullptr != pboattype) {
1471 *pboattype = go_by_boat ? boattype :
nullptr;
1473 if (
nullptr != pmove_time) {
1474 *pmove_time = move_time;
1477 (go_by_boat &&
nullptr != ferryboat ?
unit_tile(ferryboat)
1484 if (0 != punit->
id &&
nullptr != ferryboat
1487 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1488 " (go_by_boat=%d, move_time=%d, want=%d, best=%d)",
1491 TILE_XY(atile), go_by_boat, move_time, want, best);
1494 if (want > best &&
ai_fuzzy(pplayer,
true)) {
1497 *pdest_tile = atile;
1498 if (
nullptr != pferrymap) {
1499 *pferrymap = go_by_boat ? ferry_map :
nullptr;
1501 if (
nullptr != pferryboat) {
1502 *pferryboat = go_by_boat ? ferryboat :
nullptr;
1504 if (
nullptr != pboattype) {
1505 *pboattype = go_by_boat ? boattype :
nullptr;
1507 if (
nullptr != pmove_time) {
1508 *pmove_time = move_time;
1511 (go_by_boat &&
nullptr != ferryboat ?
unit_tile(ferryboat)
1558 move_time = pos.
turn;
1559 if (10 < move_time) {
1572 want,
MAX(1, move_time), bcost_bal);
1573 if (want > best &&
ai_fuzzy(pplayer,
true)) {
1575 *pdest_tile = atile;
1576 if (
nullptr != pferrymap) {
1577 *pferrymap =
nullptr;
1579 if (
nullptr != pferryboat) {
1580 *pferryboat =
nullptr;
1582 if (
nullptr != pboattype) {
1583 *pboattype =
nullptr;
1585 if (
nullptr != pmove_time) {
1586 *pmove_time = move_time;
1588 goto_dest_tile = atile;
1595 if (!ppath->
empty()) {
1596 *ppath = (
nullptr != goto_dest_tile && goto_dest_tile != punit_tile
1602 if (
nullptr != ferry_map
1603 && (
nullptr == pferrymap || *pferrymap != ferry_map)) {
1625 struct city *pcity, *best_city =
nullptr;
1634 if (move_cost > best) {
1677 bool only_continent =
true;
1682 only_continent =
false;
1687 only_continent,
false,
false,
true,
nullptr))) {
1693 struct unit *ferry =
nullptr;
1741 struct tile *dest_tile;
1744 struct city *pcity =
nullptr;
1774 struct unit *ferryboat;
1778 &ferryboat,
nullptr,
nullptr);
1796 if (
nullptr != ferryboat) {
1849 if (pcity !=
nullptr
1893 if ((ferryboat <= 0)) {
1895 "in find_boat_for_unit cannot find any boats.");
1898 if (current_city ==
nullptr) {
1900 if (city_near !=
nullptr) {
1905 if (!path_to_ferry.
empty()) {
1925 const struct city *dest_city,
bool help_wonder,
1926 bool required_boat,
bool request_boat)
1940 help_wonder ?
"help a wonder" :
"trade",
1941 city_name_get(dest_city), required_boat ?
"with a boat" :
"");
1942 if (required_boat) {
1984 ACTION_HELP_WONDER);
1992 ACTION_TRADE_ROUTE);
2001 ACTION_MARKETPLACE);
2026 const struct unit *caravan =
static_cast<const unit *
>(data);
2042 struct tile *src =
nullptr, *dest =
nullptr, *src_home_city =
nullptr;
2043 struct city *phome_city =
nullptr;
2049 if (phome_city !=
nullptr) {
2054 if (src ==
nullptr || dest ==
nullptr || src_home_city ==
nullptr) {
2114 if (aplayer == pplayer || !aplayer->
is_alive) {
2141 struct city *nearest =
nullptr;
2154 if (this_dist < min_dist) {
2155 min_dist = this_dist;
2162 if (nearest !=
nullptr) {
2183 const struct city *homecity;
2184 const struct city *dest =
nullptr;
2186 bool help_wonder =
false;
2187 bool required_boat =
false;
2188 bool request_boat =
false;
2189 bool tired_of_waiting_boat =
false;
2215 if (homecity ==
nullptr) {
2226 if ((city_dest ==
nullptr)
2258 tired_of_waiting_boat =
true;
2264 request_boat =
false;
2270 if (homecity ==
nullptr) {
2277 if (homecity ==
nullptr) {
2300 if (tired_of_waiting_boat) {
2306 if (result.
dest !=
nullptr) {
2312 request_boat = required_boat;
2321 if (dest !=
nullptr) {
2343 struct city *safe =
nullptr;
2377 "didn't find a city to recover in!");
2385 if (punit->
hp == punittype->
hp) {
2414 if ((punit->
activity == ACTIVITY_SENTRY
2415 || punit->
activity == ACTIVITY_FORTIFIED)
2419 unit_data->
done =
true;
2430 int result, sanity = punit->
id;
2442 }
else if (result >= 1) {
2458 switch (unit_data->
task) {
2512 }
else if (pcity || punit->
activity == ACTIVITY_IDLE) {
2516 if (punit->
activity == ACTIVITY_IDLE
2517 || punit->
activity == ACTIVITY_SENTRY) {
2536 unit_data->
done =
true;
2556 bool is_ferry =
false;
2565 "is under human orders, aborting AI control.");
2567 unit_data->
done =
true;
2582 unit_data->
done =
true;
2626 unit_data->
done =
true;
2666 int total_defense = 0;
2668 bool emergency =
false;
2674 int entertainers = 0;
2675 bool enough =
false;
2680 entertainers += pcity->specialists[sp];
2685 martless_unhappy += entertainers;
2689 && (total_defense <= total_attack
2690 || (count < mart_max && mart_each > 0
2691 && martless_unhappy > mart_each * count))) {
2693 struct unit *best =
nullptr;
2694 bool defense_needed =
2695 total_defense <= total_attack;
2706 if (want > best_want) {
2714 if (best ==
nullptr) {
2715 if (defense_needed) {
2728 if ((martless_unhappy < mart_each * count || count >= mart_max
2738 total_defense += best_want;
2745 "Evaluating defense: %d defense, %d incoming"
2746 ", %d defenders (out of %d)",
2747 total_defense, total_attack, count,
2748 unit_list_size(pcity->tile->units));
2772 unit_data->
done =
false;
2803 const struct city *pcity)
2805 const struct impr_type *impr_req =
nullptr;
2810 city_tile(pcity),
nullptr, putype,
nullptr,
nullptr,
2820 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2822 impr_req = preq->source.value.building;
2837 struct unit *leader)
2843 struct unit *worst_danger;
2844 int move_cost, best_move_cost;
2852 && 1 < unit_list_size(leader_tile->
units))) {
2869 && warrior->moves_left > 0) {
2898 if (0 < body_guards) {
2929 worst_danger =
nullptr;
2934 if (other_player == pplayer) {
2942 best_move_cost = move_cost;
2943 worst_danger = punit;
2952 if (
nullptr == worst_danger) {
2965 safest_tile = leader_tile;
2979 "Barbarian leader: safest is (%d, %d), safeness %d",
2980 TILE_XY(near_tile), best_move_cost);
2981 best_move_cost = move_cost;
2982 safest_tile = near_tile;
2991 "Barbarian leader: reached the safest position.");
3024 int extras_bonus = 0;
3042 db += (db * extras_bonus) / 100;
3058 if ((a * a * 10) >= d) {
3083 if (
A_NEVER != punittype->require_advance
3088 && !
utype_fuel(punittype) && punittype->transport_capacity < 8) {
3113 utai->
ferry =
false;
3129 if (pbonus->type == CBONUS_FIREPOWER1) {
3145 if (punittype->transport_capacity > 0) {
3178 bool can_move_like_charge =
false;
3189 can_move_like_charge =
true;
3194 if (can_move_like_charge) {
3215 if (utai ==
nullptr) {
3236 unit_data->
done =
false;
3273 if (unit_data !=
nullptr) {
3276 unit_data =
nullptr;
3285 const char *unitstr)
3304 const char *unitstr)
3319 enum terrain_class
tc;
3346 enum terrain_class tc)
3357 const struct unit *defender)
3363 bool able_to_strike =
false;
3371 if (move_cost > max_move_cost) {
3375 if (ptile == ptarget) {
3376 able_to_strike =
true;
3384 return able_to_strike;
bool is_action_enabled_unit_on_city(const action_id wanted_action, const struct unit *actor_unit, const struct city *target_city)
Returns TRUE if actor_unit can do wanted_action to target_city as far as action enablers are concerne...
int adv_unit_def_rating_basic_squared(const struct unit *punit)
Square of the previous function - used in actual computations.
int adv_unit_def_rating_basic(const struct unit *punit)
Basic (i.e.
bool adv_unit_execute_path(struct unit *punit, const PFPath &path)
This is a function to execute paths returned by the path-finding engine, for units controlled by advi...
int adv_unittype_att_rating(const struct unit_type *punittype, int veteran, int moves_left, int hp)
Attack rating of this kind of unit.
int adv_unit_att_rating(const struct unit *punit)
Attack rating of this particular unit assuming that it has a complete move left.
bool adv_follow_path(struct unit *punit, const PFPath &path, struct tile *ptile)
Move a unit along a path without disturbing its activity, role or assigned destination Return FALSE i...
int adv_could_unit_move_to_tile(struct unit *punit, struct tile *dest_tile)
returns: 0 if can't move 1 if zoc_ok -1 if zoc could be ok?
void dai_manage_airunit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Trying to manage bombers and stuff.
void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
If we are the only diplomat in a threatened city, defend against enemy actions.
bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
This function is to be called if punit needs to use a boat to get to the destination.
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, PFPath *path)
Proper and real PF function for finding a boat.
void dai_manage_ferryboat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
It's about 12 feet square and has a capacity of almost 1000 pounds.
bool is_boat_free(struct ai_type *ait, struct unit *boat, struct unit *punit, int cap)
Runs a few checks to determine if "boat" is a free boat that can carry "cap" units of the same type a...
bool aiferry_goto_amphibious(struct ai_type *ait, struct unit *ferry, struct unit *passenger, struct tile *ptile)
Move a passenger on a ferry to a specified destination.
bool is_boss_of_boat(struct ai_type *ait, struct unit *punit)
Check if unit is boss in ferry.
int aiferry_avail_boats(struct ai_type *ait, struct player *pplayer)
Returns the number of available boats.
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Should unit be considered a ferry?
void aiferry_clear_boat(struct ai_type *ait, struct unit *punit)
Use on a unit which no longer needs a boat.
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Which unit (if any) has a guard been assigned to? Returns nullptr if the unit is not the guard for a ...
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Remove the assignment of a charge to a guard.
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Request a (new) bodyguard for the unit.
bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
Has a guard been assigned to a charge?
void aiguard_assign_guard_unit(struct ai_type *ait, struct unit *charge, struct unit *guard)
Assign a bodyguard to a unit.
bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
Has a unit requested a guard and not (yet) been provided with one?
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Assign a guard to a city.
struct city * aiguard_charge_city(struct ai_type *ait, struct unit *guard)
Which city (if any) has a guard been assigned to? Returns nullptr if the unit is not a guard for a ci...
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Remove assignment of bodyguard for a unit.
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Which unit, if any, is the body guard of a unit? Returns nullptr if the unit has not been assigned a ...
bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
Has a charge unit been assigned to a guard?
void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
Check whether the assignment of a guard is still sane, and fix and problems.
#define CHECK_GUARD(ait, guard)
bool dai_hunter_qualify(struct player *pplayer, struct unit *punit)
Does this unit qualify as a hunter?
int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Manage a (possibly virtual) hunter.
#define BODYGUARD_LOG(ait, loglevel, punit, msg,...)
void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer, struct unit *punit)
This function does manage the paratrooper units of the AI.
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
int look_for_charge(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
See if we can find something to defend.
void dai_unit_save(struct ai_type *ait, const char *aitstr, struct section_file *file, const struct unit *punit, const char *unitstr)
Save AI data of a unit.
static bool has_defense(struct city *pcity)
This is a much simplified form of assess_defense (see daimilitary.c), but which doesn't use pcity->se...
void dai_manage_military(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Decide what to do with a military unit.
static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
Try to move caravan to suitable city and to make it caravan's homecity.
int find_something_to_kill(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile **pdest_tile, PFPath *ppath, struct pf_map **pferrymap, struct unit **pferryboat, const struct unit_type **pboattype, int *pmove_time)
Find something to kill! This function is called for units to find targets to destroy and for cities t...
static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer, struct unit *punit, const struct city *dest_city, bool help_wonder, bool required_boat, bool request_boat)
Send the caravan to the specified city, or make it help the wonder / trade, if it's already there.
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)
Defence rating of def_type unit against att_type unit, squared.
static bool unit_role_defender(const struct unit_type *punittype)
Does the unit with the id given have the flag L_DEFEND_GOOD?
void dai_units_ruleset_init(struct ai_type *ait)
Initialise the unit data from the ruleset for the AI.
struct city * find_nearest_safe_city(struct unit *punit)
Find safe city to recover in.
static int unit_att_rating_now(const struct unit *punit)
Attack rating of this particular unit right now.
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Go berserk, assuming there are no targets nearby.
static void invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Mark invasion possibilities of punit in the surrounding cities.
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Switch to autoexploring.
static int unit_def_rating_squared(const struct unit *punit, const struct unit *pdef)
Square of the previous function - used in actual computations.
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Request a boat for a unit to transport it to another continent.
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Returns an improvement that will make it possible to build units of the specified type the specified ...
static void reinforcements_cost_and_value(struct unit *punit, struct tile *ptile0, int *value, int *cost)
Calculates the value and cost of nearby allied units to see if we can expect any help in our attack.
struct unit_type * simple_ai_types[U_LAST]
static void dai_airlift(struct ai_type *ait, struct player *pplayer)
Move defenders around with airports.
int build_cost_balanced(const struct unit_type *punittype)
In the words of Syela: "Using funky fprime variable instead of f in the denom, so that def=1 units ar...
bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct unit *defender)
Returns whether 'attacker' can attack 'defender' immediately.
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
If we are not covering our charge's ass, go do it now.
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Master manage unit function.
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Use caravans for building wonders, or send caravans to establish trade with a city,...
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Master city defense function.
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
manage one unit Careful: punit may have been destroyed upon return from this routine!
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Get unit type player can build, suitable to role, with given move type.
static bool role_unit_cb(struct unit_type *ptype, void *data)
Filter callback for role unit iteration.
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
This does the attack until we have used up all our movement, unless we should safeguard a city.
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Free unit from use with default AI.
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Manages settlers.
static int avg_benefit(int benefit, int loss, double chance)
Compute how much we want to kill certain victim we've chosen, counted in SHIELDs.
static int unit_def_rating(const struct unit *attacker, const struct unit *defender)
Defence rating of this particular unit against this attacker.
static bool dai_is_unit_tired_waiting_boat(struct ai_type *ait, struct unit *punit)
Evaluate if a unit is tired of waiting for a boat at home continent.
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
This function appraises the location (x, y) for a quick hit-n-run operation.
bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
See if the follower can follow the followee.
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Barbarian leader tries to stack with other barbarian units, and if it's not possible it runs away.
static struct city * find_neediest_airlift_city(struct ai_type *ait, const struct player *pplayer)
Returns the city with the most need of an airlift.
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
This function goes wait a unit in a city for the hitpoints to recover.
static int unit_att_rating_squared(const struct unit *punit)
Square of the adv_unit_att_rating() function - used in actual computations.
static bool is_my_turn(struct unit *punit, struct unit *pdef)
Is there another unit which really should be doing this attack? Checks all adjacent tiles and the til...
void dai_unit_init(struct ai_type *ait, struct unit *punit)
Initialize unit for use with default AI.
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Are there dangerous enemies at or adjacent to the tile 'ptile'?
static void dai_military_findjob(struct ai_type *ait, struct player *pplayer, struct unit *punit)
See if we have a specific job for the unit.
static void update_simple_ai_types()
Updates the global array simple_ai_types.
void dai_unit_load(struct ai_type *ait, const char *aitstr, const struct section_file *file, struct unit *punit, const char *unitstr)
Load AI data of a unit.
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
For debugging, print out information about every city we come to when optimizing the caravan.
#define LOGLEVEL_RECOVERY
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Free unit from use with default AI.
void dai_units_ruleset_close(struct ai_type *ait)
Close AI unit type data.
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)
Returns TRUE if a beachhead as been found to reach 'dest_tile'.
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Send a unit to the city it should defend.
int kill_desire(int benefit, int attack, int loss, int vuln, int victim_count)
Compute how much we want to kill certain victim we've chosen, counted in SHIELDs.
static PFPath find_rampage_target(struct unit *punit, int thresh_adj, int thresh_move)
Look for worthy targets within a one-turn horizon.
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Find and kill anything reachable within this turn and worth more than the relevant of the given thres...
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Check if a caravan can make a trade route to a city on a different continent.
#define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer)
#define RAMPAGE_FREE_CITY_OR_BETTER
#define RAMPAGE_HUT_OR_BETTER
#define DEFENSE_POWER(ptype)
#define IS_ATTACKER(ptype)
#define BODYGUARD_RAMPAGE_THRESHOLD
#define ATTACK_POWER(ptype)
enum unit_move_result manage_auto_explorer(struct unit *punit)
Handle eXplore mode of a unit (explorers are always in eXplore mode for AI) - explores unknown territ...
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
Change unit's advisor task.
bool is_land_barbarian(struct player *pplayer)
Is player a land barbarian?
void caravan_find_best_destination(const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result, bool omniscient)
Find the best destination city for the caravan.
void caravan_parameter_init_from_unit(struct caravan_parameter *parameter, const struct unit *caravan)
Create a valid parameter with default values based on the caravan.
bool city_production_gets_caravan_shields(const struct universal *tgt)
Returns TRUE iff the specified production should get shields from units that has done ACTION_HELP_WON...
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
How many veteran levels will created unit of this type get?
const char * city_name_get(const struct city *pcity)
Return the name of the city.
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Return whether given city can build given unit; returns FALSE if unit is obsolete.
#define city_list_iterate(citylist, pcity)
#define city_list_iterate_end
struct unit * get_defender(const struct unit *attacker, const struct tile *ptile)
Finds the best defender on the tile, given an attacker.
enum unit_attack_result unit_attack_unit_at_tile_result(const struct unit *punit, const struct unit *pdefender, const struct tile *dest_tile)
Checks if a unit can physically attack pdefender at the tile (assuming it is adjacent and at war).
enum unit_attack_result unit_attack_units_at_tile_result(const struct unit *punit, const struct tile *ptile)
Check if unit can attack unit stack at tile.
bool is_stack_vulnerable(const struct tile *ptile)
Is it a city/fortress/air base or will the whole stack die in an attack.
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
return the modified defense power of a unit.
int get_virtual_defense_power(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
May be called with a non-existing att_type to avoid any effects which depend on the attacker.
int base_get_defense_power(const struct unit *punit)
Returns the defense power, modified by veteran status.
double unit_win_chance(const struct unit *attacker, const struct unit *defender)
Returns a double in the range [0;1] indicating the attackers chance of winning.
bool can_unit_attack_tile(const struct unit *punit, const struct tile *dest_tile)
Is unit (1) diplomatically allowed to attack and (2) physically able to do so?
bool dai_can_requirement_be_met_in_city(const struct requirement *preq, const struct player *pplayer, const struct city *pcity)
Does the AI expect to ever be able to meet this requirement.
struct unit_type * dai_choose_defender_versus(struct city *pcity, struct unit *attacker)
Choose the best unit the city can build to defend against attacker v.
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
Need positive feedback in m_a_c_b and bodyguard routines.
int assess_defense_unit(struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
One unit only, mostly for findjob; handling boats correctly.
static void attack(QVariant data1, QVariant data2)
Action "Attack" for choice dialog.
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Return the value normal_decision (a boolean), except if the AI is fuzzy, then sometimes flip the valu...
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, enum effect_type effect_type, enum vision_layer vision_layer)
Returns the effect bonus that applies at a tile for a given unittype.
int get_city_bonus(const struct city *pcity, enum effect_type effect_type, enum vision_layer vlayer)
Returns the effect bonus at a city.
signed short Continent_id
struct unit * game_unit_by_number(int id)
Find unit out of all units in game: now uses fast idex method, instead of looking through all units o...
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
#define fc_assert_ret(condition)
constexpr auto LOG_VERBOSE
#define fc_assert(condition)
#define fc_assert_ret_msg(condition, message,...)
constexpr auto LOG_NORMAL
#define fc_assert_ret_val(condition, val)
#define log_base(level, message,...)
bool is_tiles_adjacent(const struct tile *tile0, const struct tile *tile1)
Are two tiles adjacent to each other.
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
int map_distance(const struct tile *tile0, const struct tile *tile1)
Return Manhattan distance between two tiles.
static int index_to_map_pos_y(int mindex)
#define square_iterate(nmap, center_tile, radius, tile_itr)
#define adjc_iterate(nmap, center_tile, itr_tile)
#define square_iterate_end
static int index_to_map_pos_x(int mindex)
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Return whether the player knows the tile.
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Returns whether the layer 'vlayer' of the tile 'ptile' is known and seen by the player 'pplayer'.
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "exist" at this location.
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
This tile is native to unit.
int unit_move_rate(const struct unit *punit)
This function calculates the move rate of the unit.
bool can_attack_from_non_native(const struct unit_type *utype)
This unit can attack from non-native tiles (Marines can attack from transport, ships from harbour cit...
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Return TRUE iff the unit can "survive" at this location.
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Return TRUE iff transporter type has ability to transport transported class.
bool can_attack_non_native(const struct unit_type *utype)
This unit can attack non-native tiles (eg.
static bool is_native_tile_to_class(const struct unit_class *punitclass, const struct tile *ptile)
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
struct nation_type * nation_of_unit(const struct unit *punit)
Return the nation of the player who owns the unit.
bool pf_map_position(struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
Get info about position at ptile and put it in pos.
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Factory function to create a new map according to the parameter.
struct pf_reverse_map * pf_reverse_map_new(const struct player *pplayer, struct tile *target_tile, int max_turns, bool omniscient, const struct civ_map *map)
'pf_reverse_map' constructor.
PFPath pf_map_path(struct pf_map *pfm, struct tile *ptile)
CHECK DOCS AFTER FULL CONVERSTION OF pf_path to class PFPath Tries to find the best path in the given...
void pf_reverse_map_destroy(struct pf_reverse_map *pfrm)
'pf_reverse_map' destructor.
int pf_reverse_map_unit_move_cost(struct pf_reverse_map *pfrm, const struct unit *punit)
Get the move costs that a unit needs to reach the start tile.
void pf_map_destroy(struct pf_map *pfm)
After usage the map must be destroyed.
int pf_map_move_cost(struct pf_map *pfm, struct tile *ptile)
Tries to find the minimal move cost to reach ptile.
#define pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
#define pf_map_tiles_iterate(ARG_pfm, NAME_tile, COND_from_start)
#define pf_map_tiles_iterate_end
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
If the specified player owns the unit with the specified id, return pointer to the unit struct.
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
#define players_iterate_end
#define players_iterate(_pplayer)
static bool is_barbarian(const struct player *pplayer)
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
Lookup a integer value in the secfile.
#define secfile_insert_int(secfile, value, path,...)
bool is_req_active(const struct player *target_player, const struct player *other_player, const struct city *target_city, const struct impr_type *target_building, const struct tile *target_tile, const struct unit *target_unit, const struct unit_type *target_unittype, const struct output_type *target_output, const struct specialist *target_specialist, const struct action *target_action, const struct requirement *req, const enum req_problem_type prob_type, const enum vision_layer vision_layer, const enum national_intelligence nintel)
Checks the requirement to see if it is active on the given target.
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
struct setting_list * level[OLEVELS_NUM]
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Return the output for the specialist type with this output type.
#define specialist_type_iterate_end
#define specialist_type_iterate(sp)
#define UNIT_LOG(_, punit, msg,...)
#define CITY_LOG(_, pcity, msg,...)
#define TIMING_LOG(timer, activity)
#define LOGLEVEL_BODYGUARD
struct ai_invasion invasion
void(* callback)(const struct caravan_result *result, void *data)
enum foreign_trade_limit allow_foreign_trade
An advisor for using caravans optimally.
struct universal production
struct civ_game::@28::@32 server
struct packet_game_info info
struct city_list * cities
struct unit_class::@81 cache
struct unit_class::@80 adv
struct unit_class_list * subset_movers
enum move_level land_move
struct unit_type_list * potential_charges
struct requirement_vector build_reqs
struct veteran_system * veteran
enum unit_activity activity
struct unit::@76::@79 server
bool is_terrain_class_near_tile(const struct tile *ptile, enum terrain_class tclass)
Is there terrain of the given class near tile? (Does not check ptile itself.)
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Calculate defense bonus given for unit type by bases and roads.
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
#define tile_terrain(_tile)
#define tile_continent(_tile)
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Return TRUE iff the two cities are capable of trade; i.e., if a caravan from one city can enter the o...
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2)
Returns TRUE iff the two cities can establish a trade route.
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Attach ai data to unit.
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Load pcargo onto ptrans.
bool is_losing_hp(const struct unit *punit)
Does unit lose hitpoints each turn?
struct unit * is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Is there an non-allied unit on this tile?
struct unit * unit_transport_get(const struct unit *pcargo)
Returns the transporter of the unit or nullptr if it is not transported.
bool is_military_unit(const struct unit *punit)
Military units are capable of enforcing martial law.
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Return TRUE iff this unit can do the specified generalized (ruleset defined) action enabler controlle...
int get_transporter_capacity(const struct unit *punit)
Return the number of units the transporter can hold (or 0).
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Determines if punit can be airlifted to dest_city now! So punit needs to be in a city now.
bool can_unit_do_activity(const struct unit *punit, enum unit_activity activity)
Return TRUE iff the unit can do the given untargeted activity at its current location.
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
bool unit_has_orders(const struct unit *punit)
Return TRUE iff the unit is following client-side orders.
#define CHECK_UNIT(punit)
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Handle request for changing activity.
void unit_do_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id, const char *name, const action_id action_type)
Handle unit action.
bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent)
Change controlling server side agent.
struct unit * unit_list_find(const struct unit_list *punitlist, int unit_id)
Look for a unit with the given ID in the unit list.
#define unit_list_iterate(unitlist, punit)
#define unit_list_iterate_safe(unitlist, _unit)
#define unit_list_iterate_end
#define unit_list_iterate_safe_end
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Return "best" unit the player can build, with given role/flag.
bool utype_has_role(const struct unit_type *punittype, int role)
Return whether the given unit type has the role.
int utype_build_shield_cost(const struct city *pcity, const struct unit_type *punittype)
Returns the number of shields it takes to build this unit type.
int utype_build_shield_cost_base(const struct unit_type *punittype)
Returns the number of shields this unit type represents.
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Return TRUE iff units of the given type can do any enabler controlled action with the specified actio...
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Return whether the unit has the given role.
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
bool unit_can_take_over(const struct unit *punit)
Return whether the unit can take over enemy cities.
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
void utype_set_ai_data(struct unit_type *ptype, const struct ai_type *ai, void *data)
Attach ai data to unit type.
bool utype_acts_hostile(const struct unit_type *putype)
Return TRUE iff units of this type can do hostile actions controlled by generalized (ruleset defined)...
struct unit_type * role_units_iterate_backwards(int role, role_unit_callback cb, void *data)
Iterate over all the role units and feed them to callback, starting from the last one.
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Return TRUE iff units of the given type can do the specified generalized (ruleset defined) action ena...
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Return pointer to ai data of given unit type and ai type.
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
#define utype_fuel(ptype)
#define combat_bonus_list_iterate_end
#define combat_bonus_list_iterate(bonuslist, pbonus)
#define unit_type_list_iterate(utype_list, ptype)
static bool utype_has_flag(const struct unit_type *punittype, int flag)
#define unit_class_list_iterate(uclass_list, pclass)
#define unit_type_iterate(_p)
#define unit_type_list_iterate_end
#define unit_type_iterate_end
#define unit_class_list_iterate_end