Week 9
Intro to AI - Doing Stupid Stuff Since '97
You ever wonder why the AI always starts with much more stuff than you do? Because it sucks at playing OP2. If you were to write a simple AI that started with a default LoS setup and fought you, 1-on-1, in a fair fight, it would lose. Wouldn't even be much of a challenge, really. But I'll do my best to help you write a somewhat decent AI. Ready? Let's start with the basics: setting up groups, building its base and mining ore.
AI GroupsFor the AI to function at all, you need to organize its units into specialized groups for construction, repairing, mining ore, (re)building mines, attacking, defending, etc. Here are some important functions common to all unit groups:
Now, some examples of how you would use some of those functions:
// Example
// Create a group to defend our base
FightGroup BaseDefense; Â // Note: For now, this should be a global variable
...
(in InitProc)
// Initialize it
BaseDefense = CreateFightGroup(Player[playerAI]);
// Create some units for it
Unit Unit1;
TethysGame::CreateUnit(Unit1, mapLynx, ... );
BaseDefense.TakeUnit(Unit1);
TethysGame::CreateUnit(Unit1, mapLynx, ... );
BaseDefense.TakeUnit(Unit1);
TethysGame::CreateUnit(Unit1, mapLynx, ... );
BaseDefense.TakeUnit(Unit1);
// Do some stuff to the group
BaseDefense.SetLights(1);
BaseDefense.SetTargCount(mapLynx, mapLaser, 3);
...
// If we research Rail Gun, replace the Lasers with Rail Guns
SCRIPT_API void RailGunResearched()
{
  BaseDefense.ClearTargCount();
  BaseDefense.SetTargCount(mapLynx, mapRailGun, 3);
}
...
// If we get Thor's Hammer, destroy this group
SCRIPT_API void ThorsResearched()
{
  BaseDefense.Destroy();
}
Now let's talk about the special functions available to the different types of groups
Bulding GroupsDespite the name, building groups don't necessarily deal with construction. There are four sub-types of the building group:
1) Building Construction
2) Building Repair
3) Mine Construction
4) Vehicle (Re)Building
5) Walls and Tubes
Let's start at the top. You may have already figured this out, but building construction groups are what you use to tell the AI to build structures. Such a group consists of three things:
1) ConVecs
2) A Structure Factory
3) A list of buildings to build (and rebuild if they get destroyed. This should include any buildings the AI starts the game with)
Repair groups are simple. You just need a ConVec (can reuse one from the build group), a Repair Vehicle, or a Spider. I'll explain using them below.
Mine construction is basically the same as construction, except you need:
1) Robo-Miner
2) A list of all mines to be built/rebuilt
Rebuilding vehicles is easy. You need:
1) At least one Vehicle Factory
2) A list of all unit groups, with an assigned priority
Building walls and tubes are just like structures. You need:
1) Earthworker(s)
2) List of tubes to build/rebuild
3) List of Normal Walls to build/rebuild
4) List of Lava Walls to build/rebuild (optional)
5) List of Microbe Walls to build/rebuild (optional)
All Building Group Functions:
Examples:
You should give that code a try. Go make a quick little colony game demo where the AI just builds a little base and some vehicles. Might want to give it unlimited resources for now. Also, keep in mind that the order you record a building to the build list is the order it will be rebuilt in.
Okay, go write that little AI demo! I'll be waiting when you get back, and we'll move on to mining and combat.