forked from SecretsOTheP/EQMacEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.cpp
5312 lines (4603 loc) · 125 KB
/
entity.cpp
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2003 EQEMu Development Team (http://eqemulator.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "../common/global_define.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <iostream>
#ifdef _WINDOWS
#include <process.h>
#else
#include <pthread.h>
#include "../common/unix.h"
#endif
#include "../common/features.h"
#include "../common/guilds.h"
#include "guild_mgr.h"
#include "petitions.h"
#include "quest_parser_collection.h"
#include "raids.h"
#include "string_ids.h"
#include "worldserver.h"
#ifdef _WINDOWS
#define snprintf _snprintf
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
extern Zone *zone;
extern volatile bool is_zone_loaded;
extern WorldServer worldserver;
extern uint32 numclients;
extern PetitionList petition_list;
extern char errorname[32];
Entity::Entity()
{
id = 0;
}
Entity::~Entity()
{
}
Client *Entity::CastToClient()
{
if (this == 0x00) {
Log(Logs::General, Logs::Error, "CastToClient error (nullptr)");
return 0;
}
#ifdef _EQDEBUG
if (!IsClient()) {
Log(Logs::General, Logs::Error, "CastToClient error (not client)");
return 0;
}
#endif
return static_cast<Client *>(this);
}
NPC *Entity::CastToNPC()
{
#ifdef _EQDEBUG
if (!IsNPC()) {
Log(Logs::General, Logs::Error, "CastToNPC error (Not NPC)");
return 0;
}
#endif
return static_cast<NPC *>(this);
}
Mob *Entity::CastToMob()
{
#ifdef _EQDEBUG
if (!IsMob()) {
std::cout << "CastToMob error" << std::endl;
return 0;
}
#endif
return static_cast<Mob *>(this);
}
Trap *Entity::CastToTrap()
{
#ifdef DEBUG
if (!IsTrap()) {
return 0;
}
#endif
return static_cast<Trap *>(this);
}
Corpse *Entity::CastToCorpse()
{
#ifdef _EQDEBUG
if (!IsCorpse()) {
std::cout << "CastToCorpse error" << std::endl;
return 0;
}
#endif
return static_cast<Corpse *>(this);
}
Object *Entity::CastToObject()
{
#ifdef _EQDEBUG
if (!IsObject()) {
std::cout << "CastToObject error" << std::endl;
return 0;
}
#endif
return static_cast<Object *>(this);
}
/*Group* Entity::CastToGroup() {
#ifdef _EQDEBUG
if(!IsGroup()) {
std::cout << "CastToGroup error" << std::endl;
return 0;
}
#endif
return static_cast<Group*>(this);
}*/
Doors *Entity::CastToDoors()
{
return static_cast<Doors *>(this);
}
Beacon *Entity::CastToBeacon()
{
return static_cast<Beacon *>(this);
}
Encounter *Entity::CastToEncounter()
{
return static_cast<Encounter *>(this);
}
const Client *Entity::CastToClient() const
{
if (this == 0x00) {
std::cout << "CastToClient error (nullptr)" << std::endl;
return 0;
}
#ifdef _EQDEBUG
if (!IsClient()) {
std::cout << "CastToClient error (not client?)" << std::endl;
return 0;
}
#endif
return static_cast<const Client *>(this);
}
const NPC *Entity::CastToNPC() const
{
#ifdef _EQDEBUG
if (!IsNPC()) {
std::cout << "CastToNPC error" << std::endl;
return 0;
}
#endif
return static_cast<const NPC *>(this);
}
const Mob *Entity::CastToMob() const
{
#ifdef _EQDEBUG
if (!IsMob()) {
std::cout << "CastToMob error" << std::endl;
return 0;
}
#endif
return static_cast<const Mob *>(this);
}
const Trap *Entity::CastToTrap() const
{
#ifdef DEBUG
if (!IsTrap()) {
return 0;
}
#endif
return static_cast<const Trap *>(this);
}
const Corpse *Entity::CastToCorpse() const
{
#ifdef _EQDEBUG
if (!IsCorpse()) {
std::cout << "CastToCorpse error" << std::endl;
return 0;
}
#endif
return static_cast<const Corpse *>(this);
}
const Object *Entity::CastToObject() const
{
#ifdef _EQDEBUG
if (!IsObject()) {
std::cout << "CastToObject error" << std::endl;
return 0;
}
#endif
return static_cast<const Object *>(this);
}
const Doors *Entity::CastToDoors() const
{
return static_cast<const Doors *>(this);
}
const Beacon* Entity::CastToBeacon() const
{
return static_cast<const Beacon *>(this);
}
const Encounter* Entity::CastToEncounter() const
{
return static_cast<const Encounter *>(this);
}
EntityList::EntityList()
:
object_timer(5000),
door_timer(5000),
corpse_timer(2000),
corpse_depop_timer(250),
group_timer(1000),
raid_timer(1000),
trap_timer(1000)
{
// set up ids between 1 and 1500
// neither client or server performs well if you have
// enough entities to exhaust this list
for (uint16 i = 1; i <= 4999; i++)
free_ids.push(i);
}
EntityList::~EntityList()
{
//must call this before the list is destroyed, or else it will try to
//delete the NPCs in the list, which it cannot do.
RemoveAllLocalities();
}
bool EntityList::CanAddHateForMob(Mob *p)
{
int count = 0;
auto it = npc_list.begin();
while (it != npc_list.end()) {
NPC *npc = it->second;
if (npc->IsOnHatelist(p))
count++;
// no need to continue if we already hit the limit
if (count > 3)
return false;
++it;
}
if (count <= 2)
return true;
return false;
}
void EntityList::AddClient(Client *client)
{
client->SetID(GetFreeID());
client_list.insert(std::pair<uint16, Client *>(client->GetID(), client));
mob_list.insert(std::pair<uint16, Mob *>(client->GetID(), client));
client->SetLastDistance(client->GetID(), 0.0f);
client->SetInside(client->GetID(), true);
// update distances to us for clients.
auto it = client_list.begin();
// go through the list and update distances
while(it != client_list.end()) {
if (it->second && it->second->GetID() > 0 && it->second != client) {
it->second->SetLastDistance(client->GetID(), DistanceSquaredNoZ(it->second->GetPosition(), client->GetPosition()));
it->second->SetLastPosition(client->GetID(), client->GetPosition());
it->second->SetInside(client->GetID(), false);
}
++it;
}
}
void EntityList::TrapProcess()
{
// MobProcess is the master and sets/disables the timers.
if(zone && RuleB(Zone, IdleWhenEmpty) && !zone->ZoneWillNotIdle())
{
if(numclients < 1 && zone->idle)
{
return;
}
}
if (trap_list.empty()) {
trap_timer.Disable();
return;
}
auto it = trap_list.begin();
while (it != trap_list.end()) {
if (!it->second->Process()) {
safe_delete(it->second);
free_ids.push(it->first);
it = trap_list.erase(it);
} else {
++it;
}
}
}
// Debug function -- checks to see if group_list has any nullptr entries.
// Meant to be called after each group-related function, in order
// to track down bugs.
void EntityList::CheckGroupList (const char *fname, const int fline)
{
std::list<Group *>::iterator it;
for (it = group_list.begin(); it != group_list.end(); ++it)
{
if (*it == nullptr)
{
Log(Logs::General, Logs::Error, "nullptr group, %s:%i", fname, fline);
}
}
}
void EntityList::GroupProcess()
{
if (numclients < 1)
return;
if (group_list.empty()) {
group_timer.Disable();
return;
}
for (auto &group : group_list)
group->Process();
#if EQDEBUG >= 5
CheckGroupList (__FILE__, __LINE__);
#endif
}
void EntityList::RaidProcess()
{
if (numclients < 1)
return;
if (raid_list.empty()) {
raid_timer.Disable();
return;
}
for (auto &raid : raid_list)
raid->Process();
}
void EntityList::DoorProcess()
{
// MobProcess is the master and sets/disables the timers.
if(zone && RuleB(Zone, IdleWhenEmpty) && !zone->ZoneWillNotIdle())
{
if(numclients < 1 && zone->idle)
{
return;
}
}
if (door_list.empty()) {
door_timer.Disable();
return;
}
auto it = door_list.begin();
while (it != door_list.end()) {
if (!it->second->Process()) {
safe_delete(it->second);
free_ids.push(it->first);
it = door_list.erase(it);
}
++it;
}
}
void EntityList::ObjectProcess()
{
// MobProcess is the master and sets/disables the timers.
if(zone && RuleB(Zone, IdleWhenEmpty) && !zone->ZoneWillNotIdle())
{
if(numclients < 1 && zone->idle)
{
return;
}
}
if (object_list.empty()) {
object_timer.Disable();
return;
}
auto it = object_list.begin();
while (it != object_list.end()) {
if (!it->second->Process()) {
safe_delete(it->second);
free_ids.push(it->first);
it = object_list.erase(it);
} else {
++it;
}
}
}
void EntityList::CorpseProcess()
{
if (corpse_list.empty()) {
corpse_timer.Disable(); // No corpses in list
corpse_depop_timer.Disable();
return;
}
auto it = corpse_list.begin();
while (it != corpse_list.end()) {
if (!it->second->Process()) {
safe_delete(it->second);
free_ids.push(it->first);
it = corpse_list.erase(it);
} else {
++it;
}
}
}
void EntityList::CorpseDepopProcess()
{
if (corpse_list.empty()) {
corpse_timer.Disable(); // No corpses in list
corpse_depop_timer.Disable();
return;
}
auto it = corpse_list.begin();
while (it != corpse_list.end()) {
if (!it->second->DepopProcess()) {
safe_delete(it->second);
free_ids.push(it->first);
it = corpse_list.erase(it);
}
else {
++it;
}
}
}
void EntityList::MobProcess()
{
bool mob_dead;
uint16 aggroed_npcs = 0;
auto it = mob_list.begin();
while (it != mob_list.end()) {
uint16 id = it->first;
Mob *mob = it->second;
size_t sz = mob_list.size();
if (zone && RuleB(Zone, IdleWhenEmpty) && !zone->ZoneWillNotIdle() && !zone->IsBoatZone())
{
static Timer* mob_settle_timer = new Timer();
if (numclients < 1 && !mob_settle_timer->Enabled() && !zone->idle)
{
mob_settle_timer->Start(RuleI(Zone, IdleTimer)); // idle timer from when the last player left the zone.
Log(Logs::General, Logs::Status, "Entity Process: Number of clients has dropped to 0. Setting idle timer.");
LogSys.log_settings[Logs::PacketServerClientWithDump].log_to_gmsay = 0;
LogSys.log_settings[Logs::PacketClientServerWithDump].log_to_gmsay = 0;
}
else if (numclients >= 1 && zone->idle)
{
if (mob_settle_timer->Enabled())
mob_settle_timer->Disable();
zone->idle = false;
Log(Logs::General, Logs::Status, "Entity Process: A player has entered the zone, leaving idle state.");
}
if (mob_settle_timer->Check())
{
mob_settle_timer->Disable();
if (numclients < 1)
{
zone->idle = true;
Log(Logs::General, Logs::Status, "Entity Process: Idle timer has expired, zone will now idle.");
}
else
{
zone->idle = false;
Log(Logs::General, Logs::Status, "Entity Process: Idle timer has expired, but there are players in the zone. Zone will not idle.");
}
}
if (zone->process_mobs_while_empty || !zone->idle || numclients > 0 || mob_settle_timer->Enabled() || mob->GetWanderType() == GridOneWayRepop || mob->GetWanderType() == GridOneWayDepop)
{
// Normal processing, or assuring that spawns that should
// path and depop do that. Otherwise all of these type mobs
// will be up and at starting positions when idle zone wakes up.
mob_dead = !mob->Process();
}
else
{
// spawn_events can cause spawns and deaths while zone idled.
// At the very least, process that.
mob_dead = mob->CastToNPC()->GetDepop();
}
}
else
mob_dead = !mob->Process();
size_t a_sz = mob_list.size();
if(a_sz > sz) {
//increased size can potentially screw with iterators so reset it to current value
//if buckets are re-orderered we may skip a process here and there but since
//process happens so often it shouldn't matter much
it = mob_list.find(id);
++it;
} else {
++it;
}
if (mob_dead) {
if(mob->IsNPC()) {
entity_list.RemoveNPC(id);
}
else {
#ifdef _WINDOWS
struct in_addr in;
in.s_addr = mob->CastToClient()->GetIP();
Log(Logs::General, Logs::ZoneServer, "Dropping client: Process=false, ip=%s port=%u", inet_ntoa(in), mob->CastToClient()->GetPort());
#endif
zone->StartShutdownTimer();
Group *g = GetGroupByMob(mob);
if(g) {
Log(Logs::General, Logs::Error, "About to delete a client still in a group.");
g->DelMember(mob);
}
Raid *r = entity_list.GetRaidByClient(mob->CastToClient());
if(r) {
Log(Logs::General, Logs::Error, "About to delete a client still in a raid.");
r->MemberZoned(mob->CastToClient());
}
entity_list.RemoveClient(id);
}
entity_list.RemoveMob(id);
}
else if (mob->IsNPC() && mob->IsEngaged())
{
aggroed_npcs++;
}
}
zone->SetNumAggroedNPCs(aggroed_npcs);
}
void EntityList::BeaconProcess()
{
auto it = beacon_list.begin();
while (it != beacon_list.end()) {
if (!it->second->Process()) {
safe_delete(it->second);
free_ids.push(it->first);
it = beacon_list.erase(it);
} else {
++it;
}
}
}
void EntityList::EncounterProcess()
{
auto it = encounter_list.begin();
while (it != encounter_list.end()) {
if (!it->second->Process()) {
// if Process is returning false here, we probably just got called from ReloadQuests .. oh well
parse->RemoveEncounter(it->second->GetEncounterName());
safe_delete(it->second);
free_ids.push(it->first);
it = encounter_list.erase(it);
}
else {
++it;
}
}
}
void EntityList::AddGroup(Group *group)
{
if (group == nullptr) //this seems to be happening somehow...
return;
uint32 gid = worldserver.NextGroupID();
if (gid == 0) {
Log(Logs::General, Logs::Error,
"Unable to get new group ID from world server. group is going to be broken.");
return;
}
AddGroup(group, gid);
#if EQDEBUG >= 5
CheckGroupList (__FILE__, __LINE__);
#endif
}
void EntityList::AddGroup(Group *group, uint32 gid)
{
group->SetID(gid);
group_list.push_back(group);
if (!group_timer.Enabled())
group_timer.Start();
#if EQDEBUG >= 5
CheckGroupList(__FILE__, __LINE__);
#endif
}
void EntityList::AddRaid(Raid *raid)
{
if (raid == nullptr)
return;
uint32 gid = worldserver.NextGroupID();
if (gid == 0) {
Log(Logs::General, Logs::Error,
"Unable to get new group ID from world server. group is going to be broken.");
return;
}
AddRaid(raid, gid);
}
void EntityList::AddRaid(Raid *raid, uint32 gid)
{
raid->SetID(gid);
raid_list.push_back(raid);
if (!raid_timer.Enabled())
raid_timer.Start();
}
void EntityList::AddCorpse(Corpse *corpse, uint32 in_id)
{
if (corpse == 0)
return;
if (in_id == 0xFFFFFFFF)
corpse->SetID(GetFreeID());
else
corpse->SetID(in_id);
corpse->CalcCorpseName();
corpse_list.insert(std::pair<uint16, Corpse *>(corpse->GetID(), corpse));
// we are a new corpse, so update distances to us for clients.
float mydist = 0;
auto it = client_list.begin();
// go through the list and update distances
while(it != client_list.end()) {
if (it->second && it->second->GetID() > 0) {
it->second->SetLastDistance(corpse->GetID(), DistanceSquaredNoZ(it->second->GetPosition(), corpse->GetPosition()));
it->second->SetLastPosition(corpse->GetID(), corpse->GetPosition());
it->second->SetInside(corpse->GetID(), true);
}
++it;
}
if (!corpse_timer.Enabled()) {
corpse_timer.Start();
corpse_depop_timer.Start();
}
}
void EntityList::AddNPC(NPC *npc, bool SendSpawnPacket, bool dontqueue)
{
npc->SetID(GetFreeID());
//npc->SetMerchantProbability((uint8) zone->random.Int(0, 99));
npc->SetSpawned();
if (SendSpawnPacket) {
if (dontqueue) { // aka, SEND IT NOW BITCH!
EQApplicationPacket app;
npc->CreateSpawnPacket(&app, npc);
QueueClients(npc, &app);
safe_delete_array(app.pBuffer);
npc->SpawnPacketSent(true);
parse->EventNPC(EVENT_SPAWN, npc, nullptr, "", 0);
if (!npc->GetDepop()) {
uint16 emoteid = npc->GetEmoteID();
if (emoteid != 0)
npc->DoNPCEmote(ONSPAWN, emoteid);
}
} else {
auto ns = new NewSpawn_Struct;
memset(ns, 0, sizeof(NewSpawn_Struct));
npc->FillSpawnStruct(ns, nullptr); // Not working on player newspawns, so it's safe to use a ForWho of 0
AddToSpawnQueue(npc->GetID(), &ns);
safe_delete(ns);
if (client_list.size() == 0) {
npc->SpawnPacketSent(true);
parse->EventNPC(EVENT_SPAWN, npc, nullptr, "", 0);
}
}
} else {
npc->SpawnPacketSent(true);
parse->EventNPC(EVENT_SPAWN, npc, nullptr, "", 0);
}
// update distances to us for clients.
auto it = client_list.begin();
// go through the list and update distances
while(it != client_list.end()) {
if (it->second && it->second->GetID() > 0) {
it->second->SetLastDistance(npc->GetID(), DistanceSquaredNoZ(it->second->GetPosition(), npc->GetPosition()));
it->second->SetLastPosition(npc->GetID(), npc->GetPosition());
it->second->SetInside(npc->GetID(), false);
}
++it;
}
npc_list.insert(std::pair<uint16, NPC *>(npc->GetID(), npc));
mob_list.insert(std::pair<uint16, Mob *>(npc->GetID(), npc));
npc->SetAttackTimer(true); // set attacker timers to be ready immediately on spawn
/* Zone controller process EVENT_SPAWN_ZONE */
if (entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID) && npc->GetNPCTypeID() != ZONE_CONTROLLER_NPC_ID) {
char data_pass[100] = { 0 };
snprintf(data_pass, 99, "%d %d", npc->GetID(), npc->GetNPCTypeID());
parse->EventNPC(EVENT_SPAWN_ZONE, entity_list.GetNPCByNPCTypeID(ZONE_CONTROLLER_NPC_ID)->CastToNPC(), nullptr, data_pass, 0);
}
}
void EntityList::AddObject(Object *obj, bool SendSpawnPacket)
{
obj->SetID(GetFreeID());
if (SendSpawnPacket) {
EQApplicationPacket app;
obj->CreateSpawnPacket(&app);
#if (EQDEBUG >= 6)
DumpPacket(&app);
#endif
QueueClients(0, &app,false);
safe_delete_array(app.pBuffer);
}
object_list.insert(std::pair<uint16, Object *>(obj->GetID(), obj));
if (!object_timer.Enabled())
object_timer.Start();
}
void EntityList::AddDoor(Doors *door)
{
door->SetEntityID(GetFreeID());
door_list.insert(std::pair<uint16, Doors *>(door->GetEntityID(), door));
if (!door_timer.Enabled())
door_timer.Start();
}
void EntityList::AddTrap(Trap *trap)
{
trap->SetID(GetFreeID());
trap_list.insert(std::pair<uint16, Trap *>(trap->GetID(), trap));
if (!trap_timer.Enabled())
trap_timer.Start();
}
void EntityList::AddBeacon(Beacon *beacon)
{
beacon->SetID(GetFreeID());
beacon_list.insert(std::pair<uint16, Beacon *>(beacon->GetID(), beacon));
}
void EntityList::AddEncounter(Encounter *encounter)
{
encounter->SetID(GetFreeID());
encounter_list.insert(std::pair<uint16, Encounter *>(encounter->GetID(), encounter));
}
void EntityList::AddToSpawnQueue(uint16 entityid, NewSpawn_Struct **ns)
{
uint32 count;
if ((count = (client_list.size())) == 0)
return;
SpawnQueue.Append(*ns);
NumSpawnsOnQueue++;
if (tsFirstSpawnOnQueue == 0xFFFFFFFF)
tsFirstSpawnOnQueue = Timer::GetCurrentTime();
*ns = nullptr;
}
void EntityList::CheckSpawnQueue()
{
// Send the stuff if the oldest packet on the queue is older than 50ms -Quagmire
if (tsFirstSpawnOnQueue != 0xFFFFFFFF && (Timer::GetCurrentTime() - tsFirstSpawnOnQueue) > 50) {
LinkedListIterator<NewSpawn_Struct *> iterator(SpawnQueue);
EQApplicationPacket outapp;
iterator.Reset();
while(iterator.MoreElements()) {
Mob::CreateSpawnPacket(&outapp, iterator.GetData());
QueueClients(0, &outapp);
Mob* mob = GetMob(iterator.GetData()->spawn.spawnId);
if (mob && mob->IsNPC())
{
mob->SpawnPacketSent(true);
parse->EventNPC(EVENT_SPAWN, mob->CastToNPC(), nullptr, "", 0);
if (!mob->CastToNPC()->GetDepop()) {
uint16 emoteid = mob->CastToNPC()->GetEmoteID();
if (emoteid != 0)
mob->CastToNPC()->DoNPCEmote(ONSPAWN, emoteid);
}
}
safe_delete_array(outapp.pBuffer);
iterator.RemoveCurrent();
}
tsFirstSpawnOnQueue = 0xFFFFFFFF;
NumSpawnsOnQueue = 0;
}
}
void EntityList::OpenFloorTeleportNear(Client* c)
{
if (!c || door_list.empty())
return;
auto client_pos = c->GetPosition();
for (auto it = door_list.begin(); it != door_list.end(); ++it)
{
Doors *cdoor = it->second;
if (!cdoor || !cdoor->IsMoveable() || cdoor->GetOpenType() != 57 || !cdoor->IsTeleport())
continue;
auto diff = c->GetPosition() - cdoor->GetPosition();
float curdist = diff.x * diff.x + diff.y * diff.y;
Log(Logs::Detail, Logs::Doors, "A floor teleport with id %d was found at %0.2f away at %0.2f Z.", cdoor->GetDoorDBID(), curdist, diff.z * diff.z);
if (diff.z * diff.z < 105 && curdist <= 600)
{
cdoor->HandleClick(c, 0, true);
return;
}
}
}
Doors *EntityList::FindNearestDoor(Client* c)
{
if (!c || door_list.empty())
return nullptr;
Doors *nearest = nullptr;
float closest = 999999.0f;
auto it = door_list.begin();
while (it != door_list.end()) {
if (!it->second)
continue;
auto diff = c->GetPosition() - it->second->GetPosition();
float curdist = diff.x * diff.x + diff.y * diff.y + diff.z * diff.z;
if (curdist < closest)
{
closest = curdist;
nearest = it->second;
}
++it;
}
return nearest;
}
glm::vec3 EntityList::GetDoorLoc(Client *c, int doorid)
{
if (!c || door_list.empty())
return glm::vec3(0.0f, 0.0f, 0.0f);
auto it = door_list.begin();
while (it != door_list.end()) {
if (!it->second)
continue;
if (it->second->GetDoorID() == doorid) {
return glm::vec3(it->second->GetPosition().x, it->second->GetPosition().y, it->second->GetPosition().z);
}
++it;
}
return glm::vec3(0.0f, 0.0f, 0.0f);
}
void EntityList::ListDoors(Client* c)
{
if (!c || door_list.empty())
return;
auto it = door_list.begin();
while (it != door_list.end()) {
if (!it->second)
continue;
c->Message(CC_Default, "DoorID %3d at %.2f, %.2f, %.2f. (%s)", it->second->GetDoorID(), it->second->GetPosition().x,it->second->GetPosition().y,it->second->GetPosition().z, it->second->GetDoorName());
++it;
}
return;
}
Doors *EntityList::FindDoor(uint8 door_id)
{
if (door_id < 0 || door_list.empty())
return nullptr;
auto it = door_list.begin();
while (it != door_list.end()) {
if (it->second->GetDoorID() == door_id)
return it->second;
++it;
}
return nullptr;
}
Object *EntityList::FindObject(uint32 object_id)
{
if (object_id == 0 || object_list.empty())
return nullptr;
auto it = object_list.begin();
while (it != object_list.end()) {
if (it->second->GetDBID() == object_id)
return it->second;
++it;
}
return nullptr;
}
Object *EntityList::FindNearbyObject(float x, float y, float z, float radius)
{
if (object_list.empty())
return nullptr;
float ox;
float oy;
float oz;
auto it = object_list.begin();
while (it != object_list.end()) {