Free and Open Source real time strategy game with a new take on micro-management

Making An AI With Echo (part 1)

From Globulation2

Revision as of 03:42, 11 July 2006 by Genixpro (talk | contribs) (Added Getting Started and Construction)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

Echo is the new subsystem for AI's. While Echo does not replace all of the existing AI system, it does provide a new interface to Glob2, and a large set of tools for making AI's. It can save allot of time, you would understand if you've made an AI without Echo.

You should read [New AI System] to get a general view of how Echo works before proceeding. You can also view nightly generated code documentation (Available from the Developer Center), which includes all of Echo's classes.

Getting Started

First of all, you have to create your AI files. In glob2/src, you can add AI*.h and AI*.cpp, where * is whatever your AI is called. Next, you need to add copyright statements to your files. Find any other source file, and copy&paste the copyright statements from the top of it to the top of your AI*.h and AI*.cpp files, replacing the name on the copyright statements with your name. Next, open up Makefile.am in glob2/src. You'll notice a list of source files, add your AI*.h and AI*.cpp to their correct position alphabetically. Add in header guards to your header file, and your ready to move on.

Now, we need to copy your AI structure over. Open up AIEcho.h, scroll about 3/4 of the way down. Your looking for a class called "EchoAI". When you find it, copy it over to your AI*.h. Remove all of the virtuals and =0's from the functions, and replace EchoAI with the name of your AI. Publicly derive your class from AIEcho::EchoAI, and add #include "AIEcho.h" to the top of your file. Now, implement all of the functions in your AI in the source file, as simple, empty functions (you won't be adding code yet).

Finally, we need to make your AI recognized by Glob2. You can use ReachToInfinity as an example Echo AI here. Open up AI.h and AI.cpp. At the top of AI.h, you'll find an enum with all of the AI names in uppercase. Add yours after all of the AI's, but before the "SIZE" variable. In AI.cpp, add the #include "AI*.h" at the top for your AI. Now, you're going to change the AI constructor. You'll see a switch statement at the top of the constructor (which should be at the top of the file). Add in a case statement for your AI. Then, set up a your initializer like the following:

 aiImplementation=new AIEcho::Echo(new your_ai_name, player);

Remember to replace your_ai_name with the name of you're actual AI. Now, scroll down the file further until you see another switch statement in AI::load. Add you're AI to this one as well, with the same initializer as above. Except, this time, after you're initializer, add the following:

 aiImplementation->load(stream, player, versionMinor);

We have one last thing to do, and thats add you're AI name to the translations. in glob2/data, you will see a bunch of texts.*.txt files for all of the translations. Open up texts.keys.txt, and scroll down to where the AI's are (you can search for [AI] in your text editor to make this faster). Add your AI name as a key, however, be very carefull to put the key in the exact position that you're AI is in the enum in AI.h. As of now(July 10'th 2006), [AIToubid] is an experiemental AI, so you have to put you're AI's key before his (since his was in the expiremental section of the enum in AI.h). Now, for the tedious bit, you have to add you're AI to each of the translations. You will have to open them up, one by one, and add you're AI name to the same position you added it in texts.keys.txt. Not only do you add your AI name in brackets, you also add it in text. Ignore the fact that you can't translate you're own AI name to every language, its important that atleast the key exists, so when translators do come along, they can translate it.

That was allot of work just to make a new AI, however, you'll find its well worth your time. You can compile everything, load the game up, and see your now-empty AI do its work. Its time to start implementing your AI! Its reccomended you add "using namespace " to the top of your source file for all of the namespaces located in AIEcho.h (they are conviently prototyped at the top of the file).

Constructing Buildings and Flags

How to

For starters, you need to make a BuildingOrder. This is an order that will be passed to Echo once you have provided all of the information to construct a building. A BuildingOrder takes three things in its constructor, a pointer to the player your team is on (obtained from echo.player), the building type (available from IntBuildingType.h, remember to do IntBuildingType::SWARM_BUILDING, instead of just SWARM_BUILDING, and the same for other buildings), and the number of workers that will be used to construct the building. After you've created the building order, you now need to add constraints to the order to choose where the building will be located.

Constraints are added via BuildingOrder::add_constraint. There are five constraints to choose from:

  • MaximumDistance - can't be to far from a provided object. Takes a GradientInfo and an integer for maximum distance.
  • MinimumDistance - can't be to close to a provided object. Takes a GradientInfo and an integer for minimum distance.
  • MaximizedDistance - preferably farther than a provided object, but can be close. Takes a GradientInfo and an integer for weight, affecting how much this effect this constraint has on the final result.
  • MinimizedDistance - preferably closer to a provided object, but can be far. Takes a GradientInfo and an integer for weight, affecting how much this effect this constraint has on the final result.
  • CenteredOn - A special constraint that only allows the building/flag to be centered on another, provided building. Usefull for flags, to center them on an enemy. Will be discussed later.

To use a constraint, however, there is something you must do first. You must set the gradient information for that constraint. Gradient information is set with the GradientInfo class. Gradients are Echo's way of computing the distance to various objects. With a GradientInfo class at hand, you add sources and obstacles for the gradient. A source is something that distance will be counted from. An obstacle is an object that may get in the way of the distance compuation. For example, you want you're building to be placed close to other buildings. However, you don't want direct distance to be counted, because that distance could be going straight through ressources. Instead, you want it to count distance arround the ressources, in which case, you would add an obstacle for the ressources. Here is a list of entities that can count as sources and obstacles:

  • Building - A specifc type of building from a particular team. Takes a building type, a team number, and a boolean for whether the building is allowed to be under construction or not.
  • AnyTeamBuilding - any building from a particular team. Takes a team number, and a boolean for whether the building is allowed to be under construction or not.
  • AnyBuilding - Any building from any team. Takes a boolean for whether the building is allowed to be under construction or not.
  • Ressource - Any ressource of a particular type. Takes a ressource type. Those are listed in Ressources.h
  • AnyRessource - Any ressource. Takes no arguments.
  • Water - Water. Takes no arguments.

You can obtain your team number from echo.player->team->teamNumber. You can use the functions GradientInfo::add_source and GradientInfo::add_obstacle to add sources and obstacles to your gradient information.

When you're finally ready to add your order to echo, use Echo::add_building_order. This function returns an ID that you can use to add ManagementOrders to the function (described later in Managing Buildings).

Annotated Example

 //The main order for the race track, to be constructed using 6 workers.
 AIEcho::Construction::BuildingOrder bo(echo.player, IntBuildingType::WALKSPEED_BUILDING, 6);
 
//Constraints arround the location of wood AIEcho::Gradients::GradientInfo gi_wood; //Add wood as a source for the gradient gi_wood.add_source(new AIEcho::Gradients::Entities::Ressource(WOOD)); //You want to be close to wood, so use a MinimizedDistance. bo.add_constraint(new AIEcho::Construction::MinimizedDistance(gi_wood, 4));
//Constraints arround the location of stone AIEcho::Gradients::GradientInfo gi_stone; //Add stone as a source for the gradient gi_stone.add_source(new AIEcho::Gradients::Entities::Ressource(STONE)); //You want to be close to stone, so use a MinimizedDistance. However, the above gi_wood has a higher weight, so being close //to wood is more important bo.add_constraint(new AIEcho::Construction::MinimizedDistance(gi_stone, 1)); //You don't want to be to close, so you have room to upgrade. You use a MinimumDistance for this. bo.add_constraint(new AIEcho::Construction::MinimumDistance(gi_stone, 2));
//Constraints arround nearby settlement AIEcho::Gradients::GradientInfo gi_building; //Add any building on my team as a source for the gradient. I don't want any construction sites counted, only completed buildings gi_building.add_source(new AIEcho::Gradients::Entities::AnyTeamBuilding(echo.player->team->teamNumber, false)); //You don't want to calculate the distance through ressources, making the AI build on the other side of a big ressource wall. So, //Add AnyRessource as an obstacle gi_building.add_obstacle(new AIEcho::Gradients::Entities::AnyRessource); //You want to be close to other buildings, but as the weight tells you, wheat is more important bo.add_constraint(new AIEcho::Construction::MinimizedDistance(gi_building, 2));
//You don't want to be too close to other buildings AIEcho::Gradients::GradientInfo gi_building_construction; //This time, unlike the above, similar constraint, we want to allow construction sites to be counted gi_building_construction.add_source(new AIEcho::Gradients::Entities::AnyTeamBuilding(echo.player->team->teamNumber, true)); //Again, we don't want to compute distance through ressources. gi_building_construction.add_obstacle(new AIEcho::Gradients::Entities::AnyRessource); //Don't build the racetrack too close to other buildings or construction sites, a minimum distance of 4 means that there are going //to be three blocks in between them. A distance of one would cause the buildings to be right next to eachother. bo.add_constraint(new AIEcho::Construction::MinimumDistance(gi_building_construction, 4));
//Add the building order to the list of orders echo.add_building_order(bo);

Flags

Flags are created in the same manner as other buildings. However, since flags can be placed on top of other buildings, its sometimes usefull have a MaximumDistance constraint set to 0, causing the flag to be put directly on top of another object. This would not be valid for other buildings.

Summary

While constructing a building can be a lengthy ammount of code, you will be happy that your building is placed in a very intelligent location, and that its construction is managed for you. There are other things you can do after the code to order the construction of a building, such as add management orders, which will be explained in another section. This becomes very convienent and usefull.

Misc