Game Development Community

Finding nearby ai's

by Christian · in Torque Game Engine · 04/29/2007 (12:35 pm) · 4 replies

Is there a resource on finding nearby ai's. I just need to search for the closest ai out of for example 100. I've done it with human players before fine, but not ai's. If I get their id I can do all the distance figuring, but a way to actually search all of them without giving them a globabl variable. Thanks.

#1
04/29/2007 (1:22 pm)
If you set the bot player object to .isAI = 1; or .isBot = 1; when you create them like
// Create the player object
   %player = new AIPlayer() {
      dataBlock = DemoPlayer;
      isBot = 1;
   };
   MissionCleanup.add(%player);

and you know how to find human players just put a if statement in the loop to check if the player is a bot

hope this is clear enough, Vincent
#2
04/29/2007 (1:28 pm)
%count = ClientGroup.getCount();
	for(%i = 0; %i < %count; %i++)
		{
		%client = ClientGroup.getObject(%i);
		%playPos = %client.actor.getPosition();
		%tempDist = VectorDist(%playPos, %botPos);
		if(%i == 0) {
		%dist = %tempDist;
		%index = %i;

is what I was using for finding human players. I didn't wright the code so i'm not entirely sure how it uses ClientGroup to find them. Is there a way to switch ClientGroup to BotGroup or AIGroup?
#3
04/29/2007 (2:07 pm)
I wast just editing my post when i received a mail from GG that a reply was posted. So ill just continue in a new reply

I already though u would use the clientgroup for finding human players. Ofcourse there are always serveral ways of doing things. How you would find AI players depends on how you implent and add your bots.

you could use such a loop for finding players. You should not copy this directly cause it wont work. Its just an example i copied (and changed a bit) from an AI targeting script

InitContainerRadiusSearch(%player.getPosition(), %maxRange, $TypeMasks::PlayerObjectType);

         while ((%targetObject = containerSearchNext()) != 0)
               {
               if(%targetObject.team.teamId == %player.client.team.teamId) continue;
               //echo("do we even get here?");
               if(%targetObject.getDamageState() $= "Disabled") continue;
               
               // maybe should just add "isInvisible" to car (in script)
               if(%targetObject.isCloaked() && %targetDistance < 25) continue;

               if(!%targetObject.isBot) continue;

               %targetDistance = vectorDist(%player.getTransform(), %targetObject.getTransform());

               if(%targetDistance < %nearestTarget)// closest target
               {
                  if(%targetDistance < %maxRange) // 55
                  {
                     %player.aimTarget = %targetObject;
                     %nearestTarget = %targetDistance;
                     %targetAcquired = true;
                  }

               }
         }

         if (%targetAcquired)
         {
             %Target = %player.aimTarget;
             // target is the closest player
         }

You could also do the following when you create your bots;

in StartGame() add something like
$Game::Bots = 0;

then where you create your bot add
$Game::Bots++;
   AIManager.bot[$Game::Bots] = %bot; // % bot is the player object

then you could use the loop you are using for finding bot players
%count = $Game::Bots;
	for(%i = 0; %i <= %count; %i++)
		{
		%bot = AIManager.bot[%i];
		%playPos = %bot.getPosition();
		%tempDist = VectorDist(%playPos, %botPos);
		if(%i == 0) {
		%dist = %tempDist;
		%index = %i;

that should work
#4
04/29/2007 (2:10 pm)
Thank you, i'll try this out