Game Development Community

Tactical AI Kit: Squads

by Bryce · 07/24/2008 (11:48 am) · 25 comments

Hello GarageGames,
I can easily say this is the biggest accomplishment with this project I've ever made in just one week. Yes--I have squad support working. None of this is scripted (by not scripted I mean the AIPlayers don't just advance when the player enters a trigger, like in Call of Duty). Read on.

How does it work?

At the moment, I've made a scriptObject called Squad1. This script object is added every time the developer adds an AI squad.
function Squad1::Create(%leader,%m1,%m2,%m3) // This squad consists of a leader and three other members
{
  %squad = new ScriptObject(Squad1) {
    member[0] = %leader;
    member[1] = %m1;
    member[2] = %m2;
    member[3] = %m3;
    numMembers = 4; // Four-person squad
    
    objectiveType = "None"; // ObjectiveTypes, for now, are Move, Contact, and None.
    objectiveData = 0; // Depending on the ObjectiveType, this can be either a position or object id.
    
    moveLocation = ""; // A place we should move to that isn't part of the objective. Move towards a sound, onto a foot switch, whatever.
    
    currentOrder = "Regroup"; // Can be Regroup (follow), Hold, Move, and Objective (fulfill the objective)
  };
  %squad.onCreate();
  echo("Squad1::Create--" @ %squad);
    return %squad;
}

Squad1 can hold a maximum of 4 members (There's no hard-coded limit--just add more member variables and increase numMembers to your liking. Make 'em storm the beaches of Normandy!). Member[0], the first member, is the squad leader, which can be both a human or AI player. The squad leader makes all of the decisions, and the other three members follow those orders (orders show up on a human player's screen using CenterPrint).

The ObjectiveType and ObjectiveData fields define the objective for the squad leader. By setting ObjectiveType to Move and ObjectiveData to the position of, say, the center of the enemy base which they need to capture, the squad leader will decide if he should pursue the objective. If so, he will set currentOrder to "Objective". CurrentOrder is the variable that the other three AI Players read--Once they see a new order, they will execute it. If the squad leader is a human player, there is a key command to set the order to objective. Each member regroups with the squad leader, and the calculates a path to the objective.

So, you can start off a squad of ai players moving towards an objective by typing this into the console:
$n = Squad1::create(spawnai(1),spawnai(1),localclientconnection.player,spawnai(1)); $n.objectiveType = "Move"; $n.objectiveData = ObjectiveMarker.position;
This will create three AI players and link the human player to the squad. Once all of the players are within 10 meters of the squad leader (regrouped), they will move to the objective.

But what if they come under fire?

If the squad members know where the enemy is that is firing, they will fire at full rate (suppressing fire) at the enemy to either kill him or force him behind cover so the squad can get to cover as well. If the enemy is close enough and there are no others around, the squad leader will have the next living member under him go and flank (sneak up on) the enemy. When the enemy is dead, the squad regroups and proceeds toward the objective.

How do they avoid ambush?

A real-life squad is asking to be cut down by enemy fire if they move towards the objective completely out in the open. So, last night I thought up a cover-to-cover movement system. Using my awesome Paint skills, I'll demonstrate.

img385.imageshack.us/img385/5218/imgazl4.jpgimg92.imageshack.us/img92/2726/imgbhf5.jpgimg175.imageshack.us/img175/5401/imgcsk6.jpg
Image A--The movement area. The green X is where the squad starts, the red X is the objective. The blue line is the path, and the black squares are randomly scattered crates, which are used for cover.

Image B--Every 20 meters a squad member travels, a point is calculated 50 meters forward in the direction they're moving. This is the guess of where an enemy engagement could possibly occur. The arrows show the location of each check. Each squad member will move to cover from that forward position, if there is cover. The cover locations chosen are the tiny purple dots.

Image C--After moving to cover and seeing that there are no enemies in that area, each member will recalculate the path to the objective, and keep performing the cover search again every 20 meters. Image C shows the resulting path.

I implemented this system this morning, works without a problem. It's getting hard to kill these guys, I've had to triple my player datablock's maxDamage field just to stay alive. That doesn't even help me much...
Screenshots:

img440.imageshack.us/img440/5328/screenshot00100001nu3.png1. The squad leader ("V") and the two other members ("O") waiting for you to regroup with them so they can go.
img242.imageshack.us/img242/3621/screenshot00100002ls5.png2. Once regrouped, we move to the objective.
img176.imageshack.us/img176/3599/screenshot00100003rk7.png3. ....and move....
img99.imageshack.us/img99/9642/screenshot00100004de1.png4. We've arrived at the objective, so we need to regroup.
img182.imageshack.us/img182/5842/screenshot00100005ti3.png5. An enemy squad of three regrouping to storm your base.
img363.imageshack.us/img363/5430/screenshot00100007ol5.png6. The squad advancing over the hill.
img370.imageshack.us/img370/5385/screenshot00100008qu1.png7. Coming under fire. If the player fires back, the squad will move to and appropriately use cover. If the player hides again, the squad will continue advancing, but will always watch the position where the player was sighted last.
img170.imageshack.us/img170/5824/screenshot00100013mr2.png8. Here you can see the enemy squad leader crouching to reload as he runs to his barrels for cover. The other two members are already there, waiting for the player to expose himself (they started firing at me right after I took the screenshot; you can see a little yellow streak of a tracer bullet coming out of the left guy). You can see the reason I only added a 3-man squad instead of a 4-man squad. I was just too lazy to add another cover object down there. If you're wondering, an AI Player that cannot find any available cover will just crouch down and fire at the enemy until something's available--He will still be able to flank if the squad leader needs him to.

A video:
www.youtube.com/watch?v=G9Cm8qLL39s

Rate and comment! I'm open to questions.
Page «Previous 1 2
#1
07/24/2008 (11:59 am)
nice work!
#2
07/24/2008 (12:03 pm)
Very cool and great work!
#3
07/24/2008 (12:03 pm)
two comments in six minutes....wow
#4
07/24/2008 (12:07 pm)
Let me second that one; nice work :)
#5
07/24/2008 (12:20 pm)
Your AI is coming along very well, bryce! Would love to see a video demonstrating the movement system.
#6
07/24/2008 (1:04 pm)
You should package this code up as a full resource, or even better as a pack. AI packs are some of the most requested and nonfulfilled areas of the store.
#7
07/24/2008 (1:33 pm)
@Morrock: Fixing a bug with the movement system, I'll hopefully have a video up tomorrow.
@Edward: My idea was to sell the code on the product page if GarageGames would accept it. As for an AI pack, what would people need in it? RTS and animal are the first things that come to mind...
#8
07/24/2008 (1:36 pm)
@Bryce, good job man. I remember when you first started doing this, and there were many people naysaying the idea of an AI kit in script (which I do understand, you probably want to eventually put the resource heavy bits into C++ and invoke them from script, but still). Very cool, and good on you for sticking to it.
#9
07/24/2008 (1:39 pm)
well yah, those are good options. A FPS gears of war style Ai might be nice. There is alot of types of AI, but since there isnt any currently other then the vanilla Aiplayer that GG ships with the engine and the other 2 Astar and single entinity based ai. So far no group AI though.
#10
07/24/2008 (1:41 pm)
@Ross: I'm finally getting a hold of C++, I'll be moving visibility and cover searching to the source code soon. As for my A* solution, it wasn't really a solution because it was covered with bugs. I implemented Dan Keller's A* resource, which is excellent, by the way.
@Edward: I'll give it a shot once this project is done...What ever happened to Stefan Beffy Moises' AI Pack? Did that ever get finished?
#11
07/24/2008 (1:44 pm)
@Bryce, awesome :) Have you checked out James Ford's post/PDF on AI in BeTheDinosaur? It has some info on what he did with Gavin Bunney's A* resource that might help you out too.
#12
07/24/2008 (2:28 pm)
@ross: You may want to check how you wrote that link--when I mouse over it my browser shows both the URL to this blog post and then the URL to what you really want me to see in quotation marks
#13
07/24/2008 (2:37 pm)
@Bryce, that's odd. It seems that the code is appending "http://www.garagegames.com" to the beginning of the string, and the rest of the URL is there, despite that in the edit of my post it appears normally between the [url] tags.

Edit: Ah, the problem was that I was quoting the URL in [url=], which GG's version of "BBCode" apparently doesn't like. Many other forums and the like do use quotes. Anyway, fixed now.
#14
07/24/2008 (3:41 pm)
Wow! Just like Operation FlashPoint!
#15
07/24/2008 (4:03 pm)
Wow this looks very cool, nice work!

AI in TGE is something that has been needed for ages.
#16
07/24/2008 (5:44 pm)
I agree Mr. Smith, I have been a advocate for some form of ai that didnt require a programming degree to understand. DOnt get me wrong the other stuff is really nice, but its pretty much useless as a non programmer. This will open up alot of modern game options that you see. Gears of Torque anyone?
#17
07/24/2008 (7:14 pm)
@Steve: I liked your Dubious demo with the squad movement, it was actually what inspired me to start working on squad support. Good luck with that project!
@Both Edwards: Thanks for all the support, this is what keeps me coding ;-). Edward Smith, I also agree...I started working with TGE when I was 10 back in 2004 and was extremely disappointed with how little AI was already implemented in Torque. I think that if a TGE 1.6 ever gets released, it should at least contains something like AIGuard to provide novice programmers a good foundation from which to work from. I actually learned Torque Script by reading through and trying to understand the Killer Kork scripts!
#18
07/24/2008 (7:39 pm)
Well then, excellent work and keep it up! Looking nice!
#19
07/24/2008 (7:52 pm)
I've been silently following your progress and it seems you have some cool stuff there. Keep up the great work !
Page «Previous 1 2