feat(game): let the player replace a move on level up (#90)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-05 23:10:59 +02:00
1 parent 1ecfabaed5
commit 4470cbda37
11 files changed
+281 -66

No files matched your search

@@ -41,6 +41,8 @@ private const val PLAYER_SIDE: Byte = 1
// The target move short the live server sends for each event. Meaning unknown, but it is fixed per
// event type in every capture: a hit carries 0x0200, other events carry 0.
private const val DELTA_MOVE_SLOTS = 0x4
private const val HP_TARGET_MOVE: Short = 0x0200
private const val DEFAULT_TARGET_MOVE: Short = 0
@@ -220,6 +222,14 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
interestManager.broadcast(battle.key, packet)
}
/** The delta that tells the client a monster's moveset changed. */
fun moveSlotsDelta(
entityId: Long,
moveSlots: List<Pair<Short, Byte>>,
ppUps: Byte,
): BattleEntityDeltaPacket =
delta(entityId, DELTA_MOVE_SLOTS).copy(moveSlots = moveSlots, ppUps = ppUps)
fun delta(
entityId: Long,
mask: Int,
@@ -10,6 +10,12 @@ import javax.inject.Singleton
/** A move learned on level up, ready to be announced to the player. */
data class LearnedMove(val level: Int, val moveId: Int, val name: String)
/** [learned] went into a free slot, [offered] needs the player to drop a move first. */
data class MoveLearnOutcome(
val learned: List<LearnedMove>,
val offered: List<LearnedMove>,
)
/** Teaches the level up moves a monster gained from a level range. */
@Singleton
class MoveLearner
@@ -19,25 +25,46 @@ constructor(
private val moves: MoveRegistry,
) {
/**
* Adds every move learned above [fromLevel] up to [toLevel] that fits a free slot and returns
* them. A monster that already knows four moves learns nothing.
*/
// TODO Let the player replace a move when the moveset is full
// A monster with four moves silently skips every move it would learn on level up. The client's
// move learn prompt packets are unknown, ByteDex holds no capture of them. Capture the prompt
// and the answer, then apply the chosen slot here.
fun learn(known: MutableList<PokemonMove>, dexId: Int, fromLevel: Int, toLevel: Int) = buildList {
/** Adds every move learned above [fromLevel] up to [toLevel] that fits a free slot. */
fun learn(
known: MutableList<PokemonMove>,
dexId: Int,
fromLevel: Int,
toLevel: Int,
): MoveLearnOutcome {
val learned = mutableListOf<LearnedMove>()
val offered = mutableListOf<LearnedMove>()
for (level in fromLevel + 1..toLevel) {
for (moveId in learnsets.movesAt(dexId, level)) {
if (known.any { it.id.toInt() == moveId }) continue
if (offered.any { it.moveId == moveId }) continue
val def = moves.get(moveId) ?: continue
val entry = LearnedMove(level, moveId, def.name)
val slot = known.indexOfFirst { it.id.toInt() == 0 }
if (slot < 0 && known.size >= MAX_MOVE_SLOTS) continue
val learned = PokemonMove(moveId.toShort(), def.pp.toByte())
if (slot < 0) known += learned else known[slot] = learned
add(LearnedMove(level, moveId, def.name))
if (slot < 0 && known.size >= MAX_MOVE_SLOTS) {
offered += entry
continue
}
val move = PokemonMove(moveId.toShort(), def.pp.toByte())
if (slot < 0) known += move else known[slot] = move
learned += entry
}
}
return MoveLearnOutcome(learned, offered)
}
/** Applies the picked moveset, keeping the pp of moves it already had. */
fun apply(known: MutableList<PokemonMove>, chosen: List<Short>, offered: List<Short>): Boolean {
if (chosen.size != known.size || chosen.size != chosen.distinct().size) return false
val allowed = known.map { it.id } + offered
if (chosen.any { it !in allowed }) return false
val applied =
chosen.map { id ->
known.firstOrNull { it.id == id }
?: PokemonMove(id, moves.get(id.toInt())?.pp?.toByte() ?: 0)
}
known.clear()
known += applied
return true
}
}
@@ -36,7 +36,6 @@ 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
@@ -50,6 +49,7 @@ 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.battle.moves.MoveLearnReplyPacket
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionResponsePacket
import de.fiereu.openmmo.net.game.packets.guild.GuildActivityLogPageRequestPacket
import de.fiereu.openmmo.net.game.packets.guild.GuildCreatePacket
@@ -132,7 +132,7 @@ constructor(
on<GuildRankLabelUpdatePacket> { event -> guildService.onRankLabelUpdate(event) }
on<GuildActivityLogPageRequestPacket> { event -> guildService.onActivityLogPageRequest(event) }
on<BattleMoveUsePacket> { event -> battleService.onBattlePacket(event) }
on<MoveLearnReplyPacket> { event -> battleService.onMoveLearnReply(event) }
on<BattlePartySwitchPacket> { event -> battleService.onBattlePacket(event) }
on<BattleActionPacket> { event -> battleService.onBattlePacket(event) }
on<BattleActionSelectPacket> { event -> battleService.onBattleAction(event) }
@@ -12,6 +12,8 @@ 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.BattleListEventDetail
import de.fiereu.openmmo.net.game.packets.battle.BattleListEventPacket
import de.fiereu.openmmo.net.game.packets.battle.moves.MoveLearnPromptPacket
import de.fiereu.openmmo.net.game.packets.battle.moves.MoveLearnReplyPacket
import de.fiereu.openmmo.pokemon.SpeciesRegistry
import de.fiereu.openmmo.server.game.battle.BattleInstance
import de.fiereu.openmmo.server.game.battle.BattleMonState
@@ -30,6 +32,7 @@ import de.fiereu.openmmo.server.game.storage.CharacterStore
import de.fiereu.openmmo.server.game.world.interest.InterestManager
import io.github.oshai.kotlinlogging.KotlinLogging
import java.time.LocalDateTime
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import javax.inject.Singleton
@@ -47,6 +50,9 @@ private const val MAX_WILD_LEVEL = 100
private const val POKE_BALL_ITEM: Short = 5004
/** A prompt waiting for its answer, kept after the battle ends. */
private data class PendingMoveLearn(val entityId: Long, val offered: List<Short>)
/**
* Orchestrates wild battles: builds the battle state from the party and a rolled wild monster,
* routes client actions through the [TurnEngine], and persists the outcome. Packets go out through
@@ -68,6 +74,8 @@ constructor(
private val moveRegistry: MoveRegistry,
) {
private val pendingLearns = ConcurrentHashMap<Long, PendingMoveLearn>()
fun onBattlePacket(event: PacketEvent<*>) {
log.info { "Battle packet ${event.packet::class.simpleName} received: ${event.packet}" }
}
@@ -88,6 +96,27 @@ constructor(
}
}
/** Applies the moveset the player picked after a level up. */
fun onMoveLearnReply(event: PacketEvent<MoveLearnReplyPacket>) {
val charId = event.session.attributes[PLAYER_STATE]?.characterId ?: return
val reply = event.packet
val pending = pendingLearns[charId] ?: return
if (pending.entityId != reply.entityId) return
pendingLearns.remove(charId)
val stored =
characterStore.getCharacter(charId)?.pokemon?.firstOrNull { it.id == reply.entityId }
?: return
val moves = stored.moves.toMutableList()
if (!moveLearner.apply(moves, reply.moveIds, pending.offered)) {
log.warn { "char=$charId picked an invalid moveset for ${reply.entityId}: ${reply.moveIds}" }
return
}
if (moves == stored.moves) return
characterStore.updatePokemon(charId, stored.copy(moves = moves))
characterStore.flushCharacterAsync(charId)
event.session.send(emitter.moveSlotsDelta(reply.entityId, moves.map { it.id to it.pp }, 0))
}
fun onChatSend(event: PacketEvent<ChatMessageSendPacket>) {
val session = event.session
val text = (event.packet.message ?: event.packet.target).trim()
@@ -110,6 +139,7 @@ constructor(
/** Ends a running battle when the player disconnects, keeping the last hp and pp state. */
fun onDisconnect(session: SessionContext) {
val charId = session.attributes[PLAYER_STATE]?.characterId ?: return
pendingLearns.remove(charId)
val battle = battles.byChar(charId) ?: return
persistParty(battle)
finishBattle(battle, BattleResult.DISCONNECTED)
@@ -328,12 +358,17 @@ constructor(
"char=${battle.charId} won: +${reward.xpGained} xp, level ${winner.level} -> ${reward.newLevel}"
}
winner.currentHp = reward.newCurrentHp
val learned =
val outcome =
moveLearner.learn(winner.moves, winner.source.dexId, winner.level, reward.newLevel)
emitter.sendVictoryDelta(battle, winner.entityId, reward)
for (move in learned) {
for (move in outcome.learned) {
emitter.sendNotice(battle, "${winner.species.name} learned ${move.name}!")
}
if (outcome.offered.isNotEmpty()) {
val offered = outcome.offered.map { it.moveId.toShort() }
pendingLearns[battle.charId] = PendingMoveLearn(winner.entityId, offered)
battle.session.send(MoveLearnPromptPacket(winner.entityId, offered))
}
characterStore.updatePokemon(
battle.charId,
winner.source.copy(
@@ -13,6 +13,7 @@ private const val GROWL: Short = 45
private const val LEECH_SEED: Short = 73
private const val VINE_WHIP: Short = 22
private const val POISON_POWDER: Short = 77
private const val SLEEP_POWDER: Short = 79
private fun slots(vararg ids: Short) = ids.map { PokemonMove(it, 0) }.toMutableList()
@@ -23,8 +24,9 @@ class MoveLearnerTest :
test("a move learned at the new level goes into a free slot") {
val known = slots(TACKLE, 0, 0, 0)
val learned = learner.learn(known, BULBASAUR, 3, 4)
learned.map { it.moveId } shouldBe listOf(GROWL.toInt())
val outcome = learner.learn(known, BULBASAUR, 3, 4)
outcome.learned.map { it.moveId } shouldBe listOf(GROWL.toInt())
outcome.offered.shouldBeEmpty()
known.map { it.id } shouldBe listOf(TACKLE, GROWL, 0, 0)
}
@@ -36,39 +38,44 @@ class MoveLearnerTest :
test("skipping levels learns every move in between") {
val known = slots(TACKLE, 0, 0, 0)
val learned = learner.learn(known, BULBASAUR, 1, 10)
learned.map { it.moveId.toShort() } shouldBe listOf(GROWL, LEECH_SEED, VINE_WHIP)
val outcome = learner.learn(known, BULBASAUR, 1, 10)
outcome.learned.map { it.moveId.toShort() } shouldBe listOf(GROWL, LEECH_SEED, VINE_WHIP)
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
}
test("a full moveset keeps its moves") {
test("a full moveset offers what it cannot fit and keeps its moves") {
val known = slots(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
learner.learn(known, BULBASAUR, 14, 15).shouldBeEmpty()
val outcome = learner.learn(known, BULBASAUR, 14, 15)
outcome.learned.shouldBeEmpty()
outcome.offered.map { it.moveId.toShort() } shouldBe listOf(POISON_POWDER, SLEEP_POWDER)
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
}
// Bulbasaur learns Poison Powder and Sleep Powder at 15, only the first one fits.
test("a partly full moveset learns until it runs out of slots") {
val known = slots(TACKLE, GROWL, LEECH_SEED, 0)
val learned = learner.learn(known, BULBASAUR, 14, 15)
learned.map { it.moveId.toShort() } shouldBe listOf(POISON_POWDER)
val outcome = learner.learn(known, BULBASAUR, 14, 15)
outcome.learned.map { it.moveId.toShort() } shouldBe listOf(POISON_POWDER)
outcome.offered.map { it.moveId.toShort() } shouldBe listOf(SLEEP_POWDER)
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, POISON_POWDER)
}
test("a known move is not learned twice") {
val known = slots(GROWL, 0, 0, 0)
learner.learn(known, BULBASAUR, 3, 4).shouldBeEmpty()
val outcome = learner.learn(known, BULBASAUR, 3, 4)
outcome.learned.shouldBeEmpty()
outcome.offered.shouldBeEmpty()
known.map { it.id } shouldBe listOf(GROWL, 0, 0, 0)
}
test("no level up learns nothing") {
val known = slots(TACKLE, 0, 0, 0)
learner.learn(known, BULBASAUR, 5, 5).shouldBeEmpty()
learner.learn(known, BULBASAUR, 5, 5).learned.shouldBeEmpty()
}
test("an unknown species learns nothing") {
val known = slots(TACKLE, 0, 0, 0)
learner.learn(known, 9999, 1, 100).shouldBeEmpty()
learner.learn(known, 9999, 1, 100).learned.shouldBeEmpty()
}
test("a moveset shorter than four slots grows") {
@@ -76,4 +83,58 @@ class MoveLearnerTest :
learner.learn(known, BULBASAUR, 3, 4)
known.map { it.id } shouldBe listOf(TACKLE, GROWL)
}
test("an offered move takes the slot the player picked") {
val known = slots(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
learner.apply(
known,
listOf(POISON_POWDER, GROWL, LEECH_SEED, VINE_WHIP),
listOf(POISON_POWDER),
) shouldBe true
known.map { it.id } shouldBe listOf(POISON_POWDER, GROWL, LEECH_SEED, VINE_WHIP)
known[0].pp shouldBe moves.get(POISON_POWDER.toInt())!!.pp.toByte()
}
test("the kept moves hold on to their pp") {
val known = mutableListOf(PokemonMove(TACKLE, 7), PokemonMove(GROWL, 3))
learner.apply(known, listOf(POISON_POWDER, GROWL), listOf(POISON_POWDER)) shouldBe true
known.map { it.id to it.pp } shouldBe listOf(POISON_POWDER to 35.toByte(), GROWL to 3)
}
test("declining every offer keeps the moveset") {
val known = slots(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
learner.apply(
known,
listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP),
listOf(POISON_POWDER),
) shouldBe true
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
}
test("a move that was never offered is refused") {
val known = slots(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
learner.apply(
known,
listOf(SLEEP_POWDER, GROWL, LEECH_SEED, VINE_WHIP),
listOf(POISON_POWDER),
) shouldBe false
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
}
test("dropping a move without learning one is refused") {
val known = slots(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
learner.apply(known, listOf(TACKLE, GROWL, LEECH_SEED), listOf(POISON_POWDER)) shouldBe
false
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
}
test("the same move twice is refused") {
val known = slots(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
learner.apply(
known,
listOf(POISON_POWDER, POISON_POWDER, LEECH_SEED, VINE_WHIP),
listOf(POISON_POWDER),
) shouldBe false
known.map { it.id } shouldBe listOf(TACKLE, GROWL, LEECH_SEED, VINE_WHIP)
}
})