-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
ca_aggressive_attack_no_suicide.lua
60 lines (44 loc) · 1.83 KB
/
ca_aggressive_attack_no_suicide.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_aggressive_attack_no_suicide = {}
function ca_aggressive_attack_no_suicide:evaluation(cfg, data)
local units = wesnoth.get_units {
side = wesnoth.current.side,
formula = 'attacks_left > 0'
}
if (not units[1]) then return 0 end
-- Get all possible attacks
local attacks = AH.get_attacks(units, { include_occupied = true })
if (not attacks[1]) then return 0 end
-- Now find the best of the possible attacks
local max_rating, best_attack = -9e99, {}
for i, att in ipairs(attacks) do
local attacker = wesnoth.get_unit(att.src.x, att.src.y)
local defender = wesnoth.get_unit(att.target.x, att.target.y)
local attacker_dst = attacker:clone()
attacker_dst.x, attacker_dst.y = att.dst.x, att.dst.y
local att_stats, def_stats = wesnoth.simulate_combat(attacker_dst, defender)
if (att_stats.hp_chance[0] == 0) then
local rating = def_stats.hp_chance[0] * 100
local attacker_damage = attacker.hitpoints - att_stats.average_hp
local defender_damage = defender.hitpoints - def_stats.average_hp
rating = rating + defender_damage
rating = rating - attacker_damage / 10.
-- Also, take strongest unit first
rating = rating + attacker.hitpoints / 10.
if (rating > max_rating) then
max_rating = rating
best_attack = att
end
end
end
if (max_rating > -9e99) then
data.attack = best_attack
return 100000
end
return 0
end
function ca_aggressive_attack_no_suicide:execution(cfg, data)
AH.robust_move_and_attack(ai, data.attack.src, data.attack.dst, data.attack.target)
data.attack = nil
end
return ca_aggressive_attack_no_suicide