From 4470cbda375eaa3a238b79dc9d625615cf0d7867 Mon Sep 17 00:00:00 2001 From: Fiereu Date: Wed, 5 Aug 2026 23:10:59 +0200 Subject: [PATCH] feat(game): let the player replace a move on level up (#90) --- .../fiereu/openmmo/net/game/GameProtocol.kt | 5 +- .../game/packets/SocialEntryFlagSetPacket.kt | 18 ---- .../packets/battle/BattleMoveUsePacket.kt | 18 ---- .../battle/moves/MoveLearnPromptPacket.kt | 17 ++++ .../battle/moves/MoveLearnReplyPacket.kt | 19 +++++ .../openmmo/net/game/MoveLearnPacketsTest.kt | 81 ++++++++++++++++++ .../server/game/battle/BattlePacketEmitter.kt | 10 +++ .../openmmo/server/game/battle/MoveLearner.kt | 53 +++++++++--- .../server/game/handler/GameAppHandler.kt | 4 +- .../server/game/services/BattleService.kt | 39 ++++++++- .../server/game/battle/MoveLearnerTest.kt | 83 ++++++++++++++++--- 11 files changed, 281 insertions(+), 66 deletions(-) delete mode 100644 protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/SocialEntryFlagSetPacket.kt delete mode 100644 protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleMoveUsePacket.kt create mode 100644 protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnPromptPacket.kt create mode 100644 protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnReplyPacket.kt create mode 100644 protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/MoveLearnPacketsTest.kt diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/GameProtocol.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/GameProtocol.kt index 00dedb8..4da2973 100644 --- a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/GameProtocol.kt +++ b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/GameProtocol.kt @@ -6,6 +6,7 @@ import de.fiereu.network.c2s import de.fiereu.network.s2c import de.fiereu.openmmo.net.game.packets.* import de.fiereu.openmmo.net.game.packets.battle.* +import de.fiereu.openmmo.net.game.packets.battle.moves.* import de.fiereu.openmmo.net.game.packets.dialog.* import de.fiereu.openmmo.net.game.packets.guild.* @@ -38,7 +39,7 @@ object GameProtocol : Protocol() { bidi(0x09u, ChatMessagePacketCodec) - c2s(0x0Au, BattleMoveUsePacketCodec) + c2s(0x0Au, MoveLearnReplyPacketCodec) s2c(0x0Au, WorldFlagTableResetPacketCodec) c2s(0x0Bu, EvolutionPromptResponsePacketCodec) @@ -73,7 +74,7 @@ object GameProtocol : Protocol() { s2c(0x16u, BattleEntityDeltaPacketCodec) c2s(0x17u, StorageBoxClosePacketCodec) - s2c(0x17u, SocialEntryFlagSetPacketCodec) + s2c(0x17u, MoveLearnPromptPacketCodec) c2s(0x18u, MonsterStatusConditionPacketCodec) s2c(0x18u, SocialEntryPresencePacketCodec) diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/SocialEntryFlagSetPacket.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/SocialEntryFlagSetPacket.kt deleted file mode 100644 index c0769ed..0000000 --- a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/SocialEntryFlagSetPacket.kt +++ /dev/null @@ -1,18 +0,0 @@ -package de.fiereu.openmmo.net.game.packets - -import de.fiereu.bytecodec.* - -data class SocialEntryFlagSetPacket( - val playerId: Long, - val flag: Byte, - val value: Short, -) - -object SocialEntryFlagSetPacketCodec : PacketCodec() { - override fun CodecScope.body(): SocialEntryFlagSetPacket { - val playerId = field(S64LE) { it.playerId } - val flag = field(S8) { it.flag } - val value = field(S16LE) { it.value } - return SocialEntryFlagSetPacket(playerId, flag, value) - } -} diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleMoveUsePacket.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleMoveUsePacket.kt deleted file mode 100644 index 54fa2c1..0000000 --- a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleMoveUsePacket.kt +++ /dev/null @@ -1,18 +0,0 @@ -package de.fiereu.openmmo.net.game.packets.battle - -import de.fiereu.bytecodec.* - -data class BattleMoveUsePacket( - val targetEntityId: Long, - val moveSlotIndex: Byte, - val moveId: Short, -) - -object BattleMoveUsePacketCodec : PacketCodec() { - override fun CodecScope.body(): BattleMoveUsePacket { - val targetEntityId = field(S64LE) { it.targetEntityId } - val moveSlotIndex = field(S8) { it.moveSlotIndex } - val moveId = field(S16LE) { it.moveId } - return BattleMoveUsePacket(targetEntityId, moveSlotIndex, moveId) - } -} diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnPromptPacket.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnPromptPacket.kt new file mode 100644 index 0000000..a27b17d --- /dev/null +++ b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnPromptPacket.kt @@ -0,0 +1,17 @@ +package de.fiereu.openmmo.net.game.packets.battle.moves + +import de.fiereu.bytecodec.* + +/** Asks the player which move to drop for a new one. */ +data class MoveLearnPromptPacket( + val entityId: Long, + val offered: List, +) + +object MoveLearnPromptPacketCodec : PacketCodec() { + override fun CodecScope.body(): MoveLearnPromptPacket { + val entityId = field(S64LE) { it.entityId } + val offered = field(S16LE.listPrefixed(U8)) { it.offered } + return MoveLearnPromptPacket(entityId, offered) + } +} diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnReplyPacket.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnReplyPacket.kt new file mode 100644 index 0000000..9f9181c --- /dev/null +++ b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/moves/MoveLearnReplyPacket.kt @@ -0,0 +1,19 @@ +package de.fiereu.openmmo.net.game.packets.battle.moves + +import de.fiereu.bytecodec.* + +/** The moveset the player picked. A new move sits in the slot it takes over. */ +data class MoveLearnReplyPacket( + val entityId: Long, + val moveIds: List, + val offered: List, +) + +object MoveLearnReplyPacketCodec : PacketCodec() { + override fun CodecScope.body(): MoveLearnReplyPacket { + val entityId = field(S64LE) { it.entityId } + val moveIds = field(S16LE.listPrefixed(U8)) { it.moveIds } + val offered = field(S16LE.listPrefixed(U8)) { it.offered } + return MoveLearnReplyPacket(entityId, moveIds, offered) + } +} diff --git a/protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/MoveLearnPacketsTest.kt b/protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/MoveLearnPacketsTest.kt new file mode 100644 index 0000000..d20718b --- /dev/null +++ b/protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/MoveLearnPacketsTest.kt @@ -0,0 +1,81 @@ +package de.fiereu.openmmo.net.game + +import de.fiereu.bytecodec.test.decodeBytes +import de.fiereu.bytecodec.test.encodeToBytes +import de.fiereu.openmmo.net.game.packets.battle.moves.MoveLearnPromptPacket +import de.fiereu.openmmo.net.game.packets.battle.moves.MoveLearnPromptPacketCodec +import de.fiereu.openmmo.net.game.packets.battle.moves.MoveLearnReplyPacket +import de.fiereu.openmmo.net.game.packets.battle.moves.MoveLearnReplyPacketCodec +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +private const val ENTITY_ID = 0x1ACEADEF2AC8C000L +private const val FURY_SWIPES: Short = 154 +private const val LICK: Short = 122 +private const val WATER_GUN: Short = 55 +private const val LEER: Short = 43 +private const val UNOVA_MOVE: Short = 526 + +private fun entityIdBytes() = + byteArrayOf( + 0x00, 0xC0.toByte(), 0xC8.toByte(), 0x2A, 0xEF.toByte(), 0xAD.toByte(), 0xCE.toByte(), 0x1A) + +class MoveLearnPacketsTest : + FunSpec({ + test("decodes a captured prompt for one move") { + val bytes = entityIdBytes() + byteArrayOf(0x01, 0x9A.toByte(), 0x00) + val decoded = MoveLearnPromptPacketCodec.decodeBytes(bytes) + decoded shouldBe MoveLearnPromptPacket(ENTITY_ID, listOf(FURY_SWIPES)) + MoveLearnPromptPacketCodec.encodeToBytes(decoded) shouldBe bytes + } + + test("decodes a captured prompt for two moves") { + val bytes = entityIdBytes() + byteArrayOf(0x02, 0x18, 0x02, 0x59, 0x01) + val decoded = MoveLearnPromptPacketCodec.decodeBytes(bytes) + decoded shouldBe MoveLearnPromptPacket(ENTITY_ID, listOf(536, 345)) + MoveLearnPromptPacketCodec.encodeToBytes(decoded) shouldBe bytes + } + + // Fury Swipes took the slot Leer held. + test("decodes a captured reply that swapped a move in") { + val bytes = + entityIdBytes() + + byteArrayOf( + 0x04, + 0x0E, + 0x02, + 0x9A.toByte(), + 0x00, + 0x7A, + 0x00, + 0x37, + 0x00, + 0x01, + 0x9A.toByte(), + 0x00) + val decoded = MoveLearnReplyPacketCodec.decodeBytes(bytes) + decoded shouldBe + MoveLearnReplyPacket( + entityId = ENTITY_ID, + moveIds = listOf(UNOVA_MOVE, FURY_SWIPES, LICK, WATER_GUN), + offered = listOf(FURY_SWIPES), + ) + MoveLearnReplyPacketCodec.encodeToBytes(decoded) shouldBe bytes + } + + // The moveset comes back as it was, so the player kept Leer. + test("decodes a captured reply that declined") { + val bytes = + entityIdBytes() + + byteArrayOf( + 0x04, 0x0E, 0x02, 0x2B, 0x00, 0x7A, 0x00, 0x37, 0x00, 0x01, 0x9A.toByte(), 0x00) + val decoded = MoveLearnReplyPacketCodec.decodeBytes(bytes) + decoded shouldBe + MoveLearnReplyPacket( + entityId = ENTITY_ID, + moveIds = listOf(UNOVA_MOVE, LEER, LICK, WATER_GUN), + offered = listOf(FURY_SWIPES), + ) + MoveLearnReplyPacketCodec.encodeToBytes(decoded) shouldBe bytes + } + }) diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt index c2f8175..df16887 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt @@ -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>, + ppUps: Byte, + ): BattleEntityDeltaPacket = + delta(entityId, DELTA_MOVE_SLOTS).copy(moveSlots = moveSlots, ppUps = ppUps) + fun delta( entityId: Long, mask: Int, diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearner.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearner.kt index 7b7513f..85ae8b8 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearner.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearner.kt @@ -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, + val offered: List, +) + /** 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, 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, + dexId: Int, + fromLevel: Int, + toLevel: Int, + ): MoveLearnOutcome { + val learned = mutableListOf() + val offered = mutableListOf() 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, chosen: List, offered: List): 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 } } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/handler/GameAppHandler.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/handler/GameAppHandler.kt index e493e6b..1181a08 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/handler/GameAppHandler.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/handler/GameAppHandler.kt @@ -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 { event -> guildService.onRankLabelUpdate(event) } on { event -> guildService.onActivityLogPageRequest(event) } - on { event -> battleService.onBattlePacket(event) } + on { event -> battleService.onMoveLearnReply(event) } on { event -> battleService.onBattlePacket(event) } on { event -> battleService.onBattlePacket(event) } on { event -> battleService.onBattleAction(event) } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/BattleService.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/BattleService.kt index 60f4828..99135f1 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/BattleService.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/BattleService.kt @@ -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) + /** * 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() + 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) { + 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) { 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( diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearnerTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearnerTest.kt index 940a4a0..3bc556a 100644 --- a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearnerTest.kt +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/battle/MoveLearnerTest.kt @@ -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) + } })