From 9cda538a6858da82d0bf45e8f44ee4a5677a39e7 Mon Sep 17 00:00:00 2001 From: Fiereu Date: Sun, 26 Jul 2026 14:34:50 +0200 Subject: [PATCH] feat(game): littleroot town intro with the truck and mom cutscene --- codegen/src/generator/jte/maps/MapDef.jte | 2 + .../openmmo/codegen/maps/ParsedModel.kt | 11 +++ .../openmmo/codegen/maps/PretGbaParser.kt | 61 +++++++++++++-- .../fiereu/openmmo/codegen/maps/RenderUtil.kt | 14 +++- .../kotlin/de/fiereu/openmmo/maps/MapDef.kt | 9 +++ .../kotlin/de/fiereu/openmmo/maps/NpcDef.kt | 2 + .../kotlin/de/fiereu/openmmo/maps/WarpTile.kt | 8 ++ .../de/fiereu/openmmo/common/CharacterInfo.kt | 4 +- .../de/fiereu/openmmo/common/DynamicWarp.kt | 16 ++++ .../server/game/script/MovementStep.kt | 25 +++--- .../server/game/script/ScriptContext.kt | 12 +++ .../server/game/script/ScriptRunner.kt | 17 ++++- .../script/generated/hoenn/LittlerootTown.kt | 32 ++++++++ .../server/game/services/MapScriptService.kt | 27 +++++-- .../server/game/services/MovementService.kt | 27 ++++++- .../server/game/services/NpcService.kt | 76 ++++++++++++------- .../game/services/ScriptMovementService.kt | 49 +++++++----- .../server/game/services/WarpService.kt | 3 +- .../game/storage/CharacterRepository.kt | 26 +++++++ .../server/game/storage/CharacterStore.kt | 33 ++++++-- .../db/migration/V3__add_dynamic_warp.sql | 10 +++ .../services/ScriptMovementServiceTest.kt | 14 ++-- .../server/game/services/StoryServiceTest.kt | 3 +- 23 files changed, 389 insertions(+), 92 deletions(-) create mode 100644 common/src/main/kotlin/de/fiereu/openmmo/common/DynamicWarp.kt create mode 100644 server.game/src/main/resources/db/migration/V3__add_dynamic_warp.sql diff --git a/codegen/src/generator/jte/maps/MapDef.jte b/codegen/src/generator/jte/maps/MapDef.jte index 69976f8..a9bd35a 100644 --- a/codegen/src/generator/jte/maps/MapDef.jte +++ b/codegen/src/generator/jte/maps/MapDef.jte @@ -16,6 +16,7 @@ import de.fiereu.openmmo.common.enums.MovementType import de.fiereu.openmmo.common.enums.Weather import de.fiereu.openmmo.maps.BgEventDef import de.fiereu.openmmo.maps.MapDef +import de.fiereu.openmmo.maps.MapFrameScript import de.fiereu.openmmo.maps.NpcDef import de.fiereu.openmmo.maps.WarpTile import de.fiereu.openmmo.maps.WildEncounterSlot @@ -46,4 +47,5 @@ internal val ${objectName}: MapDef = npcs = ${RenderUtil.INSTANCE.npcs(map.getVisibleNpcs())}, bgEvents = ${RenderUtil.INSTANCE.bgEvents(map.getBgEvents())}, onTransitionScript = "${map.getOnTransitionScript()}", + onFrameScripts = ${RenderUtil.INSTANCE.frameScripts(map.getOnFrameScripts())}, ) diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/ParsedModel.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/ParsedModel.kt index 1ea9ac7..193c5b8 100644 --- a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/ParsedModel.kt +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/ParsedModel.kt @@ -28,6 +28,14 @@ data class ParsedMap( val bgEvents: List, // Decomp label of the map's ON_TRANSITION script, or "" when the map has none. val onTransitionScript: String, + // The map's ON_FRAME table: run the script once its var equals the value, on map entry. + val onFrameScripts: List, +) + +data class ParsedFrameScript( + val varKey: String, + val value: Int, + val script: String, ) data class ParsedConnection( @@ -47,6 +55,7 @@ data class ParsedWarp( val targetX: Int, val targetY: Int, val targetElevation: Int, + val dynamic: Boolean = false, ) data class ParsedNpc( @@ -61,6 +70,8 @@ data class ParsedNpc( val trainerType: Int, val facing: String, val script: String, + // Namespaced story flag that hides this npc by default, or "" when it is always shown. + val hideFlag: String, ) data class ParsedBgEvent( diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/PretGbaParser.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/PretGbaParser.kt index 4ba31b0..ee2d2ba 100644 --- a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/PretGbaParser.kt +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/PretGbaParser.kt @@ -193,6 +193,7 @@ class PretGbaParser( visibleNpcs = parseNpcs(mapJson, mapDirName, ctx), bgEvents = parseBgEvents(mapJson), onTransitionScript = parseOnTransitionScript(mapDirName), + onFrameScripts = parseOnFrameScripts(mapDirName), ) } @@ -225,9 +226,25 @@ class PretGbaParser( mapJson["warp_events"]?.jsonArrayOrNull()?.mapNotNull { warp -> val obj = warp.jsonObject val destName = obj["dest_map"]?.jsonPrimitive?.contentOrNull ?: return@mapNotNull null - val destAddr = ctx.addresses[destName] ?: return@mapNotNull null val x = obj["x"]?.jsonPrimitive?.intOrNull ?: 0 val y = obj["y"]?.jsonPrimitive?.intOrNull ?: 0 + // A MAP_DYNAMIC warp has no static destination, the runtime resolves it from the player's + // dynamic warp (setdynamicwarp). Emit the tile so it still triggers, with dynamic = true. + if (destName == "MAP_DYNAMIC") { + return@mapNotNull ParsedWarp( + x = x, + y = y, + elevation = ((obj["elevation"]?.jsonPrimitive?.intOrNull ?: 0) - 1).coerceAtLeast(0), + targetRegion = 0, + targetBank = 0, + targetMap = 0, + targetX = 0, + targetY = 0, + targetElevation = 0, + dynamic = true, + ) + } + val destAddr = ctx.addresses[destName] ?: return@mapNotNull null val srcElevation = (obj["elevation"]?.jsonPrimitive?.intOrNull ?: 0) val destWarpId = obj["dest_warp_id"]?.jsonPrimitive?.let { it.intOrNull ?: it.content.toIntOrNull() } @@ -249,15 +266,14 @@ class PretGbaParser( ) } ?: emptyList() + // Every object event is emitted. A npc hidden by a story flag keeps the flag in hideFlag so the + // server can skip it at spawn and a script can show it later, the always-visible ones get "". private fun parseNpcs(mapJson: JsonObject, mapDirName: String, ctx: Context): List { val visibleOverride = region.defaultVisibleNpcs[mapDirName] - return mapJson["object_events"]?.jsonArrayOrNull().orEmpty().mapIndexedNotNull { idx, npcElement - -> + return mapJson["object_events"]?.jsonArrayOrNull().orEmpty().mapIndexed { idx, npcElement -> val npc = npcElement.jsonObject - val isVisible = - if (visibleOverride != null) idx in visibleOverride - else npc["flag"]?.jsonPrimitive?.contentOrNull == "0" - if (!isVisible) return@mapIndexedNotNull null + val flag = npc["flag"]?.jsonPrimitive?.contentOrNull ?: "0" + val shownByDefault = if (visibleOverride != null) idx in visibleOverride else flag == "0" val gfxName = npc["graphics_id"]?.jsonPrimitive?.contentOrNull ?: "OBJ_EVENT_GFX_BOY_1" val movementName = npc["movement_type"]?.jsonPrimitive?.contentOrNull ?: "MOVEMENT_TYPE_NONE" ParsedNpc( @@ -274,10 +290,41 @@ class PretGbaParser( else 1, facing = movementTypes.facingRef(movementName), script = npc["script"]?.jsonPrimitive?.contentOrNull ?: "0x0", + hideFlag = if (shownByDefault) "" else "${region.name}/$flag", ) } } + // Parses the map's ON_FRAME table: each map_script_2 runs its script once the var equals the + // value + // when the player enters the map. Values that are not plain integers (rare) are skipped. + private fun parseOnFrameScripts(mapDirName: String): List { + val file = File(rootDir, "data/maps/$mapDirName/scripts.inc") + if (!file.exists()) return emptyList() + val text = file.readText() + val tableLabel = + Regex("""map_script\s+MAP_SCRIPT_ON_FRAME_TABLE\s*,\s*(\w+)""") + .find(text) + ?.groupValues + ?.get(1) ?: return emptyList() + val block = + Regex("""(?m)^$tableLabel:\s*\n(.*?)(?=\n\s*\.2byte)""", RegexOption.DOT_MATCHES_ALL) + .find(text) + ?.groupValues + ?.get(1) ?: return emptyList() + val entry = Regex("""map_script_2\s+(\w+)\s*,\s*(\d+)\s*,\s*(\w+)""") + return entry + .findAll(block) + .map { + ParsedFrameScript( + varKey = "${region.name}/${it.groupValues[1]}", + value = it.groupValues[2].toInt(), + script = it.groupValues[3], + ) + } + .toList() + } + private fun parseBgEvents(mapJson: JsonObject): List = mapJson["bg_events"]?.jsonArrayOrNull()?.map { be -> val obj = be.jsonObject diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/RenderUtil.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/RenderUtil.kt index e41a431..5558d21 100644 --- a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/RenderUtil.kt +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/RenderUtil.kt @@ -31,7 +31,8 @@ object RenderUtil { "WarpTile(x = ${it.x}, y = ${it.y}, elevation = ${it.elevation}," + " targetRegionId = ${it.targetRegion}, targetBankId = ${it.targetBank}," + " targetMapId = ${it.targetMap}," + - " targetX = ${it.targetX}, targetY = ${it.targetY}, targetElevation = ${it.targetElevation})" + " targetX = ${it.targetX}, targetY = ${it.targetY}, targetElevation = ${it.targetElevation}," + + " dynamic = ${it.dynamic})" } } @@ -42,7 +43,16 @@ object RenderUtil { " x = ${it.x}, y = ${it.y}, elevation = ${it.elevation}," + " movementType = ${it.movementType}, movementRangeX = ${it.movementRangeX}," + " movementRangeY = ${it.movementRangeY}, trainerType = ${it.trainerType}," + - " facing = ${it.facing}, script = ${escapeString(it.script)})" + " facing = ${it.facing}, script = ${escapeString(it.script)}," + + " hideFlag = ${escapeString(it.hideFlag)})" + } + } + + fun frameScripts(scripts: List): String { + if (scripts.isEmpty()) return EMPTY_LIST + return scripts.joinToString(LIST_SEP, LIST_OPEN, LIST_CLOSE) { + "MapFrameScript(varKey = ${escapeString(it.varKey)}, value = ${it.value}," + + " script = ${escapeString(it.script)})" } } diff --git a/codegen/src/main/kotlin/de/fiereu/openmmo/maps/MapDef.kt b/codegen/src/main/kotlin/de/fiereu/openmmo/maps/MapDef.kt index 57dbdc1..67c05ed 100644 --- a/codegen/src/main/kotlin/de/fiereu/openmmo/maps/MapDef.kt +++ b/codegen/src/main/kotlin/de/fiereu/openmmo/maps/MapDef.kt @@ -34,6 +34,8 @@ class MapDef( val bgEvents: List = emptyList(), /** Decomp label of the script that runs when a player enters this map, or "" if none. */ val onTransitionScript: String = "", + /** Conditional entry scripts: run [MapFrameScript.script] when its var equals its value. */ + val onFrameScripts: List = emptyList(), private val blockData: String = "", private val behaviorData: String = "", ) { @@ -48,6 +50,13 @@ class MapDef( wildEncounters.firstOrNull { it.method == method } } +/** One ON_FRAME entry: run [script] when the story var [varKey] currently equals [value]. */ +data class MapFrameScript( + val varKey: String, + val value: Int, + val script: String, +) + private fun decodeBlockData(blockData: String, behaviorData: String): List { if (blockData.isEmpty()) return emptyList() val bytes = Base64.getDecoder().decode(blockData) diff --git a/codegen/src/main/kotlin/de/fiereu/openmmo/maps/NpcDef.kt b/codegen/src/main/kotlin/de/fiereu/openmmo/maps/NpcDef.kt index 40e5c00..84aec8f 100644 --- a/codegen/src/main/kotlin/de/fiereu/openmmo/maps/NpcDef.kt +++ b/codegen/src/main/kotlin/de/fiereu/openmmo/maps/NpcDef.kt @@ -15,4 +15,6 @@ data class NpcDef( val trainerType: Int, val facing: Direction, val script: String = "0x0", + /** Story flag that hides this npc by default, or "" when it is always shown. */ + val hideFlag: String = "", ) diff --git a/codegen/src/main/kotlin/de/fiereu/openmmo/maps/WarpTile.kt b/codegen/src/main/kotlin/de/fiereu/openmmo/maps/WarpTile.kt index 0204ad1..d244ed3 100644 --- a/codegen/src/main/kotlin/de/fiereu/openmmo/maps/WarpTile.kt +++ b/codegen/src/main/kotlin/de/fiereu/openmmo/maps/WarpTile.kt @@ -13,4 +13,12 @@ data class WarpTile( val targetY: Int, val targetElevation: Int = 0, val facingDirection: Direction? = null, + /** Direction the player faces after this warp, or null to infer it. */ + val exitFacing: Direction? = null, + /** + * A MAP_DYNAMIC warp whose destination is not baked in but set at runtime (the decomp + * setdynamicwarp). The target fields are placeholders, the real destination comes from the + * player's stored dynamic warp when they step on it. + */ + val dynamic: Boolean = false, ) diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt index 48508a2..f2b0316 100644 --- a/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt @@ -25,5 +25,7 @@ data class CharacterInfo( val repelLeft: Short, val repelItemId: Short, val lureLeft: Short, - val lureItemId: Short + val lureItemId: Short, + /** Runtime warp destination for MAP_DYNAMIC warps (setdynamicwarp), or null if none is set. */ + val dynamicWarp: DynamicWarp? = null, ) diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/DynamicWarp.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/DynamicWarp.kt new file mode 100644 index 0000000..8de64f5 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/DynamicWarp.kt @@ -0,0 +1,16 @@ +package de.fiereu.openmmo.common + +import de.fiereu.openmmo.common.enums.Direction + +/** + * A destination set at runtime by a script (the decomp setdynamicwarp) that a MAP_DYNAMIC warp tile + * resolves to when the player steps on it. Used for the intro truck exit. + */ +data class DynamicWarp( + val regionId: Byte, + val bankId: Byte, + val mapId: Byte, + val x: Short, + val y: Short, + val facing: Direction, +) diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt index 4c5bc91..76d4256 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt @@ -4,16 +4,19 @@ import de.fiereu.openmmo.common.enums.Direction /** * One step of an applymovement sequence: either a walk one tile in [direction] or a turn in place - * to face [direction]. This is the small, game agnostic vocabulary scripts use, decomp movement - * templates are ported into lists of these. + * to face [direction]. [action] is the client movement-action byte (from packet captures: face + * 0x00-0x03, walk 0x10-0x13); a whole sequence of these is sent in one movement packet. This is the + * small game agnostic vocabulary scripts use. */ -enum class MovementStep(val direction: Direction, val walks: Boolean) { - WALK_UP(Direction.UP, true), - WALK_DOWN(Direction.DOWN, true), - WALK_LEFT(Direction.LEFT, true), - WALK_RIGHT(Direction.RIGHT, true), - FACE_UP(Direction.UP, false), - FACE_DOWN(Direction.DOWN, false), - FACE_LEFT(Direction.LEFT, false), - FACE_RIGHT(Direction.RIGHT, false), +enum class MovementStep(val direction: Direction, val walks: Boolean, val action: Int) { + FACE_DOWN(Direction.DOWN, false, 0x00), + FACE_UP(Direction.UP, false, 0x01), + FACE_LEFT(Direction.LEFT, false, 0x02), + FACE_RIGHT(Direction.RIGHT, false, 0x03), + WALK_DOWN(Direction.DOWN, true, 0x10), + WALK_UP(Direction.UP, true, 0x11), + WALK_LEFT(Direction.LEFT, true, 0x12), + WALK_RIGHT(Direction.RIGHT, true, 0x13), + // Hides the entity in place, used at the end of a walk into a door (decomp set_invisible). + SET_INVISIBLE(Direction.DOWN, false, 0x60), } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt index 17de0af..31e3b25 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt @@ -1,7 +1,9 @@ package de.fiereu.openmmo.server.game.script import de.fiereu.network.SessionContext +import de.fiereu.openmmo.common.DynamicWarp import de.fiereu.openmmo.common.dialog.DialogLine +import de.fiereu.openmmo.common.enums.Direction import de.fiereu.openmmo.server.game.services.DialogService import de.fiereu.openmmo.server.game.services.ScriptMovementService import de.fiereu.openmmo.server.game.services.StoryService @@ -56,6 +58,16 @@ internal constructor( suspend fun moveSelf(vararg steps: MovementStep) = movement.moveSelf(session, state, steps.toList()) + /** Show a normally hidden map npc (its decomp local id) to this player, the decomp addobject. */ + fun showNpc(localId: Int) = movement.showNpc(session, state, localId) + + /** Set where a MAP_DYNAMIC warp sends this player (the decomp setdynamicwarp). */ + fun setDynamicWarp(regionId: Int, bankId: Int, mapId: Int, x: Int, y: Int, facing: Direction) = + movement.setDynamicWarp( + state, + DynamicWarp( + regionId.toByte(), bankId.toByte(), mapId.toByte(), x.toShort(), y.toShort(), facing)) + private companion object { // Sign boxes have no speaker, npc boxes point at the entity. const val SIGN = 3 diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt index 1c5b117..72b61a7 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt @@ -31,7 +31,20 @@ constructor( private val storyService: StoryService, private val movementService: ScriptMovementService, ) { - fun run(session: SessionContext, state: PlayerState, script: Script, entityId: Long) { + fun run(session: SessionContext, state: PlayerState, script: Script, entityId: Long) = + runAll(session, state, listOf(script), entityId) + + /** + * Runs [scripts] in order on one coroutine so they share the dialog lock, for example a map's + * on-transition script followed by its matching on-frame cutscene. + */ + fun runAll( + session: SessionContext, + state: PlayerState, + scripts: List