Exemple #1
0
        private void AfterTroopMoved(ActionState state)
        {
            if (state == ActionState.Completed)
            {
                ICity        city;
                ICity        targetCity;
                ITroopObject troopObject;

                if (!gameObjectLocator.TryGetObjects(cityId, troopObjectId, out city, out troopObject))
                {
                    throw new Exception("City or troop object is missing");
                }

                if (!gameObjectLocator.TryGetObjects(targetCityId, out targetCity))
                {
                    //If the target is missing, walk back
                    locker.Lock(city).Do(() =>
                    {
                        TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id,
                                                                                                troopObject.ObjectId,
                                                                                                city.PrimaryPosition.X,
                                                                                                city.PrimaryPosition.Y,
                                                                                                true,
                                                                                                true);
                        ExecuteChainAndWait(tma, AfterTroopMovedHome);
                    });
                    return;
                }

                // Get all of the stationed city id's from the target city since they will be used by the engage attack action
                CallbackLock.CallbackLockHandler lockAllStationed = delegate
                {
                    return(targetCity.Troops.StationedHere()
                           .Select(stationedStub => stationedStub.City)
                           .Cast <ILockable>()
                           .ToArray());
                };

                locker.Lock(lockAllStationed, null, city, targetCity).Do(() =>
                {
                    var bea = actionFactory.CreateCityEngageAttackPassiveAction(cityId,
                                                                                troopObject.ObjectId,
                                                                                targetCityId);
                    ExecuteChainAndWait(bea, AfterBattle);
                });
            }
        }
Exemple #2
0
        private void AfterTroopMoved(ActionState state)
        {
            if (state == ActionState.Fired)
            {
                // Verify the target is still good, otherwise we walk back immediately
                ICity        city;
                IStronghold  targetStronghold;
                ITroopObject troopObject;

                if (!gameObjectLocator.TryGetObjects(cityId, troopObjectId, out city, out troopObject))
                {
                    throw new Exception("City or troop object is missing");
                }

                if (!gameObjectLocator.TryGetObjects(targetStrongholdId, out targetStronghold))
                {
                    CancelCurrentChain();
                    return;
                }

                /*
                 * Commenting out this logic to return if stronghold is not attackable/defendable until we are able to correctly cancel assignments. At the moment,
                 * there are cases where when troops are in an assignment and a stronghold is taken over,
                 * the ones that are on the walk will return and then the ones that are dispatched will be sent to the stronghold.
                 *
                 * bool invalidTarget;
                 * using (locker.Lock(city, targetStronghold))
                 * {
                 *  invalidTarget = strongholdBattleProcedure.CanStrongholdBeAttacked(city, targetStronghold) != Error.Ok &&
                 *                  strongholdBattleProcedure.CanStrongholdBeDefended(city, targetStronghold) != Error.Ok;
                 * }
                 */

                // If player is no longer in a tribe, return
                if (!city.Owner.IsInTribe)
                {
                    CancelCurrentChain();
                }

                return;
            }

            if (state == ActionState.Failed)
            {
                // If TroopMove failed it's because we cancelled it and the target is invalid. Walk back home
                ICity        city;
                ITroopObject troopObject;

                locker.Lock(cityId, troopObjectId, out city, out troopObject).Do(() =>
                {
                    // Remove from remote stronghold and add only to this troop
                    city.Notifications.Remove(this);
                    city.Notifications.Add(troopObject, this);

                    TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id,
                                                                                            troopObject.ObjectId,
                                                                                            city.PrimaryPosition.X,
                                                                                            city.PrimaryPosition.Y,
                                                                                            true,
                                                                                            true);
                    ExecuteChainAndWait(tma, AfterTroopMovedHome);
                });

                return;
            }

            if (state == ActionState.Completed)
            {
                ICity        city;
                IStronghold  targetStronghold;
                ITroopObject troopObject;

                if (!gameObjectLocator.TryGetObjects(cityId, troopObjectId, out city, out troopObject))
                {
                    throw new Exception("City or troop object is missing");
                }

                if (!gameObjectLocator.TryGetObjects(targetStrongholdId, out targetStronghold))
                {
                    throw new Exception("Stronghold is missing");
                }

                CallbackLock.CallbackLockHandler lockAll = delegate { return(targetStronghold.LockList().ToArray()); };

                locker.Lock(lockAll, null, city, targetStronghold).Do(() =>
                {
                    if (city.Owner.IsInTribe)
                    {
                        if (targetStronghold.Tribe == city.Owner.Tribesman.Tribe)
                        {
                            if (targetStronghold.MainBattle != null)
                            {
                                MoveFromAttackToDefenseFormation(troopObject);
                                battleProcedure.AddReinforcementToBattle(targetStronghold.MainBattle, troopObject.Stub, FormationType.Defense);
                                StationTroopInStronghold(troopObject, targetStronghold, TroopState.BattleStationed);
                            }
                            else
                            {
                                StationTroopInStronghold(troopObject, targetStronghold);
                            }

                            return;
                        }

                        if (targetStronghold.GateOpenTo == city.Owner.Tribesman.Tribe)
                        {
                            // If stronghold's gate is open to the tribe, then it should engage the stronghold
                            JoinOrCreateStrongholdMainBattle(city);
                            return;
                        }

                        if (targetStronghold.GateOpenTo == null)
                        {
                            // If gate isn't open to anyone then engage the gate
                            var bea = actionFactory.CreateStrongholdEngageGateAttackPassiveAction(cityId,
                                                                                                  troopObject.ObjectId,
                                                                                                  targetStrongholdId);
                            ExecuteChainAndWait(bea, AfterGateBattle);
                            return;
                        }
                    }

                    city.Notifications.Remove(this);
                    city.Notifications.Add(troopObject, this);

                    // Walk back to city if none of the above conditions apply
                    TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id,
                                                                                            troopObject.ObjectId,
                                                                                            city.PrimaryPosition.X,
                                                                                            city.PrimaryPosition.Y,
                                                                                            true,
                                                                                            true);
                    ExecuteChainAndWait(tma, AfterTroopMovedHome);
                });
            }
        }
Exemple #3
0
        private void AfterTroopMoved(ActionState state)
        {
            if (state == ActionState.Fired)
            {
                // Verify the target is still good, otherwise we walk back immediately

                ICity        city;
                IStronghold  targetStronghold;
                ITroopObject troopObject;

                if (!gameObjectLocator.TryGetObjects(cityId, troopObjectId, out city, out troopObject))
                {
                    throw new Exception("City or troop object is missing");
                }

                if (!gameObjectLocator.TryGetObjects(targetStrongholdId, out targetStronghold))
                {
                    CancelCurrentChain();
                    return;
                }

                bool invalidTarget = false;
                locker.Lock(city, targetStronghold).Do(() =>
                {
                    invalidTarget = strongholdBattleProcedure.CanStrongholdBeDefended(city, targetStronghold) != Error.Ok;
                });

                // If the stronghold is not there or we are unable to attack/defense it, then cancel the current TroopMoveAction
                if (invalidTarget)
                {
                    CancelCurrentChain();
                }
            }
            else if (state == ActionState.Failed)
            {
                // If TroopMove failed it's because we cancelled it and the target is invalid. Walk back home
                ICity        city;
                ITroopObject troopObject;

                locker.Lock(cityId, troopObjectId, out city, out troopObject).Do(() =>
                {
                    TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id,
                                                                                            troopObject.ObjectId,
                                                                                            city.PrimaryPosition.X,
                                                                                            city.PrimaryPosition.Y,
                                                                                            true,
                                                                                            false);
                    ExecuteChainAndWait(tma, AfterTroopMovedHome);
                });
            }
            else if (state == ActionState.Completed)
            {
                ICity        city;
                IStronghold  targetStronghold;
                ITroopObject troopObject;

                if (!gameObjectLocator.TryGetObjects(cityId, troopObjectId, out city, out troopObject))
                {
                    throw new Exception("City or troop object is missing");
                }

                if (!gameObjectLocator.TryGetObjects(targetStrongholdId, out targetStronghold))
                {
                    throw new Exception("Stronghold is missing");
                }

                CallbackLock.CallbackLockHandler lockAll = delegate { return(targetStronghold.LockList().ToArray()); };

                locker.Lock(lockAll, null, city, targetStronghold).Do(() =>
                {
                    if (targetStronghold.Tribe == city.Owner.Tribesman.Tribe)
                    {
                        if (targetStronghold.MainBattle != null)
                        {
                            battleProcedure.AddReinforcementToBattle(targetStronghold.MainBattle, troopObject.Stub, FormationType.Defense);
                            StationTroopInStronghold(troopObject, targetStronghold, TroopState.BattleStationed);
                        }
                        else
                        {
                            StationTroopInStronghold(troopObject, targetStronghold);
                        }

                        return;
                    }

                    // Walk back to city if we dont own it anymore
                    TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id,
                                                                                            troopObject.ObjectId,
                                                                                            city.PrimaryPosition.X,
                                                                                            city.PrimaryPosition.Y,
                                                                                            true,
                                                                                            false);
                    ExecuteChainAndWait(tma, AfterTroopMovedHome);
                });
            }
        }
Exemple #4
0
        private void AfterTroopMoved(ActionState state)
        {
            if (state == ActionState.Fired)
            {
                IBarbarianTribe targetBarbarianTribe;
                // Verify the target is still good, otherwise we walk back immediately
                if (!gameObjectLocator.TryGetObjects(targetObjectId, out targetBarbarianTribe) || targetBarbarianTribe.CampRemains == 0)
                {
                    CancelCurrentChain();
                }

                return;
            }

            if (state == ActionState.Failed)
            {
                // If TroopMove failed it's because we cancelled it and the target is invalid. Walk back home
                ICity        city;
                ITroopObject troopObject;

                locker.Lock(cityId, troopObjectId, out city, out troopObject).Do(() =>
                {
                    TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id, troopObject.ObjectId, city.PrimaryPosition.X, city.PrimaryPosition.Y, true, true);
                    ExecuteChainAndWait(tma, AfterTroopMovedHome);
                });

                return;
            }

            if (state == ActionState.Completed)
            {
                ICity           city;
                IBarbarianTribe targetBarbarianTribe;
                ITroopObject    troopObject;

                if (!gameObjectLocator.TryGetObjects(cityId, troopObjectId, out city, out troopObject))
                {
                    throw new Exception("City or troop object is missing");
                }

                if (!gameObjectLocator.TryGetObjects(targetObjectId, out targetBarbarianTribe))
                {
                    //If the target is missing, walk back
                    locker.Lock(city).Do(() =>
                    {
                        TroopMovePassiveAction tma = actionFactory.CreateTroopMovePassiveAction(city.Id,
                                                                                                troopObject.ObjectId,
                                                                                                city.PrimaryPosition.X,
                                                                                                city.PrimaryPosition.Y,
                                                                                                true,
                                                                                                true);
                        ExecuteChainAndWait(tma, AfterTroopMovedHome);
                    });

                    return;
                }

                locker.Lock(city, targetBarbarianTribe).Do(() =>
                {
                    var bea = actionFactory.CreateBarbarianTribeEngageAttackPassiveAction(cityId, troopObject.ObjectId, targetObjectId);
                    ExecuteChainAndWait(bea, AfterBattle);
                });
            }
        }