Passed
Pull Request — master (#193)
by Vincent
11:12
created

valid(CastSimulation)   B

Complexity

Conditions 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
dl 0
loc 18
ccs 8
cts 8
cp 1
crap 6
rs 8.6666
c 1
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.simulation.CastSimulation;
24
import fr.quatrevieux.araknemu.game.fight.ai.simulation.Simulator;
25
import fr.quatrevieux.araknemu.game.fight.turn.action.Action;
26
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
27
28
import java.util.Optional;
29
30
/**
31
 * Try to boost allies (or self)
32
 *
33
 * Self boost is priorized to allies boost.
34
 * The selected spell must, at least, boost allies or self.
35
 */
36
public final class Boost implements ActionGenerator, CastSpell.SimulationSelector {
37
    private final CastSpell generator;
38
    private final double selfBoostRate;
39
    private final double alliesBoostRate;
40
    private final int minDuration;
41
    private final boolean allowWithoutDelay;
42
43 1
    public Boost(Simulator simulator, double selfBoostRate, double alliesBoostRate, int minDuration, boolean allowWithoutDelay) {
44 1
        this.generator = new CastSpell(simulator, this);
45
46 1
        this.selfBoostRate = selfBoostRate;
47 1
        this.alliesBoostRate = alliesBoostRate;
48 1
        this.minDuration = minDuration;
49 1
        this.allowWithoutDelay = allowWithoutDelay;
50 1
    }
51
52
    @Override
53
    public void initialize(AI ai) {
54 1
        generator.initialize(ai);
55 1
    }
56
57
    @Override
58
    public Optional<Action> generate(AI ai) {
59 1
        return generator.generate(ai);
60
    }
61
62
    @Override
63
    public boolean valid(CastSimulation simulation) {
64
        // @todo spell filter on interface
0 ignored issues
show
introduced by
Comment matches to-do format '(TODO:)|(@todo )'.
Loading history...
65 1
        if (!allowWithoutDelay && simulation.spell().constraints().launchDelay() <= 1) {
66 1
            return false;
67
        }
68
69 1
        if (simulation.spell().effects().stream().map(SpellEffect::duration).noneMatch(duration -> duration >= minDuration)) {
70 1
            return false;
71
        }
72
73 1
        if (simulation.suicideProbability() > 0 || simulation.killedAllies() > 0) {
74 1
            return false;
75
        }
76
77 1
        final double totalBoost = simulation.alliesBoost() + simulation.selfBoost();
78
79 1
        return totalBoost > 0 && totalBoost + simulation.alliesLife() + simulation.selfLife() > 0;
80
    }
81
82
    @Override
83
    public boolean compare(CastSimulation a, CastSimulation b) {
84 1
        return score(a) > score(b);
85
    }
86
87
    /**
88
     * Compute the score for the given simulation
89
     *
90
     * @param simulation The simulation result
91
     *
92
     * @return The score of the simulation
93
     */
94
    private double score(CastSimulation simulation) {
95 1
        double score =
96 1
            + simulation.alliesBoost() * alliesBoostRate
97 1
            + simulation.selfBoost() * selfBoostRate
98 1
            - simulation.enemiesBoost()
99
        ;
100
101 1
        if (simulation.alliesLife() < 0) {
102 1
            score += simulation.alliesLife();
103
        }
104
105 1
        if (simulation.selfLife() < 0) {
106
            score += simulation.selfLife();
107
        }
108
109 1
        return score / simulation.spell().apCost();
110
    }
111
112
    /**
113
     * Configure boost action with prioritization of self boost
114
     * And allow only long effects (>= 2 turns) to permit usage before {@link Attack}
115
     */
116
    public static Boost self(Simulator simulator) {
117 1
        return new Boost(simulator, 2d, 1d, 2, false);
118
    }
119
120
    /**
121
     * Configure boost action with prioritization of allies boost
122
     * Temporary effects (1 turn) are allowed.
123
     * This action must be declared after {@link Attack}
124
     */
125
    public static Boost allies(Simulator simulator) {
126 1
        return new Boost(simulator, 0.5d, 2d, 1, true);
127
    }
128
}
129