From 1e194407430a63becef954f1d39693797568bf2d Mon Sep 17 00:00:00 2001 From: Fiereu Date: Sat, 25 Jul 2026 16:44:11 +0200 Subject: [PATCH] feat(server.game): add gen 3 stat and exp calculators (#56) --- .../openmmo/server/game/battle/ExpCurves.kt | 47 +++++++++++++++ .../server/game/battle/MoveCategory.kt | 22 +++++++ .../server/game/battle/StatCalculator.kt | 57 +++++++++++++++++++ .../server/game/battle/ExpCurvesTest.kt | 42 ++++++++++++++ .../server/game/battle/StatCalculatorTest.kt | 30 ++++++++++ 5 files changed, 198 insertions(+) create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurves.kt create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveCategory.kt create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculator.kt create mode 100644 server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurvesTest.kt create mode 100644 server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculatorTest.kt diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurves.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurves.kt new file mode 100644 index 0000000..89638ab --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurves.kt @@ -0,0 +1,47 @@ +package de.fiereu.openmmo.server.game.battle + +import de.fiereu.openmmo.common.enums.GrowthRate + +/** + * The six Gen 3 growth curves as formulas. The decomp experience table is built from these same + * macro invocations, so evaluating them directly matches it entry for entry. + */ +object ExpCurves { + + const val MAX_LEVEL = 100 + + /** Total experience needed to reach a level. */ + fun totalXpFor(rate: GrowthRate, level: Int): Int { + require(level in 0..MAX_LEVEL) { "Level $level out of range" } + // The decomp table hardcodes the first two entries for every curve. + if (level <= 1) return level + val n = level + val cube = n * n * n + return when (rate) { + GrowthRate.MEDIUM_FAST -> cube + GrowthRate.SLOW -> 5 * cube / 4 + GrowthRate.FAST -> 4 * cube / 5 + GrowthRate.MEDIUM_SLOW -> 6 * cube / 5 - 15 * n * n + 100 * n - 140 + GrowthRate.ERRATIC -> + when { + n <= 50 -> (100 - n) * cube / 50 + n <= 68 -> (150 - n) * cube / 100 + n <= 98 -> ((1911 - 10 * n) / 3) * cube / 500 + else -> (160 - n) * cube / 100 + } + GrowthRate.FLUCTUATING -> + when { + n <= 15 -> ((n + 1) / 3 + 24) * cube / 50 + n <= 36 -> (n + 14) * cube / 50 + else -> (n / 2 + 32) * cube / 50 + } + } + } + + /** The highest level whose total requirement fits the given xp. */ + fun levelFor(rate: GrowthRate, xp: Int): Int { + var level = 1 + while (level < MAX_LEVEL && totalXpFor(rate, level + 1) <= xp) level++ + return level + } +} diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveCategory.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveCategory.kt new file mode 100644 index 0000000..b3daa71 --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveCategory.kt @@ -0,0 +1,22 @@ +package de.fiereu.openmmo.server.game.battle + +import de.fiereu.openmmo.common.enums.PokemonType + +/** Gen 3 splits damage into physical and special by move type, not per move. */ +object MoveCategory { + + private val PHYSICAL = + setOf( + PokemonType.NORMAL, + PokemonType.FIGHTING, + PokemonType.FLYING, + PokemonType.GROUND, + PokemonType.ROCK, + PokemonType.BUG, + PokemonType.GHOST, + PokemonType.POISON, + PokemonType.STEEL, + ) + + fun isPhysical(type: PokemonType): Boolean = type in PHYSICAL +} diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculator.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculator.kt new file mode 100644 index 0000000..66903e5 --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculator.kt @@ -0,0 +1,57 @@ +package de.fiereu.openmmo.server.game.battle + +import de.fiereu.openmmo.common.Pokemon +import de.fiereu.openmmo.common.enums.PokemonNature +import de.fiereu.openmmo.common.enums.PokemonStat +import de.fiereu.openmmo.pokemon.SpeciesDef + +data class ComputedStats( + val hp: Int, + val atk: Int, + val def: Int, + val spAtk: Int, + val spDef: Int, + val spd: Int, +) + +/** Gen 3 stat formulas over base stats, IVs, EVs, level, and nature. */ +object StatCalculator { + + fun maxHp(base: Int, iv: Int, ev: Int, level: Int): Int = + (2 * base + iv + ev / 4) * level / 100 + level + 10 + + fun stat( + base: Int, + iv: Int, + ev: Int, + level: Int, + nature: PokemonNature, + stat: PokemonStat, + ): Int { + val raw = (2 * base + iv + ev / 4) * level / 100 + 5 + return when { + nature.increasedStat == nature.decreasedStat -> raw + nature.increasedStat == stat -> raw * 110 / 100 + nature.decreasedStat == stat -> raw * 90 / 100 + else -> raw + } + } + + fun computeAll(species: SpeciesDef, pokemon: Pokemon): ComputedStats { + val level = pokemon.level.toInt() + val nature = pokemon.nature + val ivs = pokemon.iVs + val evs = pokemon.eVs + return ComputedStats( + hp = maxHp(species.baseHp, ivs.hp, evs.hp, level), + atk = stat(species.baseAttack, ivs.atk, evs.atk, level, nature, PokemonStat.ATTACK), + def = stat(species.baseDefense, ivs.def, evs.def, level, nature, PokemonStat.DEFENSE), + spAtk = + stat(species.baseSpAttack, ivs.spAtk, evs.spAtk, level, nature, PokemonStat.SP_ATTACK), + spDef = + stat( + species.baseSpDefense, ivs.spDef, evs.spDef, level, nature, PokemonStat.SP_DEFENSE), + spd = stat(species.baseSpeed, ivs.spd, evs.spd, level, nature, PokemonStat.SPEED), + ) + } +} diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurvesTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurvesTest.kt new file mode 100644 index 0000000..abf6702 --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/ExpCurvesTest.kt @@ -0,0 +1,42 @@ +package de.fiereu.openmmo.server.game.battle + +import de.fiereu.openmmo.common.enums.GrowthRate +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +class ExpCurvesTest : + FunSpec({ + test("the curve totals match the known level 100 values") { + ExpCurves.totalXpFor(GrowthRate.MEDIUM_FAST, 100) shouldBe 1_000_000 + ExpCurves.totalXpFor(GrowthRate.MEDIUM_SLOW, 100) shouldBe 1_059_860 + ExpCurves.totalXpFor(GrowthRate.FAST, 100) shouldBe 800_000 + ExpCurves.totalXpFor(GrowthRate.SLOW, 100) shouldBe 1_250_000 + ExpCurves.totalXpFor(GrowthRate.ERRATIC, 100) shouldBe 600_000 + ExpCurves.totalXpFor(GrowthRate.FLUCTUATING, 100) shouldBe 1_640_000 + } + + test("piecewise branches match known table entries") { + ExpCurves.totalXpFor(GrowthRate.MEDIUM_SLOW, 5) shouldBe 135 + ExpCurves.totalXpFor(GrowthRate.ERRATIC, 50) shouldBe 125_000 + ExpCurves.totalXpFor(GrowthRate.FLUCTUATING, 15) shouldBe 1_957 + } + + test("the first two levels are the hardcoded table entries") { + GrowthRate.entries.forEach { rate -> + ExpCurves.totalXpFor(rate, 0) shouldBe 0 + ExpCurves.totalXpFor(rate, 1) shouldBe 1 + } + } + + test("levelFor inverts totalXpFor") { + GrowthRate.entries.forEach { rate -> + for (level in 1..100) { + ExpCurves.levelFor(rate, ExpCurves.totalXpFor(rate, level)) shouldBe level + } + } + } + + test("levelFor caps at 100") { + ExpCurves.levelFor(GrowthRate.MEDIUM_FAST, Int.MAX_VALUE) shouldBe 100 + } + }) diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculatorTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculatorTest.kt new file mode 100644 index 0000000..cf2e285 --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/StatCalculatorTest.kt @@ -0,0 +1,30 @@ +package de.fiereu.openmmo.server.game.battle + +import de.fiereu.openmmo.common.enums.PokemonNature +import de.fiereu.openmmo.common.enums.PokemonStat +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +class StatCalculatorTest : + FunSpec({ + test("a level 5 Bulbasaur with empty IVs and EVs matches the known stat line") { + StatCalculator.maxHp(45, 0, 0, 5) shouldBe 19 + StatCalculator.stat(49, 0, 0, 5, PokemonNature.HARDY, PokemonStat.ATTACK) shouldBe 9 + StatCalculator.stat(49, 0, 0, 5, PokemonNature.HARDY, PokemonStat.DEFENSE) shouldBe 9 + StatCalculator.stat(65, 0, 0, 5, PokemonNature.HARDY, PokemonStat.SP_ATTACK) shouldBe 11 + StatCalculator.stat(65, 0, 0, 5, PokemonNature.HARDY, PokemonStat.SP_DEFENSE) shouldBe 11 + StatCalculator.stat(45, 0, 0, 5, PokemonNature.HARDY, PokemonStat.SPEED) shouldBe 9 + } + + test("max HP at level 100 with perfect IVs and full EVs") { + StatCalculator.maxHp(45, 31, 252, 100) shouldBe 294 + } + + test("natures raise and lower by ten percent") { + val neutral = StatCalculator.stat(49, 0, 0, 50, PokemonNature.HARDY, PokemonStat.ATTACK) + neutral shouldBe 54 + StatCalculator.stat(49, 0, 0, 50, PokemonNature.ADAMANT, PokemonStat.ATTACK) shouldBe 59 + StatCalculator.stat(49, 0, 0, 50, PokemonNature.MODEST, PokemonStat.ATTACK) shouldBe 48 + StatCalculator.stat(49, 0, 0, 50, PokemonNature.MODEST, PokemonStat.DEFENSE) shouldBe 54 + } + })