# Simple space combat AI

This is a discussion on Simple space combat AI within the Game Programming forums, part of the General Programming Boards category; Have any of you done any AI for simulation type games such as flight sims and/or space sims? Here is ...

1. ## Simple space combat AI

Have any of you done any AI for simulation type games such as flight sims and/or space sims? Here is what I have so far and it seems to work fine. Right now the AI is very tough to beat and it rams the player every time. It does not seem elegant to me but it works and works as well as most of my retail space sim games.

All the AI is doing now is accelerating when in pursuit mode and then pursuing and attacking when in attack mode. If the sum total of the length of the vector between the desired up, right, and look vectors and the AI's current up, right, and look vectors then it is ok to fire at the player.
This translates to a very tough AI opponent. Anyone have any better ideas? I can also push pre-made manuevers onto the action queue prior to or after a current action. So the following scenario is very possible in my system:

1. Attack
2. When past the player (when result of dot product of velocity vectors show vectors face the same direciton) start jinking.
3. Jink for a random period of time or until distance from player is sufficiently large.
4. Alternatively the AI could loop up over the player and then come back for another pass in attack mode.
4. Either transition to orbit state, perform a split s, or back to attack state.
5. If player is sufficiently far away and based on AI aggressivenes, transition back to waypoint mode.

Ideas are welcome.

Current attack mode manuevers supported:
• Loop
• Immelman
• Split S
• Roll
• Orbit
• Attack
• Pursue
• Follow
• Form up (based on preset AI formations)
• Break formation
• Retreat
• Basic manuevers such as yaw left/right, pitch up/down, roll left/right, and accel up/down
• Match speed

2. If the sum total of the length of the vector between the desired up, right, and look vectors and the AI's current up, right, and look vectors then it is ok to fire at the player.
This translates to a very tough AI opponent. Anyone have any better ideas?
I don't entire know what you meant by this statement. Also, you said the AI 'rams' the player, do you mean the spacecrafts collide or that it simply lines it up for a shot?

Otherwise that looks quite sophisticated. You mentioned the instruction queue, did you write it as some type of virtual machine, or just simply a queue? Any particular pitfalls you didn't anticipate?

And as always, keep up the good work.

3. well to make it tougher, you should also have the AI decelerate to match speed and try to stay behind the player. But to create a more realistic enemy, we could probably discuss how to model human gameplay with neural networks.

4. You could have the AI try to attack from the players front. Like in a linear shooter they would attempt to gain a reasonable distance from the player and then charge at them from the front. While charging and retreating they can do your maneuvers. Since players generally don't like to be attacked from somewhere they can't see you can make it easier and more enjoyable. Of course it also matters a bit on your game mechanics.
it rams the player every time
Wouldn't it be pretty easy to stop it from doing this?

5. Originally Posted by abachler
well to make it tougher, you should also have the AI decelerate to match speed and try to stay behind the player. But to create a more realistic enemy, we could probably discuss how to model human gameplay with neural networks.
Have you tried this with any success?

6. Right now the enemies are doing some unexpected things but they are good things. First they rarely, if ever, attack from the front. This means that you must manuever to fire on them which I feel is quite realistic. Far more realistic than most space sims that just allow the AI to dawdle endlessly in front of the player.

@Bob:
My queue is just a simple dequeue of actions. Each 'action' can either be a basic action such as a directional action or accel/decel action. Then I have 'manuever's' which are composed of several basic actions. The 'manuevers' are taken straight from my several books on air combat and other experience with real airplanes and flight simulators.

To get all this to work there are two basic types of actions that can occur.
• Radian action - this action executes a basic manuever until a set radian value has been reached. IE: A loop is a pitch up for 6.28 radians.
• Timed action - this action executes a basic manuever until a set time has elapsed. So a loop for 1.0 second may not actually complete a full loop before the next 'action' or possibly 'actions' from a preset 'maneuver' execute.

Out of these action types I can now create just about any manuever I want by combining basic actions. An immelman is a half loop or a loop for PI radians followed by a roll left or right by PI radians. A split S is a roll left or right for PI radians and then a pitch up for PI radians. If this is a timed manuever and the computer chooses to roll left or right after this then this is the top portion of a classic high speed yo-yo.

But in reality all that is happening is that the computer will be in attack mode which is a simple find the vector to the player, follow it, and fire. When the craft is in range and the maximum error between the velocity vector of the attacker and the correct velocity vector needed to reach the target is within set limits (as determined by skill level of the AI) then the AI will fire on the target. At such a time that the closure rate brings the AI attacker too close to the player, it veers off by randomly choosing from several manuevers it can do at this point. This new manuever is pushed onto the FRONT of the queue so that when it is over ATTACK is then re-initiated.

I tried this AI system with 10 bad guys in the area. Before long I had lasers criss-crossing from all over the place aimed at my ship and various ships at various times were booming and zooming me. No matter who I pursued the others just kept on coming and attacking. Even at speeds high enough that the starfield became a big mess of lines the AI continued to attack and with precision. When my speed surpassed the limits of their ships they pursued me for quite some time before calling it quits. Crazy thing is I did not hard code this stuff nor expect these results. I just gave the AI a toolset to work with and it chooses which tool to use for the job. Interestingly enough I achieved some very believable AI with very little effort I'm sure there are more bugs to work out but so far it looks good.

For the firing solution here is some source which will explain what I mean:
Code:
```...
if (D3DXVec3Length(&(right_diff + up_diff + look_diff)) < 1.0f)
{
m_okToFire = true;
}
else
{
m_okToFire = false;
}
...```
This is where the fudge factor for firing accuracy is at. If the total length of the difference from actual required velocity vector is less than 1.0f (currently) then it is ok to fire. Larger values here will cause the AI to pray and spray lasers as they approach the correct firing vector while smaller values will result in an accurate firing solution albeit at the cost of the time it requires to attain the solution which may result in the AI veering off before the firing solution is attained.