Increasing AI Awareness with Locations

arma2OA 2012 AI Patrol

AI On Patrol

All stock Arma maps and most custom maps have locations hard coded into the Map data. These locations are classified by type and can tell your script about nearby towns, hills, vineyards etc.

Using the maps locations you can attempt to script some more strategic decisions for your AI. This could be of particular use if you have AI spawned randomly but want to generate them some useful, realistic waypoints.

To Illustrate this I’m going to build a simple script that will:

  1. find the locations that I’m interested in
  2. sort the locations by distance from the AI
  3. create waypoints to the locations
To start I’ve inserted an infantry group in the mission editor and placed the following script in the squad leader’s initialization:

_null = this execVM "findlocations.sqf"

This will call my script findlocations.sqf and pass a reference to the squad leader (this). Now I save my mission and alt-tab out to my trusty text editor to build my script.

The first two lines of my script look like this:

_pos = getPos _this;
_group = group _this;

This will create two local variables:  _pos holds the current position of the squad leader, _group will hold the name of the squad leaders group. We will need this to build waypoints for the group later.

Next I’m going to grab the positions of the nearest location types that I’m interested in.

The standard location types as listed in the biki are

Strategic
StrongpointArea
FlatArea
FlatAreaCity
FlatAreaCitySmall
CityCenter
Airport
NameMarine
NameCityCapital
NameCity
NameVillage
NameLocal
Hill
ViewPoint
RockArea
BorderCrossing
VegetationBroadleaf
VegetationFir
VegetationPalm
VegetationVineyard

 

I am only interested in two types of locations, “Strategic” and “StrongpointArea”. So I build an array of the location types I’m interested in:

_locations =  ["Strategic","StrongpointArea"];

nearestLocations calculates which location is closest to _pos (the current location of my AI squad leader) within a certain radius.

_myLocations = nearestLocations [_pos, _locations, 1000];

So in my example I’m grabbing all the “Strategic”,”StrongpointArea” locations within 1000m of my squad leader (as represented by _pos)  and stuffing them into the array _myLocations.

_myLocations is automatically is sorted from closest location to furthest, so all I need to do now is build a waypoint for my AI group and send them on their way.

_wp = _group addWaypoint [position (_myLocations select 0) , 0 ];
_wp setWaypointType "MOVE";

Where  (_myLocations select 0) is the first element of the _myLocations array – i.e. the closest location. I use the position of  that location to crate a waypoint for my ai _group.

If you put it all together my script looks like this:

_pos = getPos _this;
_group = group _this;

_locations = [“Strategic”,”StrongpointArea”];

_myLocations = nearestLocations [_pos, _locations, 1000];

_wp = _group addWaypoint [position (_myLocations select 0),0 ];
_wp setWaypointType “MOVE”;

 

So now, no matter where I place this group on the map they will automatically get assigned a waypoint to the nearest strategic or strongpoint location on the map.

 

Reference:

execVM
CfgLocationTypes
nearestLocations
addWaypoint