Files
RAGECOOP-V/Client/Main.cs

368 lines
14 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
2021-08-18 11:47:59 +02:00
using System.Drawing;
2021-07-07 13:36:25 +02:00
using CoopClient.Entities;
using CoopClient.Menus;
2021-07-07 13:36:25 +02:00
using GTA;
using GTA.Native;
namespace CoopClient
{
2021-12-03 20:30:00 +01:00
/// <summary>
/// Don't use it!
/// </summary>
public class Main : Script
2021-07-07 13:36:25 +02:00
{
2021-12-03 20:30:00 +01:00
internal static RelationshipGroup RelationshipGroup;
2021-07-07 13:36:25 +02:00
private bool GameLoaded = false;
2021-12-16 22:35:37 +01:00
internal static readonly string CurrentVersion = "V1_2_0";
2021-07-07 13:36:25 +02:00
internal static bool ShareNPCsWithPlayers = false;
2021-12-03 20:30:00 +01:00
internal static bool DisableTraffic = false;
internal static bool NPCsAllowed = false;
2021-07-13 08:30:10 +02:00
private static bool IsGoingToCar = false;
2021-07-07 13:36:25 +02:00
2021-12-15 00:25:31 +01:00
internal static Settings MainSettings = null;
internal static Networking MainNetworking = null;
2021-08-18 11:47:59 +02:00
#if !NON_INTERACTIVE
2021-12-15 00:25:31 +01:00
internal static MenusMain MainMenu = null;
#endif
2021-12-15 00:25:31 +01:00
internal static Chat MainChat = null;
2021-07-07 13:36:25 +02:00
internal static long LocalNetHandle = 0;
internal static Dictionary<long, EntitiesPlayer> Players = null;
internal static Dictionary<long, EntitiesNpc> NPCs = null;
internal static Dictionary<long, int> NPCsVehicles = null;
2021-07-07 13:36:25 +02:00
2021-12-03 20:30:00 +01:00
/// <summary>
/// Don't use it!
/// </summary>
2021-07-07 13:36:25 +02:00
public Main()
{
// Required for some synchronization!
if (Game.Version < GameVersion.v1_0_1290_1_Steam)
{
Tick += (object sender, EventArgs e) =>
{
if (Game.IsLoading)
{
return;
}
if (!GameLoaded)
{
GTA.UI.Notification.Show("~r~Please update your GTA5 to v1.0.1290 or newer!", true);
GameLoaded = true;
}
};
return;
}
MainSettings = Util.ReadSettings();
MainNetworking = new Networking();
#if !NON_INTERACTIVE
MainMenu = new MenusMain();
#endif
MainChat = new Chat();
Players = new Dictionary<long, EntitiesPlayer>();
NPCs = new Dictionary<long, EntitiesNpc>();
NPCsVehicles = new Dictionary<long, int>();
2021-07-07 13:36:25 +02:00
Function.Call((Hash)0x0888C3502DBBEEF5); // _LOAD_MP_DLC_MAPS
Function.Call((Hash)0x9BAE5AD2508DF078, true); // _ENABLE_MP_DLC_MAPS
Tick += OnTick;
#if !NON_INTERACTIVE
2021-07-07 13:36:25 +02:00
KeyDown += OnKeyDown;
#endif
2021-08-21 16:52:17 +02:00
Aborted += (object sender, EventArgs e) => CleanUp();
2021-07-13 16:32:45 +02:00
Util.NativeMemory();
2021-07-07 13:36:25 +02:00
}
2021-11-19 22:08:15 +01:00
private ulong LastDataSend;
2021-07-07 13:36:25 +02:00
private void OnTick(object sender, EventArgs e)
{
if (Game.IsLoading)
{
return;
}
else if (!GameLoaded && (GameLoaded = true))
{
RelationshipGroup = World.AddRelationshipGroup("SYNCPED");
Game.Player.Character.RelationshipGroup = RelationshipGroup;
#if !NON_INTERACTIVE
2021-11-28 22:57:39 +01:00
GTA.UI.Notification.Show(GTA.UI.NotificationIcon.AllPlayersConf, "GTACOOP:R", "Welcome!", "Press ~g~F9~s~ to open the menu.");
#endif
2021-07-07 13:36:25 +02:00
}
#if !NON_INTERACTIVE
MainMenu.MenuPool.Process();
#endif
2021-07-07 13:36:25 +02:00
MainNetworking.ReceiveMessages();
2021-07-13 08:30:10 +02:00
if (IsGoingToCar && Game.Player.Character.IsInVehicle())
{
IsGoingToCar = false;
}
2021-07-07 13:36:25 +02:00
if (!MainNetworking.IsOnServer())
{
return;
}
2021-08-18 11:47:59 +02:00
#if DEBUG
if (MainNetworking.ShowNetworkInfo)
{
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 0), $"L: {MainNetworking.Latency * 1000:N0}ms", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
2021-11-19 12:30:47 +01:00
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 30), $"R: {MainNetworking.BytesReceived * 0.000001} mb", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 60), $"S: {MainNetworking.BytesSend * 0.000001} mb", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
2021-08-18 11:47:59 +02:00
}
#endif
2021-07-07 13:36:25 +02:00
MainChat.Tick();
// Display all players
2021-08-16 14:03:05 +02:00
foreach (KeyValuePair<long, EntitiesPlayer> player in Players)
2021-07-07 13:36:25 +02:00
{
player.Value.DisplayLocally(player.Value.Username);
}
2021-08-18 11:47:59 +02:00
#if DEBUG
2021-07-07 13:36:25 +02:00
if (UseDebug)
{
Debug();
}
2021-08-18 11:47:59 +02:00
#endif
2021-07-07 13:36:25 +02:00
2021-11-28 22:57:39 +01:00
if ((Util.GetTickCount64() - LastDataSend) < Util.GetGameMs<ulong>())
2021-07-07 13:36:25 +02:00
{
2021-08-21 16:52:17 +02:00
return;
2021-07-07 13:36:25 +02:00
}
2021-08-21 16:52:17 +02:00
MainNetworking.SendPlayerData();
2021-11-19 22:08:15 +01:00
LastDataSend = Util.GetTickCount64();
2021-07-07 13:36:25 +02:00
}
#if !NON_INTERACTIVE
2021-07-07 13:36:25 +02:00
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (MainChat.Focused)
{
MainChat.OnKeyDown(e.KeyCode);
return;
}
switch (e.KeyCode)
{
case Keys.F9:
if (MainMenu.MenuPool.AreAnyVisible)
2021-07-07 13:36:25 +02:00
{
MainMenu.MainMenu.Visible = false;
MainMenu.SubSettings.MainMenu.Visible = false;
2021-07-07 13:36:25 +02:00
}
else
{
MainMenu.MainMenu.Visible = true;
2021-07-07 13:36:25 +02:00
}
break;
2021-07-13 08:30:10 +02:00
case Keys.G:
if (IsGoingToCar)
{
Game.Player.Character.Task.ClearAll();
IsGoingToCar = false;
}
else if (!Game.Player.Character.IsInVehicle())
{
2021-08-20 17:28:13 +02:00
Vehicle veh = World.GetNearbyVehicles(Game.Player.Character, 5f).FirstOrDefault();
2021-12-10 15:55:45 +01:00
if (veh != null)
2021-07-13 08:30:10 +02:00
{
for (int i = 0; i < veh.PassengerCapacity; i++)
{
if (veh.IsSeatFree((VehicleSeat)i))
{
Game.Player.Character.Task.EnterVehicle(veh, (VehicleSeat)i);
IsGoingToCar = true;
break;
}
}
}
}
break;
2021-09-28 14:44:10 +02:00
default:
if (Game.IsControlJustPressed(GTA.Control.MultiplayerInfo))
{
if (MainNetworking.IsOnServer())
{
2021-11-19 22:08:15 +01:00
ulong currentTimestamp = Util.GetTickCount64();
2021-09-28 14:44:10 +02:00
PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000 ? (currentTimestamp - 6000) : currentTimestamp;
}
return;
}
else if (Game.IsControlJustPressed(GTA.Control.MpTextChatAll))
{
if (MainNetworking.IsOnServer())
{
MainChat.Focused = true;
}
return;
}
break;
2021-07-07 13:36:25 +02:00
}
}
#endif
2021-07-07 13:36:25 +02:00
2021-12-03 20:30:00 +01:00
internal static void CleanUp()
{
2021-08-18 11:47:59 +02:00
MainChat.Clear();
2021-08-16 14:03:05 +02:00
foreach (KeyValuePair<long, EntitiesPlayer> player in Players)
{
player.Value.Character?.AttachedBlip?.Delete();
player.Value.Character?.CurrentVehicle?.Delete();
player.Value.Character?.Kill();
player.Value.Character?.Delete();
player.Value.PedBlip?.Delete();
}
Players.Clear();
foreach (KeyValuePair<long, EntitiesNpc> Npc in NPCs)
{
Npc.Value.Character?.CurrentVehicle?.Delete();
Npc.Value.Character?.Kill();
Npc.Value.Character?.Delete();
}
NPCs.Clear();
NPCsVehicles.Clear();
}
2021-08-22 13:59:15 +02:00
#if DEBUG
2021-11-19 22:08:15 +01:00
private ulong ArtificialLagCounter;
2021-12-03 20:30:00 +01:00
internal static EntitiesPlayer DebugSyncPed;
internal static ulong LastFullDebugSync = 0;
internal static bool UseDebug = false;
2021-07-07 13:36:25 +02:00
private void Debug()
{
2021-08-13 15:20:26 +02:00
Ped player = Game.Player.Character;
2021-08-16 14:03:05 +02:00
if (!Players.ContainsKey(0))
2021-07-07 13:36:25 +02:00
{
2021-08-16 14:03:05 +02:00
Players.Add(0, new EntitiesPlayer() { SocialClubName = "DEBUG", Username = "DebugPlayer" });
DebugSyncPed = Players[0];
2021-07-07 13:36:25 +02:00
}
if ((Util.GetTickCount64() - ArtificialLagCounter) < 56)
2021-07-07 13:36:25 +02:00
{
return;
}
2021-07-07 13:36:25 +02:00
bool fullSync = (Util.GetTickCount64() - LastFullDebugSync) > 500;
2021-08-15 11:33:52 +02:00
if (fullSync)
{
DebugSyncPed.ModelHash = player.Model.Hash;
DebugSyncPed.Clothes = player.GetPedClothes();
}
DebugSyncPed.Health = player.Health;
DebugSyncPed.Position = player.Position;
2021-07-10 23:41:28 +02:00
byte? flags;
2021-12-13 20:35:07 +01:00
if (player.IsInVehicle())
{
2021-12-13 20:35:07 +01:00
Vehicle veh = player.CurrentVehicle;
2021-07-12 07:06:52 +02:00
veh.Opacity = 75;
flags = veh.GetVehicleFlags();
2021-07-10 23:41:28 +02:00
byte secondaryColor;
byte primaryColor;
2021-08-13 15:52:30 +02:00
unsafe
{
Function.Call<byte>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
2021-08-13 15:52:30 +02:00
}
DebugSyncPed.VehicleModelHash = veh.Model.Hash;
DebugSyncPed.VehicleSeatIndex = (short)player.SeatIndex;
DebugSyncPed.VehiclePosition = veh.Position;
DebugSyncPed.VehicleRotation = veh.Quaternion;
2021-08-17 15:04:50 +02:00
DebugSyncPed.VehicleEngineHealth = veh.EngineHealth;
DebugSyncPed.VehRPM = veh.CurrentRPM;
2021-07-11 00:51:46 +02:00
DebugSyncPed.VehicleVelocity = veh.Velocity;
2021-07-10 10:52:43 +02:00
DebugSyncPed.VehicleSpeed = veh.Speed;
2021-07-13 16:30:33 +02:00
DebugSyncPed.VehicleSteeringAngle = veh.SteeringAngle;
DebugSyncPed.AimCoords = veh.IsTurretSeat((int)player.SeatIndex) ? Util.GetVehicleAimCoords() : new GTA.Math.Vector3();
DebugSyncPed.VehicleColors = new byte[] { primaryColor, secondaryColor };
DebugSyncPed.VehicleMods = veh.Mods.GetVehicleMods();
DebugSyncPed.VehDoors = veh.Doors.GetVehicleDoors();
2021-12-17 01:27:42 +01:00
DebugSyncPed.VehTires = veh.Wheels.GetBurstedTires();
DebugSyncPed.LastSyncWasFull = true;
2021-11-19 09:24:06 +01:00
DebugSyncPed.IsInVehicle = true;
2021-07-10 23:41:28 +02:00
DebugSyncPed.VehIsEngineRunning = (flags.Value & (byte)VehicleDataFlags.IsEngineRunning) > 0;
DebugSyncPed.VehAreLightsOn = (flags.Value & (byte)VehicleDataFlags.AreLightsOn) > 0;
DebugSyncPed.VehAreHighBeamsOn = (flags.Value & (byte)VehicleDataFlags.AreHighBeamsOn) > 0;
2021-07-13 12:00:37 +02:00
DebugSyncPed.VehIsSireneActive = (flags.Value & (byte)VehicleDataFlags.IsSirenActive) > 0;
DebugSyncPed.VehicleDead = (flags.Value & (byte)VehicleDataFlags.IsDead) > 0;
DebugSyncPed.IsHornActive = (flags.Value & (byte)VehicleDataFlags.IsHornActive) > 0;
DebugSyncPed.Transformed = (flags.Value & (byte)VehicleDataFlags.IsTransformed) > 0;
2021-12-14 14:46:10 +01:00
DebugSyncPed.VehLandingGear = veh.IsPlane ? (byte)veh.LandingGearState : (byte)0;
2021-07-07 13:36:25 +02:00
2021-08-04 18:54:56 +02:00
if (DebugSyncPed.MainVehicle != null && DebugSyncPed.MainVehicle.Exists() && player.IsInVehicle())
{
Function.Call(Hash.SET_ENTITY_NO_COLLISION_ENTITY, DebugSyncPed.MainVehicle.Handle, veh.Handle, false);
Function.Call(Hash.SET_ENTITY_NO_COLLISION_ENTITY, veh.Handle, DebugSyncPed.MainVehicle.Handle, false);
}
}
else
{
flags = player.GetPedFlags(true);
DebugSyncPed.Rotation = player.Rotation;
DebugSyncPed.Velocity = player.Velocity;
DebugSyncPed.Speed = player.GetPedSpeed();
DebugSyncPed.AimCoords = player.GetPedAimCoords(false);
DebugSyncPed.CurrentWeaponHash = (uint)player.Weapons.Current.Hash;
DebugSyncPed.WeaponComponents = player.Weapons.Current.GetWeaponComponents();
DebugSyncPed.LastSyncWasFull = true;
DebugSyncPed.IsAiming = (flags.Value & (byte)PedDataFlags.IsAiming) > 0;
DebugSyncPed.IsShooting = (flags.Value & (byte)PedDataFlags.IsShooting) > 0;
DebugSyncPed.IsReloading = (flags.Value & (byte)PedDataFlags.IsReloading) > 0;
DebugSyncPed.IsJumping = (flags.Value & (byte)PedDataFlags.IsJumping) > 0;
DebugSyncPed.IsRagdoll = (flags.Value & (byte)PedDataFlags.IsRagdoll) > 0;
DebugSyncPed.IsOnFire = (flags.Value & (byte)PedDataFlags.IsOnFire) > 0;
DebugSyncPed.IsInVehicle = false;
if (DebugSyncPed.Character != null && DebugSyncPed.Character.Exists())
{
Function.Call(Hash.SET_ENTITY_NO_COLLISION_ENTITY, DebugSyncPed.Character.Handle, player.Handle, false);
Function.Call(Hash.SET_ENTITY_NO_COLLISION_ENTITY, player.Handle, DebugSyncPed.Character.Handle, false);
2021-08-04 18:54:56 +02:00
}
}
2021-11-19 22:08:15 +01:00
ulong currentTimestamp = Util.GetTickCount64();
2021-08-15 11:33:52 +02:00
DebugSyncPed.LastUpdateReceived = currentTimestamp;
DebugSyncPed.Latency = currentTimestamp - ArtificialLagCounter;
ArtificialLagCounter = currentTimestamp;
2021-08-13 15:20:26 +02:00
2021-08-15 11:33:52 +02:00
if (fullSync)
{
LastFullDebugSync = currentTimestamp;
}
2021-07-07 13:36:25 +02:00
}
2021-08-22 13:59:15 +02:00
#endif
2021-07-07 13:36:25 +02:00
}
}