Skip to content

Commit

Permalink
Bottleneck Defense Micro AI: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsc committed Apr 19, 2014
1 parent 992ebe1 commit 83d23fa
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 222 deletions.
54 changes: 26 additions & 28 deletions data/ai/micro_ais/cas/ca_bottleneck_attack.lua
Expand Up @@ -4,75 +4,73 @@ local H = wesnoth.require "lua/helper.lua"
local ca_bottleneck_attack = {}

function ca_bottleneck_attack:evaluation(ai, cfg, self)
-- All units with attacks_left and enemies next to them
-- This will be much easier once the 'attacks' variable is implemented
local attackers = AH.get_units_with_attacks {
side = wesnoth.current.side,
{ "filter_adjacent", {
{ "filter_side", { { "enemy_of", {side = wesnoth.current.side} } } }
} }
}
--print("\n\nAttackers:",#attackers)
if (not attackers[1]) then return 0 end

-- Now loop through the attackers, and all attacks for each
local max_rating, best_att, best_tar, best_weapon = 0, {}, {}, -1
for i,a in ipairs(attackers) do
local max_rating, best_attacker, best_target, best_weapon = -9e99
for _,attacker in ipairs(attackers) do
local targets = wesnoth.get_units {
{ "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } },
{ "filter_adjacent", { id = a.id } }
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "filter_adjacent", { id = attacker.id } }
}
--print(" ",a.id,#targets)

for j,t in ipairs(targets) do
for _,target in ipairs(targets) do
local n_weapon = 0
for weapon in H.child_range(a.__cfg, "attack") do
for weapon in H.child_range(attacker.__cfg, "attack") do
n_weapon = n_weapon + 1

local att_stats, def_stats = wesnoth.simulate_combat(a, n_weapon, t)
local att_stats, def_stats = wesnoth.simulate_combat(attacker, n_weapon, target)

local rating = 0
local rating
-- This is an acceptable attack if:
-- 1. There is no counter attack
-- 2. Probability of death is >=67% for enemy, 0% for attacker
if (att_stats.hp_chance[a.hitpoints] == 1)
if (att_stats.hp_chance[attacker.hitpoints] == 1)
or ((def_stats.hp_chance[0] >= 0.67) and (att_stats.hp_chance[0] == 0))
then
rating = 1000 + t.max_hitpoints + def_stats.hp_chance[0]*100 + att_stats.average_hp - def_stats.average_hp
-- if there's a chance to make the kill, unit closest to leveling up goes first, otherwise the other way around
rating = target.max_hitpoints + def_stats.hp_chance[0] * 100
rating = rating + att_stats.average_hp - def_stats.average_hp

-- If there's a chance to make the kill, unit closest to leveling up goes first,
-- otherwise the other way around
if (def_stats.hp_chance[0] >= 0.67) then
rating = rating + (a.experience - a.max_experience) / 10.
rating = rating + (attacker.experience - attacker.max_experience) / 10.
else
rating = rating - (a.experience - a.max_experience) / 10.
rating = rating - (attacker.experience - attacker.max_experience) / 10.
end
end
--print(a.id, t.id,weapon.name, rating)
if rating > max_rating then
max_rating, best_att, best_tar, best_weapon = rating, a, t, n_weapon

if rating and (rating > max_rating) then
max_rating = rating
best_attacker, best_target, best_weapon = attacker, target, n_weapon
end
end
end
end
--print("Best attack:",best_att.id, best_tar.id, max_rating, best_weapon)

if max_rating == 0 then
if (not best_attacker) then
-- In this case we take attacks away from all units
-- This is done so that the RCA AI CAs can be kept in place
self.data.BD_bottleneck_attacks_done = true
else
self.data.BD_bottleneck_attacks_done = false
self.data.BD_attacker = best_att
self.data.BD_target = best_tar
self.data.BD_attacker = best_attacker
self.data.BD_target = best_target
self.data.BD_weapon = best_weapon
end

return cfg.ca_score
end

function ca_bottleneck_attack:execution(ai, cfg, self)
if self.data.BD_bottleneck_attacks_done then
local units = AH.get_units_with_attacks { side = wesnoth.current.side }
for i,u in ipairs(units) do
AH.checked_stopunit_attacks(ai, u)
for _,unit in ipairs(units) do
AH.checked_stopunit_attacks(ai, unit)
end
else
AH.checked_attack(ai, self.data.BD_attacker, self.data.BD_target, self.data.BD_weapon)
Expand Down

0 comments on commit 83d23fa

Please sign in to comment.