Guest User

ShowPlate.cs

a guest
May 21st, 2017
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using GTA;
  3. using GTA.Math;
  4. using GTA.Native;
  5.  
  6. public class SearchCone : Script
  7. {
  8.     public SearchCone()
  9.     {
  10.         Tick += OnTick;
  11.     }
  12.  
  13.     private bool active = false;
  14.  
  15.     bool areTheseClose(float h1, float h2, float separation)
  16.     {
  17.         var diff = Math.Abs(h1 - h2);
  18.         if (diff < separation)
  19.             return true;
  20.         if (Math.Abs(diff - 360) < separation)
  21.             return true;
  22.  
  23.         return false;
  24.     }
  25.  
  26.     void OnTick(object sender, EventArgs e)
  27.     {
  28.         Ped player = Game.Player.Character;
  29.         if (!player.IsInVehicle())
  30.         {
  31.             return;
  32.         }
  33.  
  34.         if (Game.IsControlJustPressed(0, Control.VehicleHandbrake))
  35.         {
  36.             active = !active;
  37.             UI.Notify("Plate viewer " + (active ? "enabled" : "disabled"));
  38.         }
  39.  
  40.         if (!active)
  41.         {
  42.             return;
  43.         }
  44.  
  45.         Vehicle vehicle = player.CurrentVehicle;
  46.         Vector3 vehPos = vehicle.Position;
  47.  
  48.         const float searchDist = 30.0f;
  49.  
  50.         Vehicle[] vehicles = World.GetAllVehicles();
  51.  
  52.         float closestDist = searchDist;
  53.         string text = "";
  54.         float screenX = 0.0f;
  55.         float screenY = 0.0f;
  56.  
  57.         for (int i = 0; i < vehicles.Length; i++)
  58.         {
  59.             if (vehicles[i] != vehicle)
  60.             {
  61.                 Vector3 targetPos = vehicles[i].Position;
  62.                 Vector3 direction = targetPos - vehPos;
  63.                 direction.Normalize();
  64.                 float vehDirection = Convert.ToSingle(Math.Atan2(direction.Y, direction.X) * (180.0f / Math.PI));
  65.                 float myHeading = (player.Heading + 90.0f) % 360;
  66.  
  67.                 OutputArgument x = new OutputArgument();
  68.                 OutputArgument y = new OutputArgument();
  69.                 bool success = Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, targetPos.X, targetPos.Y, targetPos.Z, x, y);
  70.                 bool close = areTheseClose(vehDirection, myHeading, 10.0f);
  71.                 if (success && close)// && areTheseClose(vehDirection, myHeading, 45.0f))
  72.                 {
  73.                     Vector3 plv = player.Position;
  74.                     float dist = World.GetDistance(plv, targetPos);
  75.                     if (dist < searchDist)
  76.                     {
  77.                         if (dist < closestDist)
  78.                         {
  79.                             closestDist = dist;
  80.                             string name = vehicles[i].FriendlyName;
  81.                             string numplate = vehicles[i].NumberPlate;
  82.  
  83.                             text = "^\n" + name + "\n" + numplate + "\n";
  84.                             screenX = x.GetResult<float>();
  85.                             screenY = y.GetResult<float>();
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         if (text != "")
  92.         {
  93.             ShowVehicleTag(text, screenX, screenY);
  94.         }
  95.     }
  96.  
  97.     private static void ShowVehicleTag(string text, float x, float y)
  98.     {
  99.         Function.Call(Hash.SET_TEXT_FONT, 0);
  100.         Function.Call(Hash.SET_TEXT_SCALE, 0.4, 0.4);
  101.         Function.Call(Hash.SET_TEXT_COLOUR, 255, 255, 255, 255);
  102.         Function.Call(Hash.SET_TEXT_WRAP, 0.0, 1.0);
  103.         Function.Call(Hash.SET_TEXT_CENTRE, 0);
  104.         Function.Call(Hash.SET_TEXT_DROPSHADOW, 0, 0, 0, 0, 0);
  105.         Function.Call(Hash.SET_TEXT_EDGE, 1, 0, 0, 0, 205);
  106.         Function.Call(Hash._SET_TEXT_ENTRY, "STRING");
  107.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  108.         Function.Call(Hash._DRAW_TEXT, x, y);
  109.         Function.Call(Hash.DRAW_RECT, x + 0.03f, y + 0.057f, 0.07f, 0.06f, 75, 75, 75, 175);
  110.     }
  111. }
RAW Paste Data