3D All in one chapter 7 aiGuard
by Michael · in Torque Game Engine · 04/05/2009 (10:55 am) · 5 replies
'Ello all. I am having a problem with the aiGuard.cs. Ive been screwing with it for 27 hours straight now and am making no progress.
The game and class compile error free, the bots spawn and look around like they are supposed to. But they do not register my player as a threat, target or shoot at it. I know theres another thread on this, but ive tried everything there to no avail, and its over a year old, so im starting a new one.
Here is the threat code...
get closest enemy....
arc of sight....
I dont really know what other code would be relevant for you guys to possibly help me, but ill be more than willing to dig through the cs files and post anything that might shed light on the problem. Any help will be GREATLY appreciated.
The game and class compile error free, the bots spawn and look around like they are supposed to. But they do not register my player as a threat, target or shoot at it. I know theres another thread on this, but ive tried everything there to no avail, and its over a year old, so im starting a new one.
Here is the threat code...
function AIGuardDB::checkForThreat(%this,%obj)
{
DebugPrint("%this:"this@"~AIGuardDB::checkForThreat (from:"@% obj@")",
"checkForThreat");
if(!isObject(%obj))
return;
if(isObject(%obj))
{
%idx = %obj.getClosestEnemy();
if (%idx < 0)
return 0;
%target = ClientGroup.getObject(%idx); //player as targets
if (!%obj.CheckArcOfSight(%target.player))
return;
if (%obj.attentionLevel > 0) //if attention level not zero keep looking
{
%testRange = %obj.range * (%obj.aggression*2);
}
else
{
%testRange = %obj.range;
}
}
else
{
return 0;
}
if (%target.player == %obj.currentTarget)
{
if (isObject(%obj))
{
if ( %obj.GetTargetRange(%target.player) < %testRange)
{
return %target.player;
}
}
else
return 0;
}
else //new threat
{
if (isObject(%obj))
{
if ( %obj.GetTargetRange(%target.Player) < %testRange)
{
return %target.player;
}
}
else
return 0;
}
DebugPrint( "no threat (from:"@%obj@")","checkForThreat");
return 0;
}
function AIGuardDB::DoScan(%this,%obj)
{
DebugPrint("%this:"@%this@"~AIGuardDB::DoScan (from:"@%obj@")","DoScan");
if (!isObject(%obj))
return;
cancel (%this.scheduledCheck);
if (%obj.attentionLevel <= 0) //if attention not zero keep looking
%obj.attentionLevel = 0;
else
%obj.attentionLevel --;
if (%obj.attentionLevel == 0)
{
%look = getRandom($NUM_CARDINALS-1);
if (%this.look != %look)
%this.look = %look;
else
{
%this.look = %look + 1;
if (%this.look < $NUM_CARDINALS -1)
%this.look++;
else
%this.look = 0;
}
}
%obj.setAimLocation($cardinalDirection[%this.look]);
if ( (%tgtPlayer =%this.checkForThreat(%obj)) != 0)
{
if (%obj.currentTarget)
{
if (%obj.currentTarget == %tgtPlayer)
{
DebugPrint( "Still a threat(from:"@obj@")","DoScan");
%obj.setAimObject(%tgtPlayer);
%obj.attentionLevel = %obj.attention;
}
else
{
DebugPrint( "CHANGED THREAT (from:"@%obj@")", "DoScan");
%obj.currentTarget = %tgtPlayer;
%obj.setAimObject ( %obj.currentTarget);
}
}
else
{
DebugPrint(" NEW THREAT!! (from:"@%obj@")","DoScan");
%obj.setAimObject(%tgtPlayer);
%obj.currentTarget = %tgtPlayer;
%obj.attentionLevel = %obj.attention;
}
}
else
{
if ( %obj.getAimObject)
{
%obj.clearAim();
DebugPrint( " > %obj.clearAim (from:"@%obj@")", "DoScan");
%obj.currentTarget = 0; //forget this target
}
%this.nextScan =
%this.schedule($MIN_SCAN_GAP+getRandom($MAX_SCAN_GAP/%this.alertness),
"doScan",%obj);
}
}get closest enemy....
function AIPlayer::getClosestEnemy(%this)
{
//DebugPrint( "%this: "@%this@"~AIPlayer::getClosestEnemy", getClosestEnemy);
%index = -1;
%botPos = %this.getPosition();
%count = ClientGroup.getCount();
for (%i = 0; %i < %count; %i++)
{
%client = ClientGroup.getObject(%i);
if (%client.player $="" || %client.player == 0)
return -1;
%playPos = %client.player.getPosition();
%tempDist = VectorDist(%playPos, %botPos);
if( %i == 0)
{
%distance = %tempDist;
%index = %i;
}
else
{
if (%distance > %tempDist)
{
%distance = %tempDist;
%index = %i;
}
}
}
return %index;
}arc of sight....
function AIPlayer::CheckArcOfSight(%this, %that)
{
%relbearing = %this.GetRelativeBearing(%that);
if ( (%relbearing > - ($ARC_OF_SIGHT/2)) && (%relbearing < ($ARC_OF_SITE/2)) )
%result = true;
else
%result = false;
return %result;
}I dont really know what other code would be relevant for you guys to possibly help me, but ill be more than willing to dig through the cs files and post anything that might shed light on the problem. Any help will be GREATLY appreciated.
#2
I bought this book used from school (DeVry) for class, and it didnt have the companion CD. I had to manually type and debug over 500 lines of code. Would it be possible to email me the .cs file from the CD if you have it? I have checked thuroughly a dozen times, but it is a pretty long file, maybe theres something I missed that im not seeing.
04/06/2009 (8:11 pm)
The code is line for line as it is from the book. The only change I made is from "Lightarmormale" to "playerBody" so that it would compile. If theres a bool somewhere else I need to change the chapter wouldnt address it. I bought this book used from school (DeVry) for class, and it didnt have the companion CD. I had to manually type and debug over 500 lines of code. Would it be possible to email me the .cs file from the CD if you have it? I have checked thuroughly a dozen times, but it is a pretty long file, maybe theres something I missed that im not seeing.
#3
Does getClosestEnemy return a value?
does arcLineofsight return?
First steps are to narrow down where things are failing.
I'd recommended you get yourself either Torsion or TorqueDev which are editors and will allow you to debug the scripts line by line by stepping through when they execute and see what it's actually doing... Not only a great way to fix things but also a great way to learn.
04/07/2009 (4:41 am)
Can't spot anything obvious reading the scripts, you mention you've debugged it so where does it stop working?Does getClosestEnemy return a value?
does arcLineofsight return?
First steps are to narrow down where things are failing.
I'd recommended you get yourself either Torsion or TorqueDev which are editors and will allow you to debug the scripts line by line by stepping through when they execute and see what it's actually doing... Not only a great way to fix things but also a great way to learn.
#4
It stops working when its time to recognize the threat and shoot at it. Running the debug the bot just scans repeatedly, it does register the player, it just doesnt perceive it to be an enemy.
Thanks for the pointer to Torsion, I didnt even know something like that existed. Ive been using UltraEdit, which is useless for debugging in itself. I will hafta get back to this another time though :(, the assignment is already passed due and I cant waste more time nitpicking over this class. I think im just going to need to write some simpler behaviors for my AI from scratch.
04/07/2009 (12:40 pm)
@Andy It stops working when its time to recognize the threat and shoot at it. Running the debug the bot just scans repeatedly, it does register the player, it just doesnt perceive it to be an enemy.
Thanks for the pointer to Torsion, I didnt even know something like that existed. Ive been using UltraEdit, which is useless for debugging in itself. I will hafta get back to this another time though :(, the assignment is already passed due and I cant waste more time nitpicking over this class. I think im just going to need to write some simpler behaviors for my AI from scratch.
#5
Don't know if its any help.
04/07/2009 (3:04 pm)
Sorry, what I was thinking of is player.cs has a boolean aiAvoidThis = true;Don't know if its any help.
Torque Owner Infinitum3D
Maybe in player.cs or aiplayer.cs?
Sorry, I'll try find more tomorrow.
Tony