RAGECOOP-V/Client/Entities/EntitiesThread.cs

73 lines
2.7 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 = (int)(1f / (Game.FPS > 60f ? 60f : Game.FPS) * 1000f);
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-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-11-19 22:08:15 +01:00
ulong tickCount = Util.GetTickCount64();
2021-09-26 22:27:49 +02:00
foreach (KeyValuePair<long, EntitiesNpc> npc in new Dictionary<long, EntitiesNpc>(localNpcs))
{
2021-09-26 22:27:49 +02:00
if ((tickCount - npc.Value.LastUpdateReceived) > 3000)
{
2021-09-26 22:27:49 +02:00
if (npc.Value.Character != null && npc.Value.Character.Exists() && npc.Value.Health > 0)
{
npc.Value.Character.Kill();
npc.Value.Character.MarkAsNoLongerNeeded();
npc.Value.Character.Delete();
}
2021-09-26 22:27:49 +02:00
if (npc.Value.MainVehicle != null && npc.Value.MainVehicle.Exists() && npc.Value.MainVehicle.IsSeatFree(VehicleSeat.Driver) && npc.Value.MainVehicle.PassengerCount == 0)
{
npc.Value.MainVehicle.MarkAsNoLongerNeeded();
npc.Value.MainVehicle.Delete();
}
2021-09-26 22:27:49 +02:00
localNpcs.Remove(npc.Key);
Main.Npcs.Remove(npc.Key);
2021-07-07 13:36:25 +02:00
}
}
}
2021-09-26 22:27:49 +02:00
foreach (EntitiesNpc npc in localNpcs.Values)
{
2021-09-26 22:27:49 +02:00
npc.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);
}
}
}
}
}