Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE prevention for new moves #545

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 63 additions & 45 deletions src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public static double ivRating(final Pokemon p) {
*/
public static double dpsForMove(final Pokemon p, final boolean primary) {
final PokemonMove move = primary ? p.getMove1() : p.getMove2();
return dpsForMove(p.getPokemonId(), move, primary);
double dps = 0.0;
if (move != null) {
dps = dpsForMove(p.getPokemonId(), move, primary);
}
return dps;
}

/**
Expand All @@ -86,9 +90,13 @@ public static double dpsForMove(final Pokemon p, final boolean primary) {
private static double dpsForMove(final PokemonId pokemonId, final PokemonMove move, final boolean primary) {
final PokemonMoveMeta moveMeta = PokemonMoveMetaRegistry.getMeta(move);
final int moveDelay = primary ? 0 : MOVE2_CHARGE_DELAY_MS;
double dps = (double) moveMeta.getPower() / (double) (moveMeta.getTime() + moveDelay) * MILLISECONDS_FACTOR;
if (PokemonUtils.hasStab(pokemonId, moveMeta.getMove())) {
dps = dps * STAB_MULTIPLIER;
double dps = 0.0;

if (moveMeta != null) {
dps = (double) moveMeta.getPower() / (double) (moveMeta.getTime() + moveDelay) * MILLISECONDS_FACTOR;
if (PokemonUtils.hasStab(pokemonId, moveMeta.getMove())) {
dps = dps * STAB_MULTIPLIER;
}
}
return dps;
}
Expand Down Expand Up @@ -177,10 +185,14 @@ public static double gymOffense(final Pokemon p) {
*/
public static double gymOffense(final PokemonId pokemonId, final PokemonMove move1, final PokemonMove move2, final int attackIV) {
final PokemonMeta meta = PokemonMetaRegistry.getMeta(pokemonId);
return Math.max(
PokemonCalculationUtils.dpsForMove(pokemonId, move1, true) * WEAVE_LENGTH_SECONDS,
PokemonCalculationUtils.weaveDps(pokemonId, move1, move2, 0)
) * (meta.getBaseAttack() + attackIV);
double gymOffense = 0.0;
if (meta != null) {
gymOffense = Math.max(
PokemonCalculationUtils.dpsForMove(pokemonId, move1, true) * WEAVE_LENGTH_SECONDS,
PokemonCalculationUtils.weaveDps(pokemonId, move1, move2, 0)
) * (meta.getBaseAttack() + attackIV);
}
return gymOffense;
}

/**
Expand Down Expand Up @@ -211,12 +223,15 @@ public static long gymDefense(final Pokemon p) {
* @link i607ch00
*/
public static long gymDefense(final PokemonId pokemonId,
final PokemonMove move1, final PokemonMove move2,
final int attackIV, final int defenseIV, final int staminaIV) {
final PokemonMove move1, final PokemonMove move2,
final int attackIV, final int defenseIV, final int staminaIV) {
final PokemonMeta meta = PokemonMetaRegistry.getMeta(pokemonId);
final double gymDefense = PokemonCalculationUtils.weaveDps(pokemonId, move1, move2, MOVE_2_ADDITIONAL_DELAY)
* (meta.getBaseAttack() + attackIV)
* PokemonCalculationUtils.tankiness(pokemonId, defenseIV, staminaIV);
double gymDefense = 0.0;
if (meta != null) {
gymDefense = PokemonCalculationUtils.weaveDps(pokemonId, move1, move2, MOVE_2_ADDITIONAL_DELAY)
* (meta.getBaseAttack() + attackIV)
* PokemonCalculationUtils.tankiness(pokemonId, defenseIV, staminaIV);
}
return Math.round(gymDefense);
}

Expand Down Expand Up @@ -284,6 +299,7 @@ public static double weaveDps(final Pokemon p, final int additionalDelay) {
* @link i607ch00
*/
public static double weaveDps(final PokemonId pokemonId, final PokemonMove move1, final PokemonMove move2, final int additionalDelay) {
double weaveDPS = 0.0;
final PokemonMoveMeta pm1 = PokemonMoveMetaRegistry.getMeta(move1);
final PokemonMoveMeta pm2 = PokemonMoveMetaRegistry.getMeta(move2);
final double moveOneStab = PokemonUtils.hasStab(pokemonId, move1) ? STAB_MULTIPLIER : NORMAL_MULTIPLIER;
Expand All @@ -308,41 +324,43 @@ public static double weaveDps(final PokemonId pokemonId, final PokemonMove move1
//=IF(AB2=100,CEILING(AB2/U2),AB2/U2)
final double weaveEnergyUsageRatio;

if (Math.abs(pm2.getEnergy()) == MAX_MOVE_ENERGY) {
weaveEnergyUsageRatio = Math.ceil((double) Math.abs(pm2.getEnergy()) / (double) pm1.getEnergy());
} else {
weaveEnergyUsageRatio = (double) Math.abs(pm2.getEnergy()) / (double) pm1.getEnergy();
}
if (pm1 != null && pm2 != null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't a' if ( conditions ) return 0.0;' more simple and legible? Consider this comment on the others validations as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a style preference. My employer has strict rules on only one return from a method for code maintenance, so it has snuck its way into my coding.

if (Math.abs(pm2.getEnergy()) == MAX_MOVE_ENERGY) {
weaveEnergyUsageRatio = Math.ceil((double) Math.abs(pm2.getEnergy()) / (double) pm1.getEnergy());
} else {
weaveEnergyUsageRatio = (double) Math.abs(pm2.getEnergy()) / (double) pm1.getEnergy();
}

//=IF(AB2=100,CEILING(AB2/U2),AB2/U2)*T2+(AA2+$AL$1)
//=IF(AB2=100,CEILING(AB2/U2),AB2/U2)*(T2+2000)+(AA2+$AL$1)
final double weaveCycleLength = weaveEnergyUsageRatio
* (pm1.getTime() + additionalDelay)
+ pm2.getTime() + PokemonCalculationUtils.MOVE2_CHARGE_DELAY_MS;
//=IF(AB2=100,CEILING(AB2/U2),AB2/U2)*T2+(AA2+$AL$1)
//=IF(AB2=100,CEILING(AB2/U2),AB2/U2)*(T2+2000)+(AA2+$AL$1)
final double weaveCycleLength = weaveEnergyUsageRatio
* (pm1.getTime() + additionalDelay)
+ pm2.getTime() + PokemonCalculationUtils.MOVE2_CHARGE_DELAY_MS;

//=FLOOR(100000/AD2)
//*(X2*(1+Y2*0.25) * (1+($AJ$1*Z2/100)))
//+CEILING(FLOOR(100000/AD2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))
//*(R2*(1+(S2*0.25)))
//+FLOOR((100000-(FLOOR(100000/AD2)*(AA2+$AL$1)+CEILING(FLOOR(100000/AD2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))*T2))/T2)
//*(R2*(1+(S2*0.25)))
//=FLOOR(100000/AF2)*(X2*(1+Y2*0.25)*(1+($AJ$1*Z2/100)))+CEILING(FLOOR(100000/AF2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))*(R2*(1+(S2*0.25)))
// +FLOOR((100000-(FLOOR(100000/AF2)*(AA2+$AL$1)+CEILING(FLOOR(100000/AF2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))*(T2+2000)))/(T2+2000))*(R2*(1+(S2*0.25)))
final double floorThingyCalculation = (
WEAVE_NUMBER - (
Math.floor(WEAVE_NUMBER / weaveCycleLength) * (pm2.getTime()
+ PokemonCalculationUtils.MOVE2_CHARGE_DELAY_MS)
+ Math.ceil(Math.floor(WEAVE_NUMBER / weaveCycleLength) * weaveEnergyUsageRatio) * (pm1.getTime() + additionalDelay)
)
) / (pm1.getTime() + additionalDelay);
//=FLOOR(100000/AD2)
//*(X2*(1+Y2*0.25) * (1+($AJ$1*Z2/100)))
//+CEILING(FLOOR(100000/AD2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))
//*(R2*(1+(S2*0.25)))
//+FLOOR((100000-(FLOOR(100000/AD2)*(AA2+$AL$1)+CEILING(FLOOR(100000/AD2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))*T2))/T2)
//*(R2*(1+(S2*0.25)))
//=FLOOR(100000/AF2)*(X2*(1+Y2*0.25)*(1+($AJ$1*Z2/100)))+CEILING(FLOOR(100000/AF2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))*(R2*(1+(S2*0.25)))
// +FLOOR((100000-(FLOOR(100000/AF2)*(AA2+$AL$1)+CEILING(FLOOR(100000/AF2)*IF(AB2=100,CEILING(AB2/U2),AB2/U2))*(T2+2000)))/(T2+2000))*(R2*(1+(S2*0.25)))
final double floorThingyCalculation = (
WEAVE_NUMBER - (
Math.floor(WEAVE_NUMBER / weaveCycleLength) * (pm2.getTime()
+ PokemonCalculationUtils.MOVE2_CHARGE_DELAY_MS)
+ Math.ceil(Math.floor(WEAVE_NUMBER / weaveCycleLength) * weaveEnergyUsageRatio) * (pm1.getTime() + additionalDelay)
)
) / (pm1.getTime() + additionalDelay);

//noinspection UnnecessaryLocalVariable
final double weaveDPS = Math.floor(WEAVE_NUMBER / weaveCycleLength)
* (pm2.getPower() * moveTwoStab * (1 + (PokemonCalculationUtils.CRIT_DAMAGE_BONUS * pm2.getCritChance())))
+ Math.ceil(Math.floor(WEAVE_NUMBER / weaveCycleLength) * weaveEnergyUsageRatio)
* (pm1.getPower() * moveOneStab)
+ Math.floor(floorThingyCalculation)
* (pm1.getPower() * moveOneStab);
//noinspection UnnecessaryLocalVariable
weaveDPS = Math.floor(WEAVE_NUMBER / weaveCycleLength)
* (pm2.getPower() * moveTwoStab * (1 + (PokemonCalculationUtils.CRIT_DAMAGE_BONUS * pm2.getCritChance())))
+ Math.ceil(Math.floor(WEAVE_NUMBER / weaveCycleLength) * weaveEnergyUsageRatio)
* (pm1.getPower() * moveOneStab)
+ Math.floor(floorThingyCalculation)
* (pm1.getPower() * moveOneStab);
}

return weaveDPS;
}
Expand Down
11 changes: 8 additions & 3 deletions src/me/corriekay/pokegoutil/utils/pokemon/PokemonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,14 @@ public static boolean hasStab(final Pokemon p, final boolean primary) {
* @return Weather or not the Pokémon has STAB.
*/
public static boolean hasStab(final PokemonId pokemonId, final PokemonMove move) {
boolean hasStab = false;
final PokemonMeta meta = PokemonMetaRegistry.getMeta(pokemonId);
final PokemonMoveMeta moveMeta = PokemonMoveMetaRegistry.getMeta(move);
return meta.getType1().equals(moveMeta.getType()) || meta.getType2().equals(moveMeta.getType());
if (meta != null) {
final PokemonMoveMeta moveMeta = PokemonMoveMetaRegistry.getMeta(move);
if (moveMeta != null) {
hasStab = meta.getType1().equals(moveMeta.getType()) || meta.getType2().equals(moveMeta.getType());
}
}
return hasStab;
}
}