My bot has no bullets
by Princeabendego McDonald · in Torque 3D Professional · 09/11/2011 (8:51 pm) · 2 replies
so i was following the simple fps tutorial and i fixed a few other things that didn't work. I got to the part where the Bots are supposed to shoot and while it says attacking nothing seems to happen.
This is the code i have for it:
From scripts/server:
=====================================================================================
/*----------
--Logic--
----------*/
function AIPlayer::targetPlayer(%this)
{
%count = ClientGroup.getCount();
for (%i = 0; %i < %count; %i++)
{
%client = ClientGroup.getObject(%i);
if (%client.player $= "" || %client.player == 0)
return -1;
%target = %client.player;
echo(%this.getname() @ " Target Aquired" @ %target);
return %target;
}
}
function AIPlayer::checkRange(%this, %target)
{
%rangeFrom = %this.range;
%fromMe = %this.getPosition();
%toYou = %target.getPosition();
%rangeTo = VectorDist(%fromMe, %toYou);
if(%rangeTo > %rangeFrom)
{
echo(%this.getname() @ " target too far");
return false;
}
else
{
echo(%this.getname() @ " target in range");
return true;
}
}
function AIPlayer::clearShot(%this, %target)
{
%ISpy = %this.getEyePoint();
%YouSpy = %target.getEyePoint();
%search = containerRayCast(%ISpy, %YouSpy, $TutorialBot::Obstacles, %this.getID());
%impactID = firstWord(%search);
echo("impactID of targeted object = " @ %impactID);
if(%impactID == %target || %impactID == 0) //NOTE: see below
{
echo(%this.getname() @ " has a clearShot");
return true;
}
else
{
echo(%this.getname() @ " has no shot");
return false;
}
}
function AIPlayer::botThink (%this)
{
if (%this.getState() $= "dead")
return;
%this.combatmode();
echo(%this.getname() @ " thinking");
%this.schedule(500, botThink);
}
function AIPlayer::combatMode(%this)
{
%targetID = %this.targetPlayer(%target);
if(%targetID != -1)
if(%this.checkRange(%targetID) == true)
if(%this.clearShot(%targetID) == true)
%this.attackTarget(%targetID);
}
function AIPlayer::attackTarget(%this, %target)
{
echo(%this.getname() @ " attacking!");
%this.setAimObject(%target, "0 0 1.5");
%this.schedule(150, "shoot");
// a slight delay to make sure we're aiming at the target
//you could always use a raycast to see if the target is in the bot's sights instead
}
function AIPlayer::shoot(%this)
{
%this.setImageTrigger(0, true);
%this.schedule(%this.shootingDelay, "ceasefire");
}
function AIPlayer::ceasefire(%this)
{
%this.setImageTrigger(0, false);
}
/*----------
-Spawning-
----------*/
function AIPlayer::spawnBot(%name, %spawnPoint, %weapon)
{
%player = new AiPlayer(%name)
{
dataBlock = TutorialBot;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint.getTransform());
echo(%name @ " has spawned at " @ %spawnpoint);
%player.equipBot(%weapon);
%player.schedule (200, botThink); // start thinking process
return %player;
}
function AIPlayer :: equipBot (%this, %weapon)
{
if (%weapon $= "semiauto")
{
%weaponAmmo = "semiautoAmmo";
%weaponImage = "semiautoImage";
%this.range = 200;
}
if (%weapon $= "fullauto")
{
%weaponAmmo = "fullautoAmmo";
%weaponImage = "fullautoImage";
%this.range = 100;
}
%this.setInventory (%weapon, 1);
%this.setInventory (%weapon, %this.maxInventory(%weaponAmmo));
%this.mountImage (%weaponImage, 0);
}
This is the code i have for it:
From scripts/server:
=====================================================================================
/*----------
--Logic--
----------*/
function AIPlayer::targetPlayer(%this)
{
%count = ClientGroup.getCount();
for (%i = 0; %i < %count; %i++)
{
%client = ClientGroup.getObject(%i);
if (%client.player $= "" || %client.player == 0)
return -1;
%target = %client.player;
echo(%this.getname() @ " Target Aquired" @ %target);
return %target;
}
}
function AIPlayer::checkRange(%this, %target)
{
%rangeFrom = %this.range;
%fromMe = %this.getPosition();
%toYou = %target.getPosition();
%rangeTo = VectorDist(%fromMe, %toYou);
if(%rangeTo > %rangeFrom)
{
echo(%this.getname() @ " target too far");
return false;
}
else
{
echo(%this.getname() @ " target in range");
return true;
}
}
function AIPlayer::clearShot(%this, %target)
{
%ISpy = %this.getEyePoint();
%YouSpy = %target.getEyePoint();
%search = containerRayCast(%ISpy, %YouSpy, $TutorialBot::Obstacles, %this.getID());
%impactID = firstWord(%search);
echo("impactID of targeted object = " @ %impactID);
if(%impactID == %target || %impactID == 0) //NOTE: see below
{
echo(%this.getname() @ " has a clearShot");
return true;
}
else
{
echo(%this.getname() @ " has no shot");
return false;
}
}
function AIPlayer::botThink (%this)
{
if (%this.getState() $= "dead")
return;
%this.combatmode();
echo(%this.getname() @ " thinking");
%this.schedule(500, botThink);
}
function AIPlayer::combatMode(%this)
{
%targetID = %this.targetPlayer(%target);
if(%targetID != -1)
if(%this.checkRange(%targetID) == true)
if(%this.clearShot(%targetID) == true)
%this.attackTarget(%targetID);
}
function AIPlayer::attackTarget(%this, %target)
{
echo(%this.getname() @ " attacking!");
%this.setAimObject(%target, "0 0 1.5");
%this.schedule(150, "shoot");
// a slight delay to make sure we're aiming at the target
//you could always use a raycast to see if the target is in the bot's sights instead
}
function AIPlayer::shoot(%this)
{
%this.setImageTrigger(0, true);
%this.schedule(%this.shootingDelay, "ceasefire");
}
function AIPlayer::ceasefire(%this)
{
%this.setImageTrigger(0, false);
}
/*----------
-Spawning-
----------*/
function AIPlayer::spawnBot(%name, %spawnPoint, %weapon)
{
%player = new AiPlayer(%name)
{
dataBlock = TutorialBot;
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint.getTransform());
echo(%name @ " has spawned at " @ %spawnpoint);
%player.equipBot(%weapon);
%player.schedule (200, botThink); // start thinking process
return %player;
}
function AIPlayer :: equipBot (%this, %weapon)
{
if (%weapon $= "semiauto")
{
%weaponAmmo = "semiautoAmmo";
%weaponImage = "semiautoImage";
%this.range = 200;
}
if (%weapon $= "fullauto")
{
%weaponAmmo = "fullautoAmmo";
%weaponImage = "fullautoImage";
%this.range = 100;
}
%this.setInventory (%weapon, 1);
%this.setInventory (%weapon, %this.maxInventory(%weaponAmmo));
%this.mountImage (%weaponImage, 0);
}
About the author
#2
try adding
after
09/12/2011 (12:24 am)
This is the line that is actually trying to shoot:%this.setImageTrigger(0, true);
try adding
%this.setImageAmmo(0, true);
after
%this.mountImage (%weaponImage, 0);
Torque 3D Owner Christopher Tauscher
Default Studio