//仿真时间单独计时

using System.IO;

public class Fire
{
    public class Aircraft
    {
        public string f_model { get; set; } //机型
        public string f_type { get; set; } //飞机种类
        public int f_pswaterw { get; set; } //喷洒宽度
        public int f_pswaterl { get; set; } //长度
        public double f_psmj { get; set; } //喷洒面积
    }

    //随机数X:0到1
    public static double GenerateRandomNumber()
    {
        Random random = new Random();
        double randomNumber = random.NextDouble();
        return randomNumber;
    }

    //小型直升机洒水概率(IP*POS)
    public static double SAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.72 + 0.01 * Math.Exp(exponent);
        double result = POS * 1;
        return POS;
    }

    //中型直升机洒水概率
    public static double MAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.8 + 0.1 * Math.Exp(exponent);
        double result = POS * 0.93;
        return POS;
    }

    //大型直升机
    public static double LAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.62 + 0.2 * Math.Exp(exponent);
        double result = POS * 0.87;
        return POS;
    }

    //多引擎水陆两栖飞机
    public static double MultiAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.72 + 0.16 * Math.Exp(exponent);
        double result = POS * 1;
        return POS;
    }

    //单引擎水陆两栖飞机
    public static double AmAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.88 + 0.02 * Math.Exp(exponent);
        double result = POS * 1;
        return POS;
    }

    //单引擎灭火飞机
    public static double SEAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.53 + 0.27 * Math.Exp(exponent);
        double result = POS * 0.8;
        return POS;
    }

    //大型灭火飞机
    public static double LFAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.53 + 0.27 * Math.Exp(exponent);
        double result = POS * 0.74;
        return POS;
    }

    //超大型灭火飞机
    public static double ULFAirProbability(double sigma = 1)
    {
        double x = GenerateRandomNumber();
        double exponent = -Math.Pow(x, 2) / (2 * Math.Pow(sigma, 2));
        double POS = 0.53 + 0.27 * Math.Exp(exponent);
        double result = POS * 0.76;
        return POS;
    }


    //飞机有效洒水面积,sigma一般等于1
    public static double SprinklerArea(string type, double sprinklerArea, double sigma = 1)
    {
        double _sprinklerArea = 0;
        switch (type)
        {
            case "大型直升机":
                double probability1 = LAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability1;
                break;
            case "中型直升机":
                double probability2 = MAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability2;
                break;
            case "小型直升机":
                double probability3 = SAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability3;
                break;
            case "多引擎水陆两栖飞机":
                double probability4 = MultiAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability4;
                break;
            case "单引擎水陆两栖飞机":
                double probability5 = AmAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability5;
                break;
            case "单引擎灭火飞机":
                double probability6 = SEAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability6;
                break;
            case "大型灭火飞机":
                double probability7 = LFAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability7;
                break;
            case "超大型灭火飞机":
                double probability8 = ULFAirProbability(sigma) * AirdropAccuracy.GetIpForType(type);
                _sprinklerArea = sprinklerArea * probability8;
                break;
            default:
                break;
        }

        return _sprinklerArea;
    }

    //地面人员灭火有效面积
    //任务开始后,每一批次人员相当于一架飞机,每半小时灭火面积相当于一次洒水面积
    //地面部署人员、机降人员到达时间、介入人数(王子涵传入)
    public static double groundPersonnelWater(int num)
    {
        // 灭火火线长度
        double PL;
        // 10分钟洒水火线长度      
        PL = 0.16 * num * 55;
        // 10分钟灭火面积
        double result = PL * 5;
        return result;
    }

    public class CountArea
    {
        public double burncount { get; set; } //火焰蔓延网格数
        public double burnarea { get; set; } //火焰蔓延面积
    }

    public class FireGrid
    {
        public double Time;
        public double FireGrids;
        public double FiredGrids;
    }

    //输入初始火场面积,计算已蔓延时间
    public static double InitialburnTime(double wind, double slope, double InitialArea)
    {
        double count = InitialArea / 400;
        double radians = slope * (Math.PI / 180); // 将角度转换为弧度
        double KS = 0.1127 * Math.Pow(Math.Tan(radians), 2);
        double KW = 0.422 * Math.Pow((3.284 * wind), 0.381);
        double time = (count / (KS * KW) + 9.06) / 0.305;
        return time;
    }


    /// <summary>
    /// 火焰蔓延计算方法,第一次洒水前是仿真时间
    /// </summary>
    /// <param name="wind">风速</param>
    /// <param name="slope">坡度</param>
    /// <param name="time">飞机仿真时间/s</param>
    /// <param name="burnarea">上一次洒水后的面积</param>
    /// <param name="tn">本次洒水时间</param>
    /// <param name="tn_1">上一次洒水时间</param>
    /// <param name="InitialArea">初始输入火场面积</param>
    /// <returns></returns>
    //
    public static CountArea burnCalculate(double wind, double slope, double time, double burnarea, double tn,
        double tn_1, double InitialArea)
    {
        double x = GenerateRandomNumber();
        if (wind == 0) wind = 1;
        if (slope < 16) slope = 16;
        if (burnarea == 0)
        {
            if (InitialArea > 0) time = InitialburnTime(wind, slope, InitialArea) + time;
        }
        else
        {
            //上一次洒水后面积计算得到的时间,加上两次洒水间隔
            time = InitialburnTime(wind, slope, burnarea) + (tn - tn_1);
        }

        double radians = slope * (Math.PI / 180); // 将角度转换为弧度
        double KS = 0.1127 * Math.Pow(Math.Tan(radians), 2);
        double KW = 0.422 * Math.Pow((3.284 * wind), 0.381);
        double count = KS * KW * (0.305 * time - 9.06);
        double area = count * 400;
        return new CountArea { burncount = count, burnarea = area };
    }

    /// <summary>
    /// 过火面积计算输出公式
    /// </summary>
    /// <param name="wind">风速</param>
    /// <param name="slope">坡度</param>
    /// <param name="time">任务耗时</param>
    /// <param name="InitialArea">初始输入火场面积</param>
    /// <returns></returns>
    public static CountArea burnedCalculate(double wind, double slope, double time, double InitialArea)
    {
        double x = GenerateRandomNumber();
        if (wind == 0) wind = 1;
        if (slope < 16) slope = 16;
        if (InitialArea > 0) time += InitialburnTime(wind, slope, InitialArea);
        double radians = slope * (Math.PI / 180); // 将角度转换为弧度
        double KS = 0.0648 * Math.Pow(Math.Tan(radians), 2);
        double KW = 0.438 * Math.Pow((3.284 * wind), 0.381);
        double count = KS * KW * (2.546 * time - 2999);
        double area = count * 400;
        return new CountArea { burncount = count, burnarea = area };
    }
}