Results 1 to 11 of 11

Thread: Game class extensions

  1. #1
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,313
    Post Thanks / Like
    Total Downloaded
    185.19 MB

    Class extensions - AiAircraft

    We all know that the many of the game's classes are missing some vital functionaity. This topic is to introduce a series of class file EXTENSIONS that add more fuctionality to your coding.

    How to use the Extensions class? - Just drop the entire extensions class file below (not inside) your mission script file. You will then have each of these goodies available as extension methods (no need for in-line functions or additional coding):

    AIAIRCRAFT CLASS EXTENSIONS

    aircraft.AirCrew(); - Returns an array of type AiPerson of the aircraft's aircrew
    aircraft.AirspeedIAS(); - Returns the indicated airspeed of the aircraft
    aircraft.AirspeedTAS(); - Returns the true airspeed of the aircraft
    aircraft.AirTempAmbient(); - Returns the ambient air temperature in degrees C at the aircraft's position
    aircraft.AltitudeAGL(); - Returns the altitude in metres above ground level of the aircraft
    aircraft.AltitudeF(); - Returns the altitude in feet of the aircraft
    aircraft.AltitudeM(); - Returns the altitude in metres of the aircraft
    aircraft.AltitudeMSL(); - Returns the altitude in metres above mean sea level of the aircraft
    aircraft.CoPilot(); - Returns the co-pilot of the aircraft as an AiPerson
    aircraft.CrewPlaceFunction(function); - Returns to place number for the specified crew function
    aircraft.EnginesStart(); - Starts the specifed aircraft's engines
    aircraft.EnginesStop(); - Stops the specifed aircraft's engines
    aircraft.HasCrewFunction(function); - Returns true if the aircraft has the specified aircrew position/function
    aircraft.Heading(); - Instrument-free method to return the heading of the aircraft (aircraft dont need to have a compass)
    aircraft.IsAiControlled(); - Returns true if aircraft is AI controlled (no humans aboard)
    aircraft.IsAirborne(); - Returns true if aircraft is in the air
    aircraft.IsEnginesOff(); - Returns true if the aircraft engines are turned off
    aircraft.IsMoving(); - Returns true if the aircraft is moving
    aircraft.NumberOfAirCrew(); - Returns the number of aircrew in the aircraft
    aircraft.NumberOfAirCrewHuman(); - Returns the number of aircrew in the aircraft that are human players
    aircraft.NumberOfEngines(); - Returns the number of engines in the aircraft
    aircraft.NumberOfGunners(); - Returns the number of gunners in specified aircraft
    aircraft.NumberOfPilots(); - Returns the number of pilots (pilot & copilot) in specified aircraft
    aircraft.Pilot(); - Returns the pilot of the aircraft as an AiPerson
    aircraft.Pitch(); - Non-instrument method to return the pitch of the aircraft
    aircraft.PlayerFunction(); - Returns the crew function of the first human player in specified aircraft
    aircraft.PlayerPlace(); - Returns the place number of first human player in specified aircraft
    aircraft.RandomSystemFailure(p);- Causes random aircraft system failure with specified probability p.
    aircraft.ReturnToHomeBase(); - Sets the aircraft to return to it's home base
    aircraft.Speed(); - Instrument-free method to return the speed of the aircraft (does not rely on aircraft instruments)
    aircraft.TurnRate(); Returns the turn rate of the aircraft
    aircraft.Yaw(); - Non-instrument method to return the yaw of the aircraft


    Code:
    public static class Extensions : IGamePlay, IBattle, IBriefing, IGame, IGameSingle, IGameServer
    {
        #region AIAIRCRAFT CLASS EXTENSIONS
    
        internal class Damage
        {
            public string EDamageType { get; set; }
            public Damage(string eDamageType)
            {
                this.EDamageType = eDamageType;
            }
        }
    
        public static void RandomSystemFailure(this AiAircraft aircraft, double liklihood)
        {   // Purpose: Causes random aircraft system failure
            // Use: aircraft.RandomSystemFailure(n);
            // where n is the probability of failure (0 - 1)
            // zero means no chance to occur 1 means 100% chance to occur
            // eg. aircraft.RandomSystemFailure(aircraft, 0.01); means (0.01 * 100) = 1% chance to occur
            Random random = new Random(Guid.NewGuid().GetHashCode());
            if (random.NextDouble() > liklihood) RandomSystemFailure(aircraft);
        }
    
        private static void RandomSystemFailure(this AiAircraft aircraft)
        {   // RandomSystemFailure helper function
            if (aircraft == null || !(aircraft is AiAircraft)) return;
            Random random = new Random(Guid.NewGuid().GetHashCode());
            List<Damage> DamageList = new List<Damage>
            {
                // electrical systems
                new Damage ("ElecBatteryFailure"),
                new Damage ("ElecGeneratorFailure"),
                new Damage ("ElecIlluminationFailure"),
                new Damage ("ElecMasterCompassFailure"),
                new Damage ("ElecPrimaryFailure"),
                new Damage ("ElecPriNavigationFailure"),
                new Damage ("ElecSecNavigationFailure"),
                new Damage ("ElecSecondaryFailure"),
                new Damage ("ElecTransceiverFailure"),
                new Damage ("ElecWeaponryFailure"),
                new Damage ("Eng0Magneto1Failure"),
                new Damage ("Eng0Magneto2Failure"),
    
                //  fuel systems
                new Damage ("FuelPumpFailure"),
                new Damage ("FuelTank0PumpFailure"),
                new Damage ("FuelTank1PumpFailure"),
                new Damage ("FuelTank2PumpFailure"),
                new Damage ("FuelTank3PumpFailure"),
                new Damage ("FuelTank4PumpFailure"),
                new Damage ("FuelTank5PumpFailure"),
                new Damage ("FuelTank6PumpFailure"),
                new Damage ("FuelTank7PumpFailure"),
    
                // hydrolic & pneumatic systems
                new Damage ("HydraulicsPumpFailure"),
                new Damage ("PneumaticsCompressorFailure"),
    
                // pumps failures
                new Damage ("Eng0OilPumpFailure"),
                new Damage ("Eng0CompressorFailure"),
                new Damage ("Eng0FuelPumpFailure"),
                new Damage ("Eng0OilPumpFailure"),
                new Damage ("Eng0FuelPumpFailure"),
                new Damage ("Eng0WaterPumpFailure"),
              
                // engine failures
                new Damage ("Eng0CarbFailure"),
                new Damage ("Eng0CompressorGovernorFailure"),
                new Damage ("Eng0ExhaustHeadFailure"),
                new Damage ("Eng0GovernorFailure"),
                new Damage ("Eng0Plug00Failure"),
    
                // controls failures
                new Damage ("Eng0CarbControlsFailure"),
                new Damage ("FuelTank7PumpFailure"),
                new Damage ("FuelTank7PumpFailure"),
    	    };
    
            // do the failure
            string damage = null;
            int rnd = random.Next(DamageList.Count);
            damage = DamageList[rnd].EDamageType;
            if (DamageList[rnd].EDamageType.Contains("Eng"))
            {
                int i = random.Next((aircraft.Group() as AiAirGroup).aircraftEnginesNum());
                string[] parts = DamageList[rnd].EDamageType.Split(new string[] { "Eng" }, StringSplitOptions.None);
                damage = parts[0] + "Eng" + i.ToString() + parts[1];
                // aircraft.hitNamed((part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), "Eng" + i.ToString() + "TotalFailure"));
    
                // Eng0Plug00Failure
                if (damage.Contains("Plug00"))
                {
                    parts = damage.Split(new string[] { "Plug00" }, StringSplitOptions.None);
                    damage = parts[0] + "Plug" + random.Next(18).ToString("00") + parts[1];
                }
            }
            // do the failure
            part.NamedDamageTypes TheDamage = (part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), damage, true);
            aircraft.hitNamed(TheDamage);
    
        }
    
        #region WITHOUT DELEGATES
        public static double AltitudeM(this AiAircraft aircraft) 
        {   // Purpose: Returns the altitude in metres of the aircraft Use: aircraft.Altitude();
            return aircraft.Pos().z;
        }
        
        public static double AltitudeF(this AiAircraft aircraft)
        {   // Purpose: Returns the altitude in feet of the aircraft. Use: aircraft.Altitude();
            return aircraft.Pos().z * 3.28084;
        }
        
        public static bool IsMoving(this AiAircraft aircraft)
        {   // Purpose: Returns true if the aircraft is moving. Use: aircraft.IsMoving();
            return aircraft.getParameter(part.ParameterTypes.Z_VelocityTAS, -1) > 0;
        }
        
    
        public static bool IsEnginesOff(this AiAircraft aircraft)
        {   // Purpose: Returns true if the aircraft engines are turned off. Use: aircraft.IsEnginesOn();
            return (aircraft as AiAirGroup).Idle;
        }
    
        public static double TurnRate(this AiAircraft aircraft)
        {   // Purpose: Returns the turn rate of the aircraft. Use: aircraft.TurnRate();
            return aircraft.getParameter(part.ParameterTypes.I_Turn, -1);
        }
    
        public static double AirspeedTAS(this AiAircraft aircraft)
        {   // Purpose: Returns the true airspeed of the aircraft. Use: aircraft.AirspeedTAS();
            return aircraft.getParameter(part.ParameterTypes.Z_VelocityTAS, -1);
        }
    
        public static double AirspeedIAS(this AiAircraft aircraft)
        {   // Purpose: Returns the indicated airspeed of the aircraft. Use: aircraft.AirspeedIAS();
            return aircraft.getParameter(part.ParameterTypes.Z_VelocityIAS, -1);
        }
    
        public static double AltitudeAGL(this AiAircraft aircraft)
        {   // Purpose: Returns the altitude in metres above ground level of the aircraft. Use: aircraft.AltitudeAGL();
            return aircraft.getParameter(part.ParameterTypes.Z_AltitudeAGL, -1);
        }
        
        public static double AltitudeMSL(this AiAircraft aircraft)
        {   // Purpose: Returns the altitude in metres above mean sea level of the aircraft. Use: aircraft.AltitudeMSL();
            return aircraft.getParameter(part.ParameterTypes.Z_AltitudeMSL, -1);
        }
    
        public static double AirTempAmbient(this AiAircraft aircraft)
        {   // Purpose: Returns the ambient air teperature in degrees C at the aircraft's position. Use: aircraft.AirTempAmbient();
            return aircraft.getParameter(part.ParameterTypes.Z_AmbientAirTemperature, -1);
        }
        
        public static int NumberOfAirCrew(this AiAircraft aircraft)
        {   // Purpose: Returns the number of aircrew in the aircraft. Use: aircraft.NumberOfAirCrew();
            return aircraft.Places();
        }
        
        public static int NumberOfEngines(this AiAircraft aircraft)
        {   // Purpose: Returns the number of engines in the aircraft. // Use: aircraft.NumberOfEngines();
            return (aircraft.Group() as AiAirGroup).aircraftEnginesNum();
        }
        
        public static void ReturnToHomeBase(this AiAircraft aircraft)
        {   // Purpose: sets aircraft to RTB. Use: aircraft.ReturnToHomeBase();
            (aircraft as AiAirGroup).setTask(AiAirGroupTask.RETURN, aircraft as AiAirGroup);
        }
    
        public static bool IsAirborne(this AiAircraft aircraft) 
        {   // Purpose: Return true if aircraft is in th air. Use: aircraft.IsAirborne();
            return (aircraft as AiAirGroup).IsAirborne();
        }
        
        public static bool EnginesStart(this AiAircraft aircraft)
        {   // Purpose: starts the specifed aircraft's engines Use: aircraft.EnginesStart();
            return (aircraft as AiAirGroup).Idle = false;
        }
    
        public static void EnginesStop(this AiAircraft aircraft)
        {   // Purpose: stops the specifed aircraft's engines Use: aircraft.EnginesStop();
             (aircraft as AiAirGroup).Idle = true;
        }
    
        public static bool HasCrewFunction(this AiAircraft aircraft, CrewFunction function)
        {
            for (int i = 0; i < aircraft.Places(); i++)
            {
                if (aircraft.CrewFunctionPlace(i) == function) return true;
            }
            return false;
        }
    
        public static int CrewPlaceFunction(this AiAircraft aircraft, CrewFunction function)
        {   // Purpose: Returns to place number for the specified crew function
            //          Returns -1 if the specified aircraft does not have the named crew function 
            // Use: aircraft.CrewPlaceFunction(function);
            for (int i = 0; i < aircraft.Places(); i++)
            {
                if (aircraft.CrewFunctionPlace(i) == function)
                    return i;
            }
            return -1;
        }
    
        public static int NumberOfPilots(this AiAircraft aircraft) 
        {   // Purpose: Returns the number of pilots (pilot & copilot) in specified aircraft
            // Use: aircraft.NumberOfPilots();
            int result = 0;
            for (int i = 0; i < aircraft.Places(); i++)
            {
                if (aircraft.CrewFunctionPlace(i) == CrewFunction.Pilot ||
                    aircraft.CrewFunctionPlace(i) == CrewFunction.CoPilot)
                    result += 1;
            }
            return result;
        }
    
        public static int NumberOfGunners(this AiAircraft aircraft)
        {   // Purpose: Returns the number of gunners in specified aircraft
            // Use: aircraft.NumberOfGunners();
            int result = 0;
            for (int i = 0; i < aircraft.Places(); i++)
            {
                if (aircraft.CrewFunctionPlace(i) == CrewFunction.Gunner ||
                    aircraft.CrewFunctionPlace(i) == CrewFunction.NoseGunner ||
                    aircraft.CrewFunctionPlace(i) == CrewFunction.RearGunner ||
                    aircraft.CrewFunctionPlace(i) == CrewFunction.VentralGunner ||
                    aircraft.CrewFunctionPlace(i) == CrewFunction.WaistGunner ||
                    aircraft.CrewFunctionPlace(i) == CrewFunction.TopGunner)
                    result += 1;
            }
            return result;
        }
    
        public static CrewFunction PlayerFunction(this AiAircraft aircraft)
        {   // Purpose: Returns the crew function of the first human player in specified aircraft
            //          Returns Nil if no humans in the aircraft
            // Use: aircraft.PlayerFunction();
            AiCart cart = aircraft as AiCart;
            if (cart != null)
            {
                for (int i = 0; i < aircraft.Places(); i++)
                {
                    if (aircraft.Person(i) != null) return cart.CrewFunctionPlace(i);
                }
            }
            return CrewFunction.Nil;
        }
    
        public static int PlayerPlace(this AiAircraft aircraft) 
        {   // Purpose: Returns the place number of first human player in specified aircraft
            //          Returns -1 if no humans in the aircraft
            // Use: aircraft.PlayerFunction();
            for (int i = 0; i < aircraft.Places(); i++)
            {
                if (aircraft.Player(i) != null)
                    return i;
            }
            return -1;
        }
    
        public static float AirCrewHealth(this AiAircraft aircraft, CrewFunction function)
        {   // Purpose: Returns the health of the AiPerson in the specified aircrew position
            //          Returns -1 if the aircraft does not have the specified crew function 
            // Use: aircraft.AirCrewHealth(function);
            AiCart cart = aircraft as AiCart;
            for (int i = 0; i < cart.Places(); i++)
            {
                if (cart != null && cart.CrewFunctionPlace(i) == function)
                {
                    AiPerson p = aircraft.Player(i) as AiPerson;
                    if (p != null) return p.Health;
                }
            }
            return -1;
    
        }
    
        public static bool HasCrewFunction(this AiAircraft aircraft, CrewFunction function)
        {   // Purpose: Returns true if the aircraft has the specified aircrew position/function
            // Use: aircraft.HasCrewFunction(function);
            AiCart cart = aircraft as AiCart;
            for (int i = 0; i < cart.Places(); i++)
            {
                if (cart != null && cart.CrewFunctionPlace(i) == function)
                    return true;
            }
            return false;
        }
    
        public static AiPerson Pilot(this AiAircraft aircraft) 
        {   // Purpose: Returns the pilot of the aircraft as an AiPerson. 
            // Use: aircraft.Pilot();
            AiCart cart = aircraft as AiCart;
            if (cart != null)
            {
                for (int i = 0; i < aircraft.Places(); i++)
                {
                    if (cart.CrewFunctionPlace(i) == CrewFunction.Pilot) return aircraft.Person(i);
                }
            }
            return null;
        }
    
        public static AiPerson CoPilot(this AiAircraft aircraft) 
        {   // Purpose: Returns the co-pilot of the aircraft as an AiPerson
            //          Returns null if aircraft does not have co-pilot
            // Use: aircraft.CoPilot();
            AiCart cart = aircraft as AiCart;
            if (cart != null)
            {
                for (int i = 0; i < aircraft.Places(); i++)
                {
                    if (cart.CrewFunctionPlace(i) == CrewFunction.CoPilot) return aircraft.Person(i);
                }
            }
            return null;
        }
    
        public static bool IsAiControlle(this AiAircraft aircraft)
        {   // Purpose: Returns true if aircraft is AI controlled (no humans aboard)
            // Use: aircraft.IsAiControlled();
            if (aircraft == null) return false;
            for (int i = 0; i < aircraft.Places(); i++) if (aircraft.Player(i) != null) return false;
            return true;
        }
    
        public static AiPerson[] AirCrew(this AiAircraft aircraft)
        {   // Purpose: Returns an array of type AiPerson of the aircraft's aircrew
            // Use: aircraft.AirCrew();
            List<AiPerson> result = new List<AiPerson>();
            for (int i = 0; i < aircraft.Places(); i++) result.Add(aircraft.Player(i) as AiPerson);
            return result.ToArray();
        }
    
        public static int NumberOfAirCrewHuman(this AiAircraft aircraft) 
        {   // Purpose: Returns the number of aircrew in the aircraft that are human players
            // Use: aircraft.NumberOfAirCrewHuman();
            int result = 0;
            for (int i = 0; i < aircraft.Places(); i++)
            {
                if (aircraft.Player(i) != null) result++;
            }
            return result;
        }
    
        public static double Heading(this AiAircraft aircraft) 
        {   // Purpose: Returns the heading in degree of the aircraft
            //          Return -1 if calculations fail
            // Use: aircraft.Heading();
            AiAirGroup airgroup = aircraft as AiAirGroup;
            if (airgroup != null)
            {
                Vector2d v = new Vector2d(airgroup.Vwld().x, airgroup.Vwld().y);
                double radians = v.direction();
                return radians * (180 / Math.PI);
            }
            return -1;
        }
    
        public static double Speed(this AiAircraft aircraft)
        {   // Purpose: Returns the speed of the aircraft
            //          Return -1 if calculations fail
            // Use: aircraft.Speed();
            AiAirGroup airgroup = aircraft as AiAirGroup;
            if (airgroup != null)
            {
                Vector2d v = new Vector2d(airgroup.Vwld().x, airgroup.Vwld().y);
                return v.length();
            }
            return -1;
        }
    
        public static double Pitch(this AiAircraft aircraft)
        {   // Purpose: Returns the pitch of the aircraft
            //          Return -1 if calculations fail
            // Use: aircraft.Pitch();
            AiAirGroup airgroup = aircraft as AiAirGroup;
            if (airgroup != null)
            {
                Vector3d v = new Vector3d(airgroup.Vwld().x, airgroup.Vwld().y, airgroup.Vwld().z);
                return Math.Asin(v.y) * (180 / Math.PI); // pitch = asin(-d.Y);
            }
            return -1;
        }
    
        public static double Yaw(this AiAircraft aircraft)
        {   // Purpose: Returns the Yaw of the aircraft
            //          Return -1 if calculations fail
            // Use: aircraft.Yaw();
            AiAirGroup airgroup = aircraft as AiAirGroup;
            if (airgroup != null)
            {
                Vector3d v = new Vector3d(airgroup.Vwld().x, airgroup.Vwld().y, airgroup.Vwld().z);
                return Math.Atan2(v.x, v.z) * (180 / Math.PI); // yaw = atan2(d.X, d.Z)
            }
            return -1;
        }
    
        public static double Roll(this AiAircraft aircraft)
        {   // Purpose: Returns the Roll of the aircraft
            //          Return -1 if calculations fail
            // Use: aircraft.Roll();
            AiAirGroup airgroup = aircraft as AiAirGroup;
            if (airgroup != null)
            {
                Vector3d v = new Vector3d(airgroup.Vwld().x, airgroup.Vwld().y, airgroup.Vwld().z);
                return Math.Atan2(v.y, v.z) * (180 / Math.PI); // 
            }
            return -1;
        }
        #endregion
    }
    Last edited by Salmo; Oct-09-2016 at 05:01.

  2. Likes ChiefRedCloud, ATAG_Lolsav, TWC_Flug liked this post
  3. #2
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,313
    Post Thanks / Like
    Total Downloaded
    185.19 MB

    Re: Game class extensions

    reserved

  4. #3
    ATAG Member ATAG_Lolsav's Avatar
    Join Date
    Jun 2012
    Posts
    4,705
    Post Thanks / Like
    Blog Entries
    1
    Total Downloaded
    16.32 MB

    Re: Game class extensions

    Wow this is impressive! Im no coder but i can easily predict it will give very fine tools to those who can speak girblcodelish

    Salmo... what about the "dump data" option to be worked onto a MySql website? Still no go?

  5. #4
    ATAG Member ATAG_Freya's Avatar
    Join Date
    Jan 2013
    Location
    Lethbridge, Alberta
    Posts
    3,543
    Post Thanks / Like
    Total Downloaded
    16.05 MB

    Re: Game class extensions

    Very nice Salmo! So.. I don't speak codetalk but for discussions sake, as an amateur mission builder I am seeing a possible usage at first glance - a bomber training "tool" for the tab menu. For example, a player could get info about his current speed (IAS + TAS), altitude (m and/or ft), and extra stuff like heading displayed as a hud message for input to bombsight settings through use of the tab - 4 system. Just an as aid for bombsight training perhaps. Maybe? I don't understand the instruction in the OP though:

    Just drop the entire extensions class file below (not inside) your mission script file
    Will the script just have to reference this other file somehow? eg. I've used aircraft.IsMoving before, but just had it in the script file. Maybe I just haven't clued in yet... It'll hit me eventually

  6. #5
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,313
    Post Thanks / Like
    Total Downloaded
    185.19 MB

    Re: Game class extensions

    Quote Originally Posted by ATAG_Freya View Post
    ... I don't understand the instruction in the OP though: Will the script just have to reference this other file somehow?
    No. The C# compiler will automatically recognise the extension class. No specific references needed.

    Quote Originally Posted by ATAG_Freya View Post
    ...eg. I've used aircraft.IsMoving before, but just had it in the script file. Maybe I just haven't clued in yet... It'll hit me eventually
    Example structure below ...

    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using maddox.game;
    using maddox.game.world;
    using maddox.GP;
    using maddox.game.play;
    
    public class Mission : AMission
    {    // your script code here
         public override void OnAircraftTookOff(int missionNumber, string shortName, AiAircraft aircraft)
        {
              base.OnAircraftTookOff(missionNumber, shortName, aircraft);
    
              GamePlay.gpHUDLogCenter(string.Concat(aircraft.Pilot().Name(), " tookoff"));  // example class extension
              // GamePlay.gpHUDLogCenter(string.Concat(aircraft.Player(0).Name(), " tookoff")); // without extension method
        }
    }
    
    // .... pre-made class extensions code pasted below your script file class
    public static class Extensions : IGamePlay, IBattle, IBriefing, IGame, IGameSingle, IGameServer
    {
         // .... pre-made class extensions code
    }
    
    Last edited by Salmo; Oct-05-2016 at 01:40.

  7. Likes ATAG_Freya liked this post
  8. #6
    Banned
    Join Date
    May 2013
    Location
    Stamford, Lincs, UK
    Posts
    1,033
    Post Thanks / Like
    Blog Entries
    8
    Total Downloaded
    7.46 MB

    Re: Game class extensions

    Nice work Salmo.

    Any chance of getting fuel tank explosions in the random damage?

  9. #7
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,313
    Post Thanks / Like
    Total Downloaded
    185.19 MB

    Re: Game class extensions

    Quote Originally Posted by SoW Reddog View Post
    ... Any chance of getting fuel tank explosions in the random damage?
    You're welcome to add additional functionality to the RandomDamage extension method. I'm hoping that the experienced coders like yourself will contribute to the extensions to we can collectively add the functionality that scripters actually need to make exciting missions. My current code-set for the RandomDamage extension method is below, note I added the ability to specify the aircraft system to damage.

    Code:
    internal class Damage
        {
            public string EDamageType { get; set; }
            public Damage(string eDamageType)
            {
                this.EDamageType = eDamageType;
            }
        }
    
        public static void RandomSystemFailure(this AiAircraft aircraft, AircraftSystem system, double liklihood)
        {   // Purpose: Causes random aircraft system failure
            // Use: aircraft.RandomSystemFailure(aircraftsystem, p);
            // where p is the probability of failure (0 - 1)
            // zero means no chance to occur 1 means 100% chance to occur
            // eg. aircraft.RandomSystemFailure(aircraft, 0.01); means (0.01 * 100) = 1% chance to occur
            Random random = new Random(Guid.NewGuid().GetHashCode());
            if (random.NextDouble() > liklihood) RandomSystemFailure(aircraft, system, liklihood);
        }
    
        public enum AircraftSystem
        {
            Electrical,
            Fuel,
            Hydraulic,
            Engines,
            Controls,
            AllSystems,
            None
        }
    
        private static void RandomSystemFailure(this AiAircraft aircraft, AircraftSystem system)
        {   // RandomSystemFailure helper function
            if (aircraft == null || !(aircraft is AiAircraft)) return;
            Random random = new Random(Guid.NewGuid().GetHashCode());
            List<Damage> DamageList = new List<Damage>();
    
            List<Damage> ElectricalDamages = new List<Damage>
            {
                // electrical systems
                new Damage ("ElecBatteryFailure"),
                new Damage ("ElecGeneratorFailure"),
                new Damage ("ElecIlluminationFailure"),
                new Damage ("ElecMasterCompassFailure"),
                new Damage ("ElecPrimaryFailure"),
                new Damage ("ElecPriNavigationFailure"),
                new Damage ("ElecSecNavigationFailure"),
                new Damage ("ElecSecondaryFailure"),
                new Damage ("ElecTransceiverFailure"),
                new Damage ("ElecWeaponryFailure"),
                new Damage ("Eng0Magneto1Failure"),
                new Damage ("Eng0Magneto2Failure")
            };
    
            List<Damage> FuelDamages = new List<Damage>
            {
                //  fuel systems
                new Damage ("FuelPumpFailure"),
                new Damage ("FuelTank0PumpFailure"),
                new Damage ("FuelTank1PumpFailure"),
                new Damage ("FuelTank2PumpFailure"),
                new Damage ("FuelTank3PumpFailure"),
                new Damage ("FuelTank4PumpFailure"),
                new Damage ("FuelTank5PumpFailure"),
                new Damage ("FuelTank6PumpFailure"),
                new Damage ("FuelTank7PumpFailure")
            };
    
            List<Damage> HydraulicDamages = new List<Damage>
            {   // hydrolic & pneumatic systems
                new Damage ("HydraulicsPumpFailure"),
                new Damage ("PneumaticsCompressorFailure"),
    
                // pumps failures
                new Damage ("Eng0OilPumpFailure"),
                new Damage ("Eng0CompressorFailure"),
                new Damage ("Eng0FuelPumpFailure"),
                new Damage ("Eng0OilPumpFailure"),
                new Damage ("Eng0FuelPumpFailure"),
                new Damage ("Eng0WaterPumpFailure")
            };
    
            List<Damage> EngineDamages = new List<Damage>
            {
                // engine failures
                new Damage ("Eng0CarbFailure"),
                new Damage ("Eng0CompressorGovernorFailure"),
                new Damage ("Eng0ExhaustHeadFailure"),
                new Damage ("Eng0GovernorFailure"),
                new Damage ("Eng0Plug00Failure")
            };
    
            List<Damage> ControlsDamages = new List<Damage>
            {   // controls failures
                new Damage ("Eng0CarbControlsFailure"),
                new Damage ("FuelTank7PumpFailure"),
                new Damage ("FuelTank7PumpFailure")
    	    };
    
            switch (system)
            {
                case AircraftSystem.Controls:
                    DamageList = ControlsDamages;
                    break;
                case AircraftSystem.Electrical:
                    DamageList = ElectricalDamages;
                    break;
                case AircraftSystem.Engines:
                    DamageList = EngineDamages;
                    break;
                case AircraftSystem.Fuel:
                    DamageList = FuelDamages;
                    break;
                case AircraftSystem.Hydraulic:
                    DamageList = HydraulicDamages;
                    break;
                case AircraftSystem.AllSystems:
                    DamageList.AddRange(ControlsDamages);
                    DamageList.AddRange(ElectricalDamages);
                    DamageList.AddRange(EngineDamages);
                    DamageList.AddRange(FuelDamages);
                    DamageList.AddRange(HydraulicDamages);
                    break;
            }
    
            // do the failure
            string damage = null;
            int rnd = random.Next(DamageList.Count);
            damage = DamageList[rnd].EDamageType;
            if (DamageList[rnd].EDamageType.Contains("Eng"))
            {
                int i = random.Next((aircraft.Group() as AiAirGroup).aircraftEnginesNum());
                string[] parts = DamageList[rnd].EDamageType.Split(new string[] { "Eng" }, StringSplitOptions.None);
                damage = parts[0] + "Eng" + i.ToString() + parts[1];
                // aircraft.hitNamed((part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), "Eng" + i.ToString() + "TotalFailure"));
    
                // Eng0Plug00Failure
                if (damage.Contains("Plug00"))
                {
                    parts = damage.Split(new string[] { "Plug00" }, StringSplitOptions.None);
                    damage = parts[0] + "Plug" + random.Next(18).ToString("00") + parts[1];
                }
            }
            // do the failure
            part.NamedDamageTypes TheDamage = (part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), damage, true);
            aircraft.hitNamed(TheDamage);
    
        }
    ... and I presume you'd want an extension method that impliments something like ....
    Code:
        public override void  OnActorDamaged(int missionNumber, string shortName, AiActor actor, AiDamageInitiator initiator, part.NamedDamageTypes damageType)
        {
     	    base.OnActorDamaged(missionNumber, shortName, actor, initiator, damageType);
    
            if (actor is AiAircraft && damageType == part.NamedDamageTypes.FuelTank0Fire)
            {
                double ChanceTankFireExplodes = 0.05;
                if (random.NextDouble() < ChanceTankFireExplodes)
                    (actor as AiAircraft).hitNamed(part.NamedDamageTypes.FuelTank0Exploded);
            }
        }
    ... and a very quick convesion of the code above into an extension method before I go to bed. Just plug into the AIAIRCRFT EXTENSION CLASS. Best to call it from the OnActorDamaged routine with ....

    if (actor is AiAircraft) (actor as AiAircraft).FuelTankExplosion(damageType, 0.05);

    Code:
        public static void FuelTankExplosion(this AiAircraft aircraft, part.NamedDamageTypes damageTypeCausingExplosion, double ChanceTankFireExplodes)
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            if (damageTypeCausingExplosion == part.NamedDamageTypes.FuelTank0Fire)
            {
                if (random.NextDouble() < ChanceTankFireExplodes)
                    aircraft.hitNamed(part.NamedDamageTypes.FuelTank0Exploded);
            }
        }
    Last edited by Salmo; Oct-05-2016 at 07:41.

  10. #8
    Novice Pilot
    Join Date
    Feb 2014
    Posts
    68
    Post Thanks / Like
    Total Downloaded
    81.47 MB

    Re: Game class extensions

    Thank you for this...

    opens up a lot of new possibilities

  11. #9
    Combat pilot
    Join Date
    May 2015
    Posts
    183
    Post Thanks / Like
    Total Downloaded
    68.74 MB

    Re: Game class extensions

    This looks really great.

    I agree with you--something like this and the Gameplay class extensions you posted would be super-useful if built right into CloD.

  12. #10
    Student Pilot
    Join Date
    Apr 2017
    Posts
    6
    Post Thanks / Like
    Total Downloaded
    670.4 KB

    Re: Game class extensions

    Hello,

    Nice work. I was beginning to play with AiAircraft class and this will be of great help to learn.

    I found another issue in some of my work. The ingame coordinates system is (x / y) based. I need to get some (longitude / latitude) coordinates instead. I'm looking for some hints on how to translate from one system to the other. Any help would be greatly appreciated.

    Regards

  13. #11
    Team Fusion Salmo's Avatar
    Join Date
    Nov 2011
    Posts
    2,313
    Post Thanks / Like
    Total Downloaded
    185.19 MB

    Re: Game class extensions

    See the other thread you started ....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •