forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnTable.cs
More file actions
92 lines (78 loc) · 2.89 KB
/
Copy pathSpawnTable.cs
File metadata and controls
92 lines (78 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using L2dotNET.DataContracts;
using L2dotNET.Models.Npcs;
using L2dotNET.Services.Contracts;
using System.Linq;
using System.Timers;
using System;
using System.Threading.Tasks;
using NLog;
namespace L2dotNET.Tables
{
public class SpawnTable : IInitialisable
{
private readonly IServerService _serverService;
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private Timer RespawnTimerTask;
private readonly IdFactory _idFactory;
private readonly Config.Config _config;
public bool Initialised { get; private set; }
public SpawnTable(IServerService serverService, IdFactory idFactory, Config.Config config)
{
_config = config;
_serverService = serverService;
_idFactory = idFactory;
}
public async Task Initialise()
{
if (Initialised)
{
return;
}
List<SpawnlistContract> spawnsList = (await _serverService.GetAllSpawns()).ToList();
spawnsList.ForEach((spawn) =>
{
L2Spawn l2Spawn =
new L2Spawn(NpcTable.GetTemplate(spawn.TemplateId), _idFactory, this)
{
Location = new SpawnLocation(spawn.LocX, spawn.LocY, spawn.LocZ, spawn.Heading, spawn.RespawnDelay)
};
l2Spawn.Spawn(false);
Spawns.Add(l2Spawn);
});
Log.Info($"Spawned {spawnsList.Count} npcs.");
if (_config.GameplayConfig.NpcConfig.Misc.AutoMobRespawn)
{
RespawnTimerTask = new Timer();
RespawnTimerTask.Elapsed += new ElapsedEventHandler(RespawnTimerTick);
RespawnTimerTask.Interval = 2000;
RespawnTimerTask.Start();
Log.Info($"Started RespawnTimerTask.");
}
Initialised = true;
}
private void RespawnTimerTick(object sender, ElapsedEventArgs e)
{
foreach (var kvp in RespawnDict.ToArray())
{
long elapsedTicks = DateTime.Now.Ticks - kvp.Value.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
if (elapsedSpan.TotalMilliseconds >= kvp.Key.Location.RespawnDelay)
{
kvp.Key.Spawn(true);
DeRegisterRespawn(kvp.Key);
}
}
}
public void RegisterRespawn(L2Spawn spawn)
{
RespawnDict.Add(spawn,DateTime.Now);
}
private void DeRegisterRespawn(L2Spawn spawn)
{
RespawnDict.Remove(spawn);
}
public readonly List<L2Spawn> Spawns = new List<L2Spawn>(50000);
private readonly Dictionary<L2Spawn,DateTime> RespawnDict = new Dictionary<L2Spawn, DateTime>();
}
}