// TODO: track total cost, and only upgrade if player can afford X number of this upgrade to prevent spending all their money. public static void UpgradeParty(PartyBase party) { var memberRoster = party.MemberRoster; var troopUpgradeMetadataCollection = new List <TroopUpgradeMetadata>(); for (var memberIndex = 0; memberIndex < memberRoster.Count; ++memberIndex) { var elementCopyAtIndex = memberRoster.GetElementCopyAtIndex(memberIndex); if (!elementCopyAtIndex.HasHigherTierToUpgradeTo()) { continue; } var upgradeXpCostPerUnit = elementCopyAtIndex.Character.UpgradeXpCost; var totalUnitsReadyToUpgradeInElement = elementCopyAtIndex.NumberReadyToUpgrade; for (var upgradeTargetIndex = 0; upgradeTargetIndex < elementCopyAtIndex.Character.UpgradeTargets.Length; ++upgradeTargetIndex) { var totalUnitsToUpgradeInElement = totalUnitsReadyToUpgradeInElement; var upgradeTarget = elementCopyAtIndex.Character.UpgradeTargets[upgradeTargetIndex]; // the higher tier unit type to upgrade to var costToUpgradeToHigherTierPerUnit = elementCopyAtIndex.Character.UpgradeCost(party, upgradeTargetIndex); var totalCostToUpgradeToHigherTier = totalUnitsToUpgradeInElement * costToUpgradeToHigherTierPerUnit; if (party.LeaderHero != null && costToUpgradeToHigherTierPerUnit != 0 && totalCostToUpgradeToHigherTier > party.LeaderHero.Gold) { totalUnitsToUpgradeInElement = party.LeaderHero.Gold / costToUpgradeToHigherTierPerUnit; } var canUpgrade = true; if (elementCopyAtIndex.Character.UpgradeTargets[upgradeTargetIndex].UpgradeRequiresItemFromCategory != null) { canUpgrade = false; var totalItemsAvailableToSupplyTroopUpgrade = 0; foreach (var itemRosterElement in party.ItemRoster) { if (itemRosterElement.EquipmentElement.Item.ItemCategory == upgradeTarget.UpgradeRequiresItemFromCategory) { totalItemsAvailableToSupplyTroopUpgrade += itemRosterElement.Amount; canUpgrade = true; if (totalItemsAvailableToSupplyTroopUpgrade >= totalUnitsToUpgradeInElement) { break; } } } if (canUpgrade) { totalUnitsToUpgradeInElement = Math.Min(totalItemsAvailableToSupplyTroopUpgrade, totalUnitsToUpgradeInElement); } } if (party.Culture.IsBandit) { canUpgrade = elementCopyAtIndex.Character.UpgradeTargets[upgradeTargetIndex].Culture.IsBandit; } if (canUpgrade && totalUnitsToUpgradeInElement > 0) { troopUpgradeMetadataCollection.Add( new TroopUpgradeMetadata( memberIndex, elementCopyAtIndex.Character.UpgradeTargets[upgradeTargetIndex], totalUnitsToUpgradeInElement, costToUpgradeToHigherTierPerUnit ) ); } } if (troopUpgradeMetadataCollection.Any()) { var randomElement = troopUpgradeMetadataCollection.GetRandomElement(); var upgradedTierCharacterObject = randomElement.CharacterUpgradeTarget; var totalUnitsToUpgradeInElement = randomElement.TotalUnitsToUpgradeInElement; var costToUpgradeToHigherTierPerUnit = randomElement.CostToUpgradeToHigherTierPerUnit; var totalUpgradeXpCost = upgradeXpCostPerUnit * totalUnitsToUpgradeInElement; memberRoster.SetElementXp(memberIndex, memberRoster.GetElementXp(memberIndex) - totalUpgradeXpCost); memberRoster.AddToCounts(elementCopyAtIndex.Character, -totalUnitsToUpgradeInElement); memberRoster.AddToCounts(upgradedTierCharacterObject, totalUnitsToUpgradeInElement); if (upgradedTierCharacterObject.UpgradeRequiresItemFromCategory != null) { var newTotalUnitsToUpgradeInElement = totalUnitsToUpgradeInElement; foreach (var itemRosterElement in party.ItemRoster) { if (itemRosterElement.EquipmentElement.Item.ItemCategory == upgradedTierCharacterObject.UpgradeRequiresItemFromCategory) { var totalUnitsToUpgradeInElementDueToItemRequirements = Math.Min(newTotalUnitsToUpgradeInElement, itemRosterElement.Amount); party.ItemRoster.AddToCounts(itemRosterElement.EquipmentElement.Item, -totalUnitsToUpgradeInElementDueToItemRequirements); newTotalUnitsToUpgradeInElement -= totalUnitsToUpgradeInElementDueToItemRequirements; if (newTotalUnitsToUpgradeInElement == 0) { break; } } } } var totalCostToUpgradeUnitsInElement = costToUpgradeToHigherTierPerUnit * totalUnitsToUpgradeInElement; if (party.Owner.Gold < totalCostToUpgradeUnitsInElement) { totalUnitsToUpgradeInElement = party.Owner.Gold / costToUpgradeToHigherTierPerUnit; } if (totalUnitsToUpgradeInElement > 0) { if (party.Owner != null) { SkillLevelingManager.OnUpgradeTroops(party, upgradedTierCharacterObject, totalUnitsToUpgradeInElement); GiveGoldAction.ApplyBetweenCharacters(party.Owner, null, totalCostToUpgradeUnitsInElement, true); } else if (party.LeaderHero != null) { SkillLevelingManager.OnUpgradeTroops(party, upgradedTierCharacterObject, totalUnitsToUpgradeInElement); GiveGoldAction.ApplyBetweenCharacters(party.LeaderHero, null, totalCostToUpgradeUnitsInElement, true); } } } } }
public static void UpgradeReadyTroops(PartyBase party) { var memberRoster = party.MemberRoster; var troopUpgradeModel = Campaign.Current.Models.PartyTroopUpgradeModel; var cavalryRatioAtParty = party.MobileParty == null || party.MobileParty.IsGarrison || party.MobileParty.IsMilitia || party.MobileParty.IsVillager ? 1f : party.NumberOfMenWithHorse / (party.NumberOfAllMembers + 0.1f); for (var index1 = 0; index1 < memberRoster.Count; ++index1) { var elementCopyAtIndex = memberRoster.GetElementCopyAtIndex(index1); if (troopUpgradeModel.IsTroopUpgradeable(party, elementCopyAtIndex.Character)) { var characterObjects = new List <Tuple <CharacterObject, int, int> >(); var numberReadyToUpgrade = elementCopyAtIndex.NumberReadyToUpgrade; var upgradeXpCost = elementCopyAtIndex.Character.UpgradeXpCost; if (numberReadyToUpgrade > elementCopyAtIndex.Number - elementCopyAtIndex.WoundedNumber) { numberReadyToUpgrade = elementCopyAtIndex.Number - elementCopyAtIndex.WoundedNumber; } if (numberReadyToUpgrade > 0) { for (var index2 = 0; index2 < elementCopyAtIndex.Character.UpgradeTargets.Length; ++index2) { var upgradeTarget = elementCopyAtIndex.Character.UpgradeTargets[index2]; var upgradeGoldCost = elementCopyAtIndex.Character.UpgradeCost(party, index2); if (party.LeaderHero != null && upgradeGoldCost != 0 && numberReadyToUpgrade * upgradeGoldCost > party.LeaderHero.Gold) { numberReadyToUpgrade = party.LeaderHero.Gold / upgradeGoldCost; } if (party.Owner != null && elementCopyAtIndex.Character.UpgradeTargets[index2].UpgradeRequiresItemFromCategory != null) { var flag = false; var itemCount = 0; foreach (var itemRosterElement in party.ItemRoster) { if (itemRosterElement.EquipmentElement.Item.ItemCategory == upgradeTarget.UpgradeRequiresItemFromCategory) { itemCount += itemRosterElement.Amount; flag = true; if (itemCount >= numberReadyToUpgrade) { break; } } } if (flag) { numberReadyToUpgrade = Math.Min(itemCount, numberReadyToUpgrade); } } //if (party.Culture.IsBandit) // flag = elementCopyAtIndex.Character.UpgradeTargets[index2].Culture.IsBandit; //if (elementCopyAtIndex.Character.Occupation == Occupation.Bandit) // flag = troopUpgradeModel.CanPartyUpgradeTroopToTarget(party, elementCopyAtIndex.Character, elementCopyAtIndex.Character.UpgradeTargets[index2]); if (numberReadyToUpgrade > 0) { characterObjects.Add(new Tuple <CharacterObject, int, int>(elementCopyAtIndex.Character.UpgradeTargets[index2], numberReadyToUpgrade, upgradeGoldCost)); } } if (characterObjects.Count > 0) { var character = characterObjects.GetRandomElement(); if (party.IsMobile && party.LeaderHero != null && cavalryRatioAtParty < 0.360000014305115) { var num2 = 0f; foreach (var tuple2 in characterObjects) { num2 += CalculateUpgradeChance(tuple2.Item1, 1f, cavalryRatioAtParty); } var num3 = num2 * MBRandom.RandomFloat; foreach (var tuple2 in characterObjects) { num3 -= CalculateUpgradeChance(tuple2.Item1, 1f, cavalryRatioAtParty); if (num3 < 0.0) { character = tuple2; break; } } } var characterObject = character.Item1; var numberToUpgrade = character.Item2; var upgradeGoldCost = character.Item3; var totalXpCost = upgradeXpCost * numberToUpgrade; memberRoster.SetElementXp(index1, memberRoster.GetElementXp(index1) - totalXpCost); memberRoster.AddToCounts(elementCopyAtIndex.Character, -numberToUpgrade); memberRoster.AddToCounts(characterObject, numberToUpgrade); var gold = party.Owner?.Gold; var totalGoldCost = upgradeGoldCost * numberToUpgrade; if ((gold.GetValueOrDefault() < totalGoldCost) & gold.HasValue) { numberToUpgrade = party.Owner.Gold / upgradeGoldCost; } if (numberToUpgrade > 0) { if (party.Owner != null) { SkillLevelingManager.OnUpgradeTroops(party, characterObject, numberToUpgrade); GiveGoldAction.ApplyBetweenCharacters(party.Owner, null, upgradeGoldCost * numberToUpgrade, true); } else if (party.LeaderHero != null) { SkillLevelingManager.OnUpgradeTroops(party, characterObject, numberToUpgrade); GiveGoldAction.ApplyBetweenCharacters(party.LeaderHero, null, upgradeGoldCost * numberToUpgrade, true); } } } } } } }