Creating a Single Player Mission

Enemy AI - Scripting Attacks (5.9)

Now that you've established some recruit types you can begin sending them out to attack. AI movement is handled through the standard objective actions that trigger other game events. Thus, you can trigger attacks when the player enters a region, a tag is destroyed, or a certain amount of time elapses, just to name a few. AI objectives can be placed in the objectives_ai.cfg file you created earlier, along with the rest of the AI's scripts. AI scripts can be broken down into sub-sections: type of movement, type of recruitment and type of destination. Certain variants can be added to modify these types.

All AI scripts follow a similar pattern, with certain variables supplementing the standard syntax. The objective action ExecuteScript is used to trigger AI movement, and is used in the same manner as other objective actions:

CreateObjectType("ai_patrol", "Objective")
{
  Condition("Timer")
  {
    Time(120);
  }
  Action()
  {
    ExecuteScript("[script name]", "squad.move.tagtoregion")
    {
      Op("%.tag", "=", "[tag name]");
      Op("%.region", "=", "[region name]");
    }
  }
}

The above example utilizes the same format you used to create player objectives. The objective is given a unique name (in this case, "ai_patrol") and an objective condition (a Timer). After the Timer counts down, the AI script is executed. The script itself breaks down as follows. Each script is given a unique name, followed by the script type (in this case, squad.move.tagtoregion). This type requires two variables, the tag to recruit and the region to travel to. In this example, 120 seconds after the objective is assigned to the AI, it will recruit the tag specified and have it travel to the region.

Scripts break down into four different methods of recruiting to fulfill their orders. Thus, you may have squad.move.spawn, squad.move.tag, squad.move.type and squad.move.typebase scripts in your missions. They will all order AI units to travel the map and attack enemies, but they differ in how they fill out their squads. The four types are as follows:

All scripts need a destination. The two types of destinations are regions and trails. A squad sent to a region will remain in that region until destroyed unless drawn out in pursuit of an enemy. Squads sent on one-way trails will follow that trail, then remain at the end point. Squads sent on other trails will remain on them indefinitely, leaving only long enough to engage enemy forces.

Some examples of script types are as follows:

  Action()
  {
    ExecuteScript("[script name]", "squad.move.typetoregion")
    {
      Op("%.types", "=", "[recruit type]");
      Op("%.region.src", "=", "[region name]");
      Op("%.region.dst", "=", "[region name]");
      Op("%.acceptInsufficient","=", 1);
    }
  }

In the above example, the AI will attempt to form the squad from the recruit type specified ("types") located within 100 meters of the source region ("region.src"). It will then send the squad to its destination, specified in "region.dst". In the event there are not enough units to fulfill the recruit type, it will launch the squad anyway. If you want the AI to wait until the recruit type is completely filled, remove the "acceptInsufficient" line.

  Action()
  {
    ExecuteScript("[script name]", "squad.move.typebasetoregion")
    {
      Op("%.types", "=", "[recruit type]");
      Op("%.base", "=", "[base name]");
      Op("%.region", "=", "[region name]");
    }
  }

In this example, the AI is executing a similar move. Instead of recruiting the type from a specific region, however, it is taking whichever units are available from the team (typebase). The squad will then move to the region specified.

  Action()
  {
    ExecuteScript("[script name]", "squad.move.spawntoregion")
    {
      Op("%.types", "=", "[recruit type]");
      Op("%.region.src", "=", "[region name]");
      Op("%.region.dst", "=", "[region name]");
    }
  }

And in this example, the AI will not recruit the units at all. Instead, it will create the recruit type directly at the source region, before moving the squad to the destination region.