feat(game): implement the Kanto story from Pallet Town through the Pokedex (#127)
* feat(game): implement Kanto story through the Pokedex * fix(game): stop the rival walking into the player in the pokedex scene * fix(game): stop oak teleporting to his desk in the starter scene * fix(game): make a scripted warp arrive the way a walked one does * fix(game): finish the oaks lab rival scene whether the player wins or loses * chore(game): tidy the kanto script and item codegen comments * docs: point the script guides at MapEntryScripts and trainerBattle * fix(codegen): send item ids in the region whose table matches the decomp
This commit is contained in:
1 parent
70c746fdc6
commit
9215a0862a
35 files changed
+1311
-508
No files matched your search
@@ -41,6 +41,16 @@ jteCodegen {
|
||||
"$region|${rootProject.layout.projectDirectory.dir("decomp/$decomp").asFile.absolutePath}"
|
||||
})
|
||||
}
|
||||
// Item ids scripts hand to giveItem. Both GBA games number items the same, so one decomp is
|
||||
// enough, like moves and species. The client holds one item table per region, so the region
|
||||
// decides what a number means. Checked against a real client: region 0 reads 3, 4 and 13 as
|
||||
// Great Ball, Poke Ball and Potion, matching the decomp, while region 1 and region 5 read the
|
||||
// same numbers as unrelated items.
|
||||
register("item") {
|
||||
mainClass.set("de.fiereu.openmmo.codegen.item.Main")
|
||||
inputDirs.from(sourceDecompDir)
|
||||
extraArgs.set(listOf("kanto|${sourceDecompDir.asFile.absolutePath}"))
|
||||
}
|
||||
register("moves") {
|
||||
mainClass.set("de.fiereu.openmmo.codegen.move.Main")
|
||||
templatesSubdir.set("move")
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
@import java.util.List
|
||||
@import de.fiereu.openmmo.codegen.item.ParsedItem
|
||||
|
||||
@param List<ParsedItem> items
|
||||
package de.fiereu.openmmo.items.generated
|
||||
|
||||
/**
|
||||
* Item ids the client uses, already carrying the region prefix of the decomp they were read from.
|
||||
* Generated from the GBA decomp item header, do not edit by hand.
|
||||
*/
|
||||
object Items {
|
||||
@for(ParsedItem item : items)
|
||||
const val ${item.getName()} = ${item.getId()}
|
||||
@endfor
|
||||
}
|
||||
@@ -13,7 +13,14 @@ class TextParser(private val decompDir: File) {
|
||||
private val labelLine = Regex("^(\\w+):+\\s*$")
|
||||
|
||||
fun parseAll(): List<DecompText> {
|
||||
val roots = listOf(File(decompDir, "data/maps"), File(decompDir, "data/scripts"))
|
||||
// Emerald keeps its shared text next to the scripts that use it, FireRed keeps it in its own
|
||||
// data/text folder. Reading all three roots covers both.
|
||||
val roots =
|
||||
listOf(
|
||||
File(decompDir, "data/maps"),
|
||||
File(decompDir, "data/scripts"),
|
||||
File(decompDir, "data/text"),
|
||||
)
|
||||
val out = LinkedHashMap<String, String>()
|
||||
for (root in roots) {
|
||||
if (!root.isDirectory) continue
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package de.fiereu.openmmo.codegen.item
|
||||
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Reads the item constants out of a GBA decomp header. The two GBA decomps use the same item
|
||||
* numbering, so one decomp is the single source of truth, the same way moves and species are.
|
||||
*
|
||||
* The client keys an item by `regionId * 1000 + itemId` and holds one item table per region.
|
||||
* [regionId] picks the table that matches the decomp's own numbering, which leaves the Poke Ball at
|
||||
* its decomp 4.
|
||||
*/
|
||||
class ItemParser(private val decompDir: File, private val regionId: Int) {
|
||||
|
||||
fun parseAll(): List<ParsedItem> {
|
||||
val header = File(decompDir, "include/constants/items.h")
|
||||
require(header.exists()) { "Missing ${header.path}" }
|
||||
val define = Regex("""^#define\s+ITEM_([A-Z0-9_]+)\s+(\d+)\s*$""")
|
||||
// ITEMS_COUNT closes the item list. Everything after it reuses the ITEM_ prefix for unrelated
|
||||
// constants like the bag usage types, so parsing stops there.
|
||||
return header
|
||||
.readLines()
|
||||
.takeWhile { !it.trimStart().startsWith("#define ITEMS_COUNT") }
|
||||
.mapNotNull { define.find(it.trim()) }
|
||||
.map { ParsedItem(it.groupValues[1], regionId * 1000 + it.groupValues[2].toInt()) }
|
||||
// Placeholder slots are named after their hex index, for example ITEM_15C.
|
||||
.filterNot { it.name.first().isDigit() }
|
||||
.distinctBy(ParsedItem::name)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package de.fiereu.openmmo.codegen.item
|
||||
|
||||
import gg.jte.ContentType
|
||||
import gg.jte.TemplateEngine
|
||||
import gg.jte.output.FileOutput
|
||||
import gg.jte.resolve.DirectoryCodeResolver
|
||||
import java.io.File
|
||||
|
||||
class ItemsRenderer(
|
||||
private val templatesDir: File,
|
||||
private val outputDir: File,
|
||||
private val classCacheDir: File,
|
||||
) {
|
||||
|
||||
fun render(items: List<ParsedItem>) {
|
||||
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()
|
||||
packageRoot.mkdirs()
|
||||
|
||||
FileOutput(File(packageRoot, "Items.kt").toPath()).use { out ->
|
||||
engine.render("Items.jte", mapOf("items" to items), out)
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val BASE_PACKAGE = "de.fiereu.openmmo.items.generated"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
@file:JvmName("Main")
|
||||
|
||||
package de.fiereu.openmmo.codegen.item
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Region
|
||||
import java.io.File
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
require(args.size >= 4) {
|
||||
"Usage: <output-dir> <templates-dir> <class-cache-dir> <region|decomp> got ${args.toList()}"
|
||||
}
|
||||
val outputDir = File(args[0])
|
||||
val templatesDir = File(args[1])
|
||||
val classCacheDir = File(args[2])
|
||||
val (region, decomp) = args[3].split("|")
|
||||
val decompDir = File(decomp)
|
||||
val regionId = Region.valueOf(region.uppercase()).wireValue.toInt()
|
||||
|
||||
println("[item] parsing $region items from $decompDir")
|
||||
val items = ItemParser(decompDir, regionId).parseAll()
|
||||
println("[item] parsed ${items.size} items. writing to $outputDir")
|
||||
ItemsRenderer(templatesDir, outputDir, classCacheDir).render(items)
|
||||
println("[item] done")
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package de.fiereu.openmmo.codegen.item
|
||||
|
||||
/** One item constant: the decomp name without its ITEM_ prefix and the id the client uses. */
|
||||
data class ParsedItem(val name: String, val id: Int)
|
||||
@@ -308,8 +308,9 @@ class PretGbaParser(
|
||||
.find(text)
|
||||
?.groupValues
|
||||
?.get(1) ?: return emptyList()
|
||||
// Emerald writes the table label with one colon, FireRed with two.
|
||||
val block =
|
||||
Regex("""(?m)^$tableLabel:\s*\n(.*?)(?=\n\s*\.2byte)""", RegexOption.DOT_MATCHES_ALL)
|
||||
Regex("""(?m)^$tableLabel:+[^\n]*\n(.*?)(?=\n\s*\.2byte)""", RegexOption.DOT_MATCHES_ALL)
|
||||
.find(text)
|
||||
?.groupValues
|
||||
?.get(1) ?: return emptyList()
|
||||
|
||||
@@ -18,32 +18,27 @@ object FlagVarParser {
|
||||
fun vars(decompDir: File): List<StoryConstant> =
|
||||
constants(decompDir, "include/constants/vars.h", "VAR_")
|
||||
|
||||
/** Emerald new-game reset flags. */
|
||||
/** Flags the source game's new-game reset sets. */
|
||||
fun initialFlags(decompDir: File): List<String> =
|
||||
scriptFlags(
|
||||
decompDir,
|
||||
"data/scripts/new_game.inc",
|
||||
"EventScript_ResetAllMapFlags",
|
||||
)
|
||||
scriptFlags(decompDir, "EventScript_ResetAllMapFlags")
|
||||
|
||||
/** Gender-specific flags normally established by InsideOfTruck's intro script. */
|
||||
/**
|
||||
* Gender-specific flags the source game establishes during its intro. Emerald sets them in the
|
||||
* moving truck. A game without such a script contributes nothing.
|
||||
*/
|
||||
fun maleIntroFlags(decompDir: File): List<String> =
|
||||
scriptFlags(
|
||||
decompDir,
|
||||
"data/maps/InsideOfTruck/scripts.inc",
|
||||
"InsideOfTruck_EventScript_SetIntroFlagsMale",
|
||||
)
|
||||
scriptFlags(decompDir, "InsideOfTruck_EventScript_SetIntroFlagsMale")
|
||||
|
||||
fun femaleIntroFlags(decompDir: File): List<String> =
|
||||
scriptFlags(
|
||||
decompDir,
|
||||
"data/maps/InsideOfTruck/scripts.inc",
|
||||
"InsideOfTruck_EventScript_SetIntroFlagsFemale",
|
||||
)
|
||||
scriptFlags(decompDir, "InsideOfTruck_EventScript_SetIntroFlagsFemale")
|
||||
|
||||
private fun scriptFlags(decompDir: File, relativePath: String, label: String): List<String> {
|
||||
val file = File(decompDir, relativePath)
|
||||
if (!file.exists()) return emptyList()
|
||||
/**
|
||||
* The flags one decomp script sets, found by its label. Each game keeps these scripts in a
|
||||
* different file (Emerald in data/scripts/new_game.inc, FireRed in data/event_scripts.s), so the
|
||||
* label is looked up across the data folder instead of at a fixed path.
|
||||
*/
|
||||
private fun scriptFlags(decompDir: File, label: String): List<String> {
|
||||
val file = findScriptFile(decompDir, label) ?: return emptyList()
|
||||
val result = mutableListOf<String>()
|
||||
var inBlock = false
|
||||
for (rawLine in file.readLines()) {
|
||||
@@ -63,6 +58,15 @@ object FlagVarParser {
|
||||
return result.distinct()
|
||||
}
|
||||
|
||||
private fun findScriptFile(decompDir: File, label: String): File? {
|
||||
val definition = "$label::"
|
||||
return File(decompDir, "data")
|
||||
.walkTopDown()
|
||||
.filter { it.isFile && it.extension in SCRIPT_EXTENSIONS }
|
||||
.sortedBy(File::getPath)
|
||||
.firstOrNull { file -> file.useLines { lines -> lines.any { it.trim() == definition } } }
|
||||
}
|
||||
|
||||
private fun constants(
|
||||
decompDir: File,
|
||||
relativePath: String,
|
||||
@@ -309,4 +313,6 @@ object FlagVarParser {
|
||||
|
||||
private fun parseNumber(value: String): Int =
|
||||
if (value.startsWith("0x", ignoreCase = true)) value.drop(2).toInt(16) else value.toInt()
|
||||
|
||||
private val SCRIPT_EXTENSIONS = setOf("inc", "s")
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package de.fiereu.openmmo.codegen.item
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Region
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
class ItemParserTest :
|
||||
FunSpec({
|
||||
test("prefixes decomp item ids with their region and stops at the list end") {
|
||||
val decomp = kotlin.io.path.createTempDirectory("items").toFile()
|
||||
decomp.resolve("include/constants").mkdirs()
|
||||
decomp
|
||||
.resolve("include/constants/items.h")
|
||||
.writeText(
|
||||
"""
|
||||
#define ITEM_NONE 0
|
||||
#define ITEM_GREAT_BALL 3
|
||||
#define ITEM_POKE_BALL 4
|
||||
#define ITEM_15C 348
|
||||
#define ITEM_LIST_END 0xFFFF
|
||||
#define ITEMS_COUNT 377
|
||||
#define ITEM_USE_MAIL 0
|
||||
#define ITEM_HAS_EFFECT(item) ((item) >= ITEM_POTION)
|
||||
"""
|
||||
.trimIndent())
|
||||
|
||||
ItemParser(decomp, Region.KANTO.wireValue.toInt()).parseAll() shouldBe
|
||||
listOf(
|
||||
ParsedItem("NONE", 0),
|
||||
ParsedItem("GREAT_BALL", 3),
|
||||
ParsedItem("POKE_BALL", 4),
|
||||
)
|
||||
|
||||
ItemParser(decomp, Region.HOENN.wireValue.toInt()).parseAll() shouldBe
|
||||
listOf(
|
||||
ParsedItem("NONE", 1000),
|
||||
ParsedItem("GREAT_BALL", 1003),
|
||||
ParsedItem("POKE_BALL", 1004),
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -76,4 +76,23 @@ class FlagVarParserTest :
|
||||
FlagVarParser.maleIntroFlags(decomp) shouldBe listOf("FLAG_MALE")
|
||||
FlagVarParser.femaleIntroFlags(decomp) shouldBe listOf("FLAG_FEMALE")
|
||||
}
|
||||
|
||||
// FireRed keeps the reset script in data/event_scripts.s and has no moving truck intro.
|
||||
test("finds the reset script wherever the decomp keeps it") {
|
||||
val decomp = kotlin.io.path.createTempDirectory("story-script-flags-alt").toFile()
|
||||
decomp.resolve("data").mkdirs()
|
||||
decomp
|
||||
.resolve("data/event_scripts.s")
|
||||
.writeText(
|
||||
"""
|
||||
EventScript_ResetAllMapFlags::
|
||||
setflag FLAG_HIDE_OAK_IN_HIS_LAB
|
||||
setvar VAR_SOMETHING, 500
|
||||
end
|
||||
"""
|
||||
.trimIndent())
|
||||
|
||||
FlagVarParser.initialFlags(decomp) shouldBe listOf("FLAG_HIDE_OAK_IN_HIS_LAB")
|
||||
FlagVarParser.maleIntroFlags(decomp) shouldBe emptyList()
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user