/// <summary> /// Fetches the BF2 map into an object. If the map has already been loaded /// into an object previously, that object will be returned instead. /// </summary> /// <param name="Name">The map FOLDER name</param> /// <returns></returns> 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); }
/// <summary> /// Called when a map is selected or changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MapListSelect_SelectedIndexChanged(object sender, EventArgs e) { // Reset image MapPictureBox.Image = null; // If an error occurs while loading the maps .desc file, then just return if (MapListSelect.SelectedIndex == -1) return; // Dont Need to reset anything if its the first time loading a map if (!isMapSelected) { GameModeSelect.Enabled = true; isMapSelected = true; } else { // Reset select list's text MapSizeSelect.Text = ""; GameModeSelect.Text = ""; // Remove all items from the Mode select and MapSize select GameModeSelect.Items.Clear(); MapSizeSelect.Items.Clear(); // Disable MapList select MapSizeSelect.Enabled = false; AddToMapList.Enabled = false; } // Try and load the map object... a common error here is that the // Descriptor file containing illegal XML characters try { string map = MapListSelect.SelectedItem.ToString(); SelectedMap = MainForm.SelectedMod.LoadMap(map); isMapSelected = true; } catch (Exception E) { // Get our Inner exception message if its an InvalidMapException // We do this because InvalidMapException doesnt really tell us the issue, // but if there is an inner exception, we will have the issue. string mess = (E is InvalidMapException && E.InnerException != null) ? E.InnerException.Message : E.Message; MessageBox.Show("There was an error loading the map descriptor file" + Environment.NewLine + Environment.NewLine + "Message: " + mess, "Map Loading Error", MessageBoxButtons.OK, MessageBoxIcon.Warning ); // Reset the GUI MapListSelect.SelectedIndex = -1; GameModeSelect.Enabled = false; isMapSelected = false; } // If we have no map loaded, it failed if (!isMapSelected) return; // Add all map supported game modes to the GameMode select list foreach (KeyValuePair<string, List<string>> Mode in SelectedMap.GameModes) GameModeSelect.Items.Add(new KeyValueItem(Mode.Key, GameModeToString(Mode.Key))); // Set the default map gamemode GameModeSelect.SelectedIndex = 0; }
/// <summary> /// Fetches the BF2 map into an object. If the map has already been loaded /// into an object previously, that object will be returned instead. /// </summary> /// <param name="Name">The map FOLDER name</param> /// <returns></returns> 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; }
/// <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(); }
/// <summary> /// Called when a map is selected or changed /// </summary> private void MapListSelect_SelectedIndexChanged(object sender, EventArgs e) { // Reset image MapPictureBox.Image = null; // If an error occurs while loading the maps .desc file, then just return if (MapListSelect.SelectedIndex == -1) { return; } // Dont Need to reset anything if its the first time loading a map if (!isMapSelected) { GameModeSelect.Enabled = true; isMapSelected = true; } else { // Reset select list's text MapSizeSelect.Text = ""; GameModeSelect.Text = ""; // Remove all items from the Mode select and MapSize select GameModeSelect.Items.Clear(); MapSizeSelect.Items.Clear(); // Disable MapList select MapSizeSelect.Enabled = false; AddToMapList.Enabled = false; } // Try and load the map object... a common error here is that the // Descriptor file containing illegal XML characters try { string map = MapListSelect.SelectedItem.ToString(); SelectedMap = MainForm.SelectedMod.LoadMap(map); isMapSelected = true; } catch (Exception E) { // Get our Inner exception message if its an InvalidMapException // We do this because InvalidMapException doesnt really tell us the issue, // but if there is an inner exception, we will have the issue. string mess = (E is InvalidMapException && E.InnerException != null) ? E.InnerException.Message : E.Message; MessageBox.Show("There was an error loading the map descriptor file" + Environment.NewLine + Environment.NewLine + "Message: " + mess, "Map Loading Error", MessageBoxButtons.OK, MessageBoxIcon.Warning ); // Reset the GUI MapListSelect.SelectedIndex = -1; GameModeSelect.Enabled = false; isMapSelected = false; } // If we have no map loaded, it failed if (!isMapSelected) { return; } // Add all map supported game modes to the GameMode select list foreach (KeyValuePair <string, List <string> > Mode in SelectedMap.GameModes) { GameModeSelect.Items.Add(new KeyValuePair(Mode.Key, GameModeToString(Mode.Key))); } // Set the default map gamemode GameModeSelect.SelectedIndex = 0; }