| Author: beffy (c++programmer) |
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" ;-)
First of all, import the two files\engine\game\aiPlayer.h \engine\game\aiPlayer.ccinto the MSVC project, if they aren't already in there! Compile the example project.
UPDATE: (09. Oct. 2002)
With the current HEAD release, check if you got the following in player.cc to get the bots to move:
void Player::processTick(const Move* move)
{
PROFILE_START(Player_ProcessTick);
// If we're not being controlled by a client, let the
// AI sub-module get a chance at producing a move.
Move aiMove;
if (!move && getAIMove(&aiMove))
move = &aiMove;
...
}
and
bool Player::getAIMove(Move* move)
{
return false;
}
also, in player.h
virtual bool getAIMove(Move*);
For details, check out this thread.
Then, load any example level. Open the console by pressing ^ and type in the following:
$myBot01 = aiAddPlayer("Bill Gates");
UPDATE: (09. Oct. 2002)
Due to recent changes in the HEAD version of the engine and a complete new AIPlayer class, you have to add bots like this now:
$myBot01 = AIPlayer::spawnPlayer();
CADD: 1432 ai:local I've been addedThe 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();
UPDATE: (09. Oct. 2002)
With the new bot code, you don't need
$myBot01.move();anymore.
Of course, you can also pass the values of other bots or 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... :-)
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);
MissionCleanup.add($bots[$botCounter]);
$botCounter++;
}
function serverCmdMoveBotsToPlayer(%client)
{
if (isObject(%client.player))
{
for(%i = 0; %i < $botCounter; %i++)
{
if(isObject($bots[%i])){
// introduced in part 2 of this tutorial
// $bots[%i].clearAim();
$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, just to count the bots from 0 each time a mission starts, I set $botCounter to 0 in "fps/server/scripts/game.cs" -> startGame():$botCounter = 0;Another idea is to make the bots immedeately run in random directions. To achieve this, simply include the following function into "commands.cs":
function serverCmdMoveBotsRandom(%client) { for(%i = 0; %i < $botCounter; %i++) { if(isObject($bots[%i])){ %dest = getRandom(550) SPC getRandom(550) SPC getRandom(550); $bots[%i].setMoveDestination(%dest); $bots[%i].move(); } } }and you can call it directly on creation by calling the function in serverCmdAddBot() or you could bind a key to it to call the function from the client with "commandToServer('MoveBotsRandom')" and therefore make the bots spread manually.Maybe you could write some functions to make some bots "escort" your player by updating their positions with your player moves, etc. ... just go for it! And then, of course, have them attack each other, mount vehicles, hunt your player, stuff like that... ;-)
Happy "botting"!