Ejemplo n.º 1
0
        public void MoveFromBattleToNormal(CityBattleProcedure cityBattleProcedure, IGlobal global, TroopStub stub)
        {
            Global.Current = global;
            Global.Current.FireEvents.Returns(false);

            stub.AddFormation(FormationType.Normal);
            stub.AddFormation(FormationType.Garrison);
            stub.AddFormation(FormationType.InBattle);

            stub.AddUnit(FormationType.Normal, 101, 10);

            cityBattleProcedure.MoveUnitFormation(stub, FormationType.Normal, FormationType.InBattle);
            cityBattleProcedure.MoveUnitFormation(stub, FormationType.InBattle, FormationType.Normal);

            Assert.True(stub[FormationType.Normal].Type == FormationType.Normal);
            Assert.True(stub[FormationType.InBattle].Type == FormationType.InBattle);
        }
Ejemplo n.º 2
0
        public override void Callback(object custom)
        {
            ICity city;

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

            CallbackLock.CallbackLockHandler lockHandler = delegate
            {
                var toBeLocked = new List <ILockable>();
                toBeLocked.AddRange(city.Battle.LockList);
                toBeLocked.Add(city);
                toBeLocked.AddRange(city.Troops.StationedHere().Select(stub => stub.City).Distinct());
                return(toBeLocked.ToArray());
            };

            locker.Lock(lockHandler, null, city).Do(() =>
            {
                if (city.Battle.ExecuteTurn())
                {
                    // Battle continues, just save it and reschedule
                    dbManager.Save(city.Battle);
                    endTime = SystemClock.Now.AddSeconds(formula.GetBattleInterval(city.Battle.Defenders, city.Battle.Attackers));
                    StateChange(ActionState.Fired);
                    return;
                }

                // Battle has ended
                // Delete the battle
                city.Battle.ActionAttacked -= BattleActionAttacked;
                city.Battle.UnitKilled     -= BattleUnitKilled;
                city.Battle.EnterRound     -= BattleEnterRound;
                world.Remove(city.Battle);
                dbManager.Delete(city.Battle);
                city.Battle = null;

                // Move the default troop back into normal and clear its temporary battle stats
                city.DefaultTroop.BeginUpdate();
                city.DefaultTroop.Template.ClearStats();
                city.DefaultTroop.State = TroopState.Idle;
                cityBattleProcedure.MoveUnitFormation(city.DefaultTroop, FormationType.InBattle, FormationType.Normal);
                city.DefaultTroop.EndUpdate();

                // Get a COPY of the stubs that are stationed in the town since the loop below will modify city.Troops
                var stationedTroops = city.Troops.StationedHere().Where(stub => stub.State == TroopState.BattleStationed).ToList();

                // Go through each stationed troop and either remove them from the city if they died
                // or set their states back to normal
                foreach (var stub in stationedTroops)
                {
                    // Check if stub has died, if so, remove it completely from both cities
                    if (stub.TotalCount == 0)
                    {
                        city.Troops.RemoveStationed(stub.StationTroopId);
                        stub.City.Troops.Remove(stub.TroopId);
                        continue;
                    }

                    // Set the stationed stub back to just TroopState.Stationed
                    stub.BeginUpdate();
                    stub.State = TroopState.Stationed;
                    stub.EndUpdate();
                }

                // Handle SenseOfUrgency technology
                if (destroyedHp > 0)
                {
                    battleProcedure.SenseOfUrgency(city, destroyedHp);
                }

                StateChange(ActionState.Completed);
            });
        }