Repository navigation
Expand file tree
/
Copy pathMap.java
More file actions
357 lines (317 loc) · 13.3 KB
/
Copy pathMap.java
File metadata and controls
357 lines (317 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.pokegoapi.api.map;
import POGOProtos.Map.Fort.FortDataOuterClass;
import POGOProtos.Map.Fort.FortTypeOuterClass;
import POGOProtos.Map.MapCellOuterClass;
import POGOProtos.Map.Pokemon.MapPokemonOuterClass;
import POGOProtos.Map.Pokemon.NearbyPokemonOuterClass;
import POGOProtos.Map.Pokemon.WildPokemonOuterClass;
import POGOProtos.Networking.Requests.Messages.*;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Responses.*;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.map.Pokemon.CatchablePokemon;
import com.pokegoapi.api.map.Pokemon.NearbyPokemon;
import com.pokegoapi.api.map.fort.FortDetails;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.google.common.geometry.MutableInteger;
import com.pokegoapi.google.common.geometry.S2CellId;
import com.pokegoapi.google.common.geometry.S2LatLng;
import com.pokegoapi.main.ServerRequest;
import java8.util.function.Function;
import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import static com.pokegoapi.google.common.geometry.S2CellId.MAX_LEVEL;
public class Map {
private static long NEW_MAP_OBJECTS_EXPIRY = 60000; // 60 seconds
private PokemonGo api;
private long lastMapUpdate;
private MapObjects lastMapObjects;
private long lastLong;
private long lastLat;
@Getter
@Setter
private boolean useCache;
public Map(PokemonGo api) {
this.api = api;
lastMapUpdate = 0;
useCache = true;
}
/**
* Gets a new map objects if there has been a lat/long change or the last request was done greater then NEW_MAP_OBJECTS_EXPIRY
*
* @return List<CatchablePokemon> at your current location
*/
private MapObjects getRetainedMapObject() throws LoginFailedException, RemoteServerException {
// get new MapObjects or used existing one
if (!useCache) return lastMapObjects;
if (api.getLatitude() != lastLat && api.getLongitude() != lastLong || (System.currentTimeMillis() - lastMapUpdate) > NEW_MAP_OBJECTS_EXPIRY) {
getMapObjects(); // should update the lastMapObjects variable
}
return lastMapObjects;
}
/**
* Returns a list of catchable pokemon around the current location
*
* @return List<CatchablePokemon> at your current location
*/
public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException, RemoteServerException {
List<CatchablePokemon> catchablePokemons = new ArrayList<>();
MapObjects objects = getRetainedMapObject();
for (MapPokemonOuterClass.MapPokemon mapPokemon : objects.getCatchablePokemons()) {
catchablePokemons.add(new CatchablePokemon(api, mapPokemon));
}
for (WildPokemonOuterClass.WildPokemon wildPokemon : objects.getWildPokemons()) {
catchablePokemons.add(new CatchablePokemon(api, wildPokemon));
}
return catchablePokemons;
}
/**
* Returns a list of nearby pokemon (non-catchable)
*
* @return List<NearbyPokemon> at your current location
*/
public List<NearbyPokemon> getNearbyPokemon() throws LoginFailedException, RemoteServerException {
List<NearbyPokemon> pokemons = new ArrayList<>();
MapObjects objects = getRetainedMapObject();
for (NearbyPokemonOuterClass.NearbyPokemon pokemon : objects.getNearbyPokemons()) {
pokemons.add(new NearbyPokemon(pokemon));
}
return pokemons;
}
/**
* Returns MapObjects around your current location
*
* @return MapObjects at your current location
*/
public MapObjects getMapObjects() throws LoginFailedException, RemoteServerException {
return getMapObjects(9);
}
/**
* Returns MapObjects around your current location within a given width
*
* @param width
* @return MapObjects at your current location
*/
public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteServerException {
return getMapObjects(getCellIds(api.getLatitude(), api.getLongitude(), width), api.getLatitude(), api.getLongitude(), api.getAltitude());
}
/**
* Returns 9x9 cells with the requested lattitude/longitude in the center cell
*
* @param latitude
* @param longitude
* @return MapObjects in the given cells
*/
@Deprecated
public MapObjects getMapObjects(double latitude, double longitude) throws LoginFailedException, RemoteServerException {
return getMapObjects(latitude, longitude, 9);
}
/**
* Returns the cells requested, you should send a latitude/longitude to fake a near location
*
* @param cellIds List<Long> of cellId
* @param latitude
* @param longitude
* @return MapObjects in the given cells
*/
@Deprecated
public MapObjects getMapObjects(List<Long> cellIds, double latitude, double longitude) throws LoginFailedException, RemoteServerException {
return getMapObjects(cellIds, latitude, longitude, 0);
}
/**
* Returns `width` * `width` cells with the requested latitude/longitude in the center
*
* @param latitude
* @param longitude
* @param width
* @return MapObjects in the given cells
*/
@Deprecated
public MapObjects getMapObjects(double latitude, double longitude, int width) throws LoginFailedException, RemoteServerException {
return getMapObjects(getCellIds(latitude, longitude, width), latitude, longitude);
}
/**
* Returns the cells requested
*
* @param cellIds
* @param latitude
* @param longitude
* @return MapObjects in the given cells
*/
@Deprecated
public MapObjects getMapObjects(List<Long> cellIds, double latitude, double longitude, double altitude) throws LoginFailedException, RemoteServerException {
api.setLatitude(latitude);
api.setLongitude(longitude);
api.setAltitude(altitude);
return getMapObjects(cellIds);
}
/**
* Returns the cells requested
*
* @param cellIds List<Long> of cellId
* @return MapObjects in the given cells
*/
public MapObjects getMapObjects(List<Long> cellIds) throws LoginFailedException, RemoteServerException {
GetMapObjectsMessageOuterClass.GetMapObjectsMessage.Builder builder = GetMapObjectsMessageOuterClass.GetMapObjectsMessage.newBuilder()
.setLatitude(api.getLatitude())
.setLongitude(api.getLongitude());
int i = 0;
for (Long cellId : cellIds) {
builder.addCellId(cellId);
builder.addSinceTimestampMs(lastMapUpdate);
i++;
}
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.GET_MAP_OBJECTS, builder.build());
api.getRequestHandler().request(serverRequest);
api.getRequestHandler().sendServerRequests();
GetMapObjectsResponseOuterClass.GetMapObjectsResponse response = null;
try {
response = GetMapObjectsResponseOuterClass.GetMapObjectsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
MapObjects result = new MapObjects(api);
for (MapCellOuterClass.MapCell mapCell : response.getMapCellsList()) {
result.addNearbyPokemons(mapCell.getNearbyPokemonsList());
result.addCatchablePokemons(mapCell.getCatchablePokemonsList());
result.addWildPokemons(mapCell.getWildPokemonsList());
result.addDecimatedSpawnPoints(mapCell.getDecimatedSpawnPointsList());
result.addSpawnPoints(mapCell.getSpawnPointsList());
java.util.Map<FortTypeOuterClass.FortType, List<FortDataOuterClass.FortData>> groupedForts
= StreamSupport.stream(mapCell.getFortsList()).collect(Collectors.groupingBy(new Function<FortDataOuterClass.FortData, FortTypeOuterClass.FortType>() {
@Override
public FortTypeOuterClass.FortType apply(FortDataOuterClass.FortData t) {
return t.getType();
}
}));
result.addGyms(groupedForts.get(FortTypeOuterClass.FortType.GYM));
result.addPokestops(groupedForts.get(FortTypeOuterClass.FortType.CHECKPOINT));
}
lastMapObjects = result;
lastMapUpdate = System.currentTimeMillis();
return result;
}
/**
* Get a list of all the Cell Ids
*
* @param latitude
* @param longitude
* @param width
* @return List of Cells
*/
public List<Long> getCellIds(double latitude, double longitude, int width) {
S2LatLng latLng = S2LatLng.fromDegrees(latitude, longitude);
S2CellId cellId = S2CellId.fromLatLng(latLng).parent(15);
MutableInteger i = new MutableInteger(0);
MutableInteger j = new MutableInteger(0);
int level = cellId.level();
int size = 1 << (MAX_LEVEL - level);
int face = cellId.toFaceIJOrientation(i, j, null);
List<Long> cells = new ArrayList<Long>();
int halfWidth = (int) Math.floor(width / 2);
for (int x = -halfWidth; x <= halfWidth; x++) {
for (int y = -halfWidth; y <= halfWidth; y++) {
cells.add(cellId.fromFaceIJ(face, i.intValue() + x * size, j.intValue() + y * size).parent(15).id());
}
}
return cells;
}
public FortDetails getFortDetails(String id, long lon, long lat) throws LoginFailedException, RemoteServerException {
FortDetailsMessageOuterClass.FortDetailsMessage reqMsg = FortDetailsMessageOuterClass.FortDetailsMessage.newBuilder()
.setFortId(id)
.setLatitude(lat)
.setLongitude(lon)
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.FORT_DETAILS, reqMsg);
api.getRequestHandler().request(serverRequest);
api.getRequestHandler().sendServerRequests();
FortDetailsResponseOuterClass.FortDetailsResponse response = null;
try {
response = FortDetailsResponseOuterClass.FortDetailsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
return new FortDetails(response);
}
@Deprecated
public FortSearchResponseOuterClass.FortSearchResponse searchFort(FortDataOuterClass.FortData fortData) throws LoginFailedException, RemoteServerException {
FortSearchMessageOuterClass.FortSearchMessage reqMsg = FortSearchMessageOuterClass.FortSearchMessage.newBuilder()
.setFortId(fortData.getId())
.setFortLatitude(fortData.getLatitude())
.setFortLongitude(fortData.getLongitude())
.setPlayerLatitude(api.getLatitude())
.setPlayerLongitude(api.getLongitude())
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.FORT_SEARCH, reqMsg);
api.getRequestHandler().request(serverRequest);
api.getRequestHandler().sendServerRequests();
FortSearchResponseOuterClass.FortSearchResponse response = null;
try {
response = FortSearchResponseOuterClass.FortSearchResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
return response;
}
@Deprecated
public EncounterResponseOuterClass.EncounterResponse encounterPokemon(MapPokemonOuterClass.MapPokemon catchablePokemon) throws LoginFailedException, RemoteServerException {
EncounterMessageOuterClass.EncounterMessage reqMsg = EncounterMessageOuterClass.EncounterMessage.newBuilder()
.setEncounterId(catchablePokemon.getEncounterId())
.setPlayerLatitude(api.getLatitude())
.setPlayerLongitude(api.getLongitude())
.setSpawnpointId(catchablePokemon.getSpawnpointId())
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.ENCOUNTER, reqMsg);
api.getRequestHandler().request(serverRequest);
api.getRequestHandler().sendServerRequests();
EncounterResponseOuterClass.EncounterResponse response = null;
try {
response = EncounterResponseOuterClass.EncounterResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
return response;
}
@Deprecated
public CatchPokemonResponseOuterClass.CatchPokemonResponse catchPokemon(MapPokemonOuterClass.MapPokemon catchablePokemon, double normalizedHitPosition, double normalizedReticleSize, double spinModifier, int pokeball) throws LoginFailedException, RemoteServerException {
CatchPokemonMessageOuterClass.CatchPokemonMessage reqMsg = CatchPokemonMessageOuterClass.CatchPokemonMessage.newBuilder()
.setEncounterId(catchablePokemon.getEncounterId())
.setHitPokemon(true)
.setNormalizedHitPosition(normalizedHitPosition)
.setNormalizedReticleSize(normalizedReticleSize)
.setSpawnPointGuid(catchablePokemon.getSpawnpointId())
.setSpinModifier(spinModifier)
.setPokeball(pokeball)
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.CATCH_POKEMON, reqMsg);
api.getRequestHandler().request(serverRequest);
api.getRequestHandler().sendServerRequests();
CatchPokemonResponseOuterClass.CatchPokemonResponse response = null;
try {
response = CatchPokemonResponseOuterClass.CatchPokemonResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
return response;
}
}