Saturday, June 30, 2012

Outline for the Fighter AI

I have been working on an outline for the AI routine.  This routine will be restricted to fighters; there will be a separate routine for capital ships due to stark differences between the roles of fighters and capital ships. 

Per advice from the Doctor (Professor Samuel Joseph), I plan on using a simplified approach.  This routine utilizes a simplified grading method based on distance and bearing and a fuzzy logic selection method.  Each enemy object is given a grade with the top five targets going into a target list.  Each target in the list is weighted in favor of the top target, and a target is randomly selected from this list.

Here's the outline:

1.      Initialize global variables
a.      Set up flight variables
                                                    i.     Set up the transform variable (vTrans)
                                                   ii.     Set up the FlightControl variable (vFlightControl)
                                                  iii.     Set up a update variable (vNextUpdate)
                                                  iv.     Set up mission target
                                                   v.     Set up current target as null
b.      Set up weapon variables
                                                    i.     Set up a max ammo variable (vMaxAmmo)
                                                   ii.     Set up a gun burst variable (vBurst)
2.      Flight control AI
a.      If fighter has sufficient fuel (less than 10% fuel), has ammo and is combat capable (greater than 25% structure remaining) execute maneuvering routines.
                                                    i.     Iterate through all targets in the DradisList
1.      Check for collision zones
2.      Catalogue eligible targets into a target list (limited to five targets) using range and direction parameters
                                                   ii.     If the fighter is in a collision zone or if there is a collision alert via raycasting in front of the ship
1.      Execute avoidance routines
2.      End loop
                                                  iii.     Target selection
1.      If the target list is empty
a.      Check for a mission target; set mission target as current target
2.      If the target list is not empty
a.      Calculate probability baseline by adding an iteration of the targets in the target list (5+4+3+2+1 = 15).
b.      Generate a random number based on baseline number (1-15)
c.      Calculate number of item selected by iterating through the number of targets in the target list (1-5 = 1, 6-10 = 2, etc…)
d.      Make the target associate with calculated number the current target
                                                  iv.     Maneuver the ship
1.      If there is a current target
a.      Get the distance from current target
b.      Get the size of the current target
c.      Set an disengagement distance as  size * 50
d.      If ship is within the disengagement distance
                                                                                                                i.     Set disengagement flag as true
e.      If disengagement flag is true
                                                                                                                i.     Move away from target
                                                                                                               ii.     If distance is twice the disengagement distance then set disengagement flag to false
f.       Otherwise move towards the current target
2.      Otherwise return to base
b.      Return to base
                                                    i.     If home base is null
1.      Find the closest friendly carrier ship
2.      Set home base to found ship
                                                   ii.     If home base is still null
1.      Slow to zero by applying inertial dampeners
2.      Wait for something to change
                                                  iii.     Otherwise
1.      Get closest landing zone
2.      Set approach point as closest landing zone
3.      If final point is null
a.      Move towards approach point
b.      If distance to approach point is less than 50
                                                                                                                i.     get final point
                                                                                                               ii.     deploy landing gears
4.      Otherwise
a.      Move towards final point
b.      If distance is less than 50 to final point
                                                                                                                i.     Apply inertial dampeners
                                                                                                               ii.     Set landed timer to current time plus five seconds
c.      If landed timer is greater than zero
                                                                                                                i.     If landed timer is greater than current time
                                                                                                               ii.     Remove ship from game
3.      Fire control AI routines
a.      For gun controls
                                                    i.     Get closest friendly target within firing parameters (range and direction)
                                                   ii.     If friendly target exists don’t fire
                                                  iii.     Otherwise
1.      Get the closest enemy target within firing parameters (range and direction)
2.      If there is an eligible target
a.      If firing timer is greater than current time
                                                                                                                i.     Fire the guns
                                                                                                               ii.     Set firing timer to ammo rate to current time
                                                                                                              iii.     Add one to burst fire variable
                                                                                                              iv.     If burst fire is equal to or greater than maximum burst
1.      Set firing timer to current time plus three seconds
2.      Set burst fire to zero
b.      Otherwise reduce burst fire by one if burst fire is greater than zero
b.      For missiles
                                                    i.     If missile target is null
1.      If mission target is eligible for lock (bearing and distance)
a.      If lock target is null
                                                                                                                i.     Set lock target to mission target
                                                                                                               ii.     Set lock timer to current time plus three seconds
b.      If lock timer is less than current time
                                                                                                                i.     Set missile target to lock target
                                                                                                               ii.     Set lock target to null
2.      Otherwise if current target is eligible for lock
a.      If there is no mission target and current target is eligible for lock
                                                                                                                i.     If lock target is null
1.      Set lock target to current target
2.      Set lock timer to current time plus three seconds
                                                                                                               ii.     If lock timer is less than current time
1.      Set missile target to lock target
2.      Set lock target to null
                                                   ii.     Otherwise
1.      If missiles are available (timer check and missile capacity)
a.      Fire missile at missile target
This is just a preliminary routine, and I'll probably have to polish it.  The current plan is to place these update routines in the FixedUpdate() section.  FixedUpdate() updates ten times a second in the current setup; Update() updates every frame.  FixedUpdate() should minimize the resource usage and make the fighters fly smoother.  It will also allow for the careful control of the burst fire feature for the fighter's guns.

Like I said, it is a simple routine.  As the game progresses, I'll add routines and complexity on top of this routine.  Or, I'll replace it completely in future prototypes.  I'm on prototype two; prototype three might see a departure from this set up. 

Friday, June 29, 2012

Feedback on BSG Prototype v2b20

I just played the latest prototype: and I actually got an impromptu microphone working on win 7 in order to be able to give you commentary feedback in real time ...! Here's the crash log:

Thursday, June 21, 2012

Musing about AI in Battlestar

These are just thoughts on AI to this point.

First, the AI routine needs to incorporate a timer mechanism.  The two basic update features an update by frame and an update at an interval.  Update at an interval is already an integral part of the game design and is set at ten update per second.  That is subject to change; however, it won't explode to a timeframe more conductive to manuevers.  In addition, it won't vary (which is a likely need for manuevers) during an actual mission.

Second, there are a large number of factors that go into selecting the "best" target.  The previous prototype had a simple AI, mostly to test features.  It consisted of a check against the DradisList (under a different name in the previous prototype) and checked the range and direction (in relation to the fighter).  Based off of these factors (distance and how in front the target was - distance being a slightly larger weight) it selected a target.  This created a very predictible flight pattern.  In this prototype, I want to create a more realistic and complex AI process.

Each fighter will need a standard stance: aggressive, balanced and defensive.  These will give different weight to different options.  An aggressive stance will more likely take an attack option; while a defensive stance will concentrate on avoiding damage.  Stances can change over the course of a mission; however, they can also stay static depending on the needs of the mission. 

In addition, each fighter should have a mission target option (represented by a game object related variable).  Fighters will have an option to ignore fighters (depending on circumstance) and attack the mission target.  Mission targets are more likely to be engaged with missiles, and the attack vector should be longer (i.e. engage at a distance) than a fighter versus fighter scenario.  I'm not sure if I advocate a mission target then looking for a target of opprotunity or weighing each target while using a single selection routine.

Fighter engagements need to be based on the current circumstances in the battlespace.  Factors could be:

Is there a target behind me?  How do I handle a trailing target?

Is there a target in front of me with a reasonable distance? 

Are there other factors like a closing fighter that need to be addressed?

Are there objects that I'm about to hit?  Which way is the best direction to travel if there are?

Am I about to travel into a "fire zone"?  Is it worth going into a "fire zone"?

A "fire zone" is the generally the area between to capital ships while both ships are firing on each other.  The bullets and missiles in this area are generally considered hazards, and BSG has traditionally handled this as a no-fly zone.

Third, if a fighter selects an attack pattern or a defensive manuever, how long should the fighter keep that engagement?  If it gets hit, will it continue pursuit?  Or will it turn and engage the offending target?  Or, will it execute evasive manuevers?  If I'm following a target with high hopes of destroying, do I worry about a trailing fighter behind me?  Do I continue with the engagement?  Do switch targets?  Do perform evasive manuevers and look for another target when I'm safe? 

If I engage a target and destroy, it's an easy call to go back to the selection process.  If I get hit, going back to the selection / decision process is pretty clear.  If someone is following me, I probably should go back to the decision process. 

Fourth, avoidance routines should always be on.  It breaks immersion when a Cylon raider (or a colonial viper for that matter) rams a capital ship for no reason.  There should be a forward raycast for each ship with a distance check for a hit.  Depending on the ship, it should perform an avoidance routine at certain distances.  However, should it go back to a decision process or attempt to reengage it's previous target? 

Fifth, fighters need to perform some form of "formation" flight.  Basically, fighters should also follow an ally to provide cover.  This is a difficult thing to emulate.  It's also a difficult thing to add to the decision process.  In addition, it might be difficult to calculate how long a fighter stays in formation.  It's been said in the fighter community (real world and many fictional worlds) to never abandon your wingman.  How far does that need to be taken to provide a fun but somewhat realistic game. 

So many considerations...  I have a headache now. 

Friday, June 15, 2012

The Basics are done...

I've finished up the last part of the turrets and finished missile launchers.  Actually, they are considered the same thing by the game.  The only real difference is whether they have bullets or missiles (or both for that matter) loaded into their data structure.  They have a methodology to turn and shoot (from a non-player entity perspective).  I do have to add one more thing, now that I thought of it.  I have to add locking onto a target routine for non-player entities when it comes to missile locks. 

The rest is done though.  At this point, the objects are fully designed except for the AI routines.  In addition, I do have to create a robust resource manager.  At this point, I have to sit down and figure out how I want the AI to work for fighters and capital ships (for both sides).  I'm not sure how I want to approach this right now. 

I'll have to map out the general process of what I look at when attempting to engage a target and come up with some method for a decision making process.  I'm not sure if a fuzzy logic or scripted AI approach will be best suited at this point...

Monday, June 4, 2012

Final Update (before Finals)

This is my final version before dedicating myself to my finals. The key features added in this version are:

>> Landing bay gravity (only on the Colonial destroyer at this point)
>> Automatic damage mitigation for larger objects (it's just harder for a viper to destroy a basestar - I had to add this so the structure numbers don't get out of hand)
>> Landing Gears (you can press "L" for landing gears - they don't anything yet; so, you will still take some damage when landing)
>> Landing (you can land on the destroyer!)
>> Turret controls (turrets should go around and around - there's a default value being inputted to all turrets - the system works for when I decide to put AI routines in)
>> Turret destruction (you can destroy turrets without destroying the big ship - the colonial destroyer is the only ship with turrets at this point - but you can test it out)
>> Turret take damage, and damage effects the efficiency of the turret.
>> A colonial destroyer (christened the Hermes Class Destroyer by me - it's even mostly textured)
>> A cylon destroyer (not textured at this point for really functional beyond target practice)

In the next version, I will focus on completing the turret control and develop missile launchers. As it stands, I need to complete the loading functios for turrets. Each turret has a fire zone; a turret should not be able to turn and face its own ship. To facilitate this, I need to define the fire zone for each turret. This will be done via the database. Each turret will be stored and sorted by the turret designation (each turret on a capital ship will have a designation) and ship classification. This means turret TT11 for a Hermes class destroyer may have a different fire zone than turret TT11 on the Battlestar Galactica.

In addition to fire zone information, I will need to store the basic parameter (like vehicle data, turret control, fire control and other variables) in the database. During the Start() routine for the parent ship, these values will be loaded into each turret.

Missile launchers will follow a similar design philosphy. The only real difference will be the projectile and in the AI routines. Bullets require a good line of sight setup to hit a target; missiles can be a release and forget variety. Missiles, after all, can have independent guidance systems. If I can get the turrets completed, I should be able to get missile launchers up and running without any major issue.

With turrets and missile launchers out of the way, I will probably have enough of a game framework to start on the AI routines and mission development. That's assuming that I have not missed anything significant (it's been known to happen on occassion).

At any rate, here is the latest prototype. I think there is a crash error somewhere in this version; however, I have not been able to pin it down. If anyone finds it, I would appreciate the output and crash logs. In addition, a description of what was going on would be really appreciated...

Sunday, June 3, 2012

Update (Late I guess)

School has been hectic lately; so, I haven't been posting here much.  I ave not worked on the project for at least a wek either.  However, I did take time to do some work on the project before finals week (military campus semester). 

I found a couple of issues that set me back somewhat.  First, the turret control system was not functioning with my parameters.  I went back into the class and reworked it.  Basically, the turret control system is a cross between the flight control and the HUD's camera control systems.  It does not utilize a smooth move concept used in the flight control due to the limited update functions in the module. 

Here is the class:

I added sections to make the class functional without an actual load routine system.  The Start() and FixedUpdate() routines have extra statements inserted to make the module functional.  The turrets with this script (along with the PlayerData and VehicleData classes) rotate in a circle.  I've also tested the elevation commands for the turret, and they are functional. 

With this module in place, I can now create the load routines.  I've decided to change the loading routine process (as per my previous posts).  Each turret will only need a fire zone loaded into each turret.  Cutout zones will be simulated by a different method than I originally designed in my last post.

Before firing the guns, the fire control routine will do a line of sight check.  If the ray generated by this process "hits" a friendly target, then the weapon will not fire.  This routine will be inserted into the fire control class. 

I may have to change the setup fo each gun enabled ship in the game.  Each bullet spawn point will need to changed into a camera (cameras easier to implement a draw ray function).  If a ray intersects with a friendly target from these cameras; the weapon won't fire. 

Also, I will need to address the ability to target turrets (and in the future, missile launchers) via a missile lock.  I'm still considering the best way of addressing this issue.