Esempio n. 1
0
            public bool LoadReplay(string FileName)
            {
                #region Deny if already loading!
                if (Loading)
                {
                    Debug.WriteLine("Failure to load YSFlight Replay (Already Loading! Can't Dual Process!)");
                    return(false);
                }
                #endregion
                #region Deny if already loaded!
                if (Loaded)
                {
                    Debug.WriteLine("Failure to load YSFlight Replay (Already Loaded! Can't OverWrite!)");
                    return(false);
                }
                #endregion
                #region Deny if file not found!
                if (!Files.FileExists(FileName))
                {
                    Debug.WriteLine("Failure to load YSFlight Replay (File Not Found):\n    " + FileName);
                    return(false);
                }
                #endregion
                #region load records to buffer
                string[] Contents = Files.FileReadAllLines(FileName);
                #endregion
                #region clear the records, ready to write to.
                CurrentTime = 0;
                Records.Clear();
                #endregion

                Console.WriteLine("&eLoading Replay...");
                Loading = true;

                Threads.Add(() =>

                {
                    string Mode = "NONE";

                    #region Aircraft Data
                    string CurrentAC_Identify = "NULL";
                    uint CurrentAC_ID         = 0;
                    uint CurrentAC_IFF        = 0;
                    float CurrentAC_Fuel      = 100;
                    string CurrentAC_Tag      = "Replay";
                    #endregion

                    for (int CurrentLineNumber = 0; CurrentLineNumber < Contents.Length; CurrentLineNumber++)
                    {
                        #region prepare variables
                        string ThisLine  = Contents[CurrentLineNumber];
                        string Keyword   = ThisLine;
                        string Arguments = "";
                        if (Keyword.Contains(' '))
                        {
                            Arguments = Keyword.Split(new char[] { ' ' }, 2)[1];
                            Keyword   = Keyword.Split(' ')[0].ToUpperInvariant();
                        }

                        #endregion

                        #region No Aircraft at the moment.
                        if (Mode == "NONE")
                        {
                            if (Keyword == "AIRPLANE")
                            {
                                Mode         = "AIRPLANE";
                                CurrentAC_ID = World.Objects.GetNextID() | 256 * 256;
                                AircraftIDs.Add(CurrentAC_ID);
                                if (ThisLine.Contains(' '))
                                {
                                    CurrentAC_Identify = ThisLine.Split(' ')[1];
                                }
                                continue;
                            }
                        }
                        #endregion
                        #region Working on an aircraft.
                        if (Mode == "AIRPLANE")
                        {
                            #region get IFF
                            if (Keyword == "IDENTIFY")
                            {
                                UInt32.TryParse(Arguments, out CurrentAC_IFF);
                                continue;
                            }
                            #endregion
                            #region get Tag
                            if (Keyword == "IDANDTAG")
                            {
                                if (Arguments.Contains(' '))
                                {
                                    CurrentAC_Tag = Arguments.Split(new char[] { ' ' }, 2)[1];
                                    if (CurrentAC_Tag == "\"\"")
                                    {
                                        CurrentAC_Tag = "Replay";
                                    }
                                    continue;
                                }
                            }
                            #endregion
                            #region get airpcmnd
                            if (Keyword == "AIRPCMND")
                            {
                                if (Arguments.Contains(' '))
                                {
                                    string[] SubArguments = Arguments.Split(' ');
                                    #region get fuel
                                    if (SubArguments.Length > 1)
                                    {
                                        try
                                        {
                                            if (SubArguments[0].ToUpperInvariant() == "INITFUEL")
                                            {
                                                try
                                                {
                                                    CurrentAC_Fuel = (float)MetaData._Aircraft.FindByName(CurrentAC_Identify).Cache().WEIGFUEL;
                                                }
                                                catch
                                                {
                                                    Debug.WriteLine("Failed to set default fuel!");
                                                }
                                                try
                                                {
                                                    CurrentAC_Fuel = CurrentAC_Fuel / 100 * UInt32.Parse(SubArguments[1].Split('%')[0]);
                                                }
                                                catch
                                                {
                                                    Debug.WriteLine("Failed to load fuel: " + ThisLine);
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            Debug.WriteLine("Failed to process AIRPCMND: " + ThisLine);
                                        }
                                    }
                                    #endregion
                                    continue;
                                }
                            }
                            #endregion
                            #region process flight records.
                            if (Keyword == "NUMRECOR")
                            {
                                int RecordsCount = 0;
                                if (Arguments.Contains(' '))
                                {
                                    Int32.TryParse(Arguments.Split(' ')[0], out RecordsCount);
                                }
                                CurrentLineNumber++;
                                List <Packets.Packet_11_FlightData> ThisAircraftFlightDataRecords = new List <Packets.Packet_11_FlightData>();
                                for (int i = 0; i < RecordsCount; i++)
                                {
                                    string[] RecordContents = new string[4];
                                    try
                                    {
                                        RecordContents[0] = Contents[CurrentLineNumber];
                                        RecordContents[1] = Contents[CurrentLineNumber + 1];
                                        RecordContents[2] = Contents[CurrentLineNumber + 2];
                                        RecordContents[3] = Contents[CurrentLineNumber + 3];
                                    }
                                    catch
                                    {
                                        Debug.WriteLine("Error reading lines for YSF Record!");
                                        continue;
                                    }
                                    CurrentLineNumber                      += 4;
                                    PacketEvent ThisPacketEvent             = ProcessYSFlightAircraftRecord(RecordContents, CurrentAC_ID);
                                    Packets.Packet_11_FlightData FlightData = new Packets.Packet_11_FlightData(ThisPacketEvent.Packet);
                                    FlightData.Weight_Fuel                  = CurrentAC_Fuel;
                                    FlightData.Weight_SmokeOil              = 100;
                                    FlightData.Weight_Payload               = 0;
                                    #region spawn the aircraft
                                    if (i == 0)
                                    {
                                        //first event, need to add a creation packet!
                                        Packets.Packet_05_EntityJoined EntityJoined = new Packets.Packet_05_EntityJoined();
                                        EntityJoined.ID                   = CurrentAC_ID;
                                        EntityJoined.IFF                  = CurrentAC_IFF;
                                        EntityJoined.PosX                 = FlightData.PosX;
                                        EntityJoined.PosY                 = FlightData.PosY;
                                        EntityJoined.PosZ                 = FlightData.PosZ;
                                        EntityJoined.RotX                 = (float)(FlightData.HdgX / 32767 * Math.PI);
                                        EntityJoined.RotY                 = (float)(FlightData.HdgY / 32767 * Math.PI);
                                        EntityJoined.RotZ                 = (float)(FlightData.HdgZ / 32767 * Math.PI);
                                        EntityJoined.Identify             = CurrentAC_Identify;
                                        EntityJoined.OwnerName            = CurrentAC_Tag;
                                        EntityJoined.IsOwnedByThisPlayer  = false;
                                        EntityJoined.IsOwnedByOtherPlayer = true;
                                        EntityJoined.IsAircraft           = true;

                                        Records.Add(new PacketEvent(FlightData.TimeStamp, EntityJoined));
                                    }
                                    #endregion
                                    if (i > 0)
                                    {
                                        Packets.Packet_11_FlightData PrevFlightData = ThisAircraftFlightDataRecords.ToArray()[i - 1];
                                        float TimeDifference = FlightData.TimeStamp - PrevFlightData.TimeStamp;
                                        float ScalingFactor  = 1f / TimeDifference;

                                        //short _X_Left = (ushort)FlightData.HdgX - (ushort)ThisAircraftFlightDataRecords.ToArray()[i - 1].HdgX; //350-010 = 340; //010-350 = -340
                                        //short _X_Right = (ushort)ThisAircraftFlightDataRecords.ToArray()[i - 1].HdgX - (ushort)FlightData.HdgX; //

                                        float ChangeYDeg = Numbers.AngleAcuteDifference(FlightData.HdgX.ToDegrees(), PrevFlightData.HdgX.ToDegrees());
                                        float ChangeXDeg = Numbers.AngleAcuteDifference(FlightData.HdgY.ToDegrees(), PrevFlightData.HdgY.ToDegrees());
                                        float ChangeZDeg = Numbers.AngleAcuteDifference(FlightData.HdgZ.ToDegrees(), PrevFlightData.HdgZ.ToDegrees());

                                        //FlightData.V_HdgX = (float)(ChangeXDeg * ScalingFactor / 180 * Math.PI);
                                        //FlightData.V_HdgY = (float)(ChangeYDeg * ScalingFactor / 180 * Math.PI);
                                        //FlightData.V_HdgZ = (float)(ChangeZDeg * ScalingFactor / 180 * Math.PI);
                                        //FlightData.V_HdgX = 0;
                                        //FlightData.V_HdgY = 2;
                                        //FlightData.V_HdgZ = 0;
                                        FlightData.V_PosX = (short)((FlightData.PosX - PrevFlightData.PosX) * ScalingFactor * 10);
                                        FlightData.V_PosY = (short)((FlightData.PosY - PrevFlightData.PosY) * ScalingFactor * 10);
                                        FlightData.V_PosZ = (short)((FlightData.PosZ - PrevFlightData.PosZ) * ScalingFactor * 10);
                                    }
                                    ThisPacketEvent.Packet = FlightData;
                                    Records.Add(ThisPacketEvent);
                                    ThisAircraftFlightDataRecords.Add(FlightData);
                                    #region despawn the aircraft
                                    if (i == RecordsCount - 1)
                                    {
                                        //Last record, need to destory the aircraft now!
                                        Packets.Packet_13_RemoveAirplane RemoveACPacket = new Packets.Packet_13_RemoveAirplane(CurrentAC_ID);
                                        Records.Add(new PacketEvent(FlightData.TimeStamp, RemoveACPacket));
                                    }
                                    continue;
                                    #endregion
                                }
                                Mode = "NONE";
                                CurrentLineNumber--;
                            }
                            #endregion
                        }
                        #endregion
                    }
                    Loaded  = true;
                    Loading = false;
                    Console.WriteLine("&aLoaded Replay.");
                }, "Replay Loader: " + FileName);

                return(true);
            }