Game Development Community

AI targeting

by Christian · in Technical Issues · 01/24/2007 (10:29 pm) · 2 replies

This is some code I wrote up for an AIplayer. It works fine except it doesn't return the player it's targeting. Down in the code it's suppose to attack the closest player with '.applyDamage', but i'm not sure how to get the player that he's targeting into a variable to be .damaged'd. What do I need to do to fix it?

function aiCrunchey::onImpact(%this, %spell, %caster, %impObj, %impPos, %impNorm)
{
if (!isObject($crunchey))
{
$crunchey = new AIPlayer(Crunchey) {
datablock = "OrcWarriorAvatar"; 
position = %caster.getPosition(); };
}
%nextClient = ClientGroup.getObject(getclosesthuman());
schedule(100,0,botmelee);
}

function getclosesthuman()
{
%botPos = $crunchey.getposition();
%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;
$closetoast = %playPos;
$wherecrunchey = $crunchey.getPosition();
$apartness = %dist;
$whoiscruncheytargeting = %client;
schedule(100,0,getclosesthuman);
}
else {
if(%dist > %tempDist) {
%dist = %tempDist;
%index = %i;
}
}
}
return %index;
}

function botmelee() 
{
//%tempDist1 = VectorDist($wherecrunchey, $closetoast());
if ($apartness > 10) 
{
echo("apartness > 10");
$crunchey.setmovedestination(VectorAdd($closetoast, "10 0 0"));
schedule(100,0,botmelee);
}
else {
echo("here comes health check");
schedule(2000,0, healthcheck);
}
}

function healthcheck()
{
if ($crunchey.getdamagelevel() > 60)
{
echo("retreat is coming");
schedule(100,0,retreat);
}
else
{
schedule(100,0,attack);
echo("attack is coming");
}
}

function attack()
{
$crunchey.setmovedestination($closetoast);
echo("were closing into attack position");
//%tempDist2 = VectorDist(%cruncheycurrentpos, $closetoast());
if ($apartness < 5)
{
echo("wam");
$whoiscruncheytargeting.applyDamage(10);

%healing_amt = -10;
%damage_level = $whoiscruncheytargeting.getDamageLevel() - %healing_amt;
if (%damage_level < 0)
%damage_level = 0;



schedule(100,0,botmelee);
}
else
{
schedule(100,0,attack);
echo("we'll try to attack again");
}
}

function retreat()
{
$crunchey.setmovedestination(VectorAdd($crunchey.getposition(), "-30 0 0"));
schedule(3000,0,botmelee);
echo("runaway");
}

#1
01/24/2007 (11:16 pm)
%client.player

Aun.
#2
01/25/2007 (1:11 am)
Thanks. Works.