1local ns = {}
2setmetatable(ns, {__index = _G})
3andromalius_ai = ns;
4setfenv(1, ns);
5
6-- Useful common functions
7-- -----------------------
8function _GetHeroWithMostSP(battle_instance)
9    local id = 0;
10    local most_sp = -1;
11    local id_most_sp = -1;
12    local nb_heroes = battle_instance:GetNumberOfCharacters();
13    while id < nb_heroes do
14        local hero = battle_instance:GetCharacterActor(id);
15        if (hero ~= nil and hero:CanFight() == true) then
16            if (hero:GetSkillPoints() > most_sp) then
17                most_sp = hero:GetSkillPoints();
18                id_most_sp = id;
19            end
20        end
21    id = id + 1;
22    end
23
24    -- Default choice if everything failed.
25    if (id_most_sp == -1) then
26        id_most_sp = 0;
27    end
28
29    return battle_instance:GetCharacterActor(id_most_sp);
30end
31
32function _GetHeroWithLessHP(battle_instance)
33    local id = 0;
34    local less_hp = 100000;
35    local id_less_hp = -1;
36    local nb_heroes = battle_instance:GetNumberOfCharacters();
37    while id < nb_heroes do
38        local hero = battle_instance:GetCharacterActor(id);
39        if (hero ~= nil and hero:CanFight() == true) then
40            if (hero:GetHitPoints() < less_hp) then
41                less_hp = hero:GetHitPoints();
42                id_less_hp = id;
43            end
44        end
45    id = id + 1;
46    end
47
48    -- Default choice if everything failed.
49    if (id_less_hp == -1) then
50        id_less_hp = 0;
51    end
52
53    return battle_instance:GetCharacterActor(id_less_hp);
54end
55
56
57-- Skills
58-- 1014,  -- Shake - (atk + AGI -) - 0 SP
59-- 10100, -- Fire - 7 SP
60-- 10007, -- Magical poison - 40 SP
61-- 21002, -- Dark Wish (Revives an ally) - 10 SP
62
63-- The battle_actor parameter is the enemy thinking, useful to exclude itself from other opponents.
64function DecideAction(battle_instance, battle_actor)
65    local Battle = battle_instance;
66
67    local nb_enemies = Battle:GetNumberOfEnemies();
68    local nb_heroes = Battle:GetNumberOfCharacters();
69
70    local index = 0
71    local dead_enemies = false;
72    while index < nb_enemies do
73        local enemy = Battle:GetEnemyActor(index);
74        if (enemy == nil) then
75            break;
76        end
77
78        if (enemy:IsAlive() == false) then
79            dead_enemies = true;
80
81            -- Let's revive Dorver
82            if (battle_actor:GetSkillPoints() >= 10) then
83                battle_actor:SetAction(21002, enemy); -- Dark Wish to revive the enemy ally.
84                return;
85            end
86        end
87
88        index = index + 1;
89    end
90
91    if (math.random(0, 100) > 50 and battle_actor:GetSkillPoints() >= 40) then
92        -- Magical Poison on the hero with most SP
93        battle_actor:SetAction(10007, _GetHeroWithMostSP(Battle));
94        return;
95    elseif (battle_actor:GetSkillPoints() >= 7) then
96        -- Fire on the hero with less HP
97        battle_actor:SetAction(10100, _GetHeroWithLessHP(Battle));
98        return;
99    end
100
101    -- Default attack - Shake
102    battle_actor:SetAction(1014, _GetHeroWithLessHP(Battle));
103
104end
105