forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2Object.cs
More file actions
570 lines (457 loc) · 16.1 KB
/
L2Object.cs
File metadata and controls
570 lines (457 loc) · 16.1 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using L2dotNET.model.player;
using L2dotNET.model.zones;
using L2dotNET.model.zones.classes;
using L2dotNET.Models;
using L2dotNET.Network;
using L2dotNET.Network.serverpackets;
using log4net;
using L2dotNET.DataContracts;
using L2dotNET.Models.zones;
namespace L2dotNET.world
{
public abstract class L2Object
{
private readonly ILog _log = LogManager.GetLogger(typeof(L2Object));
public int ObjId;
public SortedList<int, L2Object> KnownObjects = new SortedList<int, L2Object>();
public virtual byte Level { get; set; } = 1;
public virtual bool Dead { get; set; } = false;
public virtual int X { get; set; }
public virtual int Y { get; set; }
public virtual int Z { get; set; }
public virtual int DestX { get; set; }
public virtual int DestY { get; set; }
public virtual int DestZ { get; set; }
public virtual int Heading { get; set; }
public virtual int TeamId { get; set; }
public virtual bool Visible { get; set; } = true;
public virtual string CurrentRegion { get; set; }
public byte ObjectSummonType = 0;
public virtual L2WorldRegion Region { get; set; }
public virtual void OnAction(L2Player player)
{
_log.Debug("Doing Attack with player " + player.Name);
player.DoAttack((L2Character)this);
}
public virtual void OnActionShift(L2Player player)
{
OnAction(player);
}
public virtual void OnForcedAttack(L2Player player) { }
public virtual void SendPacket(GameserverPacket pk) { }
public virtual void OnRemObject(L2Object obj) { }
public virtual void OnAddObject(L2Object obj, GameserverPacket pk, string msg = null) { }
public virtual void BroadcastUserInfo() { }
public virtual void NotifyAction(L2Player player) { }
public virtual void StartAi() { }
public virtual void AddKnownObject(L2Object obj) { }
public virtual void RemoveKnownObject(L2Object obj) { }
public virtual void SendInfo(L2Player player) { }
protected L2Object(int objectId)
{
ObjId = objectId;
}
public virtual void OnSpawn(bool notifyOthers = true)
{
}
public virtual void BroadcastPacket(GameserverPacket pk, bool excludeYourself)
{
if (!excludeYourself)
SendPacket(pk);
_log.Debug("Sending " + pk.GetType().ToString() + "To Known of " + GetKnownPlayers(false).Count);
GetKnownPlayers().ForEach(p => p.SendPacket(pk));
}
public virtual void BroadcastPacket(GameserverPacket pk)
{
BroadcastPacket(pk, false);
}
public virtual void ReduceHp(L2Character attacker, double damage) { }
public virtual void DecayMe()
{
Region = null;
L2World.Instance.RemoveObject(this);
}
public void ClearKnowns(bool deleteMe, params int[] exclude)
{
foreach (L2Object o in KnownObjects.Values)
{
o.OnClearing(this, deleteMe);
if (deleteMe && this is L2Player)
SendPacket(new DeleteObject(o.ObjId));
}
KnownObjects.Clear();
}
public void GetKnowns(int range, int height, bool zones)
{
L2World.Instance.GetObjects(); // GetKnowns(this, range, height, zones);
}
public virtual List<L2Player> GetKnownPlayers(bool excludeSelf = true)
{
L2WorldRegion region = Region;
if (region == null)
return new List<L2Player>();
List<L2Player> result = new List<L2Player>();
if(excludeSelf)
{
region.GetSurroundingRegions().ForEach(reg => result.AddRange(L2World.Instance.GetPlayers().Where(obj => obj != this)));
}
else
{
region.GetSurroundingRegions().ForEach(reg => result.AddRange(L2World.Instance.GetPlayers()));
}
return result;
}
public virtual void SetRegion(L2WorldRegion newRegion)
{
List<L2WorldRegion> oldAreas = new List<L2WorldRegion>();
if (Region != null)
{
Region.RemoveVisibleObject(this);
oldAreas = Region.GetSurroundingRegions();
}
List<L2WorldRegion> newAreas = new List<L2WorldRegion>();
if (newRegion != null)
{
newRegion.AddVisibleObject(this);
newAreas = newRegion.GetSurroundingRegions();
}
foreach (L2WorldRegion region in oldAreas.Where(region => !newAreas.Contains(region)))
{
foreach (L2Object obj in region.GetObjects().Where(obj => obj != this))
{
obj.RemoveKnownObject(this);
RemoveKnownObject(obj);
}
if (this is L2Player && region.IsEmptyNeighborhood())
region.SetActive(false);
}
foreach (L2WorldRegion region in newAreas.Where(region => !oldAreas.Contains(region)))
{
// Update all objects.
foreach (L2Object obj in region.GetObjects().Where(obj => obj != this))
{
obj.AddKnownObject(this);
AddKnownObject(obj);
}
// Activate the new neighbor region.
if (this is L2Player)
region.SetActive(true);
}
Region = newRegion;
}
private void OnClearing(L2Object target, bool deleteMe)
{
lock (KnownObjects)
KnownObjects.Remove(target.ObjId);
if (deleteMe && target is L2Player)
target.SendPacket(new DeleteObject(ObjId));
}
public void SetVisible(bool val)
{
Visible = val;
foreach (L2Object o in KnownObjects.Values)
o.CanView(this);
}
private void CanView(L2Object target)
{
foreach (L2Object o in KnownObjects.Values)
o.OnClearing(this, true);
}
public void AddKnownObject(L2Object obj, GameserverPacket pk, bool pkuse)
{
if (KnownObjects.ContainsKey(obj.ObjId))
return;
KnownObjects.Add(obj.ObjId, obj);
if (!obj.Visible)
return;
if (pkuse)
OnAddObject(obj, pk);
}
public void UpdateVisibleStatus()
{
foreach (L2Object o in KnownObjects.Values.Where(o => o.Visible))
OnAddObject(o, null);
}
public void RemoveKnownObject(L2Object obj, bool update)
{
if (!KnownObjects.ContainsKey(obj.ObjId))
return;
OnRemObject(obj);
lock (KnownObjects)
KnownObjects.Remove(obj.ObjId);
}
public void Revalidate(L2Object obj)
{
if (KnownObjects.ContainsKey(obj.ObjId))
return;
KnownObjects.Add(obj.ObjId, obj);
if (obj.Visible)
OnAddObject(obj, null);
}
public bool IsInsideRadius(L2Object o, int radius, bool checkZ, bool strictCheck)
{
return IsInsideRadius(o.X, o.Y, o.Z, radius, checkZ, strictCheck);
}
public bool IsInsideRadius(int x, int y, int radius, bool strictCheck)
{
return IsInsideRadius(x, y, 0, radius, false, strictCheck);
}
public bool IsInsideRadius(int x, int y, int z, int radius, bool checkZ, bool strictCheck)
{
double dx = x - X;
double dy = y - Y;
double dz = z - Z;
if (strictCheck)
{
if (checkZ)
return ((dx * dx) + (dy * dy) + (dz * dz)) < (radius * radius);
return ((dx * dx) + (dy * dy)) < (radius * radius);
}
if (checkZ)
return ((dx * dx) + (dy * dy) + (dz * dz)) <= (radius * radius);
return ((dx * dx) + (dy * dy)) <= (radius * radius);
}
public SortedList<int, L2Zone> ActiveZones = new SortedList<int, L2Zone>();
private bool _isInsidePeaceZone,
_isInsidePvpZone,
_isInsideWaterZone;
//private bool _isInsideSSQZone = false;
private const bool IsInsideSiegeZone = false;
private const bool IsInsideSomeDungeon = false;
public bool IsInDanger = false;
public bool IsInSiege()
{
return IsInsideSiegeZone;
}
public bool IsInDungeon()
{
return IsInsideSomeDungeon;
}
public int LastCode = -1;
private bool _forceSetPvp;
public bool IsInCombat = false;
public void SetForcedPvpZone(bool val)
{
_forceSetPvp = val;
ValidateZoneCompass();
ValidateBattleZones();
}
public virtual void ValidateZoneCompass()
{
if (_forceSetPvp)
{
if (LastCode != ExSetCompassZoneCode.Pvpzone)
{
LastCode = ExSetCompassZoneCode.Pvpzone;
SendPacket(new ExSetCompassZoneCode(ExSetCompassZoneCode.Pvpzone));
return;
}
}
int code;
if (_isInsidePvpZone)
code = ExSetCompassZoneCode.Pvpzone;
else
code = _isInsidePeaceZone ? ExSetCompassZoneCode.Peacezone : ExSetCompassZoneCode.Generalzone;
if (code == 0)
return;
if ((LastCode != -1) && (LastCode != code))
{
LastCode = code;
SendPacket(new ExSetCompassZoneCode(code));
}
else
{
LastCode = code;
SendPacket(new ExSetCompassZoneCode(code));
}
}
public void OnEnterZone(L2Zone z)
{
if (ActiveZones.ContainsKey(z.ZoneId))
return;
if (this is L2Player)
((L2Player)this).SendMessage($"entered zone {z.Name}");
ActiveZones.Add(z.ZoneId, z);
z.OnEnter(this);
RevalidateZone(z);
ValidateZoneCompass();
}
public void OnExitZone(L2Zone z, bool cls)
{
if (!ActiveZones.ContainsKey(z.ZoneId))
return;
lock (ActiveZones)
ActiveZones.Remove(z.ZoneId);
z.OnExit(this, cls);
RevalidateZone(z);
ValidateZoneCompass();
}
private void RevalidateZone(L2Zone z)
{
if (z is peace_zone)
ValidatePeaceZones();
else
{
if (z is battle_zone)
ValidateBattleZones();
else
{
if (z is water)
ValidateWaterZones();
}
}
}
public bool IsInBattle()
{
return _isInsidePvpZone;
}
public bool IsInPeace()
{
return !_isInsidePvpZone && _isInsidePeaceZone;
}
public bool IsInWater()
{
return _isInsideWaterZone;
}
public bool isInCombat()
{
return IsInCombat;
}
public void ValidatePeaceZones()
{
bool found = false,
old = _isInsidePeaceZone;
if (ActiveZones.Values.OfType<peace_zone>().Any())
{
_isInsidePeaceZone = true;
found = true;
}
if (!found)
_isInsidePeaceZone = false;
if (!old && _isInsidePeaceZone)
{
if (this is L2Player)
((L2Player)this).SendSystemMessage(SystemMessage.SystemMessageId.EnterPeacefulZone);
}
else
{
if (!old || _isInsidePeaceZone)
return;
if (this is L2Player)
((L2Player)this).SendSystemMessage(SystemMessage.SystemMessageId.ExitPeacefulZone);
}
}
public void ValidateBattleZones()
{
bool found = false,
old = _isInsidePvpZone;
if (!_forceSetPvp)
{
if (ActiveZones.Values.OfType<battle_zone>().Any())
{
_isInsidePvpZone = true;
found = true;
}
}
else
{
old = false;
_isInsidePvpZone = true;
found = true;
}
if (!found)
_isInsidePvpZone = false;
if (!old && _isInsidePvpZone)
{
if (this is L2Player)
((L2Player)this).SendSystemMessage(SystemMessage.SystemMessageId.EnteredCombatZone);
}
else
{
if (!old || _isInsidePvpZone)
return;
if (this is L2Player)
((L2Player)this).SendSystemMessage(SystemMessage.SystemMessageId.LeftCombatZone);
}
}
public virtual void SpawnMe(bool notifyOthers = true)
{
Region = L2World.Instance.GetRegion(new Location(X, Y, Z));
L2World.Instance.AddObject(this);
OnSpawn(notifyOthers);
}
public void ValidateWaterZones()
{
//bool found = false;
//foreach (L2Zone z in _activeZones.Values)
//{
// if (z is water)
// {
// _isInsideWaterZone = true;
// found = true;
// break;
// }
//}
//if(!found)
_isInsideWaterZone = (Z > -4779) && (Z < -3779);
if (this is L2Player)
((L2Player)this).WaterTimer();
}
public void ValidateVisibleObjects(int x, int y, bool zones)
{
//int range = 4000;
//int height = 1600;
if (IsInSiege())
{
//range = 2600;
//height = 1000;
}
//L2World.Instance.CheckToUpdate(this, x, y, range, height, true, zones);
}
public Timer RegenerationMethod_1S,
RegenUpdate;
public int RegenUpdateInterval = 3000;
public virtual void StartRegeneration()
{
if (RegenerationMethod_1S == null)
{
RegenerationMethod_1S = new Timer
{
Interval = 1000
};
RegenerationMethod_1S.Elapsed += new ElapsedEventHandler(RegenTaskDone);
}
if (RegenUpdate == null)
{
RegenUpdate = new Timer
{
Interval = RegenUpdateInterval
};
RegenUpdate.Elapsed += new ElapsedEventHandler(RegenUpdateTaskDone);
}
RegenerationMethod_1S.Enabled = true;
RegenUpdate.Enabled = true;
}
public virtual void RegenTaskDone(object sender, ElapsedEventArgs e) { }
public virtual void RegenUpdateTaskDone(object sender, ElapsedEventArgs e) { }
public void StopRegeneration()
{
if (RegenerationMethod_1S != null)
RegenerationMethod_1S.Enabled = false;
if (RegenUpdate != null)
RegenUpdate.Enabled = false;
}
public virtual double Radius => 11;
public virtual double Height => 22;
public virtual string AsString()
{
return $"L2Object: {ObjId}";
}
public virtual void BroadcastUserInfoToObject(L2Object l2Object)
{
}
}
}