Exemple #1
0
        private void HandleGameOptionsMessage(string data)
        {
            if (IsHost)
            {
                return;
            }

            string[] parts = data.Split(ProgramConstants.LAN_DATA_SEPARATOR);

            if (parts.Length != CheckBoxes.Count + DropDowns.Count + GAME_OPTION_SPECIAL_FLAG_COUNT)
            {
                AddNotice("The game host has sent an invalid game options message. This " +
                          "usually means that the game host has a different game version than you.");
                Logger.Log("Invalid game options message from host: " + data);
                return;
            }

            int randomSeed = Conversions.IntFromString(parts[parts.Length - GAME_OPTION_SPECIAL_FLAG_COUNT], -1);

            if (randomSeed == -1)
            {
                return;
            }

            RandomSeed = randomSeed;

            string mapSHA1  = parts[parts.Length - (GAME_OPTION_SPECIAL_FLAG_COUNT - 1)];
            string gameMode = parts[parts.Length - (GAME_OPTION_SPECIAL_FLAG_COUNT - 2)];

            GameMode gm = GameModes.Find(g => g.Name == gameMode);

            if (gm == null)
            {
                AddNotice("The game host has selected a map that doesn't exist on your " +
                          "installation. The host needs to change the map or you won't be able to play.");
                ChangeMap(null, null);
                return;
            }

            Map map = gm.Maps.Find(m => m.SHA1 == mapSHA1);

            if (map == null)
            {
                AddNotice("The game host has selected a map that doesn't exist on your " +
                          "installation. The host needs to change the map or you won't be able to play.");
                ChangeMap(null, null);
                return;
            }

            if (GameMode != gm || Map != map)
            {
                ChangeMap(gm, map);
            }

            int frameSendRate = Conversions.IntFromString(parts[parts.Length - (GAME_OPTION_SPECIAL_FLAG_COUNT - 3)], FrameSendRate);

            if (frameSendRate != FrameSendRate)
            {
                FrameSendRate = frameSendRate;
                AddNotice("The game host has changed FrameSendRate (order lag) to " + frameSendRate);
            }

            bool removeStartingLocations = Convert.ToBoolean(Conversions.IntFromString(
                                                                 parts[parts.Length - (GAME_OPTION_SPECIAL_FLAG_COUNT - 4)], Convert.ToInt32(RemoveStartingLocations)));

            SetRandomStartingLocations(removeStartingLocations);

            for (int i = 0; i < CheckBoxes.Count; i++)
            {
                GameLobbyCheckBox chkBox = CheckBoxes[i];

                bool oldValue = chkBox.Checked;
                chkBox.Checked = Conversions.IntFromString(parts[i], -1) > 0;

                if (chkBox.Checked != oldValue)
                {
                    if (chkBox.Checked)
                    {
                        AddNotice("The game host has enabled " + chkBox.Text);
                    }
                    else
                    {
                        AddNotice("The game host has disabled " + chkBox.Text);
                    }
                }
            }

            for (int i = 0; i < DropDowns.Count; i++)
            {
                int index = Conversions.IntFromString(parts[CheckBoxes.Count + i], -1);

                GameLobbyDropDown dd = DropDowns[i];

                if (index < 0 || index >= dd.Items.Count)
                {
                    return;
                }

                int oldValue = dd.SelectedIndex;
                dd.SelectedIndex = index;

                if (index != oldValue)
                {
                    string ddName = dd.OptionName;
                    if (dd.OptionName == null)
                    {
                        ddName = dd.Name;
                    }

                    AddNotice("The game host has set " + ddName + " to " + dd.SelectedItem.Text);
                }
            }
        }
        /// <summary>
        /// Attempts to load a custom map.
        /// </summary>
        /// <param name="mapPath">The path to the map file relative to the game directory.</param>
        /// <param name="userInvoked">Whether this function was invoked by the user.
        /// If true, displays notifications regarding the success of the map loading.</param>
        /// <returns>The map if loading it was succesful, otherwise false.</returns>
        protected Map LoadCustomMap(string mapPath, bool userInvoked)
        {
            // TODO This belongs to MapLoader

            if (!File.Exists(ProgramConstants.GamePath + mapPath + MapLoader.MAP_FILE_EXTENSION))
            {
                Logger.Log("LoadCustomMap: Map " + mapPath + " not found!");
                AddNotice($"Map file {mapPath}{MapLoader.MAP_FILE_EXTENSION} doesn't exist!");
                return(null);
            }

            Logger.Log("Loading custom map " + mapPath);
            Map map = new Map(mapPath, false);

            if (map.SetInfoFromMap(ProgramConstants.GamePath + mapPath + MapLoader.MAP_FILE_EXTENSION))
            {
                foreach (GameMode gm in GameModes)
                {
                    if (gm.Maps.Find(m => m.SHA1 == map.SHA1) != null)
                    {
                        Logger.Log("Custom map " + mapPath + " is already loaded!");

                        if (userInvoked)
                        {
                            AddNotice($"Map {mapPath} is already loaded.");
                        }

                        return(null);
                    }
                }

                Logger.Log("Map " + mapPath + " added succesfully.");

                foreach (string gameMode in map.GameModes)
                {
                    GameMode gm = GameModes.Find(g => g.Name == gameMode);

                    if (gm == null)
                    {
                        if (!allowedGameModes.Contains(gameMode))
                        {
                            continue;
                        }

                        gm = new GameMode(gameMode);
                        GameModes.Add(gm);
                    }

                    gm.Maps.Add(map);
                    Logger.Log("Adding map to game mode " + gm.Name);
                }

                if (userInvoked)
                {
                    AddNotice($"Map {mapPath} loaded succesfully.");
                }

                return(map);
            }

            if (userInvoked)
            {
                AddNotice($"Loading map {mapPath} failed!");
            }

            Logger.Log("Loading map " + mapPath + " failed!");
            return(null);
        }