How to add a patrol route to NeoAxis using NA 1.3 




Prerequisites are of course that you have installed Visual Studio 2010 and after that you have opened the NeoAxis solution from the NeoAxis Engine Non-Commercial SDK 1.31\Game\Src\NonCommercialLicense.sln

First we need to add a new variables to GameCharacter.cs that will hold our route and the way we want to patrol it. You find the classes in GameEntities/Action Specific



*SNIP*
public class GameCharacter : Character
{
GameCharacterType _type = null; public new GameCharacterType Type { get { return _type; } }

[FieldSerialize] //wellu: save this value
[DefaultValue(typeof(MovementPreference), "Patrol")] //this is our default selected value
private MovementPreference movPref; //this will hold our preference

public MovementPreference MovementPref //accessor for the variable. Not necessary, but could have more logic in 'set' and 'get'
{
get { return movPref; }
set { movPref = value; }
}

[FieldSerialize]
private MapObject movRoute; //will hold a MapCurve that we place on the map as patrol route


public MapObject MovementRoute //accessor for the MapCurve. Lets create some logic for the 'set'
{
get { return movRoute; }
set {
if( value is MapCurve) //accept only certain MapObjects
{
movRoute = value;
}
else movRoute = null;
}
}


public enum MovementPreference //all our movement types
{
Patrol,
Random,

}
*SNIP*



Then we need to place some logic inside GameCharacterAI.cs that will actually perform our movements. We modify the FindAutomaticTask() so that our GameCharacter will patrol if there is nothing else to do



*SNIP*
using System.Collections; //Add arraylist
*SNIP*




*SNIP*
public class GameCharacterAI : AI
{
//cached list of weapons for better performance
Weapon[] weapons;

ArrayList route; //wellu: new variable for the route
int routeIndex = 0; //index for route points
*SNIP*



Modified FindAutomaticTask(). Modified code starts at 'wellu->' the code before that is for you to find the correct spot



*SNIP*
//wellu->
else //try to wander around if there is a Patrol or random movementPrefenrence in GameCharacter & map
{
MapCurve mapCurve = controlledObj.MovementRoute as MapCurve; //get the MapCurve from the object this AI controls (GameCharacter on the map)

if (mapCurve != null) //was there one set for this GameCharacter?
{
if (route == null) //initialize patrol route, if not already done
{
route = new ArrayList();

foreach (MapCurvePoint point in mapCurve.Points) //add every MapCurvePoint as a waypoint in our route
{
route.Add(point);
}
}

if (controlledObj.MovementPref == GameCharacter.MovementPreference.Patrol) //if Patrol was selected in Map Editor
{
//create a movement task for the next point
MapCurvePoint pt = route[routeIndex] as MapCurvePoint;

this.AutomaticTasks = GameCharacterAI.AutomaticTasksEnum.EnabledOnlyWhenNoTasks; //do this only if there are no other tasks
this.DoTask(new GameCharacterAI.MoveTask(this, pt.Position, .5f), true); //create a new move task and use current CurvePoint as destination
routeIndex++; //next route waypoint

//reverse the route if we are at the end

if (routeIndex >= route.Count)
{
routeIndex = 0;
route.Reverse();
}
return true; //we found something to do!
}
else // we are not patrolling. Select a random destination from the curve
{
Random rnd = new Random();
MapCurvePoint pt = route[rnd.Next(route.Count)] as MapCurvePoint;
this.AutomaticTasks = GameCharacterAI.AutomaticTasksEnum.EnabledOnlyWhenNoTasks;
this.DoTask(new GameCharacterAI.MoveTask(this, pt.Position, .5f), false);
return true;
}
}
}//<-wellu
return false;
}

public bool AlwaysRun

*SNIP*



We are now ready to go. Just press F6 to rebuild the solution in Visual Studio. You can now open the Map Editor.

I selected the TankDemo map and I added a MapCurve from Base objects. When you select the MapCurve a Create Point button will appear on the down right of the UI. Use this to add more waypoints.
Give your MapCurve a name like 'patrol'




When your route is done add a Girl to the map and set her MovementRoute and select the MapCurve you name as 'patrol'. After that is done just press 'play' button in Map Editor and watch her patrol!