/
zone.cpp
3340 lines (2800 loc) · 82.1 KB
/
zone.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 <float.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#ifdef _WINDOWS
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#else
#include <pthread.h>
#include "../common/unix.h"
#endif
#include "../common/global_define.h"
#include "../common/features.h"
#include "../common/rulesys.h"
#include "../common/seperator.h"
#include "../common/strings.h"
#include "../common/eqemu_logsys.h"
#include "expedition.h"
#include "guild_mgr.h"
#include "map.h"
#include "npc.h"
#include "object.h"
#include "pathfinder_null.h"
#include "petitions.h"
#include "quest_parser_collection.h"
#include "spawn2.h"
#include "spawngroup.h"
#include "water_map.h"
#include "worldserver.h"
#include "zone.h"
#include "zone_config.h"
#include "mob_movement_manager.h"
#include "npc_scale_manager.h"
#include "../common/data_verification.h"
#include "zone_reload.h"
#include "../common/repositories/criteria/content_filter_criteria.h"
#include "../common/repositories/character_exp_modifiers_repository.h"
#include "../common/repositories/merchantlist_repository.h"
#include "../common/repositories/object_repository.h"
#include "../common/repositories/rule_sets_repository.h"
#include "../common/repositories/level_exp_mods_repository.h"
#include "../common/repositories/ldon_trap_entries_repository.h"
#include "../common/repositories/ldon_trap_templates_repository.h"
#include "../common/repositories/respawn_times_repository.h"
#include "../common/repositories/npc_emotes_repository.h"
#include "../common/serverinfo.h"
#include "../common/repositories/merc_stance_entries_repository.h"
#include "../common/repositories/alternate_currency_repository.h"
#include "../common/repositories/graveyard_repository.h"
#include <time.h>
#ifdef _WINDOWS
#define snprintf _snprintf
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
extern bool staticzone;
extern PetitionList petition_list;
extern QuestParserCollection* parse;
extern uint32 numclients;
extern WorldServer worldserver;
extern Zone* zone;
extern NpcScaleManager* npc_scale_manager;
Mutex MZoneShutdown;
volatile bool is_zone_loaded = false;
Zone* zone = 0;
void UpdateWindowTitle(char* iNewTitle);
bool Zone::Bootup(uint32 iZoneID, uint32 iInstanceID, bool is_static) {
const char* zonename = ZoneName(iZoneID);
if (iZoneID == 0 || zonename == 0)
return false;
if (zone != 0 || is_zone_loaded) {
std::cerr << "Error: Zone::Bootup call when zone already booted!" << std::endl;
worldserver.SetZoneData(0);
return false;
}
LogInfo("Booting [{}] ([{}]:[{}])", zonename, iZoneID, iInstanceID);
numclients = 0;
zone = new Zone(iZoneID, iInstanceID, zonename);
//init the zone, loads all the data, etc
if (!zone->Init(is_static)) {
safe_delete(zone);
std::cerr << "Zone->Init failed" << std::endl;
worldserver.SetZoneData(0);
return false;
}
std::string tmp;
if (database.GetVariable("loglevel", tmp)) {
int log_levels[4];
int tmp_i = Strings::ToInt(tmp);
if (tmp_i>9){ //Server is using the new code
for(int i=0;i<4;i++){
if (((int)tmp[i]>=48) && ((int)tmp[i]<=57))
log_levels[i]=(int)tmp[i]-48; //get the value to convert it to an int from the ascii value
else
log_levels[i]=0; //set to zero on a bogue char
}
zone->loglevelvar = log_levels[0];
LogInfo("General logging level: [{}]", zone->loglevelvar);
zone->merchantvar = log_levels[1];
LogInfo("Merchant logging level: [{}]", zone->merchantvar);
zone->tradevar = log_levels[2];
LogInfo("Trade logging level: [{}]", zone->tradevar);
zone->lootvar = log_levels[3];
LogInfo("Loot logging level: [{}]", zone->lootvar);
}
else {
zone->loglevelvar = uint8(tmp_i); //continue supporting only command logging (for now)
zone->merchantvar = 0;
zone->tradevar = 0;
zone->lootvar = 0;
}
}
is_zone_loaded = true;
worldserver.SetZoneData(iZoneID, iInstanceID);
if(iInstanceID != 0)
{
auto pack = new ServerPacket(ServerOP_AdventureZoneData, sizeof(uint16));
*((uint16*)pack->pBuffer) = iInstanceID;
worldserver.SendPacket(pack);
delete pack;
}
LogInfo("Zone server [{}] listening on port [{}]", zonename, ZoneConfig::get()->ZonePort);
LogInfo("Zone bootup type [{}] short_name [{}] zone_id [{}] instance_id [{}]",
(is_static) ? "Static" : "Dynamic", zonename, iZoneID, iInstanceID);
parse->Init();
UpdateWindowTitle(nullptr);
// Dynamic zones need to Sync here.
// Static zones sync when they connect in worldserver.cpp.
// Static zones cannot sync here as request is ignored by worldserver.
if (!is_static) {
zone->GetTimeSync();
}
zone->RequestUCSServerStatus();
zone->StartShutdownTimer();
/*
* Set Logging
*/
LogSys.StartFileLogs(StringFormat("%s_version_%u_inst_id_%u_port_%u", zone->GetShortName(), zone->GetInstanceVersion(), zone->GetInstanceID(), ZoneConfig::get()->ZonePort));
return true;
}
//this really loads the objects into entity_list
bool Zone::LoadZoneObjects()
{
const auto &l = ObjectRepository::GetWhere(
content_db,
fmt::format(
"zoneid = {} AND (version = {} OR version = -1) {}",
zoneid,
instanceversion,
ContentFilterCriteria::apply()
)
);
for (const auto &e : l) {
if (e.type == ObjectTypes::StaticLocked) {
const std::string &zone_short_name = ZoneName(e.zoneid, false);
if (zone_short_name.empty()) {
continue;
}
auto d = DoorsRepository::NewEntity();
d.zone = zone_short_name;
d.id = 1000000000 + e.id;
d.doorid = -1;
d.pos_x = e.xpos;
d.pos_y = e.ypos;
d.pos_z = e.zpos;
d.heading = e.heading;
d.name = Strings::Replace(e.objectname, "_ACTORDEF", "");
d.dest_zone = "NONE";
d.incline = e.incline;
d.client_version_mask = 0xFFFFFFFF;
if (e.size_percentage == 0) {
d.size = 100;
}
switch (d.opentype = e.solid_type)
{
case 0:
d.opentype = 31;
break;
case 1:
d.opentype = 9;
break;
}
auto door = new Doors(d);
entity_list.AddDoor(door);
}
Object_Struct data = {0};
uint32 id = 0;
uint32 icon = 0;
uint32 type = 0;
uint32 itemid = 0;
uint32 idx = 0;
int16 charges = 0;
id = e.id;
data.zone_id = e.zoneid;
data.x = e.xpos;
data.y = e.ypos;
data.z = e.zpos;
data.heading = e.heading;
itemid = e.itemid;
charges = e.charges;
type = e.type;
icon = e.icon;
data.object_type = type;
data.linked_list_addr[0] = 0;
data.linked_list_addr[1] = 0;
strn0cpy(data.object_name, e.objectname.c_str(), sizeof(data.object_name));
data.solid_type = e.solid_type;
data.incline = e.incline;
data.unknown024 = e.unknown24;
data.unknown076 = e.unknown76;
data.size = e.size;
data.tilt_x = e.tilt_x;
data.tilt_y = e.tilt_y;
data.unknown084 = 0;
glm::vec3 position;
position.x = data.x;
position.y = data.y;
position.z = data.z;
if (zone->HasMap()) {
data.z = zone->zonemap->FindBestZ(position, nullptr);
}
EQ::ItemInstance *inst = nullptr;
// tradeskill containers do not have an itemid of 0
if (!itemid) {
// Generic tradeskill container
inst = new EQ::ItemInstance(ItemInstWorldContainer);
} else {
// Groundspawn object
inst = database.CreateItem(itemid);
}
if (!inst && type != ObjectTypes::Temporary) {
inst = new EQ::ItemInstance(ItemInstWorldContainer);
}
// Load child objects if container
if (inst && inst->IsType(EQ::item::ItemClassBag)) {
database.LoadWorldContainer(id, inst);
}
auto object = new Object(id, type, icon, data, inst);
object->SetDisplayName(e.display_name.c_str());
entity_list.AddObject(object, false);
if (type == ObjectTypes::Temporary && itemid) {
entity_list.RemoveObject(object->GetID());
}
safe_delete(inst);
}
LogInfo("Loaded [{}] world objects", Strings::Commify(l.size()));
return true;
}
bool Zone::IsSpecialBindLocation(const glm::vec4& location)
{
glm::vec2 corner1;
glm::vec2 corner2;
switch (GetZoneID()) {
case Zones::NORTHKARANA:
corner1 = glm::vec2(-234, -741);
corner2 = glm::vec2(-127, -525);
break;
case Zones::OASIS:
corner1 = glm::vec2(90, 656);
corner2 = glm::vec2(-58, 471);
break;
case Zones::FIELDOFBONE:
corner1 = glm::vec2(265, -2213);
corner2 = glm::vec2(-506, -1255);
break;
case Zones::FIRIONA:
corner1 = glm::vec2(1065, -2609);
corner2 = glm::vec2(3511, -4534);
break;
case Zones::FRONTIERMTNS:
corner1 = glm::vec2(1554, -2106);
corner2 = glm::vec2(1206, -2333);
break;
case Zones::OVERTHERE:
corner1 = glm::vec2(3937, 3614);
corner2 = glm::vec2(2034, 2324);
break;
case Zones::ICECLAD:
corner1 = glm::vec2(3937, 3614);
corner2 = glm::vec2(510, 5365);
break;
default:
return false;
}
return IsWithinAxisAlignedBox(glm::vec2(location.x, location.y), corner1, corner2);
}
//this also just loads into entity_list, not really into zone
bool Zone::LoadGroundSpawns() {
GroundSpawns g{};
content_db.LoadGroundSpawns(zoneid, GetInstanceVersion(), &g);
uint32 added = 0;
for (uint16 slot_id = 0; slot_id < 50; slot_id++) {
if (EQ::ValueWithin(g.spawn[slot_id].item_id, 1, (SAYLINK_ITEM_ID - 1))) {
auto inst = database.CreateItem(g.spawn[slot_id].item_id);
const uint32 max_allowed = g.spawn[slot_id].max_allowed;
if (inst) {
for (uint32 i = 0; i < max_allowed; i++) {
auto object = new Object(
inst,
g.spawn[slot_id].name,
g.spawn[slot_id].max_x,
g.spawn[slot_id].min_x,
g.spawn[slot_id].max_y,
g.spawn[slot_id].min_y,
g.spawn[slot_id].max_z,
g.spawn[slot_id].heading,
g.spawn[slot_id].respawn_timer,
g.spawn[slot_id].fix_z
);
entity_list.AddObject(object, false);
added++;
}
safe_delete(inst);
}
}
}
LogInfo(
"Loaded [{}] Ground Spawn{}",
Strings::Commify(added),
added != 1 ? "s" : ""
);
return(true);
}
int Zone::SaveTempItem(uint32 merchantid, uint32 npcid, uint32 item, int32 charges, bool sold) {
LogInventory("[{}] [{}] charges of [{}]", ((sold) ? "Sold" : "Bought"),
charges, item);
// Iterate past main items.
// If the item being transacted is in this list, return 0;
std::list<MerchantList> merlist = merchanttable[merchantid];
std::list<MerchantList>::const_iterator itr;
uint32 temp_slot_index = 1;
for (itr = merlist.begin(); itr != merlist.end(); ++itr) {
MerchantList ml = *itr;
if (ml.item == item) {
return 0;
}
// Account for merchant lists with gaps in them.
if (ml.slot >= temp_slot_index) {
temp_slot_index = ml.slot + 1;
}
}
LogInventory("Searching Temporary List. Main list ended at [{}]", temp_slot_index-1);
// Now search the temporary list.
std::list<TempMerchantList> tmp_merlist = tmpmerchanttable[npcid];
std::list<TempMerchantList>::const_iterator tmp_itr;
TempMerchantList ml;
uint32 first_empty_slot = 0; // Save 1st vacant slot while searching..
bool found = false;
for (tmp_itr = tmp_merlist.begin(); tmp_itr != tmp_merlist.end(); ++tmp_itr) {
ml = *tmp_itr;
if (ml.item == item) {
found = true;
LogInventory("Item found in temp list at [{}] with [{}] charges", ml.origslot, ml.charges);
break;
}
}
if (found) {
tmp_merlist.clear();
std::list<TempMerchantList> oldtmp_merlist = tmpmerchanttable[npcid];
for (tmp_itr = oldtmp_merlist.begin(); tmp_itr != oldtmp_merlist.end(); ++tmp_itr) {
TempMerchantList ml2 = *tmp_itr;
if(ml2.item != item)
tmp_merlist.push_back(ml2);
else {
if (sold) {
LogInventory("Total charges is [{}] + [{}] charges", ml.charges, charges);
ml.charges = ml.charges + charges;
}
else {
ml.charges = charges;
LogInventory("new charges is [{}] charges", ml.charges);
}
if (!ml.origslot) {
ml.origslot = ml.slot;
}
if (ml.charges > 0) {
database.SaveMerchantTemp(npcid, ml.origslot, GetZoneID(), GetInstanceID(), item, ml.charges);
tmp_merlist.push_back(ml);
} else {
database.DeleteMerchantTemp(npcid, ml.origslot, GetZoneID(), GetInstanceID());
}
}
}
tmpmerchanttable[npcid] = tmp_merlist;
return ml.slot;
}
else {
if (charges < 0) { //sanity check only, shouldnt happen
charges = 0x7FFF;
}
// Find an ununsed db slot #
std::list<int> slots;
TempMerchantList ml3;
for (tmp_itr = tmp_merlist.begin(); tmp_itr != tmp_merlist.end(); ++tmp_itr) {
ml3 = *tmp_itr;
slots.push_back(ml3.origslot);
}
slots.sort();
std::list<int>::const_iterator slots_itr;
uint32 first_empty_slot = 0;
uint32 idx = temp_slot_index;
for (slots_itr = slots.begin(); slots_itr != slots.end(); ++slots_itr) {
if (!first_empty_slot && *slots_itr > idx) {
LogInventory("Popped [{}]", *slots_itr);
LogInventory("First Gap Found at [{}]", idx);
break;
}
++idx;
}
first_empty_slot = idx;
// Find an ununsed mslot
slots.clear();
for (tmp_itr = tmp_merlist.begin(); tmp_itr != tmp_merlist.end(); ++tmp_itr) {
ml3 = *tmp_itr;
slots.push_back(ml3.slot);
}
slots.sort();
uint32 first_empty_mslot=0;
idx = temp_slot_index;
for (slots_itr = slots.begin(); slots_itr != slots.end(); ++slots_itr) {
if (!first_empty_mslot && *slots_itr > idx) {
LogInventory("Popped [{}]", *slots_itr);
LogInventory("First Gap Found at [{}]", idx);
break;
}
++idx;
}
first_empty_mslot = idx;
database.SaveMerchantTemp(npcid, first_empty_slot, GetZoneID(), GetInstanceID(), item, charges);
tmp_merlist = tmpmerchanttable[npcid];
TempMerchantList ml2;
ml2.charges = charges;
LogInventory("Adding slot [{}] with [{}] charges.", first_empty_mslot, charges);
ml2.item = item;
ml2.npcid = npcid;
ml2.slot = first_empty_mslot;
ml2.origslot = first_empty_slot;
tmp_merlist.push_back(ml2);
tmpmerchanttable[npcid] = tmp_merlist;
return ml2.slot;
}
}
uint32 Zone::GetTempMerchantQuantity(uint32 NPCID, uint32 Slot) {
std::list<TempMerchantList> TmpMerchantList = tmpmerchanttable[NPCID];
std::list<TempMerchantList>::const_iterator Iterator;
for (Iterator = TmpMerchantList.begin(); Iterator != TmpMerchantList.end(); ++Iterator)
if ((*Iterator).slot == Slot) {
LogInventory("Slot [{}] has [{}] charges.", Slot, (*Iterator).charges);
return (*Iterator).charges;
}
return 0;
}
void Zone::LoadTempMerchantData()
{
auto results = content_db.QueryDatabase(
fmt::format(
SQL(
SELECT
DISTINCT npc_types.id
FROM
npc_types
JOIN spawnentry ON spawnentry.npcID = npc_types.id
JOIN spawn2 ON spawn2.spawngroupID = spawnentry.spawngroupID
WHERE
spawn2.zone = '{}'
AND spawn2.version = {}
),
GetShortName(),
GetInstanceVersion()
)
);
if (!results.Success() || results.RowCount() == 0) {
return;
}
std::vector<std::string> npc_ids;
for (auto row = results.begin(); row != results.end(); ++row) {
npc_ids.push_back(row[0]);
}
results = database.QueryDatabase(
fmt::format(
SQL(
SELECT
npcid,
slot,
charges,
itemid
FROM merchantlist_temp
WHERE npcid IN ({})
AND zone_id = {}
AND instance_id = {}
),
Strings::Implode(", ", npc_ids),
GetZoneID(), GetInstanceID()
)
);
LogInfo("Loaded [{}] temporary merchant entries", Strings::Commify(results.RowCount()));
std::map<uint32, std::list<TempMerchantList> >::iterator temp_merchant_table_entry;
uint32 npc_id = 0;
for (auto row = results.begin(); row != results.end(); ++row) {
TempMerchantList temp_merchant_list;
temp_merchant_list.npcid = Strings::ToUnsignedInt(row[0]);
if (npc_id != temp_merchant_list.npcid) {
temp_merchant_table_entry = tmpmerchanttable.find(temp_merchant_list.npcid);
if (temp_merchant_table_entry == tmpmerchanttable.end()) {
std::list<TempMerchantList> empty;
tmpmerchanttable[temp_merchant_list.npcid] = empty;
temp_merchant_table_entry = tmpmerchanttable.find(temp_merchant_list.npcid);
}
npc_id = temp_merchant_list.npcid;
}
temp_merchant_list.slot = Strings::ToUnsignedInt(row[1]);
temp_merchant_list.charges = Strings::ToUnsignedInt(row[2]);
temp_merchant_list.item = Strings::ToUnsignedInt(row[3]);
temp_merchant_list.origslot = temp_merchant_list.slot;
LogMerchants(
"Loading merchant temp items npc_id [{}] slot [{}] charges [{}] item [{}] origslot [{}]",
npc_id,
temp_merchant_list.slot,
temp_merchant_list.charges,
temp_merchant_list.item,
temp_merchant_list.origslot
);
temp_merchant_table_entry->second.push_back(temp_merchant_list);
}
}
void Zone::LoadNewMerchantData(uint32 merchantid) {
std::list<MerchantList> merchant_list;
const auto& l = MerchantlistRepository::GetWhere(
content_db,
fmt::format(
"merchantid = {} {} ORDER BY slot",
merchantid,
ContentFilterCriteria::apply()
)
);
if (l.empty()) {
return;
}
for (const auto& e : l) {
MerchantList ml;
ml.id = merchantid;
ml.item = e.item;
ml.slot = e.slot;
ml.faction_required = e.faction_required;
ml.level_required = e.level_required;
ml.min_status = e.min_status;
ml.max_status = e.max_status;
ml.alt_currency_cost = e.alt_currency_cost;
ml.classes_required = e.classes_required;
ml.probability = e.probability;
ml.bucket_name = e.bucket_name;
ml.bucket_value = e.bucket_value;
ml.bucket_comparison = e.bucket_comparison;
merchant_list.push_back(ml);
}
merchanttable[merchantid] = merchant_list;
}
void Zone::LoadMerchants()
{
const auto& l = MerchantlistRepository::GetWhere(
content_db,
fmt::format(
SQL(
`merchantid` IN (
SELECT `merchant_id` FROM `npc_types` WHERE `id` IN (
SELECT `npcID` FROM `spawnentry` WHERE `spawngroupID` IN (
SELECT `spawngroupID` FROM `spawn2` WHERE `zone` = '{}' AND (`version` = {} OR `version` = -1)
)
)
)
{}
ORDER BY `merchantlist`.`slot`
),
GetShortName(),
GetInstanceVersion(),
ContentFilterCriteria::apply()
)
);
LogInfo("Loaded [{}] merchant lists", Strings::Commify(l.size()));
if (l.empty()) {
LogDebug("No Merchant Data found for [{}]", GetShortName());
return;
}
std::map<uint32, std::list<MerchantList>>::iterator ml;
uint32 npc_id = 0;
for (const auto& e : l) {
if (npc_id != e.merchantid) {
ml = merchanttable.find(e.merchantid);
if (ml == merchanttable.end()) {
std::list<MerchantList> empty;
merchanttable[e.merchantid] = empty;
ml = merchanttable.find(e.merchantid);
}
npc_id = e.merchantid;
}
bool found = false;
for (const auto &m : ml->second) {
if (m.item == e.merchantid) {
found = true;
break;
}
}
if (found) {
continue;
}
ml->second.push_back(
MerchantList{
.id = static_cast<uint32>(e.merchantid),
.slot = e.slot,
.item = static_cast<uint32>(e.item),
.faction_required = e.faction_required,
.level_required = static_cast<int8>(e.level_required),
.min_status = e.min_status,
.max_status = e.max_status,
.alt_currency_cost = e.alt_currency_cost,
.classes_required = static_cast<uint32>(e.classes_required),
.probability = static_cast<uint8>(e.probability),
.bucket_name = e.bucket_name,
.bucket_value = e.bucket_value,
.bucket_comparison = e.bucket_comparison
}
);
}
}
void Zone::LoadMercenaryTemplates()
{
std::list<MercStanceInfo> mercenary_stances;
merc_templates.clear();
const auto& l = MercStanceEntriesRepository::GetAllOrdered(database);
if (l.empty()) {
return;
}
for (const auto& e : l) {
MercStanceInfo t{
.ProficiencyID = e.proficiency_id,
.ClassID = static_cast<uint8>(e.class_id),
.StanceID = e.stance_id,
.IsDefault = static_cast<uint8>(e.isdefault)
};
mercenary_stances.push_back(t);
}
const std::string& query = SQL(
SELECT DISTINCT MTem.merc_template_id, MTyp.dbstring
AS merc_type_id, MTem.dbstring
AS merc_subtype_id, MTyp.race_id, MS.class_id, MTyp.proficiency_id, MS.tier_id, 0
AS CostFormula, MTem.clientversion, MTem.merc_npc_type_id
FROM merc_types MTyp, merc_templates MTem, merc_subtypes MS
WHERE MTem.merc_type_id = MTyp.merc_type_id AND MTem.merc_subtype_id = MS.merc_subtype_id
ORDER BY MTyp.race_id, MS.class_id, MTyp.proficiency_id
);
auto results = database.QueryDatabase(query);
if (!results.Success() || !results.RowCount()) {
return;
}
for (auto row: results) {
MercTemplate t{
.MercTemplateID = Strings::ToUnsignedInt(row[0]),
.MercType = Strings::ToUnsignedInt(row[1]),
.MercSubType = Strings::ToUnsignedInt(row[2]),
.RaceID = static_cast<uint16>(Strings::ToUnsignedInt(row[3])),
.ClassID = static_cast<uint8>(Strings::ToUnsignedInt(row[4])),
.MercNPCID = Strings::ToUnsignedInt(row[9]),
.ProficiencyID = static_cast<uint8>(Strings::ToUnsignedInt(row[5])),
.TierID = static_cast<uint8>(Strings::ToUnsignedInt(row[6])),
.CostFormula = static_cast<uint8>(Strings::ToUnsignedInt(row[7])),
.ClientVersion = Strings::ToUnsignedInt(row[8])
};
for (int i = 0; i < MaxMercStanceID; i++) {
t.Stances[i] = 0;
}
int stance_index = 0;
for (auto i = mercenary_stances.begin(); i != mercenary_stances.end(); ++i) {
if (i->ClassID != t.ClassID || i->ProficiencyID != t.ProficiencyID) {
continue;
}
zone->merc_stance_list[t.MercTemplateID].push_back((*i));
t.Stances[stance_index] = i->StanceID;
++stance_index;
}
merc_templates[t.MercTemplateID] = t;
}
}
void Zone::LoadLevelEXPMods()
{
level_exp_mod.clear();
const auto& l = LevelExpModsRepository::All(database);
for (const auto& e : l) {
level_exp_mod[e.level].ExpMod = e.exp_mod;
level_exp_mod[e.level].AAExpMod = e.aa_exp_mod;
}
}
void Zone::LoadMercenarySpells()
{
merc_spells_list.clear();
const std::string& query = SQL(
SELECT msl.class_id, msl.proficiency_id, msle.spell_id, msle.spell_type,
msle.stance_id, msle.minlevel, msle.maxlevel, msle.slot, msle.procChance
FROM merc_spell_lists msl, merc_spell_list_entries msle
WHERE msle.merc_spell_list_id = msl.merc_spell_list_id
ORDER BY msl.class_id, msl.proficiency_id, msle.spell_type, msle.minlevel, msle.slot
);
auto results = database.QueryDatabase(query);
if (!results.Success() || !results.RowCount()) {
return;
}
for (auto row: results) {
const uint32 class_id = Strings::ToUnsignedInt(row[0]);
merc_spells_list[class_id].push_back(
MercSpellEntry{
.proficiencyid = static_cast<uint8>(Strings::ToUnsignedInt(row[1])),
.spellid = static_cast<uint16>(Strings::ToUnsignedInt(row[2])),
.type = Strings::ToUnsignedInt(row[3]),
.stance = static_cast<int16>(Strings::ToInt(row[4])),
.minlevel = static_cast<uint8>(Strings::ToUnsignedInt(row[5])),
.maxlevel = static_cast<uint8>(Strings::ToUnsignedInt(row[6])),
.slot = static_cast<int16>(Strings::ToInt(row[7])),
.proc_chance = static_cast<uint16>(Strings::ToUnsignedInt(row[8]))
}
);
}
}
bool Zone::IsLoaded() {
return is_zone_loaded;
}
void Zone::Shutdown(bool quiet)
{
if (!is_zone_loaded) {
return;
}
entity_list.StopMobAI();
std::map<uint32, NPCType *>::iterator itr;
while (!zone->npctable.empty()) {
itr = zone->npctable.begin();
delete itr->second;
itr->second = nullptr;
zone->npctable.erase(itr);
}
while (!zone->merctable.empty()) {
itr = zone->merctable.begin();
delete itr->second;
itr->second = nullptr;
zone->merctable.erase(itr);
}
zone->adventure_entry_list_flavor.clear();
std::map<uint32, LDoNTrapTemplate *>::iterator itr4;
while (!zone->ldon_trap_list.empty()) {
itr4 = zone->ldon_trap_list.begin();
delete itr4->second;
itr4->second = nullptr;
zone->ldon_trap_list.erase(itr4);
}
zone->ldon_trap_entry_list.clear();
LogInfo(
"Zone [{}] zone_id [{}] version [{}] instance_id [{}]",
zone->GetShortName(),
zone->GetZoneID(),
zone->GetInstanceVersion(),
zone->GetInstanceID()
);
petition_list.ClearPetitions();
zone->SetZoneHasCurrentTime(false);
if (!quiet) {
LogInfo(
"Zone [{}] zone_id [{}] version [{}] instance_id [{}] Going to sleep",
zone->GetShortName(),
zone->GetZoneID(),
zone->GetInstanceVersion(),
zone->GetInstanceID()
);
}
is_zone_loaded = false;
zone->ResetAuth();
safe_delete(zone);
entity_list.ClearAreas();
parse->ReloadQuests(true);
UpdateWindowTitle(nullptr);
LogSys.CloseFileLogs();
if (RuleB(Zone, KillProcessOnDynamicShutdown)) {
LogInfo("Shutting down");
EQ::EventLoop::Get().Shutdown();
}
}
void Zone::LoadZoneDoors()
{
auto door_entries = content_db.LoadDoors(GetShortName(), GetInstanceVersion());
if (door_entries.empty()) {
LogInfo("No doors loaded");
return;
}
for (const auto &entry : door_entries) {
auto d = new Doors(entry);
entity_list.AddDoor(d);
LogDoorsDetail("Door added to entity list, db id: [{}], door_id: [{}]", entry.id, entry.doorid);
}
}
Zone::Zone(uint32 in_zoneid, uint32 in_instanceid, const char* in_short_name)
: initgrids_timer(10000),
autoshutdown_timer((RuleI(Zone, AutoShutdownDelay))),
clientauth_timer(AUTHENTICATION_TIMEOUT * 1000),
spawn2_timer(1000),
hot_reload_timer(1000),
qglobal_purge_timer(30000),
m_safe_points(0.0f, 0.0f, 0.0f, 0.0f),
m_graveyard(0.0f, 0.0f, 0.0f, 0.0f)
{
zoneid = in_zoneid;
instanceid = in_instanceid;
instanceversion = database.GetInstanceVersion(instanceid);
pers_instance = false;
zonemap = nullptr;
watermap = nullptr;
pathing = nullptr;
qGlobals = nullptr;
default_ruleset = 0;
is_zone_time_localized = false;
quest_idle_override = false;
loglevelvar = 0;
merchantvar = 0;
tradevar = 0;
lootvar = 0;
short_name = strcpy(new char[strlen(in_short_name)+1], in_short_name);
strlwr(short_name);
memset(file_name, 0, sizeof(file_name));
long_name = 0;
aggroedmobs = 0;
m_graveyard_id = 0;
pgraveyard_zoneid = 0;
m_max_clients = 0;
pvpzone = false;
SetIdleWhenEmpty(true);
SetSecondsBeforeIdle(60);
if (database.GetServerType() == 1) {
pvpzone = true;