RAGECOOP-V/Client/Main.cs

336 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;
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-08-15 13:17:57 +02:00
public static readonly string CurrentModVersion = "V0_5_0";
2021-07-07 13:36:25 +02:00
public static bool ShareNpcsWithPlayers = false;
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();
public static MenusMain MainMenu = new MenusMain();
2021-07-07 13:36:25 +02:00
public static Chat MainChat = new Chat();
public static Networking MainNetworking = new Networking();
2021-08-16 14:03:05 +02:00
public static long LocalPlayerID = 0;
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;
KeyDown += OnKeyDown;
Aborted += OnAbort;
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;
}
MainMenu.MenuPool.Process();
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;
}
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);
}
if (UseDebug)
{
Debug();
}
if ((Environment.TickCount - LastDataSend) >= (1000 / 60))
{
MainNetworking.SendPlayerData();
LastDataSend = Environment.TickCount;
}
}
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;
case Keys.T:
if (MainNetworking.IsOnServer())
{
MainChat.Focused = true;
}
break;
case Keys.Y:
if (MainNetworking.IsOnServer())
{
2021-08-13 15:20:26 +02:00
int currentTimestamp = Environment.TickCount;
PlayerList.Pressed = (currentTimestamp - PlayerList.Pressed) < 5000 ? (currentTimestamp - 6000) : currentTimestamp;
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())
{
Vehicle veh = World.GetNearbyVehicles(Game.Player.Character, 5f).First();
if (veh != null)
{
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-07-07 13:36:25 +02:00
}
}
private void OnAbort(object sender, EventArgs e)
{
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();
}
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();
}
}
public static void CleanUp()
{
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();
foreach (Ped entity in World.GetAllPeds())
{
if (entity.Handle != Game.Player.Character.Handle)
{
entity.Kill();
entity.Delete();
}
}
if (!Game.Player.Character.IsInVehicle())
{
foreach (Vehicle vehicle in World.GetAllVehicles())
{
vehicle.Delete();
}
}
else
{
int? playerVehicleHandle = Game.Player.Character.CurrentVehicle?.Handle;
foreach (Vehicle vehicle in World.GetAllVehicles())
{
if (playerVehicleHandle != vehicle.Handle)
{
vehicle.Delete();
}
}
}
}
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 };
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.VehIsInBurnout = (flags.Value & (byte)VehicleDataFlags.IsInBurnout) > 0;
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
}
}
}