Game Development Community

Ai's that aim up

by fire maker · in Torque Game Engine · 06/23/2007 (8:14 am) · 8 replies

How do i make ai's that aim at me when i am on top of a building?

#1
06/23/2007 (9:21 am)
%aiguy.setAimObject(%playerGuy);
#2
06/23/2007 (11:01 am)
I know that, but the ai's always shoot way to low when i am up so high but they hit me in the face when i am level with them do u hav any sugestions?
#3
06/23/2007 (11:28 am)
Use vectors with setAimObject(). For example, setAimObject(%obj, "x y z")
#4
06/23/2007 (1:16 pm)
I see were you are coming from but would you help me tie it into my code

$MINIMUM_SCAN_INTERVAL = 200;
$MAXIMUM_SCAN_INTERVAL = 1000;

$MIN_SCAN_GAP = 1000;
$MAX_SCAN_GAP = 20000;
$MIN_TRIGGER_HOLD = 9;
$MAX_TRIGGER_HOLD = 100;
$MIN_ITCHY_FINGER = 9;
$MAX_ITCHY_FINGER = 100;
$MAX_THREAT_ENGAGE_RANGE = 1800000;
$MAX_AGGRESSIVENESS = 1800000;
$MAX_ATTENTION_LEVEL = 1800000;
$MAX_ALERTNESS = 1800000;
$MAX_ROVER_SPEED = 0.5;
$MIN_ROVER_SPEED = 0.4;
$MAX_PATROL_SPEED = 0.5;
$MIN_PATROL_SPEED = 0.4;
$STATIONARY = 1.0;

$cardinalDirection[0] = ClientGroup.getObject( %idx );

function AIBeast::checkForThreat(%this,%obj)
{
echo( "checkForThreat...");
if(!isObject(%obj))
return;

if (isObject( %obj) )
{
%idx = %obj.getClosestHuman();
if (%obj.attentionLevel>0)
{
%testRange = %obj.range * (%obj.aggression*2);
}
else
{
%testRange = %obj.range;
}
}
else
{
return 0;
}
if (%idx < 0)
return 0;
%target = ClientGroup.getObject( %idx );

if (%target.player == %obj.currentTarget)
{
if (isObject( %obj) )
{
if ( %obj.GetTargetRange(%target.player) < %testRange)
{
return %target.player;
}
}
else
return 0;
}
else
{
if (isObject( %obj) )
{
if ( %obj.GetTargetRange(%target.player) < %testRange)
{
return %target.player;
}
}
else
return 0;
}
echo( "...no threat");
return 0;
}

function AIBeast::DoScan(%this,%obj)
{
echo( "doScan... (from:"@%obj@")");
if(!isObject(%obj))
return;
cancel(%this.scheduledCheck);

%theRole = %obj.role;

if (%obj.attentionLevel<=0)
%obj.attentionLevel=0;
else
%obj.attentionLevel--;

if (%obj.attertyntionLevel==0)
{
%look = getRandom(3);
if (%this.look != %look)
%this.look = %look;
else
{
%this.look = %look + 1;
if (%this.look < 3)
%this.look++;
else
%this.look = 0;
}
}

%t = %client.player.getPosition();

%obj.setAimLocation( %t);

if ( (%tgtPlayer = %this.checkForThreat(%obj)) != 0)
{
if (%obj.currentTarget)
{
if (%obj.currentTarget==%tgtPlayer)
{
echo( "STILL A THREAT (from:"@%obj@")");
%obj.setAimObject( %tgtPlayer );
%obj.attentionLevel = %obj.attention;
if (%theRole !$= "Guard")
{
%obj.setMoveSpeed($MAX_ROVER_SPEED);
%obj.setMoveDestination(%tgtPlayer.getPosition()@"+10");
%obj.nextBlockCheck = %this.schedule($MAX_SCAN_GAP*2, "unBlock", %obj);
}
}
else
{
echo( "CHANGED THREAT (from:"@%obj@")");
%obj.currentTarget = %tgtPlayer;
%obj.setAimObject( %obj.currentTarget );
if (%theRole !$= "Guard")
{
%obj.setMoveSpeed($MAX_ROVER_SPEED);
%obj.setMoveDestination(%tgtPlayer.getPosition());
%obj.nextBlockCheck = %this.schedule($MAX_SCAN_GAP*2, "unBlock", %obj);
}
}
}
else
{
echo( "NEW THREAT!! (from:"@%obj@")");
%obj.setAimObject( %tgtPlayer );
%obj.currentTarget = %tgtPlayer;
%obj.attentionLevel = %obj.attention;
if (%theRole !$= "Guard")
{
%obj.setMoveSpeed($MAX_ROVER_SPEED);
%obj.setMoveDestination(%tgtPlayer.getPosition());
%obj.nextBlockCheck = %this.schedule($MAX_SCAN_GAP*2, "unBlock", %obj);
}
}
}
else
{
if (%obj.getAimObject)
{
%obj.clearAim();
echo( "doScan >>> %obj.clearAim (from:"@%obj@")");
%obj.currentTarget = 0;
}
%this.nextScan = %this.schedule($MIN_SCAN_GAP+getRandom($MAX_SCAN_GAP/%this.alertness), "doScan", %obj);
}
}

function AIBeast::openFire(%this,%obj)
{
if(!isObject(%obj))
return;
%obj.setImageTrigger(0,false);
%this.schedule($MIN_TRIGGER_HOLD+getRandom($MAX_TRIGGER_HOLD), "ceaseFire", %obj);
}

function AIBeast::ceaseFire(%this,%obj)
{
echo( "--------ceaseFire... (from:"@%obj@")");
if(!isObject(%obj))
return;
%obj.setImageTrigger(0,false);
%obj.clearAim();
%obj.currentTarget = 0;
}

function AIBeast::onTargetEnterLOS(%this,%obj)
{
echo( "LOS TARGET ! (from:"@%obj@")");
if(!isObject(%obj))
return;
%theRole = %obj.role;

%obj.attentionLevel = %this.attention;
%this.schedule($MIN_ITCHY_FINGER+getRandom($MAX_ITCHY_FINGER), "openFire", %obj);
%this.schedule($MIN_SCAN_GAP+getRandom($MAX_SCAN_GAP/%this.alertness), "doScan", %obj);
%obj.setImageTrigger(0,true);
}

function AIBeast::onTargetExitLOS(%this,%obj)
{
echo( ".......Fuhgetaboutit (from:"@%obj@")");
if(!isObject(%obj))
return;
%obj.setImageTrigger(0,false);
%obj.clearAim();
echo( "onTargetExitLOS >>> %obj.clearAim (from:"@%obj@")");
%obj.currentTarget = 0;
%this.schedule($MIN_SCAN_GAP, "doScan", %obj);
}

function AIPlayer::spawnBot(%index,%location,%rol)
{
%me = new AIPlayer() {
dataBlock = BeastAvatar;
aiPlayer = true;
};
MissionCleanup.add(%me);
AIGroup.add(%me);
$mebot = %me;
%me.look = 100;

%me.range = 100;
%spawn=%location;

%me.alertness = 175;
%me.aggression = 175;
%me.attention = 175;
%me.range = 150;
%me.index = %index;
%me.setTransform(%spawn);
%me.setEnergyLevel(60);
%me.role = %rol;
%me.setShapeName("Beast");
%me.attentionLevel = 0;
%me.nextBlockCheck = 0;
%n=getRandom(1);
if (%n==1)
{
%weapon = new Item() {
dataBlock = tommygun;
};
%ammo = new Item() {
dataBlock = tommygunAmmo;
};
MissionCleanup.add(%weapon);
MissionCleanup.add(%ammo);
%me.pickup(%weapon, 1);
%me.pickup(%ammo, 100);
}

if (%n==0)
{
%weapon = new Item() {
dataBlock = crossbow;
};
%ammo = new Item() {
dataBlock = crossbowAmmo;
};
MissionCleanup.add(%weapon);
MissionCleanup.add(%ammo);
%me.pickup(%weapon, 1);
%me.pickup(%ammo, 100);
}
if (%rol=="Guard")
{
%weapon = new Item() {
dataBlock = ommygun;
};
%ammo = new Item() {
dataBlock = ommygunAmmo;
};
MissionCleanup.add(%weapon);
MissionCleanup.add(%ammo);
%me.pickup(%weapon, 1);
%me.pickup(%ammo, 100);
}
%me.look = 1;
%me.setMoveSpeed(1);

echo("Added Beast [" SPC %me SPC "] :" SPC %me.task SPC ":" SPC %me.role SPC "#" SPC %me.index );

%me.setAimLocation( $cardinalDirection[%me.look]);
%me.getDataBlock().schedule(5000, "DoScan", %me);

return %me;
}

function AIPlayer::getClosestHuman(%this) {
echo( "getClosestHuman...");

%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) {
%dist = %tempDist;
%index = %i;
}
else {
if(%dist > %tempDist) {
%dist = %tempDist;
%index = %i;
}
}
}
return %index;
}
#5
06/23/2007 (2:02 pm)
Right, from what I gathered from the code, you could probably just change two things in the AIBeast::DoScan function.

echo( "STILL A THREAT (from:"@%obj@")");
%obj.setAimObject( %tgtPlayer );
%obj.attentionLevel = %obj.attention;

Should be changed to:

echo( "STILL A THREAT (from:"@%obj@")");
%obj.setAimObject( %tgtPlayer, "0 0 1");
%obj.attentionLevel = %obj.attention;

Also in the DoScan function:
echo( "CHANGED THREAT (from:"@%obj@")");
%obj.currentTarget = %tgtPlayer;
%obj.setAimObject( %obj.currentTarget );

Should be changed to:

echo( "CHANGED THREAT (from:"@%obj@")");
%obj.currentTarget = %tgtPlayer;
%obj.setAimObject( %obj.currentTarget, "0 0 1" );

Those changes to the aim object are basically a torso hit with the default orc. You may need to adjust them to suit your needs.
#6
06/23/2007 (3:02 pm)
Thank you it works.
it also fixed the problem of them acting like dick cheney aka "accidently shooting each other.
#7
06/23/2007 (3:18 pm)
Haha. Well, that's good news. Without using vectors I'm pretty sure they just target the center of the bounding box, that being the target's feet.
#8
06/23/2007 (4:22 pm)
Matt is correct in stating they they aim at the target's feet. Simply make sure you add ~1 to the Z component of the aim location and you should be shooting at the middle of the target (if its another player of ~2 units high). Also, remember that ballistic projectiles will lose height with distance quite dramatically in certain circumstances.