Exemplo n.º 1
0
        public bool AddStationed(ITroopStub stub)
        {
            if (BaseStation == null)
            {
                throw new Exception("Cannot station in this troop manager");
            }

            int nextId = IdGen.GetNext();

            if (nextId == -1)
            {
                return(false);
            }

            var stationTroopId = (ushort)nextId;

            stub.BeginUpdate();
            stub.StationTroopId = stationTroopId;
            stub.State          = TroopState.Stationed;
            stub.Station        = BaseStation;
            stub.EndUpdate();

            RegisterStub(stationTroopId, stub);
            return(true);
        }
Exemplo n.º 2
0
 private void AddToNormal(ITroopStub source, ITroopStub target)
 {
     target.BeginUpdate();
     foreach (var unit in source.SelectMany(formation => formation))
     {
         target.AddUnit(FormationType.Normal, unit.Key, unit.Value);
     }
     target.EndUpdate();
 }
Exemplo n.º 3
0
        public void DeleteTroopObject()
        {
            if (!stub.City.Troops.Remove(newTroopObject.Stub.TroopId))
            {
                return;
            }

            stub.BeginUpdate();
            stub.AddAllToFormation(FormationType.Defense, newTroopObject.Stub);
            stub.EndUpdate();

            newTroopObject.BeginUpdate();
            world.Regions.Remove(newTroopObject);
            stub.City.ScheduleRemove(newTroopObject, false);
            newTroopObject.EndUpdate();
        }
Exemplo n.º 4
0
        private void BattleActionAttacked(IBattleManager battleManager,
                                          BattleManager.BattleSide attackingSide,
                                          ICombatGroup attackerGroup,
                                          ICombatObject source,
                                          ICombatGroup targetGroup,
                                          ICombatObject target,
                                          decimal damage,
                                          int attackerCount,
                                          int targetCount)
        {
            ICity city;

            if (!gameObjectLocator.TryGetObjects(cityId, out city))
            {
                return;
            }

            var cityCombatTarget = target as CityCombatObject;

            // Check if we should retreat a unit
            if (cityCombatTarget == null || attackingSide == BattleManager.BattleSide.Defense ||
                cityCombatTarget.TroopStub.Station != city || cityCombatTarget.TroopStub.TotalCount <= 0 ||
                cityCombatTarget.TroopStub.TotalCount > cityCombatTarget.TroopStub.RetreatCount)
            {
                return;
            }

            ITroopStub stub = cityCombatTarget.TroopStub;

            // Remove the object from the battle
            city.Battle.Remove(targetGroup, BattleManager.BattleSide.Defense, ReportState.Retreating);
            stub.BeginUpdate();
            stub.State = TroopState.Stationed;
            stub.EndUpdate();

            // Send the defender back to their city
            var troopInitializer   = troopInitializerFactory.CreateStationedTroopObjectInitializer(stub);
            var retreatChainAction = actionFactory.CreateRetreatChainAction(stub.City.Id, troopInitializer);
            var result             = stub.City.Worker.DoPassive(stub.City, retreatChainAction, true);

            if (result != Error.Ok)
            {
                throw new Exception("Unexpected failure when retreating a unit from city");
            }
        }
Exemplo n.º 5
0
        public virtual bool TroopStubCreate(out ITroopStub troopStub,
                                            ICity city,
                                            ISimpleStub stub,
                                            TroopState initialState = TroopState.Idle)
        {
            if (!city.DefaultTroop.RemoveFromFormation(FormationType.Normal, stub))
            {
                troopStub = null;
                return(false);
            }

            troopStub = city.CreateTroopStub();
            troopStub.BeginUpdate();
            troopStub.Add(stub);
            troopStub.State = initialState;
            troopStub.EndUpdate();
            return(true);
        }
Exemplo n.º 6
0
        public virtual uint AddReinforcementToBattle(IBattleManager battleManager, ITroopStub stub, FormationType formationToAddToBattle)
        {
            stub.BeginUpdate();
            stub.Template.LoadStats(TroopBattleGroup.Defense);
            stub.EndUpdate();

            var defensiveGroup = combatGroupFactory.CreateCityDefensiveCombatGroup(battleManager.BattleId,
                                                                                   battleManager.GetNextGroupId(),
                                                                                   stub);

            foreach (var kvp in stub[formationToAddToBattle])
            {
                combatUnitFactory.CreateDefenseCombatUnit(battleManager, stub, formationToAddToBattle, kvp.Key, kvp.Value)
                .ToList()
                .ForEach(defensiveGroup.Add);
            }
            battleManager.Add(defensiveGroup, BattleManager.BattleSide.Defense, true);

            return(defensiveGroup.Id);
        }
Exemplo n.º 7
0
        private string CmdStrongholdAddTroop(Session session, string[] parms)
        {
            bool   help           = false;
            string strongholdName = string.Empty;
            string cityName       = string.Empty;

            try
            {
                var p = new OptionSet
                {
                    { "?|help|h", v => help = true },
                    { "stronghold=", v => strongholdName = v.TrimMatchingQuotes() },
                    { "city=", v => cityName = v.TrimMatchingQuotes() },
                };
                p.Parse(parms);
            }
            catch (Exception)
            {
                help = true;
            }

            if (help || string.IsNullOrEmpty(strongholdName) || string.IsNullOrEmpty(cityName))
            {
                return("StrongholdAddTroop --stronghold=tribe_name --city=city_name");
            }

            uint cityId;

            if (!world.Cities.FindCityId(cityName, out cityId))
            {
                return("City not found");
            }

            ICity city;

            if (!world.TryGetObjects(cityId, out city))
            {
                return("City not found");
            }

            IStronghold stronghold;

            if (!strongholdManager.TryGetStronghold(strongholdName, out stronghold))
            {
                return("Stronghold not found");
            }

            return(locker.Lock(city, stronghold).Do(() =>
            {
                if (city.DefaultTroop.Upkeep == 0)
                {
                    return "No troops in the city!";
                }

                ITroopStub stub = city.CreateTroopStub();
                stub.BeginUpdate();
                stub.AddFormation(FormationType.Defense);
                foreach (var unit in city.DefaultTroop[FormationType.Normal])
                {
                    stub.AddUnit(FormationType.Defense, unit.Key, unit.Value);
                }
                stub.Template.LoadStats(TroopBattleGroup.Defense);
                stub.EndUpdate();

                if (!stronghold.Troops.AddStationed(stub))
                {
                    return "Error Adding to Station";
                }

                return "OK!";
            }));
        }