|
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.util; |
|
21
|
|
|
|
|
22
|
|
|
import fr.arakne.utils.maps.CoordinateCell; |
|
23
|
|
|
import fr.arakne.utils.maps.path.Decoder; |
|
24
|
|
|
import fr.arakne.utils.maps.path.Path; |
|
25
|
|
|
import fr.arakne.utils.maps.path.PathException; |
|
26
|
|
|
import fr.arakne.utils.maps.path.Pathfinder; |
|
27
|
|
|
import fr.quatrevieux.araknemu.game.fight.ai.AI; |
|
28
|
|
|
import fr.quatrevieux.araknemu.game.fight.ai.action.ActionGenerator; |
|
29
|
|
|
import fr.quatrevieux.araknemu.game.fight.ai.action.AiActionFactory; |
|
30
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter; |
|
31
|
|
|
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell; |
|
32
|
|
|
import fr.quatrevieux.araknemu.game.fight.turn.action.Action; |
|
33
|
|
|
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; |
|
34
|
|
|
import org.checkerframework.checker.nullness.util.NullnessUtil; |
|
35
|
|
|
|
|
36
|
|
|
import java.util.ArrayList; |
|
37
|
|
|
import java.util.List; |
|
38
|
|
|
import java.util.Optional; |
|
39
|
|
|
import java.util.function.Predicate; |
|
40
|
|
|
import java.util.function.ToDoubleFunction; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Try to select the best move action |
|
44
|
|
|
* |
|
45
|
|
|
* - Select reachable (i.e. walkable cells in MP range) cells. Note : pathfinding is not performed, so it's not warranty that cells are really reachable |
|
46
|
|
|
* - Sort cell by the score (ascending, lower score cells selected first) |
|
47
|
|
|
* - Iterates selected cells |
|
48
|
|
|
* - Filter cells (using filter predicate) |
|
49
|
|
|
* - Try to find the path |
|
50
|
|
|
* - If a path is found, and can be performed by the current number of MPs, return the move action |
|
51
|
|
|
*/ |
|
52
|
|
|
public final class Movement<F extends ActiveFighter> implements ActionGenerator<F> { |
|
53
|
|
|
private final ToDoubleFunction<CoordinateCell<BattlefieldCell>> scoreFunction; |
|
54
|
|
|
private final Predicate<ScoredCell> filter; |
|
55
|
|
|
|
|
56
|
|
|
private @MonotonicNonNull Pathfinder<BattlefieldCell> pathfinder; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Creates the Movement action generator |
|
60
|
|
|
* |
|
61
|
|
|
* @param scoreFunction The score function. The returned score is used for select the best cell. Higher score are selected first. |
|
62
|
|
|
* @param filter The selection cell filter |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public Movement(ToDoubleFunction<CoordinateCell<BattlefieldCell>> scoreFunction, Predicate<ScoredCell> filter) { |
|
65
|
1 |
|
this.scoreFunction = scoreFunction; |
|
66
|
1 |
|
this.filter = filter; |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
@Override |
|
70
|
|
|
public void initialize(AI<F> ai) { |
|
71
|
1 |
|
this.pathfinder = new Decoder<>(ai.map()).pathfinder(); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
@Override |
|
75
|
|
|
public Optional<Action> generate(AI<F> ai, AiActionFactory actions) { |
|
76
|
1 |
|
final Pathfinder<BattlefieldCell> pathfinder = NullnessUtil.castNonNull(this.pathfinder); |
|
|
|
|
|
|
77
|
1 |
|
final int movementPoints = ai.turn().points().movementPoints(); |
|
78
|
1 |
|
final List<ScoredCell> selectedCells = selectCells(ai, movementPoints); |
|
79
|
|
|
|
|
80
|
1 |
|
final CoordinateCell<BattlefieldCell> currentCell = ai.fighter().cell().coordinate(); |
|
81
|
1 |
|
final ScoredCell currentCellScore = new ScoredCell(currentCell, scoreFunction.applyAsDouble(currentCell)); |
|
82
|
1 |
|
final boolean currentCellIsValid = filter.test(currentCellScore); |
|
83
|
|
|
|
|
84
|
1 |
|
selectedCells.sort(ScoredCell::compareTo); |
|
85
|
|
|
|
|
86
|
1 |
|
for (ScoredCell cell : selectedCells) { |
|
87
|
1 |
|
if ((currentCellIsValid && currentCellScore.score() >= cell.score()) || !filter.test(cell)) { |
|
88
|
1 |
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
final Path<BattlefieldCell> path; |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
1 |
|
path = pathfinder.findPath(ai.fighter().cell(), cell.coordinates.cell()); |
|
95
|
|
|
} catch (PathException e) { |
|
96
|
|
|
// No valid path can be found |
|
97
|
|
|
continue; |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
// The path contains, as first step, the current cell. |
|
101
|
|
|
// So this steps must not be considered in MPs |
|
102
|
1 |
|
if (path.size() - 1 > movementPoints) { |
|
103
|
1 |
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
return Optional.of(actions.move(path)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
return Optional.empty(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Select all reachable cells for movement |
|
114
|
|
|
*/ |
|
115
|
|
|
private List<ScoredCell> selectCells(AI<F> ai, int movementPoints) { |
|
116
|
1 |
|
final CoordinateCell<BattlefieldCell> currentCell = ai.fighter().cell().coordinate(); |
|
117
|
1 |
|
final List<ScoredCell> selectedCells = new ArrayList<>(); |
|
118
|
|
|
|
|
119
|
1 |
|
for (BattlefieldCell cell : ai.map()) { |
|
120
|
1 |
|
if (!cell.walkable()) { |
|
121
|
1 |
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
final CoordinateCell<BattlefieldCell> coordinates = cell.coordinate(); |
|
125
|
|
|
|
|
126
|
1 |
|
if (coordinates.distance(currentCell) > movementPoints) { |
|
127
|
1 |
|
continue; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
selectedCells.add(new ScoredCell(coordinates, scoreFunction.applyAsDouble(coordinates))); |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
return selectedCells; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public static final class ScoredCell implements Comparable<ScoredCell> { |
|
137
|
|
|
private final CoordinateCell<BattlefieldCell> coordinates; |
|
138
|
|
|
private final double score; |
|
139
|
|
|
|
|
140
|
1 |
|
public ScoredCell(CoordinateCell<BattlefieldCell> coordinates, double score) { |
|
141
|
1 |
|
this.coordinates = coordinates; |
|
142
|
1 |
|
this.score = score; |
|
143
|
1 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get the cell coordinates |
|
147
|
|
|
*/ |
|
148
|
|
|
public CoordinateCell<BattlefieldCell> coordinates() { |
|
149
|
1 |
|
return coordinates; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the cell score |
|
154
|
|
|
* Higher score are prioritized |
|
155
|
|
|
*/ |
|
156
|
|
|
public double score() { |
|
157
|
1 |
|
return score; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
@Override |
|
161
|
|
|
public int compareTo(ScoredCell o) { |
|
162
|
1 |
|
return Double.compare(o.score, score); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|