private static bool YSFHandle_05_EntityJoined(Client ThisClient, Packets.GenericPacket InPacket)
                {
                    if (InPacket.Data.Length < 171)
                    {
                        ThisClient.SendPacket(InPacket);
                        return(true);
                    }

                    Packets.Packet_05_EntityJoined EntityJoined = new Packets.Packet_05_EntityJoined(InPacket);
                    Vehicle ThisVehicle = new Vehicle();

                    ThisVehicle.Update(EntityJoined);
                    ThisVehicle.TimeStamp = 0; //So the client can have immediate control of the flight data!
                    #region Add Vehicle to Vehicles List
                    lock (Vehicles.List)
                    {
                        Vehicles.List.RemoveAll(x => x == ThisVehicle);
                        Vehicles.List.Add(ThisVehicle);
                        VehiclesHistory.List.Add(ThisVehicle);
                    }
                    //ThisClient.CurrentAircraftJoinPacket = new Packets.Packet_05_EntityJoined(EntityJoined);
                    #endregion

                    if (EntityJoined.IsGround)
                    {
                        ThisClient.SendPacket(InPacket);
                        return(true);
                    }
                    if (Clients.YSFClients[0] == ThisClient)
                    {
                        Console.WriteLine("&b" + "Aircraft ID " + EntityJoined.ID + " Created");
                    }
                    if (EntityJoined.OwnerType == 3)
                    {
                        //Console.WriteLine(EntityJoined.Data.ToDebugHexString());
                        //Console.WriteLine(ThisConnection.ThisClient.Info.Username);
                        //Console.WriteLine(EntityJoined.ID);
                        ThisClient.Vehicle = ThisVehicle;
                        ThisClient.SendPacket(InPacket);

                        /*
                         *
                         * Client.Listener AckCreation = ThisConnection.ThisClient.YSFClient.ThisConnection.SocketDescriptor.SubscribeAcknowledgement(0, EntityJoined.ID);
                         * ThisConnection.SocketDescriptor.SendPacket(InPacket);
                         * ThisConnection.ThisClient.YSFClient.WaitListener(AckCreation);
                         *
                         * Packet_09_JoinApproved JoinApproved = new Packets.Packet_09_JoinApproved(new Packets.GenericPacket() { Type = 09 });
                         * Client.Listener WaitJoinApproval = ThisConnection.ThisClient.YSFServer.ThisConnection.Subscribe(JoinApproved);
                         * ThisConnection.ThisClient.YSFServer.WaitListener(WaitJoinApproval);
                         *
                         * Client.Listener AcknowledgeJoinApproval = ThisConnection.ThisClient.YSFClient.ThisConnection.SocketDescriptor.SubscribeAcknowledgement(6, 0);
                         * ThisConnection.ThisClient.YSFClient.WaitListener(AcknowledgeJoinApproval);
                         *
                         * Console.WriteLine("ADDED VEHICLE. TOTAL VEHICLES IS NOW: " + Server.Info.Vehicles.Count.ToString());
                         */
                        return(true);
                    }
                    ThisClient.SendPacket(InPacket);
                    return(true);
                }
예제 #2
0
 public bool Update(Packets.Packet_05_EntityJoined EntityJoined)
 {
     Type      = EntityJoined.ObjectType;
     ID        = EntityJoined.ID;
     OwnerName = EntityJoined.OwnerName;
     //Timestamp = (float)(DateTime.Now - Server.Info.TimeStarted).TotalSeconds;
     Prev_PosX   = EntityJoined.PosX;
     Prev_PosY   = EntityJoined.PosY;
     Prev_PosZ   = EntityJoined.PosZ;
     PosX        = EntityJoined.PosX;
     PosY        = EntityJoined.PosY;
     PosZ        = EntityJoined.PosZ;
     HdgX        = (short)(EntityJoined.RotX / Math.PI * 32767);
     HdgY        = (short)(EntityJoined.RotY / Math.PI * 32767);
     HdgZ        = (short)(EntityJoined.RotZ / Math.PI * 32767);
     LastUpdated = DateTime.Now;
     Identify    = EntityJoined.Identify;
     IFF         = EntityJoined.IFF;
     return(true);
 }
예제 #3
0
 public Packets.Packet_05_EntityJoined GetJoinPacket(bool _OwnedByPlayerRequesting)
 {
     Packets.Packet_05_EntityJoined EntityJoined = new Packets.Packet_05_EntityJoined();
     EntityJoined.ObjectType = Type;
     EntityJoined.ID         = ID;
     EntityJoined.IFF        = IFF;
     EntityJoined.PosX       = PosX;
     EntityJoined.PosY       = PosY;
     EntityJoined.PosZ       = PosZ;
     EntityJoined.RotX       = HdgX;
     EntityJoined.RotY       = HdgY;
     EntityJoined.RotZ       = HdgZ;
     EntityJoined.Identify   = Identify;
     EntityJoined.OwnerName  = OwnerName;
     if (_OwnedByPlayerRequesting)
     {
         EntityJoined.IsOwnedByThisPlayer = true;
     }
     else
     {
         EntityJoined.IsOwnedByOtherPlayer = true;
     }
     return(EntityJoined);
 }
예제 #4
0
            public bool SendUpdate()
            {
                if (!Loaded)
                {
                    return(false);
                }
                if (Playing)
                {
                    CurrentTime += 0.1;
                }
                #region GetCurrentPackets
                List <Packets.GenericPacket> PacketsToSend =
                    (
                        from ThisPacketEvent in Records
                        where ThisPacketEvent.TimeStamp >= PreviousTime
                        where ThisPacketEvent.TimeStamp < CurrentTime
                        select ThisPacketEvent.Packet
                    ).ToList();
                if (PacketsToSend.Count == 0)
                {
                    return(false);
                }
                List <Packets.Packet_11_FlightData> FlightDataPacketsToSend =
                    (
                        from ThisFlightDataPacket in PacketsToSend
                        where ThisFlightDataPacket.Type == 11
                        select new Packets.Packet_11_FlightData(ThisFlightDataPacket)
                    ).OrderByDescending(x => x.TimeStamp).ToList();
                #endregion

                List <Client> CurrentClients = Clients.LoggedIn;
                foreach (uint ThisACID in AircraftIDs)
                {
                    #region GetFlightDataFromThisAircraft
                    List <Packets.Packet_11_FlightData> ThisAircraftFlightData =
                        (
                            from ThisFlightDataPacket in FlightDataPacketsToSend
                            where ThisFlightDataPacket.ID == ThisACID
                            select ThisFlightDataPacket
                        ).ToList();
                    #endregion
                    if (ThisAircraftFlightData.Count < 1)
                    {
                        continue;
                    }

                    #region GetLatestFlightData
                    Packets.Packet_11_FlightData NewesetFlightData =
                        (from ThisFlightDataPacket in FlightDataPacketsToSend
                         where ThisFlightDataPacket.ID == ThisACID
                         select ThisFlightDataPacket).ToArray().Last();

                    #endregion

                    #region UpdateFlightData
                    foreach (Vehicle ThisVehicle in Vehicles.List.Where(x => x.ID == ThisACID))
                    {
                        ThisVehicle.Update(NewesetFlightData);
                    }
                    #endregion

                    float difference = (float)(NewesetFlightData.TimeStamp - PreviousTime);
                    foreach (Client ThisClient in CurrentClients)
                    {
                        #region SetTimeToNow
                        NewesetFlightData.TimeStamp = (float)((DateTime.Now - OpenYS.TimeStarted).TotalSeconds - (CurrentTime - PreviousTime) + difference);
                        #endregion

                        //Debug.WriteLine(NewesetFlightData.TimeStamp + " Ready to send to " + ThisClient.Username);
                        ThisClient.SendPacket(NewesetFlightData);
                        //Debug.WriteLine(NewesetFlightData.TimeStamp + " Send Flight Data.");
                    }
                    continue;
                }

                foreach (Packets.GenericPacket ThisPacket in PacketsToSend)
                {
                    if (ThisPacket.Type == 11)
                    {
                        continue;
                    }

                    #region Join Data
                    if (ThisPacket.Type == 05)
                    {
                        Packets.Packet_05_EntityJoined ThisJoinData = new Packets.Packet_05_EntityJoined(ThisPacket);

                        #region Create Vehicle
                        Vehicle ThisVehicle = new Vehicle();
                        ThisVehicle.Update(ThisJoinData);
                        //ThisJoinData = ThisVehicle.GetJoinPacket(false);
                        #endregion

                        #region Add Vehicle to Vehicles List
                        Vehicles.List.Add(ThisVehicle);
                        VehiclesHistory.List.Add(ThisVehicle);
                        #endregion

                        foreach (Client ThisClient in CurrentClients)
                        {
                            Packets.Packet_06_Acknowledgement Acknowledgement;
                            if (ThisJoinData.IsAircraft)
                            {
                                Acknowledgement = new Packets.Packet_06_Acknowledgement(0, ThisJoinData.ID);
                            }
                            else
                            {
                                Acknowledgement = new Packets.Packet_06_Acknowledgement(1, 0);
                            }
                            //ThisClient.SendPacketGetPacket(ThisJoinData, Acknowledgement);
                            ThisClient.SendPacket(ThisJoinData);
                        }
                        continue;
                    }
                    #endregion

                    #region Leave Data
                    if (ThisPacket.Type == 13)
                    {
                        Packets.Packet_13_RemoveAirplane ThisRemoveAirplane = new Packets.Packet_13_RemoveAirplane(ThisPacket);

                        lock (Vehicles.List) Vehicles.List.RemoveAll(x => x.ID == ThisRemoveAirplane.ID);

                        foreach (Client ThisClient in CurrentClients)
                        {
                            Packets.Packet_06_Acknowledgement AcknowledgeLeave = new Packets.Packet_06_Acknowledgement(2, ThisRemoveAirplane.ID);
                            //ThisClient.SendPacketGetPacket(ThisRemoveAirplane, AcknowledgeLeave);
                            ThisClient.SendPacket(ThisRemoveAirplane);
                        }
                        continue;
                    }
                    #endregion

                    CurrentClients.SendPacket(ThisPacket);
                }
                if (Playing)
                {
                    PreviousTime = CurrentTime;
                }
                return(true);
            }
예제 #5
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);
            }