Advice on spaceship combat AI (3D)

Started by
13 comments, last by ApochPiQ 7 years, 11 months ago

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 :)

Advertisement

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

@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?

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

@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

1.


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

@ApochPiQ. To add one more point I would guess its best to have different logic for the manager depending on the ship type. (like in the example between fighters and the corvette)

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

@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.

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 shape 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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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

This topic is closed to new replies.

Advertisement