RAGECOOP-V/Client/Entities/EntitiesThread.cs

83 lines
2.8 KiB
C#
Raw Normal View History

2021-07-07 13:36:25 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using GTA;
namespace CoopClient.Entities
{
public class EntitiesThread : Script
{
public EntitiesThread()
{
Tick += OnTick;
Interval = 1000 / 60;
}
private void OnTick(object sender, EventArgs e)
{
if (Game.IsLoading || !Main.MainNetworking.IsOnServer() || !Main.NpcsAllowed)
{
return;
}
2021-08-16 14:03:05 +02:00
Dictionary<long, EntitiesNpc> localNpcs = null;
2021-07-07 13:36:25 +02:00
lock (Main.Npcs)
{
2021-08-16 14:03:05 +02:00
localNpcs = new Dictionary<long, EntitiesNpc>(Main.Npcs);
}
2021-07-07 13:36:25 +02:00
int tickCount = Environment.TickCount;
for (int i = localNpcs.Count - 1; i >= 0; i--)
{
2021-08-16 14:03:05 +02:00
long key = localNpcs.ElementAt(i).Key;
2021-08-21 16:52:17 +02:00
if ((tickCount - localNpcs[key].LastUpdateReceived) > 3000)
{
if (localNpcs[key].Character != null && localNpcs[key].Character.Exists() && localNpcs[key].Health > 0)
{
localNpcs[key].Character.Kill();
localNpcs[key].Character.Delete();
}
if (localNpcs[key].MainVehicle != null && localNpcs[key].MainVehicle.Exists() && localNpcs[key].MainVehicle.PassengerCount == 0)
{
localNpcs[key].MainVehicle.Delete();
}
localNpcs.Remove(key);
}
}
lock (Main.Npcs)
{
2021-08-16 14:03:05 +02:00
foreach (KeyValuePair<long, EntitiesNpc> npc in new Dictionary<long, EntitiesNpc>(Main.Npcs))
{
if (!localNpcs.ContainsKey(npc.Key))
2021-07-07 13:36:25 +02:00
{
Main.Npcs.Remove(npc.Key);
2021-07-07 13:36:25 +02:00
}
}
}
for (int i = 0; i < localNpcs.Count; i++)
{
localNpcs.ElementAt(i).Value.DisplayLocally(null);
}
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)
.Where(p => p.Handle != Game.Player.Character.Handle && !p.IsDead && p.RelationshipGroup != Main.RelationshipGroup)
.OrderBy(p => (p.Position - Game.Player.Character.Position).Length())
.Take((Main.MainSettings.StreamedNpc > 20 || Main.MainSettings.StreamedNpc < 0) ? 0 : Main.MainSettings.StreamedNpc))
2021-07-07 13:36:25 +02:00
{
Main.MainNetworking.SendNpcData(ped);
}
}
}
}
}