From 426bac48c31e1ac12ccd39a80fe0b6b9bc2bf7c0 Mon Sep 17 00:00:00 2001 From: knickels Date: Fri, 17 Feb 2017 12:13:15 -0500 Subject: [PATCH 1/2] Added null checks and default return values to prevent crashes on program load when encountering unrecognized moves. --- .../pokemon/PokemonCalculationUtils.java | 108 ++++++++++-------- .../utils/pokemon/PokemonUtils.java | 11 +- 2 files changed, 71 insertions(+), 48 deletions(-) diff --git a/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java b/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java index 751fab73..ac740d2c 100644 --- a/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java +++ b/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java @@ -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; } /** @@ -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; } @@ -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; } /** @@ -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); } @@ -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; @@ -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) { + 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; } diff --git a/src/me/corriekay/pokegoutil/utils/pokemon/PokemonUtils.java b/src/me/corriekay/pokegoutil/utils/pokemon/PokemonUtils.java index fb879051..2f59e753 100644 --- a/src/me/corriekay/pokegoutil/utils/pokemon/PokemonUtils.java +++ b/src/me/corriekay/pokegoutil/utils/pokemon/PokemonUtils.java @@ -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; } } - From 0225be96e3a14815021382f537d512432093696a Mon Sep 17 00:00:00 2001 From: knickels Date: Fri, 17 Feb 2017 15:09:35 -0500 Subject: [PATCH 2/2] Added null checks and default return values to prevent crashes on program load when encountering unrecognized moves. --- .../pokegoutil/utils/pokemon/PokemonCalculationUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java b/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java index ac740d2c..531676c9 100644 --- a/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java +++ b/src/me/corriekay/pokegoutil/utils/pokemon/PokemonCalculationUtils.java @@ -360,7 +360,7 @@ public static double weaveDps(final PokemonId pokemonId, final PokemonMove move1 * (pm1.getPower() * moveOneStab) + Math.floor(floorThingyCalculation) * (pm1.getPower() * moveOneStab); - } + } return weaveDPS; }