feat(game): stub wild battle flow with catch, bag, switching, field state, and flee
This commit is contained in:
1 parent
9732c9373c
commit
d618cff20e
92 files changed
+1499
-272
No files matched your search
@@ -10,6 +10,7 @@ import de.fiereu.openmmo.net.game.packets.AddFriendPacket
|
||||
import de.fiereu.openmmo.net.game.packets.BlockPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.CancelSocialInteractionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessagePacket
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessageSendPacket
|
||||
import de.fiereu.openmmo.net.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.DialogChoicePacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityInteractPacket
|
||||
@@ -26,6 +27,28 @@ import de.fiereu.openmmo.net.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestSocialProfilePacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.UnblockPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleActionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleActionSelectPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleActionSubmitPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleAppearancePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleCancelRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleChallengeRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleChatMessagePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleLeavePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleMoveUsePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattlePartySlotSelectPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattlePartySwitchPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleReadyPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleRewardSelectPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSequencePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSimulationRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSlotActionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchSelectionsPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleTargetPickPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleTeamPreviewConfirmPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleTierSelectPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleTransitionReadyPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleUseItemPacket
|
||||
import de.fiereu.openmmo.net.game.packets.guild.GuildActivityLogPageRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.guild.GuildCreatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.guild.GuildDisbandPacket
|
||||
@@ -36,6 +59,7 @@ import de.fiereu.openmmo.net.game.packets.guild.GuildMemberRankAssignPacket
|
||||
import de.fiereu.openmmo.net.game.packets.guild.GuildMotdUpdatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.guild.GuildRankLabelUpdatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.guild.GuildRankPermissionUpdatePacket
|
||||
import de.fiereu.openmmo.server.game.services.BattleService
|
||||
import de.fiereu.openmmo.server.game.services.DialogService
|
||||
import de.fiereu.openmmo.server.game.services.GuildService
|
||||
import de.fiereu.openmmo.server.game.services.InteractionService
|
||||
@@ -62,6 +86,7 @@ constructor(
|
||||
private val multiplayerService: MultiplayerService,
|
||||
private val socialService: SocialService,
|
||||
private val guildService: GuildService,
|
||||
private val battleService: BattleService,
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
private val characterStore: CharacterStore,
|
||||
scope: CoroutineScope,
|
||||
@@ -100,8 +125,32 @@ constructor(
|
||||
on<GuildRankLabelUpdatePacket> { event -> guildService.onRankLabelUpdate(event) }
|
||||
on<GuildActivityLogPageRequestPacket> { event -> guildService.onActivityLogPageRequest(event) }
|
||||
|
||||
on<BattleMoveUsePacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattlePartySwitchPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleActionPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleActionSelectPacket> { event -> battleService.onBattleAction(event) }
|
||||
on<BattleLeavePacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleSequencePacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleSlotActionPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleSwitchSelectionsPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleUseItemPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleAppearancePacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleCancelRequestPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleSimulationRequestPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleReadyPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleActionSubmitPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleTierSelectPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleChatMessagePacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattlePartySlotSelectPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleTargetPickPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleTransitionReadyPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleTeamPreviewConfirmPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleRewardSelectPacket> { event -> battleService.onBattlePacket(event) }
|
||||
on<BattleChallengeRequestPacket> { event -> battleService.onBattlePacket(event) }
|
||||
|
||||
on<KeepAlivePacket> { event -> event.session.send(event.packet) }
|
||||
on<ChatMessagePacket> { event -> onChatMessage(event) }
|
||||
on<ChatMessageSendPacket> { event -> battleService.onChatSend(event) }
|
||||
}
|
||||
|
||||
override fun onInactive() {
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.PokemonMove
|
||||
import de.fiereu.openmmo.common.enums.BattleAction
|
||||
import de.fiereu.openmmo.common.enums.EVs
|
||||
import de.fiereu.openmmo.common.enums.IVs
|
||||
import de.fiereu.openmmo.common.enums.PokemonContainer
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessageSendPacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityMovePpPacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityPresencePacket
|
||||
import de.fiereu.openmmo.net.game.packets.PokemonContainerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SocialListEntryAddPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleActionSelectPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleBulkStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityMoveEventPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleFieldMon
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleFieldStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleListEventDetail
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleListEventPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleQueuedEventPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSidePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSlotEventEnumPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInMon
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleTileMapPacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import java.time.LocalDateTime
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
private const val TEST_BATTLE_COMMAND = "/testbattle"
|
||||
// The bag is populated in the join flow, so /catch drives the catch sequence directly for testing
|
||||
// even though a ball can now be thrown from the UI.
|
||||
private const val CATCH_COMMAND = "/catch"
|
||||
|
||||
private const val PATRAT: Short = 504
|
||||
private const val WILD_MON_ID = 0x000000000003C000L
|
||||
private const val CAUGHT_PATRAT_UID = 0x000000000002C000L
|
||||
private const val WILD_PATRAT_HP: Short = 14
|
||||
private const val CAUGHT_PATRAT_LEVEL: Byte = 2
|
||||
private const val CAUGHT_PATRAT_HP: Short = 8
|
||||
private const val CAUGHT_PATRAT_XP = 8
|
||||
private const val PATRAT_TACKLE: Short = 33
|
||||
private const val PATRAT_TACKLE_PP: Byte = 35
|
||||
private const val ACTION_PROMPT: Byte = -128 // 0x80
|
||||
|
||||
private const val ENEMY_MOVE: Short = 33
|
||||
private const val MOVE_EVENT_KIND: Byte = 1
|
||||
private const val POKE_BALL_ITEM: Short = 5004
|
||||
// Slot-event type shown when the player gets away from a wild battle.
|
||||
private const val FLED_EVENT: Byte = 0
|
||||
|
||||
// The player's overworld entity is hidden while the battle scene is up and shown again when it
|
||||
// ends.
|
||||
private const val PRESENCE_IN_BATTLE: Byte = 1
|
||||
private const val PRESENCE_OVERWORLD: Byte = 0
|
||||
|
||||
// The active battle side reported to the client so the bag knows which monster an item targets.
|
||||
private const val PLAYER_SIDE: Byte = 1
|
||||
|
||||
@Singleton
|
||||
class BattleService
|
||||
@Inject
|
||||
constructor(
|
||||
private val characterStore: CharacterStore,
|
||||
) {
|
||||
|
||||
private val battleTurn = ConcurrentHashMap<Long, Int>()
|
||||
// The active party slot per battle, and which slots have been sent out as active. A monster's
|
||||
// first appearance carries its full block, a return only its active detail.
|
||||
private val battleActiveSlot = ConcurrentHashMap<Long, Int>()
|
||||
private val battleSeenActive = ConcurrentHashMap<Long, MutableSet<Int>>()
|
||||
|
||||
fun onBattlePacket(event: PacketEvent<*>) {
|
||||
log.info { "Battle packet ${event.packet::class.simpleName} received: ${event.packet}" }
|
||||
}
|
||||
|
||||
fun onBattleAction(event: PacketEvent<BattleActionSelectPacket>) {
|
||||
val session = event.session
|
||||
val charId = session.attributes[PLAYER_STATE]?.characterId ?: return
|
||||
val action = event.packet
|
||||
log.info { "Battle action char=$charId: $action" }
|
||||
when (action.action) {
|
||||
BattleAction.MOVE -> {
|
||||
playMoveAnimations(session, charId, action.moveOrItemId)
|
||||
advanceTurn(session, charId)
|
||||
}
|
||||
BattleAction.ITEM -> catchWildMon(session, charId)
|
||||
BattleAction.SWITCH -> switchMon(session, charId, action.moveOrItemId)
|
||||
BattleAction.RUN -> fleeBattle(session, charId)
|
||||
}
|
||||
}
|
||||
|
||||
// The client picked a party slot to switch to. The switch-in packet (opcode 0x35) brings the new
|
||||
// monster onto the field: the first time a monster comes out it carries its full block, otherwise
|
||||
// just its active detail. The turn then advances and re-prompts for the next action.
|
||||
private fun switchMon(session: SessionContext, charId: Long, partyIndex: Short) {
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
val target = partyIndex.toInt()
|
||||
val mon = stored.pokemon.getOrNull(target)
|
||||
if (mon == null) {
|
||||
log.warn { "Switch to invalid party index $partyIndex for char=$charId" }
|
||||
advanceTurn(session, charId)
|
||||
return
|
||||
}
|
||||
val oldSlot = battleActiveSlot[charId] ?: 0
|
||||
if (target == oldSlot) {
|
||||
advanceTurn(session, charId)
|
||||
return
|
||||
}
|
||||
val seen = battleSeenActive.getOrPut(charId) { mutableSetOf(0) }
|
||||
val fullBlock = target !in seen
|
||||
log.info { "Switch char=$charId slot $oldSlot -> $target (fullBlock=$fullBlock)" }
|
||||
session.send(
|
||||
BattleSwitchInPacket(
|
||||
newSlot = target,
|
||||
oldSlot = oldSlot,
|
||||
mon = BattleSwitchInMon(mon.id, mon.dexId.toShort(), mon.hp),
|
||||
fullBlock = fullBlock,
|
||||
),
|
||||
)
|
||||
seen.add(target)
|
||||
battleActiveSlot[charId] = target
|
||||
advanceTurn(session, charId)
|
||||
}
|
||||
|
||||
// The player fled. The slot event shows the got-away text, the flee end marker closes the battle
|
||||
// scene, and the presence toggle restores the overworld player.
|
||||
private fun fleeBattle(session: SessionContext, charId: Long) {
|
||||
battleTurn.remove(charId)
|
||||
battleActiveSlot.remove(charId)
|
||||
battleSeenActive.remove(charId)
|
||||
session.send(BattleSlotEventEnumPacket(slot = 0, eventType = FLED_EVENT))
|
||||
session.send(BattleBulkStatePacket.fled())
|
||||
session.send(EntityPresencePacket(entityId = charId, status = PRESENCE_OVERWORLD))
|
||||
}
|
||||
|
||||
private fun catchWildMon(session: SessionContext, charId: Long) {
|
||||
log.info { "Ball thrown, catching wild Patrat for char=$charId" }
|
||||
battleTurn.remove(charId)
|
||||
battleActiveSlot.remove(charId)
|
||||
battleSeenActive.remove(charId)
|
||||
val ot = characterStore.getCharacter(charId)?.info?.name ?: ""
|
||||
val patrat = caughtPatrat(charId, ot)
|
||||
// The caught Patrat is sent as a full 148-byte record on opcode 0x14 before the ball-throw
|
||||
// event, so the client can resolve the monster when the throw lands.
|
||||
session.send(SocialListEntryAddPacket(patrat))
|
||||
// "Player threw a Poke Ball" event.
|
||||
session.send(
|
||||
BattleListEventPacket(
|
||||
kind = 0,
|
||||
value = POKE_BALL_ITEM,
|
||||
subKind = 4,
|
||||
detail = BattleListEventDetail(listType = 1, value = 1),
|
||||
),
|
||||
)
|
||||
endBattle(session, charId, patrat)
|
||||
}
|
||||
|
||||
// The wild encounter is over. The bulk-state end marker tells the client to leave the battle
|
||||
// scene, the presence toggle shows the overworld player again, and the party is persisted and
|
||||
// resynced so the caught monster stays after the battle.
|
||||
// TODO: the caught monster does not show up in the party after the battle closes.
|
||||
private fun endBattle(session: SessionContext, charId: Long, caught: Pokemon) {
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
if (stored.pokemon.none { it.id == caught.id }) {
|
||||
characterStore.addPokemon(charId, caught)
|
||||
}
|
||||
session.send(BattleBulkStatePacket.battleEnd())
|
||||
session.send(EntityPresencePacket(entityId = charId, status = PRESENCE_OVERWORLD))
|
||||
session.send(
|
||||
PokemonContainerPacket(
|
||||
container = PokemonContainer.PARTY,
|
||||
hasChange = true,
|
||||
delete = false,
|
||||
pokemon = characterStore.getCharacter(charId)?.pokemon ?: emptyList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun caughtPatrat(ownerId: Long, ot: String): Pokemon =
|
||||
Pokemon(
|
||||
id = CAUGHT_PATRAT_UID,
|
||||
ownerId = ownerId,
|
||||
container = PokemonContainer.PARTY,
|
||||
containerSlot = 1,
|
||||
dexId = PATRAT.toInt(),
|
||||
seed = 0,
|
||||
ot = ot,
|
||||
nickname = "",
|
||||
level = CAUGHT_PATRAT_LEVEL,
|
||||
hp = CAUGHT_PATRAT_HP,
|
||||
xp = CAUGHT_PATRAT_XP,
|
||||
eVs = EVs(),
|
||||
iVs = IVs(),
|
||||
moves =
|
||||
listOf(
|
||||
PokemonMove(PATRAT_TACKLE, PATRAT_TACKLE_PP),
|
||||
PokemonMove(0, 0),
|
||||
PokemonMove(0, 0),
|
||||
PokemonMove(0, 0),
|
||||
),
|
||||
isShiny = false,
|
||||
hasHiddenAbility = false,
|
||||
isAlpha = false,
|
||||
isSecret = false,
|
||||
isFatefulEncounter = false,
|
||||
isRaidEncounter = false,
|
||||
caughtAt = LocalDateTime.now(),
|
||||
)
|
||||
|
||||
private fun playMoveAnimations(session: SessionContext, charId: Long, playerMove: Short) {
|
||||
// The move comes from whichever monster is currently on the field, not always the lead.
|
||||
val activeSlot = battleActiveSlot[charId] ?: 0
|
||||
val partyMon = characterStore.getCharacter(charId)?.pokemon?.getOrNull(activeSlot) ?: return
|
||||
// Decrement the used move's PP before the animation, matching the real turn order.
|
||||
val slot = partyMon.moves.indexOfFirst { it.id == playerMove }
|
||||
if (slot >= 0) {
|
||||
val newPp = (partyMon.moves[slot].pp - 1).coerceAtLeast(0).toByte()
|
||||
session.send(EntityMovePpPacket(partyMon.id, slot.toByte(), newPp))
|
||||
}
|
||||
session.send(BattleEntityMoveEventPacket(partyMon.id, playerMove, MOVE_EVENT_KIND, emptyList()))
|
||||
session.send(BattleEntityMoveEventPacket(WILD_MON_ID, ENEMY_MOVE, MOVE_EVENT_KIND, emptyList()))
|
||||
}
|
||||
|
||||
private fun advanceTurn(session: SessionContext, charId: Long) {
|
||||
val turn = battleTurn.merge(charId, 1) { a, b -> a + b } ?: 1
|
||||
session.send(BattleTileMapPacket(groupId = turn.toShort(), slotTiles = null))
|
||||
session.send(BattleQueuedEventPacket(packed = ACTION_PROMPT))
|
||||
}
|
||||
|
||||
fun onChatSend(event: PacketEvent<ChatMessageSendPacket>) {
|
||||
val session = event.session
|
||||
val text = (event.packet.message ?: event.packet.target).trim()
|
||||
when {
|
||||
text.equals(TEST_BATTLE_COMMAND, ignoreCase = true) -> startTestBattle(session)
|
||||
text.equals(CATCH_COMMAND, ignoreCase = true) -> {
|
||||
val charId = session.attributes[PLAYER_STATE]?.characterId ?: return
|
||||
catchWildMon(session, charId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTestBattle(session: SessionContext) {
|
||||
val state = session.attributes[PLAYER_STATE] ?: return
|
||||
val charId = state.characterId ?: return
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
val partyMon = stored.pokemon.firstOrNull()
|
||||
if (partyMon == null) {
|
||||
log.warn { "Cannot start test battle: char=$charId has no party monster" }
|
||||
return
|
||||
}
|
||||
log.info {
|
||||
"Starting test battle for char=$charId (${stored.info.name}): dex=${partyMon.dexId} vs wild Patrat"
|
||||
}
|
||||
battleTurn[charId] = 1
|
||||
battleActiveSlot[charId] = 0
|
||||
battleSeenActive[charId] = mutableSetOf(0)
|
||||
// Hide the overworld player entity while the battle scene is up, matching the real enter order.
|
||||
session.send(EntityPresencePacket(entityId = charId, status = PRESENCE_IN_BATTLE))
|
||||
// Tell the client which side is local so the battle bag knows which monster an item targets.
|
||||
// Without it, opening the bag crashes. Opcode 0x40 is left alone here, since re-sending it
|
||||
// would
|
||||
// wipe the balls out of the battle bag.
|
||||
session.send(BattleSidePacket(side = PLAYER_SIDE))
|
||||
session.send(
|
||||
BattleFieldStatePacket(
|
||||
playerName = stored.info.name,
|
||||
playerId = charId,
|
||||
playerParty = stored.pokemon.map { BattleFieldMon(it.id, it.dexId.toShort(), it.hp) },
|
||||
wildMon = BattleFieldMon(WILD_MON_ID, PATRAT, WILD_PATRAT_HP),
|
||||
),
|
||||
)
|
||||
session.send(BattleTileMapPacket(groupId = 1, slotTiles = null))
|
||||
session.send(BattleQueuedEventPacket(packed = ACTION_PROMPT))
|
||||
}
|
||||
}
|
||||
@@ -17,13 +17,23 @@ import de.fiereu.openmmo.net.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinPacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinResponsePacket
|
||||
import de.fiereu.openmmo.net.game.packets.LoadEntityPacket
|
||||
import de.fiereu.openmmo.net.game.packets.LocalPlayerStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.MenuPagePayloadPacket
|
||||
import de.fiereu.openmmo.net.game.packets.NewAuthData
|
||||
import de.fiereu.openmmo.net.game.packets.ObjectiveProgressBulkPacket
|
||||
import de.fiereu.openmmo.net.game.packets.PokedexSpeciesResetPacket
|
||||
import de.fiereu.openmmo.net.game.packets.PokemonContainerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RenderScreenPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestCharactersPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectedCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ViewScalePacket
|
||||
import de.fiereu.openmmo.net.game.packets.WorldFlagTableResetPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleRatingBulkPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleStateBytePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.ItemStack
|
||||
import de.fiereu.openmmo.net.game.packets.battle.itemStacksPacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.session.PlayerState
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
@@ -35,6 +45,27 @@ import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
private fun hexToBytes(hex: String): ByteArray =
|
||||
ByteArray(hex.length / 2) { hex.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
|
||||
|
||||
// The world-flag table the client loads on join. Groups one to three are zlib streams that each
|
||||
// decompress to a 67-byte flag block, the fourth is empty. The client reads real flag state while
|
||||
// building the follower and party, so empty groups leave a lookup null and crash it.
|
||||
private val WORLD_FLAG_GROUPS =
|
||||
listOf(
|
||||
"789c637060a00434a8b0320000133900ea",
|
||||
"789c637060a00434303032000012c500c2",
|
||||
"789c637060a00434303032000012c500c2",
|
||||
"",
|
||||
)
|
||||
.map(::hexToBytes)
|
||||
|
||||
private const val POKE_BALL_ITEM_ID: Short = 5004
|
||||
private const val POKE_BALL_OBJECT_ID = 0x0000000000015000L
|
||||
private const val POKE_BALL_QUANTITY: Short = 10
|
||||
private val STARTING_BAG =
|
||||
listOf(ItemStack(POKE_BALL_OBJECT_ID, POKE_BALL_ITEM_ID, POKE_BALL_QUANTITY))
|
||||
|
||||
@Singleton
|
||||
class LoginService
|
||||
@Inject
|
||||
@@ -125,6 +156,11 @@ constructor(
|
||||
sessionRegistry.bindCharacter(ctx, charId)
|
||||
log.info { "Player selected character '${stored.info.name}' (id=$charId)" }
|
||||
|
||||
// Initialise the client world-flag table before any monster or follower is built. Without it
|
||||
// the table stays null and the client crashes constructing a party monster that reads a flag.
|
||||
ctx.send(WorldFlagTableResetPacket(WORLD_FLAG_GROUPS))
|
||||
ctx.send(buildLocalPlayerState(stored.info, stored.pokemon.map { it.dexId.toShort() }))
|
||||
|
||||
val info = stored.info
|
||||
val now = LocalDateTime.now()
|
||||
val updatedInfo = info.copy(lastLogin = now)
|
||||
@@ -134,9 +170,9 @@ constructor(
|
||||
mapOf(
|
||||
PokemonContainer.PARTY to stored.pokemon,
|
||||
PokemonContainer.PC to stored.pcStorage,
|
||||
PokemonContainer.BATTLE_BOX_1 to emptyList<Pokemon>(),
|
||||
PokemonContainer.BATTLE_BOX_2 to emptyList<Pokemon>(),
|
||||
PokemonContainer.DAYCARE to emptyList<Pokemon>(),
|
||||
PokemonContainer.BATTLE_BOX_1 to emptyList(),
|
||||
PokemonContainer.BATTLE_BOX_2 to emptyList(),
|
||||
PokemonContainer.DAYCARE to emptyList(),
|
||||
)
|
||||
for ((container, pokemon) in containers) {
|
||||
ctx.send(
|
||||
@@ -158,7 +194,12 @@ constructor(
|
||||
))
|
||||
}
|
||||
|
||||
// The bag item stacks (opcode 0x40). The real server sends these during join, interleaved with
|
||||
// the container packets, so the client has the items before entering the world.
|
||||
ctx.send(itemStacksPacket(STARTING_BAG))
|
||||
|
||||
ctx.send(SelectedCharacterPacket(info))
|
||||
sendJoinState(ctx)
|
||||
ctx.send(
|
||||
ChatMessagePacket(
|
||||
ChatType.GAME_NOTIFICATIONS,
|
||||
@@ -170,6 +211,45 @@ constructor(
|
||||
preloadMapAndJoin(ctx, state, info)
|
||||
}
|
||||
|
||||
// The full local-player state snapshot the client loads on login. Missing it leaves player state
|
||||
// uninitialised and the client crashes reading it (for example when opening the battle bag).
|
||||
private fun buildLocalPlayerState(
|
||||
info: CharacterInfo,
|
||||
partyDex: List<Short>
|
||||
): LocalPlayerStatePacket =
|
||||
LocalPlayerStatePacket(
|
||||
region = info.positionRegionId,
|
||||
mapId = info.positionMapId.toShort(),
|
||||
moveSpeed = 0.05f,
|
||||
x = info.positionX,
|
||||
y = info.positionY,
|
||||
z = 0,
|
||||
money = info.money,
|
||||
gender = 0,
|
||||
skinTone = 0,
|
||||
hairColor = 0,
|
||||
playtime = 0.0,
|
||||
flags = 0,
|
||||
partyDex = partyDex,
|
||||
partyForms = partyDex.map { 0.toByte() },
|
||||
pokedexSeen = emptyList(),
|
||||
pokedexCaught = emptyList(),
|
||||
badges = emptyList(),
|
||||
variables = emptyList(),
|
||||
)
|
||||
|
||||
// Small state packets the real server sends in the join flow. The battle-state byte and menu
|
||||
// payloads initialise state the battle bag reads, so without them opening the bag crashes.
|
||||
private fun sendJoinState(ctx: SessionContext) {
|
||||
ctx.send(ViewScalePacket(viewScale = 5))
|
||||
ctx.send(BattleRatingBulkPacket(emptyList()))
|
||||
ctx.send(BattleStateBytePacket(state = 0x19))
|
||||
ctx.send(PokedexSpeciesResetPacket(emptyList()))
|
||||
ctx.send(ObjectiveProgressBulkPacket(emptyList()))
|
||||
ctx.send(MenuPagePayloadPacket(menuType = 0, page = null))
|
||||
ctx.send(MenuPagePayloadPacket(menuType = 1, page = null))
|
||||
}
|
||||
|
||||
private fun preloadMapAndJoin(
|
||||
ctx: SessionContext,
|
||||
state: PlayerState,
|
||||
|
||||
@@ -58,7 +58,7 @@ constructor(
|
||||
NpcEntry(9, 4, 14, 36, 1, 5121, 0, 1, 9),
|
||||
NpcEntry(8, 7, 32, 36, 1, 5632, 0, 0, 8),
|
||||
)
|
||||
val baseEntityId = 0x1A6BFE24CC88E000L
|
||||
val baseEntityId = 0x000000000001E000L
|
||||
for (npc in all16) {
|
||||
val entityId = baseEntityId or npc.entityIdx.toLong()
|
||||
log.info {
|
||||
|
||||
+78
-1
@@ -2,12 +2,25 @@ package de.fiereu.openmmo.server.game.storage
|
||||
|
||||
import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.PokemonMove
|
||||
import de.fiereu.openmmo.common.enums.EVs
|
||||
import de.fiereu.openmmo.common.enums.IVs
|
||||
import de.fiereu.openmmo.common.enums.PokemonContainer
|
||||
import java.time.LocalDateTime
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
// A starter party monster for the dev test character. Its uid, species, HP, and moves match the
|
||||
// field state emitted by the test battle so the client can resolve the player's monster.
|
||||
const val STARTER_SNIVY_UID = 0x000000000001C000L
|
||||
const val SNIVY_DEX_ID = 495
|
||||
|
||||
// A second party monster so the switch action is selectable in the test battle.
|
||||
const val STARTER_PATRAT_UID = 0x000000000002C000L
|
||||
const val PATRAT_DEX_ID = 504
|
||||
|
||||
data class StoredCharacter(
|
||||
val info: CharacterInfo,
|
||||
val pokemon: MutableList<Pokemon>,
|
||||
@@ -27,7 +40,9 @@ class CharacterStore @Inject constructor() {
|
||||
|
||||
private fun ensureTestCharacter() {
|
||||
if (characters.isEmpty()) {
|
||||
createCharacter(userId = 1, name = "Test")
|
||||
val test = createCharacter(userId = 1, name = "Test")
|
||||
test.pokemon.add(starterSnivy(test.info.id, test.info.name))
|
||||
test.pokemon.add(starterPatrat(test.info.id, test.info.name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +119,68 @@ class CharacterStore @Inject constructor() {
|
||||
characters[characterId] = stored.copy(info = newInfo)
|
||||
}
|
||||
|
||||
private fun starterSnivy(ownerId: Long, ot: String): Pokemon =
|
||||
Pokemon(
|
||||
id = STARTER_SNIVY_UID,
|
||||
ownerId = ownerId,
|
||||
container = PokemonContainer.PARTY,
|
||||
containerSlot = 0,
|
||||
dexId = SNIVY_DEX_ID,
|
||||
seed = 0,
|
||||
ot = ot,
|
||||
nickname = "",
|
||||
level = 5,
|
||||
hp = 20,
|
||||
xp = 165,
|
||||
eVs = EVs(),
|
||||
iVs = IVs(),
|
||||
moves =
|
||||
listOf(
|
||||
PokemonMove(33, 35),
|
||||
PokemonMove(43, 30),
|
||||
PokemonMove(0, 0),
|
||||
PokemonMove(0, 0),
|
||||
),
|
||||
isShiny = false,
|
||||
hasHiddenAbility = false,
|
||||
isAlpha = false,
|
||||
isSecret = false,
|
||||
isFatefulEncounter = false,
|
||||
isRaidEncounter = false,
|
||||
caughtAt = LocalDateTime.now(),
|
||||
)
|
||||
|
||||
private fun starterPatrat(ownerId: Long, ot: String): Pokemon =
|
||||
Pokemon(
|
||||
id = STARTER_PATRAT_UID,
|
||||
ownerId = ownerId,
|
||||
container = PokemonContainer.PARTY,
|
||||
containerSlot = 1,
|
||||
dexId = PATRAT_DEX_ID,
|
||||
seed = 0,
|
||||
ot = ot,
|
||||
nickname = "",
|
||||
level = 3,
|
||||
hp = 12,
|
||||
xp = 27,
|
||||
eVs = EVs(),
|
||||
iVs = IVs(),
|
||||
moves =
|
||||
listOf(
|
||||
PokemonMove(33, 35),
|
||||
PokemonMove(0, 0),
|
||||
PokemonMove(0, 0),
|
||||
PokemonMove(0, 0),
|
||||
),
|
||||
isShiny = false,
|
||||
hasHiddenAbility = false,
|
||||
isAlpha = false,
|
||||
isSecret = false,
|
||||
isFatefulEncounter = false,
|
||||
isRaidEncounter = false,
|
||||
caughtAt = LocalDateTime.now(),
|
||||
)
|
||||
|
||||
fun addPokemon(characterId: Long, pokemon: Pokemon) {
|
||||
characters[characterId]?.pokemon?.add(pokemon)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user