fr.quatrevieux.araknemu.game.fight.ai.util.FightersHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
dl 0
loc 98
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A adjacent() 0 2 1
A nearest() 0 8 1
A stream() 0 2 1
A FightersHelper(AIHelper,AI,Predicate) 0 4 1
A adjacent(BattlefieldCell) 0 7 1
A inRange(Interval) 0 4 1
A cells() 0 2 1
A count() 0 2 1
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.CoordinateCell;
23
import fr.arakne.utils.value.Interval;
24
import fr.quatrevieux.araknemu.game.fight.ai.AI;
25
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
26
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
27
28
import java.util.Comparator;
29
import java.util.Objects;
30
import java.util.Optional;
31
import java.util.function.Predicate;
32
import java.util.stream.Stream;
33
34
/**
35
 * Utility class for perform operations on fighters
36
 *
37
 * @see AIHelper#enemies()
38
 * @see AIHelper#allies()
39
 */
40
public final class FightersHelper {
41
    private final AIHelper helper;
42
    private final AI ai;
43
    private final Predicate<FighterData> filter;
44
45 1
    FightersHelper(AIHelper helper, AI ai, Predicate<FighterData> filter) {
46 1
        this.helper = helper;
47 1
        this.ai = ai;
48 1
        this.filter = filter;
49 1
    }
50
51
    /**
52
     * Get a stream of all alive fighters which match with filter given at constructor
53
     *
54
     * @return Stream of fighters
55
     */
56
    public Stream<? extends FighterData> stream() {
0 ignored issues
show
Comprehensibility introduced by
Remove usage of generic wildcard type.
Loading history...
57 1
        return ai.fighters().filter(filter);
58
    }
59
60
    /**
61
     * Get fighters adjacent to the given cell
62
     *
63
     * @param cell Cell to check
64
     *
65
     * @return Stream of fighters
66
     *
67
     * @see CellsHelper#adjacent(BattlefieldCell)
68
     */
69
    @SuppressWarnings("methodref.return") // BattlefieldCell::fighter return a nullable fighter
70
    public Stream<FighterData> adjacent(BattlefieldCell cell) {
71 1
        return helper.cells().adjacent(cell)
72 1
            .map(BattlefieldCell::fighter)
73 1
            .filter(Objects::nonNull)
74 1
            .filter(fighter -> !fighter.hidden())
75 1
            .filter(filter)
76
        ;
77
    }
78
79
    /**
80
     * Get fighters adjacent to the current cell
81
     * Note: if there is at lease one adjacent enemy, the current fighter can be tackled
82
     *
83
     * @return Stream of fighters
84
     *
85
     * @see CellsHelper#adjacent()
86
     */
87
    public Stream<FighterData> adjacent() {
88 1
        return adjacent(ai.fighter().cell());
89
    }
90
91
    /**
92
     * Get the nearest fighter
93
     * If multiple fighters have the same distance, the fighter with lower HP will be returned
94
     *
95
     * @return The nearest fighter
96
     */
97
    public Optional<? extends FighterData> nearest() {
0 ignored issues
show
Comprehensibility introduced by
Remove usage of generic wildcard type.
Loading history...
98 1
        final CoordinateCell<BattlefieldCell> currentCell = ai.fighter().cell().coordinate();
99
100 1
        return stream()
101 1
            .filter(fighter -> !fighter.hidden())
102 1
            .min(Comparator
103 1
                .<FighterData>comparingInt(f -> currentCell.distance(f.cell()))
104 1
                .thenComparingInt(f -> f.life().current())
105
            )
106
        ;
107
    }
108
109
    /**
110
     * Get cells of all fighters
111
     *
112
     * @return Stream of cells
113
     */
114
    public Stream<BattlefieldCell> cells() {
115 1
        return stream().filter(fighter -> !fighter.hidden()).map(FighterData::cell);
116
    }
117
118
    /**
119
     * Get all fighters which are in the given range
120
     *
121
     * @param range Range to check
122
     *
123
     * @return Stream of fighters
124
     */
125
    public Stream<? extends FighterData> inRange(Interval range) {
0 ignored issues
show
Comprehensibility introduced by
Remove usage of generic wildcard type.
Loading history...
126 1
        final CoordinateCell<BattlefieldCell> currentCell = ai.fighter().cell().coordinate();
127
128 1
        return stream().filter(fighter -> !fighter.hidden()).filter(fighter -> range.contains(currentCell.distance(fighter.cell())));
129
    }
130
131
    /**
132
     * Get the count of fighters which match with the filter given at constructor
133
     *
134
     * @return Number of fighters
135
     */
136
    public int count() {
137 1
        return (int) stream().count();
138
    }
139
}
140