示例#1
0
        public GameSession Execute(GameSession gameSession)
        {
            gameSession.AddHistoryMessage($"SessionEvents started.", GetType().Name, true);

            var result = gameSession;

            if (gameSession.Rules.IsEventsEnabled == false)
            {
                gameSession.AddHistoryMessage("SessionEvents canceled by scenario configuration.", GetType().Name, true);

                return(result);
            }

            foreach (var scenarioEvent in gameSession.GetScenarioEvents())
            {
                Logger.Debug($"Found scenario event (id={scenarioEvent.Id}.");

                var newGameEvent = new GameEvent();

                switch (scenarioEvent.Type)
                {
                case GameEventTypes.AnomalyFound:
                    break;

                case GameEventTypes.OpenDialog:
                    newGameEvent.Type     = GameEventTypes.OpenDialog;
                    newGameEvent.DialogId = scenarioEvent.ToScenarioEventDialog().DialogId;
                    break;

                case GameEventTypes.NpcSpaceShipFound:
                    newGameEvent.Type           = GameEventTypes.NpcSpaceShipFound;
                    newGameEvent.DialogId       = ((ScenarioEventGenerateNpcSpaceShip)scenarioEvent).DialogId;
                    newGameEvent.GenericObjects = ((ScenarioEventGenerateNpcSpaceShip)scenarioEvent).Execute(result);
                    break;

                case GameEventTypes.WreckSpaceShipFound:
                    newGameEvent.Type           = GameEventTypes.WreckSpaceShipFound;
                    newGameEvent.DialogId       = ((ScenarioEventGenerateNpcSpaceShip)scenarioEvent).DialogId;
                    newGameEvent.GenericObjects = ((ScenarioEventGenerateNpcSpaceShip)scenarioEvent).Execute(result);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                newGameEvent.Turn = gameSession.Turn + 1;

                newGameEvent.IsPause      = true;
                newGameEvent.IsOpenWindow = true;

                result.AddEvent(newGameEvent);
            }

            return(result);
        }
示例#2
0
        public CommandExecuteResult Execute(GameSession gameSession, Command command)
        {
            gameSession.AddHistoryMessage($"started.", GetType().Name, true);

            var explosion = gameSession.GetCelestialObject(command.CelestialObjectId).ToExplosion();

            var destroyedSpaceships = new List <ICelestialObject>();

            foreach (var celestialObject in gameSession.SpaceMap.CelestialObjects)
            {
                if (celestialObject.IsSpaceship() == false)
                {
                    continue;
                }

                var spaceShip = celestialObject.ToSpaceship();

                var distance = SpaceMapTools.GetDistance(celestialObject.GetLocation(), explosion.GetLocation());

                if (distance < explosion.Radius * 2)
                {
                    spaceShip.Damage(explosion.Damage);

                    gameSession.AddHistoryMessage($"Spaceship {spaceShip.Name} get damage '{explosion.Damage}' from '{explosion.Name}'", GetType().Name);

                    if (spaceShip.IsDestroyed)
                    {
                        destroyedSpaceships.Add(celestialObject);
                    }
                }

                foreach (var destroyedSpaceship in destroyedSpaceships)
                {
                    gameSession.RemoveCelestialObject(destroyedSpaceship);

                    gameSession.AddHistoryMessage($"Spaceship {destroyedSpaceship.Name} was destroyed.", GetType().Name);
                }
            }

            return(new CommandExecuteResult {
                Command = command, IsResume = false
            });
        }
示例#3
0
        public CommandExecuteResult Execute(GameSession gameSession, Command command)
        {
            var isResume = true;

            gameSession.AddHistoryMessage($"started.", GetType().Name, true);

            Logger.Info($"[{GetType().Name}]\t CommandsExecute ExecuteFire - Execute.");

            var missile      = gameSession.GetCelestialObject(command.CelestialObjectId);
            var targetObject = gameSession.GetCelestialObject(command.TargetCelestialObjectId);

            if (missile == null)
            {
                return new CommandExecuteResult {
                           Command = command, IsResume = false
                }
            }
            ;
            if (targetObject == null)
            {
                return new CommandExecuteResult {
                           Command = command, IsResume = false
                }
            }
            ;

            var pointCurrentLocation = new PointF(missile.PositionX, missile.PositionY);
            var pointTargetLocation  = new PointF(targetObject.PositionX, targetObject.PositionY);

            var direction = Coordinates.GetRotation(pointTargetLocation, pointCurrentLocation);

            foreach (var mapCelestialObject in gameSession.SpaceMap.CelestialObjects)
            {
                if (mapCelestialObject.Id == missile.Id)
                {
                    mapCelestialObject.Direction = direction;
                }
            }

            var distance = Coordinates.GetDistance(missile.GetLocation(), targetObject.GetLocation());

            if (distance <= missile.Speed / 2)
            {
                // BOOM!!!
                isResume = false;

                gameSession.RemoveCelestialObject(missile);
            }

            return(new CommandExecuteResult {
                Command = command, IsResume = isResume
            });
        }
    }
}
示例#4
0
        public CommandExecuteResult Execute(GameSession gameSession, Command command)
        {
            Logger.Debug(TraceMessage.Execute(this, "Execute command scanning is started."));

            gameSession.AddHistoryMessage("started.", GetType().Name, true);

            if (RandomGenerator.DiceRoller() < gameSession.Rules.Spawn.AsteroidSmallSize)
            {
                Logger.Debug(TraceMessage.Execute(this, "Add new asteroid."));

                gameSession.AddCelestialObject(CelestialObjectsFactory.GenerateAsteroid(gameSession));
            }

            return(new CommandExecuteResult {
                Command = command, IsResume = false
            });
        }
示例#5
0
        public GameSession Recalculate(GameSession gameSession, EngineSettings settings)
        {
            //var result = gameSession.SpaceMap.DeepClone();

            foreach (var celestialObject in gameSession.GetCelestialObjects())
            {
                if (celestialObject.IsSpaceship() == false)
                {
                    continue;
                }

                foreach (var module in celestialObject.ToSpaceship().Modules)
                {
                    if ((module.Reloading == module.ReloadTime))
                    {
                        continue;
                    }

                    if (!(module.Reloading <= module.ReloadTime))
                    {
                        continue;
                    }

                    Logger.Debug($"Object {celestialObject.Name} reload module {module.Name} from {module.Reloading} to {1 / settings.UnitsPerSecond} second.");

                    var reloadingprogress = Math.Round((1.0 / settings.UnitsPerSecond), 2);

                    module.Reloading += reloadingprogress;

                    if (module.Reloading == module.ReloadTime)
                    {
                        module.Reloading = module.ReloadTime;
                        Logger.Info($"'{celestialObject.Name}' finished reload module '{module.Name}'");
                        gameSession.AddHistoryMessage($"'{celestialObject.Name}' finished reload module '{module.Name}'", GetType().Name);
                    }

                    if (module.Reloading > module.ReloadTime)
                    {
                        module.Reloading = module.ReloadTime;
                    }
                }
            }

            return(gameSession);
        }
        public GameSession Execute(GameSession gameSession)
        {
            gameSession.AddHistoryMessage($"AutomaticLaunchModules started.", GetType().Name, true);

            var result = gameSession.DeepClone();

            result.GetSpaceShips()
            .Select
                (spaceship => spaceship.Modules
                .Where(_ => _.IsAutoRun)
                .Map(_ => (IWeaponModule)_)
                .Where(_ => _.Reloading >= _.ReloadTime)
                .Map(_ => (IModule)_)
                )
            .SelectMany(activeModules => activeModules).ForAll(p => Flow(result, p));

            return(result);
        }
示例#7
0
        public CommandExecuteResult Execute(GameSession gameSession, Command command)
        {
            bool isResume = true;

            gameSession.AddHistoryMessage($"started.", GetType().Name, true);

            var spaceShip    = gameSession.GetPlayerSpaceShip();
            var targetObject = gameSession.GetCelestialObject(command.TargetCelestialObjectId);

            if (spaceShip == null)
            {
                return new CommandExecuteResult {
                           Command = command, IsResume = false
                }
            }
            ;

            var result = Approach.Calculate(spaceShip.GetLocation(), targetObject.GetLocation(), spaceShip.Direction, spaceShip.Speed, 100);

            foreach (var mapCelestialObject in gameSession.SpaceMap.CelestialObjects)
            {
                if (mapCelestialObject.Id == spaceShip.Id)
                {
                    if (result.Trajectory.Count > spaceShip.Speed)
                    {
                        mapCelestialObject.Direction = result.Trajectory[spaceShip.Speed].Direction;
                        Logger.Debug($"[{GetType().Name}]\t CommandsExecute AlignTo - {mapCelestialObject.Name} Direction before is {mapCelestialObject.Direction} Direction after {mapCelestialObject.Direction}");

                        if (result.Trajectory[spaceShip.Speed].Distance < spaceShip.Speed * 2)
                        {
                            isResume = false;
                            Logger.Info($"[{GetType().Name}]\t CommandsExecute AlignTo - {mapCelestialObject.Name} finished. Target location is {targetObject.GetLocation()} current location is {spaceShip.GetLocation()}");
                        }
                    }
                }
            }

            return(new CommandExecuteResult {
                Command = command, IsResume = isResume
            });
        }
    }
示例#8
0
        private GameSession TurnLeft(GameSession gameSession, ICelestialObject celestialObject, IModule module, EngineSettings settings)
        {
            // TODO: Add property Mobility to Spacecraft
            const float MobilityInDegrees = 10.0f;

            var spacecraft = (Spaceship)celestialObject;

            double turnRotationSpeed = MobilityInDegrees / (settings.UnitsPerSecond * module.ReloadTime);

            double directionBeforeManeuver = celestialObject.Direction;
            double directionAfterManeuver  = (directionBeforeManeuver - turnRotationSpeed > 0) ? directionBeforeManeuver - turnRotationSpeed : 360 - (directionBeforeManeuver - turnRotationSpeed);

            celestialObject.SetDirection(directionAfterManeuver);

            module.Reload();

            gameSession.AddHistoryMessage($"The ship '{spacecraft.Name}' changed direction {MobilityInDegrees} degrees from {Math.Round(directionBeforeManeuver,2)} to {Math.Round(directionAfterManeuver, 2)}. ");

            return(gameSession);
        }
示例#9
0
        public CelestialMap Recalculate(GameSession gameSession)
        {
            var result = gameSession.SpaceMap.DeepClone();

            foreach (var celestialObject in result.CelestialObjects)
            {
                if (celestialObject.IsSpaceship() == false)
                {
                    continue;
                }

                foreach (var module in celestialObject.ToSpaceship().Modules.Where(m => m is IWeaponModule))
                {
                    if (!(module.ToWeapon().Reloading <= module.ToWeapon().ReloadTime))
                    {
                        continue;
                    }

                    Logger.Debug($"Object {celestialObject.Name} reload module {module.Name} from {module.ToWeapon().Reloading} to +1 second.");
                    module.ToWeapon().Reloading++;

                    if (module.ToWeapon().Reloading == module.ToWeapon().ReloadTime)
                    {
                        module.ToWeapon().Reloading = module.ToWeapon().ReloadTime;
                        gameSession.AddHistoryMessage($"'{celestialObject.Name}' finished reload module '{module.Name}'", GetType().Name);
                    }

                    if (module.ToWeapon().Reloading > module.ToWeapon().ReloadTime)
                    {
                        module.ToWeapon().Reloading = module.ToWeapon().ReloadTime;
                    }
                }
            }

            return(result);
        }
        private bool Flow(GameSession gameSession, IModule module)
        {
            module.ToWeapon().Reloading = 0;

            gameSession.AddCommand(new Command
            {
                Type = CommandTypes.Scanning,
                CelestialObjectId = module.OwnerId,
                TargetCellId      = (int)module.Id
            });

            gameSession.AddCommand(new Command
            {
                Type = CommandTypes.ReloadWeapon,
                CelestialObjectId = module.OwnerId,
                TargetCellId      = (int)module.Id
            });

            var spaceship = gameSession.GetCelestialObject(module.OwnerId).ToSpaceship();

            gameSession.AddHistoryMessage($"Spaceship '{spaceship.Name}' automatic launch module '{module.Name}'", GetType().Name);

            return(true);
        }
示例#11
0
        public GameSession Execute(GameSession gameSession)
        {
            gameSession.AddHistoryMessage($"Commands started.", GetType().Name, true);

            var result = gameSession.DeepClone();

            // Clear history log for each turn
            result.TurnHistory = new List <HistoryMessage>();

            if (gameSession.Commands == null)
            {
                // Commands pool is empty. No need execute action.
                return(result);
            }

            // Clear commands pool
            result.Commands = new List <Command>();



            foreach (var command in gameSession.Commands)
            {
                switch (command.Type)
                {
                case CommandTypes.TurnLeft:
                case CommandTypes.TurnRight:
                case CommandTypes.Acceleration:
                case CommandTypes.StopShip:
                case CommandTypes.MoveForward:

                    var executeNavigationCommand = new CommandsExecute.Navigation().Execute(result, command);

                    if (executeNavigationCommand.IsResume)
                    {
                        result.Commands.Add(executeNavigationCommand.Command.DeepClone());
                    }

                    break;

                case CommandTypes.Scanning:
                    new CommandsExecute.Scanning().Execute(result, command);

                    break;

                case CommandTypes.Explosion:

                    var executeExplosion = new CommandsExecute.Explosion().Execute(result, command);

                    if (executeExplosion.IsResume)
                    {
                        result.Commands.Add(executeExplosion.Command.DeepClone());
                    }
                    else
                    {
                        var explosion = (Explosion)gameSession.GetCelestialObject(command.CelestialObjectId);

                        result.RemoveCelestialObject(explosion);
                    }

                    break;

                case CommandTypes.AlignTo:
                    var executeAlignToResult = new CommandsExecute.AlignTo().Execute(result, command);

                    if (executeAlignToResult.IsResume)
                    {
                        result.Commands.Add(executeAlignToResult.Command.DeepClone());
                    }
                    break;

                case CommandTypes.Fire:
                    var executeFireResult = new CommandsExecute.ExecuteFire().Execute(result, command);

                    if (executeFireResult.IsResume)
                    {
                        result.Commands.Add(executeFireResult.Command.DeepClone());
                    }
                    else
                    {
                        // Replace missile align to operation to explosive object
                        var celestialObject = gameSession.GetCelestialObject(command.CelestialObjectId);
                        var missileTarget   = gameSession.GetCelestialObject(command.TargetCelestialObjectId);

                        var missile = gameSession.GetCelestialObject(command.CelestialObjectId).ToMissile();

                        result.RemoveCelestialObject(missileTarget);

                        var explosionObject = new Explosion
                        {
                            Classification = 800,
                            Damage         = missile.Damage,
                            Name           = celestialObject.Name,
                            PositionX      = missileTarget.PositionX,
                            PositionY      = missileTarget.PositionY,
                            Radius         = missile.ExplosionRadius,
                            Speed          = 0,
                            Direction      = 0
                        };

                        result.AddCelestialObject(explosionObject);

                        var explosionCommand = new Command
                        {
                            TargetCelestialObjectId = explosionObject.Id,
                            Type = CommandTypes.Explosion,
                            CelestialObjectId = explosionObject.Id
                        };
                        result.Commands.Add(explosionCommand);
                    }
                    break;

                case CommandTypes.ReloadWeapon:
                    gameSession.AddHistoryMessage($"ReloadWeapon started.", GetType().Name, true);
                    foreach (var celestialObjects in result.SpaceMap.CelestialObjects)
                    {
                        if (celestialObjects.Id == command.CelestialObjectId)
                        {
                            var spaceship = celestialObjects.ToSpaceship();

                            foreach (var module in spaceship.Modules)
                            {
                                if (module.Id == command.TargetCelestialObjectId)
                                {
                                    var moduleWeapon = (IWeaponModule)module;

                                    ((IWeaponModule)module).Reloading = 0;

                                    moduleWeapon.Reloading = 0;
                                }
                            }
                        }
                    }

                    break;
                }
            }

            return(result);
        }