Game Development Community

AI Paths and routes

by Martin Whittington · in Torque 3D Beginner · 05/05/2011 (6:27 am) · 2 replies

Hey,

I am coding my AI man to walk a path, which is in a simgroup called patrol, which in that has 3 routes (route1,2 and 3).

I'm having trouble getting the ai to switch to a new path when he has finished his circuit. Here is the ai code I have so far:

function spawnPatrolonPath(%patrol){
%ai = new AIPlayer(){
dataBlock = AIBasicDB;
arcOfSight=60;
sightRange=50;
approachRange=10;
state = "PATROL";
aiPlayer = true;
patrol = %patrol;
currentNode = 0;
currentPath = 1;
};
MissionCleanup.add(%ai);
// Assign my routes to variables I can use
%node = %patrol.getObject(%ai.currentNode);
%ai.setTransform(%node.getTransform());
// So my AI doesnt run, but does a steady patrol
%ai.setMoveSpeed(0.5);
%ai.moveToNextNode(%aiPlayer);
}

function AIPlayer::moveToNextNode(%aiPlayer){
if (%aiPlayer.CurrentNode < %aiPlayer.patrol.getCount() -1){
%aiPlayer.CurrentNode++;
}
else{
%aiPlayer.CurrentNode = 0;
}
%aiPlayer.moveToNode(%aiPlayer.CurrentNode);
}

function AIPlayer::moveToNode(%aiPlayer,%nodeIndex){
// Obtain a reference to the node
%node = %aiPlayer.patrol.getObject(%nodeIndex);
// Move to node and slow down as node is reached
%aiPlayer.setMoveDestination(%node.getTransform(),true);
}


function AIBasicDB::onReachDestination(%this,%aiPlayer){
// Added to determine if the NPC had completed a full circuit
if (%aiPlayer.currentNode == 0){
%this.onEndOfPath(%aiPlayer,%aiPlayer.patrol);
}
else {
// Move to the next node on the path
%aiPlayer.moveToNextNode();
}
}

function AIBasicDB::onEndOfPath(%this,%aiPlayer,%patrol){
echo("***PATH END***");
%aiPlayer.currentPath++;
}

function AIBasicDB::Think(%this,%aiPlayer){
%aiPlayer.setAimObject(LocalClientConnection.getControlObject());
%aiPlayer.getDataBlock().schedule(1000,"Think",%aiPlayer);
}

About the author

Recent Threads


#1
05/05/2011 (9:19 am)
function AIBasicDB::onEndOfPath(%this,%aiPlayer,%patrol){
echo("***PATH END***");
%aiPlayer.currentPath++;
}
Doesn't actually do anything except increment the currentPath number. At this point you should send the bot to the starting location of the new path number. Once there have it follow the new path as normal.
#2
05/05/2011 (2:23 pm)
Ok thanks for your reply, but how do I send my Bot to the next path. Do I need to assign e.g %route1 to nametoid...path name? or can I use the currentpath increment to dictate which path to goto. Eventually I want the patrol bot to randomly choose which path to goto