This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_map.cpp
1825 lines (1757 loc) · 78.6 KB
/
parse_map.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
/**** BEGIN LICENSE BLOCK ****
BSD 3-Clause License
Copyright (c) 2021-2023, the wind.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/
// This is where it all started. Q'N'D code for FF analysis.
// Actually it started because I grew tired of the original game freezing
// on turn x, and rendering the map not complete-able.
// Its not tested on many maps. It won't be, because there will be a specific
// library doing all the FF parsing when the project reaches certain phase.
// Thus, issues related to Q'N'D code will be closed without wasting time with
// them.
// Why is this being published?
// - to observe how this project is being developed
// - create tools based on this one
// - as showcase of how to not write C++ code :)
// /mnt/workspace/drive64_c/games/h3/Maps/Atlantis_v3.h3m
//c clang++ -std=c++11 -Wall -Wextra -Wshadow -fno-rtti -fno-exceptions -fno-threadsafe-statics -g -O0 parse_map.cpp -o parse_map -Wl,--as-needed -lz
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <zlib.h>
#define EOL "\n"
using byte = unsigned char;
class Stream
{
Stream * _f {};
public:
Stream(Stream * s) : _f {s} {};
virtual ~Stream() {}
virtual operator bool() { return _f->operator bool(); }
virtual bool Seek(off_t pos) { return _f->Seek (pos); }
virtual off_t Tell() { return _f->Tell (); }
virtual off_t Size() { return _f->Size (); }
virtual bool Read(void * b, size_t n = 1) { return _f->Read (b, n); }
virtual bool Write(const void * b, size_t n = 1) { return _f->Write (b, n); }
// const Stream * BaseStream() const { return _f; }
};
// Caution: this is very simple and optimistic (all is ok) filestream!
class FileStream final: public Stream
{
FILE * _f {};
off_t _size_on_open {};
public:
FileStream(const char * n, const char * mode = "rb")
: Stream{nullptr}, _f {fopen (n, mode)}
{
fseek (_f, 0, SEEK_END), _size_on_open = ftell (_f);
fseek (_f, 0, SEEK_SET);
}
virtual ~FileStream() override { if (_f) fclose (_f), _f = 0; }
virtual operator bool() override { return 0 != _f; }
virtual bool Seek(off_t pos) override
{
return *this ? 0 == fseek (_f, pos, SEEK_SET) : false;
}
virtual off_t Tell() override { return *this ? ftell (_f) : -1; }
virtual off_t Size() override { return _size_on_open; }
virtual bool Read(void * b, size_t n = 1) override
{
return *this ? ! b ? false : n == fread (b, 1, n, _f) : false;
}
virtual bool Write(const void * b, size_t n = 1) override
{
return *this ? ! b ? false : n == fwrite (b, 1, n, _f) : false;
}
};
class zlibInflateStream final: public Stream
{
z_stream _z {};
int r {~Z_OK};
static const int BUF_SIZE {4096};
byte buf[BUF_SIZE] {};
off_t _read {};
public:
zlibInflateStream(Stream * data)
: Stream {data}, r {inflateInit2 (&_z, 31)} // gzip h3m
{
_z.next_in = buf, _z.avail_in = 0;
}
virtual ~zlibInflateStream() override { inflateEnd (&_z); }
virtual operator bool() override { return Z_OK == r; }
virtual bool Seek(off_t) override { return false; } // can't seek
virtual off_t Tell() override { return _read; } // can tell
virtual off_t Size() override { return -1; } // no idea about usize
virtual bool Read(void * b, size_t n = 1) override // can read
{
if (! b || n < 1) return false; // param check
// printf ("requested %zu bytes" EOL, n);
_z.next_out = (byte *)b, _z.avail_out = n;
while (_z.avail_out) {
if (0 == _z.avail_in) { // block read from the decorated one
int a = Stream::Size () - Stream::Tell ();
// printf ("Avail: %d bytes" EOL, a);
if (a <= 0) return false; // no more input
int c = a < BUF_SIZE ? a : BUF_SIZE;
_z.avail_in = c;
_z.next_in = buf;
Stream::Read (buf, c);
// printf ("Read: %d bytes" EOL, c);
}
r = inflate (&_z, Z_NO_FLUSH);
// printf ("r: %d" EOL, r);
// "be called until it returns Z_STREAM_END or an error"
if (r != Z_OK && r != Z_STREAM_END) return false;
// not enough input
if (Z_STREAM_END == r && _z.avail_out) return false;
}
return _read += n, true;
}
virtual bool Write(const void *, size_t) override { return false; }
};
template <typename T> class Some0Bytes final
{
T * _b {};
public:
Some0Bytes(size_t n)
{
if (n < 0 || n > 10000) { // size 0 is allowed in town timed event
printf ("suspicious byte seqeunce of size %zu" EOL, n);
exit (1);
}
_b = (T *)calloc (n, sizeof(T));
if (! _b) printf ("Can't alloc %zu bytes. Bye." EOL, n), exit (1);
}
~Some0Bytes() { if (_b) free (_b), _b = nullptr; }
operator byte *() { return (byte *)_b; }
operator const byte *() const { return (const byte *)_b; }
operator char *() { return (char *)_b; }
operator const char *() const { return (const char *)_b; }
operator void *() { return _b; }
T & operator [](int i) { return _b[i]; }
};
namespace {static struct L final
{
template <typename T> L & Fmt(const char * f, T & v)
{
return printf (f, v), *this;
}
L & operator<<(const char * v) { return Fmt ("%s", v); }
L & operator<<(int v) { return Fmt ("%d", v); }
L & operator<<(short v) { return Fmt ("%d", v); }
L & operator<<(byte v) { return Fmt ("%d", v); }
L & operator<<(L & l) { return l; }
} Log;}
static inline L & print_byte_sequence(const byte * b, size_t n)
{
if (n <= 0) return Log << "{}";
auto e = b + n;
Log.Fmt ("{%002X", *b++);
while (b < e) Log.Fmt (" %002X", *b++);
return Log << "}";
}
#define MAP_ROE 0x0e
#define MAP_AB 0x15
#define MAP_SOD 0x1c
#define MAP_WOG 0x33
#define MAP_VERSION(V) (MAP_ROE == V ? "RoE" : \
MAP_AB == V ? "AB" : \
MAP_SOD == V ? "SoD" : \
MAP_WOG == V ? "WoG" : "Unknown")
#define MAP_BOOL(V) (V ? "true" : "false")
#define MAP_PLAYERS 8
#define MAP_FACTIONS 9
#define MAP_HEROES 156
#define MAP_PRIMARY_SKILLS 4
#define MAP_RESOURCE_QUANTITY 8
#define MAP_NO_PATROL_RADIUS 255
#define MAP_SKILL_QUANTITY 28
#define MAP_AI_RANDOM 0
#define MAP_AI_WARRIOR 1
#define MAP_AI_BUILDER 2
#define MAP_AI_EXPLORER 3
#define MAP_AI(V) (MAP_AI_RANDOM == V ? "Random" : \
MAP_AI_WARRIOR == V ? "Warrior" : \
MAP_AI_BUILDER == V ? "Builder" : \
MAP_AI_EXPLORER == V ? "Explorer" : "Unknown")
//TODO guess
#define MAP_DEFAULT_HERO_ID 0xff
#define MAP_DEFAULT_HERO_PORTRAIT_ID 0xff
#define MAP_VICTORY_CONDITION_ARTIFACT 0
#define MAP_VICTORY_CONDITION_GATHERTROOP 1
#define MAP_VICTORY_CONDITION_GATHERRESOURCE 2
#define MAP_VICTORY_CONDITION_UPG_CITY 3
#define MAP_VICTORY_CONDITION_BUILDGRAIL 4
#define MAP_VICTORY_CONDITION_BEATHERO 5
#define MAP_VICTORY_CONDITION_CAPTURECITY 6
#define MAP_VICTORY_CONDITION_BEATMONSTER 7
#define MAP_VICTORY_CONDITION_TAKEDWELLINGS 8
#define MAP_VICTORY_CONDITION_TAKEMINES 9
#define MAP_VICTORY_CONDITION_TRANSPORTITEM 10
#define MAP_VICTORY_CONDITION_STD 255
#define MAP_VC(V) (MAP_VICTORY_CONDITION_ARTIFACT == V ? "Artifact" : \
MAP_VICTORY_CONDITION_GATHERTROOP == V ? "Troop" : \
MAP_VICTORY_CONDITION_GATHERRESOURCE == V ? "Resource" : \
MAP_VICTORY_CONDITION_UPG_CITY == V ? "Upgrade City" : \
MAP_VICTORY_CONDITION_BUILDGRAIL == V ? "Grail" : \
MAP_VICTORY_CONDITION_BEATHERO == V ? "Defeat Hero" : \
MAP_VICTORY_CONDITION_CAPTURECITY == V ? "Capture City" : \
MAP_VICTORY_CONDITION_BEATMONSTER == V ? "Defeat Mon" : \
MAP_VICTORY_CONDITION_TAKEDWELLINGS == V ? "Dwellings" : \
MAP_VICTORY_CONDITION_TAKEMINES == V ? "Mines" : \
MAP_VICTORY_CONDITION_TRANSPORTITEM == V ? "Transport" : \
MAP_VICTORY_CONDITION_STD == V ? "Standard" : \
"Unknown")
#define MAP_LOSS_CONDITION_CASTLE 0
#define MAP_LOSS_CONDITION_HERO 1
#define MAP_LOSS_CONDITION_TIMEEXPIRES 2
#define MAP_LOSS_CONDITION_STD 255
#define MAP_LC(V) (MAP_LOSS_CONDITION_CASTLE == V ? "Castle" : \
MAP_LOSS_CONDITION_HERO == V ? "Hero" : \
MAP_LOSS_CONDITION_TIMEEXPIRES == V ? "Time" : \
MAP_LOSS_CONDITION_STD == V ? "Standard" : \
"Unknown")
#define MAP_TOWN 0
#define MAP_CITY 1
#define MAP_CAPITOL 2
#define MAP_HALL_LEVEL(V) (MAP_TOWN == V ? "Town" : \
MAP_CITY == V ? "City" : \
MAP_CAPITOL == V ? "Capitol" : "Unknown")
#define MAP_FORT 0
#define MAP_CITADEL 1
#define MAP_CASTLE 2
#define MAP_CASTLE_LEVEL(V) (MAP_FORT == V ? "Fort" : \
MAP_CITADEL == V ? "Citadel" : \
MAP_CASTLE == V ? "Castle" : "Unknown")
#define MAP_HERO_MALE 0
#define MAP_HERO_FEMALE 1
#define MAP_HERO_SEX(V) (MAP_HERO_MALE == V ? "Male" : \
MAP_HERO_FEMALE == V ? "Female" : "Unknown")
#define MAP_TERRAIN_DIRT 0
#define MAP_TERRAIN_SAND 1
#define MAP_TERRAIN_GRASS 2
#define MAP_TERRAIN_SNOW 3
#define MAP_TERRAIN_SWAMP 4
#define MAP_TERRAIN_ROUGH 5
#define MAP_TERRAIN_SUBTERRANEAN 6
#define MAP_TERRAIN_LAVA 7
#define MAP_TERRAIN_WATER 8
#define MAP_TERRAIN_ROCK 9
#define MAP_TT(V) (MAP_TERRAIN_DIRT == V ? "Dirt" : \
MAP_TERRAIN_SAND == V ? "Sand" : \
MAP_TERRAIN_GRASS == V ? "Grass" : \
MAP_TERRAIN_SNOW == V ? "Snow" : \
MAP_TERRAIN_SWAMP == V ? "Swamp" : \
MAP_TERRAIN_ROUGH == V ? "Rough" : \
MAP_TERRAIN_SUBTERRANEAN == V ? "Subt" : \
MAP_TERRAIN_LAVA == V ? "Lava" : \
MAP_TERRAIN_WATER == V ? "Water" : \
MAP_TERRAIN_ROCK == V ? "Rock" : "Unknown")
#define MAP_NO_RIVER 0
#define MAP_CLEAR_RIVER 1
#define MAP_ICY_RIVER 2
#define MAP_MUDDY_RIVER 3
#define MAP_LAVA_RIVER 4
#define MAP_RIVER(V) (MAP_NO_RIVER == V ? "None" : \
MAP_CLEAR_RIVER == V ? "Clear" : \
MAP_ICY_RIVER == V ? "Icy" : \
MAP_MUDDY_RIVER == V ? "Muddy" : \
MAP_LAVA_RIVER == V ? "Lava" : "Unknown")
#define MAP_NO_ROAD 0
#define MAP_DIRT_ROAD 1
#define MAP_GRAVEL_ROAD 2
#define MAP_COBBLESTONE_ROAD 3
#define MAP_ROAD(V) (MAP_NO_ROAD == V ? "None" : \
MAP_DIRT_ROAD == V ? "Dirt" : \
MAP_GRAVEL_ROAD == V ? "Gravel" : \
MAP_COBBLESTONE_ROAD == V ? "Cobblestone" : "Unknown")
#define MAP_O_ALTAR_OF_SACRIFICE 2
#define MAP_O_ANCHOR_POINT 3
#define MAP_O_ARENA 4
#define MAP_O_ARTIFACT 5
#define MAP_O_PANDORAS_BOX 6
#define MAP_O_BLACK_MARKET 7
#define MAP_O_BOAT 8
#define MAP_O_BORDERGUARD 9
#define MAP_O_KEYMASTER 10
#define MAP_O_BUOY 11
#define MAP_O_CAMPFIRE 12
#define MAP_O_CARTOGRAPHER 13
#define MAP_O_SWAN_POND 14
#define MAP_O_COVER_OF_DARKNESS 15
#define MAP_O_CREATURE_BANK 16
#define MAP_O_CREATURE_GENERATOR1 17
#define MAP_O_CREATURE_GENERATOR2 18
#define MAP_O_CREATURE_GENERATOR3 19
#define MAP_O_CREATURE_GENERATOR4 20
#define MAP_O_CURSED_GROUND1 21
#define MAP_O_CORPSE 22
#define MAP_O_MARLETTO_TOWER 23
#define MAP_O_DERELICT_SHIP 24
#define MAP_O_DRAGON_UTOPIA 25
#define MAP_O_EVENT 26
#define MAP_O_EYE_OF_MAGI 27
#define MAP_O_FAERIE_RING 28
#define MAP_O_FLOTSAM 29
#define MAP_O_FOUNTAIN_OF_FORTUNE 30
#define MAP_O_FOUNTAIN_OF_YOUTH 31
#define MAP_O_GARDEN_OF_REVELATION 32
#define MAP_O_GARRISON 33
#define MAP_O_HERO 34
#define MAP_O_HILL_FORT 35
#define MAP_O_GRAIL 36
#define MAP_O_HUT_OF_MAGI 37
#define MAP_O_IDOL_OF_FORTUNE 38
#define MAP_O_LEAN_TO 39
#define MAP_O_LIBRARY_OF_ENLIGHTENMENT 41
#define MAP_O_LIGHTHOUSE 42
#define MAP_O_MONOLITH1 43
#define MAP_O_MONOLITH2 44
#define MAP_O_MONOLITH3 45
#define MAP_O_MAGIC_PLAINS1 46
#define MAP_O_SCHOOL_OF_MAGIC 47
#define MAP_O_MAGIC_SPRING 48
#define MAP_O_MAGIC_WELL 49
#define MAP_O_MERCENARY_CAMP 51
#define MAP_O_MERMAID 52
#define MAP_O_MINE 53
#define MAP_O_MONSTER 54
#define MAP_O_MYSTICAL_GARDEN 55
#define MAP_O_OASIS 56
#define MAP_O_OBELISK 57
#define MAP_O_REDWOOD_OBSERVATORY 58
#define MAP_O_OCEAN_BOTTLE 59
#define MAP_O_PILLAR_OF_FIRE 60
#define MAP_O_STAR_AXIS 61
#define MAP_O_PRISON 62
#define MAP_O_PYRAMID 63
#define MAP_O_WOG_OBJECT 63
#define MAP_O_RALLY_FLAG 64
#define MAP_O_RANDOM_ART 65
#define MAP_O_RANDOM_TREASURE_ART 66
#define MAP_O_RANDOM_MINOR_ART 67
#define MAP_O_RANDOM_MAJOR_ART 68
#define MAP_O_RANDOM_RELIC_ART 69
#define MAP_O_RANDOM_HERO 70
#define MAP_O_RANDOM_MONSTER 71
#define MAP_O_RANDOM_MONSTER_L1 72
#define MAP_O_RANDOM_MONSTER_L2 73
#define MAP_O_RANDOM_MONSTER_L3 74
#define MAP_O_RANDOM_MONSTER_L4 75
#define MAP_O_RANDOM_RESOURCE 76
#define MAP_O_RANDOM_TOWN 77
#define MAP_O_REFUGEE_CAMP 78
#define MAP_O_RESOURCE 79
#define MAP_O_SANCTUARY 80
#define MAP_O_SCHOLAR 81
#define MAP_O_SEA_CHEST 82
#define MAP_O_SEER_HUT 83
#define MAP_O_CRYPT 84
#define MAP_O_SHIPWRECK 85
#define MAP_O_SHIPWRECK_SURVIVOR 86
#define MAP_O_SHIPYARD 87
#define MAP_O_SHRINE_OF_MAGIC_INCANTATION 88
#define MAP_O_SHRINE_OF_MAGIC_GESTURE 89
#define MAP_O_SHRINE_OF_MAGIC_THOUGHT 90
#define MAP_O_SIGN 91
#define MAP_O_SIRENS 92
#define MAP_O_SPELL_SCROLL 93
#define MAP_O_STABLES 94
#define MAP_O_TAVERN 95
#define MAP_O_TEMPLE 96
#define MAP_O_DEN_OF_THIEVES 97
#define MAP_O_TOWN 98
#define MAP_O_TRADING_POST 99
#define MAP_O_LEARNING_STONE 100
#define MAP_O_TREASURE_CHEST 101
#define MAP_O_TREE_OF_KNOWLEDGE 102
#define MAP_O_SUBTERRANEAN_GATE 103
#define MAP_O_UNIVERSITY 104
#define MAP_O_WAGON 105
#define MAP_O_WAR_MACHINE_FACTORY 106
#define MAP_O_SCHOOL_OF_WAR 107
#define MAP_O_WARRIORS_TOMB 108
#define MAP_O_WATER_WHEEL 109
#define MAP_O_WATERING_HOLE 110
#define MAP_O_WHIRLPOOL 111
#define MAP_O_WINDMILL 112
#define MAP_O_WITCH_HUT 113
#define MAP_O_HOLE 124
#define MAP_O_RANDOM_MONSTER_L5 162
#define MAP_O_RANDOM_MONSTER_L6 163
#define MAP_O_RANDOM_MONSTER_L7 164
#define MAP_O_BORDER_GATE 212
#define MAP_O_FREELANCERS_GUILD 213
#define MAP_O_HERO_PLACEHOLDER 214
#define MAP_O_QUEST_GUARD 215
#define MAP_O_RANDOM_DWELLING 216
#define MAP_O_RANDOM_DWELLING_LVL 217
#define MAP_O_RANDOM_DWELLING_FACTION 218
#define MAP_O_GARRISON2 219
#define MAP_O_ABANDONED_MINE 220
#define MAP_O_TRADING_POST_SNOW 221
#define MAP_O_CLOVER_FIELD 222
#define MAP_O_CURSED_GROUND2 223
#define MAP_O_EVIL_FOG 224
#define MAP_O_FAVORABLE_WINDS 225
#define MAP_O_FIERY_FIELDS 226
#define MAP_O_HOLY_GROUNDS 227
#define MAP_O_LUCID_POOLS 228
#define MAP_O_MAGIC_CLOUDS 229
#define MAP_O_MAGIC_PLAINS2 230
#define MAP_O_ROCKLANDS 231
#define MAP_SEER_NOTHING 0
#define MAP_SEER_EXPERIENCE 1
#define MAP_SEER_MANA_POINTS 2
#define MAP_SEER_MORALE_BONUS 3
#define MAP_SEER_LUCK_BONUS 4
#define MAP_SEER_RESOURCES 5
#define MAP_SEER_PRIMARY_SKILL 6
#define MAP_SEER_SECONDARY_SKILL 7
#define MAP_SEER_ARTIFACT 8
#define MAP_SEER_SPELL 9
#define MAP_SEER_CREATURE 10
#define MAP_SEER_REWARD(V) (MAP_SEER_NOTHING == V ? "None" : \
MAP_SEER_EXPERIENCE == V ? "Exp" : \
MAP_SEER_MANA_POINTS == V ? "Mana" : \
MAP_SEER_MORALE_BONUS == V ? "Morale" : \
MAP_SEER_LUCK_BONUS == V ? "Luck" : \
MAP_SEER_RESOURCES == V ? "Res" : \
MAP_SEER_PRIMARY_SKILL == V ? "PSkill" : \
MAP_SEER_SECONDARY_SKILL == V ? "SSkill" : \
MAP_SEER_ARTIFACT == V ? "Art" : \
MAP_SEER_SPELL == V ? "Spell" : \
MAP_SEER_CREATURE == V ? "Cre" : \
"Unknown")
/* map editor (SoD) limits:
"There is a maximum of 32 objects of type "Learning Stone" on any map!"
"There is a maximum of 32 objects of type "Star Axis" on any map!"
"There is a maximum of 144 objects of type "Creature Generator 1" on any map!"
*/
template <typename T> T Read(Stream & br)
{
T data;
if (! br.Read (&data, sizeof(T))) { printf ("Read failed\n"); exit (1); }
return data;
}
// Avoid manual size computation. "virtual template <typename T>":
template <typename T> bool Read(Stream & br, T & d, size_t n = 1)
{
if (! br.Read (&d, n * sizeof(T))) { printf ("Read failed\n"); exit (1); }
return true;
}
template <typename T> void Read(Stream & br, T * d, size_t n = 1)
{
if (! br.Read (d, n * sizeof(T))) { printf ("Read failed\n"); exit (1); }
}
static bool ReadSkip(Stream & br, size_t n)
{
Some0Bytes<byte> buf (n);
if (! br.Read (buf, n)) {
printf ("Read failed\n");
exit (1);
}
return true;
}
struct MapString
{
int Len;
Some0Bytes<byte> Chars;
MapString(Stream & s)
: Len {Read<int> (s)}, Chars {static_cast<size_t>(Len+1)}
{
Read (s, Chars.operator byte * (), Len);
}
};
#pragma pack(push, 1)
struct MapObject final
{
MapString SpriteName;
byte PassMask[6]; // w,h = (sprite_name.s(.def/.msk)).bytes[0],[1]
byte TrigMask[6]; // the mask max is 8x6 (at the editor there is a lake 7x3)
byte Unk1[2];
short TerrainMask; // ?
int EdId; // objects[i] = def_id; string ref - editor name
int SubId;
byte Type; // no idea yet
byte PrintPriority; // ?
byte Unk2[16];
MapObject(Stream & s)
: SpriteName {s}
{
s.Read (&PassMask, sizeof(MapObject) - sizeof(MapString));
}
};
#pragma pack(pop)
L & operator<<(L &, MapObject & obj)
{
Log << " sprite: "
<< obj.SpriteName.Chars.operator const char * () << EOL
<< " PMask: " << print_byte_sequence (obj.PassMask, 6)
<< ", TMask: " << print_byte_sequence (obj.TrigMask, 6) << EOL
<< " U1: " << print_byte_sequence (obj.Unk1, 2)
<< ", TerrMask: " << obj.TerrainMask
<< ", EdtorId: " << Log.Fmt ("%3d", obj.EdId)
<< ", SubId: " << Log.Fmt ("%3d", obj.SubId)
<< ", Type: " << obj.Type
<< ", zOrder: " << obj.PrintPriority << EOL
<< " U2: " << print_byte_sequence (obj.Unk2, 16) << EOL;
return Log;
}
static bool read_string(Stream & br, const char * what)
{
int len;
if (! Read (br, len)) return false;
if (len < 0 || len > 10000) // size 0 is allowed in town timed event
//TODO get the decompressed offset via BaseStream interface
return printf ("suspicious string of size %d" EOL, len), false;
Some0Bytes<byte> str (len + 1); // add my '\0' for printf
if (len > 0 && ! br.Read (str, len))
return printf ("failed to read string of size %d" EOL, len), false;
printf ("%s%s" EOL, what, (const char *)str);
return true;
}
static bool read_pos(Stream & br)
{
byte x, y, z;
bool result = Read (br, x) && Read (br, y) && Read (br, z);
if (result) printf ("x, y, z = %d, %d, %d", x, y, z);
return result;
}
static bool read_id_ROE8_16(Stream & br, int map_version, short int & id)
{
if (MAP_ROE == map_version) {
byte id8;
if (! Read (br, id8)) return false;
id = id8;
} else
if (! Read (br, id)) return false;
return true;
}
static bool loadArtifactToSlot(Stream & br, int map_version)
{
short int art_id;
read_id_ROE8_16 (br, map_version, art_id);
printf ("id: %d" EOL, art_id);
return true;
}
static bool readResourses(Stream & br)
{
for(int i = 0; i < MAP_RESOURCE_QUANTITY-1; i++)
{
int res;
if (! Read (br, res)) return false;
printf (" Resource%d: %d" EOL, i, res);
}
return true;
}
static bool readCreatureSet(Stream & br, int map_version, int num)
{
for (int i = 0; i < num; i++) {
short int id, cnum;
read_id_ROE8_16 (br, map_version, id);
printf (" Cre%d: id: %d" EOL, i, id);
if (! Read (br, cnum)) return false;
printf (" Cre%d: num: %d" EOL, i, cnum);
}
return true;
}
static bool loadArtifactsOfHero(Stream & br, int map_version, int i)
{
byte has_arts;
if (! Read (br, has_arts)) return false;
printf ("Customized Hero #%3d: has arts: %s" EOL, i,
MAP_BOOL(has_arts));
if (has_arts) {
for (int a = 0; a < 16; a++) { // TODO 16 ?
printf ("Customized Hero #%3d: art%d", i, a);
if (! loadArtifactToSlot (br, map_version)) return false;
printf (EOL);
}
if (map_version > MAP_AB) {
printf ("Customized Hero #%3d: art_mach4", i);
if (! loadArtifactToSlot (br, map_version)) return false;
}
printf ("Customized Hero #%3d: art_spellbook", i);
if (! loadArtifactToSlot (br, map_version)) return false;
printf (EOL);
if (map_version > MAP_ROE) {
printf ("Customized Hero #%3d: art_misc5", i);
if (! loadArtifactToSlot (br, map_version)) return false;
printf (EOL);
} else if (! ReadSkip (br, 1)) return 1;
short int bag_quantity;
if (! Read (br, bag_quantity)) return false;
printf ("Customized Hero #%3d: bag arts: %d" EOL, i,
bag_quantity);
for (int b = 0; b < bag_quantity; b++) {
printf ("Customized Hero #%3d: art_bag%d", i, b);
if (! loadArtifactToSlot (br, map_version)) return false;
printf (EOL);
}
} // if (has_arts)
return true;
}
static bool readMessageAndGuards(Stream & br, int map_version)
{
bool has_message;
if (! Read (br, has_message)) return false;
printf (" MsgGuard: has_message: %s" EOL, MAP_BOOL(has_message));
if (! has_message) return true;
read_string (br, " MsgGuard: Msg: ");
bool has_guard;
if (! Read (br, has_guard)) return false;
printf (" MsgGuard: has_guard: %s" EOL, MAP_BOOL(has_guard));
if (has_guard)
if (! readCreatureSet (br, map_version, 7)) return false;
if (! ReadSkip (br, 4)) return false;
return true;
}
static bool readQuest(Stream & br, byte & mission_type)
{
if (! Read (br, mission_type)) return false;
printf (" Quest: mission_type: %d" EOL, mission_type);
switch (mission_type) {
case 0: return printf (" Quest: mtype 0?" EOL), false;
case 2: {
for (int i = 0; i < 4; i++) {
byte m2stats;
if (! Read (br, m2stats)) return false;
printf (" Quest: m2stats%d: %d" EOL, i, m2stats);
}
} break;
case 1:
case 3:
case 4: {
int m13489val; //TODO WT nice mountain view?
if (! Read (br, m13489val)) return false;
printf (" Quest: m13489val?: %d" EOL, m13489val);
} break;
case 5: {
byte art_num;
if (! Read (br, art_num)) return false;
printf (" Quest: arts: %d" EOL, art_num);
for (int i = 0; i < art_num; i++) {
short int art_id;
if (! Read (br, art_id)) return false;
printf (" Quest: art%d: %d" EOL, i, art_id);
}
} break;
case 6: {
byte cre_num;
if (! Read (br, cre_num)) return false;
printf (" Quest: cre: %d" EOL, cre_num);
for (int i = 0; i < cre_num; i++) {
short int cre_type, cre_qty;
if (! Read (br, cre_type)) return false;
if (! Read (br, cre_qty)) return false;
printf (" Quest: cre%d: type: %d, qty: %d" EOL, i, cre_type,
cre_qty);
}
} break;
case 7: {
for (int i = 0; i < 7; i++) {
int res_qty;
if (! Read (br, res_qty)) return false;
printf (" Quest: res%d: qty: %d" EOL, i, res_qty);
}
} break;
case 8:
case 9: {
byte m13489val; //TODO WT nice mountain view?
if (! Read (br, m13489val)) return false;
printf (" Quest: m13489val?: %d" EOL, m13489val);
} break;
}; // switch (mission_type)
int limit;
if (! Read (br, limit)) return false;
printf (" Quest: limit: %d" EOL, limit);
read_string (br, " Quest: 1st visit: ");
read_string (br, " Quest: next visit: ");
read_string (br, " Quest: completed: ");
return true;
}
#define TELL_POS printf ("Unc Stream Pos: %.8lX" EOL, br.Tell ());
int main(int argc, char ** argv)
{
if (2 != argc) return printf ("parse_map foo.h3m" EOL), 1;
FileStream cdata {argv[1]};
zlibInflateStream br {&cdata};
// header
int map_version;
if (! Read (br, map_version)) return 1;
printf ("Map Version : %s" EOL, MAP_VERSION(map_version));
byte areAnyPlayers;
if (! Read (br, areAnyPlayers)) return 1;
printf ("areAnyPlayers: %s" EOL, MAP_BOOL(areAnyPlayers));
int h, w;
if (! Read (br, w)) return 1;
h = w;
printf ("Size : %d x %d" EOL, w, h);
byte two_level;
if (! Read (br, two_level)) return 1;
printf ("Levels : %d" EOL, two_level ? 2 : 1);
if (! read_string(br, "Name : ")) return 1;
if (! read_string(br, "Description : ")) return 1;
byte difficulty;
if (! Read (br, difficulty)) return 1;
printf ("Difficulty : %d" EOL, difficulty);
byte levelLimit = 0; // avaialble at !RoE
if (map_version != MAP_ROE && ! Read (br, levelLimit)) return 1;
printf ("Level Limit : %d" EOL, levelLimit);
// readPlayerInfo();
int map_players {};
for (int i = 0; i < MAP_PLAYERS; i++) {
byte canHumanPlay, canComputerPlay;
if (! Read (br, canHumanPlay)) return 1;
if (! Read (br, canComputerPlay)) return 1;
printf (" Player%d: Human :%s" EOL, i+1, MAP_BOOL(canHumanPlay));
printf (" Player%d: Computer:%s" EOL, i+1, MAP_BOOL(canComputerPlay));
if (0 == canHumanPlay && 0 == canComputerPlay) // can't play
switch (map_version) {
case MAP_SOD:
case MAP_WOG: if (! ReadSkip (br, 13)) return 1; break;
case MAP_AB : if (! ReadSkip (br, 12)) return 1; break;
case MAP_ROE: if (! ReadSkip (br, 6)) return 1; break;
default: printf ("You have a bug" EOL); return 1;
}
else { // can play
map_players++;
byte aiTactic;
if (! Read (br, aiTactic)) return 1;
printf (" Player%d: AI :%s" EOL, i+1, MAP_AI(aiTactic));
if (MAP_SOD == map_version || MAP_WOG == map_version) {
byte p7 {}; // unknown
if (! Read (br, p7)) return 1;
}
byte allowedFactions, totalFactions {MAP_FACTIONS};
if (! Read (br, allowedFactions)) return 1;
printf (" Player%d: factions0 :%d" EOL, i+1, allowedFactions);
if (map_version != MAP_ROE) // its 16-bit
{ if (! Read (br, allowedFactions)) return 1; }
else totalFactions--; // no Conflux in RoE
printf (" Player%d: factions1 :%d" EOL, i+1, allowedFactions);
byte isFactionRandom;
if (! Read (br, isFactionRandom)) return 1;
printf (" Player%d: random faction :%s" EOL, i+1,
MAP_BOOL(isFactionRandom));
byte hasMainTown;
if (! Read (br, hasMainTown)) return 1;
printf (" Player%d: has main town :%s" EOL, i+1,
MAP_BOOL(hasMainTown));
if (hasMainTown) {
byte generateHeroAtMainTown {1}, generateHero {0};
if (map_version != MAP_ROE) {
if (! Read (br, generateHeroAtMainTown)) return 1;
if (! Read (br, generateHero)) return 1;
printf (" Player%d: gen at main town :%s" EOL, i+1,
MAP_BOOL(generateHeroAtMainTown));
printf (" Player%d: gen hero :%s" EOL, i+1,
MAP_BOOL(generateHero));
}
printf (" Player%d: main town pos:", i+1);
if (! read_pos (br)) return 1;
printf (EOL);
}
byte hasRandomHero, mainCustomHeroId {MAP_DEFAULT_HERO_ID};
if (! Read (br, hasRandomHero)) return 1;
printf (" Player%d: has random hero :%s" EOL, i+1,
MAP_BOOL(hasRandomHero));
if (! Read (br, mainCustomHeroId)) return 1;
printf (" Player%d: mainCustomHeroId :%d" EOL, i+1, mainCustomHeroId);
if (mainCustomHeroId != MAP_DEFAULT_HERO_ID) {
byte mainCustomHeroPortrait {MAP_DEFAULT_HERO_PORTRAIT_ID};
if (! Read (br, mainCustomHeroPortrait)) return 1;
printf (" Player%d: mainCustomHeroPortraitId :%d" EOL, i+1,
mainCustomHeroPortrait);
printf (" Player%d:", i+1);
if (! read_string(br, " mainCustomHeroName : ")) return 1;
}
if (map_version != MAP_ROE) {
byte powerPlaceholders;
if (! Read (br, powerPlaceholders)) return 1;
printf (" Player%d: powerPlaceholders :%d" EOL, i+1,
powerPlaceholders);
byte heroCount;
if (! Read (br, heroCount)) return 1;
printf (" Player%d: heroCount :%d" EOL, i+1, heroCount);
if (! ReadSkip (br, 3)) return 1; //TODO its probably an int32
for (int j = 0; j < heroCount; j++) {
byte heroId;
if (! Read (br, heroId)) return 1;
printf (" Player%d: hero%d Id :%d" EOL, i+1, j+1, heroId);
printf (" Player%d: hero%d", i+1, j+1);
if (! read_string(br, " Name : ")) return 1;
}
}
} // can play
} // for (int i = 0; i < MAP_PLAYERS; i++)
// readVictoryLossConditions();
byte vicCondition;
if (! Read (br, vicCondition)) return 1;
printf ("Victory Cond : %s" EOL, MAP_VC(vicCondition));
if (vicCondition != MAP_VICTORY_CONDITION_STD) {
byte allowNormalVictory, appliesToAI;
if (! Read (br, allowNormalVictory)) return 1;
printf (" Allow Normal Victory : %s" EOL, MAP_BOOL(allowNormalVictory));
if (! Read (br, appliesToAI)) return 1;
printf (" Applies to AI : %s" EOL, MAP_BOOL(appliesToAI));
if (map_players <= 1 && allowNormalVictory)
return printf ("Defect map" EOL), 1;
switch (vicCondition) {
case MAP_VICTORY_CONDITION_ARTIFACT: {
byte objectType;
if (! Read (br, objectType)) return 1;
//TODO int16 perhaps
if (map_version != MAP_ROE)
if (! ReadSkip (br, 1)) return 1;
printf (" Artifact : %d" EOL, objectType);
} break;
case MAP_VICTORY_CONDITION_GATHERTROOP: {
byte objectType;
if (! Read (br, objectType)) return 1;
//TODO int16 perhaps
if (map_version != MAP_ROE)
if (! ReadSkip (br, 1)) return 1;
printf (" Troop : %d" EOL, objectType);
int quantity;
if (! Read (br, quantity)) return 1;
printf (" Troop quantity : %d" EOL, quantity);
} break;
case MAP_VICTORY_CONDITION_GATHERRESOURCE: {
byte objectType;
if (! Read (br, objectType)) return 1;
printf (" Resource : %d" EOL, objectType);
int quantity;
if (! Read (br, quantity)) return 1;
printf (" Resource quantity : %d" EOL, quantity);
} break;
case MAP_VICTORY_CONDITION_UPG_CITY: {
printf (" City pos :");
if (! read_pos (br)) return 1;
printf (EOL);
byte hallLevel, castleLevel;
if (! Read (br, hallLevel)) return 1;
printf (" Hall Level :%s" EOL, MAP_HALL_LEVEL(hallLevel));
if (! Read (br, castleLevel)) return 1;
printf (" Castle Level :%s" EOL, MAP_CASTLE_LEVEL(castleLevel));
} break;
case MAP_VICTORY_CONDITION_BUILDGRAIL:
case MAP_VICTORY_CONDITION_BEATHERO:
case MAP_VICTORY_CONDITION_CAPTURECITY:
case MAP_VICTORY_CONDITION_BEATMONSTER: {
printf (" Target pos :");
if (! read_pos (br)) return 1;
printf (EOL);
} break;
case MAP_VICTORY_CONDITION_TAKEDWELLINGS: break;
case MAP_VICTORY_CONDITION_TAKEMINES: break;
case MAP_VICTORY_CONDITION_TRANSPORTITEM: {
byte objectType;
if (! Read (br, objectType)) return 1;
printf (" Item Id : %d" EOL, objectType);
printf (" Item pos :");
if (! read_pos (br)) return 1;
printf (EOL);
} break;
default: return printf (" Unknown victory condition" EOL), 1;
} // switch (vicCondition) {
} // vicCondition != MAP_VICTORY_CONDITION_STD
byte lossCondition;
if (! Read (br, lossCondition)) return 1;
printf ("Loss Cond : %s" EOL, MAP_LC(lossCondition));
if (lossCondition != MAP_LOSS_CONDITION_STD)
switch (lossCondition) {
case MAP_LOSS_CONDITION_CASTLE:
case MAP_LOSS_CONDITION_HERO: {
printf (" Target pos :");
if (! read_pos (br)) return 1;
printf (EOL);
} break;
case MAP_LOSS_CONDITION_TIMEEXPIRES: {
short int quantity;
if (! Read (br, quantity)) return 1;
printf (" Time quantity : %d" EOL, quantity);
} break;
default: return printf (" Unknown loss condition" EOL), 1;
}
// readTeamInfo();
byte teams;
if (! Read (br, teams)) return 1;
printf ("Teams : %d" EOL, teams);
for (int i = 0; teams > 0 && i < MAP_PLAYERS; i++) {
byte team;
if (! Read (br, team)) return 1;
printf ("team[%d]=%d" EOL, i, team);
}
// readAllowedHeroes();
int ma_bytes = MAP_ROE == map_version ? 16 : 20;
Some0Bytes<byte> map_allowed_heroes (ma_bytes);
if (! br.Read (map_allowed_heroes, ma_bytes)) return 1;
printf ("AllowedHeroes: %d bytes" EOL, ma_bytes);
if (map_version > MAP_ROE) {
int placeholdersQty;
if (! Read (br, placeholdersQty)) return 1;
printf ("placeholdersQty: %d" EOL, placeholdersQty); // whats this?
for (int i = 0; i < placeholdersQty; i++) {
byte placeholder;
if (! Read (br, placeholder)) return 1;
printf ("placeholders%d: %d" EOL, i, placeholder); // whats this?
}
}
// header ends here according to VCMI
// readDisposedHeroes();
if (map_version >= MAP_SOD) {
byte num_dh;
if (! Read (br, num_dh)) return 1;
printf ("Num disposed? heroes : %d" EOL, num_dh);
for (int i = 0; i < num_dh; i++) {
byte id, portrait_id, players;
if (! Read (br, id)) return 1;
printf (" disposed hero%d Id: %d" EOL, i, id);
if (! Read (br, portrait_id)) return 1;
printf (" disposed hero%d Portrait Id: %d" EOL, i, portrait_id);
printf (" disposed hero%d", i);
if (! read_string(br, " Name: ")) return 1;
if (! Read (br, players)) return 1;
printf (" disposed hero%d Players?: %d" EOL, i, players);
}
}
if (! ReadSkip (br, 31)) return 1;
// readAllowedArtifacts();
if (map_version != MAP_ROE) {
int bytes2 = MAP_AB == map_version ? 17 : 18;