RAGECOOP-V/Client/Main.cs

331 lines
12 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
{
public class Main : Script
{
public static RelationshipGroup RelationshipGroup;
private bool GameLoaded = false;
2021-09-28 16:51:16 +02:00
public static readonly string CurrentVersion = "V0_8_0";
2021-07-07 13:36:25 +02:00
public static bool ShareNpcsWithPlayers = false;
2021-08-22 13:59:15 +02:00
public static bool DisableTraffic = false;
2021-07-07 13:36:25 +02:00
public 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
public static Settings MainSettings = Util.ReadSettings();
2021-08-18 11:47:59 +02:00
public static Networking MainNetworking = new Networking();
#if !NON_INTERACTIVE
public static MenusMain MainMenu = new MenusMain();
#endif
2021-08-18 11:47:59 +02:00
public static Chat MainChat = new Chat();
2021-07-07 13:36:25 +02:00
2021-08-26 17:01:32 +02:00
public static long LocalClientID = 0;
2021-08-16 14:03:05 +02:00
public static readonly Dictionary<long, EntitiesPlayer> Players = new Dictionary<long, EntitiesPlayer>();
public static readonly Dictionary<long, EntitiesNpc> Npcs = new Dictionary<long, EntitiesNpc>();
2021-07-07 13:36:25 +02:00
public Main()
{
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
}
private int LastDataSend;
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
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();
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 30), $"R: {MainNetworking.BytesReceived} bytes", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
new LemonUI.Elements.ScaledText(new PointF(Screen.PrimaryScreen.Bounds.Width / 2, 60), $"S: {MainNetworking.BytesSend} bytes", 0.5f) { Alignment = GTA.UI.Alignment.Center }.Draw();
}
#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-08-21 16:52:17 +02:00
if ((Environment.TickCount - LastDataSend) < (1000 / 60))
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();
LastDataSend = Environment.TickCount;
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();
if (veh != default)
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())
{
int currentTimestamp = Environment.TickCount;
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
public 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();
2021-08-16 14:03:05 +02:00
foreach (KeyValuePair<long, EntitiesNpc> Npc in Npcs)
{
Npc.Value.Character?.CurrentVehicle?.Delete();
Npc.Value.Character?.Kill();
Npc.Value.Character?.Delete();
}
Npcs.Clear();
2021-08-18 11:47:59 +02:00
foreach (Ped entity in World.GetAllPeds().Where(p => p.Handle != Game.Player.Character.Handle))
{
2021-08-18 11:47:59 +02:00
entity.Kill();
entity.Delete();
}
2021-08-21 16:52:17 +02:00
foreach (Vehicle veh in World.GetAllVehicles().Where(v => v.Handle != Game.Player.Character.CurrentVehicle?.Handle))
{
2021-08-18 11:47:59 +02:00
veh.Delete();
}
}
2021-08-22 13:59:15 +02:00
#if DEBUG
2021-07-11 04:04:00 +02:00
private int ArtificialLagCounter;
public static EntitiesPlayer DebugSyncPed;
2021-08-15 11:33:52 +02:00
public static int LastFullDebugSync = 0;
public 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
}
2021-08-17 18:41:09 +02:00
if ((Environment.TickCount - ArtificialLagCounter) < 157)
2021-07-07 13:36:25 +02:00
{
return;
}
2021-07-07 13:36:25 +02:00
2021-08-15 11:33:52 +02:00
bool fullSync = (Environment.TickCount - LastFullDebugSync) > 1500;
if (fullSync)
{
DebugSyncPed.ModelHash = player.Model.Hash;
DebugSyncPed.Props = Util.GetPedProps(player);
}
DebugSyncPed.Health = player.Health;
DebugSyncPed.Position = player.Position;
2021-07-10 23:41:28 +02:00
byte? flags;
if (!player.IsInVehicle())
{
2021-08-15 11:33:52 +02:00
flags = Util.GetPedFlags(player, fullSync, true);
2021-07-10 23:41:28 +02:00
2021-07-07 13:36:25 +02:00
DebugSyncPed.Rotation = player.Rotation;
DebugSyncPed.Velocity = player.Velocity;
2021-07-07 15:37:54 +02:00
DebugSyncPed.Speed = Util.GetPedSpeed(player);
DebugSyncPed.AimCoords = Util.GetPedAimCoords(player, false);
2021-07-07 13:36:25 +02:00
DebugSyncPed.CurrentWeaponHash = (int)player.Weapons.Current.Hash;
DebugSyncPed.LastSyncWasFull = (flags.Value & (byte)PedDataFlags.LastSyncWasFull) > 0;
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;
2021-07-10 23:41:28 +02:00
DebugSyncPed.IsInVehicle = (flags.Value & (byte)PedDataFlags.IsInVehicle) > 0;
2021-08-04 18:54:56 +02:00
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-07-07 13:36:25 +02:00
}
else
{
Vehicle veh = player.CurrentVehicle;
2021-07-12 07:06:52 +02:00
veh.Opacity = 75;
2021-08-15 11:33:52 +02:00
flags = Util.GetVehicleFlags(player, veh, fullSync);
2021-07-10 23:41:28 +02:00
2021-08-13 15:52:30 +02:00
int secondaryColor;
int primaryColor;
unsafe
{
Function.Call<int>(Hash.GET_VEHICLE_COLOURS, veh, &primaryColor, &secondaryColor);
}
DebugSyncPed.VehicleModelHash = veh.Model.Hash;
DebugSyncPed.VehicleSeatIndex = (int)player.SeatIndex;
DebugSyncPed.VehiclePosition = veh.Position;
DebugSyncPed.VehicleRotation = veh.Quaternion;
2021-08-17 15:04:50 +02:00
DebugSyncPed.VehicleEngineHealth = veh.EngineHealth;
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;
2021-08-13 15:52:30 +02:00
DebugSyncPed.VehicleColors = new int[] { primaryColor, secondaryColor };
2021-08-22 13:59:15 +02:00
DebugSyncPed.VehicleMods = Util.GetVehicleMods(veh);
DebugSyncPed.VehDoors = Util.GetVehicleDoors(veh.Doors);
2021-07-10 23:41:28 +02:00
DebugSyncPed.LastSyncWasFull = (flags.Value & (byte)VehicleDataFlags.LastSyncWasFull) > 0;
DebugSyncPed.IsInVehicle = (flags.Value & (byte)VehicleDataFlags.IsInVehicle) > 0;
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;
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, player.CurrentVehicle.Handle, false);
Function.Call(Hash.SET_ENTITY_NO_COLLISION_ENTITY, player.CurrentVehicle.Handle, DebugSyncPed.MainVehicle.Handle, false);
}
}
2021-08-15 11:33:52 +02:00
int currentTimestamp = Environment.TickCount;
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
}
}