SIGN UP MEMBER LOGIN:    
ARTICLE

Random Starcraft Map Chooser

Posted by David Sosby Articles | Visual C# May 12, 2003
I created a simple program that searches through Starcraft's maps directory tree and shows a list of all the maps that are searches by Starcraft's maps directory.
Reader Level:
Download Files:
 

I'm not going to lie, I play a lot of Blizzard Entertainment's Starcraft. My friend and I usually like to change things up a lot by choosing a random map to play on before every game. This was getting annoying (and a little unfair. What can I say, I like some maps better than others). So, I quickly made a simple program that would search through Starcraft's maps directory tree and show a list of all the maps that are currently on the computer, then randomly choose one. The following code was used to find the maps:

//THIS FUNCTION WILL GET RID OF THE ROOT DIRECTORY PATH
string parseRootPath(string path)
{
return path.Remove(0, rootSCPath.Length);
}
//THIS IS A RECURSIVE FUNCTION THAT WILL DISPLAY FILES
void getMapNames(string curDirPath)
{
string[] subDirs = Directory.GetDirectories(curDirPath);
foreach( string dir in subDirs )
getMapNames(dir);
string[] fileNames = Directory.GetFiles(curDirPath, "*.sc*");
foreach( string name in fileNames )
{
//UPDATE THE LIST OF MAPS
lbMaps.BeginUpdate();
lbMaps.Items.Add( parseRootPath(name) );
lbMaps.EndUpdate();
}
}

A recursive function was used in this process. If the file was a subdirectory, getMapNames() would call itself using the subdirectory as a parameter. Very simple. The main feature of my program was to choose a random map. This was a simple function to do so:

public void btnChooseClick(Object sender, EventArgs e)
{
//MAKE SURE THE LIST OF MAPS ISN'T EMPTY!
if ( lbMaps.Items.Count != 0 )
//SET A RANDOM MAP AS "SELECTED"
lbMaps.SetSelected( rand.Next(0, lbMaps.Items.Count), true );
else
MessageBox.Show("No maps in list. Populate the list.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

I also added a couple of features that would allow me to view the map and run starcraft directly from my program. To do this, I just used a Process to run a command. In the view map, this command was to just run a command with the filepath and filename of the selected map. Since Mapeditor.exe was the default program to open SC maps with, the maps were opened with it. The code is:

//OPEN THE MAP UP IN THE MAP EDITOR TO VIEW IT
public void btnViewMapClick(Object sender, EventArgs e)
{
if ( lbMaps.SelectedItem != null )
{
System.Diagnostics.Process proc =
new System.Diagnostics.Process();
proc.EnableRaisingEvents =
false;
proc.StartInfo.FileName = rootSCPath + lbMaps.SelectedItem.ToString();
proc.Start();
}
else
MessageBox.Show("No map selected. Please choose a map.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

And for the running of Starcraft.exe, we just make a process that runs starcraft.exe:

//RUN STARCRAFT.EXE
public void btnRunSCClick(Object sender, EventArgs e)
{
System.Diagnostics.Process proc =
new System.Diagnostics.Process();
proc.EnableRaisingEvents =
false;
proc.StartInfo.FileName = rootSCPath + \\starcraft.exe;
proc.Start();
}

Lots of little bits of code that could be helpful to you. This is (sadly) one of my most useful programs that I have made. But, as I was once told, Keep It Simple Stupid.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor