BF2Statistics.BF2Mod.LoadMap C# (CSharp) Method

LoadMap() public method

Fetches the BF2 map into an object. If the map has already been loaded into an object previously, that object will be returned instead.
public LoadMap ( string Name ) : BF2Map
Name string The map FOLDER name
return BF2Map
        public BF2Map LoadMap(string Name)
        {
            // Check 2 things, 1, does the map exist, and 2, if we have loaded it already
            if (!Levels.Contains(Name))
                throw new ArgumentException("Level Not Found");
            else if (LoadedLevels.ContainsKey(Name))
                return LoadedLevels[Name];

            // Create a new instance of the map, and store it for later
            BF2Map Map = new BF2Map(Name, LevelsPath);
            LoadedLevels.Add(Name, Map);
            return Map;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Event fired when the Generate Button is clicked
        /// Does the random Generating
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void GenerateBtn_Click(object sender, EventArgs e)
        {
            // Initialize lists
            List <string> Modes = new List <string>();
            List <string> Sizes = new List <string>();

            // Get list of supported Game Modes the user wants
            if (ConquestBox.Checked)
            {
                Modes.Add("gpm_cq");
            }
            if (CoopBox.Checked)
            {
                Modes.Add("gpm_coop");
            }

            // Get list of sizes the user wants
            if (s16Box.Checked)
            {
                Sizes.Add("16");
            }
            if (s32Box.Checked)
            {
                Sizes.Add("32");
            }
            if (s64Box.Checked)
            {
                Sizes.Add("64");
            }

            // Make sure we have at least 1 mode and size
            if (Modes.Count == 0 || Sizes.Count == 0)
            {
                // Handle Message
                MessageBox.Show("You must select at least 1 GameMode and Map Size!", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Initialize internal variables
            BF2Mod Mod            = MainForm.SelectedMod;
            Random Rnd            = new Random();
            int    NumOfMapsToAdd = (int)NumMaps.Value;
            int    MapCount       = Mod.Levels.Count;

            string[]      gModes = Modes.ToArray();
            StringBuilder Sb     = new StringBuilder();

            // Shuffle the maplist
            Mod.Levels.Shuffle();

            // Show loading form
            LoadingForm.ShowScreen(this);
            SetNativeEnabled(false);

            // Don't lockup GUI, run in a new task
            await Task.Run(() =>
            {
                // Loop through, the number of times the user specified, adding a map
                for (int i = 0; i < NumOfMapsToAdd; i++)
                {
                    // Prevent infinite looping and/or quit if we have reached the map count
                    if (i > 255 || (noDupesBox.Checked && i == MapCount))
                    {
                        break;
                    }

                    // Grab a random map from the levels array
                    try
                    {
                        // Try and load the map... if an exception is thrown, this loop doesnt count
                        BF2Map Map = Mod.LoadMap((noDupesBox.Checked) ? Mod.Levels[i] : Mod.Levels[Rnd.Next(0, MapCount)]);

                        // Get the common intersected gamemodes that the map has in common with what the user wants
                        string[] Common = Map.GameModes.Keys.ToArray();
                        Common          = gModes.Intersect(Common).ToArray();

                        // No common game modes
                        if (Common.Length == 0)
                        {
                            NumOfMapsToAdd++;
                            continue;
                        }

                        // Get a random gamemode key
                        string Mode = Common[Rnd.Next(0, Common.Length)];

                        // Get the common map sizes between what the user wants, and what the map supports
                        Common = Map.GameModes[Mode].Intersect(Sizes).ToArray();
                        if (Common.Length == 0)
                        {
                            // No common sizes, try another map
                            NumOfMapsToAdd++;
                            continue;
                        }

                        // Get a random size, and add the map
                        string Size = Common[Rnd.Next(0, Common.Length)];
                        Sb.AppendLine(Map.Name + " " + Mode + " " + Size);
                    }
                    catch (InvalidMapException)
                    {
                        NumOfMapsToAdd++;
                    }
                }
            });

            // Add new maplist to the maplist box
            MapListBox.Text = Sb.ToString();
            SetNativeEnabled(true);
            LoadingForm.CloseForm();
        }