Exemplo n.º 1
0
        private void MainBattleOnUnitKilled(IBattleManager battle,
                                            BattleManager.BattleSide combatObjectSide,
                                            ICombatGroup combatGroup,
                                            ICombatObject combatObject,
                                            int count)
        {
            IStronghold stronghold;

            if (!gameObjectLocator.TryGetObjects(strongholdId, out stronghold))
            {
                throw new Exception("Stronghold not found");
            }

            var defensiveMeter = battle.GetProperty <decimal>("defense_stronghold_meter");
            var offensiveMeter = battle.GetProperty <decimal>("offense_stronghold_meter");

            if (combatObjectSide == BattleManager.BattleSide.Attack)
            {
                offensiveMeter -= combatObject.Stats.NormalizedCost * count;
            }
            else
            {
                defensiveMeter -= combatObject.Stats.NormalizedCost * count;
            }

            battle.SetProperty("defense_stronghold_meter", defensiveMeter);
            battle.SetProperty("offense_stronghold_meter", offensiveMeter);
        }
Exemplo n.º 2
0
        private void MainBattleOnAboutToExitBattle(IBattleManager battle, ICombatList attackers, ICombatList defenders)
        {
            IStronghold stronghold;

            if (!gameObjectLocator.TryGetObjects(strongholdId, out stronghold))
            {
                throw new Exception("Stronghold not found");
            }

            var defensiveMeter = battle.GetProperty <decimal>("defense_stronghold_meter");
            // Transfer stronghold if
            // - defensive meter is 0
            // - occupied state and there is no one left defending it
            // - neutral state and the attacker killed the main group

            var hasDefendingUnitsLeft = stronghold.Troops.StationedHere().Any(p => p.TotalCount > 0);

            if (defensiveMeter <= 0 ||
                (stronghold.StrongholdState == StrongholdState.Occupied && !hasDefendingUnitsLeft) ||
                (stronghold.StrongholdState == StrongholdState.Neutral && npcGroupKilled))
            {
                strongholdManager.TransferTo(stronghold, stronghold.GateOpenTo);
            }
            else
            {
                strongholdManager.TribeFailedToTakeStronghold(stronghold, stronghold.GateOpenTo);
            }
        }
Exemplo n.º 3
0
        private void MainBattleOnExitTurn(IBattleManager battle, ICombatList attackers, ICombatList defenders, uint turn)
        {
            IStronghold stronghold;

            if (!gameObjectLocator.TryGetObjects(strongholdId, out stronghold))
            {
                throw new Exception("Stronghold not found");
            }

            var defensiveMeter = battle.GetProperty <decimal>("defense_stronghold_meter");
            var offensiveMeter = battle.GetProperty <decimal>("offense_stronghold_meter");

            // Make copy since defenders may change
            var defendersLoopCopy = defenders.ToList();

            // Remove defenders if:
            //  defensive meter is 0
            //  stronghold is still neutral (no tribe)
            //  the defender isnt part of the tribe that owns the stronghold
            foreach (var defender in defendersLoopCopy)
            {
                var cityCombatGroup = defender as CityDefensiveCombatGroup;

                // If cityCombatGroup is null then we're dealing w/ a NPC unit
                if (cityCombatGroup == null)
                {
                    if (defensiveMeter > 0)
                    {
                        continue;
                    }

                    battle.Remove(defender, BattleManager.BattleSide.Defense, ReportState.OutOfStamina);
                }
                // Else we're dealing w/ a player unit
                else
                {
                    if (defensiveMeter > 0 && stronghold.Tribe != null && defender.Tribe == stronghold.Tribe)
                    {
                        continue;
                    }

                    battle.Remove(cityCombatGroup, BattleManager.BattleSide.Defense, ReportState.OutOfStamina);

                    // Dead troops should just be removed immediately
                    if (cityCombatGroup.TroopStub.TotalCount == 0)
                    {
                        stronghold.Troops.RemoveStationed(cityCombatGroup.TroopStub.StationTroopId);
                        cityCombatGroup.TroopStub.City.Troops.Remove(cityCombatGroup.TroopStub.TroopId);
                        continue;
                    }

                    // Defenders need to manually be sent back
                    cityCombatGroup.TroopStub.BeginUpdate();
                    cityCombatGroup.TroopStub.State = TroopState.Stationed;
                    cityCombatGroup.TroopStub.EndUpdate();

                    var troopInitializer   = troopInitializerFactory.CreateStationedTroopObjectInitializer(cityCombatGroup.TroopStub);
                    var retreatChainAction = actionFactory.CreateRetreatChainAction(cityCombatGroup.TroopStub.City.Id, troopInitializer);
                    var result             = cityCombatGroup.TroopStub.City.Worker.DoPassive(cityCombatGroup.TroopStub.City, retreatChainAction, true);
                    if (result != Error.Ok)
                    {
                        throw new Exception("Unexpected failure when retreating a unit from stronghold");
                    }
                }
            }

            // Remove attackers that have quit the tribe or have low meter
            // Make copy since attackers will be changing
            var attackerLoopCopy = attackers.ToList();

            foreach (var attacker in attackerLoopCopy.Where(attacker => offensiveMeter <= 0 || attacker.Tribe != stronghold.GateOpenTo))
            {
                // Remove from battle, no need to send them back since attacking troops have actions to handle that
                battle.Remove(attacker, BattleManager.BattleSide.Attack, ReportState.OutOfStamina);
            }
        }