Skip to content

Commit

Permalink
Lua AIs: get unit cost/level directly from proxy unit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattsc committed Aug 27, 2018
1 parent 3f54908 commit 8af988c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions data/ai/lua/battle_calcs.lua
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions data/ai/lua/ca_high_xp_attack.lua
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion data/ai/lua/generic_rush_engine.lua
Expand Up @@ -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
Expand Down

0 comments on commit 8af988c

Please sign in to comment.