Passed
Push — master ( 95c2ad...b1bd2c )
by Vincent
06:21 queued 12s
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
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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-2020 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.Simulator;
24
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
25
import fr.quatrevieux.araknemu.game.fight.turn.action.Action;
26
import fr.quatrevieux.araknemu.game.fight.turn.action.factory.ActionsFactory;
27
28
import java.util.Optional;
29
30
/**
31
 * Try to move for perform an attack
32
 *
33
 * The nearest cell for perform an attack is selected.
34
 * If the current cell permit attacking, the fighter will not perform any move.
35
 *
36
 * For select the cell, the generator will iterate over all reachable cells
37
 * with the current amount of MPs, sort them by distance,
38
 * and check all spells on all available cells.
39
 * The first matching cell is selected.
40
 */
41
public final class MoveToAttack<F extends ActiveFighter> implements ActionGenerator<F> {
42
    private final MoveToCast<F> generator;
43
    private final Attack<F> attack;
44
45 1
    private MoveToAttack(Simulator simulator, MoveToCast.TargetSelectionStrategy<F> strategy) {
46 1
        this.attack = new Attack<>(simulator);
47 1
        this.generator = new MoveToCast<>(simulator, attack, strategy);
48 1
    }
49
50
    @Override
51
    public void initialize(AI<F> ai) {
52 1
        attack.initialize(ai);
53 1
        generator.initialize(ai);
54 1
    }
55
56
    @Override
57
    public Optional<Action> generate(AI<F> ai, ActionsFactory<F> actions) {
58 1
        return generator.generate(ai, actions);
59
    }
60
61
    /**
62
     * Select the nearest cell where a cast is possible
63
     *
64
     * Note: This selected cell is not the best cell for perform an attack, but the nearest cell.
65
     *       So, it do not perform the best move for maximize damage.
66
     */
67
    public static <F extends ActiveFighter> MoveToAttack<F> nearest(Simulator simulator) {
68 1
        return new MoveToAttack<>(simulator, new MoveToCast.NearestStrategy<>());
69
    }
70
71
    /**
72
     * Select the best target cell for cast a spell, and maximizing damage
73
     */
74
    public static <F extends ActiveFighter> MoveToAttack<F> bestTarget(Simulator simulator) {
75 1
        return new MoveToAttack<>(simulator, new MoveToCast.BestTargetStrategy<>());
76
    }
77
}
78