This repository was archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathPokeInfoCalculator.java
More file actions
483 lines (430 loc) · 19.5 KB
/
PokeInfoCalculator.java
File metadata and controls
483 lines (430 loc) · 19.5 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package com.kamron.pogoiv.scanlogic;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.kamron.pogoiv.GoIVSettings;
import com.kamron.pogoiv.R;
import com.kamron.pogoiv.utils.LevelRange;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
/**
* Created by Johan Swanberg on 2016-08-18.
* A class which interprets pokemon information
*/
public class PokeInfoCalculator {
private static PokeInfoCalculator instance;
private ArrayList<Pokemon> pokedex = new ArrayList<>();
private String[] typeNamesArray;
/**
* Pokemons that aren't evolutions of any other one.
*/
private ArrayList<Pokemon> basePokemons = new ArrayList<>();
/**
* Pokemons who's name appears as a type of candy.
* For most, this is the basePokemon (ie: Pidgey candies)
* For some, this is an original Gen1 Pokemon (ie: Magmar candies, instead of Magby candies)
*/
private ArrayList<Pokemon> candyPokemons = new ArrayList<>();
private HashMap<String, Pokemon> pokemap = new HashMap<>();
@NonNull
public static synchronized PokeInfoCalculator getInstance(@NonNull GoIVSettings settings, @NonNull Resources res) {
if (instance == null) {
instance = new PokeInfoCalculator(settings, res);
}
return instance;
}
/**
* Get the instance of pokeInfoCalculator. Must have been initiated first!
*
* @return the already activated instance of PokeInfoCalculator.
*/
@Nullable
public static PokeInfoCalculator getInstance() {
return instance;
}
/**
* Creates a pokemon info calculator with the pokemon as argument.
*
* @param settings Settings instance
* @param res System resources
*/
private PokeInfoCalculator(@NonNull GoIVSettings settings, @NonNull Resources res) {
populatePokemon(settings, res);
}
public List<Pokemon> getPokedex() {
return Collections.unmodifiableList(pokedex);
}
public List<Pokemon> getBasePokemons() {
return Collections.unmodifiableList(basePokemons);
}
/**
* Returns the full list of possible candy names.
*
* @return List of all candy names that exist in Pokemon Go
*/
public List<Pokemon> getCandyPokemons() {
return Collections.unmodifiableList(candyPokemons);
}
/**
* Returns a pokemon which corresponds to the number sent in.
*
* @param number the number which this application internally uses to identify pokemon
* @return The pokemon if valid number, null if no pokemon found.
*/
public Pokemon get(int number) {
if (number >= 0 && number < pokedex.size()) {
return pokedex.get(number);
}
return null;
}
public Pokemon get(String name) {
return pokemap.get(name.toLowerCase());
}
private static String[] getPokemonNamesArray(Resources res) {
if (res.getBoolean(R.bool.use_default_pokemonsname_as_ocrstring)) {
// If flag ON, force to use English strings as pokemon name for OCR.
Configuration conf = res.getConfiguration();
Locale originalLocale; // Save original locale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
originalLocale = conf.getLocales().get(0);
} else {
originalLocale = conf.locale;
}
conf.setLocale(Locale.ENGLISH);
res.updateConfiguration(conf, null);
String[] rtn = res.getStringArray(R.array.pokemon);
conf.setLocale(originalLocale); // Restore to original locale
res.updateConfiguration(conf, null);
return rtn;
}
return res.getStringArray(R.array.pokemon);
}
private static String[] getPokemonDisplayNamesArray(GoIVSettings settings, Resources res) {
if (settings.isShowTranslatedPokemonName()) {
// If pref ON, use translated strings as pokemon name.
return res.getStringArray(R.array.pokemon);
}
// Otherwise, use default locale's pokemon name.
return getPokemonNamesArray(res);
}
/**
* Fills the list "pokemon" with the information of all pokemon by reading the
* arrays in integers.xml and the names from the strings.xml resources.
*/
private void populatePokemon(@NonNull GoIVSettings settings, @NonNull Resources res) {
final String[] names = getPokemonNamesArray(res);
final String[] displayNames = getPokemonDisplayNamesArray(settings, res);
final int[] attack = res.getIntArray(R.array.attack);
final int[] defense = res.getIntArray(R.array.defense);
final int[] stamina = res.getIntArray(R.array.stamina);
final int[] devolution = res.getIntArray(R.array.devolutionNumber);
final int[] evolutionCandyCost = res.getIntArray(R.array.evolutionCandyCost);
final int[] candyNamesArray = res.getIntArray(R.array.candyNames);
final String[] types = res.getStringArray(R.array.type);
int pokeListSize = names.length;
typeNamesArray = res.getStringArray(R.array.typeName);
for (int i = 0; i < pokeListSize; i++) {
Pokemon p = new Pokemon(names[i], displayNames[i], i, attack[i], defense[i], stamina[i], devolution[i],
evolutionCandyCost[i], getTypeNames(types[i]));
pokedex.add(p);
pokemap.put(names[i].toLowerCase(), p);
if (!names[i].equals(displayNames[i])) {
pokemap.put(displayNames[i], p);
}
}
for (int i = 0; i < pokeListSize; i++) {
if (devolution[i] != -1) {
Pokemon devo = pokedex.get(devolution[i]);
devo.evolutions.add(pokedex.get(i));
} else {
candyPokemons.add(pokedex.get(candyNamesArray[i]));
basePokemons.add(pokedex.get(i));
}
}
}
/**
* Gets the needed required candy and stardust to hit max level (relative to trainer level)
*
* @param goalLevel The level to reach
* @param estimatedPokemonLevel The estimated level of hte pokemon
* @return The text that shows the amount of candy and stardust needed.
*/
public UpgradeCost getUpgradeCost(double goalLevel, double estimatedPokemonLevel) {
int neededCandy = 0;
int neededStarDust = 0;
while (estimatedPokemonLevel != goalLevel) {
int rank = 5;
if ((estimatedPokemonLevel % 10) >= 1 && (estimatedPokemonLevel % 10) <= 2.5) {
rank = 1;
} else if ((estimatedPokemonLevel % 10) > 2.5 && (estimatedPokemonLevel % 10) <= 4.5) {
rank = 2;
} else if ((estimatedPokemonLevel % 10) > 4.5 && (estimatedPokemonLevel % 10) <= 6.5) {
rank = 3;
} else if ((estimatedPokemonLevel % 10) > 6.5 && (estimatedPokemonLevel % 10) <= 8.5) {
rank = 4;
}
if (estimatedPokemonLevel <= 10.5) {
neededCandy++;
neededStarDust += rank * 200;
} else if (estimatedPokemonLevel > 10.5 && estimatedPokemonLevel <= 20.5) {
neededCandy += 2;
neededStarDust += 1000 + (rank * 300);
} else if (estimatedPokemonLevel > 20.5 && estimatedPokemonLevel <= 25.5) {
neededCandy += 3;
neededStarDust += 2500 + (rank * 500);
} else if (estimatedPokemonLevel > 25.5 && estimatedPokemonLevel <= 30.5) {
neededCandy += 4;
neededStarDust += 2500 + (rank * 500);
} else if (estimatedPokemonLevel > 30.5 && estimatedPokemonLevel <= 32.5) {
neededCandy += 6;
neededStarDust += 5000 + (rank * 1000);
} else if (estimatedPokemonLevel > 32.5 && estimatedPokemonLevel <= 34.5) {
neededCandy += 8;
neededStarDust += 5000 + (rank * 1000);
} else if (estimatedPokemonLevel > 34.5 && estimatedPokemonLevel <= 36.5) {
neededCandy += 10;
neededStarDust += 5000 + (rank * 1000);
} else if (estimatedPokemonLevel > 36.5 && estimatedPokemonLevel <= 38.5) {
neededCandy += 12;
neededStarDust += 5000 + (rank * 1000);
} else if (estimatedPokemonLevel > 38.5) {
neededCandy += 15;
neededStarDust += 5000 + (rank * 1000);
}
estimatedPokemonLevel += 0.5;
}
return new UpgradeCost(neededStarDust, neededCandy);
}
/**
* Calculates all the IV information that can be gained from the pokemon level, hp and cp
* and fills the information in an IVScanResult, which is returned.
*
* @param estimatedPokemonLevel The estimated pokemon level range
* @param pokemonHP The pokemon HP
* @param pokemonCP The pokemon CP
* @return An IVScanResult which contains the information calculated about the pokemon, or null if there are too
* many possibilities or if there are none.
*/
public IVScanResult getIVPossibilities(Pokemon selectedPokemon, LevelRange estimatedPokemonLevel,
int pokemonHP, int pokemonCP, Pokemon.Gender pokemonGender) {
if (estimatedPokemonLevel.min == estimatedPokemonLevel.max) {
return getSingleLevelIVPossibility(selectedPokemon, estimatedPokemonLevel.min, pokemonHP, pokemonCP,
pokemonGender);
}
List<IVScanResult> possibilities = new ArrayList<>();
for (double i = estimatedPokemonLevel.min; i <= estimatedPokemonLevel.max; i += 0.5) {
possibilities.add(getSingleLevelIVPossibility(selectedPokemon, i, pokemonHP, pokemonCP, pokemonGender));
}
IVScanResult result = new IVScanResult(selectedPokemon, estimatedPokemonLevel, pokemonCP, pokemonGender);
for (IVScanResult ivs : possibilities) {
result.addPossibilitiesFrom(ivs);
}
return result;
}
/**
* Calculates all the IV information that can be gained from the pokemon level, hp and cp
* and fills the information in an IVScanResult, which is returned.
*
* @param estimatedPokemonLevel The estimated pokemon level
* @param pokemonHP THe pokemon hp
* @param pokemonCP The pokemonCP
* @return An IVScanResult which contains the information calculated about the pokemon, or null if there are too
* many possibilities.
*/
private IVScanResult getSingleLevelIVPossibility(Pokemon selectedPokemon, double estimatedPokemonLevel,
int pokemonHP, int pokemonCP, Pokemon.Gender pokemonGender) {
int baseAttack = selectedPokemon.baseAttack;
int baseDefense = selectedPokemon.baseDefense;
int baseStamina = selectedPokemon.baseStamina;
double lvlScalar = Data.getLevelCpM(estimatedPokemonLevel);
double lvlScalarPow2 = Math.pow(lvlScalar, 2) * 0.1; // instead of computing again in every loop
//IV vars for lower and upper end cp ranges
IVScanResult returner = ScanContainer.createIVScanResult(selectedPokemon, new LevelRange(estimatedPokemonLevel),
pokemonCP, pokemonGender);
for (int staminaIV = 0; staminaIV < 16; staminaIV++) {
int hp = (int) Math.max(Math.floor((baseStamina + staminaIV) * lvlScalar), 10);
if (hp == pokemonHP) {
double lvlScalarStamina = Math.sqrt(baseStamina + staminaIV) * lvlScalarPow2;
for (int defenseIV = 0; defenseIV < 16; defenseIV++) {
for (int attackIV = 0; attackIV < 16; attackIV++) {
int cp = Math.max(10, (int) Math.floor((baseAttack + attackIV) * Math.sqrt(baseDefense
+ defenseIV) * lvlScalarStamina));
if (cp == pokemonCP) {
returner.addIVCombination(attackIV, defenseIV, staminaIV);
}
}
}
} else if (hp > pokemonHP) {
break;
}
}
returner.scannedHP = pokemonHP;
return returner;
}
/**
* getCpAtRangeLeve
* Used to calculate CP ranges for a species at a specific level based on the lowest and highest
* IV combination.
* <p/>
* Returns a string on the form of "\n CP at lvl X: A - B" where x is the pokemon level, A is minCP and B is maxCP
*
* @param pokemon the index of the pokemon species within the pokemon list (sorted)
* @param low combination of lowest IVs
* @param high combination of highest IVs
* @param level pokemon level for CP calculation
* @return CPrange containing the CP range including the specified level.
*/
public CPRange getCpRangeAtLevel(Pokemon pokemon, IVCombination low, IVCombination high, double level) {
if (low == null || high == null || level < 0 || pokemon == null) {
return new CPRange(0, 0);
}
int baseAttack = pokemon.baseAttack;
int baseDefense = pokemon.baseDefense;
int baseStamina = pokemon.baseStamina;
double lvlScalar = Data.getLevelCpM(level);
int cpMin = (int) Math.floor(
(baseAttack + low.att) * Math.sqrt(baseDefense + low.def) * Math.sqrt(baseStamina + low.sta)
* Math.pow(lvlScalar, 2) * 0.1);
int cpMax = (int) Math.floor((baseAttack + high.att) * Math.sqrt(baseDefense + high.def)
* Math.sqrt(baseStamina + high.sta) * Math.pow(lvlScalar, 2) * 0.1);
if (cpMin > cpMax) {
int tmp = cpMax;
cpMax = cpMin;
cpMin = tmp;
}
return new CPRange(cpMin, cpMax);
}
/**
* Get the combined cost for evolving all steps between two pokemon, for example the cost from caterpie ->
* metapod is 12,
* caterpie -> butterfly is 12+50 = 62.
*
* @param start which pokemon to start from
* @param end the end evolution
* @return the combined candy cost for all required evolutions
*/
public int getCandyCostForEvolution(Pokemon start, Pokemon end) {
Pokemon devolution = get(end.devoNumber);
Pokemon dedevolution = null;
if (devolution != null) { //devolution must exist for there to be a devolution of the devolution
dedevolution = get(devolution.devoNumber);
}
boolean isEndReallyAfterStart = (devolution == start)
|| dedevolution == start; //end must be devolution or devolution of devolution of start
int cost = 0;
if (isInSameEvolutionChain(start, end) && isEndReallyAfterStart) {
while (start != end) { //move backwards from end until you've reached start
Pokemon beforeEnd = get(end.devoNumber);
cost += beforeEnd.candyEvolutionCost;
end = beforeEnd;
}
}
return cost;
}
/**
* Check if two pokemon are in the same complete evolution chain. Jolteon and vaporeon would return true
*
* @param p1 first pokemon
* @param p2 second pokemon
* @return true if both pokemon are in the same pokemon evolution tree
*/
private boolean isInSameEvolutionChain(Pokemon p1, Pokemon p2) {
ArrayList<Pokemon> evolutionLine = getEvolutionLine(p1);
for (Pokemon poke : evolutionLine) {
if (poke.number == p2.number) {
return true;
}
}
return false;
}
/**
* Get the lowest evolution in the chain of a pokemon.
*
* @param poke a pokemon, example charizard
* @return a pokemon, in the example would return charmander
*/
private Pokemon getLowestEvolution(Pokemon poke) {
if (poke.devoNumber < 0) {
return poke; //already lowest evolution
}
Pokemon devoPoke = get(poke.devoNumber);
while (devoPoke.devoNumber >= 0) { //while devol
devoPoke = get(devoPoke.devoNumber);
}
return devoPoke;
}
/**
* Returns the evolution line of a pokemon.
*
* @param poke the pokemon to check the evolution line of
* @return a list with pokemon, input pokemon plus its evolutions
*/
public ArrayList<Pokemon> getEvolutionLine(Pokemon poke) {
poke = getLowestEvolution(poke);
ArrayList<Pokemon> list = new ArrayList<>();
list.add(poke); //add self
list.addAll(poke.evolutions); //add all immediate evolutions
for (Pokemon evolution : poke.evolutions) {
list.addAll(evolution.evolutions);
}
return list;
}
/**
* Get how much hp a pokemon will have at a certain level, including the stamina IV taken from the scan results.
* If the prediction is not exact because of big possible variation in stamina IV, the average will be returnred.
*
* @param ivScanResult Scan results which includes stamina ivs
* @param selectedLevel which level to get the hp for
* @param selectedPokemon Which pokemon to get Hp for
* @return An integer representing how much hp selectedpokemon with ivscanresult stamina ivs has at selectedlevel
*/
public int getHPAtLevel(IVScanResult ivScanResult, double selectedLevel, Pokemon selectedPokemon) {
double lvlScalar = Data.getLevelCpM(selectedLevel);
int highHp = (int) Math.max(Math.floor((selectedPokemon.baseStamina + ivScanResult.highStamina) * lvlScalar),
10);
int lowHp = (int) Math.max(Math.floor((selectedPokemon.baseStamina + ivScanResult.highStamina) * lvlScalar),
10);
int averageHP = Math.round(highHp + lowHp) / 2;
return averageHP;
}
/**
* Get localized pokemon type names as a string array from a base20 value defined in types.xml.
*
* @param typeBase20 a pokemon type value indicated with 2 digits base20 value defined in types.xml
* @return A string array including localized pokemon type names. This array has 2 elements as max-length for a
* multi-type pokemon, or 1 element as min-length for a single type pokemon.
* If invalid value input, currently "N/A" returned as 1 element string array.
*/
private String[] getTypeNames(String typeBase20) {
// check invalid value
if (typeBase20.length() != 2) {
//TODO error handling
String[] typeNames = {"N/A"};
return typeNames;
}
Integer[] typeNum = new Integer[2];
typeNum[0] = Integer.parseInt(typeBase20.substring(0, 1), 20); // 1st type
typeNum[1] = Integer.parseInt(typeBase20.substring(1, 2), 20); // 2nd type
// check invalid value
if (!(0 < typeNum[0] && typeNum[0] < typeNamesArray.length)
|| !(0 <= typeNum[1] && typeNum[1] < typeNamesArray.length)) {
//TODO error handling
String[] typeNames = {"N/A"};
return typeNames;
}
if (typeNum[1] == 0) {
// Single-type Pokemon
String[] typeNames = {typeNamesArray[typeNum[0] - 1]};
return typeNames;
} else {
// Multi-type Pokemon
String[] typeNames = {typeNamesArray[typeNum[0] - 1], typeNamesArray[typeNum[1] - 1]};
return typeNames;
}
}
}