feat(game): give monsters their real level up moveset (#85)

* feat(codegen): generate the level up learnsets from the decomp

* feat(game): give monsters their real level up moveset
This commit is contained in:
Fiereu authored and GitHub committed 2026-08-05 10:38:55 +02:00
1 parent e24bc5518d
commit 1ecfabaed5
22 files changed
+485 -39

No files matched your search

@@ -0,0 +1,26 @@
@import java.util.List
@import de.fiereu.openmmo.codegen.learnset.ParsedLearnset
@import de.fiereu.openmmo.codegen.learnset.RenderUtil
@param List<List<ParsedLearnset>> chunks
package de.fiereu.openmmo.pokemon.generated
import de.fiereu.openmmo.pokemon.LearnsetRegistry
import de.fiereu.openmmo.pokemon.LevelUpMove
internal object GeneratedLearnsets {
fun loadInto(reg: LearnsetRegistry) {
@for(int i = 0; i < chunks.size(); i++)
registerChunk${i}(reg)
@endfor
}
@for(int c = 0; c < chunks.size(); c++)
private fun registerChunk${c}(reg: LearnsetRegistry) {
@for(ParsedLearnset l : chunks.get(c))
${RenderUtil.INSTANCE.learnset(l)}
@endfor
}
@endfor
}
@@ -0,0 +1,13 @@
package de.fiereu.openmmo.codegen
import java.io.File
/** Reads a `#define <prefix><name> <number>` block, keyed by the full constant name. */
fun defineTable(file: File, prefix: String): Map<String, Int> {
if (!file.exists()) return emptyMap()
val regex = Regex("""^#define\s+($prefix\w+)\s+(\d+)\s*$""")
return file
.readLines()
.mapNotNull { regex.find(it.trim()) }
.associate { it.groupValues[1] to it.groupValues[2].toInt() }
}
@@ -0,0 +1,67 @@
package de.fiereu.openmmo.codegen.learnset
import de.fiereu.openmmo.codegen.defineTable
import de.fiereu.openmmo.codegen.pokemon.NationalDex
import de.fiereu.openmmo.codegen.pokemon.SpeciesParser
import java.io.File
class LearnsetParser(private val rootDir: File) {
fun parseAll(): List<ParsedLearnset> {
val speciesIds = readDefineTable("include/constants/species.h", "SPECIES_")
val nationalDex = NationalDex.read(rootDir)
val moveIds = readDefineTable("include/constants/moves.h", "MOVE_")
val tables = readTables()
// The decomp keeps learnset pointers for placeholder species with no stats, drop those.
val known = SpeciesParser(rootDir).parseAll().mapTo(HashSet()) { it.id }
return readPointers()
.mapNotNull { (constant, symbol) ->
// Same as the species data, the client keys by national dex.
val dexId = speciesIds[constant]?.let { nationalDex[it] } ?: return@mapNotNull null
if (dexId !in known) return@mapNotNull null
val entries = tables[symbol] ?: return@mapNotNull null
val moves =
entries.mapNotNull { (level, move) ->
moveIds[move]?.let { ParsedLevelUpMove(level, it) }
}
if (moves.isEmpty()) null else ParsedLearnset(dexId, moves)
}
.sortedBy { it.dexId }
}
// static const u16 s<Name>LevelUpLearnset[] = { LEVEL_UP_MOVE(lvl, MOVE_X), ... };
private fun readTables(): Map<String, List<Pair<Int, String>>> {
val file = File(rootDir, "src/data/pokemon/level_up_learnsets.h")
require(file.exists()) {
"Decomp not initialized at $rootDir (missing ${file.path}). " +
"Run: git submodule update --init --recursive"
}
val table =
Regex(
"""static\s+const\s+u16\s+(\w+)\s*\[\s*]\s*=\s*\{(.*?)}""",
RegexOption.DOT_MATCHES_ALL,
)
val move = Regex("""LEVEL_UP_MOVE\(\s*(\d+)\s*,\s*(MOVE_\w+)\s*\)""")
return table.findAll(file.readText()).associate { match ->
match.groupValues[1] to
move
.findAll(match.groupValues[2])
.map { it.groupValues[1].toInt() to it.groupValues[2] }
.toList()
}
}
private fun readPointers(): Map<String, String> {
val file = File(rootDir, "src/data/pokemon/level_up_learnset_pointers.h")
require(file.exists()) { "Missing learnset pointer table at ${file.path}" }
val regex = Regex("""\[(SPECIES_\w+)]\s*=\s*(\w+)""")
return regex.findAll(file.readText()).associate { it.groupValues[1] to it.groupValues[2] }
}
private fun readDefineTable(path: String, prefix: String): Map<String, Int> {
val file = File(rootDir, path)
require(file.exists()) { "Missing $prefix table at ${file.path}" }
return defineTable(file, prefix)
}
}
@@ -0,0 +1,39 @@
package de.fiereu.openmmo.codegen.learnset
import gg.jte.ContentType
import gg.jte.TemplateEngine
import gg.jte.output.FileOutput
import gg.jte.resolve.DirectoryCodeResolver
import java.io.File
class LearnsetRenderer(
private val templatesDir: File,
private val outputDir: File,
private val classCacheDir: File,
private val registryChunkSize: Int = 50,
) {
fun render(learnsets: List<ParsedLearnset>) {
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 chunks = learnsets.sortedBy { it.dexId }.chunked(registryChunkSize)
val registry = File(outputDir, "${BASE_PACKAGE.replace('.', '/')}/GeneratedLearnsets.kt")
registry.parentFile.mkdirs()
FileOutput(registry.toPath()).use { out ->
engine.render("LearnsetRegistry.jte", mapOf("chunks" to chunks), out)
}
}
companion object {
private const val BASE_PACKAGE = "de.fiereu.openmmo.pokemon.generated"
}
}
@@ -0,0 +1,21 @@
@file:JvmName("Main")
package de.fiereu.openmmo.codegen.learnset
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("[learnset] parsing from $decompDir")
val learnsets = LearnsetParser(decompDir).parseAll()
println("[learnset] parsed ${learnsets.size} learnsets. writing to $outputDir")
LearnsetRenderer(templatesDir, outputDir, classCacheDir).render(learnsets)
println("[learnset] done")
}
@@ -0,0 +1,5 @@
package de.fiereu.openmmo.codegen.learnset
data class ParsedLearnset(val dexId: Int, val moves: List<ParsedLevelUpMove>)
data class ParsedLevelUpMove(val level: Int, val moveId: Int)
@@ -0,0 +1,9 @@
package de.fiereu.openmmo.codegen.learnset
object RenderUtil {
fun learnset(l: ParsedLearnset): String {
val moves = l.moves.joinToString(", ") { "LevelUpMove(${it.level}, ${it.moveId})" }
return "reg.register(${l.dexId}, listOf($moves))"
}
}
@@ -1,5 +1,6 @@
package de.fiereu.openmmo.codegen.move
import de.fiereu.openmmo.codegen.defineTable
import java.io.File
class MoveParser(private val rootDir: File) {
@@ -35,11 +36,7 @@ class MoveParser(private val rootDir: File) {
"Decomp not initialized at $rootDir (missing ${file.path}). " +
"Run: git submodule update --init --recursive"
}
val regex = Regex("""^#define\s+(MOVE_\w+)\s+(\d+)\s*$""")
return file
.readLines()
.mapNotNull { regex.find(it.trim()) }
.associate { it.groupValues[1] to it.groupValues[2].toInt() }
return defineTable(file, "MOVE_")
}
private fun readMoveNames(): Map<String, String> {
@@ -1,5 +1,6 @@
package de.fiereu.openmmo.codegen.pokemon
import de.fiereu.openmmo.codegen.defineTable
import java.io.File
/**
@@ -10,23 +11,16 @@ import java.io.File
object NationalDex {
fun read(rootDir: File): Map<Int, Int> {
val internalBySuffix = defineTable(File(rootDir, "include/constants/species.h"), "SPECIES_")
val internalBySuffix =
defineTable(File(rootDir, "include/constants/species.h"), "SPECIES_").mapKeys {
it.key.removePrefix("SPECIES_")
}
val nationalBySuffix = enumTable(File(rootDir, "include/constants/pokedex.h"), "NATIONAL_DEX_")
return internalBySuffix
.mapNotNull { (suffix, internal) -> nationalBySuffix[suffix]?.let { internal to it } }
.toMap()
}
// #define SPECIES_<suffix> <n>
private fun defineTable(file: File, prefix: String): Map<String, Int> {
if (!file.exists()) return emptyMap()
val re = Regex("""^#define\s+$prefix(\w+)\s+(\d+)\s*$""")
return file
.readLines()
.mapNotNull { re.find(it.trim()) }
.associate { it.groupValues[1] to it.groupValues[2].toInt() }
}
// A plain enum where each NATIONAL_DEX_<suffix> takes the next value, starting at NONE = 0.
private fun enumTable(file: File, prefix: String): Map<String, Int> {
if (!file.exists()) return emptyMap()
@@ -1,5 +1,6 @@
package de.fiereu.openmmo.codegen.pokemon
import de.fiereu.openmmo.codegen.defineTable
import java.io.File
class SpeciesParser(private val rootDir: File) {
@@ -86,11 +87,7 @@ class SpeciesParser(private val rootDir: File) {
private fun readDefineTable(path: String, prefix: String): Map<String, Int> {
val file = File(rootDir, path)
require(file.exists()) { "Missing $prefix table at ${file.path}" }
val regex = Regex("""^#define\s+($prefix\w+)\s+(\d+)\s*$""")
return file
.readLines()
.mapNotNull { regex.find(it.trim()) }
.associate { it.groupValues[1] to it.groupValues[2].toInt() }
return defineTable(file, prefix)
}
private fun braceList(value: String): List<String> =