Tuesday, 9 February 2016

Coding - How the space battle flight behaviours work

Coding Development - How the Game Works Part 1

I thought that given the game is complete now, and that given the game is available for free and all marketing has now been completed I would share some insights throughout the next set of blog posts about how the game hangs together - particularly the more unusual or innovative types of things the game does.

The first of these is the core gameplay feature of 'weighted behavioural AI' as I describe it for the flight behaviours of the ships in the game.

Space Battles in the game are precalculated, although the process I'm about to describe would work in both real time and preprocessing in any other game.

I'm not going to go into the basic mechanics of game programming - it will be expected that certain basic concepts such as a main game loop and the core principles of obtaining user/entity input, update world, draw world are understood generally speaking.

During the space battle each frame the various space ships have a counter decremented.  Once that counter reaches zero it is reset to a particular value and the count down is restarted again.  Should that space ship's currently targeted enemy spaceship be eliminated before this point the count down timer is automatically reset to zero.

Upon the countdown counter reaching zero a series of conditional statements are evaluated.

Within a short loop each enemy vessel is evaluated against a set of parameters such as their distance away, the vector position relative to the current vessel, their hitpoints, their damage, their speed and so on.  The AI flight settings set by the player beforehand results in a score being assigned to each of these parameters.  The individual scores based for each if statement (if true then add the value of the AI setting) are then totalled and compared with each other.  The enemy vessel with the greatest score is then set as the 'target enemy' for that ship.  

Once a target enemy for a ship is chosen the current ship will continually adjust its velocity to aim towards the enemy vessel.  Should it come too close then it will be given a nudge in a direction perpendicular to the direction to the enemy target vessel which will result in the current ship turning away from the target.  

That is the basis of how the ships acquire their targets throughout the space battle.

The pseudo code for the process would look something like this.

if 'aicounter' is zero then 
     reset ai counter to starting value (eg 200)
     set targetingscore to a highly negative value
     set targetedenemyvessel to -1
     loop through all enemy vessels
         if 'some condition' is true then 
                add relevant ai weighting score to the currenttarget's score
         repeat if statement and add score for other conditions
         if current target's score is the highest then set the targetedenemyvessel to this one
if 'currentship has a targetedenemyvessel' (which it always will unless the enemy fleet is destroyed)
     adjust velocity of ship to aim towards enemy vessel and fly towards it
loop through all enemy vessels
     if a collision is likely with the current vessel and an enemy vessel then 
           change the trajectory of the current vessel to be away from the vessel it will collide with    

No comments:

Post a Comment