Test Failed
Branch master (3c0c2d)
by Vincent
12:24
created

decoder()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 2
rs 10
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.util;
21
22
import fr.arakne.utils.maps.constant.Direction;
23
import fr.arakne.utils.maps.path.Decoder;
24
import fr.arakne.utils.maps.path.Pathfinder;
25
import fr.quatrevieux.araknemu.game.fight.ai.AI;
26
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
27
28
import java.util.Arrays;
29
import java.util.Optional;
30
import java.util.stream.Stream;
31
import java.util.stream.StreamSupport;
32
33
/**
34
 * Utility class for perform operation on map cells
35
 *
36
 * @see AIHelper#cells()
37
 */
38
public final class CellsHelper {
39
    private final AI ai;
40
41
    private Decoder<FightCell> decoder;
42
43 1
    CellsHelper(AI ai) {
44 1
        this.ai = ai;
45 1
    }
46
47
    /**
48
     * Get all available cells
49
     * A cell is considered available when it's walkable without consider fighters
50
     *
51
     * @return Stream of all targetable cells
52
     */
53
    public Stream<FightCell> stream() {
54 1
        return StreamSupport.stream(ai.map().spliterator(), false).filter(FightCell::walkableIgnoreFighter);
55
    }
56
57
    /**
58
     * Get cells which is adjacent to the fighter
59
     * Adjacent cells are cells directly accessible through the four restricted directions (i.e. {@link Direction#restrictedDirections()})
60
     *
61
     * @return Stream of cells adjacent to the current cell
62
     *
63
     * @see FightersHelper#adjacent() To check adjacent fighters
64
     */
65
    public Stream<FightCell> adjacent() {
66 1
        return adjacent(ai.fighter().cell());
67
    }
68
69
    /**
70
     * Get cells which is adjacent to the given cell
71
     * Adjacent cells are cells directly accessible through the four restricted directions (i.e. {@link Direction#restrictedDirections()})
72
     *
73
     * @param cell Cell to check
74
     *
75
     * @return Stream of cells adjacent to the given cell
76
     *
77
     * @see FightersHelper#adjacent(FightCell) To check adjacent fighters
78
     */
79
    public Stream<FightCell> adjacent(FightCell cell) {
80 1
        final Decoder<FightCell> decoder = decoder();
0 ignored issues
show
Comprehensibility introduced by
The variable decodershadows a field with the same name declared in line 41. Consider renaming it.
Loading history...
81
82 1
        return Arrays.stream(Direction.restrictedDirections())
83 1
            .map(direction -> decoder.nextCellByDirection(cell, direction))
84 1
            .filter(Optional::isPresent)
85 1
            .map(Optional::get)
86 1
            .filter(FightCell::walkableIgnoreFighter)
87
        ;
88
    }
89
90
    /**
91
     * Create a new instance of the pathfinder
92
     */
93
    public Pathfinder<FightCell> pathfinder() {
94 1
        return decoder().pathfinder();
95
    }
96
97
    private Decoder<FightCell> decoder() {
98 1
        if (decoder == null) {
99 1
            decoder = new Decoder<>(ai.map());
100
        }
101
102 1
        return decoder;
103
    }
104
}
105