Game Development Community

AI that do what you do! - script

by Adam \\\"Adam487\\\" Oachs · in Technical Issues · 04/28/2007 (10:28 am) · 6 replies

Hi. I have recently made AI that do what you do. They move the same direction, hold the same weapon, shoot when you do, and even look in the same direction as you. This requires no engine changes!




function serverCmdBotMimic(%client,%bot)
{
startMimic(%client,%client.bot[%bot]);
}

function serverCmdBotStopMimic(%client,%bot)
{
cancel(%client.bot[%bot].mimicSchedule);
}

function servercmdcreatebot(%client)
{
if(%client.botcount >= 5)
return;
%player = AIPlayer::spawn(%client);
%player.followPlayer(%client);
%client.botcount++;
%client.bot[%client.botcount] = %player;
}

function servercmddeletebot(%client)
{
if(%client.botcount <= 0)
return;
for(%i = 5; %i >= -1; %i--)
{
if(isObject(%client.bot[%i]))
{
%client.bot[%i].delete();
%client.botcount--;
return;
}
}
}

function BotPlayer::onReachDestination(%this,%obj)
{
// Moves to the next node on the path.
// Override for all player. Normally we'd override this for only
// a specific player datablock or class of players.
return;
}

function AIPlayer::followPlayer(%this,%client)
{
%this.stopThread(0);
if (!isObject(%client) || !isobject(%this) || %this.getState() $= "Dead")
{
%this.following = "";
return;
}
%this.following = %client;
%this.setAimObject(%client.player);
%this.think();
}

function AIPlayer::think(%this)
{
%client = %this.following;
if (!isObject(%client) || !isobject(%this) || %this.getState() $= "Dead")
return;
if(!isobject(%client.player))
{
%this.schedule(30,think);
return;
}
%y = getWord(%client.player.getDataBlock().boundingbox, 1);
%pos = getWords(%client.player.getTransform(), 0, 2);
%oldPos = %pos;
%vec = " 0 -1 0";
%vec = MatrixMulVector(%client.player.getTransform(), %vec);
%pos = "0 0 0";
%pos = VectorAdd(%oldPos, VectorScale(%vec, %y));

%followPos = %pos;
%pos = getWords(%player.position, 0, 2);
%vec = VectorSub(%followPos, %pos);
%len = VectorLen(%vec);

%this.setAimObject(%client.player);
if (%len > getWord(%this.getDataBlock().boundingbox, 1))
%this.setMoveDestination(%followPos, false);
%this.moveSchedule = %this.schedule(30,think);
}

function serverCmdStopFollow(%client,%bot)
{
cancel(%client.bot[%bot].moveSchedule);
}

function serverCmdStartFollow(%client,%bot)
{
%client.bot[%bot].think();
}


function AIPlayer::create(%this)
{
%obj = new AIPlayer()
{
dataBlock = BotPlayer;
};
return %obj;
}

function AIPlayer::spawn(%client)
{
// Create the demo player object
%player = AIPlayer::create();
//MissionCleanup.add(%player);
if(isObject(%client))
{
%player.setTransform(%client.player.gettransform());
%player.setShapeName(%client.name);
%player.client.name = %client.name;
%player.name = %client.name;
}
else
{
%player.settransform(pickSpawnPoint());
%player.setShapeName("Jumble Bot");
%player.client.name = "Jumble Bot";
%player.name = "Jumble Bot";
}
%player.setMoveSpeed(1);
%player.setEnergyLevel(60);
return %player;
}

SEE NEXT POST

#1
04/28/2007 (10:28 am)
Function startMimic(%client,%bot)
{
if(!isObject(%client.player) || !isObject(%bot))
{
echo("One of those objects don't exist!");
return;
}
%bot.clearAim();
cancel(%bot.mimicSchedule);
%bot.posDiff = VectorSub(%client.player.getPosition(),%bot.getPosition());
%client.mimicBot[%client.mimicCount++] = %bot;
doMimic(%client,%bot);
}

function doMimic(%client,%bot)
{
if(isObject(%bot) && isObject(%client.player))
{
%bot.setMoveDestination(VectorAdd(%bot.posDiff,%client.player.getPosition()));
%aimPos = VectorAdd(%client.player.getPosition(),VectorScale(%client.player.getEyeVector(),1000000));
%bot.setAimLocation(%aimPos);
if(%client.player.getMountedImage(0))
{
%bot.mountImage(%client.player.getMountedImage(0),0);
if(!%bot.isArmReady)
{
%bot.playThread(0,armReadyRight);
%bot.isArmReady = 1;
}
}
else
{
%bot.unMountImage(0);
%bot.playThread(0,root);
%bot.isArmReady = 0;
}
%bot.mimicSchedule = schedule(100,0,doMimic,%client,%bot);
}
}

function PlayerStandardArmor::onTrigger(%this, %obj, %triggerNum, %val)
{
if(%triggerNum == 0)
{
for(%mimic=0;%mimic<%obj.client.mimicCount;%mimic++)
{
if(%val)
{
%obj.client.mimicBot[%mimic].setImageTrigger(0,true);
}
if(!%val)
{
%obj.client.mimicBot[%mimic].setImageTrigger(0,false);
}
}
}
}

datablock PlayerData(BotPlayer : PlayerStandardArmor)
{
cmdCategory = "AIPlayer";
groundImpactMinSpeed = "3";
horizMaxSpeed = "120";
horizResistSpeed = "15";
maxBackwardCrouchSpeed = "15";
maxBackwardSpeed = "20";
maxForwardCrouchSpeed = "15";
maxForwardSpeed = "20";
maxSideCrouchSpeed = "15";
maxSideSpeed = "20";
maxUnderwaterBackwardSpeed = "15";
maxUnderwaterForwardSpeed = "20";
maxUnderwaterSideSpeed = "15";
minImpactSpeed = "5";
upMaxSpeed = "125";
upResistSpeed = "15";
maxSideProneSpeed = "15";
maxSideWalkSpeed = "20";
maxForwardProneSpeed = "15";
maxForwardWalkSpeed = "20";
maxBackwardProneSpeed = "15";
maxBackwardWalkSpeed = "20";
};




You'll need to change PlayerStandardArmor to whatever your player datablock is.
#2
04/28/2007 (4:13 pm)
COOL! I haven't tried it, but it looks great!
#3
05/03/2007 (9:08 pm)
Great for a clone army. Great job!
#4
05/03/2007 (9:35 pm)
Cool nice work. Ive been looking for an ai teammate implementation, this just might be good enough :)