From 42dff5584e25dd7230fe0f0799acf038249ebe7f Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:27:28 +0200 Subject: [PATCH] "can be promoted" notification only when it's actually new (#9230) * "can be promoted" notification only when it's actually new * Reviews - sumOf instead of fold --- core/src/com/unciv/logic/battle/Battle.kt | 55 ++++++++++++----------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/core/src/com/unciv/logic/battle/Battle.kt b/core/src/com/unciv/logic/battle/Battle.kt index 6eb7412d53..8369ff54e8 100644 --- a/core/src/com/unciv/logic/battle/Battle.kt +++ b/core/src/com/unciv/logic/battle/Battle.kt @@ -523,43 +523,48 @@ object Battle { // XP! private fun addXp(thisCombatant: ICombatant, amount: Int, otherCombatant: ICombatant) { - var baseXP = amount if (thisCombatant !is MapUnitCombatant) return - val modConstants = thisCombatant.unit.civ.gameInfo.ruleset.modOptions.constants - if (thisCombatant.unit.promotions.totalXpProduced() >= modConstants.maxXPfromBarbarians - && otherCombatant.getCivInfo().isBarbarian()) + val civ = thisCombatant.getCivInfo() + val otherIsBarbarian = otherCombatant.getCivInfo().isBarbarian() + val promotions = thisCombatant.unit.promotions + val modConstants = civ.gameInfo.ruleset.modOptions.constants + + if (otherIsBarbarian && promotions.totalXpProduced() >= modConstants.maxXPfromBarbarians) return + val unitCouldAlreadyPromote = promotions.canBePromoted() - val stateForConditionals = StateForConditionals(civInfo = thisCombatant.getCivInfo(), ourCombatant = thisCombatant, theirCombatant = otherCombatant) + val stateForConditionals = StateForConditionals(civInfo = civ, ourCombatant = thisCombatant, theirCombatant = otherCombatant) - for (unique in thisCombatant.getMatchingUniques(UniqueType.FlatXPGain, stateForConditionals, true)) - baseXP += unique.params[0].toInt() + val baseXP = amount + thisCombatant + .getMatchingUniques(UniqueType.FlatXPGain, stateForConditionals, true) + .sumOf { it.params[0].toInt() } - var xpModifier = 1f - - for (unique in thisCombatant.getMatchingUniques(UniqueType.PercentageXPGain, stateForConditionals, true)) - xpModifier += unique.params[0].toFloat() / 100 + val xpBonus = thisCombatant + .getMatchingUniques(UniqueType.PercentageXPGain, stateForConditionals, true) + .sumOf { it.params[0].toDouble() } + val xpModifier = 1.0 + xpBonus / 100 val xpGained = (baseXP * xpModifier).toInt() - thisCombatant.unit.promotions.XP += xpGained + promotions.XP += xpGained - - if (thisCombatant.getCivInfo().isMajorCiv() && !otherCombatant.getCivInfo().isBarbarian()) { // Can't get great generals from Barbarians - var greatGeneralPointsModifier = 1f - for (unique in thisCombatant.getMatchingUniques(UniqueType.GreatPersonEarnedFaster, stateForConditionals, true)) { - val unitName = unique.params[0] - // From the unique we know this unit exists - val unit = thisCombatant.getCivInfo().gameInfo.ruleset.units[unitName]!! - if (unit.uniques.contains("Great Person - [War]")) - greatGeneralPointsModifier += unique.params[1].toFloat() / 100 - } + if (!otherIsBarbarian && civ.isMajorCiv()) { // Can't get great generals from Barbarians + val greatGeneralPointsBonus = thisCombatant + .getMatchingUniques(UniqueType.GreatPersonEarnedFaster, stateForConditionals, true) + .filter { unique -> + val unitName = unique.params[0] + // From the unique we know this unit exists + val unit = civ.gameInfo.ruleset.units[unitName]!! + unit.uniques.contains("Great Person - [War]") + } + .sumOf { it.params[1].toDouble() } + val greatGeneralPointsModifier = 1.0 + greatGeneralPointsBonus / 100 val greatGeneralPointsGained = (xpGained * greatGeneralPointsModifier).toInt() - thisCombatant.getCivInfo().greatPeople.greatGeneralPoints += greatGeneralPointsGained + civ.greatPeople.greatGeneralPoints += greatGeneralPointsGained } - if (!thisCombatant.isDefeated() && thisCombatant.unit.promotions.canBePromoted()) - thisCombatant.getCivInfo().addNotification("[${thisCombatant.unit.displayName()}] can be promoted!",thisCombatant.getTile().position, NotificationCategory.Units, thisCombatant.unit.name) + if (!thisCombatant.isDefeated() && !unitCouldAlreadyPromote && promotions.canBePromoted()) + civ.addNotification("[${thisCombatant.unit.displayName()}] can be promoted!",thisCombatant.getTile().position, NotificationCategory.Units, thisCombatant.unit.name) } private fun conquerCity(city: City, attacker: MapUnitCombatant) {