示例#1
0
        private void AttackCity(Session session, Packet packet)
        {
            uint        cityId;
            uint        targetCityId;
            Position    target;
            ISimpleStub simpleStub;
            AttackMode  mode;

            try
            {
                mode         = (AttackMode)packet.GetByte();
                cityId       = packet.GetUInt32();
                targetCityId = packet.GetUInt32();
                target       = new Position(packet.GetUInt32(), packet.GetUInt32());
                simpleStub   = PacketHelper.ReadStub(packet, FormationType.Attack);
            }
            catch (Exception)
            {
                ReplyError(session, packet, Error.Unexpected);
                return;
            }

            var cityNotFound = locker.Lock(session.Player).Do(() => session.Player.GetCity(cityId) == null);

            if (cityNotFound)
            {
                ReplyError(session, packet, Error.Unexpected);
                return;
            }

            Dictionary <uint, ICity> cities;

            locker.Lock(out cities, cityId, targetCityId).Do(() =>
            {
                if (cities == null)
                {
                    ReplyError(session, packet, Error.Unexpected);
                    return;
                }

                ICity city = cities[cityId];

                var troopInitializer = troopObjectInitializerFactory.CreateCityTroopObjectInitializer(cityId,
                                                                                                      simpleStub,
                                                                                                      TroopBattleGroup.Attack,
                                                                                                      mode);

                var attackAction = actionFactory.CreateCityAttackChainAction(cityId,
                                                                             troopInitializer,
                                                                             targetCityId,
                                                                             target);

                var result = city.Worker.DoPassive(city, attackAction, true);

                ReplyWithResult(session, packet, result);
            });
        }
示例#2
0
        /// <summary>
        ///     Called to dispatch a unit
        /// </summary>
        /// <param name="stub"></param>
        /// <returns></returns>
        private bool Dispatch(ITroopStub stub)
        {
            // Create troop object
            ITroopObject troopObject;

            procedure.TroopObjectCreate(stub.City, stub, out troopObject);

            PassiveAction action;

            if (Target.LocationType == LocationType.City)
            {
                IStructure structure = (IStructure)gameObjectLocator.Regions.GetObjectsInTile(X, Y).FirstOrDefault(z => z is IStructure);
                if (structure == null)
                {
                    procedure.TroopObjectDelete(troopObject, true);
                    stub.City.Owner.SendSystemMessage(null,
                                                      "Assignment Failed",
                                                      string.Format(
                                                          @"Assigned target({0},{1}) has already been destroyed. The reserved troops have been returned to the city.",
                                                          X,
                                                          Y));
                    return(false);
                }

                if (IsAttack)
                {
                    var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Attack, AttackMode);
                    action = actionFactory.CreateCityAttackChainAction(stub.City.Id,
                                                                       troopObjectInitializer,
                                                                       structure.City.Id,
                                                                       new Position(X, Y));
                }
                else
                {
                    var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Defense, AttackMode);

                    action = actionFactory.CreateCityDefenseChainAction(stub.City.Id,
                                                                        troopObjectInitializer,
                                                                        structure.City.Id);
                }
            }
            else if (Target.LocationType == LocationType.Stronghold)
            {
                if (IsAttack)
                {
                    var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Attack, AttackMode);

                    action = actionFactory.CreateStrongholdAttackChainAction(stub.City.Id,
                                                                             troopObjectInitializer,
                                                                             Target.LocationId,
                                                                             forceAttack: true);
                }
                else
                {
                    var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Defense, AttackMode);
                    action = actionFactory.CreateStrongholdDefenseChainAction(stub.City.Id,
                                                                              troopObjectInitializer,
                                                                              Target.LocationId);
                }
            }
            else
            {
                procedure.TroopObjectDelete(troopObject, true);
                return(false);
            }

            if (stub.City.Worker.DoPassive(stub.City, action, true) != Error.Ok)
            {
                procedure.TroopObjectDelete(troopObject, true);
                return(false);
            }

            return(true);
        }