Files
RAGECOOP-V/Client/Scripts/Sync/EntityPool.cs

560 lines
19 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-05-22 15:55:26 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
2022-10-23 19:02:39 +08:00
using GTA;
using GTA.Native;
using Lidgren.Network;
using RageCoop.Client.Scripting;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
{
internal class EntityPool
{
public static object PedsLock = new object();
#if BENCHMARK
2022-10-23 19:02:39 +08:00
private static Stopwatch PerfCounter = new Stopwatch();
private static Stopwatch PerfCounter2 = Stopwatch.StartNew();
#endif
2022-10-23 19:02:39 +08:00
2022-08-11 16:29:29 +08:00
#region ACTIVE INSTANCES
2022-05-22 15:55:26 +08:00
2022-08-14 17:08:43 +08:00
public static Dictionary<int, SyncedPed> PedsByID = new Dictionary<int, SyncedPed>();
public static Dictionary<int, SyncedPed> PedsByHandle = new Dictionary<int, SyncedPed>();
2022-05-22 15:55:26 +08:00
2022-08-14 17:08:43 +08:00
public static Dictionary<int, SyncedVehicle> VehiclesByID = new Dictionary<int, SyncedVehicle>();
public static Dictionary<int, SyncedVehicle> VehiclesByHandle = new Dictionary<int, SyncedVehicle>();
2022-08-14 17:08:43 +08:00
public static Dictionary<int, SyncedProjectile> ProjectilesByID = new Dictionary<int, SyncedProjectile>();
public static Dictionary<int, SyncedProjectile> ProjectilesByHandle = new Dictionary<int, SyncedProjectile>();
2022-08-11 16:29:29 +08:00
public static Dictionary<int, SyncedProp> ServerProps = new Dictionary<int, SyncedProp>();
2022-07-03 15:28:28 +08:00
public static Dictionary<int, Blip> ServerBlips = new Dictionary<int, Blip>();
2022-09-06 21:46:35 +08:00
2022-08-11 16:29:29 +08:00
#endregion
2022-07-03 15:28:28 +08:00
2022-08-11 16:29:29 +08:00
#region LOCKS
2022-07-03 15:28:28 +08:00
2022-08-11 16:29:29 +08:00
public static object VehiclesLock = new object();
public static object ProjectilesLock = new object();
public static object PropsLock = new object();
public static object BlipsLock = new object();
#endregion
2022-10-23 19:02:39 +08:00
2022-07-20 17:50:01 +08:00
public static void Cleanup(bool keepPlayer = true, bool keepMine = true)
2022-05-22 15:55:26 +08:00
{
2022-10-19 19:07:46 +08:00
foreach (var ped in PedsByID.Values.ToArray())
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if ((keepPlayer && ped.ID == Main.LocalPlayerID) ||
(keepMine && ped.OwnerID == Main.LocalPlayerID)) continue;
RemovePed(ped.ID);
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
2022-08-11 16:29:29 +08:00
PedsByID.Clear();
PedsByHandle.Clear();
2022-05-22 15:55:26 +08:00
2022-10-23 19:02:39 +08:00
foreach (var id in VehiclesByID.Keys.ToArray())
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if (keepMine && VehiclesByID[id].OwnerID == Main.LocalPlayerID) continue;
2022-05-22 15:55:26 +08:00
RemoveVehicle(id);
}
2022-10-23 19:02:39 +08:00
2022-08-11 16:29:29 +08:00
VehiclesByID.Clear();
VehiclesByHandle.Clear();
2022-10-19 19:07:46 +08:00
foreach (var p in ProjectilesByID.Values.ToArray())
2022-09-06 21:46:35 +08:00
if (p.Shooter.ID != Main.LocalPlayerID && p.MainProjectile != null && p.MainProjectile.Exists())
p.MainProjectile.Delete();
2022-08-11 16:29:29 +08:00
ProjectilesByID.Clear();
ProjectilesByHandle.Clear();
2022-07-02 17:14:56 +08:00
2022-10-23 19:02:39 +08:00
foreach (var p in ServerProps.Values) p?.MainProp?.Delete();
2022-07-02 17:14:56 +08:00
ServerProps.Clear();
2022-07-03 15:28:28 +08:00
2022-07-20 17:50:01 +08:00
foreach (var b in ServerBlips.Values)
2022-07-03 15:28:28 +08:00
if (b.Exists())
b.Delete();
ServerBlips.Clear();
2022-05-22 15:55:26 +08:00
}
#region PEDS
2022-10-23 19:02:39 +08:00
public static SyncedPed GetPedByID(int id)
{
return PedsByID.TryGetValue(id, out var p) ? p : null;
}
public static SyncedPed GetPedByHandle(int handle)
{
return PedsByHandle.TryGetValue(handle, out var p) ? p : null;
}
public static List<int> GetPedIDs()
{
return new List<int>(PedsByID.Keys);
}
2022-05-22 15:55:26 +08:00
public static bool AddPlayer()
{
2022-10-23 19:02:39 +08:00
var p = Game.Player.Character;
2022-08-20 12:33:55 +08:00
// var clipset=p.Gender==Gender.Male? "MOVE_M@TOUGH_GUY@" : "MOVE_F@TOUGH_GUY@";
// Function.Call(Hash.SET_PED_MOVEMENT_CLIPSET,p,clipset,1f);
2022-10-23 19:02:39 +08:00
var player = GetPedByID(Main.LocalPlayerID);
2022-09-06 21:46:35 +08:00
if (player == null)
2022-05-22 15:55:26 +08:00
{
Main.Logger.Debug($"Creating SyncEntity for player, handle:{p.Handle}");
2022-10-23 19:02:39 +08:00
var c = new SyncedPed(p);
2022-08-11 14:29:18 +02:00
Main.LocalPlayerID = c.OwnerID = c.ID;
2022-05-22 15:55:26 +08:00
Add(c);
2022-07-11 11:59:32 +08:00
Main.Logger.Debug($"Local player ID is:{c.ID}");
2022-07-20 17:50:01 +08:00
PlayerList.SetPlayer(c.ID, Main.Settings.Username);
2022-05-22 15:55:26 +08:00
return true;
}
2022-08-11 14:29:18 +02:00
if (player.MainPed != p)
{
// Player model changed
player.MainPed = p;
// Remove it from Handle_Characters
var pairs = PedsByHandle.Where(x => x.Value == player);
if (pairs.Any())
{
var pair = pairs.First();
// Re-add
PedsByHandle.Remove(pair.Key);
2022-10-23 19:02:39 +08:00
if (PedsByHandle.ContainsKey(p.Handle)) RemovePed(PedsByHandle[p.Handle].ID);
2022-08-11 14:29:18 +02:00
PedsByHandle.Add(p.Handle, player);
}
}
2022-10-23 19:02:39 +08:00
2022-05-22 15:55:26 +08:00
return false;
}
2022-10-23 19:02:39 +08:00
2022-05-22 15:55:26 +08:00
public static void Add(SyncedPed c)
{
2022-08-11 16:29:29 +08:00
if (PedsByID.ContainsKey(c.ID))
2022-09-06 21:46:35 +08:00
PedsByID[c.ID] = c;
2022-05-22 15:55:26 +08:00
else
2022-08-11 16:29:29 +08:00
PedsByID.Add(c.ID, c);
2022-10-23 19:02:39 +08:00
if (c.MainPed == null) return;
2022-08-11 16:29:29 +08:00
if (PedsByHandle.ContainsKey(c.MainPed.Handle))
2022-09-06 21:46:35 +08:00
PedsByHandle[c.MainPed.Handle] = c;
2022-05-22 15:55:26 +08:00
else
2022-08-11 16:29:29 +08:00
PedsByHandle.Add(c.MainPed.Handle, c);
2022-10-23 19:02:39 +08:00
if (c.IsLocal) API.Events.InvokePedSpawned(c);
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
2022-07-20 17:50:01 +08:00
public static void RemovePed(int id, string reason = "Cleanup")
2022-05-22 15:55:26 +08:00
{
2022-08-11 16:29:29 +08:00
if (PedsByID.ContainsKey(id))
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
var c = PedsByID[id];
2022-05-22 15:55:26 +08:00
var p = c.MainPed;
2022-09-06 21:46:35 +08:00
if (p != null)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if (PedsByHandle.ContainsKey(p.Handle)) PedsByHandle.Remove(p.Handle);
2022-07-01 12:22:31 +08:00
// Main.Logger.Debug($"Removing ped {c.ID}. Reason:{reason}");
2022-05-22 15:55:26 +08:00
p.AttachedBlip?.Delete();
p.Kill();
2022-07-17 14:23:19 +08:00
p.Model.MarkAsNoLongerNeeded();
p.MarkAsNoLongerNeeded();
2022-05-22 15:55:26 +08:00
p.Delete();
}
2022-10-23 19:02:39 +08:00
2022-05-22 15:55:26 +08:00
c.PedBlip?.Delete();
c.ParachuteProp?.Delete();
2022-08-11 16:29:29 +08:00
PedsByID.Remove(id);
2022-10-23 19:02:39 +08:00
if (c.IsLocal) API.Events.InvokePedDeleted(c);
2022-05-22 15:55:26 +08:00
}
}
2022-10-23 19:02:39 +08:00
#endregion
2022-05-22 15:55:26 +08:00
#region VEHICLES
2022-10-23 19:02:39 +08:00
public static SyncedVehicle GetVehicleByID(int id)
{
return VehiclesByID.TryGetValue(id, out var v) ? v : null;
}
public static SyncedVehicle GetVehicleByHandle(int handle)
{
return VehiclesByHandle.TryGetValue(handle, out var v) ? v : null;
}
public static List<int> GetVehicleIDs()
{
return new List<int>(VehiclesByID.Keys);
}
2022-05-22 15:55:26 +08:00
public static void Add(SyncedVehicle v)
{
2022-08-11 16:29:29 +08:00
if (VehiclesByID.ContainsKey(v.ID))
2022-09-06 21:46:35 +08:00
VehiclesByID[v.ID] = v;
2022-05-22 15:55:26 +08:00
else
2022-08-11 16:29:29 +08:00
VehiclesByID.Add(v.ID, v);
2022-10-23 19:02:39 +08:00
if (v.MainVehicle == null) return;
2022-08-11 16:29:29 +08:00
if (VehiclesByHandle.ContainsKey(v.MainVehicle.Handle))
2022-09-06 21:46:35 +08:00
VehiclesByHandle[v.MainVehicle.Handle] = v;
2022-05-22 15:55:26 +08:00
else
2022-08-11 16:29:29 +08:00
VehiclesByHandle.Add(v.MainVehicle.Handle, v);
2022-10-23 19:02:39 +08:00
if (v.IsLocal) API.Events.InvokeVehicleSpawned(v);
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
2022-07-20 17:50:01 +08:00
public static void RemoveVehicle(int id, string reason = "Cleanup")
2022-05-22 15:55:26 +08:00
{
2022-08-11 16:29:29 +08:00
if (VehiclesByID.ContainsKey(id))
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
var v = VehiclesByID[id];
2022-05-22 15:55:26 +08:00
var veh = v.MainVehicle;
2022-09-06 21:46:35 +08:00
if (veh != null)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if (VehiclesByHandle.ContainsKey(veh.Handle)) VehiclesByHandle.Remove(veh.Handle);
2022-07-01 12:22:31 +08:00
// Main.Logger.Debug($"Removing vehicle {v.ID}. Reason:{reason}");
2022-05-22 15:55:26 +08:00
veh.AttachedBlip?.Delete();
2022-07-17 14:23:19 +08:00
veh.Model.MarkAsNoLongerNeeded();
veh.MarkAsNoLongerNeeded();
2022-05-22 15:55:26 +08:00
veh.Delete();
}
2022-10-23 19:02:39 +08:00
2022-08-11 16:29:29 +08:00
VehiclesByID.Remove(id);
2022-10-23 19:02:39 +08:00
if (v.IsLocal) API.Events.InvokeVehicleDeleted(v);
2022-05-22 15:55:26 +08:00
}
}
#endregion
2022-05-23 19:19:56 +08:00
#region PROJECTILES
2022-10-23 19:02:39 +08:00
public static SyncedProjectile GetProjectileByID(int id)
{
2022-09-06 21:46:35 +08:00
return ProjectilesByID.TryGetValue(id, out var p) ? p : null;
}
2022-10-23 19:02:39 +08:00
public static void Add(SyncedProjectile p)
{
2022-10-23 19:02:39 +08:00
if (!p.IsValid) return;
2022-09-06 21:46:35 +08:00
if (p.WeaponHash == (WeaponHash)VehicleWeaponHash.Tank)
{
Networking.SendBullet(((SyncedVehicle)p.Shooter).MainVehicle.Driver.GetSyncEntity().ID, (uint)VehicleWeaponHash.Tank, p.Position + p.Velocity);
return;
}
2022-08-11 16:29:29 +08:00
if (ProjectilesByID.ContainsKey(p.ID))
2022-09-06 21:46:35 +08:00
ProjectilesByID[p.ID] = p;
else
2022-08-11 16:29:29 +08:00
ProjectilesByID.Add(p.ID, p);
2022-10-23 19:02:39 +08:00
if (p.MainProjectile == null) return;
2022-08-11 16:29:29 +08:00
if (ProjectilesByHandle.ContainsKey(p.MainProjectile.Handle))
2022-09-06 21:46:35 +08:00
ProjectilesByHandle[p.MainProjectile.Handle] = p;
else
2022-08-11 16:29:29 +08:00
ProjectilesByHandle.Add(p.MainProjectile.Handle, p);
}
2022-10-23 19:02:39 +08:00
public static void RemoveProjectile(int id, string reason)
{
2022-08-11 16:29:29 +08:00
if (ProjectilesByID.ContainsKey(id))
{
2022-10-23 19:02:39 +08:00
var sp = ProjectilesByID[id];
var p = sp.MainProjectile;
2022-09-06 21:46:35 +08:00
if (p != null)
{
2022-10-23 19:02:39 +08:00
if (ProjectilesByHandle.ContainsKey(p.Handle)) ProjectilesByHandle.Remove(p.Handle);
Main.Logger.Debug($"Removing projectile {sp.ID}. Reason:{reason}");
p.Explode();
}
2022-10-23 19:02:39 +08:00
2022-08-11 16:29:29 +08:00
ProjectilesByID.Remove(id);
}
}
2022-05-23 19:19:56 +08:00
2022-10-23 19:02:39 +08:00
public static bool PedExists(int id)
{
return PedsByID.ContainsKey(id);
}
public static bool VehicleExists(int id)
{
return VehiclesByID.ContainsKey(id);
}
public static bool ProjectileExists(int id)
{
return ProjectilesByID.ContainsKey(id);
}
#endregion
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
private static int vehStateIndex;
private static int pedStateIndex;
private static int vehStatesPerFrame;
private static int pedStatesPerFrame;
private static int i;
2022-05-22 15:55:26 +08:00
public static void DoSync()
{
2022-08-08 17:03:41 +08:00
UpdateTargets();
#if BENCHMARK
2022-05-22 15:55:26 +08:00
PerfCounter.Restart();
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.CheckProjectiles] = PerfCounter.ElapsedTicks;
#endif
2022-11-30 20:17:06 +08:00
var allPeds = World.GetAllPeds();
var allVehicles = World.GetAllVehicles();
var allProjectiles = World.GetAllProjectiles();
2022-09-06 21:46:35 +08:00
vehStatesPerFrame = allVehicles.Length * 2 / (int)Game.FPS + 1;
pedStatesPerFrame = allPeds.Length * 2 / (int)Game.FPS + 1;
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.GetAllEntities] = PerfCounter.ElapsedTicks;
2022-07-01 12:22:31 +08:00
#endif
lock (ProjectilesLock)
{
2022-10-23 19:02:39 +08:00
foreach (var p in allProjectiles)
2022-08-11 16:29:29 +08:00
if (!ProjectilesByHandle.ContainsKey(p.Handle))
Add(new SyncedProjectile(p));
2022-10-23 19:02:39 +08:00
foreach (var p in ProjectilesByID.Values.ToArray())
// Outgoing sync
if (p.IsLocal)
{
2022-09-06 21:46:35 +08:00
if (p.MainProjectile.AttachedEntity == null)
{
2022-07-01 13:54:18 +08:00
// Prevent projectiles from exploding next to vehicle
2022-10-23 19:02:39 +08:00
if (p.WeaponHash == (WeaponHash)VehicleWeaponHash.Tank ||
(p.MainProjectile.OwnerEntity?.EntityType == EntityType.Vehicle &&
p.MainProjectile.Position.DistanceTo(p.Origin) < 2)) continue;
Networking.SendProjectile(p);
}
}
else // Incoming sync
{
if (p.Exploded || p.IsOutOfSync)
RemoveProjectile(p.ID, "OutOfSync | Exploded");
else
p.Update();
}
}
2022-07-20 17:50:01 +08:00
2022-09-06 21:46:35 +08:00
i = -1;
lock (PedsLock)
2022-05-22 15:55:26 +08:00
{
2022-09-06 21:46:35 +08:00
AddPlayer();
2022-05-22 15:55:26 +08:00
2022-10-23 19:02:39 +08:00
foreach (var p in allPeds)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
var c = GetPedByHandle(p.Handle);
if (c == null && p != Game.Player.Character)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if (allPeds.Length > Main.Settings.WorldPedSoftLimit &&
p.PopulationType == EntityPopulationType.RandomAmbient && !p.IsInVehicle())
{
2022-08-11 14:29:18 +02:00
p.Delete();
continue;
}
2022-10-23 19:02:39 +08:00
2022-07-01 12:22:31 +08:00
// Main.Logger.Trace($"Creating SyncEntity for ped, handle:{p.Handle}");
2022-09-06 21:46:35 +08:00
c = new SyncedPed(p);
2022-05-22 15:55:26 +08:00
2022-09-06 21:46:35 +08:00
Add(c);
2022-05-22 15:55:26 +08:00
}
}
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.AddPeds] = PerfCounter.ElapsedTicks;
#endif
2022-08-11 16:29:29 +08:00
var ps = PedsByID.Values.ToArray();
2022-09-06 21:46:35 +08:00
pedStateIndex += pedStatesPerFrame;
2022-10-23 19:02:39 +08:00
if (pedStateIndex >= ps.Length) pedStateIndex = 0;
2022-08-11 14:29:18 +02:00
2022-10-23 19:02:39 +08:00
foreach (var c in ps)
{
i++;
2022-10-23 19:02:39 +08:00
if (c.MainPed != null && !c.MainPed.Exists())
2022-05-22 15:55:26 +08:00
{
2022-09-06 21:46:35 +08:00
RemovePed(c.ID, "non-existent");
2022-05-22 15:55:26 +08:00
continue;
}
// Outgoing sync
2022-07-02 17:14:56 +08:00
if (c.IsLocal)
2022-05-22 15:55:26 +08:00
{
#if BENCHMARK
var start = PerfCounter2.ElapsedTicks;
#endif
2022-05-22 15:55:26 +08:00
// event check
SyncEvents.Check(c);
2022-10-23 19:02:39 +08:00
Networking.SendPed(c, i - pedStateIndex < pedStatesPerFrame);
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.SendPed] = PerfCounter2.ElapsedTicks-start;
#endif
2022-05-22 15:55:26 +08:00
}
else // Incoming sync
{
#if BENCHMARK
var start = PerfCounter2.ElapsedTicks;
#endif
2022-05-22 15:55:26 +08:00
c.Update();
2022-10-23 19:02:39 +08:00
if (c.IsOutOfSync) RemovePed(c.ID, "OutOfSync");
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.UpdatePed] = PerfCounter2.ElapsedTicks-start;
#endif
2022-05-22 15:55:26 +08:00
}
}
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.PedTotal] = PerfCounter.ElapsedTicks;
#endif
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
2022-08-24 22:38:50 +08:00
var check = Main.Ticked % 100 == 0;
2022-09-06 21:46:35 +08:00
i = -1;
lock (VehiclesLock)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
foreach (var veh in allVehicles)
2022-08-11 16:29:29 +08:00
if (!VehiclesByHandle.ContainsKey(veh.Handle))
2022-05-22 15:55:26 +08:00
{
2022-09-06 21:46:35 +08:00
if (allVehicles.Length > Main.Settings.WorldVehicleSoftLimit)
{
var type = veh.PopulationType;
2022-09-06 21:46:35 +08:00
if (type == EntityPopulationType.RandomAmbient || type == EntityPopulationType.RandomParked)
{
2022-07-20 17:50:01 +08:00
foreach (var p in veh.Occupants)
{
p.Delete();
2022-09-06 21:46:35 +08:00
var c = GetPedByHandle(p.Handle);
2022-10-23 19:02:39 +08:00
if (c != null) RemovePed(c.ID, "ThrottleTraffic");
}
2022-10-23 19:02:39 +08:00
veh.Delete();
continue;
}
}
2022-07-01 12:22:31 +08:00
// Main.Logger.Debug($"Creating SyncEntity for vehicle, handle:{veh.Handle}");
2022-05-22 15:55:26 +08:00
2022-08-11 16:29:29 +08:00
Add(new SyncedVehicle(veh));
2022-05-22 15:55:26 +08:00
}
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.AddVehicles] = PerfCounter.ElapsedTicks;
#endif
2022-08-11 16:29:29 +08:00
var vs = VehiclesByID.Values.ToArray();
2022-09-06 21:46:35 +08:00
vehStateIndex += vehStatesPerFrame;
2022-10-23 19:02:39 +08:00
if (vehStateIndex >= vs.Length) vehStateIndex = 0;
2022-08-11 14:29:18 +02:00
2022-10-23 19:02:39 +08:00
foreach (var v in vs)
2022-05-22 15:55:26 +08:00
{
i++;
2022-10-23 19:02:39 +08:00
if (v.MainVehicle != null && !v.MainVehicle.Exists())
2022-05-22 15:55:26 +08:00
{
2022-08-24 22:38:50 +08:00
RemoveVehicle(v.ID, "non-existent");
2022-05-22 15:55:26 +08:00
continue;
}
2022-10-23 19:02:39 +08:00
if (check) v.SetUpFixedData();
2022-05-22 15:55:26 +08:00
// Outgoing sync
2022-07-02 17:14:56 +08:00
if (v.IsLocal)
2022-05-22 15:55:26 +08:00
{
2022-10-23 19:02:39 +08:00
if (!v.MainVehicle.IsVisible) continue;
SyncEvents.Check(v);
2022-10-23 19:02:39 +08:00
Networking.SendVehicle(v, i - vehStateIndex < vehStatesPerFrame);
2022-05-22 15:55:26 +08:00
}
else // Incoming sync
{
v.Update();
2022-10-23 19:02:39 +08:00
if (v.IsOutOfSync) RemoveVehicle(v.ID, "OutOfSync");
2022-05-22 15:55:26 +08:00
}
}
2022-07-02 17:14:56 +08:00
#if BENCHMARK
2022-10-23 19:02:39 +08:00
Debug.TimeStamps[TimeStamp.VehicleTotal] = PerfCounter.ElapsedTicks;
#endif
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
2022-08-08 17:03:41 +08:00
Networking.Peer.FlushSendQueue();
}
2022-09-06 21:46:35 +08:00
private static void UpdateTargets()
2022-08-08 17:03:41 +08:00
{
2022-09-06 21:46:35 +08:00
Networking.Targets = new List<NetConnection>(PlayerList.Players.Count) { Networking.ServerConnection };
2022-08-08 17:03:41 +08:00
foreach (var p in PlayerList.Players.Values.ToArray())
2022-09-06 21:46:35 +08:00
if (p.HasDirectConnection && p.Position.DistanceTo(Main.PlayerPosition) < 500)
2022-08-08 17:03:41 +08:00
Networking.Targets.Add(p.Connection);
2022-05-22 15:55:26 +08:00
}
2022-05-22 15:55:26 +08:00
public static void RemoveAllFromPlayer(int playerPedId)
{
2022-10-23 19:02:39 +08:00
foreach (var p in PedsByID.Values.ToArray())
2022-09-06 21:46:35 +08:00
if (p.OwnerID == playerPedId)
2022-05-22 15:55:26 +08:00
RemovePed(p.ID);
2022-08-11 14:29:18 +02:00
2022-10-23 19:02:39 +08:00
foreach (var v in VehiclesByID.Values.ToArray())
2022-09-06 21:46:35 +08:00
if (v.OwnerID == playerPedId)
2022-05-22 15:55:26 +08:00
RemoveVehicle(v.ID);
}
public static int RequestNewID()
{
2022-10-23 19:02:39 +08:00
var ID = 0;
while (ID == 0 || PedsByID.ContainsKey(ID) || VehiclesByID.ContainsKey(ID) ||
ProjectilesByID.ContainsKey(ID))
{
2022-10-23 19:02:39 +08:00
var rngBytes = new byte[4];
RandomNumberGenerator.Create().GetBytes(rngBytes);
// Convert the bytes into an integer
ID = BitConverter.ToInt32(rngBytes, 0);
}
2022-10-23 19:02:39 +08:00
return ID;
}
2022-10-23 19:02:39 +08:00
2022-05-22 15:55:26 +08:00
private static void SetBudget(int b)
{
Function.Call(Hash.SET_PED_POPULATION_BUDGET, b); // 0 - 3
Function.Call(Hash.SET_VEHICLE_POPULATION_BUDGET, b); // 0 - 3
}
2022-10-23 19:02:39 +08:00
2022-05-23 19:19:56 +08:00
public static string DumpDebug()
{
2022-08-11 14:29:18 +02:00
return $"\nID_Peds: {PedsByID.Count}" +
$"\nHandle_Peds: {PedsByHandle.Count}" +
$"\nID_Vehicles: {VehiclesByID.Count}" +
$"\nHandle_vehicles: {VehiclesByHandle.Count}" +
$"\nID_Projectiles: {ProjectilesByID.Count}" +
$"\nHandle_Projectiles: {ProjectilesByHandle.Count}" +
$"\npedStatesPerFrame: {pedStatesPerFrame}" +
$"\nvehStatesPerFrame: {vehStatesPerFrame}";
2022-05-23 19:19:56 +08:00
}
2022-10-23 19:02:39 +08:00
public static class ThreadSafe
{
public static void Add(SyncedVehicle v)
{
2022-09-06 21:46:35 +08:00
lock (VehiclesLock)
{
EntityPool.Add(v);
}
}
2022-10-23 19:02:39 +08:00
public static void Add(SyncedPed p)
{
2022-09-06 21:46:35 +08:00
lock (PedsLock)
{
EntityPool.Add(p);
}
}
2022-10-23 19:02:39 +08:00
public static void Add(SyncedProjectile sp)
{
2022-09-06 21:46:35 +08:00
lock (ProjectilesLock)
{
EntityPool.Add(sp);
}
}
}
2022-05-22 15:55:26 +08:00
}
2022-10-23 19:02:39 +08:00
}