feat(dialog): generate per-location gba dialog enums from the rom and drop the old script params (#64)
This commit is contained in:
1 parent
e89ae6e6df
commit
9c52eeb8c6
13 files changed
+419
-132
No files matched your search
@@ -28,6 +28,9 @@ val regionSources =
|
|||||||
// is the canonical pick, same as byRegion is for maps.
|
// is the canonical pick, same as byRegion is for maps.
|
||||||
val sourceDecompDir = rootProject.layout.projectDirectory.dir("decomp/pokeemerald")
|
val sourceDecompDir = rootProject.layout.projectDirectory.dir("decomp/pokeemerald")
|
||||||
|
|
||||||
|
// ROMs (gitignored) that dialog codegen reads offsets from
|
||||||
|
val romsDir = rootProject.layout.projectDirectory.dir("roms")
|
||||||
|
|
||||||
jteCodegen {
|
jteCodegen {
|
||||||
register("maps") {
|
register("maps") {
|
||||||
mainClass.set("de.fiereu.openmmo.codegen.maps.Main")
|
mainClass.set("de.fiereu.openmmo.codegen.maps.Main")
|
||||||
@@ -54,4 +57,17 @@ jteCodegen {
|
|||||||
inputDirs.from(sourceDecompDir)
|
inputDirs.from(sourceDecompDir)
|
||||||
extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath))
|
extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath))
|
||||||
}
|
}
|
||||||
|
// Dialog ids are ROM offsets, so the generator reads a ROM from the roms folder,
|
||||||
|
// picking it by its GBA header game code. Each region is "package|gameCode|decompDir".
|
||||||
|
register("dialog") {
|
||||||
|
mainClass.set("de.fiereu.openmmo.codegen.dialog.Main")
|
||||||
|
val fireredDir = rootProject.layout.projectDirectory.dir("decomp/pokefirered")
|
||||||
|
inputDirs.from(sourceDecompDir, fireredDir, romsDir)
|
||||||
|
extraArgs.set(
|
||||||
|
listOf(
|
||||||
|
romsDir.asFile.absolutePath,
|
||||||
|
"hoenn|BPEE|${sourceDecompDir.asFile.absolutePath}",
|
||||||
|
"kanto|BPRE|${fireredDir.asFile.absolutePath}",
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
@import java.util.List
|
||||||
|
@import de.fiereu.openmmo.codegen.dialog.DialogEntry
|
||||||
|
@import de.fiereu.openmmo.codegen.dialog.RenderUtil
|
||||||
|
|
||||||
|
@param String pkg
|
||||||
|
@param String enumName
|
||||||
|
@param List<DialogEntry> entries
|
||||||
|
package ${pkg}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overworld dialog lines for this location, generated from the decomp text. Each [textId] is
|
||||||
|
* 0x10000000 or the string's retail ROM offset, ready to send in a DialogAction packet.
|
||||||
|
*/
|
||||||
|
enum class ${enumName}(val textId: Int) {
|
||||||
|
@for(DialogEntry e : entries)
|
||||||
|
${RenderUtil.INSTANCE.entry(e)}
|
||||||
|
@endfor
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The GBA text encoder built from a pret decomp charmap.txt. It turns the literal content of a
|
||||||
|
* `.string` (letters, `\n`/`\l`/`\p` escapes, `{NAME}` placeholders, the `$` terminator) into the
|
||||||
|
* exact bytes the game stores in the ROM, so the encoded form can be located there.
|
||||||
|
*/
|
||||||
|
class Charmap
|
||||||
|
private constructor(
|
||||||
|
private val chars: Map<Char, ByteArray>,
|
||||||
|
private val escapes: Map<String, ByteArray>,
|
||||||
|
private val placeholders: Map<String, ByteArray>,
|
||||||
|
) {
|
||||||
|
|
||||||
|
/** Encodes [text], or returns null if it uses a symbol the charmap cannot represent. */
|
||||||
|
fun encode(text: String): ByteArray? {
|
||||||
|
val out = ArrayList<Byte>(text.length + 1)
|
||||||
|
var i = 0
|
||||||
|
while (i < text.length) {
|
||||||
|
val c = text[i]
|
||||||
|
when {
|
||||||
|
c == '\\' && i + 1 < text.length -> {
|
||||||
|
val esc = escapes["\\" + text[i + 1]] ?: return null
|
||||||
|
out.addAll(esc.toList())
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
c == '{' -> {
|
||||||
|
val end = text.indexOf('}', i)
|
||||||
|
if (end < 0) return null
|
||||||
|
val bytes = placeholders[text.substring(i + 1, end)] ?: return null
|
||||||
|
out.addAll(bytes.toList())
|
||||||
|
i = end + 1
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val bytes = chars[c] ?: return null
|
||||||
|
out.addAll(bytes.toList())
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// key = value where key may itself be the '=' char, value is one or more hex bytes, and an
|
||||||
|
// optional trailing "@ comment" follows whitespace.
|
||||||
|
private val entry = Regex("^(.*?)=\\s*([0-9A-Fa-f]{2}(?:\\s+[0-9A-Fa-f]{2})*)\\s*(?:@.*)?$")
|
||||||
|
|
||||||
|
fun load(charmapFile: File): Charmap {
|
||||||
|
val chars = HashMap<Char, ByteArray>()
|
||||||
|
val escapes = HashMap<String, ByteArray>()
|
||||||
|
val placeholders = HashMap<String, ByteArray>()
|
||||||
|
for (raw in charmapFile.readLines()) {
|
||||||
|
val line = raw.replace(Regex("\\s+@.*$"), "").trimEnd()
|
||||||
|
val m = entry.matchEntire(line) ?: continue
|
||||||
|
val key = m.groupValues[1].trim()
|
||||||
|
val bytes =
|
||||||
|
m.groupValues[2].split(Regex("\\s+")).map { it.toInt(16).toByte() }.toByteArray()
|
||||||
|
if (key.isEmpty() || bytes.isEmpty()) continue
|
||||||
|
if (key.length >= 2 && key.first() == '\'' && key.last() == '\'') {
|
||||||
|
when (val inner = key.substring(1, key.length - 1)) {
|
||||||
|
"\\n",
|
||||||
|
"\\l",
|
||||||
|
"\\p",
|
||||||
|
"\\r" -> escapes[inner] = bytes
|
||||||
|
"\\'" -> chars['\''] = bytes
|
||||||
|
else -> if (inner.length == 1) chars[inner[0]] = bytes
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
placeholders[key] = bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Charmap(chars, escapes, placeholders)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One dialog line resolved from the decomp: its full label, packed textId, and readable preview.
|
||||||
|
*/
|
||||||
|
data class DialogLine(val label: String, val textId: Int, val preview: String)
|
||||||
|
|
||||||
|
/** A single enum entry inside a location's dialog enum. */
|
||||||
|
data class DialogEntry(val name: String, val textId: Int, val preview: String)
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
import gg.jte.ContentType
|
||||||
|
import gg.jte.TemplateEngine
|
||||||
|
import gg.jte.output.FileOutput
|
||||||
|
import gg.jte.resolve.DirectoryCodeResolver
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/** Emits one Kotlin file per location, each holding an enum of that location's dialog lines. */
|
||||||
|
class DialogRenderer(
|
||||||
|
region: String,
|
||||||
|
private val templatesDir: File,
|
||||||
|
private val outputDir: File,
|
||||||
|
private val classCacheDir: File,
|
||||||
|
) {
|
||||||
|
private val basePackage = "de.fiereu.openmmo.dialog.generated.$region"
|
||||||
|
|
||||||
|
fun render(lines: List<DialogLine>) {
|
||||||
|
classCacheDir.mkdirs()
|
||||||
|
val engine =
|
||||||
|
TemplateEngine.create(
|
||||||
|
DirectoryCodeResolver(templatesDir.toPath()),
|
||||||
|
classCacheDir.toPath(),
|
||||||
|
ContentType.Plain,
|
||||||
|
)
|
||||||
|
|
||||||
|
val packageRoot = File(outputDir, basePackage.replace('.', '/'))
|
||||||
|
if (packageRoot.exists()) packageRoot.deleteRecursively()
|
||||||
|
packageRoot.mkdirs()
|
||||||
|
|
||||||
|
lines
|
||||||
|
.groupBy { RenderUtil.location(it.label) }
|
||||||
|
.forEach { (location, group) ->
|
||||||
|
val className = RenderUtil.className(location)
|
||||||
|
val entries =
|
||||||
|
group
|
||||||
|
.map { DialogEntry(RenderUtil.entryName(it.label), it.textId, it.preview) }
|
||||||
|
.distinctBy { it.name }
|
||||||
|
.sortedBy { it.textId }
|
||||||
|
FileOutput(File(packageRoot, "${className.trim('`')}.kt").toPath()).use { out ->
|
||||||
|
engine.render(
|
||||||
|
"LocationDialog.jte",
|
||||||
|
mapOf("pkg" to basePackage, "enumName" to className, "entries" to entries),
|
||||||
|
out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
@file:JvmName("Main")
|
||||||
|
|
||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
// GBA dialog ids carry the retail ROM file offset in the low bits with the mode-1 resolver on top.
|
||||||
|
private const val GBA_MODE = 0x10000000
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
require(args.size >= 5) {
|
||||||
|
"Usage: <output-dir> <templates-dir> <class-cache-dir> <roms-dir> <region|gameCode|decomp>... got ${args.toList()}"
|
||||||
|
}
|
||||||
|
val outputDir = File(args[0])
|
||||||
|
val templatesDir = File(args[1])
|
||||||
|
val classCacheDir = File(args[2])
|
||||||
|
val romsDir = File(args[3])
|
||||||
|
|
||||||
|
for (spec in args.drop(4)) {
|
||||||
|
val (region, gameCode, decomp) = spec.split("|")
|
||||||
|
generateRegion(region, gameCode, File(decomp), romsDir, outputDir, templatesDir, classCacheDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generateRegion(
|
||||||
|
region: String,
|
||||||
|
gameCode: String,
|
||||||
|
decompDir: File,
|
||||||
|
romsDir: File,
|
||||||
|
outputDir: File,
|
||||||
|
templatesDir: File,
|
||||||
|
classCacheDir: File,
|
||||||
|
) {
|
||||||
|
val renderer = DialogRenderer(region, templatesDir, outputDir, classCacheDir)
|
||||||
|
val rom = RomIndex.find(romsDir, gameCode)
|
||||||
|
if (rom == null) {
|
||||||
|
println("[dialog] no $gameCode ROM found in $romsDir, generating empty $region")
|
||||||
|
renderer.render(emptyList())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val charmap = Charmap.load(File(decompDir, "charmap.txt"))
|
||||||
|
val texts = TextParser(decompDir).parseAll()
|
||||||
|
println("[dialog] $region: parsed ${texts.size} text labels from $decompDir")
|
||||||
|
|
||||||
|
var unencodable = 0
|
||||||
|
var notFound = 0
|
||||||
|
val lines =
|
||||||
|
texts.mapNotNull { t ->
|
||||||
|
val bytes = charmap.encode(t.content)
|
||||||
|
if (bytes == null) {
|
||||||
|
unencodable++
|
||||||
|
return@mapNotNull null
|
||||||
|
}
|
||||||
|
val offset = rom.offsetOf(bytes)
|
||||||
|
if (offset < 0) {
|
||||||
|
notFound++
|
||||||
|
return@mapNotNull null
|
||||||
|
}
|
||||||
|
DialogLine(t.label, GBA_MODE or offset, RenderUtil.preview(t.content))
|
||||||
|
}
|
||||||
|
|
||||||
|
println(
|
||||||
|
"[dialog] $region: resolved ${lines.size} lines (skipped $unencodable unencodable, $notFound not in ROM)")
|
||||||
|
renderer.render(lines)
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
object RenderUtil {
|
||||||
|
|
||||||
|
private val KEYWORDS =
|
||||||
|
setOf(
|
||||||
|
"as",
|
||||||
|
"break",
|
||||||
|
"class",
|
||||||
|
"continue",
|
||||||
|
"do",
|
||||||
|
"else",
|
||||||
|
"false",
|
||||||
|
"for",
|
||||||
|
"fun",
|
||||||
|
"if",
|
||||||
|
"in",
|
||||||
|
"interface",
|
||||||
|
"is",
|
||||||
|
"null",
|
||||||
|
"object",
|
||||||
|
"package",
|
||||||
|
"return",
|
||||||
|
"super",
|
||||||
|
"this",
|
||||||
|
"throw",
|
||||||
|
"true",
|
||||||
|
"try",
|
||||||
|
"typealias",
|
||||||
|
"typeof",
|
||||||
|
"val",
|
||||||
|
"var",
|
||||||
|
"when",
|
||||||
|
"while")
|
||||||
|
|
||||||
|
/** Location the label belongs to: everything before `_Text_`, or "Misc" when it has none. */
|
||||||
|
fun location(label: String): String =
|
||||||
|
if ("_Text_" in label) label.substringBefore("_Text_") else "Misc"
|
||||||
|
|
||||||
|
/** The enum entry name: the part after `_Text_`, or the whole label, made a valid identifier. */
|
||||||
|
fun entryName(label: String): String =
|
||||||
|
ident(if ("_Text_" in label) label.substringAfter("_Text_") else label)
|
||||||
|
|
||||||
|
fun className(location: String): String = ident(location)
|
||||||
|
|
||||||
|
private fun ident(s: String): String {
|
||||||
|
val safe = if (s.isNotEmpty() && s[0].isDigit()) "_$s" else s
|
||||||
|
return if (safe in KEYWORDS) "`$safe`" else safe
|
||||||
|
}
|
||||||
|
|
||||||
|
fun entry(e: DialogEntry): String = "/** ${e.preview} */ ${e.name}(0x${"%08X".format(e.textId)}),"
|
||||||
|
|
||||||
|
/** Turns raw `.string` content into a short single-line preview safe inside a KDoc comment. */
|
||||||
|
fun preview(content: String): String {
|
||||||
|
val text =
|
||||||
|
content
|
||||||
|
.replace("\\n", " ")
|
||||||
|
.replace("\\l", " ")
|
||||||
|
.replace("\\p", " ")
|
||||||
|
.replace("$", "")
|
||||||
|
.replace(Regex("\\s+"), " ")
|
||||||
|
.replace("*/", "* /")
|
||||||
|
.trim()
|
||||||
|
return if (text.length > 70) text.take(70) + "…" else text
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A retail GBA ROM located by its header game code, used to resolve a text's file offset. The ROM
|
||||||
|
* is byte-identical to the pret decomp, so the offset of an encoded string is the value PokeMMO
|
||||||
|
* packs into a dialog textId.
|
||||||
|
*/
|
||||||
|
class RomIndex private constructor(private val latin1: String) {
|
||||||
|
|
||||||
|
/** The first offset of [bytes] in the ROM, or -1 if absent. */
|
||||||
|
fun offsetOf(bytes: ByteArray): Int = latin1.indexOf(String(bytes, Charsets.ISO_8859_1))
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val GAME_CODE_OFFSET = 0xAC
|
||||||
|
|
||||||
|
/** Finds the ROM in [romsDir] whose header game code equals [gameCode] (e.g. "BPEE"). */
|
||||||
|
fun find(romsDir: File, gameCode: String): RomIndex? {
|
||||||
|
val rom =
|
||||||
|
romsDir
|
||||||
|
.listFiles { f -> f.isFile }
|
||||||
|
?.firstOrNull { f ->
|
||||||
|
f.length() > GAME_CODE_OFFSET + 4 && readGameCode(f) == gameCode
|
||||||
|
} ?: return null
|
||||||
|
return RomIndex(String(rom.readBytes(), Charsets.ISO_8859_1))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun readGameCode(file: File): String =
|
||||||
|
file.inputStream().use { s ->
|
||||||
|
s.skip(GAME_CODE_OFFSET.toLong())
|
||||||
|
val b = ByteArray(4)
|
||||||
|
if (s.read(b) != 4) "" else String(b, Charsets.US_ASCII)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package de.fiereu.openmmo.codegen.dialog
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/** A `.string` text label found in the decomp: its label and the concatenated literal content. */
|
||||||
|
data class DecompText(val label: String, val content: String)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collects every `.string` text label from the overworld data (map and shared scripts). A label is
|
||||||
|
* a text label when it is directly followed by one or more `.string` lines.
|
||||||
|
*/
|
||||||
|
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"))
|
||||||
|
val out = LinkedHashMap<String, String>()
|
||||||
|
for (root in roots) {
|
||||||
|
if (!root.isDirectory) continue
|
||||||
|
root
|
||||||
|
.walkTopDown()
|
||||||
|
.filter { it.isFile && it.extension == "inc" }
|
||||||
|
.forEach { file -> parseFile(file, out) }
|
||||||
|
}
|
||||||
|
return out.map { DecompText(it.key, it.value) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseFile(file: File, out: MutableMap<String, String>) {
|
||||||
|
var pending: String? = null
|
||||||
|
var active: String? = null
|
||||||
|
val buffer = StringBuilder()
|
||||||
|
fun flush() {
|
||||||
|
val label = active
|
||||||
|
if (label != null && label !in out) out[label] = buffer.toString()
|
||||||
|
active = null
|
||||||
|
buffer.setLength(0)
|
||||||
|
}
|
||||||
|
for (raw in file.readLines()) {
|
||||||
|
val line = raw.trim()
|
||||||
|
val m = labelLine.matchEntire(line)
|
||||||
|
when {
|
||||||
|
m != null -> {
|
||||||
|
flush()
|
||||||
|
pending = m.groupValues[1]
|
||||||
|
}
|
||||||
|
line.startsWith(".string") -> {
|
||||||
|
val label = active ?: pending ?: continue
|
||||||
|
if (active == null) {
|
||||||
|
active = label
|
||||||
|
buffer.setLength(0)
|
||||||
|
}
|
||||||
|
buffer.append(stringLiteral(line))
|
||||||
|
}
|
||||||
|
line.isEmpty() -> {
|
||||||
|
flush()
|
||||||
|
pending = null
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
flush()
|
||||||
|
pending = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stringLiteral(line: String): String {
|
||||||
|
val first = line.indexOf('"')
|
||||||
|
val last = line.lastIndexOf('"')
|
||||||
|
return if (first in 0 until last) line.substring(first + 1, last) else ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Retail ROMs live here for codegen (identified by GBA header game code + version, not filename).
|
||||||
|
# They are copyrighted and must never be committed. This .gitignore keeps the folder tracked
|
||||||
|
# while ignoring everything else in it.
|
||||||
|
*
|
||||||
|
!.gitignore
|
||||||
+3
-78
@@ -1,13 +1,10 @@
|
|||||||
package de.fiereu.openmmo.server.game.services
|
package de.fiereu.openmmo.server.game.services
|
||||||
|
|
||||||
import de.fiereu.network.PacketEvent
|
import de.fiereu.network.PacketEvent
|
||||||
import de.fiereu.network.SessionContext
|
|
||||||
import de.fiereu.openmmo.net.game.packets.DialogChoicePacket
|
import de.fiereu.openmmo.net.game.packets.DialogChoicePacket
|
||||||
import de.fiereu.openmmo.net.game.packets.DialogStatePacket
|
import de.fiereu.openmmo.net.game.packets.DialogStatePacket
|
||||||
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionPacket
|
|
||||||
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionResponsePacket
|
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionResponsePacket
|
||||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||||
import de.fiereu.openmmo.server.game.session.ScriptPage
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@@ -17,85 +14,13 @@ private val log = KotlinLogging.logger {}
|
|||||||
@Singleton
|
@Singleton
|
||||||
class DialogService @Inject constructor() {
|
class DialogService @Inject constructor() {
|
||||||
|
|
||||||
fun scriptParams(script: String): List<ScriptPage>? {
|
|
||||||
if (script == "0x0") return null
|
|
||||||
val known =
|
|
||||||
mapOf<String, List<ScriptPage>>(
|
|
||||||
"LittlerootTown_ProfessorBirchsLab_EventScript_Birch" to
|
|
||||||
listOf(ScriptPage(type = 0x04, unk1 = 0xAA74, unk2 = 0x1F10, unk3 = 0x0708)),
|
|
||||||
"LittlerootTown_ProfessorBirchsLab_EventScript_Aide" to
|
|
||||||
listOf(ScriptPage(type = 0x04, unk1 = 0xA6CE, unk2 = 0x1F10, unk3 = 0x04B0)),
|
|
||||||
"PlayersHouse_1F_EventScript_Mom" to
|
|
||||||
listOf(
|
|
||||||
ScriptPage(type = 0x04, unk1 = 0x087D, unk2 = 0x1F10, unk3 = 0x04B0),
|
|
||||||
ScriptPage(type = 0x04, unk1 = 0x7D5C, unk2 = 0x1F10, unk3 = 0x02BC),
|
|
||||||
),
|
|
||||||
"RivalsHouse_1F_EventScript_RivalMom" to
|
|
||||||
listOf(ScriptPage(type = 0x04, unk1 = 0x8CE3, unk2 = 0x1E10, unk3 = 0x02BC)),
|
|
||||||
"RivalsHouse_1F_EventScript_RivalSibling" to
|
|
||||||
listOf(ScriptPage(type = 0x04, unk1 = 0x8B25, unk2 = 0x1E10, unk3 = 0x0708)),
|
|
||||||
"LittlerootTown_EventScript_Twin" to
|
|
||||||
listOf(ScriptPage(type = 0x04, unk1 = 0x6292, unk2 = 0x1F10, unk3 = 0x04B0)),
|
|
||||||
"LittlerootTown_EventScript_Boy" to
|
|
||||||
listOf(ScriptPage(type = 0x04, unk1 = 0x938D, unk2 = 0x1F10, unk3 = 0x04B0)),
|
|
||||||
)
|
|
||||||
return known[script]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sendInteractive(
|
|
||||||
ctx: SessionContext,
|
|
||||||
id: Int,
|
|
||||||
entityId: Long,
|
|
||||||
type: Int = 0x04,
|
|
||||||
unk1: Int = 0xAA74,
|
|
||||||
unk2: Int = 0x1F10,
|
|
||||||
unk3: Int = 0x0708,
|
|
||||||
) {
|
|
||||||
ctx.send(
|
|
||||||
DialogActionPacket(
|
|
||||||
flags = id.toByte(),
|
|
||||||
actionType = type.toByte(),
|
|
||||||
// The client reads the text id as one little endian int whose two halves the scripts
|
|
||||||
// carry as unk1 (low) and unk2 (high).
|
|
||||||
textId = unk1 or (unk2 shl 16),
|
|
||||||
entityId = entityId,
|
|
||||||
contextValue = unk3,
|
|
||||||
messageArgs = emptyList(),
|
|
||||||
detail = ByteArray(0),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onInteractive(event: PacketEvent<DialogActionResponsePacket>) {
|
fun onInteractive(event: PacketEvent<DialogActionResponsePacket>) {
|
||||||
val ctx = event.session
|
val ctx = event.session
|
||||||
val state = ctx.attributes[PLAYER_STATE] ?: return
|
val state = ctx.attributes[PLAYER_STATE] ?: return
|
||||||
val charId = state.characterId ?: return
|
|
||||||
|
|
||||||
if (state.inDialog) {
|
if (state.inDialog) {
|
||||||
val respId = event.packet.id
|
ctx.send(DialogStatePacket(false))
|
||||||
log.info {
|
state.inDialog = false
|
||||||
"Interactive response: id=$respId for char $charId (page ${state.dialogPageIndex + 1}/${state.dialogPages.size})"
|
state.dialogNpcEntityId = 0
|
||||||
}
|
|
||||||
val nextPageIndex = state.dialogPageIndex + 1
|
|
||||||
if (nextPageIndex < state.dialogPages.size) {
|
|
||||||
val seqId = state.dialogSeqId++
|
|
||||||
val page = state.dialogPages[nextPageIndex]
|
|
||||||
state.dialogPageIndex = nextPageIndex
|
|
||||||
sendInteractive(
|
|
||||||
ctx,
|
|
||||||
seqId,
|
|
||||||
state.dialogNpcEntityId,
|
|
||||||
page.type,
|
|
||||||
page.unk1,
|
|
||||||
page.unk2,
|
|
||||||
page.unk3,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
ctx.send(DialogStatePacket(false))
|
|
||||||
state.inDialog = false
|
|
||||||
state.dialogNpcEntityId = 0
|
|
||||||
state.dialogPages = emptyList()
|
|
||||||
state.dialogPageIndex = 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-45
@@ -3,7 +3,6 @@ package de.fiereu.openmmo.server.game.services
|
|||||||
import de.fiereu.network.PacketEvent
|
import de.fiereu.network.PacketEvent
|
||||||
import de.fiereu.openmmo.common.enums.Direction
|
import de.fiereu.openmmo.common.enums.Direction
|
||||||
import de.fiereu.openmmo.maps.MapManager
|
import de.fiereu.openmmo.maps.MapManager
|
||||||
import de.fiereu.openmmo.net.game.packets.DialogStatePacket
|
|
||||||
import de.fiereu.openmmo.net.game.packets.EntityInteractPacket
|
import de.fiereu.openmmo.net.game.packets.EntityInteractPacket
|
||||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||||
@@ -18,7 +17,6 @@ class InteractionService
|
|||||||
@Inject
|
@Inject
|
||||||
constructor(
|
constructor(
|
||||||
private val npcService: NpcService,
|
private val npcService: NpcService,
|
||||||
private val dialogService: DialogService,
|
|
||||||
private val mapManager: MapManager,
|
private val mapManager: MapManager,
|
||||||
private val characterStore: CharacterStore,
|
private val characterStore: CharacterStore,
|
||||||
) {
|
) {
|
||||||
@@ -40,33 +38,10 @@ constructor(
|
|||||||
val bankId = stored.info.positionBankId.toInt()
|
val bankId = stored.info.positionBankId.toInt()
|
||||||
val mapId = stored.info.positionMapId.toInt()
|
val mapId = stored.info.positionMapId.toInt()
|
||||||
for (npc in currentMap.npcs) {
|
for (npc in currentMap.npcs) {
|
||||||
val expectedId = npcService.getNpcEntityId(bankId, mapId, npc.entityIdx)
|
if (npcService.getNpcEntityId(bankId, mapId, npc.entityIdx) == npcEntityId) {
|
||||||
if (expectedId == npcEntityId) {
|
|
||||||
log.info {
|
log.info {
|
||||||
"Entity interaction: NPC entityIdx=${npc.entityIdx} entityId=$npcEntityId script=${npc.script}"
|
"Entity interaction: NPC entityIdx=${npc.entityIdx} entityId=$npcEntityId script=${npc.script}"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (npc.script != "0x0") {
|
|
||||||
val params = dialogService.scriptParams(npc.script)
|
|
||||||
if (params != null) {
|
|
||||||
state.inDialog = true
|
|
||||||
state.dialogNpcEntityId = npcEntityId
|
|
||||||
state.dialogPages = params
|
|
||||||
state.dialogPageIndex = 0
|
|
||||||
val seqId = state.dialogSeqId++
|
|
||||||
val page = params[0]
|
|
||||||
ctx.send(DialogStatePacket(true))
|
|
||||||
dialogService.sendInteractive(
|
|
||||||
ctx,
|
|
||||||
seqId,
|
|
||||||
npcEntityId,
|
|
||||||
page.type,
|
|
||||||
page.unk1,
|
|
||||||
page.unk2,
|
|
||||||
page.unk3,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,25 +68,7 @@ constructor(
|
|||||||
val bgEvent =
|
val bgEvent =
|
||||||
currentMap.bgEvents.find { it.x == facingX && it.y == facingY && facingDirOk(it.facingDir) }
|
currentMap.bgEvents.find { it.x == facingX && it.y == facingY && facingDirOk(it.facingDir) }
|
||||||
if (bgEvent != null) {
|
if (bgEvent != null) {
|
||||||
val params = dialogService.scriptParams(bgEvent.script)
|
log.info { "Entity interaction: bg event at ($facingX, $facingY) script=${bgEvent.script}" }
|
||||||
if (params != null) {
|
|
||||||
state.inDialog = true
|
|
||||||
state.dialogNpcEntityId = npcEntityId
|
|
||||||
state.dialogPages = params
|
|
||||||
state.dialogPageIndex = 0
|
|
||||||
val seqId = state.dialogSeqId++
|
|
||||||
val page = params[0]
|
|
||||||
ctx.send(DialogStatePacket(true))
|
|
||||||
dialogService.sendInteractive(
|
|
||||||
ctx,
|
|
||||||
seqId,
|
|
||||||
npcEntityId,
|
|
||||||
page.type,
|
|
||||||
page.unk1,
|
|
||||||
page.unk2,
|
|
||||||
page.unk3,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,6 @@ package de.fiereu.openmmo.server.game.session
|
|||||||
|
|
||||||
import de.fiereu.openmmo.common.enums.Direction
|
import de.fiereu.openmmo.common.enums.Direction
|
||||||
|
|
||||||
data class ScriptPage(
|
|
||||||
val type: Int = 0x04,
|
|
||||||
val unk1: Int,
|
|
||||||
val unk2: Int,
|
|
||||||
val unk3: Int,
|
|
||||||
)
|
|
||||||
|
|
||||||
data class PlayerState(
|
data class PlayerState(
|
||||||
val userId: Int,
|
val userId: Int,
|
||||||
var characterId: Long? = null,
|
var characterId: Long? = null,
|
||||||
@@ -17,8 +10,6 @@ data class PlayerState(
|
|||||||
var inDialog: Boolean = false,
|
var inDialog: Boolean = false,
|
||||||
var dialogNpcEntityId: Long = 0,
|
var dialogNpcEntityId: Long = 0,
|
||||||
var dialogSeqId: Int = 0,
|
var dialogSeqId: Int = 0,
|
||||||
var dialogPages: List<ScriptPage> = emptyList(),
|
|
||||||
var dialogPageIndex: Int = 0,
|
|
||||||
var regionId: Int = 1,
|
var regionId: Int = 1,
|
||||||
var bankId: Int = 51,
|
var bankId: Int = 51,
|
||||||
var mapId: Int = 3,
|
var mapId: Int = 3,
|
||||||
|
|||||||
Reference in new issue
Block a user