AI Guard Unit
by Mark Holcomb · 11/27/2004 (5:18 pm) · 335 comments
Download Code File
This is my second attempt at making an AI controlled character. The first was an
AIPlayer that would follow paths. This character is a guard unit. The guard will
wait at it's post (spawn point) until it sees a target. It will then attack that
target while trying to close with it.
If the target is lost the bot will wait at the last place it saw the character and
look around for a little bit, and then it will try to return to it's post.
There is a chance the bot will get stuck trying to return, but there is a simple
routine that has the bot try to move in random directions to try and clear itself
of any obstacles between it and it's post. It's not perfect.
The thinking routine is a simple state machine - which can be expanded on to give the bot more responses and actions.
As with my first ai character, I am using a simple system that allows the designer to drop
markers in the game map that will mark where the bots will start out. When the mission is loaded, the markers are detected and bots are spawned at the marker locations. The markers are then hidden from view. (The markers can be left visible to help in map editing.)
The markers for the guards can be given a dynamic variable called respawn. (Assigned to the marker during map editing.) 'Respawn' will determine whether a bot respawns or not upon death.
The bots have an attention setting. How often they scan is determined by their attention level. The bots get more sluggish (freeing up processor time) when targets are too far away. Conversely, the bot becomes incrementally more attentive as targets come within range, and further aware as targets come into sight.
When a target is found in range and in sight the bot will shoot at the target. The firing sequence is a step of scheduled calls that call for a firing cycle, a trigger down cycle, and a firing delay to control bot rate of fire.
When a bot is attacked it will attempt to sideatep and it's field of vision is temporarily increased to a 360deg field of vision - to emulate looking around to see what happened. The bot's attention level is also set to make it think at it's fastest rate when attacked.
To use the AIGuard...the following changes need to be made.
1. Back up your original game.cs, player.cs, and your current build of Torque. To install AIGuard will require a recompile, since I have created a new class cloned by copying AIPlayer.cc and AIPlayer.h and renaming all references to AIPlayer to AIGuard.
(I did this because I wanted to be able to run both my AIPatroller and AIGuards in the same maps and did not want to have any confusion between them.)
2. Add the files AIGuard.cc and AIGuard.h to your Torque project. (In my instance I saved them to my c:\Torque\engine\game directory and then added them into my project.)
3. Recompile your project and copy your new executable to the appropriate directory for your app.
4. Copy the file AIGuard.cs into your server/scripts directory.
5. Modify game.cs to add the line
to the function onServerCreated().
6. Also in game.cs: The section for function StartGame needs to modified the following ways:
Below the lines that read:
add this:
(If you followed my previous resource you may not have any reference to AImanager. Don't fret... just look for the place in game.cs where you have
and put in
right underneath it.
7. In player.cs in the code for Armor::Damage modify the lines
to read:
*** If you followed my previous resource there should be no need to change this code.
8. Load your map - Stronghold as an example.
9. Go into the map editor. (F11) Then go into the Editor Creator (F4)
10. Under Shapes there should be a drop down called AIMarker, under that a new item called AIGuard.
11. Create a new AIGuard marker.
12. Select your marker, position it where you like and hit (F3) to modify the marker.
13. If you want to override the default respawn value - create a dynamic variable called respawn and set it's value to true or false.
14. Update your item by clicking 'APPLY'- very important and easy to miss step.
15. Save your mission and reload it.
A bot called Guard1 should appear at the spot of your marker.
If you come within range of the guard he should shoot at you and try to hunt you down.
If you get away, or when you die, he should return to his post.
I hope the resource helps other people get up and running with some AI code in their game.
And again, I'd like to thank the other members of this website whose code has been used in several places in the scripting to make this all work.
Mark H.
P.S. I've also included my AIPatrol class files with this - to install it follow the same instructions as for AIGuard, just substitute AIPatrol where AIGuard appears in the instructions. They can both be run at the same times with no problems.
P.S.S. 11/28/04 - I modified the AIPatrol.cs file to correct for a couple of typos.
P.S.S.S 12/3/04 - Added ammo and health seeking capabilities and fixed some errors. (Read posts below - or file in .zip for full details.)
This is my second attempt at making an AI controlled character. The first was an
AIPlayer that would follow paths. This character is a guard unit. The guard will
wait at it's post (spawn point) until it sees a target. It will then attack that
target while trying to close with it.
If the target is lost the bot will wait at the last place it saw the character and
look around for a little bit, and then it will try to return to it's post.
There is a chance the bot will get stuck trying to return, but there is a simple
routine that has the bot try to move in random directions to try and clear itself
of any obstacles between it and it's post. It's not perfect.
The thinking routine is a simple state machine - which can be expanded on to give the bot more responses and actions.
As with my first ai character, I am using a simple system that allows the designer to drop
markers in the game map that will mark where the bots will start out. When the mission is loaded, the markers are detected and bots are spawned at the marker locations. The markers are then hidden from view. (The markers can be left visible to help in map editing.)
The markers for the guards can be given a dynamic variable called respawn. (Assigned to the marker during map editing.) 'Respawn' will determine whether a bot respawns or not upon death.
The bots have an attention setting. How often they scan is determined by their attention level. The bots get more sluggish (freeing up processor time) when targets are too far away. Conversely, the bot becomes incrementally more attentive as targets come within range, and further aware as targets come into sight.
When a target is found in range and in sight the bot will shoot at the target. The firing sequence is a step of scheduled calls that call for a firing cycle, a trigger down cycle, and a firing delay to control bot rate of fire.
When a bot is attacked it will attempt to sideatep and it's field of vision is temporarily increased to a 360deg field of vision - to emulate looking around to see what happened. The bot's attention level is also set to make it think at it's fastest rate when attacked.
To use the AIGuard...the following changes need to be made.
1. Back up your original game.cs, player.cs, and your current build of Torque. To install AIGuard will require a recompile, since I have created a new class cloned by copying AIPlayer.cc and AIPlayer.h and renaming all references to AIPlayer to AIGuard.
(I did this because I wanted to be able to run both my AIPatroller and AIGuards in the same maps and did not want to have any confusion between them.)
2. Add the files AIGuard.cc and AIGuard.h to your Torque project. (In my instance I saved them to my c:\Torque\engine\game directory and then added them into my project.)
3. Recompile your project and copy your new executable to the appropriate directory for your app.
4. Copy the file AIGuard.cs into your server/scripts directory.
5. Modify game.cs to add the line
exec("./aiGuard.cs"); to the function onServerCreated().
6. Also in game.cs: The section for function StartGame needs to modified the following ways:
Below the lines that read:
// Start the AIManager
new ScriptObject(AIManager) {};
MissionCleanup.add(AIManager);
AIManager.think();add this:
AIGuard::LoadEntities();
(If you followed my previous resource you may not have any reference to AImanager. Don't fret... just look for the place in game.cs where you have
AIPlayer::LoadEntities
and put in
AIGuard::LoadEntities();
right underneath it.
7. In player.cs in the code for Armor::Damage modify the lines
// Deal with client callbacks here because we don't have this
// information in the onDamage or onDisable methods
%client = %obj.client;
%sourceClient = %sourceObject ? %sourceObject.client : 0;
if (%obj.getState() $= "Dead")
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);to read:
// Deal with client callbacks here because we don't have this
// information in the onDamage or onDisable methods
%client = %obj.client;
%sourceClient = %sourceObject ? %sourceObject.client : 0; if (%obj.isbot == true)
{
%obj.attentionlevel=1;
%obj.enhancefov(%obj);
}
if (%obj.getState() $= "Dead")
{
if (%obj.isbot == true)
{
if (%obj.respawn == true)
{
%obj.delaybeforerespawn(%obj.botname, %obj.markerpos, %obj.marker);
%this.player=0;
}
}
else
{
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}
}*** If you followed my previous resource there should be no need to change this code.
8. Load your map - Stronghold as an example.
9. Go into the map editor. (F11) Then go into the Editor Creator (F4)
10. Under Shapes there should be a drop down called AIMarker, under that a new item called AIGuard.
11. Create a new AIGuard marker.
12. Select your marker, position it where you like and hit (F3) to modify the marker.
13. If you want to override the default respawn value - create a dynamic variable called respawn and set it's value to true or false.
14. Update your item by clicking 'APPLY'- very important and easy to miss step.
15. Save your mission and reload it.
A bot called Guard1 should appear at the spot of your marker.
If you come within range of the guard he should shoot at you and try to hunt you down.
If you get away, or when you die, he should return to his post.
I hope the resource helps other people get up and running with some AI code in their game.
And again, I'd like to thank the other members of this website whose code has been used in several places in the scripting to make this all work.
Mark H.
P.S. I've also included my AIPatrol class files with this - to install it follow the same instructions as for AIGuard, just substitute AIPatrol where AIGuard appears in the instructions. They can both be run at the same times with no problems.
P.S.S. 11/28/04 - I modified the AIPatrol.cs file to correct for a couple of typos.
P.S.S.S 12/3/04 - Added ammo and health seeking capabilities and fixed some errors. (Read posts below - or file in .zip for full details.)
#322
I'll try again on a different computer in a while though just to see if anything changes then.
thanks again for all the help.
08/19/2008 (11:06 pm)
any luck mark? I've followed a few different tutorials i've found and nothing changes... I'll try again on a different computer in a while though just to see if anything changes then.
thanks again for all the help.
#323
If you're still having trouble, check out the Improved AI Guard Unit resource that I just posted. It doesn't need any engine modification, and has a lot of enhancements and new features.
08/27/2008 (1:33 am)
I'm not sure about with the express edition, but with the standard edition of Visual Studio 2008 I've been able to compile fine. Try this article here; but make sure to use the VS2005 files, not the vc7 files (they won't work with VS2008).If you're still having trouble, check out the Improved AI Guard Unit resource that I just posted. It doesn't need any engine modification, and has a lot of enhancements and new features.
#324
Why doesnt the guard appear?
Thanks.
01/24/2009 (3:36 am)
Hi,I tried AiGuard but when i add the AImarker and reboot mission,nothing comes up.The code aiGuard.cs has the codes $AI_GUARD_ENABLED = true; but in the console it still shows guard entituies disabled.Under game.cs i also have the codes exec("./aiGuard.cs"); and AIGuard::LoadEntities();Why doesnt the guard appear?
Thanks.
#325
Sounds like you did all of the scripting correctly, but did you add in then compile the source code? It doesn’t say “Torque Owner” under your name; which is either a website bug or that means you don’t currently have access to the source code.
In any case, this resource is more or less obsolete and unsupported by the author; so I'd recommend using the Improved AI Guard Unit resource instead. It’s an updated, script-only version of this resource that includes quite a number of bug fixes and new features.
01/24/2009 (11:26 am)
@ Yi song LohSounds like you did all of the scripting correctly, but did you add in then compile the source code? It doesn’t say “Torque Owner” under your name; which is either a website bug or that means you don’t currently have access to the source code.
In any case, this resource is more or less obsolete and unsupported by the author; so I'd recommend using the Improved AI Guard Unit resource instead. It’s an updated, script-only version of this resource that includes quite a number of bug fixes and new features.
#326
I do believe you are correct and that he has not compiled the code, or is trying to use it in a newer version which may require some updates.
01/24/2009 (12:32 pm)
This resource is far from being obsolete, please do not tell people that. The author did an outstanding job on this and it is even used today. In fact, I believe it helped you create the resource above, so it means it's very much still usable.I also don't expect the author to support something for so many years, that's where the community steps in. But I have seen Mark answer questions on here about 4-5 months ago? So he is responsive when he does not need to be.I do believe you are correct and that he has not compiled the code, or is trying to use it in a newer version which may require some updates.
#327
I also agree that Mark Holcomb made a great resource. I thank him for it, and there's no way I could have made my updated version without this resource (and other resources) to help me (which is why I posted the improved version as a resource in the first place).
Now, having said that; this resource has been made obsolete by my resource. The improved version can do everything this resource can but is easier to install, has fewer bugs and more features. There's no reason I can see why the majority of people should use this resource anymore. This resource was great for its time, but it is now a bit outdated. There maybe a few very specialized cases where this resource could be better than mine, but for the most part, people should just use the newer version I made.
I also completely agree with you when you said: “I also don't expect the author to support something for so many years, that's where the community steps in.”
And as part of the community, I stepped in. I’ve updated and improved this resource as well as attempted to help a few of the previous commenters with their problems. And part of that help was advising them to use the newest version of this resource; which seemed appropriate since both of them seemed to be having problems complying and my resource is script only.
01/24/2009 (2:10 pm)
@ Randy HearnI also agree that Mark Holcomb made a great resource. I thank him for it, and there's no way I could have made my updated version without this resource (and other resources) to help me (which is why I posted the improved version as a resource in the first place).
Now, having said that; this resource has been made obsolete by my resource. The improved version can do everything this resource can but is easier to install, has fewer bugs and more features. There's no reason I can see why the majority of people should use this resource anymore. This resource was great for its time, but it is now a bit outdated. There maybe a few very specialized cases where this resource could be better than mine, but for the most part, people should just use the newer version I made.
I also completely agree with you when you said: “I also don't expect the author to support something for so many years, that's where the community steps in.”
And as part of the community, I stepped in. I’ve updated and improved this resource as well as attempted to help a few of the previous commenters with their problems. And part of that help was advising them to use the newest version of this resource; which seemed appropriate since both of them seemed to be having problems complying and my resource is script only.
#328
Oh, and I do prefer to include as much as possible in the code as opposed to scripting. Just personal preference I guess.
01/24/2009 (2:34 pm)
Did not to mean too offend you, I simply prefer this one over yours. It is a simple starting place for people to "build" upon for what they want it to do.Oh, and I do prefer to include as much as possible in the code as opposed to scripting. Just personal preference I guess.
#329
My resource however combines the functions of the AI guard and patrol into one, then just uses the AI player class. So even though you don't have to make any source code changes to use my version, it actually utilizes the source code as much as this resource does.
If you're going to be heavily modifying the AI, you're correct that this script is probably a bit easier to understand at first. My script has everything form this resource and a lot of other functions (making it larger and more complex).
But on the other hand, my resource has a number of bug fixes (I can think of about 5-6 off the top of my head) which you would need to, or at least may want to, port back. And in the end, merging those fixes (or recoding them again yourself from scratch) would likely end up being more work than just starting with mine from the beginning.
01/24/2009 (2:59 pm)
You say that you like your AI to do "as much as possible in the code". But, this resource's source code is actually more or less just copies of the aiPlayer.cc and aiPlayer.h files. The reason this resource did that was so that you could have the AI guard unit, the AI patrol and another random AI player all working at the same time. My resource however combines the functions of the AI guard and patrol into one, then just uses the AI player class. So even though you don't have to make any source code changes to use my version, it actually utilizes the source code as much as this resource does.
If you're going to be heavily modifying the AI, you're correct that this script is probably a bit easier to understand at first. My script has everything form this resource and a lot of other functions (making it larger and more complex).
But on the other hand, my resource has a number of bug fixes (I can think of about 5-6 off the top of my head) which you would need to, or at least may want to, port back. And in the end, merging those fixes (or recoding them again yourself from scratch) would likely end up being more work than just starting with mine from the beginning.
#330
I'm glad that the code is still available for people to use. The work was always issued as a starting point for people to take and adapt or expand upon since the core Torque installation had very very little to work with. And many people throughout the GG community have adapted and modified it.
For that I'm glad, especially when people take their work and make it available for others. That's the spirit of this community that makes this site a great resource for everyone from beginner to pro.
So while it makes me sad to hear that my code is being deemed "obsolete" because it reminds me that I haven't been able to go back and revisit a project that I enjoyed. I can't argue the fact that other people have continued their work and have other tools out there.
But one thing I do know is that there are no perfect drop in solutions in this area. It's what we can all learn from the collective that we can put into our own works that is important. So take pieces from my code, pieces from Twisted Jenius' code and use what makes sense or that fits your needs. I know that if I were to sit down and rewrite this resource again that it wouldn't be the same. I've studied other people's work and learned different ways to approach some of the things that I was trying to address the first time around.
To Yi song Loh and others: It is difficult for me to respond back to people when they are having issues with the code. Even though I wrote it, I haven't touched any Torque scripting or any programming projects since shortly after I made this resource. Layoffs, career changes, kids, etc... can cause that to happen.
So now, going back the code looks almost as alien and foreign to me as it probably does to others seeing it for the first time. Which only stresses... comment your code, and then comment it some more.
Thankfully there are still many others out here on the community that will step in and guide people when they are struggling with something. In this case I believe that Twisted's initial diagnosis that you didn't compile the program is correct.
01/25/2009 (4:11 pm)
Well, speaking for the author, which I think I'm entitled to do...I'm glad that the code is still available for people to use. The work was always issued as a starting point for people to take and adapt or expand upon since the core Torque installation had very very little to work with. And many people throughout the GG community have adapted and modified it.
For that I'm glad, especially when people take their work and make it available for others. That's the spirit of this community that makes this site a great resource for everyone from beginner to pro.
So while it makes me sad to hear that my code is being deemed "obsolete" because it reminds me that I haven't been able to go back and revisit a project that I enjoyed. I can't argue the fact that other people have continued their work and have other tools out there.
But one thing I do know is that there are no perfect drop in solutions in this area. It's what we can all learn from the collective that we can put into our own works that is important. So take pieces from my code, pieces from Twisted Jenius' code and use what makes sense or that fits your needs. I know that if I were to sit down and rewrite this resource again that it wouldn't be the same. I've studied other people's work and learned different ways to approach some of the things that I was trying to address the first time around.
To Yi song Loh and others: It is difficult for me to respond back to people when they are having issues with the code. Even though I wrote it, I haven't touched any Torque scripting or any programming projects since shortly after I made this resource. Layoffs, career changes, kids, etc... can cause that to happen.
So now, going back the code looks almost as alien and foreign to me as it probably does to others seeing it for the first time. Which only stresses... comment your code, and then comment it some more.
Thankfully there are still many others out here on the community that will step in and guide people when they are struggling with something. In this case I believe that Twisted's initial diagnosis that you didn't compile the program is correct.
#331
I got a little problem. I change the default AI 'Kork' to my own AI. It will attack the kork i am controlling. But when i change my own character(kork) to my own character, the AI suddenly doesn't seems to recognise me and wont attack me anymore. Any of you got the same problem?
Thanks in advance!
02/01/2010 (10:22 am)
HiI got a little problem. I change the default AI 'Kork' to my own AI. It will attack the kork i am controlling. But when i change my own character(kork) to my own character, the AI suddenly doesn't seems to recognise me and wont attack me anymore. Any of you got the same problem?
Thanks in advance!
#332
In mine the toon would only be seen by the AI when I jumped, so he was seen just not all the time as my center point was between the characters feet and would dissappear under the terrain mesh and LOS checks would fail.
I think it's the center of the bounds box. See if that helps,
02/01/2010 (6:04 pm)
Back in 2005 when I used this resource I remember that the ai would aim at the center point of, and it's been a while mind you so I don't remember which, either the LOS box or the center of the bounds box. I had to go into my model and move it's center point up to about neck high to get proper detection.In mine the toon would only be seen by the AI when I jumped, so he was seen just not all the time as my center point was between the characters feet and would dissappear under the terrain mesh and LOS checks would fail.
I think it's the center of the bounds box. See if that helps,
#333
Thanks for the reply. I realise the problem lies with my model. I try using model with no joints, the AI will attack it. But when i put a model with full joint, the AI will total ignore it. Any of you got any idea what is going on?
Thanks in advance.
02/01/2010 (9:05 pm)
hi karczagThanks for the reply. I realise the problem lies with my model. I try using model with no joints, the AI will attack it. But when i put a model with full joint, the AI will total ignore it. Any of you got any idea what is going on?
Thanks in advance.
#334
02/04/2010 (10:33 pm)
My first suggestion is to put a proper skelaton/armature structure in your model with all the required nodes. Then see if it works properly. May be the ai defaults to a node for LOS that yours does not have. So they can't see yours.
#335
09/20/2012 (4:05 am)
Downloadable resource file is gone :-( 
Associate Jaimi McEntire
King of Flapjacks
needs to be changed to: