Browsing the Samples
Previous Top Next

We included a Visual Studio® solution that contains some AI samples written in Visual C#®, so you can get a feel for how AI programs can be written. The sample solution is installed in your My Documents\Bots\Projects\Samples\SampleAI folder.

Note: If you decide to build and run the solution and you get errors, see the Solution Notes below.

To browse the solution, open it in Visual Studio® by double-clicking the solution (.sln) file. Inside Visual Studio®, if the Solution Explorer window isn't already open, from the main menu choose View >> Solution Explorer. You'll see there are two projects: one called AI, which contains the AI programs (classes), and one called AIRunner, which is an application (.exe) project that can be used to test and debug those programs.

graphic

First, the AIRunner project should be set as the startup project; it should appear in bold text as shown above. If not, right-click the AIRunner project node and choose Set as StartUp Project.

You can look over the classes in the AI project to get the idea of how AI programs can be written. For example, double-click the Roamer.cs file to open it. Focus your attention on the Run method, which is the highest-level method in an AI program, executed when the game scenario is started. It typically has an endless loop so the bot stays alive until the game is stopped. From there, other methods are called to do lower-level work.

/// <summary>
/// Executes the AI. This is the main, outermost method of the program.
/// </summary>
protected override void Run()
{
       while (true)
       {
              Look();
              Travel();

              // Give the bot's CPU a rest so it doesn't overheat
              Thread.Sleep(100);
       }
}

/// <summary>
/// Scans for objects, firing at obstacles and enemy bots.
/// </summary>
private void Look()
{
       List<ScannedObject> foundObjects = Scanner.Scan();
       foreach (ScannedObject foundObject in foundObjects)
       {
              // If the object isn't an ally bot, isn't an outer wall (which
              // is indestructible), and is in weapon range, fire at it.
              if (foundObject.ObjectType != ScannedObjectType.AllyBot &&
                     foundObject.ObjectType != ScannedObjectType.OuterWall &&
                     Weapons.Pulsar.IsInRange(foundObject.Position))
              {
                     Weapons.Pulsar.Fire(foundObject.Position);
              }
       }
}

/// <summary>
/// Moves around, avoiding obstacles.
/// </summary>
private void Travel()
{
       try
       {
              Nav.Move(10); // Move forward 10 meters
       }
       catch (CollisionException)
       {
              // We hit something so avoid this obstacle
              try
              {
                     Nav.Move(MoveDirection.Backward, 2); // Back up 2 meters
                     Nav.Turn(45); // Turn right 45 degrees
              }
              catch (CollisionException)
              {
                     // We hit something else while backing up or turning,
                     // so just give up.
              }
       }
}

The program accesses the bot's various onboard systems through properties and method calls, which allows it to make decisions and tell the bot what to do. The Roamer class makes use of the bot's navigation, scanner, and weapons system through the Nav, Scanner, and Weapons properties, respectively.

The other project in the solution, the AIRunner project, is used for testing and debugging the AI classes in the AI project. To see how it works, double-click Program.cs to open it. Look at the Main method. It makes a simple method call to start testing one of the AI programs.

       GameConnection.RunAI("Roamer", typeof(Roamer));

To run a test, you set up a game in the Game Viewer that includes a bot named "Roamer", and then switch back to Visual Studio® and run the solution by pressing F5 (or choosing Debug >> Start Debugging). (If you get errors, see the Build Notes below.) The above code "injects" the Roamer AI program into the bot named "Roamer" in the game. You can then switch your attention back to the Game Viewer to watch the action. You can set breakpoints in Roamer's code to suspend its execution, allowing you to inspect variables, make changes, etc., and then resume execution when you're ready.

In conclusion, please note that the purpose of the sample solution is only for illustration. Although you could use this solution to contain your own AI classes, we recommend creating your own solution for your AI, and that you use a naming convention that identifies and distinguishes your AI from the samples and from other people's creations. Please see Solution Guidelines for details.


Solution Notes
  1. If you get build errors related to Omega.Bots.dll when trying to build the sample solution, which is possible depending on your environment and configuration subtleties, you can probably resolve them by removing and re-adding this reference in each of the two projects. To do this, in the Solution Explorer window expand the References node under the AI project node (if it's not already expanded) so you can see that project's references. Right-click on Omega.Bots, choose Delete, and then click OK to confirm. Next, right-click on the References node and choose Add Reference... In the Add Reference dialog, select Bots AI Library from the list and click OK. Do the same for the AIRunner project to delete and re-add its reference for Omega.Bots.
  2. If you get a LoaderLock error when you run the sample solution, please see FAQ and Troubleshooting for an easy fix. (The glitch is a known issue related to DirectX.)
If you still have problems, please see FAQ and Troubleshooting for possible resolutions.


Recommended Topics
Solution Guidelines
AI Class Anatomy