41 const struct tile *ptile)
46 if (!pcity && unit_list_size((ptile->
units)) == 0) {
72 const struct unit *attacker)
84 const struct unit *attacker,
85 const struct tile *location)
122 const struct unit *pdefender,
123 const struct tile *dest_tile)
137 USP_NATIVE_TILE,
false)
139 ACTION_SUICIDE_ATTACK,
140 USP_NATIVE_TILE,
false)) {
169 const struct tile *ptile)
171 bool any_reachable_unit =
false;
172 bool any_neverprotect_unit =
false;
188 any_neverprotect_unit =
true;
190 }
else if (result !=
ATT_OK) {
193 any_reachable_unit =
true;
200 return (any_reachable_unit || !any_neverprotect_unit) ?
ATT_OK
211 const struct tile *ptile)
238 const struct tile *ptile)
240 if (
game.
info.unreachable_protects) {
252 const struct tile *dest_tile)
270 double win_chance(
int as,
int ahp,
int afp,
int ds,
int dhp,
int dfp)
277 int att_N_lose = (ahp + dfp - 1) / dfp;
278 int def_N_lose = (dhp + afp - 1) / afp;
281 (as + ds == 0) ? 0.5 :
static_cast<double>(ds) / (as + ds);
282 double def_P_lose1 = 1 - att_P_lose1;
330 double binom_save = pow(def_P_lose1,
static_cast<double>(def_N_lose - 1));
331 double accum_prob = binom_save;
334 for (lr = 1; lr < att_N_lose; lr++) {
336 int n = lr + def_N_lose - 1;
339 binom_save *= att_P_lose1;
341 accum_prob += binom_save;
344 accum_prob *= def_P_lose1;
353 const struct unit *defender,
int *att_fp,
409 const struct unit *defender)
419 chance =
win_chance(att_power, attacker->
hp, att_fp, def_power,
420 defender->
hp, def_fp);
431 const struct tile *ptile)
440 nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
513 power = (power * db) / 100;
527 const struct unit *defender)
536 return attackpower * mod / 100;
550 const struct unit *def,
551 const struct player *def_player,
552 const struct tile *ptile,
int defensepower)
559 if (
nullptr != att_type) {
560 int defense_divider_pct;
561 int defense_multiplier_pct =
564 defensepower = defensepower * defense_multiplier_pct / 100;
570 defensepower =
MAX(0, defensepower * mod / 100);
572 defense_divider_pct =
575 CBONUS_DEFENSE_DIVIDER_PCT)
578 CBONUS_DEFENSE_DIVIDER);
580 defensepower = defensepower * 100 / defense_divider_pct;
586 defensepower = defensepower
591 nullptr,
nullptr, EFT_FORTIFY_DEFENSE_BONUS))
603 struct player *def_player,
struct tile *ptile,
628 vdef->
activity = ACTIVITY_FORTIFIED;
655 const struct unit *defender)
668 struct unit *defender)
670 const struct unit_type *att_type =
nullptr;
671 enum unit_activity real_act;
675 if (attacker !=
nullptr) {
683 defender->
activity = ACTIVITY_FORTIFIED;
701 const struct unit *defender)
709 rating *= (defender->
hp + afp - 1) / (afp ?: 1);
722 const struct tile *ptile)
724 struct unit *bestdef =
nullptr;
725 int bestvalue = -99, best_cost = 0, rating_of_best = 0;
747 int unit_def =
static_cast<int>(
759 if (unit_def > bestvalue) {
761 }
else if (unit_def == bestvalue) {
762 if (build_cost < best_cost) {
764 }
else if (build_cost == best_cost) {
765 if (rating_of_best < defense_rating) {
772 bestvalue = unit_def;
774 best_cost = build_cost;
775 rating_of_best = defense_rating;
793 const struct unit *pvictim,
794 const struct tile *tgt_tile)
854 enum combat_bonus_type type)
860 if (pbonus->type == type &&
utype_has_flag(enemy, pbonus->flag)) {
861 value += pbonus->value;
bool BV_ISSET(const BV &bv, int bit)
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
struct unit * get_defender(const struct unit *attacker, const struct tile *ptile)
Finds the best defender on the tile, given an attacker.
bool is_unit_reachable_at(const struct unit *defender, const struct unit *attacker, const struct tile *location)
Can unit attack other at given location.
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_attack_power(const struct unit *punit)
Convenience wrapper for base_get_attack_power.
int combat_bonus_against(const struct combat_bonus_list *list, const struct unit_type *enemy, enum combat_bonus_type type)
Get bonus value against given unit type from bonus list.
int get_fortified_defense_power(const struct unit *attacker, struct unit *defender)
Return total defense power of the unit if it fortifies, if possible, where it is.
int base_get_attack_power(const struct unit_type *punittype, int veteran, int moves_left)
Returns the attack power, modified by moves left, and veteran status.
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
return the modified defense power of a unit.
static enum unit_attack_result unit_attack_any_at_tile_result(const struct unit *punit, const struct tile *ptile)
When unreachable_protects setting is FALSE: To attack a stack, unit must be able to attack some 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.
static int get_defense_power(const struct unit *punit)
Returns the defense power, modified by terrain and veteran status.
static int get_defense_rating(const struct unit *attacker, const struct unit *defender)
A number indicating the defense strength.
struct city * sdi_try_defend(const struct player *owner, const struct tile *ptile)
Try defending against nuclear attack; if successful, return a city which had enough luck and EFT_NUKE...
static enum unit_attack_result unit_attack_all_at_tile_result(const struct unit *punit, const struct tile *ptile)
When unreachable_protects setting is TRUE: To attack a stack, unit must be able to attack every unit ...
bool is_tired_attack(int moves_left)
Returns if the attack is going to be a tired attack.
struct unit * get_diplomatic_defender(const struct unit *act_unit, const struct unit *pvictim, const struct tile *tgt_tile)
Returns the defender of the tile in a diplomatic battle or nullptr if no diplomatic defender could be...
static int defense_multiplication(const struct unit_type *att_type, const struct unit *def, const struct player *def_player, const struct tile *ptile, int defensepower)
Return an increased defensepower.
double win_chance(int as, int ahp, int afp, int ds, int dhp, int dfp)
Returns the chance of the attacker winning, a number between 0 and 1.
int get_total_attack_power(const struct unit *attacker, const struct unit *defender)
Return the modified attack power of a unit.
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?
void get_modified_firepower(const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp)
A unit's effective firepower depend on the situation.
static bool can_player_attack_tile(const struct player *pplayer, const struct tile *ptile)
Checks if player is restricted diplomatically from attacking the tile.
static bool is_unit_reachable_by_unit(const struct unit *defender, const struct unit *attacker)
Can unit attack other.
int get_target_bonus_effects(struct effect_list *plist, 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, enum effect_type effect_type, enum vision_layer vision_layer, enum national_intelligence nintel)
Returns the effect bonus of a given type for any target.
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.
#define fc_assert_ret_val(condition, val)
#define fc_assert_action(condition, action)
#define square_iterate(nmap, center_tile, radius, tile_itr)
#define square_iterate_end
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Return TRUE iff a unit of the given unit type can "exist" at this location.
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
This tile is native to unit.
bool unit_can_defend_here(const struct civ_map *nmap, const struct unit *punit)
Return TRUE iff the unit can be a defender at its current location.
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)
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players can attack each other.
struct packet_game_info info
struct veteran_system * veteran
struct unit_type::@83 cache
int defense_mp_bonuses_pct[U_LAST]
struct combat_bonus_list * bonuses
enum unit_activity activity
bool tile_has_extra_flag(const struct tile *ptile, enum extra_flag_id flag)
Check if tile contains extra providing effect.
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.
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base native for unit.
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
#define tile_terrain(_tile)
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
bool unit_transported(const struct unit *pcargo)
Returns TRUE iff the unit is transported.
#define unit_list_iterate(unitlist, punit)
#define unit_list_iterate_end
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
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_act_when_ustate(const struct unit_type *punit_type, const action_id act_id, const enum ustate_prop prop, const bool is_there)
Return TRUE iff the unit type can do the specified (action enabler controlled) action while its 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_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Unit_type_id utype_index(const struct unit_type *punittype)
Return the unit type index.
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...
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Return veteran level properties of given unit in given veterancy level.
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
#define combat_bonus_list_iterate_end
#define combat_bonus_list_iterate(bonuslist, pbonus)
static bool utype_has_flag(const struct unit_type *punittype, int flag)
#define uclass_index(_c_)