generate(AI,AiActionFactory)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
crap 2
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.arakne.utils.maps.CoordinateCell;
23
import fr.quatrevieux.araknemu.game.fight.ai.AI;
24
import fr.quatrevieux.araknemu.game.fight.ai.action.util.Movement;
25
import fr.quatrevieux.araknemu.game.fight.ai.util.AIHelper;
26
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
27
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
28
import fr.quatrevieux.araknemu.game.fight.turn.action.Action;
29
import org.checkerframework.checker.nullness.util.NullnessUtil;
30
31
import java.util.Collections;
32
import java.util.List;
33
import java.util.Optional;
34
import java.util.stream.Collectors;
35
36
/**
37
 * Try to move far all enemies
38
 *
39
 * The selected cell is the cell with the highest distance from the nearest enemy
40
 * Select only cells with higher distance than current cell
41
 */
42
public final class MoveFarEnemies<F extends ActiveFighter> implements ActionGenerator<F> {
43
    private final Movement<F> movement;
44
45 1
    private List<CoordinateCell<BattlefieldCell>> enemiesCells = Collections.emptyList();
46
47
    @SuppressWarnings("methodref.receiver.bound")
48 1
    public MoveFarEnemies() {
49 1
        movement = new Movement<>(this::score, scoredCell -> true);
50 1
    }
51
52
    @Override
53
    public void initialize(AI<F> ai) {
54 1
        movement.initialize(ai);
55 1
    }
56
57
    @Override
58
    public Optional<Action> generate(AI<F> ai, AiActionFactory actions) {
59 1
        final AIHelper helper = ai.helper();
60
61 1
        if (!helper.canMove()) {
62 1
            return Optional.empty();
63
        }
64
65 1
        enemiesCells = helper.enemies().cells().map(BattlefieldCell::coordinate).collect(Collectors.toList());
66
67 1
        return movement.generate(ai, actions);
68
    }
69
70
    /**
71
     * The score function
72
     *
73
     * Select the highest distance
74
     */
75
    private double score(CoordinateCell<BattlefieldCell> cell) {
76 1
        return NullnessUtil.castNonNull(enemiesCells).stream().mapToDouble(cell::distance).min().orElse(0);
77
    }
78
}
79