Artificial Intelligence Beginning
by Mark Holcomb · 11/24/2004 (6:00 pm) · 72 comments
Download Code File
I was looking for a way to make Kork a little more responsive and aggressive. To which end I have come up with the following AI scheme in scripting.
I have created a simple system that allows the designer to drop markers in the game map that will mark where patrolling 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 themselves can be given two dynamic variables in the map editor. The first is 'respawn' which will determine whether a bot respawns or not. The second variable is 'pathname' which should be set to a valid path on the map. (If no path is set the bot acts as a stationary guard.)
The bots will patrol the paths, routinely scanning for nearby clients to target.
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'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.
In my code I wiped out using AIManager, and placed all of the AIManager code in a seperate file called AIManger.cs - which I don't load. I don't use it - because each bot thinks individually based on it's attention level.
To implement this version of AIPlayer the following changes need to be made.
1. Back up your original game.cs, player.cs, and AIPlayer.cs
2. In game.cs: The section for function StartGame needs to modified the following ways:
Delete the following lines.
and add this in it's place:
3. In player.cs in the code for Armor::Damage modify the lines
to read:
4. Replace AIPlayer.CS with the code from the file newaiplayer.cs
5. Load your map - Stronghold as an example. Kork should be missing.
6. Go into the map editor. (F11) Then go into the Editor Creator (F4)
7. Under Shapes there should be a new drop down called AIMarker, under that a new item called AIPlayer.
8. Create a new AIPlayer marker.
9. Select your marker, position it where you like and hit (F3) to modify the marker.
10. Create a new dynamic variable called 'pathname' and set its value to 'path1'
11. If you want to override the default respawn value - create another dynamic variable called respawn and set it's value to true or false.
12. Update your item by clicking 'APPLY' - very important and easy to miss step.
13. Save your mission and reload it.
A bot called Kork1 should appear and be following the usual path. Kork1 should shoot at you if you are in front of him, and within range.
I hope the resource helps other people get up and running with some AI code in their game.
I'd like to thank the other members of this website whose code has been used in several places in the script above.
I was looking for a way to make Kork a little more responsive and aggressive. To which end I have come up with the following AI scheme in scripting.
I have created a simple system that allows the designer to drop markers in the game map that will mark where patrolling 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 themselves can be given two dynamic variables in the map editor. The first is 'respawn' which will determine whether a bot respawns or not. The second variable is 'pathname' which should be set to a valid path on the map. (If no path is set the bot acts as a stationary guard.)
The bots will patrol the paths, routinely scanning for nearby clients to target.
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'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.
In my code I wiped out using AIManager, and placed all of the AIManager code in a seperate file called AIManger.cs - which I don't load. I don't use it - because each bot thinks individually based on it's attention level.
To implement this version of AIPlayer the following changes need to be made.
1. Back up your original game.cs, player.cs, and AIPlayer.cs
2. In game.cs: The section for function StartGame needs to modified the following ways:
Delete the following lines.
// Start the AIManager
new ScriptObject(AIManager) {};
MissionCleanup.add(AIManager);
AIManager.think();and add this in it's place:
AIPlayer::LoadEntities();
3. 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.getState() $= "Dead")
{
if(%obj.isbot == false)
{
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}
}4. Replace AIPlayer.CS with the code from the file newaiplayer.cs
5. Load your map - Stronghold as an example. Kork should be missing.
6. Go into the map editor. (F11) Then go into the Editor Creator (F4)
7. Under Shapes there should be a new drop down called AIMarker, under that a new item called AIPlayer.
8. Create a new AIPlayer marker.
9. Select your marker, position it where you like and hit (F3) to modify the marker.
10. Create a new dynamic variable called 'pathname' and set its value to 'path1'
11. If you want to override the default respawn value - create another dynamic variable called respawn and set it's value to true or false.
12. Update your item by clicking 'APPLY' - very important and easy to miss step.
13. Save your mission and reload it.
A bot called Kork1 should appear and be following the usual path. Kork1 should shoot at you if you are in front of him, and within range.
I hope the resource helps other people get up and running with some AI code in their game.
I'd like to thank the other members of this website whose code has been used in several places in the script above.
#62
The bot is spawned but does not move. If I leave the mission and then load it back up he spawns again and does follow the path !?
Anyone know what may cause that?
EDIT: The part where I say he spawns and does not move I am not referring to the marker itself, that I placed into the mission, saved and is there. Its when I start the game fresh on the first load the AI is stationary but moves when I enter the game on the second time.
EDIT 2: OK so I tracked down something really odd. My problem does not exist in a debug build of the engine, but it does occur in a release version. For the heck of I took an older build of my project and made a release version and sure enough I have the same issue.
So it seems as though my problem happens in a release build and not in a demo build, anyone else have this happen?
06/21/2008 (12:01 pm)
This works for me in an odd way.The bot is spawned but does not move. If I leave the mission and then load it back up he spawns again and does follow the path !?
Anyone know what may cause that?
EDIT: The part where I say he spawns and does not move I am not referring to the marker itself, that I placed into the mission, saved and is there. Its when I start the game fresh on the first load the AI is stationary but moves when I enter the game on the second time.
EDIT 2: OK so I tracked down something really odd. My problem does not exist in a debug build of the engine, but it does occur in a release version. For the heck of I took an older build of my project and made a release version and sure enough I have the same issue.
So it seems as though my problem happens in a release build and not in a demo build, anyone else have this happen?
#63
same error for me, is this code still good for 1.5? I tried everything to fix this :(
03/04/2009 (8:38 am)
Quote:this don't work for me :( (i may have done it wrong..im new :3)
Compiling starter.fps/server/scripts/player.cs...
starter.fps/server/scripts/player.cs Line: 826 - parse error
>>> Advanced script error report. Line 826.
>>> Some error context, with ## on sides of error halt:
%sourceClient = %sourceObject ? %sourceObject.client : 0;
if (%obj.getState() $= "Dead")
{
if(%obj.isbot == false)
{
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}
}
function Armor::onDamage(%this, %obj, %delta)
##{##
// This method is invoked by the ShapeBase code whenever the
// object's damage level changes.
if (%delta > 0 && %obj.getState() !$= "Dead") {
// Increment the flash based on the amount.
%flash = %obj.getDamageFlash() + ((%delta / %this.maxDamage) * 2);
>>> Error report complete.
same error for me, is this code still good for 1.5? I tried everything to fix this :(
#64
Using a script IDE like Torsion makes it much easier to find such errors, if you're not using one already.
03/04/2009 (2:04 pm)
@Vince: you're likely looking at a basic syntax error somewhere. Perhaps an opening { without a closing }, or vice versa.Using a script IDE like Torsion makes it much easier to find such errors, if you're not using one already.
#65
03/05/2009 (3:01 am)
If you're having problems with this resource, you could try the Improved AI Guard Unit resource. It contains all of the features of this resource (unless you count the bot never being allowed to deviate from its path as a feature) and has been updated.
#66
04/23/2009 (12:09 pm)
he for sum reason it shows the weapon that the bot is holding, but he wont shoot at me, and also the aimarker is shown through the game. How do i fix this
#67
Also, have you read all the comments under the resource? It is often invaluable.
04/23/2009 (12:30 pm)
@devry: You need to give much more detail about what you've done and tried.Also, have you read all the comments under the resource? It is often invaluable.
#68
04/23/2009 (3:09 pm)
Well I have gotten me AI player loaded into the game, i have copied all of the necesary files into the approperate folder. When I start the game my bot spawns and he walks toward the path that I made but when he approaches me he doesnt fire, he aims at me but he doesnt shoot anything. I have done some tutorials on codesampler.com/torque, and I know that one of the tutorials has to do with adding a bot file into the server file. It seems like if I make the wrong change in the neaiplayer file then the aiplayer wont load. Oh and btw I am starting from scratch, I am not using the FPS tutorial I am using another mission that I am starting from scratch. I have done all of this in the actual FPS tutorial and it works, when I load up my own game everything works besides the aiplayer shooting at me.
#69
06/26/2009 (4:55 am)
For some reason the AI marker dosen't show up in the shapes menu. I am using TGEA 1.8.1 Can anyone tell me why this might be?
#70
03/08/2010 (4:23 pm)
I was wondering if there were any updates for T3D to convert this over. I tried using it but I can't get it to work.
#71
10. Create a new dynamic variable called 'pathname' and set its value to 'path1'
I'm a new guy, and I don't know how to create a new dynamic varible in the world mapping, please help me..
05/06/2010 (8:38 pm)
Thank you for your teaching! And I have a problem in doing this:10. Create a new dynamic variable called 'pathname' and set its value to 'path1'
I'm a new guy, and I don't know how to create a new dynamic varible in the world mapping, please help me..
#72
Thanks,
Robert
01/02/2012 (8:21 pm)
Thanks for posting this. I am trying to add my first AI enemy. Can I use this code with T3D 1.2? Do I need the UAISK to use this code?Thanks,
Robert

Torque Owner Oliver \Tails\ Leigh