コード例 #1
0
ファイル: SyncPed.cs プロジェクト: syaifulnizamyahya/GTACoop
        public void DisplayLocally()
        {
            try {
                const float hRange  = 200f;
                var         gPos    = IsInVehicle ? VehiclePosition : Position;
                var         inRange = Game.Player.Character.IsInRangeOf(gPos, hRange);

                if (inRange && !_isStreamedIn)
                {
                    _isStreamedIn = true;
                    if (_mainBlip != null)
                    {
                        _mainBlip.Remove();
                        _mainBlip = null;
                    }
                }
                else if (!inRange && _isStreamedIn)
                {
                    Clear();
                    _isStreamedIn = false;
                }

                if (!inRange)
                {
                    if (_mainBlip == null && _blip)
                    {
                        _mainBlip       = World.CreateBlip(gPos);
                        _mainBlip.Color = BlipColor.White;
                        _mainBlip.Scale = 0.8f;
                        SetBlipNameFromTextFile(_mainBlip, Name == null ? "<nameless>" : Name);
                    }
                    if (_blip && _mainBlip != null)
                    {
                        _mainBlip.Position = gPos;
                    }
                    return;
                }


                if (Character == null || !Character.Exists() || !Character.IsInRangeOf(gPos, hRange) || Character.Model.Hash != ModelHash || (Character.IsDead && PedHealth > 0))
                {
                    if (Character != null)
                    {
                        Character.Delete();
                    }

                    Character = World.CreatePed(new Model(ModelHash), gPos, Rotation.Z);
                    if (Character == null)
                    {
                        return;
                    }

                    Character.BlockPermanentEvents = true;
                    Character.IsInvincible         = true;
                    Character.CanRagdoll           = false;
                    Character.RelationshipGroup    = _relGroup;
                    if (_blip)
                    {
                        Character.AddBlip();
                        if (Character.CurrentBlip == null)
                        {
                            return;
                        }
                        Character.CurrentBlip.Color = BlipColor.White;
                        Character.CurrentBlip.Scale = 0.8f;
                        SetBlipNameFromTextFile(Character.CurrentBlip, Name);
                    }
                    return;
                }

                if (!Character.IsOccluded && Character.IsInRangeOf(Game.Player.Character.Position, 20f))
                {
                    var oldPos = UI.WorldToScreen(Character.Position + new Vector3(0, 0, 1.5f));
                    if (oldPos.X != 0 && oldPos.Y != 0)
                    {
                        var res = UIMenu.GetScreenResolutionMantainRatio();
                        var pos = new Point((int)((oldPos.X / (float)UI.WIDTH) * res.Width),
                                            (int)((oldPos.Y / (float)UI.HEIGHT) * res.Height));


                        new UIResText(Name == null ? "<nameless>" : Name, pos, 0.3f, Color.WhiteSmoke, Font.ChaletLondon, UIResText.Alignment.Centered)
                        {
                            Outline = true,
                        }.Draw();
                    }
                }

                if ((!_lastVehicle && IsInVehicle && VehicleHash != 0) || (_lastVehicle && IsInVehicle && (MainVehicle == null || !Character.IsInVehicle(MainVehicle) || MainVehicle.Model.Hash != VehicleHash || VehicleSeat != Util.GetPedSeat(Character))))
                {
                    if (MainVehicle != null && Util.IsVehicleEmpty(MainVehicle))
                    {
                        MainVehicle.Delete();
                    }

                    var vehs = World.GetAllVehicles().OrderBy(v =>
                    {
                        if (v == null)
                        {
                            return(float.MaxValue);
                        }
                        return((v.Position - Character.Position).Length());
                    }).ToList();


                    if (vehs.Any() && vehs[0].Model.Hash == VehicleHash && vehs[0].IsInRangeOf(gPos, 3f))
                    {
                        MainVehicle = vehs[0];
                        if (Game.Player.Character.IsInVehicle(MainVehicle) &&
                            VehicleSeat == Util.GetPedSeat(Game.Player.Character))
                        {
                            Game.Player.Character.Task.WarpOutOfVehicle(MainVehicle);
                            UI.Notify("~r~Car jacked!");
                        }
                    }
                    else
                    {
                        MainVehicle = World.CreateVehicle(new Model(VehicleHash), gPos, 0);
                    }

                    if (MainVehicle != null)
                    {
                        MainVehicle.PrimaryColor   = (VehicleColor)VehiclePrimaryColor;
                        MainVehicle.SecondaryColor = (VehicleColor)VehicleSecondaryColor;
                        MainVehicle.Quaternion     = VehicleRotation;
                        MainVehicle.IsInvincible   = true;
                        Character.Task.WarpIntoVehicle(MainVehicle, (VehicleSeat)VehicleSeat);

                        /*if (_playerSeat != -2 && !Game.Player.Character.IsInVehicle(_mainVehicle))
                         * { // TODO: Fix me.
                         *  Game.Player.Character.Task.WarpIntoVehicle(_mainVehicle, (VehicleSeat)_playerSeat);
                         * }*/
                    }

                    _lastVehicle         = true;
                    _justEnteredVeh      = true;
                    _enterVehicleStarted = DateTime.Now;
                    return;
                }

                if (_lastVehicle && _justEnteredVeh && IsInVehicle && !Character.IsInVehicle(MainVehicle) && DateTime.Now.Subtract(_enterVehicleStarted).TotalSeconds <= 4)
                {
                    return;
                }
                _justEnteredVeh = false;

                if (_lastVehicle && !IsInVehicle && MainVehicle != null)
                {
                    if (Character != null)
                    {
                        Character.Task.LeaveVehicle(MainVehicle, true);
                    }
                }

                Character.Health = PedHealth;

                _switch++;
                if (IsInVehicle)
                {
                    if (VehicleSeat == (int)GTA.VehicleSeat.Driver ||
                        MainVehicle.GetPedOnSeat(GTA.VehicleSeat.Driver) == null)
                    {
                        MainVehicle.Health = VehicleHealth;
                        if (MainVehicle.Health <= 0)
                        {
                            MainVehicle.IsInvincible = false;
                            //_mainVehicle.Explode();
                        }
                        else
                        {
                            MainVehicle.IsInvincible = true;
                            if (MainVehicle.IsDead)
                            {
                                MainVehicle.Repair();
                            }
                        }

                        MainVehicle.PrimaryColor   = (VehicleColor)VehiclePrimaryColor;
                        MainVehicle.SecondaryColor = (VehicleColor)VehicleSecondaryColor;

                        if (VehicleMods != null && _modSwitch % 50 == 0 &&
                            Game.Player.Character.IsInRangeOf(VehiclePosition, 30f))
                        {
                            var id = _modSwitch / 50;

                            if (VehicleMods.ContainsKey(id) && VehicleMods[id] != MainVehicle.GetMod((VehicleMod)id))
                            {
                                Function.Call(Hash.SET_VEHICLE_MOD_KIT, MainVehicle.Handle, 0);
                                MainVehicle.SetMod((VehicleMod)id, VehicleMods[id], false);
                                Function.Call(Hash.RELEASE_PRELOAD_MODS, id);
                            }
                        }
                        _modSwitch++;

                        if (_modSwitch >= 2500)
                        {
                            _modSwitch = 0;
                        }

                        if (IsHornPressed && !_lastHorn)
                        {
                            _lastHorn = true;
                            MainVehicle.SoundHorn(99999);
                        }

                        if (!IsHornPressed && _lastHorn)
                        {
                            _lastHorn = false;
                            MainVehicle.SoundHorn(1);
                        }

                        if (MainVehicle.SirenActive && !Siren)
                        {
                            MainVehicle.SirenActive = Siren;
                        }
                        else if (!MainVehicle.SirenActive && Siren)
                        {
                            MainVehicle.SirenActive = Siren;
                        }

                        var dir = VehiclePosition - _lastVehiclePos;

                        dir.Normalize();

                        var range = Math.Max(20f, Speed * Math.Ceiling(DateTime.Now.Subtract(LastUpdateReceived).TotalSeconds));

                        if (MainVehicle.IsInRangeOf(VehiclePosition, (float)range))
                        {
                            var timeElapsed  = (float)DateTime.Now.Subtract(LastUpdateReceived).TotalSeconds;
                            var acceleration = Speed - _lastSpeed;
                            MainVehicle.Position = _lastVehiclePos + dir * (Speed * timeElapsed) +
                                                   dir * (0.5f * acceleration * (float)Math.Pow(timeElapsed, 2));
                        }
                        else
                        {
                            MainVehicle.Position = VehiclePosition;
                            _lastVehiclePos      = VehiclePosition - (dir * 0.5f);
                        }
                    #if DEBUG
                        if (MainVehicle.Heading < 270)
                        {
                            MainVehicle.Quaternion = Util.LerpQuaternion(MainVehicle.Quaternion, VehicleRotation, 0.1f);
                        }
                        else if (MainVehicle.Heading >= 270)
                        {
                            MainVehicle.Quaternion = Util.LerpQuaternion(VehicleRotation, MainVehicle.Quaternion, 0.1f);
                        }
                    #else
                        MainVehicle.Quaternion = VehicleRotation;
                    #endif
                    }
                }
                else
                {
                    if (PedProps != null && _clothSwitch % 50 == 0 && Game.Player.Character.IsInRangeOf(Position, 30f))
                    {
                        var id = _clothSwitch / 50;

                        if (PedProps.ContainsKey(id) &&
                            PedProps[id] != Function.Call <int>(Hash.GET_PED_DRAWABLE_VARIATION, Character.Handle, id))
                        {
                            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, Character.Handle, id, PedProps[id], 0, 0);
                        }
                    }

                    _clothSwitch++;
                    if (_clothSwitch >= 750)
                    {
                        _clothSwitch = 0;
                    }

                    if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeapon)
                    {
                        var wep = Character.Weapons.Give((WeaponHash)CurrentWeapon, 9999, true, true);
                        Character.Weapons.Select(wep);
                    }

                    if (!_lastJumping && IsJumping)
                    {
                        Character.Task.Jump();
                    }

                    if (IsParachuteOpen)
                    {
                        if (_parachuteProp == null)
                        {
                            _parachuteProp = World.CreateProp(new Model(1740193300), Character.Position,
                                                              Character.Rotation, false, false);
                            _parachuteProp.FreezePosition = true;
                            Function.Call(Hash.SET_ENTITY_COLLISION, _parachuteProp.Handle, false, 0);
                        }
                        Character.FreezePosition  = true;
                        Character.Position        = Position - new Vector3(0, 0, 1);
                        Character.Quaternion      = Rotation;
                        _parachuteProp.Position   = Character.Position + new Vector3(0, 0, 3.7f);
                        _parachuteProp.Quaternion = Character.Quaternion;

                        Character.Task.PlayAnimation("skydive@parachute@first_person", "chute_idle_right", 8f, 5000,
                                                     false, 8f);
                    }
                    else
                    {
                        var dest = Position;
                        Character.FreezePosition = false;

                        if (_parachuteProp != null)
                        {
                            _parachuteProp.Delete();
                            _parachuteProp = null;
                        }

                        const int threshold = 50;
                        if (IsAiming && !IsShooting && !Character.IsInRangeOf(Position, 0.5f) && _switch % threshold == 0)
                        {
                            Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, dest.X, dest.Y,
                                          dest.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, 2f, 0, 0x3F000000, 0x40800000, 1, 512, 0,
                                          (uint)FiringPattern.FullAuto);
                        }
                        else if (IsAiming && !IsShooting && Character.IsInRangeOf(Position, 0.5f))
                        {
                            Character.Task.AimAt(AimCoords, 100);
                        }

                        if (!Character.IsInRangeOf(Position, 0.5f) &&
                            ((IsShooting && !_lastShooting) ||
                             (IsShooting && _lastShooting && _switch % (threshold * 2) == 0)))
                        {
                            Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, dest.X, dest.Y,
                                          dest.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, 2f, 1, 0x3F000000, 0x40800000, 1, 0, 0,
                                          (uint)FiringPattern.FullAuto);
                        }
                        else if ((IsShooting && !_lastShooting) ||
                                 (IsShooting && _lastShooting && _switch % (threshold / 2) == 0))
                        {
                            Function.Call(Hash.TASK_SHOOT_AT_COORD, Character.Handle, AimCoords.X, AimCoords.Y,
                                          AimCoords.Z, 1500, (uint)FiringPattern.FullAuto);
                        }

                        if (!IsAiming && !IsShooting && !IsJumping)
                        {
                            switch (SyncMode)
                            {
                            case SynchronizationMode.Tasks:
                                if (!Character.IsInRangeOf(Position, 0.5f))
                                {
                                    Character.Task.RunTo(Position, true, 500);
                                    //var targetAngle = Rotation.Z/Math.Sqrt(1 - Rotation.W*Rotation.W);
                                    //Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character.Handle, Position.X, Position.Y, Position.Z, 5f, 3000, targetAngle, 0);
                                }
                                if (!Character.IsInRangeOf(Position, 5f))
                                {
                                    Character.Position   = dest - new Vector3(0, 0, 1f);
                                    Character.Quaternion = Rotation;
                                }
                                break;

                            case SynchronizationMode.Teleport:
                                Character.Position   = dest - new Vector3(0, 0, 1f);
                                Character.Quaternion = Rotation;
                                break;
                            }
                        }
                    }
                    _lastJumping  = IsJumping;
                    _lastShooting = IsShooting;
                    _lastAiming   = IsAiming;
                }
                _lastVehicle = IsInVehicle;
            }
            catch (Exception ex)
            {
                UI.Notify("Sync error: " + ex.Message);
            }
        }
コード例 #2
0
ファイル: SyncPed.cs プロジェクト: oldnapalm/GTACoOp
        public void DisplayLocally()
        {
            try
            {
                var   isPlane = Function.Call <bool>(Hash.IS_THIS_MODEL_A_PLANE, VehicleHash);
                float hRange  = isPlane ? 1200f : 400f;

                var gPos    = IsInVehicle ? VehiclePosition : Position;
                var inRange = isPlane ? true : Game.Player.Character.IsInRangeOf(gPos, hRange);

                if (inRange && !_isStreamedIn)
                {
                    _isStreamedIn = true;
                    if (_mainBlip != null)
                    {
                        _mainBlip.Remove();
                        _mainBlip = null;
                    }
                }
                else if (!inRange && _isStreamedIn)
                {
                    Clear();
                    _isStreamedIn = false;
                }

                if (!inRange)
                {
                    if (_mainBlip == null && _blip)
                    {
                        _mainBlip       = World.CreateBlip(gPos);
                        _mainBlip.Color = BlipColor.White;
                        _mainBlip.Scale = 0.8f;
                        SetBlipName(_mainBlip, Name == null ? "<nameless>" : Name);
                    }
                    if (_blip && _mainBlip != null)
                    {
                        _mainBlip.Position = gPos;
                    }
                    return;
                }


                if (Character == null || !Character.Exists() || (!Character.IsInRangeOf(gPos, hRange) && Game.GameTime - LastUpdateReceived < 5000) || Character.Model.Hash != ModelHash || (Character.IsDead && PedHealth > 0))
                {
                    if (Character != null)
                    {
                        Character.Delete();
                    }

                    Character = World.CreatePed(new Model(ModelHash), gPos, Rotation.Z);
                    if (Character == null)
                    {
                        return;
                    }

                    Character.BlockPermanentEvents = true;
                    Character.IsInvincible         = true;
                    Character.CanRagdoll           = false;
                    Character.RelationshipGroup    = _relGroup;
                    if (_blip)
                    {
                        Character.AddBlip();
                        if (Character.CurrentBlip == null)
                        {
                            return;
                        }
                        Character.CurrentBlip.Color = BlipColor.White;
                        Character.CurrentBlip.Scale = 0.8f;
                        SetBlipName(Character.CurrentBlip, Name);
                    }

                    if (PedProps != null)
                    {
                        foreach (KeyValuePair <int, int> pedprop in PedProps)
                        {
                            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, Character.Handle, pedprop.Key, pedprop.Value, 0, 0);
                        }
                    }

                    return;
                }

                if (!Character.IsOccluded && Character.IsInRangeOf(Game.Player.Character.Position, 20f))
                {
                    Vector3 targetPos = Character.GetBoneCoord(Bone.IK_Head) + new Vector3(0, 0, 0.5f);

                    targetPos += Character.Velocity / Game.FPS;

                    Function.Call(Hash.SET_DRAW_ORIGIN, targetPos.X, targetPos.Y, targetPos.Z, 0);

                    float sizeOffset = Math.Max(1f - ((GameplayCamera.Position - Character.Position).Length() / 30f), 0.3f);

                    new UIResText(Name ?? "<Nameless>", new Point(0, 0), 0.4f * sizeOffset, Color.WhiteSmoke, Font.ChaletLondon, UIResText.Alignment.Centered)
                    {
                        Outline = true,
                    }.Draw();

                    Function.Call(Hash.CLEAR_DRAW_ORIGIN);
                }

                if ((!_lastVehicle && IsInVehicle && VehicleHash != 0) || (_lastVehicle && IsInVehicle && (MainVehicle == null || !Character.IsInVehicle(MainVehicle) || MainVehicle.Model.Hash != VehicleHash || VehicleSeat != Util.GetPedSeat(Character))))
                {
                    if (MainVehicle != null && Util.IsVehicleEmpty(MainVehicle))
                    {
                        MainVehicle.Position = MainVehicle.GetOffsetInWorldCoords(new Vector3(0, 0, -100));
                        MainVehicle.Delete();
                    }

                    var vehs = World.GetAllVehicles().OrderBy(v =>
                    {
                        if (v == null)
                        {
                            return(float.MaxValue);
                        }
                        return((v.Position - Character.Position).Length());
                    }).ToList();


                    if (vehs.Any() && vehs[0].Model.Hash == VehicleHash && vehs[0].IsInRangeOf(gPos, 3f))
                    {
                        MainVehicle = vehs[0];

                        /*if (Game.Player.Character.IsInVehicle(MainVehicle) &&
                         *  VehicleSeat == Util.GetPedSeat(Game.Player.Character))
                         * {
                         *  Game.Player.Character.Task.WarpOutOfVehicle(MainVehicle);
                         *  UI.Notify("~r~Car jacked!");
                         * }*/
                    }
                    else
                    {
                        MainVehicle = World.CreateVehicle(new Model(VehicleHash), gPos, 0);
                    }

                    if (MainVehicle != null)
                    {
                        Function.Call(Hash.SET_VEHICLE_COLOURS, MainVehicle, VehiclePrimaryColor, VehicleSecondaryColor);
                        MainVehicle.Livery = Livery;

                        MainVehicle.Quaternion   = VehicleRotation;
                        MainVehicle.IsInvincible = true;
                        Character.SetIntoVehicle(MainVehicle, (VehicleSeat)VehicleSeat);

                        /*if (_playerSeat != -2 && !Game.Player.Character.IsInVehicle(_mainVehicle))
                         * { // TODO: Fix me.
                         *  Game.Player.Character.Task.WarpIntoVehicle(_mainVehicle, (VehicleSeat)_playerSeat);
                         * }*/
                    }

                    _lastVehicle         = true;
                    _justEnteredVeh      = true;
                    _enterVehicleStarted = DateTime.Now;
                    return;
                }

                if (_lastVehicle && _justEnteredVeh && IsInVehicle && !Character.IsInVehicle(MainVehicle) && DateTime.Now.Subtract(_enterVehicleStarted).TotalSeconds <= 4)
                {
                    return;
                }
                _justEnteredVeh = false;

                if (_lastVehicle && !IsInVehicle && MainVehicle != null)
                {
                    if (Character != null)
                    {
                        Character.Task.LeaveVehicle(MainVehicle, true);
                    }
                }

                Character.Health = PedHealth;

                _switch++;

                if (!inRange)
                {
                    if (Character != null && Game.GameTime - LastUpdateReceived < 10000)
                    {
                        if (!IsInVehicle)
                        {
                            Character.PositionNoOffset = gPos;
                        }
                        else if (MainVehicle != null && GetResponsiblePed(MainVehicle).Handle == Character.Handle)
                        {
                            MainVehicle.Position   = VehiclePosition;
                            MainVehicle.Quaternion = VehicleRotation;
                        }
                    }
                    return;
                }

                if (IsInVehicle)
                {
                    if (GetResponsiblePed(MainVehicle).Handle == Character.Handle)
                    {
                        MainVehicle.Health = VehicleHealth;
                        if (MainVehicle.Health <= 0)
                        {
                            MainVehicle.IsInvincible = false;
                            //_mainVehicle.Explode();
                        }
                        else
                        {
                            MainVehicle.IsInvincible = true;
                            if (MainVehicle.IsDead)
                            {
                                MainVehicle.Repair();
                            }
                        }

                        MainVehicle.EngineRunning = IsEngineRunning;

                        if (Plate != null)
                        {
                            MainVehicle.NumberPlate = Plate;
                        }

                        var radioStations = Util.GetRadioStations();

                        if (radioStations?.ElementAtOrDefault(RadioStation) != null)
                        {
                            Function.Call(Hash.SET_VEH_RADIO_STATION, radioStations[RadioStation]);
                        }

                        if (VehicleMods != null && _modSwitch % 50 == 0 &&
                            Game.Player.Character.IsInRangeOf(VehiclePosition, 30f))
                        {
                            var id = _modSwitch / 50;

                            if (VehicleMods.ContainsKey(id) && VehicleMods[id] != MainVehicle.GetMod((VehicleMod)id))
                            {
                                Function.Call(Hash.SET_VEHICLE_MOD_KIT, MainVehicle.Handle, 0);
                                MainVehicle.SetMod((VehicleMod)id, VehicleMods[id], false);
                                Function.Call(Hash.RELEASE_PRELOAD_MODS, id);
                            }
                        }
                        _modSwitch++;

                        if (_modSwitch >= 2500)
                        {
                            _modSwitch = 0;
                        }

                        if (IsHornPressed && !_lastHorn)
                        {
                            _lastHorn = true;
                            MainVehicle.SoundHorn(99999);
                        }

                        if (!IsHornPressed && _lastHorn)
                        {
                            _lastHorn = false;
                            MainVehicle.SoundHorn(1);
                        }

                        if (IsInBurnout && !_lastBurnout)
                        {
                            Function.Call(Hash.SET_VEHICLE_BURNOUT, MainVehicle, true);
                            Function.Call(Hash.TASK_VEHICLE_TEMP_ACTION, Character, MainVehicle, 23, 120000); // 30 - burnout
                        }
                        else if (!IsInBurnout && _lastBurnout)
                        {
                            Function.Call(Hash.SET_VEHICLE_BURNOUT, MainVehicle, false);
                            Character.Task.ClearAll();
                        }

                        _lastBurnout = IsInBurnout;

                        Function.Call(Hash.SET_VEHICLE_BRAKE_LIGHTS, MainVehicle, Speed > 0.2 && _lastSpeed > Speed);

                        if (MainVehicle.SirenActive && !Siren)
                        {
                            MainVehicle.SirenActive = Siren;
                        }
                        else if (!MainVehicle.SirenActive && Siren)
                        {
                            MainVehicle.SirenActive = Siren;
                        }

                        MainVehicle.LightsOn    = LightsOn;
                        MainVehicle.HighBeamsOn = HighBeamsOn;
                        MainVehicle.SirenActive = Siren;
                        if (Steering != MainVehicle.SteeringAngle)
                        {
                            Util.CustomSteeringAngle(MainVehicle.Handle, (float)(Math.PI / 180) * Steering);
                        }
                        Function.Call(Hash.SET_VEHICLE_LIVERY, MainVehicle, Livery);

                        Function.Call(Hash.SET_VEHICLE_COLOURS, MainVehicle, VehiclePrimaryColor, VehicleSecondaryColor);

                        if (MainVehicle.Model.IsPlane && LandingGear != MainVehicle.LandingGear)
                        {
                            MainVehicle.LandingGear = LandingGear;
                        }

                        if (Character.IsOnBike && MainVehicle.ClassType == VehicleClass.Cycles)
                        {
                            var isPedaling     = IsPedaling(false);
                            var isFastPedaling = IsPedaling(true);
                            if (Speed < 2f)
                            {
                                if (isPedaling)
                                {
                                    StopPedalingAnim(false);
                                }
                                else if (isFastPedaling)
                                {
                                    StopPedalingAnim(true);
                                }
                            }
                            else if (Speed < 11f && !isPedaling)
                            {
                                StartPedalingAnim(false);
                            }
                            else if (Speed >= 11f && !isFastPedaling)
                            {
                                StartPedalingAnim(true);
                            }
                        }

                        if ((Speed > 0.2f || IsInBurnout) && MainVehicle.IsInRangeOf(VehiclePosition, 7.0f))
                        {
                            MainVehicle.Velocity = VehicleVelocity + (VehiclePosition - MainVehicle.Position);

                            MainVehicle.Quaternion = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f);

                            _stopTime = Game.GameTime;
                        }
                        else if ((Game.GameTime - _stopTime) <= 1000)
                        {
                            Vector3 posTarget = Util.LinearVectorLerp(MainVehicle.Position, VehiclePosition + (VehiclePosition - MainVehicle.Position), (Game.GameTime - _stopTime), 1000);
                            MainVehicle.PositionNoOffset = posTarget;
                            MainVehicle.Quaternion       = Quaternion.Slerp(MainVehicle.Quaternion, VehicleRotation, 0.5f);
                        }
                        else
                        {
                            MainVehicle.PositionNoOffset = VehiclePosition;
                            MainVehicle.Quaternion       = VehicleRotation;
                        }
                    }
                }
                else
                {
                    if (PedProps != null && _clothSwitch % 50 == 0 && Game.Player.Character.IsInRangeOf(Position, 30f))
                    {
                        var id = _clothSwitch / 50;

                        if (PedProps.ContainsKey(id) &&
                            PedProps[id] != Function.Call <int>(Hash.GET_PED_DRAWABLE_VARIATION, Character.Handle, id))
                        {
                            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, Character.Handle, id, PedProps[id], 0, 0);
                        }
                    }

                    _clothSwitch++;
                    if (_clothSwitch >= 750)
                    {
                        _clothSwitch = 0;
                    }

                    if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeapon)
                    {
                        var wep = Character.Weapons.Give((WeaponHash)CurrentWeapon, 9999, true, true);
                        Character.Weapons.Select(wep);
                    }

                    if (!_lastJumping && IsJumping)
                    {
                        Character.Task.Jump();
                    }

                    if (IsParachuteOpen)
                    {
                        if (_parachuteProp == null)
                        {
                            _parachuteProp = World.CreateProp(new Model(1740193300), Character.Position,
                                                              Character.Rotation, false, false);
                            _parachuteProp.FreezePosition = true;
                            Function.Call(Hash.SET_ENTITY_COLLISION, _parachuteProp.Handle, false, 0);
                        }
                        Character.FreezePosition  = true;
                        Character.Position        = Position - new Vector3(0, 0, 1);
                        Character.Quaternion      = Rotation;
                        _parachuteProp.Position   = Character.Position + new Vector3(0, 0, 3.7f);
                        _parachuteProp.Quaternion = Character.Quaternion;

                        Character.Task.PlayAnimation("skydive@parachute@first_person", "chute_idle_right", 8f, 5000,
                                                     false, 8f);
                    }
                    else
                    {
                        var dest = Position;
                        Character.FreezePosition = false;

                        if (_parachuteProp != null)
                        {
                            _parachuteProp.Delete();
                            _parachuteProp = null;
                        }

                        const int threshold = 50;
                        if (IsAiming && !IsShooting && !Character.IsInRangeOf(Position, 0.5f) && _switch % threshold == 0)
                        {
                            Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, dest.X, dest.Y,
                                          dest.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, 2f, 0, 0x3F000000, 0x40800000, 1, 512, 0,
                                          (uint)FiringPattern.FullAuto);
                        }
                        else if (IsAiming && !IsShooting && Character.IsInRangeOf(Position, 0.5f))
                        {
                            Character.Task.AimAt(AimCoords, 100);
                        }

                        if (!Character.IsInRangeOf(Position, 0.5f) &&
                            ((IsShooting && !_lastShooting) ||
                             (IsShooting && _lastShooting && _switch % (threshold * 2) == 0)))
                        {
                            Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, dest.X, dest.Y,
                                          dest.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, 2f, 1, 0x3F000000, 0x40800000, 1, 0, 0,
                                          (uint)FiringPattern.FullAuto);
                        }
                        else if ((IsShooting && !_lastShooting) ||
                                 (IsShooting && _lastShooting && _switch % (threshold / 2) == 0))
                        {
                            Function.Call(Hash.TASK_SHOOT_AT_COORD, Character.Handle, AimCoords.X, AimCoords.Y,
                                          AimCoords.Z, 1500, (uint)FiringPattern.FullAuto);
                        }

                        if (!IsAiming && !IsShooting && !IsJumping && !IsInParachuteFreeFall)
                        {
                            float distance = Character.Position.DistanceTo(Position);
                            if (distance <= 0.15f || distance > 7.0f) // Still or too far away
                            {
                                if (distance > 7.0f)
                                {
                                    Character.Position   = dest - new Vector3(0, 0, 1f);
                                    Character.Quaternion = Rotation;
                                }
                            }
                            else if (distance <= 1.25f) // Walking
                            {
                                Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character, Position.X, Position.Y, Position.Z, 1.0f, -1, Character.Heading, 0.0f);
                                Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character, 1.0f);
                            }
                            else if (distance > 1.75f) // Sprinting
                            {
                                Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character, Position.X, Position.Y, Position.Z, 3.0f, -1, Character.Heading, 2.0f);
                                Function.Call(Hash.SET_RUN_SPRINT_MULTIPLIER_FOR_PLAYER, Character, 1.49f);
                                Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character, 3.0f);
                            }
                            else // Running
                            {
                                Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character, Position.X, Position.Y, Position.Z, 4.0f, -1, Character.Heading, 1.0f);
                                Function.Call(Hash.SET_PED_DESIRED_MOVE_BLEND_RATIO, Character, 2.0f);
                            }
                        }

                        if (IsInParachuteFreeFall)
                        {
                            if (!Function.Call <bool>(Hash.IS_PED_IN_PARACHUTE_FREE_FALL, Character))
                            {
                                Function.Call(Hash.TASK_SKY_DIVE, Character);
                            }
                            Character.Position   = dest - new Vector3(0, 0, 1f);
                            Character.Quaternion = Rotation;
                        }
                    }
                    _lastJumping  = IsJumping;
                    _lastShooting = IsShooting;
                    _lastAiming   = IsAiming;
                }
                _lastVehicle = IsInVehicle;
            }
            catch (Exception ex)
            {
                UI.Notify("Sync error: " + ex.Message);
                Main.Logger.WriteException("Exception in SyncPed code", ex);
            }
        }
コード例 #3
0
ファイル: SyncPed.cs プロジェクト: Mercurial/GTACoop
        public void DisplayLocally()
        {
            var gPos    = IsInVehicle ? VehiclePosition : Position;
            var inRange = Game.Player.Character.IsInRangeOf(gPos, 100f);



            if (inRange && !_isStreamedIn)
            {
                _isStreamedIn = true;
                _mainBlip?.Remove();
            }
            else if (!inRange && _isStreamedIn)
            {
                Clear();
                _isStreamedIn = false;
            }

            if (!inRange)
            {
                if (_mainBlip == null && _blip)
                {
                    _mainBlip       = World.CreateBlip(gPos);
                    _mainBlip.Color = BlipColor.White;
                    _mainBlip.Scale = 0.8f;
                    SetBlipNameFromTextFile(_mainBlip, Name);
                }
                if (_blip && _mainBlip != null)
                {
                    _mainBlip.Position = gPos;
                }
                return;
            }


            if (Character == null || !Character.Exists() || Character.Model.Hash != ModelHash || (Character.IsDead && PedHealth > 0))
            {
                if (Character != null)
                {
                    Character.Delete();
                }
                Character = World.CreatePed(new Model(ModelHash), Position, Rotation.Z);
                if (Character == null)
                {
                    return;
                }

                Character.BlockPermanentEvents = true;
                Character.IsInvincible         = true;
                Character.CanRagdoll           = false;
                Character.RelationshipGroup    = _relGroup;
                if (_blip)
                {
                    Character.AddBlip();
                    if (Character.CurrentBlip == null)
                    {
                        return;
                    }
                    Character.CurrentBlip.Color = BlipColor.White;
                    Character.CurrentBlip.Scale = 0.8f;
                    SetBlipNameFromTextFile(Character.CurrentBlip, Name);
                }
                return;
            }

            if (!Character.IsOccluded && Character.IsInRangeOf(Game.Player.Character.Position, 20f))
            {
                var oldPos = UI.WorldToScreen(Character.Position + new Vector3(0, 0, 1.5f));
                if (oldPos.X != 0 && oldPos.Y != 0)
                {
                    var res = UIMenu.GetScreenResolutionMantainRatio();
                    var pos = new Point((int)((oldPos.X / (float)UI.WIDTH) * res.Width),
                                        (int)((oldPos.Y / (float)UI.HEIGHT) * res.Height));


                    new UIResText(Name, pos, 0.3f, Color.WhiteSmoke, Font.ChaletLondon, UIResText.Alignment.Centered)
                    {
                        Outline = true,
                    }.Draw();
                } //*/
            }

            if ((!_lastVehicle && IsInVehicle && VehicleHash != 0) || _mainVehicle == null || !Character.IsInVehicle(_mainVehicle) || _mainVehicle.Model.Hash != VehicleHash || VehicleSeat != Util.GetPedSeat(Character))
            {
                if (_mainVehicle != null && Util.IsVehicleEmpty(_mainVehicle))
                {
                    _mainVehicle.Delete();
                }

                var vehs = World.GetAllVehicles().OrderBy(v =>
                {
                    if (v == null)
                    {
                        return(float.MaxValue);
                    }
                    return((v.Position - Character.Position).Length());
                }).ToList();


                if (vehs.Any() && vehs[0].Model.Hash == VehicleHash)
                {
                    _mainVehicle = vehs[0];
                }
                else
                {
                    _mainVehicle = World.CreateVehicle(new Model(VehicleHash), VehiclePosition, 0);
                }

                if (_mainVehicle != null)
                {
                    _mainVehicle.PrimaryColor   = (VehicleColor)VehiclePrimaryColor;
                    _mainVehicle.SecondaryColor = (VehicleColor)VehicleSecondaryColor;
                    _mainVehicle.Quaternion     = VehicleRotation;
                    _mainVehicle.IsInvincible   = true;
                    Character.Task.WarpIntoVehicle(_mainVehicle, (VehicleSeat)VehicleSeat);
                }

                _lastVehicle         = true;
                _justEnteredVeh      = true;
                _enterVehicleStarted = DateTime.Now;
                return;
            }

            if (_lastVehicle && _justEnteredVeh && IsInVehicle && !Character.IsInVehicle(_mainVehicle) && DateTime.Now.Subtract(_enterVehicleStarted).TotalSeconds <= 4)
            {
                return;
            }
            _justEnteredVeh = false;

            if (_lastVehicle && !IsInVehicle && _mainVehicle != null)
            {
                if (Character != null)
                {
                    Character.Task.LeaveVehicle(_mainVehicle, true);
                }
            }

            Character.Health = PedHealth;

            _switch++;
            if (IsInVehicle)
            {
                if (VehicleSeat == (int)GTA.VehicleSeat.Driver ||
                    _mainVehicle.GetPedOnSeat(GTA.VehicleSeat.Driver) == null)
                {
                    _mainVehicle.Health = VehicleHealth;
                    if (_mainVehicle.Health <= 0)
                    {
                        _mainVehicle.IsInvincible = false;
                        //_mainVehicle.Explode();
                    }
                    else
                    {
                        _mainVehicle.IsInvincible = true;
                        if (_mainVehicle.IsDead)
                        {
                            _mainVehicle.Repair();
                        }
                    }

                    _mainVehicle.PrimaryColor   = (VehicleColor)VehiclePrimaryColor;
                    _mainVehicle.SecondaryColor = (VehicleColor)VehicleSecondaryColor;

                    if (VehicleMods != null && _modSwitch % 50 == 0 && Game.Player.Character.IsInRangeOf(VehiclePosition, 30f))
                    {
                        var id = _modSwitch / 50;

                        if (VehicleMods.ContainsKey(id) && VehicleMods[id] != _mainVehicle.GetMod((VehicleMod)id))
                        {
                            Function.Call(Hash.SET_VEHICLE_MOD_KIT, _mainVehicle.Handle, 0);
                            _mainVehicle.SetMod((VehicleMod)id, VehicleMods[id], false);
                            Function.Call(Hash.RELEASE_PRELOAD_MODS, id);
                        }
                    }
                    _modSwitch++;

                    if (_modSwitch >= 2500)
                    {
                        _modSwitch = 0;
                    }


                    if (IsHornPressed && DateTime.Now.Subtract(_lastHornPress).TotalMilliseconds > 1500)
                    {
                        _mainVehicle.SoundHorn(1500);
                        _lastHornPress = DateTime.Now;
                    }

                    if (_mainVehicle.SirenActive && !Siren)
                    {
                        _mainVehicle.SirenActive = Siren;
                    }
                    else if (!_mainVehicle.SirenActive && Siren)
                    {
                        _mainVehicle.SirenActive = Siren;
                    }

                    var dir = VehiclePosition - _mainVehicle.Position;
                    dir.Normalize();

                    if (!_mainVehicle.IsInRangeOf(VehiclePosition, 0.08f))
                    {
                        _mainVehicle.ApplyForce(dir);
                    }
                    if (Main.GlobalSyncMode == SynchronizationMode.Teleport && !_mainVehicle.IsInRangeOf(VehiclePosition, 0.8f))
                    {
                        _mainVehicle.Position = VehiclePosition;
                    }
                    if (Main.GlobalSyncMode == SynchronizationMode.Tasks && !_mainVehicle.IsInRangeOf(VehiclePosition, 30f))
                    {
                        _mainVehicle.Position = VehiclePosition;
                    }
                    _mainVehicle.Quaternion = VehicleRotation;
                }
            }
            else
            {
                if (PedProps != null && _clothSwitch % 50 == 0 && Game.Player.Character.IsInRangeOf(Position, 30f))
                {
                    var id = _clothSwitch / 50;

                    if (PedProps.ContainsKey(id) && PedProps[id] != Function.Call <int>(Hash.GET_PED_DRAWABLE_VARIATION, Character.Handle, id))
                    {
                        Function.Call(Hash.SET_PED_COMPONENT_VARIATION, Character.Handle, id, PedProps[id], 0, 0);
                    }
                }

                _clothSwitch++;
                if (_clothSwitch >= 750)
                {
                    _clothSwitch = 0;
                }

                if (Character.Weapons.Current.Hash != (WeaponHash)CurrentWeapon)
                {
                    var wep = Character.Weapons.Give((WeaponHash)CurrentWeapon, 9999, true, true);
                    Character.Weapons.Select(wep);
                }

                if (!_lastJumping && IsJumping)
                {
                    Character.Task.Jump();
                }

                var dest = Position;

                const int threshold = 50;
                if (IsAiming && !IsShooting && !Character.IsInRangeOf(Position, 0.5f) && _switch % threshold == 0)
                {
                    Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, dest.X, dest.Y,
                                  dest.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, 2f, 0, 0x3F000000, 0x40800000, 1, 512, 0,
                                  (uint)FiringPattern.FullAuto);
                }
                else if (IsAiming && !IsShooting && Character.IsInRangeOf(Position, 0.5f))
                {
                    Character.Task.AimAt(AimCoords, 100);
                }

                if (!Character.IsInRangeOf(Position, 0.5f) &&
                    ((IsShooting && !_lastShooting) || (IsShooting && _lastShooting && _switch % (threshold * 2) == 0)))
                {
                    Function.Call(Hash.TASK_GO_TO_COORD_WHILE_AIMING_AT_COORD, Character.Handle, dest.X, dest.Y,
                                  dest.Z, AimCoords.X, AimCoords.Y, AimCoords.Z, 2f, 1, 0x3F000000, 0x40800000, 1, 0, 0,
                                  (uint)FiringPattern.FullAuto);
                }
                else if ((IsShooting && !_lastShooting) ||
                         (IsShooting && _lastShooting && _switch % (threshold / 2) == 0))
                {
                    Function.Call(Hash.TASK_SHOOT_AT_COORD, Character.Handle, AimCoords.X, AimCoords.Y,
                                  AimCoords.Z, 1500, (uint)FiringPattern.FullAuto);
                }

                if (!IsAiming && !IsShooting && !IsJumping)
                {
                    switch (SyncMode)
                    {
                    case SynchronizationMode.Tasks:
                        if (!Character.IsInRangeOf(Position, 0.5f))
                        {
                            Character.Task.RunTo(Position, true, 500);
                            //var targetAngle = Rotation.Z/Math.Sqrt(1 - Rotation.W*Rotation.W);
                            //Function.Call(Hash.TASK_GO_STRAIGHT_TO_COORD, Character.Handle, Position.X, Position.Y, Position.Z, 5f, 3000, targetAngle, 0);
                        }
                        if (!Character.IsInRangeOf(Position, 5f))
                        {
                            Character.Position   = dest - new Vector3(0, 0, 1f);
                            Character.Quaternion = Rotation;
                        }
                        break;

                    case SynchronizationMode.Teleport:
                        Character.Position   = dest - new Vector3(0, 0, 1f);
                        Character.Quaternion = Rotation;
                        break;
                    }
                }
                _lastJumping  = IsJumping;
                _lastShooting = IsShooting;
                _lastAiming   = IsAiming;
            }
            _lastVehicle = IsInVehicle;
        }