private void SendVehiclePacket(NetworkVehicle netVehicle, NetworkPlayer netPlayer) { Vehicle vehicle = netVehicle.PhysicalVehicle; NetOutgoingMessage outgoingMessage = NetClient.CreateMessage(); outgoingMessage.Write((byte)PacketType.TotalVehicleData); outgoingMessage.Write(netVehicle.ID); outgoingMessage.Write(netPlayer.PlayerID); outgoingMessage.Write(vehicle.Model.Hash); outgoingMessage.Write(vehicle.Position.X); outgoingMessage.Write(vehicle.Position.Y); outgoingMessage.Write(vehicle.Position.Z); outgoingMessage.Write(vehicle.Quaternion.W); outgoingMessage.Write(vehicle.Quaternion.X); outgoingMessage.Write(vehicle.Quaternion.Y); outgoingMessage.Write(vehicle.Quaternion.Z); outgoingMessage.Write((int)vehicle.PrimaryColor); outgoingMessage.Write((int)vehicle.SecondaryColor); outgoingMessage.Write(vehicle.Speed); outgoingMessage.Write(Utility.GetPedSeat(Game.Player.Character)); NetClient.SendMessage(outgoingMessage, NetDeliveryMethod.Unreliable); }
public void API_createVehicle(int vehicleHash, int posX, int posY, int posZ, int rotX, int rotY, int rotZ) { NetworkVehicle networkVehicle = new NetworkVehicle(); networkVehicle.Hash = vehicleHash; networkVehicle.PosX = posX; networkVehicle.PosY = posY; networkVehicle.PosZ = posZ; networkVehicle.RotX = rotX; networkVehicle.RotY = rotY; networkVehicle.RotZ = rotZ; networkVehicle.RotW = 0; networkVehicle.PrimaryColor = 0; networkVehicle.SecondaryColor = 0; Server.RegisterVehicle(networkVehicle); }
public void ReadTotalVehicleDataPacket(NetIncomingMessage netIncomingMessage) { if (GetLocalPlayer() == null) return; int id = netIncomingMessage.ReadInt32(); int playerId = netIncomingMessage.ReadInt32(); int vehicleHash = netIncomingMessage.ReadInt32(); float posX = netIncomingMessage.ReadFloat(); float posY = netIncomingMessage.ReadFloat(); float posZ = netIncomingMessage.ReadFloat(); float rotW = netIncomingMessage.ReadFloat(); float rotX = netIncomingMessage.ReadFloat(); float rotY = netIncomingMessage.ReadFloat(); float rotZ = netIncomingMessage.ReadFloat(); int primaryColor = netIncomingMessage.ReadInt32(); int secondaryColor = netIncomingMessage.ReadInt32(); float speed = netIncomingMessage.ReadFloat(); int seat = netIncomingMessage.ReadInt32(); NetworkVehicle networkVehicle = NetworkVehicle.Exists(Vehicles, id); // Clone debug sync NetworkPlayer networkPlayer = NetworkPlayer.Get(Players, playerId); if(networkPlayer != null && Main.CloneSync) { if(networkPlayer.CloneVehicle == null) { networkPlayer.CloneVehicle = new NetworkVehicle(); networkPlayer.CloneVehicle.PhysicalVehicle = World.CreateVehicle(vehicleHash, new Vector3(posX + 5, posY, posZ)); networkPlayer.CloneVehicle.PhysicalVehicle.Quaternion = new Quaternion(rotX, rotY, rotZ, rotW); networkPlayer.CloneVehicle.PhysicalVehicle.CanTiresBurst = false; networkPlayer.CloneVehicle.PhysicalVehicle.PlaceOnGround(); // Testing ;) networkPlayer.CloneVehicle.PhysicalVehicle.CanBeVisiblyDamaged = false; networkPlayer.CloneVehicle.PhysicalVehicle.EngineRunning = true; networkPlayer.CloneVehicle.PhysicalVehicle.PrimaryColor = (VehicleColor)primaryColor; networkPlayer.CloneVehicle.PhysicalVehicle.SecondaryColor = (VehicleColor)secondaryColor; Main.ChatBox.Add("Created new debug vehicle"); } // Interpolation networkPlayer.VehicleLatestPosition = new Vector3(posX + 5, posY, posZ); DateTime dt = DateTime.Now; dt = dt.AddMilliseconds((float)Rate.VehicleInterpolation); networkPlayer.VehicleLatestPositionTime = dt; networkPlayer.VehiclePreviousPosition = networkPlayer.CloneVehicle.PhysicalVehicle.Position; networkPlayer.CloneVehicle.PhysicalVehicle.Quaternion = new Quaternion(rotX, rotY, rotZ, rotW); if (!Main.Interpolation) { networkPlayer.CloneVehicle.PhysicalVehicle.Position = new Vector3(posX, posY, posZ) + new Vector3(5, 0, 0); networkPlayer.CloneVehicle.PhysicalVehicle.Quaternion = new Quaternion(rotX, rotY, rotZ, rotW); } } // It exists in our list if (networkVehicle == null) { networkVehicle = new NetworkVehicle(); // Client side only! networkVehicle.PhysicalVehicle = World.CreateVehicle(vehicleHash, new Vector3(posX, posY, posZ)); networkVehicle.PhysicalVehicle.Quaternion = new Quaternion(rotX, rotY, rotZ, rotW); networkVehicle.PhysicalVehicle.CanTiresBurst = false; networkVehicle.PhysicalVehicle.PlaceOnGround(); networkVehicle.PhysicalVehicle.NumberPlate = "ID: " + id; Function.Call(Hash.SET_ENTITY_AS_MISSION_ENTITY, networkVehicle.PhysicalVehicle, true, true); // More control for us, less control for Rockstar // Testing ;) networkVehicle.PhysicalVehicle.CanBeVisiblyDamaged = false; networkVehicle.PhysicalVehicle.IsInvincible = true; networkVehicle.PhysicalVehicle.EngineRunning = true; networkVehicle.PhysicalVehicle.PrimaryColor = (VehicleColor)primaryColor; networkVehicle.PhysicalVehicle.SecondaryColor = (VehicleColor)secondaryColor; Vehicles.Add(networkVehicle); Main.ChatBox.Add("(internal) Added new vehicle id: " + id); } // -1 to playerId, or other playerId means someone entered the vehicle! if (networkVehicle.PlayerID != playerId) { NetworkPlayer driver = NetworkPlayer.Get(Players, playerId); if (driver != null) { if(!Main.CloneSync) { driver.Ped?.Task.WarpIntoVehicle(networkVehicle.PhysicalVehicle, (VehicleSeat)seat); // If we don't have a ped, i.e. localplayer } else { driver.Ped.Task.WarpIntoVehicle(driver.CloneVehicle.PhysicalVehicle, (VehicleSeat)seat); } driver.NetVehicle = networkVehicle; Main.ChatBox.Add("(internal) Set driver ped in vehicle: " + networkVehicle.PhysicalVehicle.Model.ToString()); } } if (networkVehicle.PlayerID != GetLocalPlayer().PlayerID) { //var distance = new Vector3(posX, posY, posZ) - networkVehicle.PhysicalVehicle.Position; //distance.Normalize(); networkVehicle.PhysicalVehicle.Position = new Vector3(posX, posY, posZ); networkVehicle.PhysicalVehicle.Quaternion = new Quaternion(rotX, rotY, rotZ, rotW); //if (speed >= 1 && (Switch % 3 == 0)) { // var direction = distance * Math.Abs(speed - networkVehicle.PhysicalVehicle.Speed); // networkVehicle.PhysicalVehicle.ApplyForce(direction); //} } // No driver, so just force the physical vehicle's position to the actual position in order to prevent desync between physical and virtual data if (networkVehicle.PlayerID == -1) { networkVehicle.PhysicalVehicle.Position = new Vector3(posX, posY, posZ); networkVehicle.PhysicalVehicle.Quaternion = new Quaternion(rotX, rotY, rotZ, rotW); } networkVehicle.ID = id; networkVehicle.PlayerID = playerId; networkVehicle.Hash = vehicleHash; networkVehicle.PosX = posX; networkVehicle.PosY = posY; networkVehicle.PosZ = posZ; networkVehicle.RotW = rotW; networkVehicle.RotX = rotX; networkVehicle.RotY = rotY; networkVehicle.RotZ = rotZ; networkVehicle.PrimaryColor = primaryColor; networkVehicle.SecondaryColor = secondaryColor; networkVehicle.Speed = speed; }