예제 #1
0
        /// <summary>
        /// Creates a new GameCore instance.
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="gamemode"></param>
        /// <param name="skipInit">Whether to skip the customary X second init and gamemode setup</param>
        public GameCore(ILogger logger, Gamemodes.Gamemode gamemode, ModAssetInfo map, EngineSettings settings = null)
        {
            if (Logger == null)
            {
                Logger = new NullLogger();
            }
            else
            {
                Logger = new ModuleLogger(logger, "Game");
            }
            if (settings == null)
            {
                Settings = new EngineSettings();
            }
            else
            {
                Settings = settings;
            }

            //Set up the game mode internally
            Gamemode = gamemode;
            Gamemode.SetGame(this);

            Map = Maps.Map.LoadMap(map, this);

            InitializeGame();
        }
예제 #2
0
 public NetworkedGame(bool authoritative, Gamemode gamemode, ILogger gameLogger,
                      ModAssetInfo mapData, EngineSettings settings = null)
 {
     Game = new Engine.GameCore(new NullLogger(), gamemode, mapData, settings);
     if (!authoritative)
     {
         Game.BeginGame(false);
     }
     Authoritative = authoritative;
 }
예제 #3
0
파일: Map.cs 프로젝트: pashcovich/mptanks2d
        public static Map LoadMap(ModAssetInfo mapFile, GameCore game)
        {
            if (mapFile.AssetName == null)
            {
                return(new Map(game));// it's null
            }
            var data = mapFile.ReadAsString();

            var map = new Map(game, MapJSON.Load(data), data);

            map.AssetInfo = mapFile;

            return(map);
        }
예제 #4
0
        static ModAssetInfo ChooseMap()
        {
            var maps = new List <ModAssetInfo>();

            foreach (var mod in ModDatabase.LoadedModulesList)
            {
                maps.AddRange(mod.Header.MapFiles
                              .Select(a => new ModAssetInfo {
                    AssetName = a, ModInfo = mod.ModInfo
                }));
            }

            _logger.Info($"Choose map (1-{maps.Count})");
            int id = 1;

            foreach (var map in maps)
            {
                _logger.Info($"[{id++}] - {map.AssetName} from {map.ModInfo.ModName}.");
            }

            ModAssetInfo info  = new ModAssetInfo();
            bool         valid = false;

            while (!valid)
            {
                var num = WaitInt(30000);
                if (num == -1)
                {
                    num = 1;            //Chose the first one if no input
                }
                if (num < 1 || num > maps.Count)
                {
                    _logger.Error($"{num} is out of range. It must be between 1 and {maps.Count}.");
                    continue;
                }
                valid = true;
                info  = maps[num - 1];
            }

            _logger.Info($"Chose map: {info.AssetName} from {info.ModInfo.ModName}.");
            return(info);
        }
예제 #5
0
        public static FullGameState Read(NetIncomingMessage message)
        {
            var state = new FullGameState();

            state.MapInfo = ModAssetInfo.Decode(message.ReadBytes(message.ReadUInt16()));
            state.GamemodeReflectionName = message.ReadString();
            state.FriendlyFireEnabled    = message.ReadBoolean();
            state.HasStarted             = message.ReadBoolean();
            message.ReadPadBits();
            state.GamemodeState               = message.ReadBytes(message.ReadInt32());
            state.Status                      = (GameCore.CurrentGameStatus)message.ReadByte();
            state.TimescaleString             = message.ReadString();
            state.TimescaleValue              = message.ReadDouble();
            state.CurrentGameTimeMilliseconds = message.ReadDouble();
            state.NextObjectId                = message.ReadUInt16();
            state.GameEndedTime               = message.ReadDouble();

            var objCount = message.ReadInt32();

            for (var i = 0; i < objCount; i++)
            {
                state.ObjectStates.Add(new FullObjectState(message.ReadBytes(message.ReadInt32())));
            }

            var playersCount = message.ReadInt32();

            for (var i = 0; i < playersCount; i++)
            {
                state.Players.Add(FullStatePlayer.Read(message));
            }

            var loadedModCount = message.ReadInt32();

            for (var i = 0; i < loadedModCount; i++)
            {
                state.GameLoadedMods.Add(ModInfo.Decode(message.ReadBytes(message.ReadUInt16())));
            }

            return(state);
        }
예제 #6
0
        static void Main(string[] args)
        {
            Console.Title = $"MP Tanks 2D Dedicated Server v{StaticSettings.VersionMajor}.{StaticSettings.VersionMinor}";

            _logger.Info("Server started, loading core mods...");

            Client.GameSandbox.Mods.CoreModLoader.LoadTrustedMods(GameSettings.Instance);
            _logger.Info("Core mods loaded.");

            LoadMods();

            _logger.Info("Loaded mods list: ");
            foreach (var mod in Modding.ModLoader.LoadedMods)
            {
                var m = mod.Value;
                _logger.Info($"\t{m.Name} version {m.Version.Major}." +
                             $"{m.Version.Minor}-{m.Version.Tag ?? ""}");
            }

            _gamemode = ChooseGamemode();
            _map      = ChooseMap();
            _server   = new Server(new Configuration
            {
                MaxPlayers = 16
            },
                                   new GameCore(new NullLogger(), _gamemode, _map,
                                                EngineSettings.GetInstance()), true, _logger);

            _server.GameInstance.GameChanged += (a, e) =>
            {
                _logger.Info("Game changed (new game created).");
                e.Game.EventEngine.OnGameStarted += (b, f) => _logger.Info("Game started");
                e.Game.EventEngine.OnGameEnded   += (b, f) =>
                {
                    _logger.Info("Game ended.");
                    //Start a new game of the same type
                    _server.SetGame(new GameCore(new NullLogger(), _gamemode, _map,
                                                 EngineSettings.GetInstance()));
                };
            };
            _server.OnCountdownStarted += (a, e) =>
                                          _logger.Info($"Game countdown started: {e.TotalSeconds.ToString("N0")} seconds remaining.");
            _server.OnCountdownStopped += (a, e) =>
                                          _logger.Info("Game countdown stopped.");
            _server.SetGame(_server.Game);

            for (var i = 0; i < 1; i++)
            {
                _server.AddPlayer(new ServerPlayer(_server, new Networking.Common.NetworkPlayer()
                {
                    Username = "******" + _server.Players.Count,
                    UniqueId = Guid.NewGuid()
                }));
            }

            _logger.Info("For help, type \"help\".");

            Stopwatch sw = Stopwatch.StartNew();
            GameTime  gt = new GameTime();

            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                if (Console.CursorLeft <= 1)
                {
                    Console.CursorLeft = 0;
                    Console.Write(">> ");
                }
                _server.Update(gt);
                Process();
                var el = (int)sw.Elapsed.TotalMilliseconds;
                if (16 - el > 0)
                {
                    Thread.Sleep(16 - el);
                }

                var elapsed = sw.Elapsed;
                gt.TotalGameTime  += elapsed;
                gt.ElapsedGameTime = elapsed;
                if (gt.ElapsedGameTime.TotalMilliseconds > 500)
                {
                    gt.ElapsedGameTime = TimeSpan.FromMilliseconds(500);
                }
                sw.Restart();
            }
        }
예제 #7
0
        static void Process()
        {
            var info = WaitLine(0);

            if (info == null)
            {
                return;
            }
            info = info.ToLower();
            if (info.StartsWith("change-gamemode"))
            {
                _gamemode = ChooseGamemode();
            }
            else if (info.StartsWith("force-restart"))
            {
                _server.SetGame(new GameCore(new NullLogger(),
                                             _gamemode, _map, EngineSettings.GetInstance()));
            }
            else if (info.StartsWith("change-map"))
            {
                _map = ChooseMap();
            }
            else if (info.StartsWith("help"))
            {
                _logger.Info("Help menu");
                _logger.Info("help - Show this menu");
                _logger.Info("change-gamemode - Change the gamemode for the next game.");
                _logger.Info("change-map - Change the map for the next game.");
                _logger.Info("change-timescale {scale} - Change the timescale for the current game.");
                _logger.Info("force-restart - Force an immediate restart of the game," +
                             " updating to the new gamemode and map");
                _logger.Info("kick {username} - Kicks the specified user from the server");
                _logger.Info("exit - Exits the game");
            }
            else if (info.StartsWith("change-timescale"))
            {
                if (info.Split(' ').Length < 2)
                {
                    _logger.Error("change-timescale requires a timescale parameter.");
                    return;
                }
                double scale;
                if (!double.TryParse(info.Split(' ')[1], out scale))
                {
                    _logger.Error($"{info.Split(' ')[1]} is not a valid timescale.");
                    return;
                }
                _server.Game.Timescale = new GameCore.TimescaleValue(scale, scale.ToString());
            }
            else if (info.StartsWith("kick"))
            {
                if (info.Split(' ').Length < 2)
                {
                    _logger.Error("kick requires a username parameter.");
                    return;
                }
                string name   = info.Split(' ')[1];
                var    player = _server.Players.FirstOrDefault(a => a.Player.Username.Equals(name, StringComparison.InvariantCultureIgnoreCase));
                if (player == null)
                {
                    _logger.Error("No player by that name is on the server.");
                    return;
                }
                _server.RemovePlayer(player, "Kicked from server");
            }
            else if (info.StartsWith("exit"))
            {
                _server.Close();
                _logger.Info("Server closed.");
                Environment.Exit(0);
            }
            else
            {
                _logger.Error("Command not recognized.");
            }
        }
예제 #8
0
 public GameCore(ILogger logger, string gamemodeReflectionName, ModAssetInfo map, EngineSettings settings = null)
     : this(logger, Gamemodes.Gamemode.ReflectiveInitialize(gamemodeReflectionName), map, settings)
 {
 }