Passed
Pull Request — master (#249)
by Vincent
13:26
created

generate(AI,ActionsFactory)   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.ai.action;
21
22
import fr.quatrevieux.araknemu.game.fight.ai.AI;
23
import fr.quatrevieux.araknemu.game.fight.ai.action.util.CastSpell;
24
import fr.quatrevieux.araknemu.game.fight.ai.simulation.CastSimulation;
25
import fr.quatrevieux.araknemu.game.fight.ai.simulation.Simulator;
26
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
27
import fr.quatrevieux.araknemu.game.fight.turn.action.Action;
28
import fr.quatrevieux.araknemu.game.fight.turn.action.factory.ActionsFactory;
29
30
import java.util.Optional;
31
32
/**
33
 * Try to attack enemies
34
 *
35
 * Select spells causing damage on enemies
36
 * All cells are tested for select the most effective target for each spells
37
 */
38
public final class Attack<F extends ActiveFighter> implements ActionGenerator<F>, CastSpell.SimulationSelector {
39
    private final CastSpell<F> generator;
40
    private final SuicideStrategy suicideStrategy;
41
42 1
    private double averageEnemyLifePoints = 0;
43 1
    private int enemiesCount = 0;
44
45
    public Attack(Simulator simulator) {
46 1
        this(simulator, SuicideStrategy.IF_KILL_ENEMY);
47 1
    }
48
49
    @SuppressWarnings({"assignment", "argument"})
50 1
    public Attack(Simulator simulator, SuicideStrategy suicideStrategy) {
51 1
        this.generator = new CastSpell<>(simulator, this);
52 1
        this.suicideStrategy = suicideStrategy;
53 1
    }
54
55
    @Override
56
    public void initialize(AI<F> ai) {
57 1
        generator.initialize(ai);
58 1
        averageEnemyLifePoints = ai.helper().enemies().stream().mapToInt(fighter -> fighter.life().max()).average().orElse(0);
59 1
        enemiesCount = ai.helper().enemies().count();
60 1
    }
61
62
    @Override
63
    public Optional<Action> generate(AI<F> ai, ActionsFactory<F> actions) {
64 1
        return generator.generate(ai, actions);
65
    }
66
67
    @Override
68
    public boolean valid(CastSimulation simulation) {
69 1
        if (simulation.enemiesLife() >= 0) {
70 1
            return false;
71
        }
72
73
        // Kill all enemies
74 1
        if (simulation.killedEnemies() >= enemiesCount) {
75 1
            return true;
76
        }
77
78 1
        if (!suicideStrategy.allow(simulation)) {
79 1
            return false;
80
        }
81
82
        // Kill more allies than enemies
83 1
        if (simulation.killedAllies() > simulation.killedEnemies()) {
84 1
            return false;
85
        }
86
87
        // At least one enemy will be killed
88 1
        if (simulation.killedEnemies() >= 0.99) {
89 1
            return true;
90
        }
91
92
        // Cause more damage on enemies than allies
93 1
        return simulation.enemiesLife() < simulation.alliesLife() + simulation.selfLife();
94
    }
95
96
    @Override
97
    public boolean compare(CastSimulation a, CastSimulation b) {
98 1
        return score(a) > score(b);
99
    }
100
101
    /**
102
     * Compute the score for the given simulation
103
     *
104
     * @param simulation The simulation result
105
     *
106
     * @return The score of the simulation. 0 is null
107
     */
108
    public double score(CastSimulation simulation) {
109 1
        final double score = damageScore(simulation) + killScore(simulation) + boostScore(simulation);
110
111 1
        return score / simulation.spell().apCost();
112
    }
113
114
    private double damageScore(CastSimulation simulation) {
115 1
        return - simulation.enemiesLife() + simulation.alliesLife() + simulation.selfLife() * 2;
116
    }
117
118
    private double killScore(CastSimulation simulation) {
119 1
        final double killRatio = simulation.killedEnemies()
120 1
            - 1.5 * simulation.killedAllies()
121 1
            - 2 * simulation.suicideProbability()
122
        ;
123
124 1
        return averageEnemyLifePoints * killRatio;
125
    }
126
127
    private double boostScore(CastSimulation simulation) {
128 1
        return (simulation.alliesBoost() + simulation.selfBoost() - simulation.enemiesBoost()) / 10;
129
    }
130
131
    /**
132
     * Filter the cast by the suicide probability
133
     *
134
     * @see CastSimulation#suicideProbability()
135
     */
136 1
    enum SuicideStrategy {
137
        /**
138
         * Always allow suicide
139
         * Should be used on The Sacrificial Doll AI
140
         */
141 1
        ALLOW {
142
            @Override
143
            public boolean allow(CastSimulation simulation) {
144 1
                return true;
145
            }
146
        },
147
148
        /**
149
         * Suicide is never accepted
150
         */
151 1
        DENY {
152
            @Override
153
            public boolean allow(CastSimulation simulation) {
154 1
                return simulation.suicideProbability() <= 0;
155
            }
156
        },
157
158
        /**
159
         * Suicide is accepted only if there is more chance (or number) to kill an enemy
160
         */
161 1
        IF_KILL_ENEMY {
162
            @Override
163
            public boolean allow(CastSimulation simulation) {
164 1
                return simulation.suicideProbability() <= simulation.killedEnemies();
165
            }
166
        },
167
        ;
168
169
        /**
170
         * Does the simulation is allowed about the suicide probability ?
171
         *
172
         * @param simulation The cast simulation to check
173
         *
174
         * @return false if the cast must not be performed
175
         */
176
        public abstract boolean allow(CastSimulation simulation);
177
    }
178
}
179