The basics of AI
by Adam \\\"Adam487\\\" Oachs · in Technical Issues · 04/28/2007 (11:01 am) · 8 replies
I'm sure at least a few topic like this have been made, but I'll make one.
Is this topic for you?
This topic is for those who have little or no idea how to make AI. This topic discusses the basic of AI : Creating, making it go places, making it aim somewhere or at something. If you need advanced AI, like path finding, this topic is not for you, in fact, I'm not even the person for you.
What skills do you need?
To use this, you need to understand basics like making functions, calling function, etc.
Creating -- This is a very basic function for creating an AI Player. Note, you will need to change the datablock it uses to match what your game uses.
function serverCmdMakeBot(%client)
{
%client.bot[%client.botCount++] = new AIPlayer() //use of array, so client can make more then one bot
{
datablock = PlayerStandardArmor; // MAKE SURE TO CHANGE THIS TO MATCH YOUR GAME!!!!
isAiPlayer = true;
};
%client.bot[%client.botCount].setTransform(%client.player.getTransform()); //put it at client's position
%client.bot[%client.botCount].setShapeName(%client.name); //give the bot a name
}
Following -- using %bot.setMoveDestination, you can make your bots follow objects/players. This function will need to be looped using a schedule.
function serverCmdBotFollowMe(%client,%bot)
{
cancel(%client.bot[%bot].moveSchedule); //we can't have our bot following multiple things at once
doBotFollow(%client,%bot); //do the following
}
function doBotFollow(%client,%bot)
{
if(!isObject(%client.player) || !isObject(%client.player.bot[%bot])) //we can't follow non-existant objects!
return; //one of those don't exist, so quit
%client.bot[%bot].setMoveDestination(%client.player.getPosition()); //move it to the client's position
%client.bot[%bot].moveSchedule = schedule(100,0,doBotFollow,%client,%bot); //make it do it again
}
function serverCmdBotStopFollowMe(%client,%bot)
{
cancel(%client.bot[%bot].moveSchedule); //make it stop following
}
Looking at things -- using %bot.setAimLocation and %bot.setAimObject, you can make your bots look at things
function serverCmdBotLookAtThis(%client,%bot,%obj)
{
%client.bot[%bot].setAimObject(%obj); //look at %obj
}
function serverCmdBotLookAtMe(%client,%bot)
{
%client.bot[%bot].setAimObject(%client.player); //look at the client
}
function serverCmdBotLookThere(%client,%bot,%pos)
{
%client.bot[%bot].setAimLocation(%pos); //look in that direction
}
function serverCmdBotStopLook(%client,%bot)
{
%client.bot[%bot].clearAim();
}
Making your bot shoot -- You'll need to mount a weapon (%bot.mountImage(image,slot); ) to whatever mountPoint you use for weapons, then use %bot.setImageTrigger(%slot,true);
function serverCmdBotShoot(%client,%bot)
{
%client.bot[%bot].setImageTrigger(0,true); //make it shoot
}
function serverCmdBotStopShoot(%client,%bot)
{
%client.bot[%bot].setImageTrigger(0,false); //make it stop shooting
}
Hope this helped!
Is this topic for you?
This topic is for those who have little or no idea how to make AI. This topic discusses the basic of AI : Creating, making it go places, making it aim somewhere or at something. If you need advanced AI, like path finding, this topic is not for you, in fact, I'm not even the person for you.
What skills do you need?
To use this, you need to understand basics like making functions, calling function, etc.
Creating -- This is a very basic function for creating an AI Player. Note, you will need to change the datablock it uses to match what your game uses.
function serverCmdMakeBot(%client)
{
%client.bot[%client.botCount++] = new AIPlayer() //use of array, so client can make more then one bot
{
datablock = PlayerStandardArmor; // MAKE SURE TO CHANGE THIS TO MATCH YOUR GAME!!!!
isAiPlayer = true;
};
%client.bot[%client.botCount].setTransform(%client.player.getTransform()); //put it at client's position
%client.bot[%client.botCount].setShapeName(%client.name); //give the bot a name
}
Following -- using %bot.setMoveDestination, you can make your bots follow objects/players. This function will need to be looped using a schedule.
function serverCmdBotFollowMe(%client,%bot)
{
cancel(%client.bot[%bot].moveSchedule); //we can't have our bot following multiple things at once
doBotFollow(%client,%bot); //do the following
}
function doBotFollow(%client,%bot)
{
if(!isObject(%client.player) || !isObject(%client.player.bot[%bot])) //we can't follow non-existant objects!
return; //one of those don't exist, so quit
%client.bot[%bot].setMoveDestination(%client.player.getPosition()); //move it to the client's position
%client.bot[%bot].moveSchedule = schedule(100,0,doBotFollow,%client,%bot); //make it do it again
}
function serverCmdBotStopFollowMe(%client,%bot)
{
cancel(%client.bot[%bot].moveSchedule); //make it stop following
}
Looking at things -- using %bot.setAimLocation and %bot.setAimObject, you can make your bots look at things
function serverCmdBotLookAtThis(%client,%bot,%obj)
{
%client.bot[%bot].setAimObject(%obj); //look at %obj
}
function serverCmdBotLookAtMe(%client,%bot)
{
%client.bot[%bot].setAimObject(%client.player); //look at the client
}
function serverCmdBotLookThere(%client,%bot,%pos)
{
%client.bot[%bot].setAimLocation(%pos); //look in that direction
}
function serverCmdBotStopLook(%client,%bot)
{
%client.bot[%bot].clearAim();
}
Making your bot shoot -- You'll need to mount a weapon (%bot.mountImage(image,slot); ) to whatever mountPoint you use for weapons, then use %bot.setImageTrigger(%slot,true);
function serverCmdBotShoot(%client,%bot)
{
%client.bot[%bot].setImageTrigger(0,true); //make it shoot
}
function serverCmdBotStopShoot(%client,%bot)
{
%client.bot[%bot].setImageTrigger(0,false); //make it stop shooting
}
Hope this helped!
About the author
#2
Move
Aim
Fire (or jump, etc)
Are you saying you want it so that if it's trying to go somewhere, and an object's in the way, It'll go around it? That's pathfinding.
http://en.wikipedia.org/wiki/Pathfinding
04/28/2007 (2:21 pm)
As far as I'm aware of, AI can mainly do :Move
Aim
Fire (or jump, etc)
Are you saying you want it so that if it's trying to go somewhere, and an object's in the way, It'll go around it? That's pathfinding.
http://en.wikipedia.org/wiki/Pathfinding
#3
04/28/2007 (10:32 pm)
Adam I'm not saying I want it to do anything, I was trying to get a very general feel for how ai in tge works. Coding a smart ai player would be based of a bunch of way points and coding the choice it makes? I don't have any specific goals in mind I am just curious.
#4
04/30/2007 (1:11 pm)
Ok, the movement is based on setting a movement XYZ location. Then there's the function AIPlayer::onReachMoveDestination so you can set what it does when it reaches its destination
#5
04/30/2007 (2:59 pm)
I was read your post. " Following -- using %bot.setMoveDestination, you can make your bots follow objects/players. This function will need to be looped using a schedule. " How do you create the loop and where? Thanks!
#6
04/30/2007 (5:12 pm)
%bot.setMoveDestination can be looped wherever you have the %bot. For example you could dofunction botmove(%bot, %player)
{
%pos = %player.getPosition();
%bot.setMoveDestination(%pos);
schedule(100,0,botmove,%bot,%player);
}That will have your %bot follow %player forever. You can start that from wherever your origional %bot was made such as.function anyFunction(%player)
{
%bot = new AIPlayer {
datablock = PlayerData;
position = "24 -355 100.5"; }
schedule(100,0,botmove,%bot,%player);
}
#7
04/30/2007 (5:35 pm)
Thanks!
#8
04/07/2008 (7:06 pm)
Adam 487 (or anyone else) Were do I go to put the code in to make a bot?
Torque Owner Steve D