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

Page «Previous 1 2 3 4 5 Last »
#1
07/03/2006 (12:07 pm)
Absolutely Sick! Outstanding Job! Adding this resource right now.
#2
07/03/2006 (12:14 pm)
Outstanding job.. I will check this one later... :D
#3
07/03/2006 (7:04 pm)
This looks great. One question though, i noticed his primary function is to kill people, but are they configurable to, for example capture flags as well as kill people?

Or to use vehicle turrets e.t.c ?
#4
07/04/2006 (3:27 am)
Got it working, getting a bit of a hit on performance though but thats probably down to runnning too much at the same time :p.
Thanks to you and your brother creating for the resource.
#5
07/04/2006 (6:18 am)
This is too cool man. Ive been working with aiGaurd and aiPatrol for a couple of weeks.

Thanks
#6
07/05/2006 (6:59 am)
Very nice AI, they fragged me a few times, but after 3 minutes of game play i suffered great performance loss. I had no other programs open at this time.
#7
07/05/2006 (7:07 am)
Ah, I turned down the people on each team too two people each side, much better.
#8
07/05/2006 (9:24 pm)
I don't understand how to set them up in-game. How do I decide where they spawn? How do I spawn them in the first place?
#9
07/05/2006 (11:43 pm)
Oh, it may seem obvious, but you missed the exec in game.cs for:

..\server\scripts\aiSensObj.cs
..\server\scripts\aiAlgo1.cs

Simple to most Torquers but to people just beginning, this may mess them up.
#10
07/06/2006 (12:57 pm)
Actually I did exec the scripts. I just don't understand what it's supposed to do. I want to be able to decide where the bots spawn, but they don't seem to want to spawn at all.
#11
07/06/2006 (5:24 pm)
@Plague
By default (atleast in my setup) you need to launch a single player mission for the bots to spawn.
#12
07/06/2006 (8:17 pm)
What is the Con::hardOn() function for, if you don't mind my asking?

edit: Maybe if you were to use MissionCleanup.add() for the objects that the author has commented about .delete() not working. He was trying to do it (edit: memory cleanup) but didn't know how it seems.

edit again: aiPlayer.cs line 234
#13
07/07/2006 (12:26 pm)
Looking through the scripts, it looks like you need to do the following:

Create a path in your mission for the AI to spawn on. E.G., "MissionGroup/Paths/Path1"

Then call the following once:
AIManager::think();
to get the AI to start spawning.

'aiSensObj.cs', 'aiAlgo1.cs' are automatically exec'd if 'aiPlayer.cs' is exec'd
#14
07/08/2006 (12:40 am)
In 'function AIPlayer::isObjAud(%this, %obj, %sensObj)", the reference to the sensing objects minimum velocity has a typo:

$AIPlayer::AudPlayerVel
should probably be:
$AIPlayer::AudVelPlayer
#15
07/08/2006 (11:40 am)
Most excellent. I added this to the starter.fps demo (just copied the files to the appropriate folder, I didn't need to add any further exec commands to game.cs) and it just worked. Very nice. Please thank your brother for his contribution!
#16
07/09/2006 (11:16 am)
Hi, and thanks for all the comments. I posted this almost three months ago, and rather decided it wasn't going to make it public. But here it is!

@Ian - sure, it's configurable to do whatever you write it to do! =) At the moment, though, the ai is pretty 'limited' to what I wrote in the description. Take a look at the aiAlgo1.cs, this is where the 'thinking states' are. That would be a good place to add your own objectives.

@Plague - As Jeff pointed out, they spawn on Path1 (which is a default path in starter.fps). If you want to change the spawn point, change the marker location of Path1. Look at lines 995,1000,1008.1015 of aiPlayer.cs, and you'll see where the two teams spawn. As stated, you can also create new paths, and have each team spawn on different paths. While in 'wander' mode, they will also not stray far from the path markers, so put them where you want the bots to roam.

@Jeff - Thanks for the correction =)

@Midhir - The Con::hardOn() function gives all non-ai players a health regen (set to val 1000), making them a little 'harder' for the ai bots to kill... ;)

@Matt - As Rob commented, as long as you're running the stock setup, this should work without having to exec it anywhere else. If I remember correctly, game.cs calles aiPlayer.cs, and the modified version of aiPlayer.cs in the zip calles aiAlgo1.cs & aiSensObj.cs, as well as starting the think cycles.

Hope this clarifies things a bit! Happy torque'ing

daniel
#17
07/10/2006 (1:26 am)
Ah, i see, thanks Devans. Noob question: How do you change the names of the players? Is there a way to change the person model of the one of teams?
#18
07/26/2006 (2:29 pm)
I implemented this and every time I quit out of my mission and restart my mission, it now crashes. But runs fine the first time.
#19
07/28/2006 (1:09 pm)
That above bug was unrelated to this resource.. Never mind.. what are the distance variables set as far as units go? 1 meter ?
#20
08/03/2006 (3:06 pm)
Nevermind.
Page «Previous 1 2 3 4 5 Last »