AI still did not follow player
by Rushh · in Technical Issues · 12/23/2007 (8:50 am) · 3 replies
Hi I took some codes in this forum and implement it into my codes. But the AI still do not follow the player.
Here is my codes...
function AIPlayer::seekPlayer(%this)
{
%ppos = localclientconnection.player.getTransform();
%bpos = %this.getTransform();
echo("player" SPC %ppos SPC "bot" SPC %bpos);
%dist = vectorSub(%ppos,%bpos);
if(%dist<20.0)
{
%this.followPath("");//don't follow the path
%this.setMoveDestination( %ppos , true);
}
//this scheduling is where the aiplayer will think
//before he hits the player...
%this.schedule(500,seekPlayer,MyBot);
}
I put it in bot.cs which is the AI player. The bot just stand there doing nothing..
Here is my codes...
function AIPlayer::seekPlayer(%this)
{
%ppos = localclientconnection.player.getTransform();
%bpos = %this.getTransform();
echo("player" SPC %ppos SPC "bot" SPC %bpos);
%dist = vectorSub(%ppos,%bpos);
if(%dist<20.0)
{
%this.followPath("");//don't follow the path
%this.setMoveDestination( %ppos , true);
}
//this scheduling is where the aiplayer will think
//before he hits the player...
%this.schedule(500,seekPlayer,MyBot);
}
I put it in bot.cs which is the AI player. The bot just stand there doing nothing..
#2
This is my bot.cs
12/23/2007 (8:34 pm)
HI, still no luck. The bot is still not moving. But when i execute the function, the bot position is the same as the player. Its just that the bot doesnt move or update itself. Any idea?This is my bot.cs
datablock PlayerData( MyBot : PlayerBody)
{
shapeFile = "~/data/shapes/player/player.dts";
};
function MyBot::onReachDestination( %this, %obj )
{
// Moves to the next node on the path.
if( %obj.path !$= "" )
{
if( %obj.currentNode == %obj.targetNode )
%this.onEndOfPath( %obj, %obj.path );
else
%obj.moveToNextNode();
}
else
echo( "MyBot::onReachDestination warning - Path is blank!" );
}
function MyBot::onEndOfPath( %this, %obj, %path )
{
%obj.nextTask();
}
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer::spawn( %name, %spawnPoint )
{
// Create the A.I. driven bot object...
%player = new AIPlayer()
{
dataBlock = MyBot;
path = "";
};
MissionCleanup.add( %player );
%player.setShapeName( %name );
%player.setTransform( %spawnPoint );
return %player;
}
function AIPlayer::spawnOnPath( %name, %path )
{
// Spawn a bot and place him on the first node of the path
if( !isObject( %path ) )
{
echo( "AIPlayer::spawnOnPath failed - Bad Path!" );
%this.path = "";
return;
}
%node = %path.getObject(0);
%player = AIPlayer::spawn( %name, %node.getTransform() );
return %player;
}
function AIPlayer::followPath( %this, %path, %node )
{
// Start the bot following a path
%this.stopThread(0);
if( !isObject( %path ) )
{
echo( "AIPlayer::followPath failed - Bad Path!" );
%this.path = "";
return;
}
if( %node > %path.getCount() - 1 )
%this.targetNode = %path.getCount() - 1;
else
%this.targetNode = %node;
if( %this.path $= %path )
%this.moveToNode(%this.currentNode);
else
{
%this.path = %path;
%this.moveToNode(0);
}
}
function AIPlayer::moveToNextNode( %this )
{
if( %this.targetNode < 0 || %this.currentNode < %this.targetNode )
{
if( %this.currentNode < %this.path.getCount() - 1 )
%this.moveToNode( %this.currentNode + 1 );
else
%this.moveToNode( 0 );
}
else
{
if( %this.currentNode == 0 )
%this.moveToNode( %this.path.getCount() - 1 );
else
%this.moveToNode( %this.currentNode - 1 );
}
}
function AIPlayer::moveToNode( %this, %index )
{
// Move to the given path node index
%this.currentNode = %index;
%node = %this.path.getObject(%index);
%this.setMoveDestination( %node.getTransform(), %index == %this.targetNode );
}
function AIPlayer::seekPlayer(%this)
{
%ppos = localclientconnection.player.getPosition();
%bpos = %this.getPosition();
echo("player" SPC %ppos SPC "bot" SPC %bpos);
%dist = vectorDist(%ppos,%bpos);
echo("fghfghfgh " @ %dist);
if(%dist< 20)
{
echo("aaaaaaa " @ localclientconnection.player.getTransform());
%this.setMoveDestination( localclientconnection.player.getTransform() , true);
%this.followPath("");//don't follow the path
}
//this scheduling is where the aiplayer will think
//before he hits the player...
%this.schedule(5,seekPlayer,MyBot);
}
//-----------------------------------------------------------------------------
// AIManager static functions
//-----------------------------------------------------------------------------
function AIManager::think( %this )
{
if( !isObject( %this.player ) )
%this.player = %this.spawn();
%this.schedule( 500, think );
}
function AIManager::spawn( %this )
{
// Bot_1 simply spawns without a path to follow. Since he has no path to
// follow, he'll simply spawn at the same spot that you do. This means that
// you'll have to move around to see him when the game first starts because
// he'll be right on top of you.
//%bot = AIPlayer::spawn( "Bot_1", pickSpawnPoint() );
// Bot_2 has a path to follow, so we'll help him out by having him spawn
// by the Path's first Path Marker. Once he spawns, he'll begin to follow
// the path using the A.I. helper functions defined here in this file.
%bot = AIPlayer::spawnOnPath( "Bot_2", "MissionGroup/myPath" );
%bot.followPath( "MissionGroup/myPath", -1 );
return %bot;
}
#3
12/23/2007 (10:45 pm)
You're never calling the function, so it will do nothing. I think this will work though. I got rid of all the code to follow paths, because I wasn't using it.$aischedule = 0;
datablock PlayerData( MyBot : PlayerBody)
{
shapeFile = "~/data/shapes/player/player.dts";
};
function MyBot::onReachDestination( %this, %obj )
{
// Moves to the next node on the path.
%obj.seekPlayer();
}
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer::spawn( %name, %spawnPoint )
{
// Create the A.I. driven bot object...
%player = new AIPlayer()
{
dataBlock = MyBot;
path = "";
};
MissionCleanup.add( %player );
%player.setShapeName( %name );
%player.setTransform( %spawnPoint );
return %player;
}
function AIPlayer::spawnOnPath( %name, %path )
{
// Spawn a bot and place him on the first node of the path
if( !isObject( %path ) )
{
echo( "AIPlayer::spawnOnPath failed - Bad Path!" );
%this.path = "";
return;
}
%node = %path.getObject(0);
%player = AIPlayer::spawn( %name, %node.getTransform() );
return %player;
}
function AIPlayer::seekPlayer(%this)
{
cancel($aischedule)
%ppos = localclientconnection.player.getPosition();
%bpos = %this.getPosition();
echo("player" SPC %ppos SPC "bot" SPC %bpos);
%dist = vectorDist(%ppos,%bpos);
echo("fghfghfgh " @ %dist);
if(%dist< 20)
{
echo("aaaaaaa " @ localclientconnection.player.getTransform());
%this.setMoveDestination( localclientconnection.player.getTransform() , true);
%this.followPath("");//don't follow the path
}
//this scheduling is where the aiplayer will think
//before he hits the player...
$aischedule = %this.schedule(500,seekPlayer,MyBot);
}
//-----------------------------------------------------------------------------
// AIManager static functions
//-----------------------------------------------------------------------------
function AIManager::spawn( %this )
{
// Bot_1 simply spawns without a path to follow. Since he has no path to
// follow, he'll simply spawn at the same spot that you do. This means that
// you'll have to move around to see him when the game first starts because
// he'll be right on top of you.
//%bot = AIPlayer::spawn( "Bot_1", pickSpawnPoint() );
// Bot_2 has a path to follow, so we'll help him out by having him spawn
// by the Path's first Path Marker. Once he spawns, he'll begin to follow
// the path using the A.I. helper functions defined here in this file.
%bot = AIPlayer::spawnOnPath( "Bot_2", "MissionGroup/myPath" );
%bot.seekPlayer( "MissionGroup/myPath", -1 );
return %bot;
}
Torque Owner Tyler Slabinski