feat(codegen): generate the gen 3 type chart (#54)
This commit is contained in:
1 parent
017f6bca60
commit
391e2efeb7
8 files changed
+201
No files matched your search
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -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<Pair<PokemonType, PokemonType>, 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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user