Sign in to follow this  

Advice on spaceship combat AI (3D)

Recommended Posts

Hey guys,


I'm currently trying to work out and plan the AI im going to use in my space combat game (3D) but im kinda stuck on a couple of things and was wondering if I could have some help with ideas. Been looking at games like X3 for inspiration. 


Currently for the movement of the AI I am using steering behaviours (either directly or I will use them to compute the velocity I need and then turn the ship to face that direction). I will be using a state machine though I might try and use a behaviour tree instead later for its advantages.



The logic for my fighter AI currently is as follows


  • in the state machine I will have 3 main states. search for target, attack/chase and flee
  • find an enemy ship and set that as a target (will have to make sure not all ships target one enemy but should be easy enough) and move to its location
  • once it gets close enough match its speed
  • fire if the angle between its forward vector and the enemy's ship is low enough 
  • start to feel if the my health is to low
  • scan for the closest bullet and see if it is heading in the ships direction, if it is use the evade steering behaviour to try and avoid it (this is passive behaviour that will happen in every state)
  • another passive behaviour will be obstacle avoidance (avoiding ships and asteroids)

So for this I realised I will have some problems 


  1. The first being that if two ships target each other, they are going to be seeking each other which will end badly 
  2. When i make a ship flee (if i still want to after thinking it all), how would i make it harder to hit. I could give it a random force to push it side to side but using evade or flee from its enemy alone wont work as fare as i know (flee will just move away from it and since the other ship is just heading straight towards the fleeing ship, evade will do the same as flee).

in future I would like to look into fuzzy logic and utility systems with behaviour trees to create a much more intelligent AI but for now I'm just trying to get something that works to an ok extend (game still needs to be fun for the player).


Any ideas or help with understanding concepts/the solution to my problems would be very appreciated. Also open to new ideas of how to do the movement :)

Edited by lexington

Share this post

Link to post
Share on other sites

Based on my experience writing large swaths of combat AI in X2 and X3, and my broader experience building the foundational technology that went into X:Rebirth, I'd make a single, pointed recommendation: don't do it with autonomous agent-based AI.


My ideal solution would be to have a "combat director" which creates little "bubbles" of action whenever an engagement begins. Within the bubble, all movement, maneuvering, formation flying, and even weapons firing would be controlled by the director.


Space combat is not fun when it is realistic. I spent a few years trying to make realistic combat entertaining, and failed. There will either be a ton of edge cases that are easy to exploit (X2, X3) or it'll be way too easy (or hard) depending on the player's skill level.


Good space combat games should feel cinematic and epic. It's far easier to get a cool-looking chase/evade sequence by top-level control over the space, than it is to get that to happen emergently from autonomous agents.


What your ships do out of combat is up to you; I'd recommend a behavior tree or utility-based reasoner. But in combat: definitely centralize control and you get a huge toolbox full of toys like "make this guy explode in front of the player so he sees it" and "wait to disintegrate this formation of ships until the player is watching." Even if you don't base anything on player attention, getting a coordinated, cinematic fight is so much easier when you don't have to hope all the AIs decide to do something cool at the same time.

Share this post

Link to post
Share on other sites

@ApochPiQ That answer a lot of my questions about how it is done in games Thanks :) 


Just some questions about the combat director. what would be the best way to go about it, I'm guessing I will still need some intelligence when it comes to making sure the ai does not just crash into objects.  Am I just telling the AI ship what to do in curtain situations or is there more to it?

Share this post

Link to post
Share on other sites
So your basic setup looks pretty similar to the autonomous flavor. You have a steering system, a collision avoidance system which feeds into steering, and a handful of basic modules for maneuvers and such that also feed into steering.

The difference is that instead of each ship deciding for itself what maneuver to make, or when to shoot, or whatnot, you centralize that logic into the director.

Basically think of it as a single AI orchestrating the entire combat process. Some ships will still behave as if they were autonomous and "alone", but in reality under the hood, everyone is placed into choreographed movement patterns.

I guess the best illustration is with an example. Suppose you have a formation of 5 fighters going up against one corvette. Instead of each ship in the formation trying to independently remain in formation, decide to attack in synchrony, and so on, you have the director steer the formation overall, make strafing runs against the corvette, etc. Meanwhile the corvette will maneuver to get as many guns facing the fighters as it can. All this is handled by a single chunk of code instead of distributed across six total ships worth of AI.

The results are dramatic if done correctly. Instead of having ships occasionally make dumb decisions like evasive maneuvers that break formation and cause them to hit a friendly ship, you can coordinate the evasion so that all the ships evade even though only one was taking fire, and so on. The results look markedly more natural and cooperative.

You're really writing the same AI, just placing it in charge of multiple ships at once instead of just a single craft per AI instance, if that helps.

Share this post

Link to post
Share on other sites

@ApochPiQ so for my game where i will have 10 fighters vs 10 fighters, when one needs to go into evading, they will all take an evasive stance (due to the manager) but they will evade what is local to them. from this I would just use steering behaviours to achieve what I need in terms of the movement (correct me if im wrong). 


If that is correct that I believe I understand the concept but just need to help with a couple more things 




and a handful of basic modules for maneuvers


Will these be pre planned in code and then I just give this as a desired seek (if say it was based on moving up and around) and im guessing all fighters will do this as it will be acted by the manager


2. What would be the best way of working out what behaviour to do (some like dodging will be easy but others like when to do a manoeuvres i'm guessing could be based on a selector with some logic),(would i have a state machine or behaviour tree setup for the manager to makes decisions)?


3. With firing at enemy ships, would I have that as an add on logic for the director with the simple code of "if angle between me and ship is between 10 or -10 fire" or would i just say fire to all ships as its only behaviour at the time (if you have a shot ofcs)


4. My final questions kinda links to 2 (and im guessing the answers will be based on the workings out of the logic for my ships) but say both teams have the behaviour to chase and the fire down the enemy, would I not run into the same problem. From this would I have triggers (just like in a state machine when states transition)  to say if your going to collide, everyone dodge, then do something different. In other words will the 2 managers have to communicate what they are doing or will it just be based on internal logic of the manager and its ships. 


Again thanks for the help 

Edited by lexington

Share this post

Link to post
Share on other sites
So the manager exists to make sure that ships make decisions that are coordinated and make sense in the context of everyone around them.

It is not a replacement for individualized behavior or even per-ship-type behavior. Quite the contrary, the idea is to ensure that if a ship does something special, like evade a missile, it does so in a way that looks cohesive with the rest of the combat scenario.

I would set it up so that the manager has a bidding system, probably blackboard based, where individual ships can "ask" to do various things. So one ship might ask to evade an incoming missile. That might be a very high priority action, so the "bid" is high. The manager then has every ship that is in formation with the "special case" ship do a formation-based evasive maneuver, instead of each ship individually deciding what to do.

So for example the corvette could bid to "be attacked and try to repel boarders." If this bid wins, the manager snags an eligible wing of fighters and a personnel carrier, and has them perform a strafing run and then an attempted boarding.

Or maybe the bid that wins is for the fighters to lose a ship to hostile fire from the corvette, because one is low on health/shields/etc. and nothing else interesting is going on.

Another example would be a "furball" dogfight between fighter groups, where each fighter bids on a target and then spends a short period of time chasing that target and firing when it has a clear shot.

You can select to play these mini-scenarios in any way you want, although a simple utility-based model is probably most effective.

The key is that there is only one "AI" device per combat situation. It controls all "sides" of the battle and coordinates both offensive and defensive maneuvers and attacks. This allows you to control the pace of the battle over time, control who is "winning" at a given moment, and so on. Think of it like a movie script for a combat scene more than anything else. You're directing actors to do things with an eye for the holistic result more than the individual details.

Share this post

Link to post
Share on other sites

@ApochPiQ arr so there is one manager for both teams which decides what to do based on what each ship is saying.


So for the dog fight as you said, each ship from one team could bid on attacking a target and if they win then the whole formation would attack the ship for a time, which the ships being attack would either just wonder and take the fire or maybe do a dodge maneuver  so in terms of behaviours this would be "fire on targets that try and dodge" or "fire on targets that dont try to dodge" (feel free to comment if this is still wrong). 


So really your manager is almost telling a story or conducting the battle (as you have mentioned before). From this the manager tells the ships what they can/should do, then the individual ai of the ship takes care of the doing. 

Edited by lexington

Share this post

Link to post
Share on other sites

The manager is not just issuing orders to individual AI. The whole idea is that a ship in combat is part of a larger scene, and should be treated as such. I wouldn't bother even writing code for controlling individual ships (at least as far as good combat goes) - almost every kind of movement or action in combat will be an interaction between at least two moving parts, so maintaining the relationship between those elements is crucial.


Of course you can have a steering component on the ship object and issue it commands under the hood, but conceptually there is no "AI" - you're basically just playing animations that happen to include timed triggers of actual weapons fire.



Say you choose to play the "fighter attack run on corvette" vignette. It should recruit a number of fighters, ideally fighters which are already associated in a formation of some kind, and obviously a corvette. Once this scene starts to play out, the maneuvers of both the fighters and the corvette are basically on rails. This doesn't mean that the exact distances/positions/etc. are predetermined necessarily - but the overall [i]shape[/i] of the flight movement should be fairly canned. For example, you might approach the corvette from aft to avoid a heavy broadside from the guns, then strafe along the hull towards the front and then peel off. A more daring approach from the side might be a different vignette that the AI can choose, for instance if a gun is disabled on the corvette leaving a blind spot.


The real trick is coming up with a library of these scenes so you can have appropriate-looking options for (almost) any actual arrangement of ships. They will tend to range from very specific situations to more general "fallback" options. Try to play the most specific one you can - which in a sense becomes an exercise in pattern recognition where you have to analyze the battlefield a bit and figure out what options are best.


As you noticed there are many permutations possible - such as "allow evasion" or whatnot. If you can plan out what kinds of scenes you want to create from day one, you can easily break them down into constituent parts and make arbitrarily composable scenes out of those bits.

Share this post

Link to post
Share on other sites

OK I think i have a good understanding.


For picking the scenarios im guessing i will need to have some logic or picking them depending on the conditions in the battle (and any favor i have for one action). Would this be what a utility systems would be good for picking 

Share this post

Link to post
Share on other sites

Yep, utility is a great way to do this, although many other techniques can be applicable as well. The hardest part is turning the input data (ship positions, orientations, velocities, health, etc.) into something you can recognize patterns in.


From a technical sophistication standpoint, you can do this with a lot of different methods, but the simplest and easiest is to just write a bunch of rules down. If the ships match this one situation, fire this AI scene in response; and so on. Evaluate the rules in priority order (i.e. write the if() statements in order of highest-priority to lowest) and you're done.


At the top end of the spectrum you could do it with all kinds of pattern recognition algorithms, but that's probably overkill unless you want to spend a lot of time building the AI.


Utility is kind of a sweet spot of "easy to calculate individual pieces" and "easy to combine those pieces of data into meaningful pattern detection."

Share this post

Link to post
Share on other sites

Ok so just to sum up my understanding of it all


I have a manager that know every ship in the scene (including the player but ofcs it wont issue commands to the player). The manager also has info to all aspects of the ship (e.g. health, velocity, direction vector/orientation, do i still have missiles)


I will have a list of actions that i want to launch which i could either pick based on scoring each of them for the current situation or just by a list of ifs based on the situation(as you mentioned)


Some of these actions might be do an attack run on a capital ship, get close to any enemy fighter you wanted to target and fire some rounds (could come from behind or from the top etc) or do a dodge maneuver/ dodge incoming fire (life take a hard left).


One might be where one ship wants to dog fight another when the manager rules might say (really basic)

  • if 2 ships on other teams are close to each other 
  • chose one to attack the other
  • fire while the other ship takes the fire
  • do allow collision avoidance/separation


These behaviors can have allowances like allow collision avoidance and separation. I could also instead have a bidding system where ships bid curtain actions.


The only logic the ships themselves would do is steering behaviors and rotation. If i was to have bids this would be done too.


The manager can use all ships on the scene to do actions that it wants to do. in every tick the manager should make sure that either a ship has been used for an action or is currently in progress of doing an action.


With all this I can have the manager play different scenes for what is happening in the battle based on the rules and actions I have.


This fixes my original 2 problems as no 2 ships will try and chase each other and end up hitting each other as the managers actions i specify would never allow this or the other problem where one ship will just feel and take hits easy hits as i could desire for that ship to take hits (either because it is doing something else or i have told it to) or i can give it an option to do an maneuver so it dodges (i will have to work out some logic to see which one i want to do). 


Anything i have missed?


Thank you very much for the time and help ApochPiQ. 

Share this post

Link to post
Share on other sites

Hey ApochPiQ, after fixing some problems with the player movement, I finally have time to work on the manager and if you don't mind i just have a couple more questions about some areas.


I have decided my manager is going to be based on scoring different set actions that ships can do (looping through every ship). For example I will have the following for fighters


  • Target an enemy ship and hit (targeting will be done on another level of scoring)
  • Target an enemy ship and the enemy evades (i will have a list of manoeuvres which will be a projection of seek points relative to the ships position)
  • Target an enemy bomber and hit (targeting will be done on another level of scoring)
  • Target an enemy bomber and the enemy evades
  • Target an enemy capital ship and hit
  • Target an enemy capital ship and die
  • Fire missile on enemy ship (targeting will be done on another level of scoring)
  • do a two pronged attack on an enemy ship

so for most of these I can score by the following


  • distance from each target
  • dot product/angle between my forward vector and distance between vector
  • health of capital ship/number of turrents it has left
  • number of missiles
  • is a friendly unit near me
  • my current HP

Each ship will have a boolean to state if it is free, each tick, the manager will go around and make sure every ship is doing something and set that bool to false (im doing something)


Each behaviour has a set time that it happens for. 


So i only have two more questions


  1. what is the best way to score if we should hit of missing the target, im guessing i could base it how the fight is going to give advantages but i would prefer another determining factor.
  2. say one ship is already attacking or fleeing/dodging another (flee will just be done when the target will get hit and a manoeuvres is done for dodging) is it a problem at all to target that ship (with this I would say that the behaviour that would win is Target an enemy ship and hit as its not trying to dodge but since the other ship is still doing something it wont matter to it)
  3. or (in relation to 2) If a ship is free and it targets an enemy ship that is already doing something should I make it so it resets its behaviour to the new one 

Ofcourse 2 and 3 will not be a concern when targeting the player :)


Anyone else feel free to chip in if you want to 


Cheers for the help in advance

Share this post

Link to post
Share on other sites
1. You can easily just toss a coin, or do a weighted random to bias towards one team, or whatever; it doesn't have to be complex. A simple but powerful trick for this is to use a response curve - a function that goes from (0,0) to (1,1). You feed in an X value of time, and the (Y value - 0.5) gives you a bias towards one team or another. Positive is one team winning, negative is the other team. This can be used to get "tension" or "near escapes" or whatever just by tuning the function.

2/3. If a ship is already busy, there are two rules you obey: (1) it cannot be placed into a maneuver unless the score of the maneuver is higher than the score of whatever it is currently doing. (2) If you don't need to control the ship's position, such as for a "fighter scores a hit on enemy fighter" event, then the ship can be in multiple "events" at once.

Hope that's helpful!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this