Game Development Community

dev|Pro Game Development Curriculum

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.
// 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.

About the author

Recent Blogs

• Scripted Doors
• AI Guard Unit
Page «Previous 1 2 3 4 Last »
#1
11/24/2004 (6:22 pm)
awesome! this will make it into my next release "Advanced sp_starter kit" coming soon.
#2
11/24/2004 (8:06 pm)
I was searching for a resource to make RTS Units able to detect and react to enemy units, and got lucky enough to find this--extremely well documented and thought out, and looks to be very customizable--thanks for the hard work!
#3
11/25/2004 (10:24 am)
Hey this is a great resource and I have been looking for something like. Great Work!

-Stephen
#4
11/25/2004 (11:41 am)
mark I add the variable 'pathname' and I set it's value to 'path1' but my bot not moving thru the path, how to solve this?
#5
11/25/2004 (4:46 pm)
that happenrd to me too! you have to add it to the aimarker not the bot and make sure you actually have a path1
#6
11/25/2004 (5:13 pm)
Ace, thanks for the quick help to Firas.

I've got another resource that I'll be submitting within the next day or two for approval as well. It's similar but the bot is a stationary guard that pursues when attacked, and then does his best to return once the target is lost.
#7
11/25/2004 (6:19 pm)
np, and cool, dude you rock, i was going to get to the ai script only stuff eventually, you just made it a whole lot easyer

thank you for all your hard work :)
#8
11/25/2004 (10:56 pm)
It's great to see resources like these! Great work Mark!

Nick
#9
11/26/2004 (5:09 am)
THank you so much for this awesome resource. Mabye once you get them all done you should consider doing a short ebook that people could download and it would be entirly about torque AI :)

There was one strange thing though. When I first did this I created him on the dock, with a path1 marker in front of him. He ran through it and then took a left and ran over the top of a mountain. Then he turned around ran over it again and turned around and so on so forth. I suspect it is because i did not have another path1 marker in his line of sight(it was a far ways away from the village) but I did fix so no problems, it was just kind of funny.

keep up the good work!
#10
11/26/2004 (5:10 pm)
Oops i have one question. When I made the first guy he followed the path. When I made a second guy in the exact same way and set the variable pathname to path1 he did not follow it. Any reason for this? Mabye hes just not seeing it.
#11
11/26/2004 (6:39 pm)
dont know! it worked for me , try moving the aimarker

added:
i dont think it maters where you put the aimarker, mine are quite awys from the path, course i m not using the default torque , im using the advanced sp_starter kit (not released yet) and have muli classes, but that shouldnt matter
#12
11/26/2004 (10:31 pm)
Very nice resource. Lots of good info. One question: any insight on how to get the bot to not be premature in firing it's weapon? Seems as if the bot is aiming in it's current direction for the first few rounds before the aim is set to the player. The code aims before pulling the trigger so I'm at a loss.
#13
11/27/2004 (1:01 am)
Really great resource Mark .
Keep up the good work !
#14
11/27/2004 (4:48 am)
Mark this stuff rocks !
#15
11/27/2004 (5:31 pm)
Thanks guys.

Jerry, I'll take a look at the code to see if I can find out why the bots are firing too early. probably just the order that I have things called.

As for why the player would not follow a path I'm unclear. I've started my guys out well away from the paths and they will head for the first marker - assuming they don't get blocked. I've even run multiple paths with no problems.

I would recommend double checking the paths to make sure they're right, and then make sure when you set the variable for 'pathname' that you are sure to APPLY the change - if not that might cause your bot to become lost with no path to lock onto.

In the meantime... I've been focusing on another class of ai - a guard. That resource is here:

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6773

This is a more complex AI - that uses a simple state engine - which for those of you who want to tweak and add your own AI routines, this is a good platform to work from.
#16
11/27/2004 (7:00 pm)
OMG im so stupid. I forgot to apply with the second guy. lol sorry for the trouble :)
#17
12/05/2004 (8:04 pm)
I implented this and I also have the ammo hud set up.. now it spams the console looking for "starter.fps/server/scripts/weapon.cs (128): Unable to find object: '' attempting to call function 'setAmmoAmountHud'" in the console over and over...

Is there a way to add this to the bot so he will stop freaking out about the hud :)

other then that works awesome.. I just hate console spam and so if there is a way to stop it I would love to.. otherwise I may have to disable him til I figure it out.. :)
#18
12/05/2004 (8:14 pm)
I have the HUD thing as well, and I had the same issue.

Here's how you stop the spamming of the console.

Edit your weapon.cs file and look for the lines that update the hud. (Search for HUD and it should take you to a line that looks like this.

%obj.client.setAmmoAmountHud(%currentAmmo);

change it to read:

if(%obj.isbot != true) %obj.client.setAmmoAmountHud(%currentAmmo);

There are two places I believe. One in 'onmount' and the other in 'onInventory'

Once you do that you should be alright. You may still get some from inventory.cs - I'm slowly working on hunting down those annoyances.

If I get them all hammered down, I'll let you know.
#19
12/05/2004 (8:23 pm)
thanks..
now thats fixed but getting more.. :)

but at least I am on the right track now.. :)
#20
12/05/2004 (8:29 pm)
woohoo
fixed that spam but now if I die the bot will spam looking for me....
starter.fps/server/scripts/aipatrol.cs (446): Unable to find object: '0' attempting to call function 'getPosition'

when I respawn its all good, no more spam.. so guess I need to stay alive.. :)
Page «Previous 1 2 3 4 Last »