Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTA;
- using GTA.Math;
- using GTA.Native;
- using System;
- using System.Windows.Forms;
- public class Winch : Script
- {
- Rope rope;
- int windingsound;
- public Winch()
- {
- Tick += OnTick;
- KeyDown += OnKeyDown;
- KeyUp += OnKeyUp;
- }
- bool winding = false;
- void OnTick(object sender, EventArgs e)
- {
- if (Game.IsControlPressed(2, GTA.Control.VehicleHandbrake))
- {
- if (Game.IsControlJustPressed(2, GTA.Control.VehicleMoveUpOnly))
- {
- if (Game.Player.IsAiming)
- {
- AimedHook();
- }
- else
- {
- HookNearbyCar(true);
- }
- }
- if (Game.IsControlJustPressed(2, GTA.Control.VehicleMoveDownOnly))
- {
- if (Game.Player.IsAiming)
- {
- AimedHook();
- }
- else
- {
- HookNearbyCar(false);
- }
- }
- if (rope != null && rope.Exists())
- {
- if (Game.IsControlJustPressed(2, GTA.Control.VehicleBrake))
- {
- if (!winding)
- {
- Function.Call(Hash.START_ROPE_WINDING, rope); //start wind
- winding = true;
- DisplayHelpTextThisFrame("Winding started");
- windingsound = Audio.PlaySoundFrontend("OPENED", "DOOR_GARAGE");
- }
- else
- {
- Function.Call(Hash.STOP_ROPE_WINDING, rope);
- Function.Call(Hash.STOP_ROPE_UNWINDING_FRONT, rope);
- winding = false;
- DisplayHelpTextThisFrame("Winding stopped");
- //Audio.StopSound(windingsound);
- }
- }
- if (Game.IsControlJustPressed(2, GTA.Control.VehicleAccelerate))
- {
- if (!winding)
- {
- Function.Call(Hash.START_ROPE_UNWINDING_FRONT, rope); //start unwind
- winding = true;
- DisplayHelpTextThisFrame("Unwinding started");
- windingsound = Audio.PlaySoundFrontend("Truck_Ramp_Raise", "BIG_SCORE_3A_SOUNDS");
- }
- else
- {
- Function.Call(Hash.STOP_ROPE_WINDING, rope);
- Function.Call(Hash.STOP_ROPE_UNWINDING_FRONT, rope);
- winding = false;
- DisplayHelpTextThisFrame("Unwinding stopped");
- Audio.StopSound(windingsound);
- }
- }
- }
- }
- if (Game.Player.Character.IsSittingInVehicle() && !Game.IsControlPressed(2, GTA.Control.VehicleHandbrake) && rope != null && rope.Exists())
- {
- if (Game.IsControlPressed(2, GTA.Control.VehicleAccelerate) && GetLastVehicle(Game.Player.Character).Speed < 2f)
- {
- Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, 0f, 0.5f, 0f, 0f, 0f, 0f, 0, true, true, true, true, true);
- }
- if (Game.IsControlPressed(2, GTA.Control.VehicleBrake) && GetLastVehicle(Game.Player.Character).Speed < 2f)
- {
- Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, 0f, -0.5f, 0f, 0f, 0f, 0f, 0, true, true, true, true, true);
- }
- if (GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_THIS_MODEL_A_HELI, GetLastVehicle(Game.Player.Character).Model))
- {
- if (Game.IsControlPressed(2, GTA.Control.VehicleFlyThrottleUp) && GetLastVehicle(Game.Player.Character).Velocity.Z < 2f)
- {
- Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, 0f, 0f, 1f, 0f, 0f, 0f, 0, true, true, true, true, true);
- }
- }
- }
- }
- void OnKeyDown(object sender, KeyEventArgs e)
- {
- }
- void OnKeyUp(object sender, KeyEventArgs e)
- {
- }
- void HookNearbyCar(bool forward)
- {
- //TOWING_ENGINE_TURNING_MASTER
- // Function.Call(Hash.PLAY_SOUND_FROM_ENTITY, )
- int radius = -10;
- if (forward) radius = 10;
- if (rope != null && rope.Exists())
- {
- rope.Delete();
- DisplayHelpTextThisFrame("Vehicle Hook freed.");
- }
- else
- {
- Vehicle targetcar = World.GetClosestVehicle(GetLastVehicle(Game.Player.Character).Position + GetLastVehicle(Game.Player.Character).ForwardVector * radius, 9f);
- if (Function.Call<bool>(Hash.IS_THIS_MODEL_A_HELI, GetLastVehicle(Game.Player.Character).Model))
- {
- targetcar = World.GetClosestVehicle(GetLastVehicle(Game.Player.Character).Position + GetLastVehicle(Game.Player.Character).UpVector * -30, 25f);
- }
- if (CanWeUse(targetcar))
- {
- Vector3 back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * -(GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.1f));
- if (forward)
- {
- back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * (GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.6f));
- }
- Vector3 front;
- if (IsThisEntityAheadThatEntity(GetLastVehicle(Game.Player.Character), targetcar))
- {
- front = targetcar.Position + (targetcar.ForwardVector * (targetcar.Model.GetDimensions().Y / 2.6f));
- }
- else
- {
- front = targetcar.Position + (targetcar.ForwardVector * -(targetcar.Model.GetDimensions().Y / 2.1f));
- }
- if (GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_THIS_MODEL_A_HELI, GetLastVehicle(Game.Player.Character).Model))
- {
- back = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, -1);
- front = targetcar.Position + (targetcar.UpVector * (targetcar.Model.GetDimensions().Z / 1.5f));
- }
- if (back != Vector3.Zero && front != Vector3.Zero)
- {
- Vector3 pos = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, 2f);
- rope = World.AddRope(RopeType.Normal, pos, pos + new Vector3(0, 0, 5f), back.DistanceTo(front), 0f, false);
- rope.ActivatePhysics();
- rope.AttachEntities(GetLastVehicle(Game.Player.Character), back, targetcar, front, back.DistanceTo(front));
- DisplayHelpTextThisFrame(targetcar.FriendlyName + " hooked.");
- }
- }
- }
- }
- public Vector3 RotToDir(Vector3 Rot)
- {
- float z = Rot.Z;
- float retz = z * 0.0174532924F;//degree to radian
- float x = Rot.X;
- float retx = x * 0.0174532924F;
- float absx = (float)System.Math.Abs(System.Math.Cos(retx));
- return new Vector3((float)-System.Math.Sin(retz) * absx, (float)System.Math.Cos(retz) * absx, (float)System.Math.Sin(retx));
- }
- public RaycastResult Cam_Raycast_Forward()
- {
- Vector3 camrot = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_ROT, 0);
- Vector3 campos = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_COORD);
- Vector3 multiplied = new Vector3(RotToDir(camrot).X * 1000.0f, RotToDir(camrot).Y * 1000.0f, RotToDir(camrot).Z * 1000.0f);
- RaycastResult ray = World.Raycast(campos, campos + multiplied * 5000, IntersectOptions.Everything);
- return ray;
- }
- void AimedHook()
- {
- Entity target;
- Vector3 coordoffset;
- if (CanWeUse(Game.Player.GetTargetedEntity()))
- {
- target = Game.Player.GetTargetedEntity();
- coordoffset = target.Position;
- RaycastResult raycastoffset = Cam_Raycast_Forward();
- if(raycastoffset.DitHitEntity && raycastoffset.HitEntity.Handle == target.Handle) coordoffset=raycastoffset.HitCoords;
- if (Function.Call<bool>(Hash.IS_ENTITY_A_PED, target))
- {
- Ped ped = target as Ped;
- if (ped.IsSittingInVehicle() && CanWeUse(GetLastVehicle(ped))) target = GetLastVehicle(ped);
- }
- if (rope != null && rope.Exists())
- {
- rope.Delete();
- DisplayHelpTextThisFrame("Hook freed.");
- }
- else
- {
- Vector3 back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * -(GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.1f));
- if (IsThisEntityAheadThatEntity(target, GetLastVehicle(Game.Player.Character)))
- {
- back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * (GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.6f));
- }
- Vector3 front = target.Position;
- if (back != Vector3.Zero && front != Vector3.Zero)
- {
- Vector3 pos = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, 2f);
- rope = World.AddRope(RopeType.Normal, pos, pos + new Vector3(0, 0, 5f), back.DistanceTo(coordoffset), 0f, false);
- rope.ActivatePhysics();
- rope.AttachEntities(GetLastVehicle(Game.Player.Character), back, target, coordoffset, back.DistanceTo(coordoffset));
- DisplayHelpTextThisFrame("hooked.");
- }
- }
- }
- /*
- else
- {
- if (rope != null && rope.Exists())
- {
- rope.Delete();
- DisplayHelpTextThisFrame("Hook freed.");
- }
- else
- {
- Vector3 back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * -(GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.1f));
- RaycastResult coords = Cam_Raycast_Forward();
- Vector3 coordspos;
- if (coords.DitHitAnything)
- {
- coordspos = coords.HitCoords;
- Vector3 pos = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, 2f);
- rope = World.AddRope(RopeType.Normal, pos, pos + new Vector3(0, 0, 5f), back.DistanceTo(coordspos), 0f, false);
- rope.ActivatePhysics();
- rope.PinVertex(1, coordspos);
- rope.AttachEntity(GetLastVehicle(Game.Player.Character), coordspos);
- DisplayHelpTextThisFrame("hooked.");
- }
- }
- }
- //TOWING_ENGINE_TURNING_MASTER
- // Function.Call(Hash.PLAY_SOUND_FROM_ENTITY, )
- */
- }
- protected override void Dispose(bool dispose)
- {
- if (rope != null && rope.Exists()) rope.Delete();
- }
- int GetBoneIndex(Entity entity, string value)
- {
- return GTA.Native.Function.Call<int>(Hash._0xFB71170B7E76ACBA, entity, value);
- }
- Vector3 GetBoneCoords(Entity entity, int boneIndex)
- {
- return GTA.Native.Function.Call<Vector3>(Hash._0x44A8FCB8ED227738, entity, boneIndex);
- }
- bool CanWeUse(Entity entity)
- {
- return entity != null && entity.Exists();
- }
- Vehicle GetLastVehicle(Ped ped)
- {
- Vehicle vehicle = null;
- vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, ped, true);
- if (vehicle == null)
- {
- vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, ped, false);
- }
- return vehicle;
- }
- void DisplayHelpTextThisFrame(string text)
- {
- Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
- Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
- Function.Call(Hash._0x238FFE5C7B0498A6, 0, 0, 1, -1);
- }
- bool IsThisEntityAheadThatEntity(Entity ent1, Entity ent2)
- {
- Vector3 pos = ent1.Position;
- return Function.Call<Vector3>(Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, ent2, pos.X, pos.Y, pos.Z).Y > 0f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement