Game Development Community

How to get silly bots into a level

by Stefan Beffy Moises · 02/16/2002 (7:53 pm) · 39 comments

This short and basic tutorial discusses how to get bots into your game via the console and the rudimentary "aiPlayer" class provided with the latest engine release. It also lets your bots run around in your level. Not much, though, but enough to get you started and to save you from your "in-game-solitude" ;-)
It's basically a summary of different forum threads I found here, and I thought it would be helpful to concentrate it into a little tutorial for Torque newbies - just like me... :-) Here we go ...

First of all, import the two files
\engine\game\aiPlayer.h
\engine\game\aiPlayer.cc

into the MSVC project, if they aren't already in there! Compile the example project.
Then, load any example level. Open the console by pressing ^ and type in the following:

$myBot01 = aiAddPlayer("Bill Gates");


You should get something like this as result:
CADD: 1426 ai:local
I've been added

The bot should spawn at any of the spawn spheres in the level, maybe you have to walk around to find him... ;-)


After that, you can access your bot through the global variable $myBot01. Go ahead and try it with:

echo($myBot01.getClassName());

which should give you the "answer"
AIPlayer


By the way, if you'd like to see all fields and methods of your (aiPlayer) classes, just type

$myBot01.dump();


You can make your bot move like this:
$myBot01.setMoveDestination("-224.153 -166.305 214.662");
$myBot01.move();

which lets your bot run to the specified coordinates.
Of course, you can also pass the values of other bots/objects:

$myBot02 = aiAddPlayer("Psycho");
$myBot02.setMoveDestination($myBot01.getMoveDestination());
$myBot02.move();

which lets the new Psycho-Bot run at the location of your first bot...

To have your bots do more advanced stuff like fighting or drinking beer ;-), you'd have to implement your own routines (e.g. pathfinding) for now, because the aiPlayer class is work in progress right now ... If I got time to work on that and finish it before somebody else does, that's definitely something for a second part of this tutorial... :-)

You should probably set up all this in a script and play around with it for a while... Maybe you could write some functions to make some bots "escort" your player by updating their positions with your players' moves, etc. ... just go for it! And then, of course, have them attack each other, mount vehicles, hunt your player, funny stuff like that... ;-)

If you want some more, make sure you read the 2nd part of this tutorial!
Page «Previous 1 2
#1
02/17/2002 (12:38 am)
Great I was looking how to do this.. Thanks and keep up the good work.
#2
02/17/2002 (4:50 am)
Looks like a great tutorial! I'm still using v1.1.0 at the moment because of the D3D rendering problems with the latest release, so I can't try it out. I'm looking forward to the day when this problem is fixed so I can use the AI code.
Well done. :)
#3
02/17/2002 (6:40 am)
Oh man, wheeled vehicle code, New bot tutorial, I havnt enought free time to work on all this! where was this when i was sitting on my hands (so to speak) in december :) he j/k... looks great stefan, cant wait to have a stalker later tonite...
ryan
#4
02/17/2002 (2:51 pm)
thats all great and dandy and all, but how about a lil more details,(since i am having a real hard time understanding the script stuff) like how would you:

bind it to akey to spawn it
or have spawn auto maticly when entering the game

and it dont die when you shoot it

im all for learning this stuff but i need somthing to go by

thanks
#5
02/17/2002 (3:55 pm)
Great info! I combined this with David Myers' "Object selection in Torque" resource to allow me to select a bot and have it automatically run over to me:

%bot = %client.getSelectedObj().client;
%bot.setMoveDestination( %client.player.getPosition() );
%bot.move();

Look out! He's coming right for you!

Thanks,
Fram
#6
02/18/2002 (12:07 am)
Gary, here you go:
I think the best place to put the Bot generation code is in a server script, I've put the following in "fps/server/scripts/commands.cs":

//------------------------------------------------------------------------------
// adding Bots
//------------------------------------------------------------------------------
$botCounter = 0;
function serverCmdAddBot(%client) {
$bots[$botCounter] = aiAddPlayer("Bot" @ $botCounter);
$botCounter++;
}
function serverCmdMoveBotsToPlayer(%client)
{
if (isObject(%client.player))
{
for(%i = 0; %i < $botCounter; %i++)
{
$bots[%i].setMoveDestination( %client.player.getPosition() );
$bots[%i].move();
}
}
}

Then I added the commands on the client side, in "fps/client/scripts/default.bind.cs":

function addBot(%val) {
if(%val)
{
commandToServer('AddBot');
}
}
function moveBotsToPlayer(%val)
{
if(%val)
{
commandToServer('MoveBotsToPlayer');
}
}
moveMap.bind(keyboard, "alt b", addBot);
moveMap.bind(keyboard, "strg b", moveBotsToPlayer);

and I also added these mappings to
"fps/client/config.cs" like this:

moveMap.bind(keyboard, "alt b", addBot);
moveMap.bind(keyboard, "ctrl b", moveBotsToPlayer);

Now you are able to add both functions to the "Options/Controls" gui dialog (if you want) by including the following in the file "fps/client/scripts/optionsDlg.cs":

$RemapName[$RemapCount] = "Add Bot";
$RemapCmd[$RemapCount] = "addBot";
$RemapCount++;
$RemapName[$RemapCount] = "Move bots to player";
$RemapCmd[$RemapCount] = "moveBotsToPlayer";
$RemapCount++;


And my bots DO die if I shoot at them a couple of times... don't know what may be the problem with that...?

Happy "botting" now! I hope you're not afraid of bots running after you...? ;-) Oh, and you should hit "ALT B" more than once, it's funny to see the bots standing on each others shoulders...:-)

By the way, does anybody know how to get the player's (client) position on the console??

echo(%client.player.getPosition());

doesn't work, the %client variable doesn't seem to be defined there and I get a "unable to find object" message ...
#7
02/18/2002 (2:36 am)
whoa , now thats what im talking about:)hehe 31 bots really drag this p4 down ;)....but now i have something to work with, i dont know why they dont die (still)but will work on it, and im going to add a find radious to the code so they will detect you when you are in range

btw i tried fro hours to get it just to bind to a key be for posting for some help (but like i said ihave trouble understanding script)

thanks a bunch stefan thats a great peice of work
#8
02/18/2002 (4:20 am)
Err, sorry, some small updates:
First of all, I added the following in server/scripts/commands.cs:

if(isObject($bots[%i])){
$bots[%i].setMoveDestination( ... );
...
}

because otherwise the game would crash if you killed a bot and then call this function...

Furthermore I added the bots for MissionCleanup like this (in the serverCmdAddBot function):

MissionCleanup.add($bots[$botCounter]);

And, just to count the bots from 0 each time a mission starts, I set $botCounter to 0 in
server/scripts/game.cs -> startGame():

$botCounter = 0;

So now the two bot functions look like this:

//------------------------------------------------------------------------------
// adding Bots
//------------------------------------------------------------------------------
$botCounter = 0;
function serverCmdAddBot(%client) {
$bots[$botCounter] = aiAddPlayer("Bot" @ $botCounter);
MissionCleanup.add($bots[$botCounter]);
$botCounter++;
}
function serverCmdMoveBotsToPlayer(%client)
{
if (isObject(%client.player))
{
for(%i = 0; %i < $botCounter; %i++)
{
if(isObject($bots[%i])){
$bots[%i].setMoveDestination( %client.player.getPosition() );
$bots[%i].move();
}
}
}
}

Sorry for any inconveniences... ;-)
#9
02/18/2002 (6:49 am)
ok, this is pretty sweet. My question is, how can i have a bot respawn if i kill it?
ryan
#10
02/18/2002 (7:07 pm)
Thanks a lot. This is very helpful. I was wondering how you could get the bots to be there when you load the mission. Place aiAddPlayer() somewhere in the mission script?
#11
02/18/2002 (9:05 pm)
Ah, splendid, I've been waiting for this.
#12
02/18/2002 (10:39 pm)
Jared, that shouldn't be a problem. You could simply write a script "placeBots.cs" or something and add it in "fps/server/scripts/game.cs":

...
exec("./placeBots.cs");
...

In this script you could just call the bot-generating functions a couple of times ...
maybe you should move each bot randomly a couple of inches on creation to prevent them from stepping on each other ;-)

Ryan, why would you respawn a bot, you could just create a new bot... but I don't know how to respawn them? Anybody else?
#13
02/19/2002 (1:39 am)
Respawning is a problem (I looked at the code briefly trying to figure out a way around it)

The problem stems from the fact that mouse1 must be pressed to trigger it. I didn't look into it much, but in that time I Couldn't figure out a way to do a time-based respawn combined with an optional mouse-click trigger which would allieviate the need for the endless creation of bots (which really bogs things down)

If someone can figure out how to respawn things without needing to press on the mouse button, we'll have automatically respawning bots :)
#14
02/19/2002 (5:44 am)
stefan, if you create a new one, it takes up another place in your player list. i am looking for something along the lines of quake3/unreal tournament types of bots. I want them to be like players in every sense. I have no idea on how to respawn them, but im going to look into it
ryan
#15
02/19/2002 (7:31 pm)
How again would you do it so the player and the bots don't spawn right on top of each other? Thanks.
#16
02/19/2002 (11:14 pm)
heh, looks like this thread may be geting into some of the sdk disscussion, so i have created a torque ai forum that cant be seen un less you have moderator status ,which i would grant for interested ppl (i guess if you have a "plan" it means you paid for torque,,,they ar nice forums at
http://dynamic4.gamespy.com/~noescape/cgi/forum.cgi

what cha think?
#17
02/19/2002 (11:49 pm)
There is a (more theoretical at the moment) Torque AI discussing going on in the Core Forum on this site: www.garagegames.com/index.php?sec=mg&mod=forums&page=result.thread&qt=3588 - maybe you'd like to join there?
#18
02/20/2002 (12:10 am)
yeah, i been watchin that too, but theorys and wish lists dont mean any thing to me , i am dedicating all of my time strictly to this ai stuff till i understand it completely (that means a lot of trial and error for me) but with out the monster ai a game aint squat imo... and they seem to be consentrating on bots (i could really care less about them)

im looking at the q2 code as a reference and the scripting is a good place to get familar with torque ,but im realy going for ids q2 monster ai style and hard code , and of course that style in torque is gonna rock :) the scripting stuff is for mod makers (no offence ment to any one but that just what i think of this scripting stuff)

in fact when they do come up with a cool bot code i will mor then likely just make it a mod for practiceing,.
#19
02/20/2002 (12:22 am)
Gary, you're right ... I'd rather dive into the code, too! But it will be a very challenging task, so some theory should be discussed ahead... what I need is a sophisticated AI for a single player game with NPCs behaving like "real" players - and my first aim is to work out a really good pathfinding algorithm to be able to let them wander through my levels without looking like complete jerks and running into every obstacle, taking bridges rather than jumping into the water like lemmings, stuff like that...
And I'm really curious what Mychal and his team achieved with these issues...
But anyhow, why don't you make up a new thread here at GG dealing with "practical AI"? It will be hard to follow another forum - it's hard to keep up to date with all the information here at GG, though... but if you really want, I'm with you at gamespy ;-)
#20
02/20/2002 (12:25 pm)
well , the thing is if we start a thread here and call it monster ai im a fraid its going to be come just like tcp ai thread and will be a huge wish list type thing, we could try it ,and specifi exactly what we are doing ,and if it gets to be a jumble mess we can move the ifo we want to a fourm under our control (where we can delete the useless stuff)


new monsterai thread started here
Page «Previous 1 2