feat(codegen): generate the gen 3 type chart (#54)

This commit is contained in:
Fiereu authored and GitHub committed 2026-07-25 16:40:48 +02:00
1 parent 017f6bca60
commit 391e2efeb7
8 files changed
+201

No files matched your search

@@ -0,0 +1,16 @@
@import java.util.List
@import de.fiereu.openmmo.codegen.typechart.ParsedMatchup
@param List<ParsedMatchup> 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
}
}
@@ -0,0 +1,21 @@
@file:JvmName("Main")
package de.fiereu.openmmo.codegen.typechart
import java.io.File
fun main(args: Array<String>) {
require(args.size >= 4) {
"Usage: <output-dir> <templates-dir> <class-cache-dir> <decomp-dir> 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")
}
@@ -0,0 +1,7 @@
package de.fiereu.openmmo.codegen.typechart
data class ParsedMatchup(
val attacker: String,
val defender: String,
val multiplier: Int,
)
@@ -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<ParsedMatchup> {
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)
}
}
@@ -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<ParsedMatchup>) {
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"
}
}