Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Building Battling Bots with DumbBots.Net

Today's project was one that I just happened across and I thought kind of cool, I mean what's more fun than programming bots to hunt down a kill each other? (Answer: Playing with the source of the project that lets you program bots to hunt down and kill each other)

DumbBots.Net

Project Description
A simple programming game that allows editing of scripts while the game is still being run. The scripts control the AI of some combatants in a Quake III-style game.

The game can be used as a tool to get people interested in coding, or simply as a competitive game between different code.

Some extensibility provided to create a simple sandbox environment. A "director" AI can be programmed to alter the dynamics of the game for a customizable experience.

Scripts are currently written in C#.

Features

  • Program scripts while game is in play to immediately view results
  • Scintilla.NET code editor
  • Basic "intellisense" to quickly find method needed
  • "Director" AI allows a customizable experience - change the game rules, load custom models, etc
  • Basic messaging system allows interaction between different AI's
  • Powered by Irrlicht engine

SNAGHTML4dd9989

The arena is 3D which is kind of cool too;

image

image

Included is a simple map editor;

SNAGHTML4e0bd6e

Here's the Solution;

image

So how does it work? Here's an abbreviated view of the code path that handles the runtime code compiling.

private void btnCompileTeam1_Click(object sender, EventArgs e)
    {
        CompileScript(Team.Team1);
    }
private void CompileScript(Team team)
{
    SoundManager.PlayUpdate();
    ScriptCompiler sc = new ScriptCompiler();
    List<string> dllList = new List<string>();

    //TODO: need to reimplement referencing assemblies

    if (team == Team.Director)
    {
        rchTxtBoxErrors.Text = sc.CompileDirectorScript("C#", _txtCode.Text, dllList);
        ScriptRunner.ReflectDirectorScript();
    }
    else
    {
        rchTxtBoxErrors.Text = sc.CompileScript("C#", _txtCode.Text, (int)team, dllList);
        ScriptRunner.ReflectScript(team);
    }
}

internal string CompileScript(string language, string code, string fileName, List<string> dllList)
{
    CodeDomProvider cdp = CodeDomProvider.CreateProvider(language);
    CompilerParameters cp = new CompilerParameters();

    cp.OutputAssembly = fileName;
    cp.ReferencedAssemblies.Add("DumbBots.NET.exe");
    cp.ReferencedAssemblies.Add("System.dll");
    cp.ReferencedAssemblies.Add("System.Drawing.dll");
    cp.ReferencedAssemblies.Add("Irrlicht.NET.dll");
    for (int i = 0; i < dllList.Count; i++)
    {
        cp.ReferencedAssemblies.Add(dllList[i]); //Add external DLLs mentioned in user script
    }

    cp.WarningLevel = 1;

    cp.CompilerOptions = "/target:library /optimize";
    cp.GenerateExecutable = false;
    cp.GenerateInMemory = false;

    System.CodeDom.Compiler.TempFileCollection tfc = new TempFileCollection(AppDomain.CurrentDomain.BaseDirectory, false);
    CompilerResults cr = new CompilerResults(tfc);

    cr = cdp.CompileAssemblyFromSource(cp, code);

    StringBuilder sb = new StringBuilder();

    if (cr.Errors.Count > 0)
    {
        foreach (CompilerError ce in cr.Errors)
        {
            sb.AppendLine(ce.ErrorNumber + ": " + ce.ErrorText + " Line: " + ce.Line);
        }
    }
    else
    {
        sb.AppendLine("Build succeeded");
    }

    return sb.ToString();
}

internal static void ReflectScript(Team team)
  {
      try
      {
          byte[] rawAssembly = LoadFile(String.Format("AI\\AIscript{0}.dll", (int)team));
          Assembly assembly = AppDomain.CurrentDomain.Load(rawAssembly);

          TeamScripts[team] = (ICommand)assembly.CreateInstance("Scripting.AIScript");

          assembly = null;
          rawAssembly = null;
      }
      catch (Exception)
      {
          TeamScripts[team] = null;
      }
  }

If you've ever thought about building bots that battle, interested in runtime code compiling or just looking for something fun to play with, here you go...

Tags:

Follow the Discussion

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.