/
mapimporter.cpp
1974 lines (1663 loc) · 65.9 KB
/
mapimporter.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
/** @file mapimporter.cpp Resource importer for id Tech 1 format maps.
*
* @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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; either version 2 of the License, or (at your
* option) any later version. This program 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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "mapimporter.h"
#include <de/libcore.h>
#include <de/Error>
#include <de/ByteRefArray>
#include <de/LogBuffer>
#include <de/Reader>
#include <de/Time>
#include <de/Vector>
#include "importidtech1.h"
#include <QVector>
#include <vector>
#include <list>
#include <set>
using namespace de;
namespace idtech1 {
namespace internal {
/**
* Intersect an unbounded line with a bounded line segment.
*
* @todo This is from libgloom (geomath.h); should not duplicate it here but use
* that one in the future when it is available.
*
* @returns true, if line A-B intersects the line segment @a other.
*/
static bool lineSegmentIntersection(double &lineT,
const Vec2d &lineA, const Vec2d &lineB,
const Vec2d &segmentA, const Vec2d &segmentB)
{
const auto &p = segmentA;
const auto r = segmentB - segmentA;
const auto &q = lineA;
const auto s = lineB - lineA;
const double r_s = r.cross(s);
if (std::abs(r_s) < EPSILON)
{
return false;
}
lineT = (q - p).cross(r) / r_s;
// It has to hit somewhere on `other`.
const double u = (q - p).cross(s) / r_s;
return u >= 0 && u < 1;
}
/// @todo kludge - remove me.
class Id1MapElement
{
public:
Id1MapElement(MapImporter &map) : _map(&map) {}
Id1MapElement(Id1MapElement const &other) : _map(other._map) {}
virtual ~Id1MapElement() {}
MapImporter &map() const {
DENG2_ASSERT(_map != 0);
return *_map;
}
MapImporter *_map;
};
struct Vertex
{
Vec2d pos;
std::set<int> lines; // lines connected to this vertex
};
struct SideDef : public Id1MapElement
{
dint index;
dint16 offset[2];
MaterialId topMaterial;
MaterialId bottomMaterial;
MaterialId middleMaterial;
dint sector;
SideDef(MapImporter &map) : Id1MapElement(map) {}
void operator << (de::Reader &from)
{
Id1MapRecognizer::Format format = Id1MapRecognizer::Format(from.version());
from >> offset[VX]
>> offset[VY];
dint idx;
switch(format)
{
case Id1MapRecognizer::DoomFormat:
case Id1MapRecognizer::HexenFormat: {
Block name;
from.readBytes(8, name);
topMaterial = map().toMaterialId(name.constData(), WallMaterials);
from.readBytes(8, name);
bottomMaterial = map().toMaterialId(name.constData(), WallMaterials);
from.readBytes(8, name);
middleMaterial = map().toMaterialId(name.constData(), WallMaterials);
break; }
case Id1MapRecognizer::Doom64Format:
from.readAs<duint16>(idx);
topMaterial = map().toMaterialId(idx, WallMaterials);
from.readAs<duint16>(idx);
bottomMaterial = map().toMaterialId(idx, WallMaterials);
from.readAs<duint16>(idx);
middleMaterial = map().toMaterialId(idx, WallMaterials);
break;
default:
DENG2_ASSERT(!"idtech1::SideDef::read: unknown map format!");
break;
};
from.readAs<duint16>(idx);
sector = (idx == 0xFFFF? -1 : idx);
}
};
/**
* @defgroup lineAnalysisFlags Line Analysis flags
*/
///@{
#define LAF_POLYOBJ 0x1 ///< Line defines a polyobj segment.
///@}
#define PO_LINE_START (1) ///< Polyobj line start special.
#define PO_LINE_EXPLICIT (5)
#define SEQTYPE_NUMSEQ (10)
struct LineDef : public Id1MapElement
{
enum Side {
Front,
Back
};
dint index;
dint v[2];
dint sides[2];
dint16 flags; ///< MF_* flags.
// Analysis data:
dint16 aFlags;
// DOOM format members:
dint16 dType;
dint16 dTag;
// Hexen format members:
dint8 xType;
dint8 xArgs[5];
// DOOM64 format members:
dint8 d64drawFlags;
dint8 d64texFlags;
dint8 d64type;
dint8 d64useType;
dint16 d64tag;
dint ddFlags;
duint validCount; ///< Used for polyobj line collection.
LineDef(MapImporter &map) : Id1MapElement(map) {}
int sideIndex(Side which) const
{
DENG2_ASSERT(which == Front || which == Back);
return sides[which];
}
inline bool hasSide(Side which) const { return sideIndex(which) >= 0; }
inline bool hasFront() const { return hasSide(Front); }
inline bool hasBack() const { return hasSide(Back); }
inline bool isTwoSided() const { return hasFront() && hasBack(); }
inline dint front() const { return sideIndex(Front); }
inline dint back() const { return sideIndex(Back); }
void operator << (de::Reader &from)
{
Id1MapRecognizer::Format format = Id1MapRecognizer::Format(from.version());
dint idx;
from.readAs<duint16>(idx);
v[0] = (idx == 0xFFFF? -1 : idx);
from.readAs<duint16>(idx);
v[1] = (idx == 0xFFFF? -1 : idx);
from >> flags;
switch(format)
{
case Id1MapRecognizer::DoomFormat:
from >> dType
>> dTag;
break;
case Id1MapRecognizer::Doom64Format:
from >> d64drawFlags
>> d64texFlags
>> d64type
>> d64useType
>> d64tag;
break;
case Id1MapRecognizer::HexenFormat:
from >> xType
>> xArgs[0]
>> xArgs[1]
>> xArgs[2]
>> xArgs[3]
>> xArgs[4];
break;
default:
DENG2_ASSERT(!"idtech1::LineDef::read: unknown map format!");
break;
};
from.readAs<duint16>(idx);
sides[Front] = (idx == 0xFFFF? -1 : idx);
from.readAs<duint16>(idx);
sides[Back] = (idx == 0xFFFF? -1 : idx);
aFlags = 0;
validCount = 0;
ddFlags = 0;
// Translate the line flags for Doomsday:
const int16_t ML_BLOCKING = 1; // Solid, is an obstacle.
const int16_t ML_DONTPEGTOP = 8; // Upper texture unpegged.
const int16_t ML_DONTPEGBOTTOM = 16; // Lower texture unpegged.
/// If set ALL flags NOT in DOOM v1.9 will be zeroed upon map load.
const int16_t ML_INVALID = 2048;
const int16_t DOOM_VALIDMASK = 0x01ff;
/**
* Zero unused flags if ML_INVALID is set.
*
* @attention "This has been found to be necessary because of errors
* in Ultimate DOOM's E2M7, where around 1000 linedefs have
* the value 0xFE00 masked into the flags value.
* There could potentially be many more maps with this problem,
* as it is well-known that Hellmaker wads set all bits in
* mapthings that it does not understand."
* Thanks to Quasar for the heads up.
*
* Only valid for DOOM format maps.
*/
if(format == Id1MapRecognizer::DoomFormat)
{
if(flags & ML_INVALID)
flags &= DOOM_VALIDMASK;
}
if(flags & ML_BLOCKING)
{
ddFlags |= DDLF_BLOCKING;
flags &= ~ML_BLOCKING;
}
if(flags & ML_DONTPEGTOP)
{
ddFlags |= DDLF_DONTPEGTOP;
flags &= ~ML_DONTPEGTOP;
}
if(flags & ML_DONTPEGBOTTOM)
{
ddFlags |= DDLF_DONTPEGBOTTOM;
flags &= ~ML_DONTPEGBOTTOM;
}
}
};
inline LineDef::Side opposite(LineDef::Side side)
{
return side == LineDef::Front ? LineDef::Back : LineDef::Front;
}
enum {
// Sector analysis flags.
SAF_NONE = 0,
SAF_IS_LINK_TARGET = 0x1,
SAF_HAS_AT_LEAST_ONE_SELF_REFERENCING_LINE = 0x2,
SAF_HAS_SELF_REFERENCING_LOOP = 0x4,
// Detected hacks.
HACK_SELF_REFERENCING = 0x01,
HACK_MISSING_OUTSIDE_TOP = 0x02, // invisible door
HACK_MISSING_OUTSIDE_BOTTOM = 0x04, // invisible platform
HACK_MISSING_INSIDE_TOP = 0x08, // flat bleeding in ceiling
HACK_MISSING_INSIDE_BOTTOM = 0x10, // flat bleeding in floor
};
struct SectorDef : public Id1MapElement
{
dint index;
dint16 floorHeight;
dint16 ceilHeight;
dint16 lightLevel;
dint16 type;
dint16 tag;
MaterialId floorMaterial;
MaterialId ceilMaterial;
// DOOM64 format members:
dint16 d64flags;
duint16 d64floorColor;
duint16 d64ceilingColor;
duint16 d64unknownColor;
duint16 d64wallTopColor;
duint16 d64wallBottomColor;
// Internal bookkeeping:
std::set<int> lines;
std::vector<int> selfRefLoop;
int singleSidedCount = 0;
int aFlags = 0;
int foundHacks = 0;
struct de_api_sector_hacks_s hackParams{{}, -1};
SectorDef(MapImporter &map) : Id1MapElement(map) {}
void operator << (de::Reader &from)
{
Id1MapRecognizer::Format format = Id1MapRecognizer::Format(from.version());
from >> floorHeight
>> ceilHeight;
switch(format)
{
case Id1MapRecognizer::DoomFormat:
case Id1MapRecognizer::HexenFormat: {
Block name;
from.readBytes(8, name);
floorMaterial = map().toMaterialId(name.constData(), PlaneMaterials);
from.readBytes(8, name);
ceilMaterial = map().toMaterialId(name.constData(), PlaneMaterials);
from >> lightLevel;
break; }
case Id1MapRecognizer::Doom64Format: {
duint16 idx;
from >> idx;
floorMaterial = map().toMaterialId(idx, PlaneMaterials);
from >> idx;
ceilMaterial = map().toMaterialId(idx, PlaneMaterials);
from >> d64ceilingColor
>> d64floorColor
>> d64unknownColor
>> d64wallTopColor
>> d64wallBottomColor;
lightLevel = 160; ///?
break; }
default:
DENG2_ASSERT(!"idtech1::SectorDef::read: unknown map format!");
break;
};
from >> type
>> tag;
if(format == Id1MapRecognizer::Doom64Format)
from >> d64flags;
}
};
// Thing DoomEdNums for polyobj anchors/spawn spots.
#define PO_ANCHOR_DOOMEDNUM (3000)
#define PO_SPAWN_DOOMEDNUM (3001)
#define PO_SPAWNCRUSH_DOOMEDNUM (3002)
/// @todo Get these from a game api header.
#define MTF_Z_FLOOR 0x20000000 ///< Spawn relative to floor height.
#define MTF_Z_CEIL 0x40000000 ///< Spawn relative to ceiling height (minus thing height).
#define MTF_Z_RANDOM 0x80000000 ///< Random point between floor and ceiling.
#define ANG45 0x20000000
struct Thing : public Id1MapElement
{
dint index;
dint16 origin[3];
angle_t angle;
dint16 doomEdNum;
dint32 flags;
dint32 skillModes;
// Hexen format members:
dint16 xTID;
dint8 xSpecial;
dint8 xArgs[5];
// DOOM64 format members:
dint16 d64TID;
Thing(MapImporter &map) : Id1MapElement(map) {}
void operator << (de::Reader &from)
{
Id1MapRecognizer::Format format = Id1MapRecognizer::Format(from.version());
switch(format)
{
case Id1MapRecognizer::DoomFormat: {
#define MTF_EASY 0x00000001 ///< Can be spawned in Easy skill modes.
#define MTF_MEDIUM 0x00000002 ///< Can be spawned in Medium skill modes.
#define MTF_HARD 0x00000004 ///< Can be spawned in Hard skill modes.
#define MTF_DEAF 0x00000008 ///< Mobj will be deaf spawned deaf.
#define MTF_NOTSINGLE 0x00000010 ///< (BOOM) Can not be spawned in single player gamemodes.
#define MTF_NOTDM 0x00000020 ///< (BOOM) Can not be spawned in the Deathmatch gameMode.
#define MTF_NOTCOOP 0x00000040 ///< (BOOM) Can not be spawned in the Co-op gameMode.
#define MTF_FRIENDLY 0x00000080 ///< (BOOM) friendly monster.
#define MASK_UNKNOWN_THING_FLAGS (0xffffffff \
^ (MTF_EASY|MTF_MEDIUM|MTF_HARD|MTF_DEAF|MTF_NOTSINGLE|MTF_NOTDM|MTF_NOTCOOP|MTF_FRIENDLY))
origin[VZ] = 0;
from >> origin[VX]
>> origin[VY];
from.readAs<dint16>(angle);
angle = (angle / 45) * ANG45;
from >> doomEdNum;
from.readAs<dint16>(flags);
skillModes = 0;
if(flags & MTF_EASY) skillModes |= 0x00000001 | 0x00000002;
if(flags & MTF_MEDIUM) skillModes |= 0x00000004;
if(flags & MTF_HARD) skillModes |= 0x00000008 | 0x00000010;
flags &= ~MASK_UNKNOWN_THING_FLAGS;
// DOOM format things spawn on the floor by default unless their
// type-specific flags override.
flags |= MTF_Z_FLOOR;
#undef MASK_UNKNOWN_THING_FLAGS
#undef MTF_FRIENDLY
#undef MTF_NOTCOOP
#undef MTF_NOTDM
#undef MTF_NOTSINGLE
#undef MTF_AMBUSH
#undef MTF_HARD
#undef MTF_MEDIUM
#undef MTF_EASY
break; }
case Id1MapRecognizer::Doom64Format: {
#define MTF_EASY 0x00000001 ///< Appears in easy skill modes.
#define MTF_MEDIUM 0x00000002 ///< Appears in medium skill modes.
#define MTF_HARD 0x00000004 ///< Appears in hard skill modes.
#define MTF_DEAF 0x00000008 ///< Thing is deaf.
#define MTF_NOTSINGLE 0x00000010 ///< Appears in multiplayer game modes only.
#define MTF_DONTSPAWNATSTART 0x00000020 ///< Do not spawn this thing at map start.
#define MTF_SCRIPT_TOUCH 0x00000040 ///< Mobjs spawned from this spot will envoke a script when touched.
#define MTF_SCRIPT_DEATH 0x00000080 ///< Mobjs spawned from this spot will envoke a script on death.
#define MTF_SECRET 0x00000100 ///< A secret (bonus) item.
#define MTF_NOTARGET 0x00000200 ///< Mobjs spawned from this spot will not target their attacker when hurt.
#define MTF_NOTDM 0x00000400 ///< Can not be spawned in the Deathmatch gameMode.
#define MTF_NOTCOOP 0x00000800 ///< Can not be spawned in the Co-op gameMode.
#define MASK_UNKNOWN_THING_FLAGS (0xffffffff \
^ (MTF_EASY|MTF_MEDIUM|MTF_HARD|MTF_DEAF|MTF_NOTSINGLE|MTF_DONTSPAWNATSTART|MTF_SCRIPT_TOUCH|MTF_SCRIPT_DEATH|MTF_SECRET|MTF_NOTARGET|MTF_NOTDM|MTF_NOTCOOP))
from >> origin[VX]
>> origin[VY]
>> origin[VZ];
from.readAs<dint16>(angle);
angle = (angle / 45) * ANG45;
from >> doomEdNum;
from.readAs<dint32>(flags);
skillModes = 0;
if(flags & MTF_EASY) skillModes |= 0x00000001;
if(flags & MTF_MEDIUM) skillModes |= 0x00000002;
if(flags & MTF_HARD) skillModes |= 0x00000004 | 0x00000008;
flags &= ~MASK_UNKNOWN_THING_FLAGS;
// DOOM64 format things spawn relative to the floor by default
// unless their type-specific flags override.
flags |= MTF_Z_FLOOR;
from >> d64TID;
#undef MASK_UNKNOWN_THING_FLAGS
#undef MTF_NOTCOOP
#undef MTF_NOTDM
#undef MTF_NOTARGET
#undef MTF_SECRET
#undef MTF_SCRIPT_DEATH
#undef MTF_SCRIPT_TOUCH
#undef MTF_DONTSPAWNATSTART
#undef MTF_NOTSINGLE
#undef MTF_DEAF
#undef MTF_HARD
#undef MTF_MEDIUM
#undef MTF_EASY
break; }
case Id1MapRecognizer::HexenFormat: {
#define MTF_EASY 0x00000001
#define MTF_MEDIUM 0x00000002
#define MTF_HARD 0x00000004
#define MTF_AMBUSH 0x00000008
#define MTF_DORMANT 0x00000010
#define MTF_FIGHTER 0x00000020
#define MTF_CLERIC 0x00000040
#define MTF_MAGE 0x00000080
#define MTF_GSINGLE 0x00000100
#define MTF_GCOOP 0x00000200
#define MTF_GDEATHMATCH 0x00000400
// The following are not currently used:
#define MTF_SHADOW 0x00000800 ///< (ZDOOM) Thing is 25% translucent.
#define MTF_INVISIBLE 0x00001000 ///< (ZDOOM) Makes the thing invisible.
#define MTF_FRIENDLY 0x00002000 ///< (ZDOOM) Friendly monster.
#define MTF_STILL 0x00004000 ///< (ZDOOM) Thing stands still (only useful for specific Strife monsters or friendlies).
#define MASK_UNKNOWN_THING_FLAGS (0xffffffff \
^ (MTF_EASY|MTF_MEDIUM|MTF_HARD|MTF_AMBUSH|MTF_DORMANT|MTF_FIGHTER|MTF_CLERIC|MTF_MAGE|MTF_GSINGLE|MTF_GCOOP|MTF_GDEATHMATCH|MTF_SHADOW|MTF_INVISIBLE|MTF_FRIENDLY|MTF_STILL))
from >> xTID
>> origin[VX]
>> origin[VY]
>> origin[VZ];
from.readAs<dint16>(angle);
from >> doomEdNum;
// For some reason, the Hexen format stores polyobject tags in the
// angle field in THINGS. Thus, we cannot translate the angle until
// we know whether it is a polyobject type or not.
if(doomEdNum != PO_ANCHOR_DOOMEDNUM &&
doomEdNum != PO_SPAWN_DOOMEDNUM &&
doomEdNum != PO_SPAWNCRUSH_DOOMEDNUM)
{
angle = ANG45 * (angle / 45);
}
from.readAs<dint16>(flags);
skillModes = 0;
if(flags & MTF_EASY) skillModes |= 0x00000001 | 0x00000002;
if(flags & MTF_MEDIUM) skillModes |= 0x00000004;
if(flags & MTF_HARD) skillModes |= 0x00000008 | 0x00000010;
flags &= ~MASK_UNKNOWN_THING_FLAGS;
// Translate flags:
// Game type logic is inverted.
flags ^= (MTF_GSINGLE|MTF_GCOOP|MTF_GDEATHMATCH);
// HEXEN format things spawn relative to the floor by default
// unless their type-specific flags override.
flags |= MTF_Z_FLOOR;
from >> xSpecial
>> xArgs[0]
>> xArgs[1]
>> xArgs[2]
>> xArgs[3]
>> xArgs[4];
#undef MASK_UNKNOWN_THING_FLAGS
#undef MTF_STILL
#undef MTF_FRIENDLY
#undef MTF_INVISIBLE
#undef MTF_SHADOW
#undef MTF_GDEATHMATCH
#undef MTF_GCOOP
#undef MTF_GSINGLE
#undef MTF_MAGE
#undef MTF_CLERIC
#undef MTF_FIGHTER
#undef MTF_DORMANT
#undef MTF_AMBUSH
#undef MTF_HARD
#undef MTF_NORMAL
#undef MTF_EASY
break; }
default:
DENG2_ASSERT(!"idtech1::Thing::read: unknown map format!");
break;
};
}
};
struct TintColor : public Id1MapElement
{
dint index;
dfloat rgb[3];
dint8 xx[3];
TintColor(MapImporter &map) : Id1MapElement(map) {}
void operator << (de::Reader &from)
{
//Id1Map::Format format = Id1Map::Format(from.version());
from.readAs<dint8>(rgb[0]); rgb[0] /= 255;
from.readAs<dint8>(rgb[1]); rgb[1] /= 255;
from.readAs<dint8>(rgb[2]); rgb[2] /= 255;
from >> xx[0]
>> xx[1]
>> xx[2];
}
};
struct Polyobj
{
typedef QVector<int> LineIndices;
dint index;
LineIndices lineIndices;
dint tag;
dint seqType;
dint16 anchor[2];
};
struct MaterialDict
{
StringPool dict;
String const &find(MaterialId id) const
{
return dict.stringRef(id);
}
MaterialId toMaterialId(String name, MaterialGroup group)
{
// In original DOOM, texture name references beginning with the
// hypen '-' character are always treated as meaning "no reference"
// or "invalid texture" and surfaces using them were not drawn.
if(group != PlaneMaterials && name[0] == '-')
{
return 0; // Not a valid id.
}
// Prepare the encoded URI for insertion into the dictionary.
// Material paths must be encoded.
AutoStr *path = Str_PercentEncode(AutoStr_FromText(name.toUtf8().constData()));
de::Uri uri(Str_Text(path), RC_NULL);
uri.setScheme(group == PlaneMaterials? "Flats" : "Textures");
// Intern this material URI in the dictionary.
return dict.intern(uri.compose());
}
MaterialId toMaterialId(dint uniqueId, MaterialGroup group)
{
// Prepare the encoded URI for insertion into the dictionary.
de::Uri textureUrn(String("urn:%1:%2").arg(group == PlaneMaterials? "Flats" : "Textures").arg(uniqueId), RC_NULL);
uri_s *uri = Materials_ComposeUri(P_ToIndex(DD_MaterialForTextureUri(reinterpret_cast<uri_s *>(&textureUrn))));
String uriComposedAsString = Str_Text(Uri_Compose(uri));
Uri_Delete(uri);
// Intern this material URI in the dictionary.
return dict.intern(uriComposedAsString);
}
};
} // namespace internal
using namespace internal;
static uint validCount = 0; ///< Used with Polyobj LineDef collection.
DENG2_PIMPL(MapImporter)
{
Id1MapRecognizer::Format format;
std::vector<Vertex> vertices;
typedef std::vector<LineDef> Lines;
Lines lines;
typedef std::vector<SideDef> Sides;
Sides sides;
typedef std::vector<SectorDef> Sectors;
Sectors sectors;
typedef std::vector<Thing> Things;
Things things;
typedef std::vector<TintColor> SurfaceTints;
SurfaceTints surfaceTints;
typedef std::list<Polyobj> Polyobjs;
Polyobjs polyobjs;
internal::MaterialDict materials;
Impl(Public *i) : Base(i), format(Id1MapRecognizer::UnknownFormat)
{}
int indexOf(const SectorDef §or) const
{
return int(§or - sectors.data());
}
void readVertexes(de::Reader &from, dint numElements)
{
vertices.resize(size_t(numElements));
Id1MapRecognizer::Format format = Id1MapRecognizer::Format(from.version());
for (auto &vert : vertices)
{
switch (format)
{
case Id1MapRecognizer::Doom64Format: {
// 16:16 fixed-point.
dint32 x, y;
from >> x >> y;
vert.pos.x = FIX2FLT(x);
vert.pos.y = FIX2FLT(y);
break;
}
default: {
dint16 x, y;
from >> x >> y;
vert.pos.x = x;
vert.pos.y = y;
break;
}
}
}
}
void readLineDefs(de::Reader &reader, dint numElements)
{
if(numElements <= 0) return;
lines.reserve(lines.size() + numElements);
for(dint n = 0; n < numElements; ++n)
{
lines.push_back(LineDef(self()));
LineDef &line = lines.back();
line << reader;
line.index = n;
}
}
void readSideDefs(de::Reader &reader, dint numElements)
{
if(numElements <= 0) return;
sides.reserve(sides.size() + numElements);
for(dint n = 0; n < numElements; ++n)
{
sides.push_back(SideDef(self()));
SideDef &side = sides.back();
side << reader;
side.index = n;
}
}
void readSectorDefs(de::Reader &reader, dint numElements)
{
if(numElements <= 0) return;
sectors.reserve(sectors.size() + numElements);
for(dint n = 0; n < numElements; ++n)
{
sectors.push_back(SectorDef(self()));
SectorDef §or = sectors.back();
sector << reader;
sector.index = n;
}
}
void readThings(de::Reader &reader, dint numElements)
{
if(numElements <= 0) return;
things.reserve(things.size() + numElements);
for(dint n = 0; n < numElements; ++n)
{
things.push_back(Thing(self()));
Thing &thing = things.back();
thing << reader;
thing.index = n;
}
}
void readTintColors(de::Reader &reader, dint numElements)
{
if(numElements <= 0) return;
surfaceTints.reserve(surfaceTints.size() + numElements);
for(dint n = 0; n < numElements; ++n)
{
surfaceTints.push_back(TintColor(self()));
TintColor &tint = surfaceTints.back();
tint << reader;
tint.index = n;
}
}
void linkLines()
{
for (int i = 0; i < int(lines.size()); ++i)
{
const auto &line = lines[i];
// Link to vertices.
for (int p = 0; p < 2; ++p)
{
const int vertIndex = line.v[p];
if (vertIndex >= 0 && vertIndex < int(vertices.size()))
{
vertices[vertIndex].lines.insert(i);
}
}
// Link to sectors.
for (auto s : {LineDef::Front, LineDef::Back})
{
if (line.hasSide(s))
{
const auto sec = sides[line.sideIndex(s)].sector;
if (sec >= 0 && sec < int(sectors.size()))
{
sectors[sec].lines.insert(i);
}
}
}
}
}
bool isSelfReferencing(const LineDef &line) const
{
// Use of middle materials indicates that this is not a render hack.
const auto *s = line.sides;
return !(line.aFlags & LAF_POLYOBJ) &&
line.isTwoSided() &&
!sides[s[0]].middleMaterial &&
!sides[s[1]].middleMaterial &&
sides[s[0]].sector == sides[s[1]].sector &&
sides[s[0]].sector >= 0;
}
double lineLength(const LineDef &line) const
{
return (vertices[line.v[0]].pos - vertices[line.v[1]].pos).length();
}
int otherSector(const LineDef &line, int sectorIndex) const
{
DENG2_ASSERT(line.isTwoSided());
if (sides[line.sides[0]].sector == sectorIndex)
{
return sides[line.sides[1]].sector;
}
return sides[line.sides[0]].sector;
}
int sideOfSector(const LineDef &line, int sectorIndex) const
{
for (auto s : {LineDef::Front, LineDef::Back})
{
if (line.sides[s] >= 0)
{
if (sides[line.sides[s]].sector == sectorIndex)
{
return s;
}
}
}
return -1;
}
std::set<int> sectorVertices(const SectorDef §or) const
{
std::set<int> verts;
// If a self-referencing loop has been detected in the sector, we are only interested
// in the loop because it is being used for render hacks.
if (!sector.selfRefLoop.empty())
{
for (int i : sector.selfRefLoop)
{
verts.insert(lines[i].v[0]);
verts.insert(lines[i].v[1]);
}
}
else for (int i : sector.lines)
{
verts.insert(lines[i].v[0]);
verts.insert(lines[i].v[1]);
}
return verts;
}
#if 0
Vec2d sectorBoundsMiddle(const SectorDef §or) const
{
Vec2d mid;
int count = 0;
for (int v : sectorVertices(sector))
{
mid += vertices[v].pos;
++count;
}
if (count > 0) mid /= count;
return mid;
}
#endif
std::vector<double> findSectorIntercepts(const SectorDef §or, const Vec2d &start, const Vec2d &dir) const
{
const Vec2d end = start + dir;
std::vector<double> intercepts;
for (int i : sector.lines)
{
const auto &line = lines[i];
const Vec2d a = vertices[line.v[0]].pos;
const Vec2d b = vertices[line.v[1]].pos;
double t;
if (lineSegmentIntersection(t, start, end, a, b))
{
if (t > 0)
{
intercepts.push_back(t);
}
}
}
return intercepts;
}
/**
* Finds a point that is inside the sector. The first option is to use the
* middle of the sector's bounding box, but if that is outside the sector,
* tries to intersect against the sector lines to find a valid point inside.
*
* @param sector Sector.
*
* @return A point inside the sector.
*/
Vec2d findPointInsideSector(const SectorDef §or) const
{
Vec2d inside;
int count = 0;
for (int i : sectorVertices(sector))
{
inside += vertices[i].pos;
count++;
}
if (count > 0) inside /= count;
// Is this actually inside the sector? Need to do a polygon check.
{
Vec2d dir{1, 0};
std::vector<double> intercepts = findSectorIntercepts(sector, inside, dir);
if (intercepts.empty())
{