self(Simulator)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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