-
Notifications
You must be signed in to change notification settings - Fork 404
/
zone.cpp
2275 lines (1948 loc) · 63.6 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 <math.h>
#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/string_util.h"
#include "../common/eqemu_logsys.h"
#include "guild_mgr.h"
#include "map.h"
#include "net.h"
#include "npc.h"
#include "object.h"
#include "pathing.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 <time.h>
#include <ctime>
#include <iostream>
#ifdef _WINDOWS
#define snprintf _snprintf
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
extern bool staticzone;
extern NetConnection net;
extern PetitionList petition_list;
extern QuestParserCollection* parse;
extern uint16 adverrornum;
extern uint32 numclients;
extern WorldServer worldserver;
extern Zone* zone;
Mutex MZoneShutdown;
volatile bool ZoneLoaded = false;
Zone* zone = 0;
bool Zone::Bootup(uint32 iZoneID, uint32 iInstanceID, bool iStaticZone) {
const char* zonename = database.GetZoneName(iZoneID);
if (iZoneID == 0 || zonename == 0)
return false;
if (zone != 0 || ZoneLoaded) {
std::cerr << "Error: Zone::Bootup call when zone already booted!" << std::endl;
worldserver.SetZone(0);
return false;
}
Log.Out(Logs::General, Logs::Status, "Booting %s (%d:%d)", zonename, iZoneID, iInstanceID);
numclients = 0;
zone = new Zone(iZoneID, iInstanceID, zonename);
//init the zone, loads all the data, etc
if (!zone->Init(iStaticZone)) {
safe_delete(zone);
std::cerr << "Zone->Init failed" << std::endl;
worldserver.SetZone(0);
return false;
}
zone->zonemap = Map::LoadMapFile(zone->map_name);
zone->watermap = WaterMap::LoadWaterMapfile(zone->map_name);
zone->pathing = PathManager::LoadPathFile(zone->map_name);
char tmp[10];
if (database.GetVariable("loglevel",tmp, 9)) {
int log_levels[4];
if (atoi(tmp)>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];
Log.Out(Logs::General, Logs::Status, "General logging level: %i", zone->loglevelvar);
zone->merchantvar = log_levels[1];
Log.Out(Logs::General, Logs::Status, "Merchant logging level: %i", zone->merchantvar);
zone->tradevar = log_levels[2];
Log.Out(Logs::General, Logs::Status, "Trade logging level: %i", zone->tradevar);
zone->lootvar = log_levels[3];
Log.Out(Logs::General, Logs::Status, "Loot logging level: %i", zone->lootvar);
}
else {
zone->loglevelvar = uint8(atoi(tmp)); //continue supporting only command logging (for now)
zone->merchantvar = 0;
zone->tradevar = 0;
zone->lootvar = 0;
}
}
ZoneLoaded = true;
worldserver.SetZone(iZoneID, iInstanceID);
if(iInstanceID != 0)
{
ServerPacket *pack = new ServerPacket(ServerOP_AdventureZoneData, sizeof(uint16));
*((uint16*)pack->pBuffer) = iInstanceID;
worldserver.SendPacket(pack);
delete pack;
}
Log.Out(Logs::General, Logs::Normal, "---- Zone server %s, listening on port:%i ----", zonename, ZoneConfig::get()->ZonePort);
Log.Out(Logs::General, Logs::Status, "Zone Bootup: %s (%i: %i)", zonename, iZoneID, iInstanceID);
parse->Init();
UpdateWindowTitle();
zone->GetTimeSync();
/* Set Logging */
Log.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() {
std::string query = StringFormat("SELECT id, zoneid, xpos, ypos, zpos, heading, "
"itemid, charges, objectname, type, icon, unknown08, "
"unknown10, unknown20, unknown24, unknown76 fROM object "
"WHERE zoneid = %i AND (version = %u OR version = -1)",
zoneid, instanceversion);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
Log.Out(Logs::General, Logs::Error, "Error Loading Objects from DB: %s",results.ErrorMessage().c_str());
return false;
}
Log.Out(Logs::General, Logs::Status, "Loading Objects from DB...");
for (auto row = results.begin(); row != results.end(); ++row) {
if (atoi(row[9]) == 0)
{
// Type == 0 - Static Object
const char* shortname = database.GetZoneName(atoi(row[1]), false); // zoneid -> zone_shortname
if (!shortname)
continue;
Door d;
memset(&d, 0, sizeof(d));
strn0cpy(d.zone_name, shortname, sizeof(d.zone_name));
d.db_id = 1000000000 + atoi(row[0]); // Out of range of normal use for doors.id
d.door_id = -1; // Client doesn't care if these are all the same door_id
d.pos_x = atof(row[2]); // xpos
d.pos_y = atof(row[3]); // ypos
d.pos_z = atof(row[4]); // zpos
d.heading = atof(row[5]); // heading
strn0cpy(d.door_name, row[8], sizeof(d.door_name)); // objectname
// Strip trailing "_ACTORDEF" if present. Client won't accept it for doors.
int len = strlen(d.door_name);
if ((len > 9) && (memcmp(&d.door_name[len - 9], "_ACTORDEF", 10) == 0))
d.door_name[len - 9] = '\0';
memcpy(d.dest_zone, "NONE", 5);
if ((d.size = atoi(row[11])) == 0) // unknown08 = optional size percentage
d.size = 100;
switch (d.opentype = atoi(row[12])) // unknown10 = optional request_nonsolid (0 or 1 or experimental number)
{
case 0:
d.opentype = 31;
break;
case 1:
d.opentype = 9;
break;
}
d.incline = atoi(row[13]); // unknown20 = optional model incline value
d.client_version_mask = 0xFFFFFFFF; //We should load the mask from the zone.
Doors* 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 = (uint32)atoi(row[0]);
data.zone_id = atoi(row[1]);
data.x = atof(row[2]);
data.y = atof(row[3]);
data.z = atof(row[4]);
data.heading = atof(row[5]);
itemid = (uint32)atoi(row[6]);
charges = (int16)atoi(row[7]);
strcpy(data.object_name, row[8]);
type = (uint8)atoi(row[9]);
icon = (uint32)atoi(row[10]);
data.object_type = type;
data.linked_list_addr[0] = 0;
data.linked_list_addr[1] = 0;
data.unknown008 = (uint32)atoi(row[11]);
data.unknown010 = (uint32)atoi(row[12]);
data.unknown020 = (uint32)atoi(row[13]);
data.unknown024 = (uint32)atoi(row[14]);
data.unknown076 = (uint32)atoi(row[15]);
data.unknown084 = 0;
ItemInst* inst = nullptr;
//FatherNitwit: this dosent seem to work...
//tradeskill containers do not have an itemid of 0... at least what I am seeing
if (itemid == 0) {
// Generic tradeskill container
inst = new ItemInst(ItemInstWorldContainer);
}
else {
// Groundspawn object
inst = database.CreateItem(itemid);
}
//Father Nitwit's fix... not perfect...
if(inst == nullptr && type != OT_DROPPEDITEM) {
inst = new ItemInst(ItemInstWorldContainer);
}
// Load child objects if container
if (inst && inst->IsType(ItemClassContainer)) {
database.LoadWorldContainer(id, inst);
}
Object* object = new Object(id, type, icon, data, inst);
entity_list.AddObject(object, false);
if(type == OT_DROPPEDITEM && itemid != 0)
entity_list.RemoveObject(object->GetID());
safe_delete(inst);
}
return true;
}
//this also just loads into entity_list, not really into zone
bool Zone::LoadGroundSpawns() {
Ground_Spawns groundspawn;
memset(&groundspawn, 0, sizeof(groundspawn));
int gsindex=0;
Log.Out(Logs::General, Logs::Status, "Loading Ground Spawns from DB...");
database.LoadGroundSpawns(zoneid, GetInstanceVersion(), &groundspawn);
uint32 ix=0;
char* name=0;
uint32 gsnumber=0;
for(gsindex=0;gsindex<50;gsindex++){
if(groundspawn.spawn[gsindex].item>0 && groundspawn.spawn[gsindex].item<500000){
ItemInst* inst = nullptr;
inst = database.CreateItem(groundspawn.spawn[gsindex].item);
gsnumber=groundspawn.spawn[gsindex].max_allowed;
ix=0;
if(inst){
name = groundspawn.spawn[gsindex].name;
for(ix=0;ix<gsnumber;ix++){
Object* object = new Object(inst,name,groundspawn.spawn[gsindex].max_x,groundspawn.spawn[gsindex].min_x,groundspawn.spawn[gsindex].max_y,groundspawn.spawn[gsindex].min_y,groundspawn.spawn[gsindex].max_z,groundspawn.spawn[gsindex].heading,groundspawn.spawn[gsindex].respawntimer);//new object with id of 10000+
entity_list.AddObject(object, false);
}
safe_delete(inst);
}
}
}
return(true);
}
int Zone::SaveTempItem(uint32 merchantid, uint32 npcid, uint32 item, int32 charges, bool sold) {
int freeslot = 0;
std::list<MerchantList> merlist = merchanttable[merchantid];
std::list<MerchantList>::const_iterator itr;
uint32 i = 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 >= i)
i = ml.slot + 1;
}
std::list<TempMerchantList> tmp_merlist = tmpmerchanttable[npcid];
std::list<TempMerchantList>::const_iterator tmp_itr;
bool update_charges = false;
TempMerchantList ml;
while (freeslot == 0 && !update_charges) {
freeslot = i;
for (tmp_itr = tmp_merlist.begin(); tmp_itr != tmp_merlist.end(); ++tmp_itr) {
ml = *tmp_itr;
if (ml.item == item) {
update_charges = true;
freeslot = 0;
break;
}
if ((ml.slot == i) || (ml.origslot == i)) {
freeslot = 0;
}
}
i++;
}
if (update_charges) {
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);
}
if (sold)
ml.charges = ml.charges + charges;
else
ml.charges = charges;
if (!ml.origslot)
ml.origslot = ml.slot;
if (charges > 0) {
database.SaveMerchantTemp(npcid, ml.origslot, item, ml.charges);
tmp_merlist.push_back(ml);
}
else {
database.DeleteMerchantTemp(npcid, ml.origslot);
}
tmpmerchanttable[npcid] = tmp_merlist;
if (sold)
return ml.slot;
}
if (freeslot) {
if (charges < 0) //sanity check only, shouldnt happen
charges = 0x7FFF;
database.SaveMerchantTemp(npcid, freeslot, item, charges);
tmp_merlist = tmpmerchanttable[npcid];
TempMerchantList ml2;
ml2.charges = charges;
ml2.item = item;
ml2.npcid = npcid;
ml2.slot = freeslot;
ml2.origslot = ml2.slot;
tmp_merlist.push_back(ml2);
tmpmerchanttable[npcid] = tmp_merlist;
}
return freeslot;
}
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)
return (*Iterator).charges;
return 0;
}
void Zone::LoadTempMerchantData() {
Log.Out(Logs::General, Logs::Status, "Loading Temporary Merchant Lists...");
std::string query = StringFormat(
"SELECT "
"DISTINCT ml.npcid, "
"ml.slot, "
"ml.charges, "
"ml.itemid "
"FROM "
"merchantlist_temp ml, "
"spawnentry se, "
"spawn2 s2 "
"WHERE "
"ml.npcid = se.npcid "
"AND se.spawngroupid = s2.spawngroupid "
"AND s2.zone = '%s' AND s2.version = %i "
"ORDER BY ml.slot ", GetShortName(), GetInstanceVersion());
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return;
}
std::map<uint32, std::list<TempMerchantList> >::iterator cur;
uint32 npcid = 0;
for (auto row = results.begin(); row != results.end(); ++row) {
TempMerchantList ml;
ml.npcid = atoul(row[0]);
if (npcid != ml.npcid){
cur = tmpmerchanttable.find(ml.npcid);
if (cur == tmpmerchanttable.end()) {
std::list<TempMerchantList> empty;
tmpmerchanttable[ml.npcid] = empty;
cur = tmpmerchanttable.find(ml.npcid);
}
npcid = ml.npcid;
}
ml.slot = atoul(row[1]);
ml.charges = atoul(row[2]);
ml.item = atoul(row[3]);
ml.origslot = ml.slot;
cur->second.push_back(ml);
}
pQueuedMerchantsWorkID = 0;
}
void Zone::LoadNewMerchantData(uint32 merchantid) {
std::list<MerchantList> merlist;
std::string query = StringFormat("SELECT item, slot, faction_required, level_required, alt_currency_cost, "
"classes_required, probability FROM merchantlist WHERE merchantid=%d ORDER BY slot", merchantid);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return;
}
for(auto row = results.begin(); row != results.end(); ++row) {
MerchantList ml;
ml.id = merchantid;
ml.item = atoul(row[0]);
ml.slot = atoul(row[1]);
ml.faction_required = atoul(row[2]);
ml.level_required = atoul(row[3]);
ml.alt_currency_cost = atoul(row[4]);
ml.classes_required = atoul(row[5]);
ml.probability = atoul(row[6]);
merlist.push_back(ml);
}
merchanttable[merchantid] = merlist;
}
void Zone::GetMerchantDataForZoneLoad() {
Log.Out(Logs::General, Logs::Status, "Loading Merchant Lists...");
std::string query = StringFormat(
"SELECT "
"DISTINCT ml.merchantid, "
"ml.slot, "
"ml.item, "
"ml.faction_required, "
"ml.level_required, "
"ml.alt_currency_cost, "
"ml.classes_required, "
"ml.probability "
"FROM "
"merchantlist AS ml, "
"npc_types AS nt, "
"spawnentry AS se, "
"spawn2 AS s2 "
"WHERE nt.merchant_id = ml.merchantid AND nt.id = se.npcid "
"AND se.spawngroupid = s2.spawngroupid AND s2.zone = '%s' AND s2.version = %i "
"ORDER BY ml.slot ", GetShortName(), GetInstanceVersion());
auto results = database.QueryDatabase(query);
std::map<uint32, std::list<MerchantList> >::iterator cur;
uint32 npcid = 0;
if (results.RowCount() == 0) {
Log.Out(Logs::General, Logs::None, "No Merchant Data found for %s.", GetShortName());
return;
}
for (auto row = results.begin(); row != results.end(); ++row) {
MerchantList ml;
ml.id = atoul(row[0]);
if (npcid != ml.id) {
cur = merchanttable.find(ml.id);
if (cur == merchanttable.end()) {
std::list<MerchantList> empty;
merchanttable[ml.id] = empty;
cur = merchanttable.find(ml.id);
}
npcid = ml.id;
}
std::list<MerchantList>::iterator iter = cur->second.begin();
bool found = false;
while (iter != cur->second.end()) {
if ((*iter).item == ml.id) {
found = true;
break;
}
++iter;
}
if (found) {
continue;
}
ml.slot = atoul(row[1]);
ml.item = atoul(row[2]);
ml.faction_required = atoul(row[3]);
ml.level_required = atoul(row[4]);
ml.alt_currency_cost = atoul(row[5]);
ml.classes_required = atoul(row[6]);
ml.probability = atoul(row[7]);
cur->second.push_back(ml);
}
}
void Zone::LoadMercTemplates(){
std::list<MercStanceInfo> merc_stances;
merc_templates.clear();
std::string query = "SELECT `class_id`, `proficiency_id`, `stance_id`, `isdefault` FROM "
"`merc_stance_entries` ORDER BY `class_id`, `proficiency_id`, `stance_id`";
auto results = database.QueryDatabase(query);
if (!results.Success())
Log.Out(Logs::General, Logs::Error, "Error in ZoneDatabase::LoadMercTemplates()");
else {
for (auto row = results.begin(); row != results.end(); ++row) {
MercStanceInfo tempMercStanceInfo;
tempMercStanceInfo.ClassID = atoi(row[0]);
tempMercStanceInfo.ProficiencyID = atoi(row[1]);
tempMercStanceInfo.StanceID = atoi(row[2]);
tempMercStanceInfo.IsDefault = atoi(row[3]);
merc_stances.push_back(tempMercStanceInfo);
}
}
query = "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;";
results = database.QueryDatabase(query);
if (!results.Success()) {
Log.Out(Logs::General, Logs::Error, "Error in ZoneDatabase::LoadMercTemplates()");
return;
}
for (auto row = results.begin(); row != results.end(); ++row) {
MercTemplate tempMercTemplate;
tempMercTemplate.MercTemplateID = atoi(row[0]);
tempMercTemplate.MercType = atoi(row[1]);
tempMercTemplate.MercSubType = atoi(row[2]);
tempMercTemplate.RaceID = atoi(row[3]);
tempMercTemplate.ClassID = atoi(row[4]);
tempMercTemplate.ProficiencyID = atoi(row[5]);
tempMercTemplate.TierID = atoi(row[6]);
tempMercTemplate.CostFormula = atoi(row[7]);
tempMercTemplate.ClientVersion = atoi(row[8]);
tempMercTemplate.MercNPCID = atoi(row[9]);
for(int i = 0; i < MaxMercStanceID; i++)
tempMercTemplate.Stances[i] = 0;
int stanceIndex = 0;
for (auto mercStanceListItr = merc_stances.begin(); mercStanceListItr != merc_stances.end(); ++mercStanceListItr) {
if(mercStanceListItr->ClassID != tempMercTemplate.ClassID || mercStanceListItr->ProficiencyID != tempMercTemplate.ProficiencyID)
continue;
zone->merc_stance_list[tempMercTemplate.MercTemplateID].push_back((*mercStanceListItr));
tempMercTemplate.Stances[stanceIndex] = mercStanceListItr->StanceID;
++stanceIndex;
}
merc_templates[tempMercTemplate.MercTemplateID] = tempMercTemplate;
}
}
void Zone::LoadLevelEXPMods(){
level_exp_mod.clear();
const std::string query = "SELECT level, exp_mod, aa_exp_mod FROM level_exp_mods";
auto results = database.QueryDatabase(query);
if (!results.Success()) {
Log.Out(Logs::General, Logs::Error, "Error in ZoneDatabase::LoadEXPLevelMods()");
return;
}
for (auto row = results.begin(); row != results.end(); ++row) {
uint32 index = atoi(row[0]);
float exp_mod = atof(row[1]);
float aa_exp_mod = atof(row[2]);
level_exp_mod[index].ExpMod = exp_mod;
level_exp_mod[index].AAExpMod = aa_exp_mod;
}
}
void Zone::LoadMercSpells(){
merc_spells_list.clear();
const std::string query = "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()) {
Log.Out(Logs::General, Logs::Error, "Error in Zone::LoadMercSpells()");
return;
}
for (auto row = results.begin(); row != results.end(); ++row) {
uint32 classid;
MercSpellEntry tempMercSpellEntry;
classid = atoi(row[0]);
tempMercSpellEntry.proficiencyid = atoi(row[1]);
tempMercSpellEntry.spellid = atoi(row[2]);
tempMercSpellEntry.type = atoi(row[3]);
tempMercSpellEntry.stance = atoi(row[4]);
tempMercSpellEntry.minlevel = atoi(row[5]);
tempMercSpellEntry.maxlevel = atoi(row[6]);
tempMercSpellEntry.slot = atoi(row[7]);
tempMercSpellEntry.proc_chance = atoi(row[8]);
merc_spells_list[classid].push_back(tempMercSpellEntry);
}
Log.Out(Logs::General, Logs::Mercenaries, "Loaded %i merc spells.", merc_spells_list[1].size() + merc_spells_list[2].size() + merc_spells_list[9].size() + merc_spells_list[12].size());
}
bool Zone::IsLoaded() {
return ZoneLoaded;
}
void Zone::Shutdown(bool quite)
{
if (!ZoneLoaded)
return;
entity_list.StopMobAI();
std::map<uint32,NPCType *>::iterator itr;
while(zone->npctable.size()) {
itr=zone->npctable.begin();
delete itr->second;
zone->npctable.erase(itr);
}
while(zone->merctable.size()) {
itr=zone->merctable.begin();
delete itr->second;
zone->merctable.erase(itr);
}
zone->adventure_entry_list_flavor.clear();
std::map<uint32,LDoNTrapTemplate*>::iterator itr4;
while(zone->ldon_trap_list.size())
{
itr4 = zone->ldon_trap_list.begin();
delete itr4->second;
zone->ldon_trap_list.erase(itr4);
}
zone->ldon_trap_entry_list.clear();
Log.Out(Logs::General, Logs::Status, "Zone Shutdown: %s (%i)", zone->GetShortName(), zone->GetZoneID());
petition_list.ClearPetitions();
zone->GotCurTime(false);
if (!quite)
Log.Out(Logs::General, Logs::Normal, "Zone shutdown: going to sleep");
ZoneLoaded = false;
zone->ResetAuth();
safe_delete(zone);
entity_list.ClearAreas();
parse->ReloadQuests(true);
UpdateWindowTitle();
Log.CloseFileLogs();
}
void Zone::LoadZoneDoors(const char* zone, int16 version)
{
Log.Out(Logs::General, Logs::Status, "Loading doors for %s ...", zone);
uint32 maxid;
int32 count = database.GetDoorsCount(&maxid, zone, version);
if(count < 1) {
Log.Out(Logs::General, Logs::Status, "... No doors loaded.");
return;
}
Door *dlist = new Door[count];
if(!database.LoadDoors(count, dlist, zone, version)) {
Log.Out(Logs::General, Logs::Error, "... Failed to load doors.");
delete[] dlist;
return;
}
int r;
Door *d = dlist;
for(r = 0; r < count; r++, d++) {
Doors* newdoor = new Doors(d);
entity_list.AddDoor(newdoor);
}
delete[] dlist;
}
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),
qglobal_purge_timer(30000),
hotzone_timer(120000),
m_SafePoint(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;
loglevelvar = 0;
merchantvar = 0;
tradevar = 0;
lootvar = 0;
if(RuleB(TaskSystem, EnableTaskSystem)) {
taskmanager->LoadProximities(zoneid);
}
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;
pgraveyard_id = 0;
pgraveyard_zoneid = 0;
pMaxClients = 0;
pQueuedMerchantsWorkID = 0;
pvpzone = false;
if(database.GetServerType() == 1)
pvpzone = true;
database.GetZoneLongName(short_name, &long_name, file_name, &m_SafePoint.x, &m_SafePoint.y, &m_SafePoint.z, &pgraveyard_id, &pMaxClients);
if(graveyard_id() > 0)
{
Log.Out(Logs::General, Logs::None, "Graveyard ID is %i.", graveyard_id());
bool GraveYardLoaded = database.GetZoneGraveyard(graveyard_id(), &pgraveyard_zoneid, &m_Graveyard.x, &m_Graveyard.y, &m_Graveyard.z, &m_Graveyard.w);
if(GraveYardLoaded)
Log.Out(Logs::General, Logs::None, "Loaded a graveyard for zone %s: graveyard zoneid is %u at %s.", short_name, graveyard_zoneid(), to_string(m_Graveyard).c_str());
else
Log.Out(Logs::General, Logs::Error, "Unable to load the graveyard id %i for zone %s.", graveyard_id(), short_name);
}
if (long_name == 0) {
long_name = strcpy(new char[18], "Long zone missing");
}
autoshutdown_timer.Start(AUTHENTICATION_TIMEOUT * 1000, false);
Weather_Timer = new Timer(60000);
Weather_Timer->Start();
Log.Out(Logs::General, Logs::None, "The next weather check for zone: %s will be in %i seconds.", short_name, Weather_Timer->GetRemainingTime()/1000);
zone_weather = 0;
weather_intensity = 0;
blocked_spells = nullptr;
totalBS = 0;
aas = nullptr;
totalAAs = 0;
gottime = false;
Instance_Shutdown_Timer = nullptr;
bool is_perma = false;
if(instanceid > 0)
{
uint32 rem = database.GetTimeRemainingInstance(instanceid, is_perma);
if(!is_perma)
{
if(rem < 150) //give some leeway to people who are zoning in 2.5 minutes to finish zoning in and get ported out
rem = 150;
Instance_Timer = new Timer(rem * 1000);
}
else
{
pers_instance = true;
Instance_Timer = nullptr;
}
}
else
{
Instance_Timer = nullptr;
}
adv_data = nullptr;
map_name = nullptr;
Instance_Warning_timer = nullptr;
did_adventure_actions = false;
database.QGlobalPurge();
if(zoneid == RuleI(World, GuildBankZoneID))
GuildBanks = new GuildBankManager;
else
GuildBanks = nullptr;
}
Zone::~Zone() {
spawn2_list.Clear();
safe_delete(zonemap);
safe_delete(watermap);
safe_delete(pathing);
if (worldserver.Connected()) {
worldserver.SetZone(0);
}
safe_delete_array(short_name);
safe_delete_array(long_name);
safe_delete(Weather_Timer);
NPCEmoteList.Clear();
zone_point_list.Clear();
entity_list.Clear();
ClearBlockedSpells();
safe_delete(Instance_Timer);
safe_delete(Instance_Shutdown_Timer);
safe_delete(Instance_Warning_timer);
safe_delete(qGlobals);
safe_delete_array(adv_data);
safe_delete_array(map_name);
if(aas != nullptr) {
int r;
for(r = 0; r < totalAAs; r++) {
uchar *data = (uchar *) aas[r];
safe_delete_array(data);
}
safe_delete_array(aas);
}
safe_delete(GuildBanks);
}
//Modified for timezones.
bool Zone::Init(bool iStaticZone) {
SetStaticZone(iStaticZone);
Log.Out(Logs::General, Logs::Status, "Loading spawn conditions...");
if(!spawn_conditions.LoadSpawnConditions(short_name, instanceid)) {
Log.Out(Logs::General, Logs::Error, "Loading spawn conditions failed, continuing without them.");
}
Log.Out(Logs::General, Logs::Status, "Loading static zone points...");
if (!database.LoadStaticZonePoints(&zone_point_list, short_name, GetInstanceVersion())) {
Log.Out(Logs::General, Logs::Error, "Loading static zone points failed.");
return false;
}
Log.Out(Logs::General, Logs::Status, "Loading spawn groups...");
if (!database.LoadSpawnGroups(short_name, GetInstanceVersion(), &spawn_group_list)) {
Log.Out(Logs::General, Logs::Error, "Loading spawn groups failed.");
return false;
}
Log.Out(Logs::General, Logs::Status, "Loading spawn2 points...");
if (!database.PopulateZoneSpawnList(zoneid, spawn2_list, GetInstanceVersion()))
{
Log.Out(Logs::General, Logs::Error, "Loading spawn2 points failed.");
return false;
}
Log.Out(Logs::General, Logs::Status, "Loading player corpses...");
if (!database.LoadCharacterCorpses(zoneid, instanceid)) {
Log.Out(Logs::General, Logs::Error, "Loading player corpses failed.");
return false;
}
Log.Out(Logs::General, Logs::Status, "Loading traps...");
if (!database.LoadTraps(short_name, GetInstanceVersion()))
{
Log.Out(Logs::General, Logs::Error, "Loading traps failed.");
return false;
}
Log.Out(Logs::General, Logs::Status, "Loading adventure flavor text...");
LoadAdventureFlavor();
Log.Out(Logs::General, Logs::Status, "Loading ground spawns...");
if (!LoadGroundSpawns())
{
Log.Out(Logs::General, Logs::Error, "Loading ground spawns failed. continuing.");
}
Log.Out(Logs::General, Logs::Status, "Loading World Objects from DB...");
if (!LoadZoneObjects())
{
Log.Out(Logs::General, Logs::Error, "Loading World Objects failed. continuing.");
}
Log.Out(Logs::General, Logs::Status, "Flushing old respawn timers...");
database.QueryDatabase("DELETE FROM `respawn_times` WHERE (`start` + `duration`) < UNIX_TIMESTAMP(NOW())");
//load up the zone's doors (prints inside)
zone->LoadZoneDoors(zone->GetShortName(), zone->GetInstanceVersion());
zone->LoadBlockedSpells(zone->GetZoneID());
//clear trader items if we are loading the bazaar
if(strncasecmp(short_name,"bazaar",6)==0) {
database.DeleteTraderItem(0);
database.DeleteBuyLines(0);
}
zone->LoadLDoNTraps();
zone->LoadLDoNTrapEntries();
zone->LoadVeteranRewards();
zone->LoadAlternateCurrencies();
zone->LoadNPCEmotes(&NPCEmoteList);
//Load AA information
adverrornum = 500;
LoadAAs();
//Load merchant data
adverrornum = 501;
zone->GetMerchantDataForZoneLoad();
//Load temporary merchant data
adverrornum = 502;
zone->LoadTempMerchantData();
// Merc data
if (RuleB(Mercs, AllowMercs)) {
zone->LoadMercTemplates();
zone->LoadMercSpells();
}
if (RuleB(Zone, LevelBasedEXPMods))
zone->LoadLevelEXPMods();
adverrornum = 503;
petition_list.ClearPetitions();
petition_list.ReadDatabase();
//load the zone config file.
if (!LoadZoneCFG(zone->GetShortName(), zone->GetInstanceVersion(), true)) // try loading the zone name...
LoadZoneCFG(zone->GetFileName(), zone->GetInstanceVersion()); // if that fails, try the file name, then load defaults
if(RuleManager::Instance()->GetActiveRulesetID() != default_ruleset)
{
std::string r_name = RuleManager::Instance()->GetRulesetName(&database, default_ruleset);
if(r_name.size() > 0)
{
RuleManager::Instance()->LoadRules(&database, r_name.c_str());
}
}
Log.Out(Logs::General, Logs::Status, "Loading timezone data...");
zone->zone_time.setEQTimeZone(database.GetZoneTZ(zoneid, GetInstanceVersion()));
Log.Out(Logs::General, Logs::Status, "Init Finished: ZoneID = %d, Time Offset = %d", zoneid, zone->zone_time.getEQTimeZone());
LoadTickItems();
//MODDING HOOK FOR ZONE INIT
mod_init();