Lua AIs: fix uses of new unit functions

I switched to using defense_on() and resistance_against() incorrectly in 55478e20. I had tested these, but apparently for cases when it does not make a difference ...

The change for movement_on() is for convenience and consistency with other code only.
This commit is contained in:
mattsc 2019-12-18 09:23:23 -08:00
parent f1e8807330
commit 68bb48ddf1
11 changed files with 29 additions and 28 deletions

View file

@ -1515,12 +1515,12 @@ function ai_helper.next_hop(unit, x, y, cfg)
unit_in_way:to_map()
local terrain = wesnoth.get_terrain(next_hop_ideal[1], next_hop_ideal[2])
local move_cost_endpoint = wesnoth.units.movement_on(unit, terrain)
local move_cost_endpoint = unit:movement_on(terrain)
local inverse_reach_map = LS.create()
for _,r in pairs(inverse_reach) do
-- We want the moves left for moving into the opposite direction in which the reach map was calculated
local terrain = wesnoth.get_terrain(r[1], r[2])
local move_cost = wesnoth.units.movement_on(unit, terrain)
local move_cost = unit:movement_on(terrain)
local inverse_cost = r[3] + move_cost - move_cost_endpoint
inverse_reach_map:insert(r[1], r[2], inverse_cost)
end
@ -1723,7 +1723,7 @@ function ai_helper.custom_cost_with_avoid(x, y, prev_cost, unit, avoid_map, ally
local max_moves = unit.max_moves
local terrain = wesnoth.get_terrain(x, y)
local move_cost = wesnoth.units.movement_on(unit, terrain)
local move_cost = unit:movement_on(terrain)
if (move_cost > max_moves) then
return ai_helper.no_path
@ -1809,7 +1809,7 @@ function ai_helper.custom_cost_with_avoid(x, y, prev_cost, unit, avoid_map, ally
defense = H.round(defense / 10) * 10
if (defense > 90) then defense = 90 end
if (defense < 10) then defense = 10 end
move_cost_int = move_cost_int + defense
move_cost_int = move_cost_int + (100 - defense)
-- And finally we add a (very small) penalty for this hex if it is to be avoided
-- This is used for the next hex to determine whether the previous hex was to be
-- avoided via avoid_penalty above.

View file

@ -72,7 +72,7 @@ function battle_calcs.unit_attack_info(unit, cache)
local attack_types = { "arcane", "blade", "cold", "fire", "impact", "pierce" }
for _,attack_type in ipairs(attack_types) do
unit_info.resist_mod[attack_type] = unit:resistance_against(attack_type) / 100.
unit_info.resist_mod[attack_type] = 1 - unit:resistance_against(attack_type) / 100.
end
if cache then cache[cind] = unit_info end
@ -117,6 +117,7 @@ function battle_calcs.strike_damage(attacker, defender, att_weapon, def_weapon,
-- Opponent resistance modifier
local att_multiplier = defender_info.resist_mod[attacker_info.attacks[att_weapon].type] or 1
-- TOD modifier
att_multiplier = att_multiplier * AH.get_unit_time_of_day_bonus(attacker_info.alignment, att_lawful_bonus)
@ -676,8 +677,8 @@ function battle_calcs.battle_outcome(attacker, defender, cfg, cache)
if (def_max_hits > att_strikes) then def_max_hits = att_strikes end
-- Probability of landing a hit
local att_hit_prob = defender:defense_on(wesnoth.get_terrain(defender.x, defender.y)) / 100.
local def_hit_prob = attacker:defense_on(wesnoth.get_terrain(dst[1], dst[2])) / 100.
local att_hit_prob = 1 - defender:defense_on(wesnoth.get_terrain(defender.x, defender.y)) / 100.
local def_hit_prob = 1 - attacker:defense_on(wesnoth.get_terrain(dst[1], dst[2])) / 100.
-- Magical: attack and defense, and under all circumstances
if att_attack.magical then att_hit_prob = 0.7 end
@ -923,7 +924,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- We don't need a bonus for good terrain for the attacker, as that is covered in the damage calculation
-- However, we add a small bonus for good terrain defense of the _defender_ on the _attack_ hex
-- This is in order to take good terrain away from defender on next move, all else being equal
local defender_defense = - defender:defense_on(wesnoth.get_terrain(dst[1], dst[2])) / 100.
local defender_defense = - (100 - defender:defense_on(wesnoth.get_terrain(dst[1], dst[2]))) / 100.
defender_value = defender_value + defender_defense * defense_weight
-- Get a very small bonus for hexes in between defender and AI leader
@ -1315,7 +1316,7 @@ function battle_calcs.best_defense_map(units, cfg)
if max_moves then unit.moves = old_moves end
for _,loc in ipairs(reach) do
local defense = 100 - unit:defense_on(wesnoth.get_terrain(loc[1], loc[2]))
local defense = unit:defense_on(wesnoth.get_terrain(loc[1], loc[2]))
if (defense > (defense_map:get(loc[1], loc[2]) or - math.huge)) then
defense_map:insert(loc[1], loc[2], defense)
@ -1523,7 +1524,7 @@ function battle_calcs.get_attack_combos_subset(units, enemy, cfg)
-- Store information about it in 'loc' and add this to 'locs'
-- Want coordinates (dst) and terrain defense (for sorting)
loc.dst = xa * 1000 + ya
loc.hit_prob = unit:defense_on(wesnoth.get_terrain(xa, ya))
loc.hit_prob = 100 - unit:defense_on(wesnoth.get_terrain(xa, ya))
table.insert(locs, loc)
-- Also mark this hex as usable

View file

@ -75,7 +75,7 @@ function ca_spread_poison:evaluation(cfg, data, filter_own)
end
-- More priority to enemies on strong terrain
local defense_rating = (100 - defender:defense_on(defender_terrain)) / 100
local defense_rating = defender:defense_on(defender_terrain) / 100
attacker_rating = attacker_rating * (1 - aggression)
local combat_rating = attacker_rating + defender_rating + additional_poison_rating

View file

@ -85,7 +85,7 @@ return {
local best_defense = 100
for i, terrain in ipairs(terrain_archetypes) do
local defense = unit:defense_on(terrain)
local defense = 100 - unit:defense_on(terrain)
if defense < best_defense then
best_defense = defense
end
@ -196,7 +196,7 @@ return {
-- TODO: calculate chance to hit
-- currently assumes 50% chance to hit using supplied constant
local attacker_resistance = attacker:resistance_against(defender_attack.type)
drain_recovery = (defender_attack.damage*defender_attack.number*attacker_resistance*attacker_defense/2)/10000
drain_recovery = (defender_attack.damage*defender_attack.number*(100-attacker_resistance)*attacker_defense/2)/10000
end
end
end
@ -204,14 +204,14 @@ return {
defense = defense/100.0
local resistance = defender:resistance_against(attack.type)
if steadfast and (resistance < 100) then
resistance = 100 - ((100 - resistance) * 2)
if (resistance < 50) then
if steadfast and (resistance > 0) then
resistance = resistance * 2
if (resistance > 50) then
resistance = 50
end
end
local base_damage = (weapon_damage+damage_bonus)*resistance*damage_multiplier
if (resistance > 100) then
local base_damage = (weapon_damage+damage_bonus)*(100-resistance)*damage_multiplier
if (resistance < 0) then
base_damage = base_damage-1
end
base_damage = math.floor(base_damage/100 + 0.5)
@ -257,7 +257,7 @@ return {
random_gender = false
}
local can_poison = poisonable(unit) and (not unit:ability('regenerate'))
local flat_defense = unit:defense_on("Gt")
local flat_defense = 100 - unit:defense_on("Gt")
local best_defense = get_best_defense(unit)
local recruit = wesnoth.units.create {
@ -266,7 +266,7 @@ return {
name = "X",
random_gender = false
}
local recruit_flat_defense = recruit:defense_on("Gt")
local recruit_flat_defense = 100 - recruit:defense_on("Gt")
local recruit_best_defense = get_best_defense(recruit)
local can_poison_retaliation = poisonable(recruit) and (not recruit:ability('regenerate'))

View file

@ -21,7 +21,7 @@ function retreat_functions.min_hp(unit)
if (caution_factor < 0) then caution_factor = 0 end
caution_factor = math.sqrt(caution_factor) * 2
local hp_per_level = unit:defense_on(wesnoth.get_terrain(unit.x, unit.y))/15 * caution_factor
local hp_per_level = (100 - unit:defense_on(wesnoth.get_terrain(unit.x, unit.y)))/15 * caution_factor
local level = unit.level
-- Leaders are considered to be higher level because of their value
@ -212,7 +212,7 @@ function retreat_functions.get_retreat_injured_units(healees, regen_amounts, avo
rating = rating - enemy_count * 100000
-- Penalty based on terrain defense for unit
rating = rating - u:defense_on(wesnoth.get_terrain(loc[1], loc[2]))/10
rating = rating - (100 - u:defense_on(wesnoth.get_terrain(loc[1], loc[2])))/10
if (loc[1] == u.x) and (loc[2] == u.y) and (not u.status.poisoned) then
if is_healthy or enemy_count == 0 then

View file

@ -95,7 +95,7 @@ function ca_assassin_move:execution(cfg)
-- Penalties for damage by enemies
local enemy_rating_map = LS.create()
enemy_damage_map:iter(function(x, y, enemy_damage)
local hit_chance = (unit:defense_on(wesnoth.get_terrain(x, y))) / 100.
local hit_chance = (100 - unit:defense_on(wesnoth.get_terrain(x, y))) / 100.
local rating = hit_chance * enemy_damage
rating = rating / unit.max_hitpoints

View file

@ -157,7 +157,7 @@ function ca_fast_attack_utils.get_unit_defense(unit_copy, x, y, defense_maps)
if (not defense_maps[unit_copy.id][x]) then defense_maps[unit_copy.id][x] = {} end
if (not defense_maps[unit_copy.id][x][y]) then
local defense = (100. - unit_copy:defense_on(wesnoth.get_terrain(x, y))) / 100.
local defense = unit_copy:defense_on(wesnoth.get_terrain(x, y)) / 100.
defense_maps[unit_copy.id][x][y] = { defense = defense }
end

View file

@ -104,7 +104,7 @@ function ca_healer_move:evaluation(cfg, data)
local is_village = wesnoth.get_terrain_info(terrain).village
if is_village then rating = rating + 2 end
local defense = 100 - healer:defense_on(terrain)
local defense = healer:defense_on(terrain)
rating = rating + defense / 10.
end

View file

@ -47,7 +47,7 @@ function ca_protect_unit_move:execution(cfg, data)
local terrain_defense_map = LS.create()
reach_map:iter(function(x, y, data)
terrain_defense_map:insert(x, y, 100 - unit:defense_on(wesnoth.get_terrain(x, y)))
terrain_defense_map:insert(x, y, unit:defense_on(wesnoth.get_terrain(x, y)))
end)
local goal_distance_map = LS.create()

View file

@ -61,7 +61,7 @@ function ca_stationed_guardian:execution(cfg)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian)
then
local defense = 100 - guardian:defense_on(wesnoth.get_terrain(xa, ya))
local defense = guardian:defense_on(wesnoth.get_terrain(xa, ya))
local nh = AH.next_hop(guardian, xa, ya)
if nh then
if (nh[1] == xa) and (nh[2] == ya) and (defense > best_defense) then

View file

@ -46,7 +46,7 @@ function ca_zone_guardian:execution(cfg)
if (not AH.is_visible_unit(wesnoth.current.side, unit_in_way))
or (unit_in_way == guardian)
then
local defense = 100 - guardian:defense_on(wesnoth.get_terrain(xa, ya))
local defense = guardian:defense_on(wesnoth.get_terrain(xa, ya))
local nh = AH.next_hop(guardian, xa, ya)
if nh then
if (nh[1] == xa) and (nh[2] == ya) and (defense > best_defense) then