Game Development Community

dev|Pro Game Development Curriculum

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

About the author

Recent Blogs

#41
05/20/2007 (2:40 pm)
Hi, and glad to see some people have been toying with this! =)

@David - Sorry, don't have AFX, so can't help you there

@Joseph - Are you using something other than the default orc model?
If you look up at the AIPlayer::spawn function (~line 146), it shows what it's using as the player model

@Bryce - Fun! Have to smile, though. Some of your mods sounds like the type of ai code I was spending my time with, while my brother's "whackos" were running around blowing my team to bits... =)

@Jason - the %index switch created one "Evil" bot for each team (Brain & Algo). Evil Id must be yours. If there was already an Evil bot running around for that team, a default bot was spawned, called Brain#, or Algo#. The "evil" bots could be set independantly to be human-unfriendly.

Without testing, i think you could just rip out the whole switch statement, and leave the part in the "else" brackets. That would give you one team of bots. If you just removed the cases, you'd get two teams of default bots.

The number after %path is which pointer on the path they should spawn. -1 should be a random point. The path is defined in the mission file, and in the default fps demo there was a path1 with 5-6 points on it. You can create multiple paths, and start the bots on whichever path/point you want.

Alle ist klar? Happy coding!

daniel
#42
05/27/2007 (2:18 pm)
How would I spawn Korkie via World Editor?
#43
05/29/2007 (12:56 am)
has anyone been able to get this to work with the latest version of torque game engine 1.5.2 .... ?????
#44
06/11/2007 (2:08 am)
Yup, copy/paste the files and the whole thing works fine in 1.5.2.

Right now I'm working on making one team a berzerker offense and the other side defensive.
#45
06/15/2007 (12:46 pm)
Can anyone tell me how to get this to work with the Implement Teams and the Player Classes resource.

Pretty much my game has two teams, each with five classes and i dont know how to tell the bots how to pick a team/class.

Implement Teams Resource;
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2312

Player Classes Resource;
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=11733

If there are any better AI resources to go with these, and some capture the flag and king of the hill type games please let me know.
#46
06/27/2007 (1:44 am)
@GAMEHACK3R: I got it working flawlessly. What problem are you having?
#47
07/08/2007 (7:33 pm)
works good but none of the animations are working. Everything moving but the bots(korks) have no animation.

Is this a known issue?
or is there a way to fix it
#48
07/10/2007 (11:24 am)
How about the player, Jermaine? Is he animating?
#49
07/11/2007 (8:40 pm)
Nope, he is just sliding.
#50
07/16/2007 (11:21 am)
Is there any way to somehow get rid of one of the teams?
#51
07/18/2007 (8:00 am)
how do you make points for kills of your AI players? Then game cycle?
#52
07/21/2007 (12:15 pm)
How do I slow down the spawning? There spawning to much!
#53
07/31/2007 (12:29 am)
Cool resource but take a lot of memory (and with many bots it's unplayable).
Tested on 1.5 version.
#54
08/03/2007 (8:40 am)
ok now i have this problem

==>AIManager::think();
fps/server/scripts/aiPlayer.cs (0): Unable to find object: '' attempting to call function 'schedule'
Mapping string: Evil Brain to index: 14

my bot still does not spawn.
#55
08/18/2007 (4:18 pm)
This is my first day with the Torque engine, and I just gotta say, Devans, thanks ever so much for this (and thanks to your brother too). Nice work, nice job. Learning a tremendous amount from the functions in your script, and it's added a bit of immediate spice to my learning curve.

Using the 1.5.2 build, copied and pasted the unzipped files into the server folder, and it worked without a hitch.

Don't know if this'll be of any use to people who are having trouble getting their bots to initially spawn, but if you've previously run the starter.fps, then in the server/scripts folder, there'll already be an existing aiplayer.cs.dso file. You might want to try removing that from the folder, and rerunning starter.fps. Another aiplayer.cs.dso file will be generated, bots will appear, mayhem will ensue, your frame-rate will be utterly destroyed, but it will be worth it.

Really great resource, fantastic work - many many thanks again.
#56
08/22/2007 (11:43 am)
This is ana excellent program. Thank you so much for sharing it with us.

The script has given me great insight into what we can achieve with the script. In the meantime,what I did with it was put one team on path1, the other on path2 and then I have two guard type areas for my players to run around and try to destroy.

Those AIs have great aim.

Many many thanks to you and your brother.
#57
08/31/2007 (7:20 am)
Has any one had this code working with legends?
Also I added it to Realmwars, after reading the forum post here, no luck...
#58
09/15/2007 (3:49 pm)
This is one of the best resources I've ever seen. I'm willing to pay if anyone is interested in modifying this so that the AI bots try to come close in order to hit the player instead of trying to shoot at the player from a distance. You can find my email in my profile. Thanks.
#59
09/28/2007 (8:32 am)
Don't need to pay anyone, Nick. Just edit aiAlgo1.cs. The first section is a bunch of params you can configure, these are the one's you're looking for (line 30 & 31):

$AIAlgo1::AttackSnipeDist = 50; // target is snipable if outside this radius
$AIAlgo1::AttackSnipeDot = -0.3; // target is snipable if looking away

Hope that helps.
#60
09/29/2007 (1:12 am)
I looked through the scripts for awhile and i couldn't find how to make them move slower, have a certain amount of health, or make them stay in one place unless they found a target or looks for health/ammo. Would anyone know how to change any of them???
Thanks