Killer Kork, an ai bot
by devans · 07/03/2006 (11:35 am) · 94 comments
Download Code File
Description:
Killer Kork is an ai bot with a purpose (you guessed it), eliminating opponents, but with a twist! With this resource, Kork must use his new-found senses of sight and sound to track down the enemy. If he hasn't seen or heard you, he doesn't 'magically' know you exist. Even with these limitations, you might find him hard to beat!
To use:
Killer Kork is set in the starter.fps scene. Just download and unzip the four files in killer.kork.ai.zip into the starter.fps\server\scripts folder (backup aiPlayer.cs first, if you want to keep the original), and launch. No TGE engine mods necessary, completely script-based.
By default the scripts will create two teams of bots, the Brains and the Algos, which will commence to battle it out. Simply setting the $AIPlayer::TeamCount, you can set how many bots are spawned on each team. It is also possible to set whether the bots will attack human opponents using the isHumanFriendly boolean in the AIManager class (bottom of aiPlayer). By default, 'Evil Brain' and 'Evil Algo' are the only bots set to attack human players on sight.
The files:
..\server\scripts\aiPlayer.cs -low lvl body functions (heavily modified from original)
..\server\scripts\aiAlgo1.cs -high lvl decision-making functions (new)
..\server\scripts\aiSensObj.cs -sensory feedback (new)
..\server\scripts\consoleFunctions.cs -convenient in-game functions (new, not required)
Features:
- Raytrace vision
- Hearing radius
- Basic flocking
- Retrieval of heath/ammo
- Basic obstacle avoidance
- Avoids bodies of water
- Attempts to get 'unstuck'
- Quick 'dodge' function that can happen between think cycles
Bonus:
- Tons of static variables to set various attributes of the bots
- Heavily commented code
- Configurable debug lvl (set through $AIPlayer::DebugLevel, or on the fly with the Con::setDebug function) of messages sent to console (0 error, 1 critical, ...5 verbose)
- Spawn counter for each team (higher number = losing team)
Bot Behavior states:
- Wander (roam the map, keeping an eye on path markers, look for health/ammo)
- Attack (if any enemies are sensed, go get 'em!)
- Search (look for a lost target)
- Respond (react to unknown threat)
- Follow (follow friendly)
- Assist (assist friendly)
Below I'll briefly try to describe some of the finer points of Kork's ai code, which might not be so obvious on first load.
- Kork has a 'center of vision', and a peripheral vision cone. Players in his center of vision have a much better chance of being spotted. Moving targets are also much easier for Kork to spot.
- If Kork has seen an opponent, then subsequently lost sight of them, he will run to the last location of the sighting and start searching from that point
- In battle, Kork attempts to slide away from the direction his opponent's weapon is facing. This tends to make two bots locked in combat weave back and forth, attempting to get out of the way while compensating for their opponent's movement.
- Kork tries to 'snipe' if his opponent is far enough away and not facing his direction, before running in to close the gap. This helps with aim, since only one object is then in motion.
- Kork will be put into a 'Respond' state if he takes damage or senses a nearby explosion without having a target.
- Kork will go to fetch ammo he might have seen immediately if he runs out, no matter what the situation. If he reaches his 'low' threshold of either ammo or health, however, he will only go to fetch if not engaged with an opponent.
- In order to facilitate retrieval of items (i.e., not getting stuck), upon approach a horizontal line is drawn out from the eyePoint, which is then used to check a clear path by raytracing points (default 40 points at 0.3 units apart)from the line to the item. Kork then uses the average of the points that tested 'good' as his approach route.
- To prevent Kork from straying too far, he attempts to stay within range of the path nodes (DistPathMax), but will freely wander around looking for something to do as long as he's seen a path marker within range.
- Since this is based on the stock starter.fps crossbow weapon, Kork will rely on the explosion radius damage of the crossbow bolt by aiming at the feet of a particularly fast-moving opponent. He does, however, do a pretty good job of anticipating the location of a moving target when shooting a bolt.
Additional info/History:
Killer Kork was born as a project to make TGE 'do' something during a visit from my brother last August. It was originally envisioned as a battleground for pitting each our own ai code against the other, but considering my brother's Ph.D. in physics, as well as 10+ years professional coding experience, it quickly became a losing battle for my ai code (which was busy doing 'victory dances' and such on getting a frag... =). The result I'm posting is his code (with his blessing), developed over a two week period, without any previous torque-script experience. That said, it obviously isn't fully featured, optimized, etc, but hopefully a solid starting point for anyone wanting some ai (and can't wait for the 'real deal').
On a side note, re-implementing the original idea of two sets of combating ai code is easily achievable by simply modifying the AIManager class (and uncomment the execute line at top of AIPlayer) to include two different versions of ai code. We were also using a modified .mission file with a second path, where the other team could start somewhat unmolested.
This code comes AS-IS. It was based on TGE 1.3, but I've given it a quick test in 1.4, and everything seems to run fine. Instead of letting it gather dust on my HD, I hope someone finds it useful. If you do use it in a project, or change it in an interesting way, please send me a mail dgevans at hotmail dot com. It was a learning experience for me, and now hopefully others =)
I'd also like to thank Sam Bacsa for Codeweaver (then called TorqueScript Dev?), which we used extensively. It was a HUGE help!
/daniel
Description:
Killer Kork is an ai bot with a purpose (you guessed it), eliminating opponents, but with a twist! With this resource, Kork must use his new-found senses of sight and sound to track down the enemy. If he hasn't seen or heard you, he doesn't 'magically' know you exist. Even with these limitations, you might find him hard to beat!
To use:
Killer Kork is set in the starter.fps scene. Just download and unzip the four files in killer.kork.ai.zip into the starter.fps\server\scripts folder (backup aiPlayer.cs first, if you want to keep the original), and launch. No TGE engine mods necessary, completely script-based.
By default the scripts will create two teams of bots, the Brains and the Algos, which will commence to battle it out. Simply setting the $AIPlayer::TeamCount, you can set how many bots are spawned on each team. It is also possible to set whether the bots will attack human opponents using the isHumanFriendly boolean in the AIManager class (bottom of aiPlayer). By default, 'Evil Brain' and 'Evil Algo' are the only bots set to attack human players on sight.
The files:
..\server\scripts\aiPlayer.cs -low lvl body functions (heavily modified from original)
..\server\scripts\aiAlgo1.cs -high lvl decision-making functions (new)
..\server\scripts\aiSensObj.cs -sensory feedback (new)
..\server\scripts\consoleFunctions.cs -convenient in-game functions (new, not required)
Features:
- Raytrace vision
- Hearing radius
- Basic flocking
- Retrieval of heath/ammo
- Basic obstacle avoidance
- Avoids bodies of water
- Attempts to get 'unstuck'
- Quick 'dodge' function that can happen between think cycles
Bonus:
- Tons of static variables to set various attributes of the bots
- Heavily commented code
- Configurable debug lvl (set through $AIPlayer::DebugLevel, or on the fly with the Con::setDebug function) of messages sent to console (0 error, 1 critical, ...5 verbose)
- Spawn counter for each team (higher number = losing team)
Bot Behavior states:
- Wander (roam the map, keeping an eye on path markers, look for health/ammo)
- Attack (if any enemies are sensed, go get 'em!)
- Search (look for a lost target)
- Respond (react to unknown threat)
- Follow (follow friendly)
- Assist (assist friendly)
Below I'll briefly try to describe some of the finer points of Kork's ai code, which might not be so obvious on first load.
- Kork has a 'center of vision', and a peripheral vision cone. Players in his center of vision have a much better chance of being spotted. Moving targets are also much easier for Kork to spot.
- If Kork has seen an opponent, then subsequently lost sight of them, he will run to the last location of the sighting and start searching from that point
- In battle, Kork attempts to slide away from the direction his opponent's weapon is facing. This tends to make two bots locked in combat weave back and forth, attempting to get out of the way while compensating for their opponent's movement.
- Kork tries to 'snipe' if his opponent is far enough away and not facing his direction, before running in to close the gap. This helps with aim, since only one object is then in motion.
- Kork will be put into a 'Respond' state if he takes damage or senses a nearby explosion without having a target.
- Kork will go to fetch ammo he might have seen immediately if he runs out, no matter what the situation. If he reaches his 'low' threshold of either ammo or health, however, he will only go to fetch if not engaged with an opponent.
- In order to facilitate retrieval of items (i.e., not getting stuck), upon approach a horizontal line is drawn out from the eyePoint, which is then used to check a clear path by raytracing points (default 40 points at 0.3 units apart)from the line to the item. Kork then uses the average of the points that tested 'good' as his approach route.
- To prevent Kork from straying too far, he attempts to stay within range of the path nodes (DistPathMax), but will freely wander around looking for something to do as long as he's seen a path marker within range.
- Since this is based on the stock starter.fps crossbow weapon, Kork will rely on the explosion radius damage of the crossbow bolt by aiming at the feet of a particularly fast-moving opponent. He does, however, do a pretty good job of anticipating the location of a moving target when shooting a bolt.
Additional info/History:
Killer Kork was born as a project to make TGE 'do' something during a visit from my brother last August. It was originally envisioned as a battleground for pitting each our own ai code against the other, but considering my brother's Ph.D. in physics, as well as 10+ years professional coding experience, it quickly became a losing battle for my ai code (which was busy doing 'victory dances' and such on getting a frag... =). The result I'm posting is his code (with his blessing), developed over a two week period, without any previous torque-script experience. That said, it obviously isn't fully featured, optimized, etc, but hopefully a solid starting point for anyone wanting some ai (and can't wait for the 'real deal').
On a side note, re-implementing the original idea of two sets of combating ai code is easily achievable by simply modifying the AIManager class (and uncomment the execute line at top of AIPlayer) to include two different versions of ai code. We were also using a modified .mission file with a second path, where the other team could start somewhat unmolested.
This code comes AS-IS. It was based on TGE 1.3, but I've given it a quick test in 1.4, and everything seems to run fine. Instead of letting it gather dust on my HD, I hope someone finds it useful. If you do use it in a project, or change it in an interesting way, please send me a mail dgevans at hotmail dot com. It was a learning experience for me, and now hopefully others =)
I'd also like to thank Sam Bacsa for Codeweaver (then called TorqueScript Dev?), which we used extensively. It was a HUGE help!
/daniel
#62
@Nateck - You might want to check the variable $AIPlayer::TeamCount in aiPlayer.cs.
Nick
10/10/2007 (6:39 am)
@devans - Thanks a lot for the info!@Nateck - You might want to check the variable $AIPlayer::TeamCount in aiPlayer.cs.
Nick
#63
12/04/2007 (11:41 am)
How do I increase or decrease the number of bots per team. Also, how do I have one team follow one path and the other follow a separate path?
#64
12/06/2007 (5:58 am)
I can not get my AI units to fire at me. I made tha change that Brad posted and everything seems to be working fine except that.
#65
EDIT: Nevermind. For everybody wondering how he does it, look at the AIAlgo1::thinkFast function. When this is called, it looks for projectiles with a radius search. If a detected projectile has an initialVelocity variable set, it's a projectile in flight. If it doesn't have the variable set, it's in the middle of fading out and, in other words, is exploding. Very clever IMHO
12/26/2007 (7:23 am)
Devans, i've been staring at your code for a while, and i'm trying to figure out how you got ai players to detect explosions....How did you get him to do that?EDIT: Nevermind. For everybody wondering how he does it, look at the AIAlgo1::thinkFast function. When this is called, it looks for projectiles with a radius search. If a detected projectile has an initialVelocity variable set, it's a projectile in flight. If it doesn't have the variable set, it's in the middle of fading out and, in other words, is exploding. Very clever IMHO
#66
If that doesn't work, let's try changing the team
If all else fails, make him rogue and shoot at everybody
12/26/2007 (7:27 am)
@Michael: Maybe you're spawning a bot that is human friendly. I'm not sure if 'isHumanFriendly' is a variable of the AIPlayer or its corresponding algo, so enter both into the console[b]botId[/b].isHumanFriendly = false; [b]botId[/b].algo.isHumanFriendly = false;
If that doesn't work, let's try changing the team
[b]botId[/b].team++; [b]botId[/b].algo.team++;
If all else fails, make him rogue and shoot at everybody
[b]botId[/b].team = 0; [b]botId[/b].algo.team = 0;
#67
01/02/2008 (5:10 pm)
How do I make it so each bot has a score at the end of a game?
#68
Cheers - Cap
01/04/2008 (7:34 am)
I know I am late, but I want to seriously thank you and your brother for making a great resource. I am a programmer but It would have taken me much longer to do this without you and your brothers help. Thanks for elevating Torques Game :0) Cheers - Cap
#69
Thank you
01/08/2008 (6:03 pm)
Can someone tell me how to create less bots. At the moment there are 8 bots 4v4. I would like to have 4 bots 2v2.Thank you
#70
01/08/2008 (7:01 pm)
Look in aiPlayer.cs for this line "$AIPlayer::TeamCount". Its value is set to 4 by default meaning there will be 4 bots on each team (8 all together). setting this to 0.5 will make one team with just one bot.
#71
01/26/2008 (9:11 am)
Really great bot script! Everything works fine, and the bots are really good, but not too good, just perfect :D. This script works as advertised and really makes the game come alive. 5/5 stars!
#72
01/30/2008 (6:55 pm)
Which part of the code is the "basic flocking" implemented? I couldn't tell where are the three rules were applied.
#73
i can't seem to get it working with this resource;
Upon runnin the starter.fps executable it gives me an error torqueDemo has encountered a problem and needs to close. I'm working with TGE 1.5.2
Anyone can help me with this. The Tridanaut Solier Content Pack works fine with my default TGE 1.5.2.
:(
01/31/2008 (6:44 pm)
Anyone tried this resource with the Tridanaut Soldier Content Pack?i can't seem to get it working with this resource;
Upon runnin the starter.fps executable it gives me an error torqueDemo has encountered a problem and needs to close. I'm working with TGE 1.5.2
Anyone can help me with this. The Tridanaut Solier Content Pack works fine with my default TGE 1.5.2.
:(
#74
I'm having quite a bit of difficulty getting only one bot to spawn and attack me. I just wanted to work with one at a time until I get things like I want, then start adding others. RIght now 8 are spawning.
I have tried changing "$AIPlayer::TeamCount", and deleting the dso, but there is no change. I deleted all dso via the batch file and still no change. I know I'm a dummy, but could anyone point me in the right direction.
I have TGE build 1.52. I unzipped killer kork to the specified directory, and it worked like a charm. I just want to know if there's anything else I need to do? I only want one bot that is UNFRIENDLY to me. Sorry I'm so stupid =(.
03/22/2008 (6:28 pm)
First of all, THANKS for this...i'm a total dumb%%% and I'd still be banging my head on my desk just hoping to get a simple AI.I'm having quite a bit of difficulty getting only one bot to spawn and attack me. I just wanted to work with one at a time until I get things like I want, then start adding others. RIght now 8 are spawning.
I have tried changing "$AIPlayer::TeamCount", and deleting the dso, but there is no change. I deleted all dso via the batch file and still no change. I know I'm a dummy, but could anyone point me in the right direction.
I have TGE build 1.52. I unzipped killer kork to the specified directory, and it worked like a charm. I just want to know if there's anything else I need to do? I only want one bot that is UNFRIENDLY to me. Sorry I'm so stupid =(.
#75
03/29/2008 (9:31 am)
Thanks you very very very much!!! This is outstanding! Excellent work!
#76
EDIT: Figured it out:
I added these lines to the datablock PlayerData(DemoPlayer : PlayerBody)
maxForwardSpeed = 10;
maxBackwardSpeed = 10;
maxSideSpeed = 10;
Like this:
//-----------------------------------------------------------------------------
// DemoPlayer datablock used by AIPlayer
//-----------------------------------------------------------------------------
datablock PlayerData(DemoPlayer : PlayerBody)
{
shapeFile = "~/data/shapes/player/iranSoldier.dts";
shootingDelay = 2000; // just a place holder
maxForwardSpeed = 10;
maxBackwardSpeed = 10;
maxSideSpeed = 10;
};
05/14/2008 (6:18 pm)
Hey I'm trying to slow him down, he strafes way too fast and is a bit too hard to shoot for novice players, how would I slow him to make him easier to shoot? ThanksEDIT: Figured it out:
I added these lines to the datablock PlayerData(DemoPlayer : PlayerBody)
maxForwardSpeed = 10;
maxBackwardSpeed = 10;
maxSideSpeed = 10;
Like this:
//-----------------------------------------------------------------------------
// DemoPlayer datablock used by AIPlayer
//-----------------------------------------------------------------------------
datablock PlayerData(DemoPlayer : PlayerBody)
{
shapeFile = "~/data/shapes/player/iranSoldier.dts";
shootingDelay = 2000; // just a place holder
maxForwardSpeed = 10;
maxBackwardSpeed = 10;
maxSideSpeed = 10;
};
#77
I would like to only have one spawn at every path marker, and then to have them not respawn after they die
05/14/2008 (8:58 pm)
Hey how do I stop it from respawning after they die?I would like to only have one spawn at every path marker, and then to have them not respawn after they die
#78
Please help, let's start this discussion. Thanks!
05/28/2008 (4:49 pm)
Hey how would I set triggers around the level to spawn killer kork bots as each trigger is hit. The reason I need this is because as is it is killing performance way too much.Please help, let's start this discussion. Thanks!
#79
07/03/2008 (10:58 pm)
I just droped this in TGEA 1.7 stronghold and hell of a lot of fun with it. I like!
#80
function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
When a human hits a human, the sourceObject.client.name will contain the name of the human, however when you get hit by an AI the .client object isn't defined, and neither are any of the AI objects such as aiplayer or the method that AIPlayer uses to get the bot's name, getShapeName().
11/12/2008 (6:14 pm)
How do I get information about the AI player's name when a human player has been damaged by the AI? In player.cs, there is the Armor::damage function which gets called when you get hit:function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
When a human hits a human, the sourceObject.client.name will contain the name of the human, however when you get hit by an AI the .client object isn't defined, and neither are any of the AI objects such as aiplayer or the method that AIPlayer uses to get the bot's name, getShapeName().

Torque Owner Nathan Kent