2021-07-07 13:36:25 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
using GTA;
|
|
|
|
|
|
|
|
|
|
namespace CoopClient.Entities
|
|
|
|
|
{
|
2021-12-03 20:30:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Don't use it!
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EntitiesThread : Script
|
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 EntitiesThread()
|
|
|
|
|
{
|
2021-12-14 14:20:41 +01:00
|
|
|
|
// Required for some synchronization!
|
|
|
|
|
if (Game.Version < GameVersion.v1_0_1290_1_Steam)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 13:36:25 +02:00
|
|
|
|
Tick += OnTick;
|
2021-11-28 22:57:39 +01:00
|
|
|
|
Interval = Util.GetGameMs<int>();
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (Game.IsLoading || !Main.MainNetworking.IsOnServer() || !Main.NpcsAllowed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 14:20:41 +01:00
|
|
|
|
Dictionary<long, EntitiesNpc> localNPCs = null;
|
|
|
|
|
lock (Main.NPCs)
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
2021-12-14 14:20:41 +01:00
|
|
|
|
localNPCs = new Dictionary<long, EntitiesNpc>(Main.NPCs);
|
2021-07-10 09:41:17 +02:00
|
|
|
|
|
2021-11-19 22:08:15 +01:00
|
|
|
|
ulong tickCount = Util.GetTickCount64();
|
2021-12-14 14:20:41 +01:00
|
|
|
|
foreach (KeyValuePair<long, EntitiesNpc> npc in new Dictionary<long, EntitiesNpc>(localNPCs))
|
2021-07-13 07:56:32 +02:00
|
|
|
|
{
|
2021-09-26 22:27:49 +02:00
|
|
|
|
if ((tickCount - npc.Value.LastUpdateReceived) > 3000)
|
2021-07-11 04:45:56 +02:00
|
|
|
|
{
|
2021-12-13 17:48:00 +01:00
|
|
|
|
if (npc.Value.Character != null && npc.Value.Character.Exists() && !npc.Value.Character.IsDead)
|
2021-09-26 22:27:49 +02:00
|
|
|
|
{
|
|
|
|
|
npc.Value.Character.Kill();
|
|
|
|
|
npc.Value.Character.MarkAsNoLongerNeeded();
|
|
|
|
|
npc.Value.Character.Delete();
|
|
|
|
|
}
|
2021-07-11 04:45:56 +02:00
|
|
|
|
|
2021-12-13 17:48:00 +01:00
|
|
|
|
if (npc.Value.MainVehicle != null && npc.Value.MainVehicle.Exists() && !npc.Value.MainVehicle.IsDead && npc.Value.MainVehicle.IsSeatFree(VehicleSeat.Driver) && npc.Value.MainVehicle.PassengerCount == 0)
|
2021-09-26 22:27:49 +02:00
|
|
|
|
{
|
|
|
|
|
npc.Value.MainVehicle.MarkAsNoLongerNeeded();
|
|
|
|
|
npc.Value.MainVehicle.Delete();
|
|
|
|
|
}
|
2021-07-13 07:56:32 +02:00
|
|
|
|
|
2021-12-14 14:20:41 +01:00
|
|
|
|
localNPCs.Remove(npc.Key);
|
|
|
|
|
Main.NPCs.Remove(npc.Key);
|
2021-07-07 13:36:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 14:20:41 +01:00
|
|
|
|
foreach (EntitiesNpc npc in localNPCs.Values)
|
2021-07-13 07:56:32 +02:00
|
|
|
|
{
|
2021-09-26 22:27:49 +02:00
|
|
|
|
npc.DisplayLocally(null);
|
2021-07-13 07:56:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 13:36:25 +02:00
|
|
|
|
// Only if that player wants to share his NPCs with others
|
|
|
|
|
if (Main.ShareNpcsWithPlayers)
|
|
|
|
|
{
|
|
|
|
|
// Send all npcs from the current player
|
|
|
|
|
foreach (Ped ped in World.GetNearbyPeds(Game.Player.Character.Position, 150f)
|
2021-12-13 17:48:00 +01:00
|
|
|
|
.Where(p => p.Handle != Game.Player.Character.Handle && p.RelationshipGroup != Main.RelationshipGroup)
|
|
|
|
|
.OrderBy(p => (p.Position - Game.Player.Character.Position).Length()))
|
2021-07-07 13:36:25 +02:00
|
|
|
|
{
|
|
|
|
|
Main.MainNetworking.SendNpcData(ped);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|