/// === Air Speed Test===
/// Mission Builder Script for
/// iL2CoD-Blitz v.5.031
/// written by Varrattu

/// === first published: 2022-04-09 ===
/// https://forum.il2sturmovik.com/topic/65736-updated-dynamic-campaign-by-theoden-dynmis-exe/

/// === last reviewed:  2022-04-09 ===

/// === NOTICE ===
/// The script may be freely copied and used in non-commercial products,
/// assuming proper credit to the author is given. IT MAY NOT BE RESOLD.
/// If you want to use the software for commercial products, please contact the author.
/// This software is provided "as is" without any warranty of any kind.

using System;
using System.Collections.Generic; // List <T>
using System.Diagnostics; // Stopwatch()
using System.Threading; 

using maddox.game;
using maddox.game.world;


public class Mission : AMission
{
	public override void OnTickGame() {
		base.OnTickGame();

		if (Time.tickCounter() % 20 == 0) {

			AiAircraft aircraft = GamePlay.gpPlayer().Place() as AiAircraft;

			double amsl = aircraft.getParameter(part.ParameterTypes.Z_AltitudeMSL, -1);
			double oat = aircraft.getParameter(part.ParameterTypes.Z_AmbientAirTemperature, -1);
			double asi = aircraft.getParameter(part.ParameterTypes.I_VelocityIAS, -1);
			double tas = aircraft.getParameter(part.ParameterTypes.Z_VelocityTAS, -1);
			string ata = EngineManPress(aircraft);
			string rpm = EngineRPM(aircraft);



			tas *= 3.6;

			double eas = EquivalentAirspeed(tas, oat);

			String str = "h:";
			str += amsl.ToString("0000");
			str += "   U/min:";
			str += rpm;
			str += "   ata:";
			str += ata;
			str += "   V(a):";
			str += asi.ToString("0");
			str += "   V(q):";
			str += eas.ToString("0");
			str += "   V(e):";
			str += tas.ToString("0");


			GamePlay.gpHUDLogCenter(null, str, null);

			/// FlightdataRecorder(tasXYZ, tasZ, str);
		}

	}

	public override void OnActorCreated(int missionNumber, string shortName, AiActor actor) {
		base.OnActorCreated(missionNumber, shortName, actor);

		Timeout(0.001, () => {

			if (actor is AiAircraft) {
				double asi = (actor as AiAircraft).getParameter(part.ParameterTypes.I_VelocityIAS, -1);
				double tas = (actor as AiAircraft).getParameter(part.ParameterTypes.Z_VelocityTAS, -1);
				double agl = (actor as AiAircraft).getParameter(part.ParameterTypes.Z_AltitudeAGL, -1);
				(actor as AiAircraft).RefuelPlane(75);
				double fuelkg = (actor as AiAircraft).GetCurrentFuelQuantity();

				tas *= 3.6;

				string str = "";
				str += "\n";
				str += "Tankinhalt: ";
				str += fuelkg.ToString("0");
				str += "kg\n\n";
				str += "Höhe über NN: ";
				str += agl.ToString("0.00");
				str += "m\n";
				str += "Angezeigte Geschwindigkeit V(a): ";
				str += asi.ToString("0");
				str += "km/h\n";
				str += "Wahre Eigengeschwindigkeit V(e): ";
				str += tas.ToString("0");
				str += "km/h\n";

				GamePlay.gpLogServer(null, str, null);
			}
		});
	}

	public double EquivalentAirspeed(double tas, double oat) {
		
		double ratTmp = oat / 288.15;
		double n = Math.Pow(ratTmp, 5.2558);
		double Ps = 101325 * n;
		double ratPs = Ps / 101325;
		double ratDensity = ratPs / ratTmp;
		double eas = tas*Math.Sqrt(ratDensity);
		return eas;
    }

	public string EngineManPress(AiAircraft aircraft) {

		string str = "";
		int num = (aircraft.Group() as AiAirGroup).aircraftEnginesNum();
		for (int i = 0; i < num; i++) {
			str += aircraft.getParameter(part.ParameterTypes.I_EngineManPress, i).ToString("0.00");
			str += " ";
		}
		return str;
	}

	public string EngineRPM(AiAircraft aircraft) {

		string str = "";
		int num = (aircraft.Group() as AiAirGroup).aircraftEnginesNum();
		for (int i = 0; i < num; i++) {
			str += aircraft.getParameter(part.ParameterTypes.I_EngineRPM, i).ToString("0");
			str += " ";

		}
		return str;
	}
}
