Advertisement
Eddlm

Winch

Jan 20th, 2016
3,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.83 KB | None | 0 0
  1. using GTA;
  2. using GTA.Math;
  3. using GTA.Native;
  4. using System;
  5. using System.Windows.Forms;
  6.  
  7.  
  8.  
  9. public class Winch : Script
  10. {
  11.     Rope rope;
  12.     int windingsound;
  13.     public Winch()
  14.     {
  15.         Tick += OnTick;
  16.         KeyDown += OnKeyDown;
  17.         KeyUp += OnKeyUp;
  18.     }
  19.     bool winding = false;
  20.     void OnTick(object sender, EventArgs e)
  21.     {
  22.         if (Game.IsControlPressed(2, GTA.Control.VehicleHandbrake))
  23.         {
  24.  
  25.             if (Game.IsControlJustPressed(2, GTA.Control.VehicleMoveUpOnly))
  26.             {
  27.                 if (Game.Player.IsAiming)
  28.                 {
  29.                     AimedHook();
  30.                 }
  31.                 else
  32.                 {
  33.                     HookNearbyCar(true);
  34.  
  35.                 }
  36.             }
  37.             if (Game.IsControlJustPressed(2, GTA.Control.VehicleMoveDownOnly))
  38.             {
  39.                 if (Game.Player.IsAiming)
  40.                 {
  41.                     AimedHook();
  42.  
  43.                 }
  44.                 else
  45.                 {
  46.                     HookNearbyCar(false);
  47.                 }
  48.             }
  49.             if (rope != null && rope.Exists())
  50.             {
  51.                 if (Game.IsControlJustPressed(2, GTA.Control.VehicleBrake))
  52.                 {
  53.                     if (!winding)
  54.                     {
  55.                         Function.Call(Hash.START_ROPE_WINDING, rope); //start wind
  56.                         winding = true;
  57.                         DisplayHelpTextThisFrame("Winding started");
  58.  
  59.                         windingsound = Audio.PlaySoundFrontend("OPENED", "DOOR_GARAGE");
  60.  
  61.                     }
  62.                     else
  63.                     {
  64.                         Function.Call(Hash.STOP_ROPE_WINDING, rope);
  65.                         Function.Call(Hash.STOP_ROPE_UNWINDING_FRONT, rope);
  66.                         winding = false;
  67.                         DisplayHelpTextThisFrame("Winding stopped");
  68.                         //Audio.StopSound(windingsound);
  69.  
  70.                     }
  71.                 }
  72.                 if (Game.IsControlJustPressed(2, GTA.Control.VehicleAccelerate))
  73.                 {
  74.                     if (!winding)
  75.                     {
  76.  
  77.                         Function.Call(Hash.START_ROPE_UNWINDING_FRONT, rope); //start unwind
  78.                         winding = true;
  79.                         DisplayHelpTextThisFrame("Unwinding started");
  80.                         windingsound = Audio.PlaySoundFrontend("Truck_Ramp_Raise", "BIG_SCORE_3A_SOUNDS");
  81.  
  82.                     }
  83.                     else
  84.                     {
  85.                         Function.Call(Hash.STOP_ROPE_WINDING, rope);
  86.                         Function.Call(Hash.STOP_ROPE_UNWINDING_FRONT, rope);
  87.                         winding = false;
  88.                         DisplayHelpTextThisFrame("Unwinding stopped");
  89.  
  90.                         Audio.StopSound(windingsound);
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.         if (Game.Player.Character.IsSittingInVehicle() && !Game.IsControlPressed(2, GTA.Control.VehicleHandbrake) && rope != null && rope.Exists())
  96.         {
  97.             if (Game.IsControlPressed(2, GTA.Control.VehicleAccelerate) && GetLastVehicle(Game.Player.Character).Speed < 2f)
  98.             {
  99.                 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);
  100.             }
  101.             if (Game.IsControlPressed(2, GTA.Control.VehicleBrake) && GetLastVehicle(Game.Player.Character).Speed < 2f)
  102.             {
  103.                 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);
  104.             }
  105.  
  106.             if (GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_THIS_MODEL_A_HELI, GetLastVehicle(Game.Player.Character).Model))
  107.             {
  108.                 if (Game.IsControlPressed(2, GTA.Control.VehicleFlyThrottleUp) && GetLastVehicle(Game.Player.Character).Velocity.Z < 2f)
  109.                 {
  110.                     Function.Call(Hash.APPLY_FORCE_TO_ENTITY, GetLastVehicle(Game.Player.Character), 3, 0f, 0f, 1f, 0f, 0f, 0f, 0, true, true, true, true, true);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     void OnKeyDown(object sender, KeyEventArgs e)
  116.     {
  117.  
  118.     }
  119.     void OnKeyUp(object sender, KeyEventArgs e)
  120.     {
  121.  
  122.     }
  123.  
  124.     void HookNearbyCar(bool forward)
  125.     {
  126.         //TOWING_ENGINE_TURNING_MASTER
  127.         // Function.Call(Hash.PLAY_SOUND_FROM_ENTITY, )
  128.         int radius = -10;
  129.         if (forward) radius = 10;
  130.         if (rope != null && rope.Exists())
  131.         {
  132.             rope.Delete();
  133.             DisplayHelpTextThisFrame("Vehicle Hook freed.");
  134.  
  135.         }
  136.         else
  137.         {
  138.             Vehicle targetcar = World.GetClosestVehicle(GetLastVehicle(Game.Player.Character).Position + GetLastVehicle(Game.Player.Character).ForwardVector * radius, 9f);
  139.  
  140.             if (Function.Call<bool>(Hash.IS_THIS_MODEL_A_HELI, GetLastVehicle(Game.Player.Character).Model))
  141.             {
  142.                 targetcar = World.GetClosestVehicle(GetLastVehicle(Game.Player.Character).Position + GetLastVehicle(Game.Player.Character).UpVector * -30, 25f);
  143.             }
  144.             if (CanWeUse(targetcar))
  145.             {
  146.                 Vector3 back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * -(GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.1f));
  147.                 if (forward)
  148.                 {
  149.                     back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * (GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.6f));
  150.                 }
  151.                 Vector3 front;
  152.                 if (IsThisEntityAheadThatEntity(GetLastVehicle(Game.Player.Character), targetcar))
  153.                 {
  154.                     front = targetcar.Position + (targetcar.ForwardVector * (targetcar.Model.GetDimensions().Y / 2.6f));
  155.                 }
  156.                 else
  157.                 {
  158.                     front = targetcar.Position + (targetcar.ForwardVector * -(targetcar.Model.GetDimensions().Y / 2.1f));
  159.                 }
  160.  
  161.                 if (GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_THIS_MODEL_A_HELI, GetLastVehicle(Game.Player.Character).Model))
  162.                 {
  163.                     back = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, -1);
  164.                     front = targetcar.Position + (targetcar.UpVector * (targetcar.Model.GetDimensions().Z / 1.5f));
  165.                 }
  166.  
  167.                 if (back != Vector3.Zero && front != Vector3.Zero)
  168.                 {
  169.  
  170.                     Vector3 pos = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, 2f);
  171.                     rope = World.AddRope(RopeType.Normal, pos, pos + new Vector3(0, 0, 5f), back.DistanceTo(front), 0f, false);
  172.                     rope.ActivatePhysics();
  173.                     rope.AttachEntities(GetLastVehicle(Game.Player.Character), back, targetcar, front, back.DistanceTo(front));
  174.                     DisplayHelpTextThisFrame(targetcar.FriendlyName + " hooked.");
  175.  
  176.                 }
  177.             }
  178.         }
  179.     }
  180.  
  181.  
  182.     public Vector3 RotToDir(Vector3 Rot)
  183.     {
  184.  
  185.         float z = Rot.Z;
  186.         float retz = z * 0.0174532924F;//degree to radian
  187.         float x = Rot.X;
  188.         float retx = x * 0.0174532924F;
  189.         float absx = (float)System.Math.Abs(System.Math.Cos(retx));
  190.         return new Vector3((float)-System.Math.Sin(retz) * absx, (float)System.Math.Cos(retz) * absx, (float)System.Math.Sin(retx));
  191.  
  192.     }
  193.     public RaycastResult Cam_Raycast_Forward()
  194.     {
  195.         Vector3 camrot = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_ROT, 0);
  196.         Vector3 campos = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_COORD);
  197.  
  198.         Vector3 multiplied = new Vector3(RotToDir(camrot).X * 1000.0f, RotToDir(camrot).Y * 1000.0f, RotToDir(camrot).Z * 1000.0f);
  199.         RaycastResult ray = World.Raycast(campos, campos + multiplied * 5000, IntersectOptions.Everything);
  200.         return ray;
  201.     }
  202.  
  203.     void AimedHook()
  204.     {
  205.         Entity target;
  206.         Vector3 coordoffset;
  207.         if (CanWeUse(Game.Player.GetTargetedEntity()))
  208.         {
  209.             target = Game.Player.GetTargetedEntity();
  210.             coordoffset = target.Position;
  211.             RaycastResult raycastoffset = Cam_Raycast_Forward();
  212.             if(raycastoffset.DitHitEntity && raycastoffset.HitEntity.Handle == target.Handle) coordoffset=raycastoffset.HitCoords;
  213.             if (Function.Call<bool>(Hash.IS_ENTITY_A_PED, target))
  214.             {
  215.                 Ped ped = target as Ped;
  216.                 if (ped.IsSittingInVehicle() && CanWeUse(GetLastVehicle(ped))) target = GetLastVehicle(ped);
  217.             }
  218.             if (rope != null && rope.Exists())
  219.             {
  220.                 rope.Delete();
  221.                 DisplayHelpTextThisFrame("Hook freed.");
  222.             }
  223.             else
  224.             {
  225.                 Vector3 back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * -(GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.1f));
  226.                 if (IsThisEntityAheadThatEntity(target, GetLastVehicle(Game.Player.Character)))
  227.                 {
  228.                     back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * (GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.6f));
  229.                 }
  230.  
  231.                 Vector3 front = target.Position;
  232.  
  233.  
  234.                 if (back != Vector3.Zero && front != Vector3.Zero)
  235.                 {
  236.                     Vector3 pos = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, 2f);
  237.                     rope = World.AddRope(RopeType.Normal, pos, pos + new Vector3(0, 0, 5f), back.DistanceTo(coordoffset), 0f, false);
  238.                     rope.ActivatePhysics();
  239.                     rope.AttachEntities(GetLastVehicle(Game.Player.Character), back, target, coordoffset, back.DistanceTo(coordoffset));
  240.                     DisplayHelpTextThisFrame("hooked.");
  241.                 }
  242.             }
  243.         }
  244.  
  245.         /*
  246.         else
  247.         {
  248.             if (rope != null && rope.Exists())
  249.             {
  250.                 rope.Delete();
  251.                 DisplayHelpTextThisFrame("Hook freed.");
  252.             }
  253.             else
  254.             {
  255.                 Vector3 back = GetLastVehicle(Game.Player.Character).Position + (GetLastVehicle(Game.Player.Character).ForwardVector * -(GetLastVehicle(Game.Player.Character).Model.GetDimensions().Y / 2.1f));
  256.                 RaycastResult coords = Cam_Raycast_Forward();
  257.                 Vector3 coordspos;
  258.                 if (coords.DitHitAnything)
  259.                 {
  260.                     coordspos = coords.HitCoords;
  261.                     Vector3 pos = GetLastVehicle(Game.Player.Character).Position + new Vector3(0, 0, 2f);
  262.                     rope = World.AddRope(RopeType.Normal, pos, pos + new Vector3(0, 0, 5f), back.DistanceTo(coordspos), 0f, false);
  263.                     rope.ActivatePhysics();
  264.                     rope.PinVertex(1, coordspos);
  265.                     rope.AttachEntity(GetLastVehicle(Game.Player.Character), coordspos);
  266.                    
  267.                     DisplayHelpTextThisFrame("hooked.");
  268.                 }
  269.  
  270.  
  271.             }
  272.         }
  273.  
  274.         //TOWING_ENGINE_TURNING_MASTER
  275.         // Function.Call(Hash.PLAY_SOUND_FROM_ENTITY, )
  276.  
  277.                 */
  278.  
  279.     }
  280.     protected override void Dispose(bool dispose)
  281.     {
  282.         if (rope != null && rope.Exists()) rope.Delete();
  283.     }
  284.  
  285.  
  286.         int GetBoneIndex(Entity entity, string value)
  287.     {
  288.         return GTA.Native.Function.Call<int>(Hash._0xFB71170B7E76ACBA, entity, value);
  289.     }
  290.  
  291.     Vector3 GetBoneCoords(Entity entity, int boneIndex)
  292.     {
  293.         return GTA.Native.Function.Call<Vector3>(Hash._0x44A8FCB8ED227738, entity, boneIndex);
  294.     }
  295.     bool CanWeUse(Entity entity)
  296.     {
  297.         return entity != null && entity.Exists();
  298.     }
  299.     Vehicle GetLastVehicle(Ped ped)
  300.     {
  301.         Vehicle vehicle = null;
  302.         vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, ped, true);
  303.         if (vehicle == null)
  304.         {
  305.             vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, ped, false);
  306.         }
  307.         return vehicle;
  308.     }
  309.  
  310.  
  311.     void DisplayHelpTextThisFrame(string text)
  312.     {
  313.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  314.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  315.         Function.Call(Hash._0x238FFE5C7B0498A6, 0, 0, 1, -1);
  316.     }
  317.  
  318.     bool IsThisEntityAheadThatEntity(Entity ent1, Entity ent2)
  319.     {
  320.         Vector3 pos = ent1.Position;
  321.  
  322.         return Function.Call<Vector3>(Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, ent2, pos.X, pos.Y, pos.Z).Y > 0f;
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement