-
Notifications
You must be signed in to change notification settings - Fork 224
/
iomap_otbm.cpp
1549 lines (1322 loc) · 41.5 KB
/
iomap_otbm.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
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor 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, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; 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, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include <wx/wfstream.h>
#include <wx/tarstrm.h>
#include <wx/zstream.h>
#include <wx/mstream.h>
#include <wx/datstrm.h>
#include "settings.h"
#include "gui.h" // Loadbar
#include "creatures.h"
#include "creature.h"
#include "map.h"
#include "tile.h"
#include "item.h"
#include "complexitem.h"
#include "town.h"
#include "iomap_otbm.h"
typedef uint8_t attribute_t;
typedef uint32_t flags_t;
// H4X
void reform(Map* map, Tile* tile, Item* item)
{
/*
int aid = item->getActionID();
int id = item->getID();
int uid = item->getUniqueID();
if(item->isDoor()) {
item->eraseAttribute("aid");
item->setAttribute("keyid", aid);
}
if((item->isDoor()) && tile && tile->getHouseID()) {
Door* self = static_cast<Door*>(item);
House* house = map->houses.getHouse(tile->getHouseID());
self->setDoorID(house->getEmptyDoorID());
}
*/
}
// ============================================================================
// Item
Item* Item::Create_OTBM(const IOMap& maphandle, BinaryNode* stream)
{
uint16_t id;
if(!stream->getU16(id)) {
return nullptr;
}
const ItemType& type = g_items.getItemType(id);
uint8_t count = 0;
if(maphandle.version.otbm == MAP_OTBM_1) {
if(type.stackable || type.isSplash() || type.isFluidContainer()) {
stream->getU8(count);
}
}
return Item::Create(id, count);
}
bool Item::readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attr, BinaryNode* stream)
{
switch (attr) {
case OTBM_ATTR_COUNT: {
uint8_t subtype;
if(!stream->getU8(subtype)) {
return false;
}
setSubtype(subtype);
break;
}
case OTBM_ATTR_ACTION_ID: {
uint16_t aid;
if(!stream->getU16(aid)) {
return false;
}
setActionID(aid);
break;
}
case OTBM_ATTR_UNIQUE_ID: {
uint16_t uid;
if(!stream->getU16(uid)) {
return false;
}
setUniqueID(uid);
break;
}
case OTBM_ATTR_CHARGES: {
uint16_t charges;
if(!stream->getU16(charges)) {
return false;
}
setSubtype(charges);
break;
}
case OTBM_ATTR_TEXT: {
std::string text;
if(!stream->getString(text)) {
return false;
}
setText(text);
break;
}
case OTBM_ATTR_DESC: {
std::string text;
if(!stream->getString(text)) {
return false;
}
setDescription(text);
break;
}
case OTBM_ATTR_RUNE_CHARGES: {
uint8_t subtype;
if(!stream->getU8(subtype)) {
return false;
}
setSubtype(subtype);
break;
}
// The following *should* be handled in the derived classes
// However, we still need to handle them here since otherwise things
// will break horribly
case OTBM_ATTR_DEPOT_ID: return stream->skip(2);
case OTBM_ATTR_HOUSEDOORID: return stream->skip(1);
case OTBM_ATTR_TELE_DEST: return stream->skip(5);
default: return false;
}
return true;
}
bool Item::unserializeAttributes_OTBM(const IOMap& maphandle, BinaryNode* stream)
{
uint8_t attribute;
while(stream->getU8(attribute)) {
if(attribute == OTBM_ATTR_ATTRIBUTE_MAP) {
if(!ItemAttributes::unserializeAttributeMap(maphandle, stream)) {
return false;
}
} else if(!readItemAttribute_OTBM(maphandle, static_cast<OTBM_ItemAttribute>(attribute), stream)) {
return false;
}
}
return true;
}
bool Item::unserializeItemNode_OTBM(const IOMap& maphandle, BinaryNode* node)
{
return unserializeAttributes_OTBM(maphandle, node);
}
void Item::serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& stream) const
{
if(maphandle.version.otbm >= MAP_OTBM_2) {
const ItemType& type = g_items.getItemType(id);
if(type.stackable || type.isSplash() || type.isFluidContainer()) {
stream.addU8(OTBM_ATTR_COUNT);
stream.addU8(getSubtype());
}
}
if(maphandle.version.otbm >= MAP_OTBM_4) {
if(attributes && !attributes->empty()) {
stream.addU8(OTBM_ATTR_ATTRIBUTE_MAP);
serializeAttributeMap(maphandle, stream);
}
} else {
if(g_items.MinorVersion >= CLIENT_VERSION_820 && isCharged()) {
stream.addU8(OTBM_ATTR_CHARGES);
stream.addU16(getSubtype());
}
uint16_t actionId = getActionID();
if(actionId > 0) {
stream.addU8(OTBM_ATTR_ACTION_ID);
stream.addU16(actionId);
}
uint16_t uniqueId = getUniqueID();
if(uniqueId > 0) {
stream.addU8(OTBM_ATTR_UNIQUE_ID);
stream.addU16(uniqueId);
}
const std::string& text = getText();
if(!text.empty()) {
stream.addU8(OTBM_ATTR_TEXT);
stream.addString(text);
}
const std::string& description = getDescription();
if(!description.empty()) {
stream.addU8(OTBM_ATTR_DESC);
stream.addString(description);
}
}
}
void Item::serializeItemCompact_OTBM(const IOMap& maphandle, NodeFileWriteHandle& stream) const
{
stream.addU16(id);
/* This is impossible
const ItemType& iType = g_items[id];
if(iType.stackable || iType.isSplash() || iType.isFluidContainer()){
stream.addU8(getSubtype());
}
*/
}
bool Item::serializeItemNode_OTBM(const IOMap& maphandle, NodeFileWriteHandle& file) const
{
file.addNode(OTBM_ITEM);
file.addU16(id);
if(maphandle.version.otbm == MAP_OTBM_1) {
const ItemType& type = g_items.getItemType(id);
if(type.stackable || type.isSplash() || type.isFluidContainer()) {
file.addU8(getSubtype());
}
}
serializeItemAttributes_OTBM(maphandle, file);
file.endNode();
return true;
}
// ============================================================================
// Teleport
bool Teleport::readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attribute, BinaryNode* stream)
{
if(OTBM_ATTR_TELE_DEST == attribute) {
uint16_t x, y;
uint8_t z;
if(!stream->getU16(x) || !stream->getU16(y) || !stream->getU8(z)) {
return false;
}
destination = Position(x, y, z);
return true;
} else {
return Item::readItemAttribute_OTBM(maphandle, attribute, stream);
}
}
void Teleport::serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& stream) const
{
Item::serializeItemAttributes_OTBM(maphandle, stream);
stream.addByte(OTBM_ATTR_TELE_DEST);
stream.addU16(destination.x);
stream.addU16(destination.y);
stream.addU8(destination.z);
}
// ============================================================================
// Door
bool Door::readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attribute, BinaryNode* stream)
{
if(OTBM_ATTR_HOUSEDOORID == attribute) {
uint8_t id = 0;
if(!stream->getU8(id)) {
return false;
}
doorId = id;
return true;
} else {
return Item::readItemAttribute_OTBM(maphandle, attribute, stream);
}
}
void Door::serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& stream) const
{
Item::serializeItemAttributes_OTBM(maphandle, stream);
if(doorId) {
stream.addByte(OTBM_ATTR_HOUSEDOORID);
stream.addU8(doorId);
}
}
// ============================================================================
// Depots
bool Depot::readItemAttribute_OTBM(const IOMap& maphandle, OTBM_ItemAttribute attribute, BinaryNode* stream)
{
if(OTBM_ATTR_DEPOT_ID == attribute) {
uint16_t id = 0;
if(!stream->getU16(id)) {
return false;
}
depotId = id;
return true;
} else {
return Item::readItemAttribute_OTBM(maphandle, attribute, stream);
}
}
void Depot::serializeItemAttributes_OTBM(const IOMap& maphandle, NodeFileWriteHandle& stream) const
{
Item::serializeItemAttributes_OTBM(maphandle, stream);
if(depotId) {
stream.addByte(OTBM_ATTR_DEPOT_ID);
stream.addU16(depotId);
}
}
// ============================================================================
// Container
bool Container::unserializeItemNode_OTBM(const IOMap& maphandle, BinaryNode* node)
{
if(!Item::unserializeAttributes_OTBM(maphandle, node)) {
return false;
}
BinaryNode* child = node->getChild();
if(child) {
do {
uint8_t type;
if(!child->getByte(type)) {
return false;
}
if(type != OTBM_ITEM) {
return false;
}
Item* item = Item::Create_OTBM(maphandle, child);
if(!item) {
return false;
}
if(!item->unserializeItemNode_OTBM(maphandle, child)) {
delete item;
return false;
}
contents.push_back(item);
} while(child->advance());
}
return true;
}
bool Container::serializeItemNode_OTBM(const IOMap& maphandle, NodeFileWriteHandle& file) const
{
file.addNode(OTBM_ITEM);
file.addU16(id);
if(maphandle.version.otbm == MAP_OTBM_1) {
// In the ludicrous event that an item is a container AND stackable, we have to do this. :p
const ItemType& type = g_items.getItemType(id);
if(type.stackable || type.isSplash() || type.isFluidContainer()) {
file.addU8(getSubtype());
}
}
serializeItemAttributes_OTBM(maphandle, file);
for(Item* item : contents) {
item->serializeItemNode_OTBM(maphandle, file);
}
file.endNode();
return true;
}
/*
OTBM_ROOTV1
|
|--- OTBM_MAP_DATA
| |
| |--- OTBM_TILE_AREA
| | |--- OTBM_TILE
| | |--- OTBM_TILE_SQUARE (not implemented)
| | |--- OTBM_TILE_REF (not implemented)
| | |--- OTBM_HOUSETILE
| |
| |--- OTBM_SPAWNS (not implemented)
| | |--- OTBM_SPAWN_AREA (not implemented)
| | |--- OTBM_MONSTER (not implemented)
| |
| |--- OTBM_TOWNS
| |--- OTBM_TOWN
|
|--- OTBM_ITEM_DEF (not implemented)
*/
bool IOMapOTBM::getVersionInfo(const FileName& filename, MapVersion& out_ver)
{
#if OTGZ_SUPPORT > 0
if(filename.GetExt() == "otgz") {
// Open the archive
std::shared_ptr<struct archive> a(archive_read_new(), archive_read_free);
archive_read_support_filter_all(a.get());
archive_read_support_format_all(a.get());
if(archive_read_open_filename(a.get(), nstr(filename.GetFullPath()).c_str(), 10240) != ARCHIVE_OK)
return false;
// Loop over the archive entries until we find the otbm file
struct archive_entry* entry;
while(archive_read_next_header(a.get(), &entry) == ARCHIVE_OK) {
std::string entryName = archive_entry_pathname(entry);
if(entryName == "world/map.otbm") {
// Read the OTBM header into temporary memory
uint8_t buffer[8096];
memset(buffer, 0, 8096);
// Read from the archive
int read_bytes = archive_read_data(a.get(), buffer, 8096);
// Check so it at least contains the 4-byte file id
if(read_bytes < 4)
return false;
// Create a read handle on it
std::shared_ptr<NodeFileReadHandle> f(new MemoryNodeFileReadHandle(buffer + 4, read_bytes - 4));
// Read the version info
return getVersionInfo(f.get(), out_ver);
}
}
// Didn't find OTBM file, lame
return false;
}
#endif
// Just open a disk-based read handle
DiskNodeFileReadHandle f(nstr(filename.GetFullPath()), StringVector(1, "OTBM"));
if(!f.isOk())
return false;
return getVersionInfo(&f, out_ver);
}
bool IOMapOTBM::getVersionInfo(NodeFileReadHandle* f, MapVersion& out_ver)
{
BinaryNode* root = f->getRootNode();
if(!root)
return false;
root->skip(1); // Skip the type byte
uint16_t u16;
uint32_t u32;
if(!root->getU32(u32)) // Version
return false;
out_ver.otbm = (MapVersionID)u32;
root->getU16(u16);
root->getU16(u16);
root->getU32(u32);
if(!root->getU32(u32)) // OTB minor version
return false;
out_ver.client = ClientVersionID(u32);
return true;
}
bool IOMapOTBM::loadMap(Map& map, const FileName& filename)
{
#if OTGZ_SUPPORT > 0
if(filename.GetExt() == "otgz") {
// Open the archive
std::shared_ptr<struct archive> a(archive_read_new(), archive_read_free);
archive_read_support_filter_all(a.get());
archive_read_support_format_all(a.get());
if(archive_read_open_filename(a.get(), nstr(filename.GetFullPath()).c_str(), 10240) != ARCHIVE_OK)
return false;
// Memory buffers for the houses & spawns
std::shared_ptr<uint8_t> house_buffer;
std::shared_ptr<uint8_t> spawn_buffer;
size_t house_buffer_size = 0;
size_t spawn_buffer_size = 0;
// See if the otbm file has been loaded
bool otbm_loaded = false;
// Loop over the archive entries until we find the otbm file
g_gui.SetLoadDone(0, "Decompressing archive...");
struct archive_entry* entry;
while(archive_read_next_header(a.get(), &entry) == ARCHIVE_OK) {
std::string entryName = archive_entry_pathname(entry);
if(entryName == "world/map.otbm") {
// Read the entire OTBM file into a memory region
size_t otbm_size = archive_entry_size(entry);
std::shared_ptr<uint8_t> otbm_buffer(new uint8_t[otbm_size], [](uint8_t* p) { delete[] p; });
// Read from the archive
size_t read_bytes = archive_read_data(a.get(), otbm_buffer.get(), otbm_size);
// Check so it at least contains the 4-byte file id
if(read_bytes < 4)
return false;
if(read_bytes < otbm_size) {
error("Could not read file.");
return false;
}
g_gui.SetLoadDone(0, "Loading OTBM map...");
// Create a read handle on it
std::shared_ptr<NodeFileReadHandle> f(
new MemoryNodeFileReadHandle(otbm_buffer.get() + 4, otbm_size - 4));
// Read the version info
if(!loadMap(map, *f.get())) {
error("Could not load OTBM file inside archive");
return false;
}
otbm_loaded = true;
} else if(entryName == "world/houses.xml") {
house_buffer_size = archive_entry_size(entry);
house_buffer.reset(new uint8_t[house_buffer_size]);
// Read from the archive
size_t read_bytes = archive_read_data(a.get(), house_buffer.get(), house_buffer_size);
// Check so it at least contains the 4-byte file id
if(read_bytes < house_buffer_size) {
house_buffer.reset();
house_buffer_size = 0;
warning("Failed to decompress houses.");
}
} else if(entryName == "world/spawns.xml") {
spawn_buffer_size = archive_entry_size(entry);
spawn_buffer.reset(new uint8_t[spawn_buffer_size]);
// Read from the archive
size_t read_bytes = archive_read_data(a.get(), spawn_buffer.get(), spawn_buffer_size);
// Check so it at least contains the 4-byte file id
if(read_bytes < spawn_buffer_size) {
spawn_buffer.reset();
spawn_buffer_size = 0;
warning("Failed to decompress spawns.");
}
}
}
if(!otbm_loaded) {
error("OTBM file not found inside archive.");
return false;
}
// Load the houses from the stored buffer
if(house_buffer.get() && house_buffer_size > 0) {
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_buffer(house_buffer.get(), house_buffer_size);
if(result) {
if(!loadHouses(map, doc)) {
warning("Failed to load houses.");
}
} else {
warning("Failed to load houses due to XML parse error.");
}
}
// Load the spawns from the stored buffer
if(spawn_buffer.get() && spawn_buffer_size > 0) {
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_buffer(spawn_buffer.get(), spawn_buffer_size);
if(result) {
if(!loadSpawns(map, doc)) {
warning("Failed to load spawns.");
}
} else {
warning("Failed to load spawns due to XML parse error.");
}
}
return true;
}
#endif
DiskNodeFileReadHandle f(nstr(filename.GetFullPath()), StringVector(1, "OTBM"));
if(!f.isOk()) {
error(("Couldn't open file for reading\nThe error reported was: " + wxstr(f.getErrorMessage())).wc_str());
return false;
}
if(!loadMap(map, f))
return false;
// Read auxilliary files
if(!loadHouses(map, filename)) {
warning("Failed to load houses.");
map.housefile = nstr(filename.GetName()) + "-house.xml";
}
if(!loadSpawns(map, filename)) {
warning("Failed to load spawns.");
map.spawnfile = nstr(filename.GetName()) + "-spawn.xml";
}
return true;
}
bool IOMapOTBM::loadMap(Map& map, NodeFileReadHandle& f)
{
BinaryNode* root = f.getRootNode();
if(!root) {
error("Could not read root node.");
return false;
}
root->skip(1); // Skip the type byte
uint8_t u8;
uint16_t u16;
uint32_t u32;
if(!root->getU32(u32))
return false;
version.otbm = (MapVersionID) u32;
if(version.otbm > MAP_OTBM_4) {
// Failed to read version
if(g_gui.PopupDialog("Map error",
"The loaded map appears to be a OTBM format that is not supported by the editor."
"Do you still want to attempt to load the map?", wxYES | wxNO) == wxID_YES)
{
warning("Unsupported or damaged map version");
} else {
error("Unsupported OTBM version, could not load map");
return false;
}
}
if(!root->getU16(u16))
return false;
map.width = u16;
if(!root->getU16(u16))
return false;
map.height = u16;
if(!root->getU32(u32) || u32 > (unsigned long)g_items.MajorVersion) { // OTB major version
if(g_gui.PopupDialog("Map error",
"The loaded map appears to be a items.otb format that deviates from the "
"items.otb loaded by the editor. Do you still want to attempt to load the map?", wxYES | wxNO) == wxID_YES)
{
warning("Unsupported or damaged map version");
} else {
error("Outdated items.otb, could not load map");
return false;
}
}
if(!root->getU32(u32) || u32 > (unsigned long)g_items.MinorVersion) { // OTB minor version
warning("This editor needs an updated items.otb version");
}
version.client = (ClientVersionID)u32;
BinaryNode* mapHeaderNode = root->getChild();
if(mapHeaderNode == nullptr || !mapHeaderNode->getByte(u8) || u8 != OTBM_MAP_DATA) {
error("Could not get root child node. Cannot recover from fatal error!");
return false;
}
uint8_t attribute;
while(mapHeaderNode->getU8(attribute)) {
switch(attribute) {
case OTBM_ATTR_DESCRIPTION: {
if(!mapHeaderNode->getString(map.description)) {
warning("Invalid map description tag");
}
//std::cout << "Map description: " << mapDescription << std::endl;
break;
}
case OTBM_ATTR_EXT_SPAWN_FILE: {
if(!mapHeaderNode->getString(map.spawnfile)) {
warning("Invalid map spawnfile tag");
}
break;
}
case OTBM_ATTR_EXT_HOUSE_FILE: {
if(!mapHeaderNode->getString(map.housefile)) {
warning("Invalid map housefile tag");
}
break;
}
default: {
warning("Unknown header node.");
break;
}
}
}
int nodes_loaded = 0;
for(BinaryNode* mapNode = mapHeaderNode->getChild(); mapNode != nullptr; mapNode = mapNode->advance()) {
++nodes_loaded;
if(nodes_loaded % 15 == 0) {
g_gui.SetLoadDone(static_cast<int32_t>(100.0 * f.tell() / f.size()));
}
uint8_t node_type;
if(!mapNode->getByte(node_type)) {
warning("Invalid map node");
continue;
}
if(node_type == OTBM_TILE_AREA) {
uint16_t base_x, base_y;
uint8_t base_z;
if(!mapNode->getU16(base_x) || !mapNode->getU16(base_y) || !mapNode->getU8(base_z)) {
warning("Invalid map node, no base coordinate");
continue;
}
for(BinaryNode* tileNode = mapNode->getChild(); tileNode != nullptr; tileNode = tileNode->advance()) {
Tile* tile = nullptr;
uint8_t tile_type;
if(!tileNode->getByte(tile_type)) {
warning("Invalid tile type");
continue;
}
if(tile_type == OTBM_TILE || tile_type == OTBM_HOUSETILE) {
//printf("Start\n");
uint8_t x_offset, y_offset;
if(!tileNode->getU8(x_offset) || !tileNode->getU8(y_offset)) {
warning("Could not read position of tile");
continue;
}
const Position pos(base_x + x_offset, base_y + y_offset, base_z);
if(map.getTile(pos)) {
warning("Duplicate tile at %d:%d:%d, discarding duplicate", pos.x, pos.y, pos.z);
continue;
}
tile = map.allocator(map.createTileL(pos));
House* house = nullptr;
if(tile_type == OTBM_HOUSETILE) {
uint32_t house_id;
if(!tileNode->getU32(house_id)) {
warning("House tile without house data, discarding tile");
continue;
}
if(house_id) {
house = map.houses.getHouse(house_id);
if(!house) {
house = newd House(map);
house->id = house_id;
map.houses.addHouse(house);
}
} else {
warning("Invalid house id from tile %d:%d:%d", pos.x, pos.y, pos.z);
}
}
//printf("So far so good\n");
uint8_t attribute;
while(tileNode->getU8(attribute)) {
switch(attribute) {
case OTBM_ATTR_TILE_FLAGS: {
uint32_t flags = 0;
if(!tileNode->getU32(flags)) {
warning("Invalid tile flags of tile on %d:%d:%d", pos.x, pos.y, pos.z);
}
tile->setMapFlags(flags);
break;
}
case OTBM_ATTR_ITEM: {
Item* item = Item::Create_OTBM(*this, tileNode);
if(item == nullptr)
{
warning("Invalid item at tile %d:%d:%d", pos.x, pos.y, pos.z);
}
tile->addItem(item);
break;
}
default: {
warning("Unknown tile attribute at %d:%d:%d", pos.x, pos.y, pos.z);
break;
}
}
}
//printf("Didn't die in loop\n");
for(BinaryNode* itemNode = tileNode->getChild(); itemNode != nullptr; itemNode = itemNode->advance()) {
Item* item = nullptr;
uint8_t item_type;
if(!itemNode->getByte(item_type)) {
warning("Unknown item type %d:%d:%d", pos.x, pos.y, pos.z);
continue;
}
if(item_type == OTBM_ITEM) {
item = Item::Create_OTBM(*this, itemNode);
if(item) {
if(!item->unserializeItemNode_OTBM(*this, itemNode)) {
warning("Couldn't unserialize item attributes at %d:%d:%d", pos.x, pos.y, pos.z);
}
//reform(&map, tile, item);
tile->addItem(item);
}
} else {
warning("Unknown type of tile child node");
}
}
tile->update();
if(house)
house->addTile(tile);
map.setTile(pos.x, pos.y, pos.z, tile);
} else {
warning("Unknown type of tile node");
}
}
} else if(node_type == OTBM_TOWNS) {
for(BinaryNode* townNode = mapNode->getChild(); townNode != nullptr; townNode = townNode->advance()) {
Town* town = nullptr;
uint8_t town_type;
if(!townNode->getByte(town_type)) {
warning("Invalid town type (1)");
continue;
}
if(town_type != OTBM_TOWN) {
warning("Invalid town type (2)");
continue;
}
uint32_t town_id;
if(!townNode->getU32(town_id)) {
warning("Invalid town id");
continue;
}
town = map.towns.getTown(town_id);
if(town) {
warning("Duplicate town id %d, discarding duplicate", town_id);
continue;
} else {
town = newd Town(town_id);
if(!map.towns.addTown(town)) {
delete town;
continue;
}
}
std::string town_name;
if(!townNode->getString(town_name)) {
warning("Invalid town name");
continue;
}
town->setName(town_name);
Position pos;
uint16_t x;
uint16_t y;
uint8_t z;
if(!townNode->getU16(x) || !townNode->getU16(y) || !townNode->getU8(z)) {
warning("Invalid town temple position");
continue;
}
pos.x = x;
pos.y = y;
pos.z = z;
town->setTemplePosition(pos);
}
} else if(node_type == OTBM_WAYPOINTS) {
for(BinaryNode* waypointNode = mapNode->getChild(); waypointNode != nullptr; waypointNode = waypointNode->advance()) {
uint8_t waypoint_type;
if(!waypointNode->getByte(waypoint_type)) {
warning("Invalid waypoint type (1)");
continue;
}
if(waypoint_type != OTBM_WAYPOINT) {
warning("Invalid waypoint type (2)");
continue;
}
Waypoint wp;
if(!waypointNode->getString(wp.name)) {
warning("Invalid waypoint name");
continue;
}
uint16_t x;
uint16_t y;
uint8_t z;
if(!waypointNode->getU16(x) || !waypointNode->getU16(y) || !waypointNode->getU8(z)) {
warning("Invalid waypoint position");
continue;
}
wp.pos.x = x;
wp.pos.y = y;
wp.pos.z = z;
map.waypoints.addWaypoint(newd Waypoint(wp));
}
}
}
if(!f.isOk())
warning(wxstr(f.getErrorMessage()).wc_str());
return true;
}
bool IOMapOTBM::loadSpawns(Map& map, const FileName& dir)
{
std::string fn = (const char*)(dir.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).mb_str(wxConvUTF8));
fn += map.spawnfile;
FileName filename(wxstr(fn));
if(!filename.FileExists())
return false;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(fn.c_str());
if(!result) {
return false;
}
return loadSpawns(map, doc);
}
bool IOMapOTBM::loadSpawns(Map& map, pugi::xml_document& doc)
{
pugi::xml_node node = doc.child("spawns");
if(!node) {
warnings.push_back("IOMapOTBM::loadSpawns: Invalid rootheader.");
return false;
}
for(pugi::xml_node spawnNode = node.first_child(); spawnNode; spawnNode = spawnNode.next_sibling()) {
if(as_lower_str(spawnNode.name()) != "spawn") {
continue;
}
Position spawnPosition;
spawnPosition.x = spawnNode.attribute("centerx").as_int();
spawnPosition.y = spawnNode.attribute("centery").as_int();
spawnPosition.z = spawnNode.attribute("centerz").as_int();
if(spawnPosition.x == 0 || spawnPosition.y == 0) {
warning("Bad position data on one spawn, discarding...");
continue;
}
int32_t radius = spawnNode.attribute("radius").as_int();
if(radius < 1) {
warning("Couldn't read radius of spawn.. discarding spawn...");
continue;
}
Tile* tile = map.getTile(spawnPosition);
if(tile && tile->spawn) {
warning("Duplicate spawn on position %d:%d:%d\n", tile->getX(), tile->getY(), tile->getZ());
continue;
}
Spawn* spawn = newd Spawn(radius);
if(!tile) {
tile = map.allocator(map.createTileL(spawnPosition));
map.setTile(spawnPosition, tile);
}
tile->spawn = spawn;
map.addSpawn(tile);
for(pugi::xml_node creatureNode = spawnNode.first_child(); creatureNode; creatureNode = creatureNode.next_sibling()) {
const std::string& creatureNodeName = as_lower_str(creatureNode.name());
if(creatureNodeName != "monster" && creatureNodeName != "npc") {
continue;
}
bool isNpc = creatureNodeName == "npc";
const std::string& name = creatureNode.attribute("name").as_string();
if(name.empty()) {
wxString err;
err << "Bad creature position data, discarding creature at spawn " << spawnPosition.x << ":" << spawnPosition.y << ":" << spawnPosition.z << " due missing name.";
warnings.Add(err);
break;
}