1
|
|
|
/* |
2
|
|
|
* This file is part of ArakneUtils. |
3
|
|
|
* |
4
|
|
|
* ArakneUtils 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
|
|
|
* ArakneUtils 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 ArakneUtils. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.arakne.utils.maps; |
21
|
|
|
|
22
|
|
|
import fr.arakne.utils.maps.constant.Direction; |
23
|
|
|
import org.checkerframework.checker.index.qual.NonNegative; |
24
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull; |
25
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
26
|
|
|
import org.checkerframework.dataflow.qual.Pure; |
27
|
|
|
|
28
|
|
|
import java.util.Objects; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Map cell with coordinates |
32
|
|
|
* |
33
|
|
|
* https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/utils/Pathfinding.as#L191 |
34
|
|
|
*/ |
35
|
|
|
public final class CoordinateCell<C extends @NonNull MapCell> { |
36
|
|
|
private final C cell; |
37
|
|
|
|
38
|
|
|
private final int x; |
39
|
|
|
private final int y; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* CoordinateCell constructor. |
43
|
|
|
* Prefer use {@link MapCell#coordinate()} method instead of directly call the constructor |
44
|
|
|
* |
45
|
|
|
* @param cell The cell to wrap |
46
|
|
|
* |
47
|
|
|
* @see MapCell#coordinate() For get the {@link CoordinateCell} instance from a cell |
48
|
|
|
*/ |
49
|
1 |
|
public CoordinateCell(C cell) { |
50
|
1 |
|
this.cell = cell; |
51
|
|
|
|
52
|
1 |
|
final int width = cell.map().dimensions().width(); |
53
|
1 |
|
final int line = cell.id() / (width * 2 - 1); |
54
|
1 |
|
final int column = cell.id() - line * (width * 2 - 1); |
55
|
1 |
|
final int offset = column % width; |
56
|
|
|
|
57
|
1 |
|
this.y = line - offset; |
58
|
1 |
|
this.x = (cell.id() - (width - 1) * this.y) / width; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the related map cell |
63
|
|
|
* |
64
|
|
|
* @return The base cell instance |
65
|
|
|
*/ |
66
|
|
|
@Pure |
67
|
|
|
public C cell() { |
68
|
1 |
|
return cell; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the cell id |
73
|
|
|
* |
74
|
|
|
* @return The cell id |
75
|
|
|
*/ |
76
|
|
|
@Pure |
77
|
|
|
public @NonNegative int id() { |
78
|
1 |
|
return cell.id(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return The X coordinate of the cell |
83
|
|
|
*/ |
84
|
|
|
@Pure |
85
|
|
|
public int x() { |
86
|
1 |
|
return x; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return The Y coordinate of the cell |
91
|
|
|
*/ |
92
|
|
|
@Pure |
93
|
|
|
public int y() { |
94
|
1 |
|
return y; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if the cell is at the given coordinates |
99
|
|
|
* |
100
|
|
|
* @param x The X coordinate |
101
|
|
|
* @param y The Y coordinate |
102
|
|
|
* |
103
|
|
|
* @return true if coordinates match, or false otherwise |
104
|
|
|
*/ |
105
|
|
|
@Pure |
106
|
|
|
public boolean is(int x, int y) { |
107
|
1 |
|
return this.x == x && this.y == y; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Compute the direction to the target cell |
112
|
|
|
* |
113
|
|
|
* https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/utils/Pathfinding.as#L204 |
114
|
|
|
* |
115
|
|
|
* @param target The target cell |
116
|
|
|
* @return The direction |
117
|
|
|
*/ |
118
|
|
|
@Pure |
119
|
|
|
public Direction directionTo(CoordinateCell<C> target) { |
120
|
1 |
|
if (x == target.x) { |
121
|
1 |
|
if (target.y > y) { |
122
|
1 |
|
return Direction.SOUTH_WEST; |
123
|
|
|
} else { |
124
|
1 |
|
return Direction.NORTH_EAST; |
125
|
|
|
} |
126
|
1 |
|
} else if (target.x > x) { |
127
|
1 |
|
return Direction.SOUTH_EAST; |
128
|
|
|
} else { |
129
|
1 |
|
return Direction.NORTH_WEST; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Compute the direction to the target cell |
135
|
|
|
* |
136
|
|
|
* https://github.com/Emudofus/Dofus/blob/1.29/ank/battlefield/utils/Pathfinding.as#L204 |
137
|
|
|
* |
138
|
|
|
* @param target The target cell |
139
|
|
|
* @return The direction |
140
|
|
|
*/ |
141
|
|
|
public Direction directionTo(C target) { |
142
|
1 |
|
return directionTo(target.coordinate()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the cell distance |
147
|
|
|
* Note: Do not compute a pythagorean distance, but "square" distance. |
148
|
|
|
* So, this distance represents the minimal number of cells for a path between current and target. |
149
|
|
|
* |
150
|
|
|
* @param target The target cell |
151
|
|
|
* @return The distance, in cells number |
152
|
|
|
*/ |
153
|
|
|
@Pure |
154
|
|
|
public @NonNegative int distance(CoordinateCell<C> target) { |
155
|
1 |
|
return Math.abs(x - target.x) + Math.abs(y - target.y); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the cell distance |
160
|
|
|
* Note: Do not compute a pythagorean distance, but "square" distance. |
161
|
|
|
* So, this distance represents the minimal number of cells for a path between current and target. |
162
|
|
|
* |
163
|
|
|
* @param target The target cell |
164
|
|
|
* @return The distance, in cells number |
165
|
|
|
*/ |
166
|
|
|
public @NonNegative int distance(C target) { |
167
|
1 |
|
return distance(target.coordinate()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
@Override |
171
|
|
|
public boolean equals(@Nullable Object o) { |
172
|
1 |
|
if (this == o) { |
173
|
1 |
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
if (o == null || getClass() != o.getClass()) { |
177
|
1 |
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
final CoordinateCell<?> that = (CoordinateCell<?>) o; |
181
|
|
|
|
182
|
1 |
|
return x == that.x && y == that.y; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
@Override |
186
|
|
|
public int hashCode() { |
187
|
1 |
|
return Objects.hash(x, y); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|