feat(codegen): generate Pokemon species defs from the GBA decomp data (#44)
This commit is contained in:
1 parent
a7d1316ab4
commit
651bf947e4
14 files changed
+497
-3
No files matched your search
@@ -22,7 +22,10 @@ val regionSources =
|
||||
"kanto" to "pokefirered",
|
||||
)
|
||||
|
||||
val movesDecompDir = rootProject.layout.projectDirectory.dir("decomp/pokeemerald")
|
||||
// Single source-of-truth decomp for the non-region-specific data (moves, species). The two
|
||||
// GBA decomps agree on the national dex; where they differ (held items, safari flee rate) this
|
||||
// is the canonical pick, same as byRegion is for maps.
|
||||
val sourceDecompDir = rootProject.layout.projectDirectory.dir("decomp/pokeemerald")
|
||||
|
||||
jteCodegen {
|
||||
register("maps") {
|
||||
@@ -37,7 +40,12 @@ jteCodegen {
|
||||
register("moves") {
|
||||
mainClass.set("de.fiereu.openmmo.codegen.move.Main")
|
||||
templatesSubdir.set("move")
|
||||
inputDirs.from(movesDecompDir)
|
||||
extraArgs.set(listOf(movesDecompDir.asFile.absolutePath))
|
||||
inputDirs.from(sourceDecompDir)
|
||||
extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath))
|
||||
}
|
||||
register("pokemon") {
|
||||
mainClass.set("de.fiereu.openmmo.codegen.pokemon.Main")
|
||||
inputDirs.from(sourceDecompDir)
|
||||
extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
@import java.util.List
|
||||
@import de.fiereu.openmmo.codegen.pokemon.ParsedSpecies
|
||||
@import de.fiereu.openmmo.codegen.pokemon.RenderUtil
|
||||
|
||||
@param List<List<ParsedSpecies>> chunks
|
||||
package de.fiereu.openmmo.pokemon.generated
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Ability
|
||||
import de.fiereu.openmmo.common.enums.BodyColor
|
||||
import de.fiereu.openmmo.common.enums.EggGroup
|
||||
import de.fiereu.openmmo.common.enums.GrowthRate
|
||||
import de.fiereu.openmmo.common.enums.PokemonType
|
||||
import de.fiereu.openmmo.pokemon.SpeciesDef
|
||||
import de.fiereu.openmmo.pokemon.SpeciesRegistry
|
||||
|
||||
internal object GeneratedSpecies {
|
||||
fun loadInto(reg: SpeciesRegistry) {
|
||||
@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: SpeciesRegistry) {
|
||||
@for(ParsedSpecies s : chunks.get(c))
|
||||
reg.register(${RenderUtil.INSTANCE.species(s)})
|
||||
@endfor
|
||||
}
|
||||
|
||||
@endfor
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.fiereu.openmmo.codegen.pokemon
|
||||
|
||||
fun typeRef(token: String): String {
|
||||
val name = token.removePrefix("TYPE_")
|
||||
val mapped = if (name == "MYSTERY") "QUESTIONQUESTIONQUESTION" else name
|
||||
return "PokemonType.$mapped"
|
||||
}
|
||||
|
||||
fun abilityRef(token: String): String = "Ability.${token.removePrefix("ABILITY_")}"
|
||||
|
||||
fun growthRef(token: String): String = "GrowthRate.${token.removePrefix("GROWTH_")}"
|
||||
|
||||
fun eggGroupRef(token: String): String = "EggGroup.${token.removePrefix("EGG_GROUP_")}"
|
||||
|
||||
fun bodyColorRef(token: String): String = "BodyColor.${token.removePrefix("BODY_COLOR_")}"
|
||||
@@ -0,0 +1,21 @@
|
||||
@file:JvmName("Main")
|
||||
|
||||
package de.fiereu.openmmo.codegen.pokemon
|
||||
|
||||
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("[pokemon] parsing from $decompDir")
|
||||
val species = SpeciesParser(decompDir).parseAll()
|
||||
println("[pokemon] parsed ${species.size} species. writing to $outputDir")
|
||||
PokemonRenderer(templatesDir, outputDir, classCacheDir).render(species)
|
||||
println("[pokemon] done")
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package de.fiereu.openmmo.codegen.pokemon
|
||||
|
||||
data class ParsedSpecies(
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val baseHp: Int,
|
||||
val baseAttack: Int,
|
||||
val baseDefense: Int,
|
||||
val baseSpeed: Int,
|
||||
val baseSpAttack: Int,
|
||||
val baseSpDefense: Int,
|
||||
val type1: String,
|
||||
val type2: String,
|
||||
val catchRate: Int,
|
||||
val expYield: Int,
|
||||
val evYieldHp: Int,
|
||||
val evYieldAttack: Int,
|
||||
val evYieldDefense: Int,
|
||||
val evYieldSpeed: Int,
|
||||
val evYieldSpAttack: Int,
|
||||
val evYieldSpDefense: Int,
|
||||
val itemCommon: Int,
|
||||
val itemRare: Int,
|
||||
val genderRatio: Int,
|
||||
val eggCycles: Int,
|
||||
val friendship: Int,
|
||||
val growthRate: String,
|
||||
val eggGroup1: String,
|
||||
val eggGroup2: String,
|
||||
val ability1: String,
|
||||
val ability2: String,
|
||||
val safariZoneFleeRate: Int,
|
||||
val bodyColor: String,
|
||||
val noFlip: Boolean,
|
||||
)
|
||||
@@ -0,0 +1,39 @@
|
||||
package de.fiereu.openmmo.codegen.pokemon
|
||||
|
||||
import gg.jte.ContentType
|
||||
import gg.jte.TemplateEngine
|
||||
import gg.jte.output.FileOutput
|
||||
import gg.jte.resolve.DirectoryCodeResolver
|
||||
import java.io.File
|
||||
|
||||
class PokemonRenderer(
|
||||
private val templatesDir: File,
|
||||
private val outputDir: File,
|
||||
private val classCacheDir: File,
|
||||
private val registryChunkSize: Int = 50,
|
||||
) {
|
||||
|
||||
fun render(species: List<ParsedSpecies>) {
|
||||
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 = species.sortedBy { it.id }.chunked(registryChunkSize)
|
||||
val registry = File(outputDir, "${BASE_PACKAGE.replace('.', '/')}/GeneratedSpecies.kt")
|
||||
registry.parentFile.mkdirs()
|
||||
FileOutput(registry.toPath()).use { out ->
|
||||
engine.render("SpeciesRegistry.jte", mapOf("chunks" to chunks), out)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val BASE_PACKAGE = "de.fiereu.openmmo.pokemon.generated"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package de.fiereu.openmmo.codegen.pokemon
|
||||
|
||||
object RenderUtil {
|
||||
|
||||
fun species(s: ParsedSpecies): String =
|
||||
"SpeciesDef(id = ${s.id}, name = ${escapeString(s.name)}," +
|
||||
" baseHp = ${s.baseHp}, baseAttack = ${s.baseAttack}, baseDefense = ${s.baseDefense}," +
|
||||
" baseSpeed = ${s.baseSpeed}, baseSpAttack = ${s.baseSpAttack}," +
|
||||
" baseSpDefense = ${s.baseSpDefense}, type1 = ${s.type1}, type2 = ${s.type2}," +
|
||||
" catchRate = ${s.catchRate}, expYield = ${s.expYield}," +
|
||||
" evYieldHp = ${s.evYieldHp}, evYieldAttack = ${s.evYieldAttack}," +
|
||||
" evYieldDefense = ${s.evYieldDefense}, evYieldSpeed = ${s.evYieldSpeed}," +
|
||||
" evYieldSpAttack = ${s.evYieldSpAttack}, evYieldSpDefense = ${s.evYieldSpDefense}," +
|
||||
" itemCommon = ${s.itemCommon}, itemRare = ${s.itemRare}," +
|
||||
" genderRatio = ${s.genderRatio}, eggCycles = ${s.eggCycles}," +
|
||||
" friendship = ${s.friendship}, growthRate = ${s.growthRate}," +
|
||||
" eggGroup1 = ${s.eggGroup1}, eggGroup2 = ${s.eggGroup2}," +
|
||||
" ability1 = ${s.ability1}, ability2 = ${s.ability2}," +
|
||||
" safariZoneFleeRate = ${s.safariZoneFleeRate}, bodyColor = ${s.bodyColor}," +
|
||||
" noFlip = ${s.noFlip})"
|
||||
|
||||
private fun escapeString(s: String): String =
|
||||
"\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package de.fiereu.openmmo.codegen.pokemon
|
||||
|
||||
import java.io.File
|
||||
|
||||
class SpeciesParser(private val rootDir: File) {
|
||||
|
||||
fun parseAll(): List<ParsedSpecies> {
|
||||
val ids = readDefineTable("include/constants/species.h", "SPECIES_")
|
||||
val names = readSpeciesNames()
|
||||
val items = readDefineTable("include/constants/items.h", "ITEM_")
|
||||
val entries = readSpeciesInfo()
|
||||
|
||||
return entries
|
||||
.mapNotNull { (constant, f) ->
|
||||
val id = ids[constant] ?: return@mapNotNull null
|
||||
if (!f.containsKey("baseHP")) return@mapNotNull null
|
||||
val types = braceList(f.getValue("types"))
|
||||
val eggGroups = braceList(f.getValue("eggGroups"))
|
||||
val abilities = braceList(f.getValue("abilities"))
|
||||
ParsedSpecies(
|
||||
id = id,
|
||||
name = names[constant] ?: constant.removePrefix("SPECIES_"),
|
||||
baseHp = intOf(f.getValue("baseHP")),
|
||||
baseAttack = intOf(f.getValue("baseAttack")),
|
||||
baseDefense = intOf(f.getValue("baseDefense")),
|
||||
baseSpeed = intOf(f.getValue("baseSpeed")),
|
||||
baseSpAttack = intOf(f.getValue("baseSpAttack")),
|
||||
baseSpDefense = intOf(f.getValue("baseSpDefense")),
|
||||
type1 = typeRef(types[0]),
|
||||
type2 = typeRef(types.getOrElse(1) { types[0] }),
|
||||
catchRate = intOf(f.getValue("catchRate")),
|
||||
expYield = intOf(f.getValue("expYield")),
|
||||
evYieldHp = intOf(f.getValue("evYield_HP")),
|
||||
evYieldAttack = intOf(f.getValue("evYield_Attack")),
|
||||
evYieldDefense = intOf(f.getValue("evYield_Defense")),
|
||||
evYieldSpeed = intOf(f.getValue("evYield_Speed")),
|
||||
evYieldSpAttack = intOf(f.getValue("evYield_SpAttack")),
|
||||
evYieldSpDefense = intOf(f.getValue("evYield_SpDefense")),
|
||||
itemCommon = items[f.getValue("itemCommon")] ?: 0,
|
||||
itemRare = items[f.getValue("itemRare")] ?: 0,
|
||||
genderRatio = genderRatio(f.getValue("genderRatio")),
|
||||
eggCycles = intOf(f.getValue("eggCycles")),
|
||||
friendship = intOf(f.getValue("friendship")),
|
||||
growthRate = growthRef(f.getValue("growthRate")),
|
||||
eggGroup1 = eggGroupRef(eggGroups[0]),
|
||||
eggGroup2 = eggGroupRef(eggGroups.getOrElse(1) { eggGroups[0] }),
|
||||
ability1 = abilityRef(abilities[0]),
|
||||
ability2 = abilityRef(abilities.getOrElse(1) { "ABILITY_NONE" }),
|
||||
safariZoneFleeRate = intOf(f.getValue("safariZoneFleeRate")),
|
||||
bodyColor = bodyColorRef(f.getValue("bodyColor")),
|
||||
noFlip = f["noFlip"] == "TRUE",
|
||||
)
|
||||
}
|
||||
.sortedBy { it.id }
|
||||
}
|
||||
|
||||
private fun readSpeciesInfo(): Map<String, Map<String, String>> {
|
||||
val file = File(rootDir, "src/data/pokemon/species_info.h")
|
||||
require(file.exists()) {
|
||||
"Decomp not initialized at $rootDir (missing ${file.path}). " +
|
||||
"Run: git submodule update --init --recursive"
|
||||
}
|
||||
val text = file.readText()
|
||||
val entryStart = Regex("""\[(SPECIES_\w+)\]\s*=""")
|
||||
val field = Regex("""\.(\w+)\s*=\s*(\{[^}]*\}|[^,\n]+?)\s*,""")
|
||||
val matches = entryStart.findAll(text).toList()
|
||||
return matches
|
||||
.mapIndexed { i, m ->
|
||||
val end = if (i + 1 < matches.size) matches[i + 1].range.first else text.length
|
||||
val body = text.substring(m.range.last + 1, end)
|
||||
m.groupValues[1] to
|
||||
field.findAll(body).associate { it.groupValues[1] to it.groupValues[2].trim() }
|
||||
}
|
||||
.toMap()
|
||||
}
|
||||
|
||||
private fun readSpeciesNames(): Map<String, String> {
|
||||
val file = File(rootDir, "src/data/text/species_names.h")
|
||||
require(file.exists()) { "Missing species names table at ${file.path}" }
|
||||
val regex = Regex("""\[(SPECIES_\w+)\]\s*=\s*_\("([^"]*)"\)""")
|
||||
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}" }
|
||||
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() }
|
||||
}
|
||||
|
||||
private fun braceList(value: String): List<String> =
|
||||
value
|
||||
.trim()
|
||||
.removePrefix("{")
|
||||
.removeSuffix("}")
|
||||
.split(",")
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
|
||||
private fun intOf(token: String): Int =
|
||||
when (token) {
|
||||
"STANDARD_FRIENDSHIP" -> 70
|
||||
"TRUE" -> 1
|
||||
"FALSE" -> 0
|
||||
else -> token.toIntOrNull() ?: token.removePrefix("0x").toIntOrNull(16) ?: 0
|
||||
}
|
||||
|
||||
private fun genderRatio(token: String): Int =
|
||||
when {
|
||||
token == "MON_MALE" -> 0
|
||||
token == "MON_FEMALE" -> 254
|
||||
token == "MON_GENDERLESS" -> 255
|
||||
token.startsWith("PERCENT_FEMALE(") ->
|
||||
minOf(
|
||||
254,
|
||||
(token.removePrefix("PERCENT_FEMALE(").removeSuffix(")").trim().toDouble() * 255 /
|
||||
100)
|
||||
.toInt())
|
||||
else -> intOf(token)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package de.fiereu.openmmo.pokemon
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Ability
|
||||
import de.fiereu.openmmo.common.enums.BodyColor
|
||||
import de.fiereu.openmmo.common.enums.EggGroup
|
||||
import de.fiereu.openmmo.common.enums.GrowthRate
|
||||
import de.fiereu.openmmo.common.enums.PokemonType
|
||||
|
||||
data class SpeciesDef(
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val baseHp: Int,
|
||||
val baseAttack: Int,
|
||||
val baseDefense: Int,
|
||||
val baseSpeed: Int,
|
||||
val baseSpAttack: Int,
|
||||
val baseSpDefense: Int,
|
||||
val type1: PokemonType,
|
||||
val type2: PokemonType,
|
||||
val catchRate: Int,
|
||||
val expYield: Int,
|
||||
val evYieldHp: Int,
|
||||
val evYieldAttack: Int,
|
||||
val evYieldDefense: Int,
|
||||
val evYieldSpeed: Int,
|
||||
val evYieldSpAttack: Int,
|
||||
val evYieldSpDefense: Int,
|
||||
val itemCommon: Int,
|
||||
val itemRare: Int,
|
||||
val genderRatio: Int,
|
||||
val eggCycles: Int,
|
||||
val friendship: Int,
|
||||
val growthRate: GrowthRate,
|
||||
val eggGroup1: EggGroup,
|
||||
val eggGroup2: EggGroup,
|
||||
val ability1: Ability,
|
||||
val ability2: Ability,
|
||||
val safariZoneFleeRate: Int,
|
||||
val bodyColor: BodyColor,
|
||||
val noFlip: Boolean,
|
||||
) {
|
||||
val types: Set<PokemonType> = setOf(type1, type2)
|
||||
|
||||
fun hasType(type: PokemonType): Boolean = type1 == type || type2 == type
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package de.fiereu.openmmo.pokemon
|
||||
|
||||
import de.fiereu.openmmo.pokemon.generated.GeneratedSpecies
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SpeciesRegistry @Inject constructor() {
|
||||
|
||||
private val species = ConcurrentHashMap<Int, SpeciesDef>()
|
||||
|
||||
init {
|
||||
GeneratedSpecies.loadInto(this)
|
||||
}
|
||||
|
||||
fun register(def: SpeciesDef) {
|
||||
species[def.id] = def
|
||||
}
|
||||
|
||||
fun get(id: Int): SpeciesDef? = species[id]
|
||||
|
||||
fun all(): Collection<SpeciesDef> = species.values
|
||||
|
||||
fun size(): Int = species.size
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package de.fiereu.openmmo.common.enums
|
||||
|
||||
enum class Ability {
|
||||
NONE,
|
||||
STENCH,
|
||||
DRIZZLE,
|
||||
SPEED_BOOST,
|
||||
BATTLE_ARMOR,
|
||||
STURDY,
|
||||
DAMP,
|
||||
LIMBER,
|
||||
SAND_VEIL,
|
||||
STATIC,
|
||||
VOLT_ABSORB,
|
||||
WATER_ABSORB,
|
||||
OBLIVIOUS,
|
||||
CLOUD_NINE,
|
||||
COMPOUND_EYES,
|
||||
INSOMNIA,
|
||||
COLOR_CHANGE,
|
||||
IMMUNITY,
|
||||
FLASH_FIRE,
|
||||
SHIELD_DUST,
|
||||
OWN_TEMPO,
|
||||
SUCTION_CUPS,
|
||||
INTIMIDATE,
|
||||
SHADOW_TAG,
|
||||
ROUGH_SKIN,
|
||||
WONDER_GUARD,
|
||||
LEVITATE,
|
||||
EFFECT_SPORE,
|
||||
SYNCHRONIZE,
|
||||
CLEAR_BODY,
|
||||
NATURAL_CURE,
|
||||
LIGHTNING_ROD,
|
||||
SERENE_GRACE,
|
||||
SWIFT_SWIM,
|
||||
CHLOROPHYLL,
|
||||
ILLUMINATE,
|
||||
TRACE,
|
||||
HUGE_POWER,
|
||||
POISON_POINT,
|
||||
INNER_FOCUS,
|
||||
MAGMA_ARMOR,
|
||||
WATER_VEIL,
|
||||
MAGNET_PULL,
|
||||
SOUNDPROOF,
|
||||
RAIN_DISH,
|
||||
SAND_STREAM,
|
||||
PRESSURE,
|
||||
THICK_FAT,
|
||||
EARLY_BIRD,
|
||||
FLAME_BODY,
|
||||
RUN_AWAY,
|
||||
KEEN_EYE,
|
||||
HYPER_CUTTER,
|
||||
PICKUP,
|
||||
TRUANT,
|
||||
HUSTLE,
|
||||
CUTE_CHARM,
|
||||
PLUS,
|
||||
MINUS,
|
||||
FORECAST,
|
||||
STICKY_HOLD,
|
||||
SHED_SKIN,
|
||||
GUTS,
|
||||
MARVEL_SCALE,
|
||||
LIQUID_OOZE,
|
||||
OVERGROW,
|
||||
BLAZE,
|
||||
TORRENT,
|
||||
SWARM,
|
||||
ROCK_HEAD,
|
||||
DROUGHT,
|
||||
ARENA_TRAP,
|
||||
VITAL_SPIRIT,
|
||||
WHITE_SMOKE,
|
||||
PURE_POWER,
|
||||
SHELL_ARMOR,
|
||||
CACOPHONY,
|
||||
AIR_LOCK,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package de.fiereu.openmmo.common.enums
|
||||
|
||||
enum class BodyColor {
|
||||
RED,
|
||||
BLUE,
|
||||
YELLOW,
|
||||
GREEN,
|
||||
BLACK,
|
||||
BROWN,
|
||||
PURPLE,
|
||||
GRAY,
|
||||
WHITE,
|
||||
PINK,
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package de.fiereu.openmmo.common.enums
|
||||
|
||||
enum class EggGroup {
|
||||
NONE,
|
||||
MONSTER,
|
||||
WATER_1,
|
||||
BUG,
|
||||
FLYING,
|
||||
FIELD,
|
||||
FAIRY,
|
||||
GRASS,
|
||||
HUMAN_LIKE,
|
||||
WATER_3,
|
||||
MINERAL,
|
||||
AMORPHOUS,
|
||||
WATER_2,
|
||||
DITTO,
|
||||
DRAGON,
|
||||
NO_EGGS_DISCOVERED,
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package de.fiereu.openmmo.common.enums
|
||||
|
||||
enum class GrowthRate {
|
||||
MEDIUM_FAST,
|
||||
ERRATIC,
|
||||
FLUCTUATING,
|
||||
MEDIUM_SLOW,
|
||||
FAST,
|
||||
SLOW,
|
||||
}
|
||||
Reference in new issue
Block a user