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