Skip to content

Instantly share code, notes, and snippets.

@ikt32
Last active May 21, 2017 13:36
Show Gist options
  • Save ikt32/a23a3750812358c9bf873e1ab7094499 to your computer and use it in GitHub Desktop.
Save ikt32/a23a3750812358c9bf873e1ab7094499 to your computer and use it in GitHub Desktop.
Shows the licence plate of the car in front
/*
* Raycast version
*/
using System;
using GTA;
using GTA.Math;
using GTA.Native;
public class Raycast : Script
{
public Raycast()
{
Tick += OnTick;
}
void OnTick(object sender, EventArgs e)
{
Ped player = Game.Player.Character;
if (!player.IsInVehicle())
{
return;
}
Vehicle vehicle = player.CurrentVehicle;
float searchdist = 30.0f;
Vector3 vehPos = vehicle.Position;
Vector3 vehDir = vehicle.ForwardVector;
Vector3 vehFor = vehPos + (vehDir * searchdist);
int ray = Function.Call<int>(Hash._CAST_RAY_POINT_TO_POINT, vehPos.X, vehPos.Y, vehPos.Z, vehFor.X, vehFor.Y, vehFor.Z, 10, vehicle, 7);
OutputArgument hit = new OutputArgument();
OutputArgument endcoords = new OutputArgument();
OutputArgument surfacenormal = new OutputArgument();
OutputArgument entityHit = new OutputArgument();
Function.Call(Hash._GET_RAYCAST_RESULT, ray, hit, endcoords, surfacenormal, entityHit);
Vehicle hitVehicle = entityHit.GetResult<Vehicle>();
Vector3 hitVehPos = hitVehicle.Position;
OutputArgument x = new OutputArgument();
OutputArgument y = new OutputArgument();
bool success = Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, hitVehPos.X, hitVehPos.Y, hitVehPos.Z, x, y);
if (success && hitVehicle.Exists())
{
string name = hitVehicle.FriendlyName;
string numplate = hitVehicle.NumberPlate;
string text = "^\n" + name + "\n" + numplate + "\n";
Function.Call(Hash.SET_TEXT_FONT, 0);
Function.Call(Hash.SET_TEXT_SCALE, 0.2, 0.2);
Function.Call(Hash.SET_TEXT_COLOUR, 255, 255, 255, 255);
Function.Call(Hash.SET_TEXT_WRAP, 0.0, 1.0);
Function.Call(Hash.SET_TEXT_CENTRE, 0);
Function.Call(Hash.SET_TEXT_DROPSHADOW, 0, 0, 0, 0, 0);
Function.Call(Hash.SET_TEXT_EDGE, 1, 0, 0, 0, 205);
Function.Call(Hash._SET_TEXT_ENTRY, "STRING");
Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
Function.Call(Hash._DRAW_TEXT, x.GetResult<float>(), y.GetResult<float>());
Function.Call(Hash.DRAW_RECT, x.GetResult<float>() + 0.027f, y.GetResult<float>() + 0.043f, 0.058f, 0.056f,
75, 75, 75, 175);
}
}
}
/*
* Search version
* Due to having multiple entries we can do fancy stuff like fade-in,
* fade-out, depending on distance, and showing multiple boxes.
*/
using System;
using System.CodeDom.Compiler;
using GTA;
using GTA.Math;
using GTA.Native;
public class SearchCone : Script
{
public SearchCone()
{
Tick += OnTick;
}
private bool active = false;
private Vehicle lastVehicle;
bool areTheseClose(float h1, float h2, float separation)
{
var diff = Math.Abs(h1 - h2);
if (diff < separation)
return true;
if (Math.Abs(diff - 360) < separation)
return true;
return false;
}
float howClose(float h1, float h2, float separation)
{
var diff = Math.Abs(h1 - h2);
if (diff < separation)
return (separation - diff) / separation;
if (Math.Abs(diff - 360) < separation)
return (separation - Math.Abs(diff - 360)) / separation;
return 0.0f;
}
void OnTick(object sender, EventArgs e)
{
Ped player = Game.Player.Character;
Vehicle vehicle;
if (!player.IsInVehicle())
{
vehicle = lastVehicle;
}
else
{
vehicle = player.CurrentVehicle;
lastVehicle = vehicle;
}
if (vehicle == null)
return;
if (Game.IsControlJustPressed(0, Control.VehicleHandbrake))
{
active = !active;
UI.Notify("Plate viewer " + (active ? "enabled" : "disabled"));
}
if (!active)
{
return;
}
Vector3 vehPos = vehicle.Position;
const float searchDist = 30.0f;
Vehicle[] vehicles = World.GetAllVehicles();
for (int i = 0; i < vehicles.Length; i++)
{
if (vehicles[i] != vehicle)
{
Vector3 targetPos = vehicles[i].Position;
Vector3 direction = targetPos - vehPos;
direction.Normalize();
float vehDirection = Convert.ToSingle(Math.Atan2(direction.Y, direction.X) * (180.0f / Math.PI));
float myHeading = (vehicle.Heading + 90.0f) % 360;
OutputArgument x = new OutputArgument();
OutputArgument y = new OutputArgument();
bool success = Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, targetPos.X, targetPos.Y, targetPos.Z, x, y);
bool close = areTheseClose(vehDirection, myHeading, 30.0f);
float centerCloseness = howClose(vehDirection, myHeading, 30.0f);
if (success && close)
{
float dist = World.GetDistance(vehPos, targetPos);
if (dist < searchDist)
{
float meCloseness = (float)Math.Pow(2.0f * (searchDist - dist) / searchDist, 2.0f);
int drawAlpha = Math.Min((int)(255 * centerCloseness * meCloseness), 255);
string name = vehicles[i].FriendlyName;
string numplate = vehicles[i].NumberPlate;
string text = "^\n" + name + "\n" + numplate + "\n";
float screenX = x.GetResult<float>();
float screenY = y.GetResult<float>();
ShowVehicleTag(text, screenX, screenY, drawAlpha);
}
}
}
}
}
private static void ShowVehicleTag(string text, float x, float y, int alpha)
{
Function.Call(Hash.SET_TEXT_FONT, 0);
Function.Call(Hash.SET_TEXT_SCALE, 0.4, 0.4);
Function.Call(Hash.SET_TEXT_COLOUR, 255, 255, 255, alpha);
Function.Call(Hash.SET_TEXT_WRAP, 0.0, 1.0);
Function.Call(Hash.SET_TEXT_CENTRE, 0);
Function.Call(Hash.SET_TEXT_DROPSHADOW, 0, 0, 0, 0, 0);
Function.Call(Hash.SET_TEXT_EDGE, 1, 0, 0, 0, 205);
Function.Call(Hash._SET_TEXT_ENTRY, "STRING");
Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
Function.Call(Hash._DRAW_TEXT, x, y);
Function.Call(Hash.DRAW_RECT, x + 0.03f, y + 0.057f, 0.07f, 0.06f, 75, 75, 75, alpha / 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment