Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTA;
- using System;
- using System.Windows.Forms;
- using GTA.Math;
- using GTA.Native;
- using NativeUI;
- public class ScriptTest : Script
- {
- Camera RTSCam = World.CreateCamera(Game.Player.Character.Position, new Vector3(0, 0, 0), 60);
- float smoothcam = 0.0f;
- float smoothcamHoriz = 0.0f;
- Entity EntToFollow;
- bool IsCursorOn = false;
- int DesiredHeight = 10;
- public ScriptTest()
- {
- Tick += OnTick;
- KeyDown += OnKeyDown;
- KeyUp += OnKeyUp;
- }
- void OnTick(object sender, EventArgs e)
- {
- if (smoothcam > 0.5f) smoothcam = 0.5f;
- if (smoothcam < -0.5f) smoothcam = -0.5f;
- if (smoothcamHoriz > 2) smoothcamHoriz = 2;
- if (DesiredHeight < 3) DesiredHeight = 3;
- if (DesiredHeight > 95) DesiredHeight = 95;
- //Math.Round(smoothcam, 1);
- HandleRTSCam();
- if (Game.IsControlJustPressed(2, GTA.Control.FrontendAccept))
- {
- if (World.RenderingCamera == RTSCam)
- {
- World.RenderingCamera = null;
- Game.Player.Character.Position = World.GetNextPositionOnSidewalk(Game.Player.Character.Position);
- }
- else
- {
- World.RenderingCamera = RTSCam;
- }
- }
- }
- void OnKeyDown(object sender, KeyEventArgs e)
- {
- }
- void OnKeyUp(object sender, KeyEventArgs e)
- {
- }
- void HandleRTSCam()
- {
- if (World.RenderingCamera == RTSCam)
- {
- DisplayHelpTextThisFrame("Full Freecam: ~INPUT_JUMP~~n~Faster Cam: ~INPUT_SPRINT~~n~Raise Cam: ~INPUT_SELECT_PREV_WEAPON~");
- Function.Call(Hash.HIDE_HUD_AND_RADAR_THIS_FRAME);
- Function.Call(Hash.DISABLE_ALL_CONTROL_ACTIONS, 0);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.LookLeftRight);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.LookUpDown);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.CursorX);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.CursorY);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.FrontendPauseAlternate);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.WeaponWheelPrev);
- Function.Call(Hash.ENABLE_CONTROL_ACTION, 0, (int)GTA.Control.WeaponWheelNext);
- Game.Player.Character.Position = RTSCam.Position - RotationToDirection(RTSCam.Rotation) * 8f;
- if (Game.IsKeyPressed(Keys.ControlKey))
- {
- Function.Call(Hash._SHOW_CURSOR_THIS_FRAME);
- if (!IsCursorOn) IsCursorOn = true;
- if (Game.IsControlJustPressed(2, GTA.Control.Aim))
- {
- Vector2 mousecoords = new Vector2(Cursor.Position.X, Cursor.Position.Y);
- mousecoords = new Vector2((mousecoords.X / UIMenu.GetScreenResolutionMantainRatio().Width) * 2 - 1, (mousecoords.Y / UIMenu.GetScreenResolutionMantainRatio().Height) * 2 - 1);
- UI.Notify(mousecoords.ToString());
- Entity ent = RaycastEverything(mousecoords);
- if (CanWeUse(ent))
- {
- EntToFollow = ent;
- //RTSCam.AttachTo(ent, EntityOffsetGivenWorldCoords(ent, ent.Position + (ent.ForwardVector * -10) + ent.UpVector * 5));
- RTSCam.Rotation = new Vector3(0, 0, ent.Rotation.Z);
- }
- else
- {
- UI.Notify("~r~Couldn't find entity.");
- }
- }
- }
- else
- {
- float mouseX = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)GTA.Control.LookLeftRight);
- float mouseY = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, (int)GTA.Control.LookUpDown);
- RTSCam.Rotation = RTSCam.Rotation + new Vector3(mouseY * -5, 0, mouseX * -5);
- if (RTSCam.Rotation.X < -90) RTSCam.Rotation = RTSCam.Rotation + new Vector3(1, 0, 0);
- if (RTSCam.Rotation.X > 90) RTSCam.Rotation = RTSCam.Rotation + new Vector3(-1, 0, 0);
- if (IsCursorOn) IsCursorOn = false;
- if (CanWeUse(EntToFollow))
- {
- //RTSCam.Position = EntToFollow.Position - new Vector3(EntToFollow.Velocity.X, EntToFollow.Velocity.Y,5*-1) + (EntToFollow.UpVector*3);
- RTSCam.Position = EntToFollow.Position+(new Vector3(0,0,EntToFollow.Model.GetDimensions().Z*2));
- if (Game.IsControlJustPressed(2, GTA.Control.Aim))
- {
- EntToFollow = null;
- }
- }
- else
- {
- bool ShouldFixCameraHeight = !Game.IsControlPressed(2, GTA.Control.Jump);
- float modifier = 1f;
- if (Game.IsControlPressed(2, GTA.Control.Sprint)) modifier = 3f;
- if (ShouldFixCameraHeight)
- {
- RaycastResult result = World.Raycast(RTSCam.Position, new Vector3(0, 0, -1), 100f, IntersectOptions.Map, Game.Player.Character);
- if (RTSCam.Position.Z - result.HitCoords.Z < DesiredHeight - 2f)
- {
- smoothcam = smoothcam + 0.05f;
- }
- else if (RTSCam.Position.Z - result.HitCoords.Z > DesiredHeight)
- {
- smoothcam = smoothcam - 0.05f;
- }
- else if (smoothcam > 0)
- {
- smoothcam = smoothcam - 0.05f;
- }
- else if (smoothcam < 0)
- {
- smoothcam = smoothcam + 0.05f;
- }
- smoothcam = (float)Math.Round(smoothcam, 2);
- }
- else
- {
- if (smoothcam != 0) smoothcam = 0;
- }
- Vector3 newpos = RTSCam.Position;
- Vector3 newdir = RotationToDirection(RTSCam.Rotation);
- float _Z = RTSCam.Position.Z;
- var rotLeft = RTSCam.Rotation + new Vector3(0, 0, -10);
- var rotRight = RTSCam.Rotation + new Vector3(0, 0, 10);
- var right = RotationToDirection(rotRight) - RotationToDirection(rotLeft);
- if (Game.IsControlPressed(0, GTA.Control.MoveUpOnly))
- {
- newpos += newdir * modifier;
- }
- if (Game.IsControlPressed(0, GTA.Control.MoveDownOnly))
- {
- newpos -= newdir * modifier;
- }
- if (Game.IsControlPressed(0, GTA.Control.MoveLeftOnly))
- {
- newpos += right * (modifier * 2);
- }
- if (Game.IsControlPressed(0, GTA.Control.MoveRightOnly))
- {
- newpos -= right * (modifier * 2);
- }
- if (Game.IsControlPressed(2, GTA.Control.WeaponWheelPrev))
- {
- if (Game.IsControlPressed(2, GTA.Control.Sprint)) DesiredHeight += 5; else DesiredHeight += 1;
- }
- if (Game.IsControlPressed(2, GTA.Control.WeaponWheelNext))
- {
- if (Game.IsControlPressed(2, GTA.Control.Sprint)) DesiredHeight -= 5; else DesiredHeight -= 1;
- }
- if (!ShouldFixCameraHeight) RTSCam.Position = newpos; else RTSCam.Position = new Vector3(newpos.X, newpos.Y, _Z + smoothcam);
- }
- }
- }
- else if (CanWeUse(EntToFollow)) EntToFollow = null; //RTSCam.Detach();
- }
- Vector3 EntityOffsetGivenWorldCoords(Entity ent, Vector3 coords)
- {
- return Function.Call<Vector3>(Hash.GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS, ent, coords.X,coords.Y,coords.Z);
- }
- bool CanWeUse(Entity entity)
- {
- return entity != null && entity.Exists();
- }
- protected override void Dispose(bool dispose)
- {
- World.RenderingCamera = null;
- RTSCam.Destroy();
- Game.Player.Character.Position = World.GetNextPositionOnSidewalk(Game.Player.Character.Position);
- base.Dispose(dispose);
- }
- void DisplayHelpTextThisFrame(string text)
- {
- Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
- Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
- Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, 0, 0, -1);
- }
- public Entity RaycastEverything(Vector2 screenCoord)
- {
- Vector3 camPos = RTSCam.Position;
- Vector3 camRot = RTSCam.Rotation;
- const float raycastToDist = 100.0f;
- const float raycastFromDist = 1f;
- var target3D = ScreenRelToWorld(camPos, camRot, screenCoord);
- var source3D = camPos;
- Entity ignoreEntity = Game.Player.Character;
- if (Game.Player.Character.IsInVehicle())
- {
- ignoreEntity = Game.Player.Character.CurrentVehicle;
- }
- var dir = (target3D - source3D);
- dir.Normalize();
- var raycastResults = World.Raycast(source3D + dir * raycastFromDist,
- source3D + dir * raycastToDist,IntersectOptions.Everything, ignoreEntity);
- if (raycastResults.DitHitEntity)
- {
- return raycastResults.HitEntity;
- }
- return null; //camPos + dir * raycastToDist;
- }
- public static Vector3 ScreenRelToWorld(Vector3 camPos, Vector3 camRot, Vector2 coord)
- {
- var camForward = RotationToDirection(camRot);
- var rotUp = camRot + new Vector3(10, 0, 0);
- var rotDown = camRot + new Vector3(-10, 0, 0);
- var rotLeft = camRot + new Vector3(0, 0, -10);
- var rotRight = camRot + new Vector3(0, 0, 10);
- var camRight = RotationToDirection(rotRight) - RotationToDirection(rotLeft);
- var camUp = RotationToDirection(rotUp) - RotationToDirection(rotDown);
- var rollRad = -DegToRad(camRot.Y);
- var camRightRoll = camRight * (float)Math.Cos(rollRad) - camUp * (float)Math.Sin(rollRad);
- var camUpRoll = camRight * (float)Math.Sin(rollRad) + camUp * (float)Math.Cos(rollRad);
- var point3D = camPos + camForward * 10.0f + camRightRoll + camUpRoll;
- Vector2 point2D;
- if (!WorldToScreenRel(point3D, out point2D)) return camPos + camForward * 10.0f;
- var point3DZero = camPos + camForward * 10.0f;
- Vector2 point2DZero;
- if (!WorldToScreenRel(point3DZero, out point2DZero)) return camPos + camForward * 10.0f;
- const double eps = 0.001;
- if (Math.Abs(point2D.X - point2DZero.X) < eps || Math.Abs(point2D.Y - point2DZero.Y) < eps) return camPos + camForward * 10.0f;
- var scaleX = (coord.X - point2DZero.X) / (point2D.X - point2DZero.X);
- var scaleY = (coord.Y - point2DZero.Y) / (point2D.Y - point2DZero.Y);
- var point3Dret = camPos + camForward * 10.0f + camRightRoll * scaleX + camUpRoll * scaleY;
- return point3Dret;
- }
- public static bool WorldToScreenRel(Vector3 worldCoords, out Vector2 screenCoords)
- {
- OutputArgument num1 = new OutputArgument();
- OutputArgument num2 = new OutputArgument();
- if (!Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, worldCoords.X, worldCoords.Y, worldCoords.Z, num1, num2))
- {
- screenCoords = new Vector2();
- return false;
- }
- screenCoords = new Vector2((num1.GetResult<float>() - 0.5f) * 2, (num2.GetResult<float>() - 0.5f) * 2);
- return true;
- }
- public static double DegToRad(double deg)
- {
- return deg * Math.PI / 180.0;
- }
- public static double RadToDeg(double deg)
- {
- return deg * 180.0 / Math.PI;
- }
- public static Vector3 RotationToDirection(Vector3 rotation)
- {
- var z = DegToRad(rotation.Z);
- var x = DegToRad(rotation.X);
- var num = Math.Abs(Math.Cos(x));
- return new Vector3
- {
- X = (float)(-Math.Sin(z) * num),
- Y = (float)(Math.Cos(z) * num),
- Z = (float)Math.Sin(x)
- };
- }
- }
Add Comment
Please, Sign In to add comment