Multiple AI Spawn points
by Matt Sanders · in Torque Game Engine · 09/21/2004 (7:05 pm) · 2 replies
I am trying to make one AI character spawn at each AI spawn point that I set. that way for later maps all I have to do is put the AI spawn spheres that I have created into positions that I like and the AI will spawn there. I was wondering how would be a good way to do this. I was thinking about using a for loop and an array but I'm not sure how well that will work. (I am currently modifying the AIplayer.cs to do this. This is what My AIRobot.cs file looks like:
well the most important functions anyway...)
datablock PlayerData(Robot : RobotBody)
{
shootingDelay = 2000;
};
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer::spawn(%name,%spawnPoint)
{
// Create the Robot
%player = new AiPlayer()
{
dataBlock = Robot;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint);
return %player;
}
//-----------------------------------------------------------------------------
// AIPlayer methods
//-----------------------------------------------------------------------------
function AIPlayer::ChooseState(%State)
{
switch$(%State)
{
case "scared": //character is scared
AIPlayer::scared();
case "mad": //Character is mad
AIPlayer::mad();
default: //Character doesn't care
AIPlayer::uncare();
}
}
//function for the AI when he is scared
function AIPlayer::scared()
{
echo("now I am scared");
}
//function for the AI when he is mad
function AIPlayer::mad()
{
echo("ok you pissed me off");
}
//function for the AI when he doesn't care
function AIPlayer::uncare()
{
echo("Yeah I do not care");
}
//-----------------------------------------------------------------------------
function AIPlayer::singleShot(%this)
{
// The shooting delay is used to pulse the trigger
%this.setImageTrigger(0,true);
%this.setImageTrigger(0,false);
%this.trigger = %this.schedule(%this.shootingDelay,singleShot);
}
//-----------------------------------------------------------------------------
function AIManager::think(%this)
{
// We could hook into the player's onDestroyed state instead of
// having to "think", but thinking allows us to consider other
// things...
if (!isObject(%this.player))
%this.player = %this.spawn();
%this.schedule(500,think);
//****************************THIS IS THINKING CODE************************
}
function AIManager::spawn(%this)
{
%i = 22;
%spawnPoint = pickRobotSpawnPoint();
%player = AIPlayer::spawn("Robot" @ %i,%spawnPoint);
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
return %player;
}
function pickRobotSpawnPoint()
{
%groupName = "MissionGroup/ROBOTDropPoints";
%group = nameToID(%groupName);
if (%group != -1) {
%count = %group.getCount();
if (%count != 0) {
%index = getRandom(%count-1);
%spawn = %group.getObject(%index);
return %spawn.getTransform();
}
else
error("No spawn points found in " @ %groupName);
}
else
error("Missing spawn points group " @ %groupName);
// Could be no spawn points, in which case we'll stick the
//player at the center of the world.
return "0 0 300 1 0 0 0";
}
well the most important functions anyway...)
datablock PlayerData(Robot : RobotBody)
{
shootingDelay = 2000;
};
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer::spawn(%name,%spawnPoint)
{
// Create the Robot
%player = new AiPlayer()
{
dataBlock = Robot;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint);
return %player;
}
//-----------------------------------------------------------------------------
// AIPlayer methods
//-----------------------------------------------------------------------------
function AIPlayer::ChooseState(%State)
{
switch$(%State)
{
case "scared": //character is scared
AIPlayer::scared();
case "mad": //Character is mad
AIPlayer::mad();
default: //Character doesn't care
AIPlayer::uncare();
}
}
//function for the AI when he is scared
function AIPlayer::scared()
{
echo("now I am scared");
}
//function for the AI when he is mad
function AIPlayer::mad()
{
echo("ok you pissed me off");
}
//function for the AI when he doesn't care
function AIPlayer::uncare()
{
echo("Yeah I do not care");
}
//-----------------------------------------------------------------------------
function AIPlayer::singleShot(%this)
{
// The shooting delay is used to pulse the trigger
%this.setImageTrigger(0,true);
%this.setImageTrigger(0,false);
%this.trigger = %this.schedule(%this.shootingDelay,singleShot);
}
//-----------------------------------------------------------------------------
function AIManager::think(%this)
{
// We could hook into the player's onDestroyed state instead of
// having to "think", but thinking allows us to consider other
// things...
if (!isObject(%this.player))
%this.player = %this.spawn();
%this.schedule(500,think);
//****************************THIS IS THINKING CODE************************
}
function AIManager::spawn(%this)
{
%i = 22;
%spawnPoint = pickRobotSpawnPoint();
%player = AIPlayer::spawn("Robot" @ %i,%spawnPoint);
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
return %player;
}
function pickRobotSpawnPoint()
{
%groupName = "MissionGroup/ROBOTDropPoints";
%group = nameToID(%groupName);
if (%group != -1) {
%count = %group.getCount();
if (%count != 0) {
%index = getRandom(%count-1);
%spawn = %group.getObject(%index);
return %spawn.getTransform();
}
else
error("No spawn points found in " @ %groupName);
}
else
error("Missing spawn points group " @ %groupName);
// Could be no spawn points, in which case we'll stick the
//player at the center of the world.
return "0 0 300 1 0 0 0";
}
#2
function AIManager::initiate()
{
%Robotsloaded = false; //robots have not been loaded yet (no purpose yet)
%groupName = "MissionGroup/ROBOTDropPoints";
%group = nameToID(%groupName);
if (%group != -1) {
//first load spawn points and save them into an array...
for (%count = %group.getCount();%count > 0;%count--)
{
%spawn = %group.getObject(%count - 1);
%spawnarray[%count + 1] = %spawn.getTransform();
}
//now place a character at each spawnpoint...
for (%count2 = %group.getCount();%count2 > 0;%count2--)
{
%player = AIPlayer::spawn("Robot" @ %count2,%spawnarray[%count2 + 1]);
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
}
}
else
error("Missing spawn points group " @ %groupName);
AIManager.think();
}
####I am not sure why but I do not need the + 1 on the arrays... I took it out and it still worked the same... Do arrays work differently in Torque or am I just confusing myself.... I just don't want to place data where it doesn't belong.
09/21/2004 (8:43 pm)
HEHE I got the characters to load according to spawn points... now I need to make sure I can control them all individually. (this is where the magic happens)function AIManager::initiate()
{
%Robotsloaded = false; //robots have not been loaded yet (no purpose yet)
%groupName = "MissionGroup/ROBOTDropPoints";
%group = nameToID(%groupName);
if (%group != -1) {
//first load spawn points and save them into an array...
for (%count = %group.getCount();%count > 0;%count--)
{
%spawn = %group.getObject(%count - 1);
%spawnarray[%count + 1] = %spawn.getTransform();
}
//now place a character at each spawnpoint...
for (%count2 = %group.getCount();%count2 > 0;%count2--)
{
%player = AIPlayer::spawn("Robot" @ %count2,%spawnarray[%count2 + 1]);
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,1000);
}
}
else
error("Missing spawn points group " @ %groupName);
AIManager.think();
}
####I am not sure why but I do not need the + 1 on the arrays... I took it out and it still worked the same... Do arrays work differently in Torque or am I just confusing myself.... I just don't want to place data where it doesn't belong.
Torque Owner Matt Sanders