diff --git a/codegen/build.gradle.kts b/codegen/build.gradle.kts index 7afb57b..8083643 100644 --- a/codegen/build.gradle.kts +++ b/codegen/build.gradle.kts @@ -11,6 +11,7 @@ dependencies { api(libs.dagger) "generatorImplementation"(libs.jte) "generatorImplementation"(libs.kotlinx.serialization.json) + testImplementation(libs.bundles.kotest) } // Region name -> source decomp. Only non-NDS (GBA, 2D) decomps that share the pret @@ -48,4 +49,9 @@ jteCodegen { inputDirs.from(sourceDecompDir) extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath)) } + register("typechart") { + mainClass.set("de.fiereu.openmmo.codegen.typechart.Main") + inputDirs.from(sourceDecompDir) + extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath)) + } } diff --git a/codegen/src/generator/jte/typechart/TypeChartRegistry.jte b/codegen/src/generator/jte/typechart/TypeChartRegistry.jte new file mode 100644 index 0000000..689dd95 --- /dev/null +++ b/codegen/src/generator/jte/typechart/TypeChartRegistry.jte @@ -0,0 +1,16 @@ +@import java.util.List +@import de.fiereu.openmmo.codegen.typechart.ParsedMatchup + +@param List matchups +package de.fiereu.openmmo.typechart.generated + +import de.fiereu.openmmo.common.enums.PokemonType +import de.fiereu.openmmo.typechart.TypeChart + +internal object GeneratedTypeChart { + fun loadInto(chart: TypeChart) { +@for(ParsedMatchup m : matchups) + chart.register(${m.getAttacker()}, ${m.getDefender()}, ${m.getMultiplier()}) +@endfor + } +} diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/Main.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/Main.kt new file mode 100644 index 0000000..0423897 --- /dev/null +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/Main.kt @@ -0,0 +1,21 @@ +@file:JvmName("Main") + +package de.fiereu.openmmo.codegen.typechart + +import java.io.File + +fun main(args: Array) { + require(args.size >= 4) { + "Usage: got ${args.toList()}" + } + val outputDir = File(args[0]) + val templatesDir = File(args[1]) + val classCacheDir = File(args[2]) + val decompDir = File(args[3]) + + println("[typechart] parsing from $decompDir") + val matchups = TypeChartParser(decompDir).parseAll() + println("[typechart] parsed ${matchups.size} matchups. writing to $outputDir") + TypeChartRenderer(templatesDir, outputDir, classCacheDir).render(matchups) + println("[typechart] done") +} diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/ParsedModel.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/ParsedModel.kt new file mode 100644 index 0000000..9495c41 --- /dev/null +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/ParsedModel.kt @@ -0,0 +1,7 @@ +package de.fiereu.openmmo.codegen.typechart + +data class ParsedMatchup( + val attacker: String, + val defender: String, + val multiplier: Int, +) diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/TypeChartParser.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/TypeChartParser.kt new file mode 100644 index 0000000..226075d --- /dev/null +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/TypeChartParser.kt @@ -0,0 +1,39 @@ +package de.fiereu.openmmo.codegen.typechart + +import de.fiereu.openmmo.codegen.move.typeRef +import java.io.File + +class TypeChartParser(private val rootDir: File) { + + fun parseAll(): List { + val file = File(rootDir, "src/battle_main.c") + require(file.exists()) { + "Decomp not initialized at $rootDir (missing ${file.path}). " + + "Run: git submodule update --init --recursive" + } + val table = + TABLE_REGEX.find(file.readText()) + ?: error("Missing gTypeEffectiveness table in ${file.path}") + return ROW_REGEX.findAll(table.groupValues[1]) + .filter { it.groupValues[1] !in CONTROL_ROWS && it.groupValues[2] !in CONTROL_ROWS } + .map { + ParsedMatchup( + attacker = typeRef("TYPE_${it.groupValues[1]}"), + defender = typeRef("TYPE_${it.groupValues[2]}"), + multiplier = MULTIPLIERS.getValue(it.groupValues[3]), + ) + } + .toList() + } + + companion object { + private val TABLE_REGEX = + Regex("""gTypeEffectiveness\[\d+]\s*=\s*\{(.*?)};""", RegexOption.DOT_MATCHES_ALL) + private val ROW_REGEX = Regex("""TYPE_(\w+),\s*TYPE_(\w+),\s*TYPE_MUL_(\w+)""") + + // The Foresight sentinel only separates the ghost immunities that move disables. Foresight + // is not implemented, so the rows behind it are kept as plain immunities. + private val CONTROL_ROWS = setOf("FORESIGHT", "ENDTABLE") + private val MULTIPLIERS = mapOf("NO_EFFECT" to 0, "NOT_EFFECTIVE" to 5, "SUPER_EFFECTIVE" to 20) + } +} diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/TypeChartRenderer.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/TypeChartRenderer.kt new file mode 100644 index 0000000..d7a75a0 --- /dev/null +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/typechart/TypeChartRenderer.kt @@ -0,0 +1,37 @@ +package de.fiereu.openmmo.codegen.typechart + +import gg.jte.ContentType +import gg.jte.TemplateEngine +import gg.jte.output.FileOutput +import gg.jte.resolve.DirectoryCodeResolver +import java.io.File + +class TypeChartRenderer( + private val templatesDir: File, + private val outputDir: File, + private val classCacheDir: File, +) { + + fun render(matchups: List) { + classCacheDir.mkdirs() + val engine = + TemplateEngine.create( + DirectoryCodeResolver(templatesDir.toPath()), + classCacheDir.toPath(), + ContentType.Plain, + ) + + val packageRoot = File(outputDir, BASE_PACKAGE.replace('.', '/')) + if (packageRoot.exists()) packageRoot.deleteRecursively() + + val registry = File(packageRoot, "GeneratedTypeChart.kt") + registry.parentFile.mkdirs() + FileOutput(registry.toPath()).use { out -> + engine.render("TypeChartRegistry.jte", mapOf("matchups" to matchups), out) + } + } + + companion object { + private const val BASE_PACKAGE = "de.fiereu.openmmo.typechart.generated" + } +} diff --git a/codegen/src/main/kotlin/de/fiereu/openmmo/typechart/TypeChart.kt b/codegen/src/main/kotlin/de/fiereu/openmmo/typechart/TypeChart.kt new file mode 100644 index 0000000..954a2be --- /dev/null +++ b/codegen/src/main/kotlin/de/fiereu/openmmo/typechart/TypeChart.kt @@ -0,0 +1,36 @@ +package de.fiereu.openmmo.typechart + +import de.fiereu.openmmo.common.enums.PokemonType +import de.fiereu.openmmo.typechart.generated.GeneratedTypeChart +import java.util.concurrent.ConcurrentHashMap +import javax.inject.Inject +import javax.inject.Singleton + +/** Gen 3 type matchups as fixed point multipliers per 10, so 5 halves and 20 doubles. */ +@Singleton +class TypeChart @Inject constructor() { + + private val matchups = ConcurrentHashMap, Int>() + + init { + GeneratedTypeChart.loadInto(this) + } + + fun register(attacker: PokemonType, defender: PokemonType, multiplier: Int) { + matchups[attacker to defender] = multiplier + } + + fun multiplier(attacker: PokemonType, defender: PokemonType): Int = + matchups[attacker to defender] ?: NEUTRAL + + /** Combined multiplier per 10 against both defender types. A mono type counts once. */ + fun effectiveness(attacker: PokemonType, def1: PokemonType, def2: PokemonType): Int { + val first = multiplier(attacker, def1) + if (def2 == def1) return first + return first * multiplier(attacker, def2) / NEUTRAL + } + + companion object { + const val NEUTRAL = 10 + } +} diff --git a/codegen/src/test/kotlin/de/fiereu/openmmo/typechart/TypeChartTest.kt b/codegen/src/test/kotlin/de/fiereu/openmmo/typechart/TypeChartTest.kt new file mode 100644 index 0000000..37af8af --- /dev/null +++ b/codegen/src/test/kotlin/de/fiereu/openmmo/typechart/TypeChartTest.kt @@ -0,0 +1,39 @@ +package de.fiereu.openmmo.typechart + +import de.fiereu.openmmo.common.enums.PokemonType +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +class TypeChartTest : + FunSpec({ + val chart = TypeChart() + + test("immunities multiply to zero") { + chart.multiplier(PokemonType.ELECTRIC, PokemonType.GROUND) shouldBe 0 + chart.multiplier(PokemonType.NORMAL, PokemonType.GHOST) shouldBe 0 + chart.multiplier(PokemonType.FIGHTING, PokemonType.GHOST) shouldBe 0 + } + + test("super effective doubles and resisted halves") { + chart.multiplier(PokemonType.FIRE, PokemonType.GRASS) shouldBe 20 + chart.multiplier(PokemonType.WATER, PokemonType.FIRE) shouldBe 20 + chart.multiplier(PokemonType.GHOST, PokemonType.STEEL) shouldBe 5 + } + + test("an unlisted matchup is neutral") { + chart.multiplier(PokemonType.NORMAL, PokemonType.NORMAL) shouldBe 10 + } + + test("dual typing multiplies both matchups") { + // Grass/Poison versus Fire: 20 * 10 / 10. + chart.effectiveness(PokemonType.FIRE, PokemonType.GRASS, PokemonType.POISON) shouldBe 20 + // Bug/Grass versus Fire: 20 * 20 / 10. + chart.effectiveness(PokemonType.FIRE, PokemonType.BUG, PokemonType.GRASS) shouldBe 40 + // Ground immunity wins over the Rock weakness for Electric. + chart.effectiveness(PokemonType.ELECTRIC, PokemonType.GROUND, PokemonType.FLYING) shouldBe 0 + } + + test("a mono type counts only once") { + chart.effectiveness(PokemonType.FIRE, PokemonType.GRASS, PokemonType.GRASS) shouldBe 20 + } + })