Exemple #1
0
        public static void UpdateAllPlayers()
        {
            List<Player> Players = PlayersController.GetAll();
            foreach (var Player in Players)
            {
                NetOutgoingMessage Msg = NetServer.CreateMessage();
                Msg.Write((int)Shared.NetworkMessageType.UpdatePlayer);
                PlayerUpdateStruct PlayerData = new PlayerUpdateStruct();
                PlayerData.ID = Player.ID;
                PlayerData.Name = Player.Name;
                PlayerData.Model = Player.Model;
                PlayerData.Health = Player.Health;
                PlayerData.Armor = Player.Armor;
                PlayerData.CurrentVehicle = Player.CurrentVehicle;
                PlayerData.VehicleSeat = Player.VehicleSeat;
                PlayerData.Pos_X = Player.Position.X;
                PlayerData.Pos_Y = Player.Position.Y;
                PlayerData.Pos_Z = Player.Position.Z;
                PlayerData.Vel_X = Player.Velocity.X;
                PlayerData.Vel_Y = Player.Velocity.Y;
                PlayerData.Vel_Z = Player.Velocity.Z;
                PlayerData.Heading = Player.Heading;
                PlayerData.IsWalking = Player.IsWalking;
                PlayerData.IsRunning = Player.IsRunning;
                PlayerData.IsJumping = Player.IsJumping;
                PlayerData.IsCrouching = Player.IsCrouching;
                PlayerData.IsGettingIntoVehicle = Player.IsGettingIntoVehicle;
                PlayerData.IsGettingOutOfVehicle = Player.IsGettingOutOfVehicle;

                Msg.WriteAllFields(PlayerData);

                NetServer.SendToAll(Msg, Player.NetConnection, NetDeliveryMethod.Unreliable, 1);
            }
        }
Exemple #2
0
        public static void OnTick(object sender, EventArgs e)
        {
            if (NetClient == null)
            {
                return;
            }

            NetIncomingMessage Msg;

            while ((Msg = NetClient.ReadMessage()) != null)
            {
                switch (Msg.MessageType)
                {
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)Msg.ReadByte();
                        switch (status)
                        {
                            case NetConnectionStatus.InitiatedConnect:
                                Game.Console.Print("Connecting to server.");
                                break;
                            case NetConnectionStatus.Connected:
                                Game.Console.Print("Connected to server.");
                                World.CarDensity = 0;
                                World.PedDensity = 0;
                                Game.WantedMultiplier = 0;
                                Game.LocalPlayer.WantedLevel = 0;
                                GTA.Native.Function.Call("CLEAR_AREA", 0.0f, 0.0f, 0.0f, 4000.0f, true);
                                Game.LocalPlayer.Character.BlockGestures = true;
                                Game.LocalPlayer.Character.BlockPermanentEvents = true;
                                Game.LocalPlayer.Character.PreventRagdoll = true;
                                Game.LocalPlayer.Character.WillFlyThroughWindscreen = false;
                                Ped[] Peds = World.GetAllPeds();
                                foreach (var Ped in Peds)
                                {
                                    if (Ped.Exists())
                                    {
                                        if (Ped != Game.LocalPlayer.Character)
                                        {
                                            Ped.Delete();
                                        }
                                    }
                                }
                                Vehicle[] Vehicles = World.GetAllVehicles();
                                foreach (var Vehicle in Vehicles)
                                {
                                    if (Vehicle.Exists())
                                    {
                                        Vehicle.Delete();
                                    }
                                }
                                RemotePlayersController = new RemotePlayersController();
                                RemoteVehiclesController = new RemoteVehiclesController();
                                ResourcesManager = new Shared.Scripting.ResourcesManager();
                                EventsManager = new Shared.Scripting.EventsManager();
                                IsSpawned = false;
                                Game.FadeScreenOut(1);
                                Game.LocalPlayer.Model = "F_Y_SWAT";
                                Initialized = true;
                                break;
                            case NetConnectionStatus.Disconnected:
                                Game.Console.Print("Disconnected from server.");
                                Game.FadeScreenIn(500);
                                break;
                        }
                        break;
                    case NetIncomingMessageType.Data:
                        if (!Initialized)
                            continue;
                        int MsgType = Msg.ReadInt32();
                        switch (MsgType)
                        {
                            case (int)Shared.NetworkMessageType.PlayerConnected:
                                {
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.PlayerDisconnected:
                                {
                                    int ID = Msg.ReadInt32();
                                    RemotePlayer Player = RemotePlayersController.GetByID(ID);
                                    if (Player != null)
                                    {
                                        RemotePlayersController.Remove(Player);
                                        Player.Destroy();
                                        Player = null;
                                    }
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.LoadResource:
                                {
                                    string ResourceName = Msg.ReadString();
                                    Shared.Scripting.Resource Resource = new Shared.Scripting.Resource();
                                    Resource.Name = ResourceName;
                                    ResourcesManager.Resources.Add(Resource);
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.StartResource:
                                {
                                    string ResourceName = Msg.ReadString();
                                    Shared.Scripting.Resource Resource = ResourcesManager.GetByName(ResourceName);
                                    if(Resource != null)
                                    {
                                        Resource.Start();
                                    }
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.ResourceFile:
                                {
                                    string ResourceName = Msg.ReadString();
                                    string ScriptName = Msg.ReadString();
                                    string ScriptCode = Msg.ReadString();
                                    ResourcesManager.GetByName(ResourceName).AddScript(ScriptName, ScriptCode);
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.UpdatePlayer:
                                {
                                    PlayerUpdateStruct PlayerData = new PlayerUpdateStruct();
                                    Msg.ReadAllFields(PlayerData);
                                    RemotePlayer Player = RemotePlayersController.GetByID(PlayerData.ID);
                                    if (Player == null)
                                    {
                                        Player = new RemotePlayer(PlayerData.Model);
                                        Player.ID = PlayerData.ID;
                                        RemotePlayersController.Add(Player);
                                    }
                                    Player.Name = PlayerData.Name;
                                    Player.SetHealth(PlayerData.Health);
                                    Player.SetArmor(PlayerData.Armor);
                                    if (PlayerData.CurrentVehicle > 0)
                                    {
                                        Player.CurrentVehicle = RemoteVehiclesController.GetByID(PlayerData.CurrentVehicle);
                                        Player.CurrentSeat = (VehicleSeat)PlayerData.VehicleSeat;
                                        Player.Update();
                                    }
                                    else
                                    {
                                        if (Player.CurrentVehicle != null)
                                        {
                                            Player.CurrentVehicle = null;
                                        }
                                        Vector3 Position = new Vector3();
                                        Position.X = PlayerData.Pos_X;
                                        Position.Y = PlayerData.Pos_Y;
                                        Position.Z = PlayerData.Pos_Z - 1.0f;
                                        Vector3 Velocity = new Vector3();
                                        Velocity.X = PlayerData.Vel_X;
                                        Velocity.Y = PlayerData.Vel_Y;
                                        Velocity.Z = PlayerData.Vel_Z;
                                        Player.SetPosition(Position);
                                        Player.SetVelocity(Velocity);
                                        Player.SetHeading(PlayerData.Heading);
                                        Player.IsWalking = PlayerData.IsWalking;
                                        Player.IsRunning = PlayerData.IsRunning;
                                        if (!Player.IsJumping && PlayerData.IsJumping == true)
                                        {
                                            Player.TaskJumpAdded = false;
                                        }
                                        Player.IsJumping = PlayerData.IsJumping;
                                        Player.IsCrouching = PlayerData.IsCrouching;

                                        Player.Interpolation_Start = DateTime.Now;
                                        Player.Interpolation_End = DateTime.Now.AddMilliseconds((double)Shared.Settings.TickRate).AddMilliseconds(NetClient.ServerConnection.AverageRoundtripTime / 1000);
                                        Player.Update();
                                    }
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.SpawnPlayer:
                                {
                                    Vector3 Position = new Vector3();
                                    Position.X = Msg.ReadFloat();
                                    Position.Y = Msg.ReadFloat();
                                    Position.Z = Msg.ReadFloat();
                                    float Heading = Msg.ReadFloat();
                                    IsSpawned = true;
                                    Game.LocalPlayer.Character.Position = Position;
                                    Game.LocalPlayer.Character.Heading = Heading;
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.FadeScreenIn:
                                {
                                    int Duration = Msg.ReadInt32();
                                    Game.FadeScreenIn(Duration);
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.FadeScreenOut:
                                {
                                    int Duration = Msg.ReadInt32();
                                    Game.FadeScreenOut(Duration);
                                    break;
                                }
                            case (int)Shared.NetworkMessageType.UpdateVehicle:
                                {
                                    VehicleUpdateStruct VehicleData = new VehicleUpdateStruct();
                                    Msg.ReadAllFields(VehicleData);

                                    RemoteVehicle Vehicle = RemoteVehiclesController.GetByID(VehicleData.ID);
                                    if (Vehicle == null)
                                    {
                                        Vehicle = new RemoteVehicle(VehicleData.Model);
                                        Vehicle.ID = VehicleData.ID;
                                        RemoteVehiclesController.Add(Vehicle);
                                    }
                                    Vehicle.Model = VehicleData.Model;
                                    Vector3 Position = new Vector3();
                                    Position.X = VehicleData.Pos_X;
                                    Position.Y = VehicleData.Pos_Y;
                                    Position.Z = VehicleData.Pos_Z;
                                    Vehicle.SetPosition(Position);
                                    Vector3 Velocity = new Vector3();
                                    Velocity.X = VehicleData.Vel_X;
                                    Velocity.Y = VehicleData.Vel_Y;
                                    Velocity.Z = VehicleData.Vel_Z;
                                    Vehicle.SetVelocity(Velocity);
                                    Vehicle.SetHeading(VehicleData.Heading);
                                    Quaternion Rotation = new Quaternion();
                                    Rotation.X = VehicleData.Rot_X;
                                    Rotation.Y = VehicleData.Rot_Y;
                                    Rotation.Z = VehicleData.Rot_Z;
                                    Rotation.W = VehicleData.Rot_A;
                                    Vehicle.SetRotation(Rotation);
                                    Vehicle.Interpolation_Start = DateTime.Now;
                                    Vehicle.Interpolation_End = DateTime.Now.AddMilliseconds((double)Shared.Settings.TickRate).AddMilliseconds(NetClient.ServerConnection.AverageRoundtripTime / 1000);
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                        break;
                    default:
                        Game.Console.Print("Unhandled message type: " + Msg.MessageType);
                        break;
                }
            }

            if (NetClient.ConnectionStatus == NetConnectionStatus.Connected)
            {
                NetOutgoingMessage OutMsg = NetClient.CreateMessage();
                PlayerUpdateStruct PlayerData = new PlayerUpdateStruct();
                Vector3 PlayerPos = Game.LocalPlayer.Character.Position;
                Vector3 PlayerRot = Game.LocalPlayer.Character.Direction;
                float PlayerHeading = Game.LocalPlayer.Character.Heading;
                PlayerData.Name = PlayerName;
                PlayerData.Health = Game.LocalPlayer.Character.Health;
                PlayerData.Armor = Game.LocalPlayer.Character.Armor;
                if (Game.LocalPlayer.Character.isInVehicle())
                {
                    Vehicle CurrentVehicle = Game.LocalPlayer.Character.CurrentVehicle;
                    PlayerData.CurrentVehicle = RemoteVehiclesController.GetByGame(CurrentVehicle).ID;
                    VehicleSeat CurrentSeat = VehicleSeat.None;
                    if (CurrentVehicle.GetPedOnSeat(VehicleSeat.Driver) == Game.LocalPlayer.Character)
                    {
                        CurrentSeat = VehicleSeat.Driver;
                    }
                    else if (CurrentVehicle.GetPedOnSeat(VehicleSeat.RightFront) == Game.LocalPlayer.Character)
                    {
                        CurrentSeat = VehicleSeat.RightFront;
                    }
                    else if (CurrentVehicle.GetPedOnSeat(VehicleSeat.LeftRear) == Game.LocalPlayer.Character)
                    {
                        CurrentSeat = VehicleSeat.LeftRear;
                    }
                    else if (CurrentVehicle.GetPedOnSeat(VehicleSeat.RightRear) == Game.LocalPlayer.Character)
                    {
                        CurrentSeat = VehicleSeat.RightRear;
                    }
                    PlayerData.VehicleSeat = (int)CurrentSeat;
                    PlayerData.Pos_X = CurrentVehicle.Position.X;
                    PlayerData.Pos_Y = CurrentVehicle.Position.Y;
                    PlayerData.Pos_Z = CurrentVehicle.Position.Z;
                    Vector3 Velocity = CurrentVehicle.Velocity;
                    Velocity.Normalize();
                    PlayerData.Vel_X = Velocity.X;
                    PlayerData.Vel_Y = Velocity.Y;
                    PlayerData.Vel_Z = Velocity.Z;
                    PlayerData.Rot_X = CurrentVehicle.RotationQuaternion.X;
                    PlayerData.Rot_Y = CurrentVehicle.RotationQuaternion.Y;
                    PlayerData.Rot_Z = CurrentVehicle.RotationQuaternion.Z;
                    PlayerData.Rot_A = CurrentVehicle.RotationQuaternion.W;
                    PlayerData.Heading = CurrentVehicle.Heading;
                    PlayerData.Speed = CurrentVehicle.Speed;
                }
                else
                {
                    Vector3 PlayerVel = Game.LocalPlayer.Character.Velocity;
                    PlayerData.Pos_X = PlayerPos.X;
                    PlayerData.Pos_Y = PlayerPos.Y;
                    PlayerData.Pos_Z = PlayerPos.Z;
                    PlayerData.Vel_X = PlayerVel.X;
                    PlayerData.Vel_Y = PlayerVel.Y;
                    PlayerData.Vel_Z = PlayerVel.Z;
                    PlayerData.Heading = PlayerHeading;
                    PlayerData.IsWalking = Game.isGameKeyPressed(GameKey.MoveBackward) ||
                        Game.isGameKeyPressed(GameKey.MoveForward) ||
                        Game.isGameKeyPressed(GameKey.MoveLeft) ||
                        Game.isGameKeyPressed(GameKey.MoveRight);
                    PlayerData.IsRunning = Game.isGameKeyPressed(GameKey.Sprint);
                    PlayerData.IsJumping = Game.isGameKeyPressed(GameKey.Jump);
                    PlayerData.IsCrouching = GTA.Native.Function.Call<bool>("IS_CHAR_DUCKING", Game.LocalPlayer.Character);
                }
                OutMsg.Write((int)NetworkMessageType.UpdatePlayer);
                OutMsg.WriteAllFields(PlayerData);
                NetClient.SendMessage(OutMsg, NetDeliveryMethod.UnreliableSequenced);
            }
        }
Exemple #3
0
        public static void OnTick(object sender, ElapsedEventArgs e)
        {
            NetIncomingMessage Msg;
            while ((Msg = NetServer.ReadMessage()) != null)
            {
                switch (Msg.MessageType)
                {
                    case NetIncomingMessageType.ConnectionApproval:
                        int Version = Msg.ReadInt32();
                        if (Version == Shared.Settings.NetworkVersion)
                        {
                            Msg.SenderConnection.Approve();
                        }
                        else
                        {
                            Msg.SenderConnection.Deny();
                        }
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)Msg.ReadByte();
                        switch (status)
                        {
                            case NetConnectionStatus.Connected:
                                {
                                    Player Player = new Player();
                                    Player.NetConnection = Msg.SenderConnection;
                                    PlayersController.Add(Player);
                                    NetOutgoingMessage OutMsg = NetServer.CreateMessage();
                                    OutMsg.Write((int)Shared.NetworkMessageType.PlayerConnected);
                                    OutMsg.Write(Player.ID);
                                    NetServer.SendToAll(OutMsg, Msg.SenderConnection, NetDeliveryMethod.ReliableUnordered, 1);
                                    EventsManager.GetEvent("OnPlayerConnected").Trigger(Jint.Native.JsValue.FromObject(Engine, new Scripting.Natives.Player(Player)));
                                    foreach(var Resource in ResourcesManager.Resources)
                                    {
                                        Resource.SendToClient(Player.NetConnection);
                                    }
                                    break;
                                }
                            case NetConnectionStatus.Disconnected:
                                {
                                    Player Player = PlayersController.GetByNetConnection(Msg.SenderConnection);
                                    PlayersController.Remove(Player);
                                    NetOutgoingMessage OutMsg = NetServer.CreateMessage();
                                    OutMsg.Write((int)Shared.NetworkMessageType.PlayerDisconnected);
                                    OutMsg.Write(Player.ID);
                                    NetServer.SendToAll(OutMsg, Msg.SenderConnection, NetDeliveryMethod.ReliableSequenced, 1);
                                    EventsManager.GetEvent("OnPlayerDisconnected").Trigger(Jint.Native.JsValue.FromObject(Engine, new Scripting.Natives.Player(Player)));
                                    Player = null;
                                    break;
                                }
                        }
                        break;
                    case NetIncomingMessageType.DebugMessage:
                        break;
                    case NetIncomingMessageType.WarningMessage:
                        break;
                    case NetIncomingMessageType.Data:
                        int MsgType = Msg.ReadInt32();
                        switch (MsgType)
                        {
                            case (int)Shared.NetworkMessageType.UpdatePlayer:
                                PlayerUpdateStruct PlayerData = new PlayerUpdateStruct();
                                Msg.ReadAllFields(PlayerData);
                                Player Player = PlayersController.GetByNetConnection(Msg.SenderConnection);
                                Player.Name = PlayerData.Name;
                                Player.Health = PlayerData.Health;
                                Player.Armor = PlayerData.Armor;
                                Vector3 Position = new Vector3();
                                Position.X = PlayerData.Pos_X;
                                Position.Y = PlayerData.Pos_Y;
                                Position.Z = PlayerData.Pos_Z;
                                Vector3 Velocity = new Vector3();
                                Velocity.X = PlayerData.Vel_X;
                                Velocity.Y = PlayerData.Vel_Y;
                                Velocity.Z = PlayerData.Vel_Z;
                                Quaternion Rotation = new Quaternion();
                                Rotation.X = PlayerData.Rot_X;
                                Rotation.Y = PlayerData.Rot_Y;
                                Rotation.Z = PlayerData.Rot_Z;
                                Rotation.W = PlayerData.Rot_A;
                                if (PlayerData.CurrentVehicle > 0)
                                {
                                    Vehicle Vehicle = VehiclesController.GetByID(PlayerData.CurrentVehicle);
                                    Vehicle.Position = Position;
                                    Vehicle.Velocity = Velocity;
                                    Vehicle.Rotation = Rotation;
                                    Vehicle.Heading = PlayerData.Heading;
                                    Vehicle.Speed = PlayerData.Speed;
                                    Vehicle.Driver = Player;
                                    Player.CurrentVehicle = PlayerData.CurrentVehicle;
                                    Player.VehicleSeat = PlayerData.VehicleSeat;
                                }
                                else
                                {
                                    if (Player.CurrentVehicle > 0)
                                    {
                                        Vehicle Vehicle = VehiclesController.GetByID(Player.CurrentVehicle);
                                        Vehicle.Driver = null;
                                        Player.CurrentVehicle = 0;
                                    }
                                    Player.Position = Position;
                                    Player.Velocity = Velocity;
                                    Player.Heading = PlayerData.Heading;
                                    Player.IsWalking = PlayerData.IsWalking;
                                    Player.IsRunning = PlayerData.IsRunning;
                                    Player.IsJumping = PlayerData.IsJumping;
                                    Player.IsCrouching = PlayerData.IsCrouching;
                                    Player.IsGettingIntoVehicle = PlayerData.IsGettingIntoVehicle;
                                    Player.IsGettingOutOfVehicle = PlayerData.IsGettingOutOfVehicle;
                                }

                                // Test Player

                                if (TestPlayer != null)
                                {
                                    TestPlayer.Name = "TestPlayer";
                                    TestPlayer.Health = PlayerData.Health;
                                    TestPlayer.Armor = PlayerData.Armor;
                                    Position.X = Position.X + 5;
                                    if (PlayerData.CurrentVehicle > 0)
                                    {
                                        if (PlayerData.CurrentVehicle == 1)
                                        {
                                            PlayerData.CurrentVehicle = 2;
                                        }
                                        Vehicle Vehicle = VehiclesController.GetByID(PlayerData.CurrentVehicle);
                                        Vehicle.Position = Position;
                                        Vehicle.Velocity = Velocity;
                                        Vehicle.Rotation = Rotation;
                                        Vehicle.Heading = PlayerData.Heading;
                                        Vehicle.Speed = PlayerData.Speed;
                                        Vehicle.Driver = TestPlayer;
                                        TestPlayer.CurrentVehicle = PlayerData.CurrentVehicle;
                                        TestPlayer.VehicleSeat = PlayerData.VehicleSeat;
                                    }
                                    else
                                    {
                                        if (TestPlayer.CurrentVehicle > 0)
                                        {
                                            Vehicle Vehicle = VehiclesController.GetByID(TestPlayer.CurrentVehicle);
                                            Vehicle.Driver = null;
                                            TestPlayer.CurrentVehicle = 0;
                                        }
                                        TestPlayer.Position = Position;
                                        TestPlayer.Velocity = Velocity;
                                        TestPlayer.Heading = PlayerData.Heading;
                                        TestPlayer.IsWalking = PlayerData.IsWalking;
                                        TestPlayer.IsRunning = PlayerData.IsRunning;
                                        TestPlayer.IsJumping = PlayerData.IsJumping;
                                        TestPlayer.IsCrouching = PlayerData.IsCrouching;
                                        TestPlayer.IsGettingIntoVehicle = PlayerData.IsGettingIntoVehicle;
                                        TestPlayer.IsGettingOutOfVehicle = PlayerData.IsGettingOutOfVehicle;
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        Console.WriteLine("Unhandled message type: " + Msg.MessageType);
                        break;
                }
                NetServer.Recycle(Msg);
            }

            UpdateAllPlayers();
            UpdateAllVehicles();
        }