From edde3b16bf3ed2c2a8e853d8900a25d9005d0e19 Mon Sep 17 00:00:00 2001 From: Mr4Goosey Date: Tue, 17 Mar 2026 22:31:58 +0100 Subject: [PATCH] Add configurable XP distribution for fainted pokemon and battle losses Currently, if a pokemon faints during battle, it receives no XP for any opponents it helped defeat - that XP simply vanishes. This is especially punishing in trainer battles where pokemon cycling is common. This adds faint-order tracking so that pokemon who contributed to a kill can still receive XP, as long as the opponent fainted before they did. It also adds support for awarding XP on battle losses or forfeits for opponents that were defeated during the battle. Both behaviors are behind new config options (default off): - awardExperienceToFaintedPokemon: fainted pokemon receive XP for opponents they helped defeat before fainting - awardExperienceOnBattleLoss: pokemon receive XP for opponents defeated even if the battle is lost or forfeited --- .../common/api/battles/model/PokemonBattle.kt | 21 ++++++++++++++++--- .../instructions/FaintInstruction.kt | 1 + .../common/battles/pokemon/BattlePokemon.kt | 3 +++ .../mod/common/config/CobblemonConfig.kt | 6 ++++++ .../assets/cobblemon/lang/en_us.json | 4 ++++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt index 6de5e88286..46605d1bc5 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/battles/model/PokemonBattle.kt @@ -137,6 +137,9 @@ open class PokemonBattle( val captureActions = mutableListOf() val majorBattleActions = hashMapOf() + + /** Monotonically increasing counter to track faint ordering within this battle. */ + var faintCounter = 0 val minorBattleActions = hashMapOf() val contextManager = ContextManager() @@ -244,12 +247,23 @@ open class PokemonBattle( fun end() { ended = true + val awardToFainted = Cobblemon.config.awardExperienceToFaintedPokemon + val awardOnLoss = Cobblemon.config.awardExperienceOnBattleLoss this.actors.forEach { actor -> val faintedPokemons = actor.pokemonList.filter { it.health <= 0 } actor.getSide().getOppositeSide().actors.forEach { opponent -> - val opponentNonFaintedPokemons = opponent.pokemonList.filter { it.health > 0 } + val opponentPokemons = if (awardToFainted) opponent.pokemonList else opponent.pokemonList.filter { it.health > 0 } faintedPokemons.forEach { faintedPokemon -> - for (opponentPokemon in opponentNonFaintedPokemons) { + for (opponentPokemon in opponentPokemons) { + // Faint-order check: if awarding XP to fainted pokemon, only award for enemies that fainted before this pokemon did + if (awardToFainted) { + val enemyFaintedAt = faintedPokemon.faintedAt ?: continue + val opponentFaintedAt = opponentPokemon.faintedAt + if (opponentFaintedAt != null && opponentFaintedAt <= enemyFaintedAt) { + continue + } + } + val facedFainted = opponentPokemon.facedOpponents.contains(faintedPokemon) val pokemon = opponentPokemon.effectedPokemon if (facedFainted) { @@ -270,7 +284,8 @@ open class PokemonBattle( else -> continue } val experience = Cobblemon.experienceCalculator.calculate(opponentPokemon, faintedPokemon, multiplier) - if (experience > 0 && actor.pokemonList.all { it.health <= 0 }) { + val enemyTeamWiped = actor.pokemonList.all { it.health <= 0 } + if (experience > 0 && (enemyTeamWiped || awardOnLoss)) { opponent.awardExperience(opponentPokemon, experience) } Cobblemon.evYieldCalculator.calculate(opponentPokemon, faintedPokemon).forEach { (stat, amount) -> diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FaintInstruction.kt b/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FaintInstruction.kt index 5ecad11776..6ba0ef00e7 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FaintInstruction.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FaintInstruction.kt @@ -51,6 +51,7 @@ class FaintInstruction(battle: PokemonBattle, val message: BattleMessage) : Inte preamble.thenAccept { faintingPokemon.effectedPokemon.currentHealth = 0 + faintingPokemon.faintedAt = battle.faintCounter++ faintingPokemon.sendUpdate() CobblemonEvents.BATTLE_FAINTED.post(BattleFaintedEvent(battle, pokemon, context, pnx)) battle.getActorAndActiveSlotFromPNX(pnx).second.battlePokemon = null diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/battles/pokemon/BattlePokemon.kt b/common/src/main/kotlin/com/cobblemon/mod/common/battles/pokemon/BattlePokemon.kt index 05c0ae285f..cc50470ad7 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/battles/pokemon/BattlePokemon.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/battles/pokemon/BattlePokemon.kt @@ -110,6 +110,9 @@ open class BattlePokemon( /** A set of all the BattlePokemon that they faced during the battle (for exp purposes) */ val facedOpponents = mutableSetOf() + /** The faint sequence number within the battle, or null if this pokemon has not fainted. */ + var faintedAt: Int? = null + /** * The [HeldItemManager] backing this [BattlePokemon]. */ diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/config/CobblemonConfig.kt b/common/src/main/kotlin/com/cobblemon/mod/common/config/CobblemonConfig.kt index d5b1476244..c0db6fa7ed 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/config/CobblemonConfig.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/config/CobblemonConfig.kt @@ -146,6 +146,12 @@ class CobblemonConfig { @CobblemonConfigField(Category.Battles, lang = "experience_share_multiplier", SERVER) var experienceShareMultiplier = .5 + @CobblemonConfigField(Category.Battles, lang = "award_experience_to_fainted_pokemon", SERVER) + var awardExperienceToFaintedPokemon = false + + @CobblemonConfigField(Category.Battles, lang = "award_experience_on_battle_loss", SERVER) + var awardExperienceOnBattleLoss = false + @CobblemonConfigField(Category.Battles, lang = "lucky_egg_multiplier", SERVER) var luckyEggMultiplier = 1.5 diff --git a/common/src/main/resources/assets/cobblemon/lang/en_us.json b/common/src/main/resources/assets/cobblemon/lang/en_us.json index 14c613bfdf..7da98ac3ef 100644 --- a/common/src/main/resources/assets/cobblemon/lang/en_us.json +++ b/common/src/main/resources/assets/cobblemon/lang/en_us.json @@ -2494,6 +2494,10 @@ "cobblemon.config.ui.allow_experience_from_pvp.tooltip": "Whether to allow Exp. gain from PvP battles.", "cobblemon.config.ui.experience_share_multiplier": "Experience Share Multiplier", "cobblemon.config.ui.experience_share_multiplier.tooltip": "Multiplier of how much of the total Exp is given to a Pokémon holding an Exp. Share.", + "cobblemon.config.ui.award_experience_to_fainted_pokemon": "Award Exp. to Fainted Pokémon", + "cobblemon.config.ui.award_experience_to_fainted_pokemon.tooltip": "Whether fainted Pokémon receive Exp. for opponents they helped defeat before fainting.", + "cobblemon.config.ui.award_experience_on_battle_loss": "Award Exp. on Battle Loss", + "cobblemon.config.ui.award_experience_on_battle_loss.tooltip": "Whether Pokémon receive Exp. for opponents they defeated even if the battle is lost. Requires 'Award Exp. to Fainted Pokémon' to be enabled for fainted Pokémon to benefit.", "cobblemon.config.ui.lucky_egg_multiplier": "Lucky Egg Multiplier", "cobblemon.config.ui.lucky_egg_multiplier.tooltip": "Multiplier of how much of the total Exp is given to a Pokémon holding a Lucky Egg.", "cobblemon.config.ui.allow_spectating": "Allow Spectating",