Lua AIs: get unit cost/level directly from proxy unit

These used to be accessible only through unit.__cfg or wesnoth.unit_types.  The Fast Micro AI is not included here as it requires a larger clean-up.

(cherry-picked from commit 8af988c697)
This commit is contained in:
mattsc 2018-08-27 12:34:29 -07:00
parent 1166bfb485
commit b5c8c60ad0
3 changed files with 9 additions and 9 deletions

View file

@ -837,7 +837,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- In addition, potentially leveling up in this attack is a huge bonus,
-- proportional to the chance of it happening and the chance of not dying itself
local level_bonus = 0.
local defender_level = wesnoth.unit_types[defender.type].level
local defender_level = defender.level
if (attacker.max_experience - attacker.experience <= defender_level) then
level_bonus = 1. - att_stats.hp_chance[0]
else
@ -849,7 +849,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- Now convert this into gold-equivalent value
local attacker_value = wesnoth.unit_types[attacker.type].cost
local attacker_value = attacker.cost
-- Being closer to leveling is good (this makes AI prefer units with lots of XP)
local xp_bonus = attacker.experience / attacker.max_experience
@ -886,7 +886,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
-- In addition, the defender potentially leveling up in this attack is a huge penalty,
-- proportional to the chance of it happening and the chance of not dying itself
local defender_level_penalty = 0.
local attacker_level = wesnoth.unit_types[attacker.type].level
local attacker_level = attacker.level
if (defender.max_experience - defender.experience <= attacker_level) then
defender_level_penalty = 1. - def_stats.hp_chance[0]
else
@ -897,7 +897,7 @@ function battle_calcs.attack_rating(attacker, defender, dst, cfg, cache)
value_fraction = value_fraction - defender_level_penalty * defender_level_weight
-- Now convert this into gold-equivalent value
local defender_value = wesnoth.unit_types[defender.type].cost
local defender_value = defender.cost
-- If this is the enemy leader, make damage to it much more important
if defender.canrecruit then
@ -1011,7 +1011,7 @@ function battle_calcs.attack_combo_stats(tmp_attackers, tmp_dsts, defender, cach
--for hp,p in pairs(tmp_def_stats[i].hp_chance) do
-- if (p > 0) then
-- local dhp_norm = (hp - av) / defender.max_hitpoints * wesnoth.unit_types[defender.type].cost
-- local dhp_norm = (hp - av) / defender.max_hitpoints * defender.cost
-- local dvar = p * dhp_norm^2
-- outcome_variance = outcome_variance + dvar
-- n_outcomes = n_outcomes + 1
@ -1028,7 +1028,7 @@ function battle_calcs.attack_combo_stats(tmp_attackers, tmp_dsts, defender, cach
-- Almost, bonus should not be quite as high as a really high CTK
-- This isn't quite true in reality, but can be refined later
if AH.has_weapon_special(attacker, "slow") then
rating = rating + wesnoth.unit_types[defender.type].cost / 2.
rating = rating + defender.cost / 2.
end
ratings[i] = { i, rating, base_rating, def_rating, att_rating }

View file

@ -242,14 +242,14 @@ function ca_attack_highxp:evaluation(cfg, data)
rating = 1000
local enemy_value_loss = (target.hitpoints - def_stats.average_hp) / target.max_hitpoints
enemy_value_loss = enemy_value_loss * wesnoth.unit_types[target.type].cost
enemy_value_loss = enemy_value_loss * target.cost
-- We want the _least_ damage to the enemy, so the minus sign is no typo!
rating = rating - enemy_value_loss
local own_value_loss = (attacker_copy.hitpoints - att_stats.average_hp) / attacker_copy.max_hitpoints
own_value_loss = own_value_loss + att_stats.hp_chance[0]
own_value_loss = own_value_loss * wesnoth.unit_types[attacker_copy.type].cost
own_value_loss = own_value_loss * attacker_copy.cost
rating = rating - own_value_loss

View file

@ -436,7 +436,7 @@ return {
local on_village = wesnoth.get_terrain_info(wesnoth.get_terrain(defender.x, defender.y)).village
-- Also, poisoning units that would level up through the attack or could level on their turn as a result is very bad
local about_to_level = defender.max_experience - defender.experience <= (wesnoth.unit_types[attacker.type].level * 2)
local about_to_level = defender.max_experience - defender.experience <= (attacker.level * 2)
if (not cant_poison) and (not on_village) and (not about_to_level) then
-- Strongest enemy gets poisoned first