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. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596

    Thumbs up 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. #2

    Join Date
    May 2005
    Posts
    1,041
    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.
    I'm not immature, I'm refined in the opposite direction.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    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. #5

    Join Date
    May 2005
    Posts
    1,041
    Quote Originally Posted by abachler View Post
    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?
    I'm not immature, I'm refined in the opposite direction.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Last edited by VirtualAce; 01-06-2009 at 01:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I make a combat msg buffer?
    By pliang in forum Game Programming
    Replies: 6
    Last Post: 04-11-2005, 04:22 AM
  2. Space Combat Ready
    By moonwalker in forum Game Programming
    Replies: 19
    Last Post: 07-29-2002, 01:58 PM
  3. Space Combat (50% Completed Program)
    By moonwalker in forum Game Programming
    Replies: 2
    Last Post: 07-26-2002, 09:59 AM
  4. Simple AI for tic-tac-toe...
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-10-2002, 08:49 PM
  5. Simple AI Needed!
    By minime6696 in forum Game Programming
    Replies: 2
    Last Post: 02-15-2002, 09:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21