/
load.cpp
1855 lines (1596 loc) · 58.1 KB
/
load.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 load.cpp
* Load and analyzation of the map data structures.
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2012 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 "wadmapconverter.h"
#include "doomsday.h"
#include <de/c_wrapper.h>
#include "map.h"
#define VERBOSE(code) { if(DENG_PLUGIN_GLOBAL(verbose) >= 1) { code; } }
#define VERBOSE2(code) { if(DENG_PLUGIN_GLOBAL(verbose) >= 2) { code; } }
extern int DENG_PLUGIN_GLOBAL(verbose);
#define map DENG_PLUGIN_GLOBAL(map)
// Size of the map data structures in bytes in the arrived WAD format.
#define SIZEOF_64VERTEX (4 * 2)
#define SIZEOF_VERTEX (2 * 2)
#define SIZEOF_64THING (2 * 7)
#define SIZEOF_XTHING (2 * 7 + 1 * 6)
#define SIZEOF_THING (2 * 5)
#define SIZEOF_XLINEDEF (2 * 5 + 1 * 6)
#define SIZEOF_64LINEDEF (2 * 6 + 1 * 4)
#define SIZEOF_LINEDEF (2 * 7)
#define SIZEOF_64SIDEDEF (2 * 6)
#define SIZEOF_SIDEDEF (2 * 3 + 8 * 3)
#define SIZEOF_64SECTOR (2 * 12)
#define SIZEOF_SECTOR (2 * 5 + 8 * 2)
#define SIZEOF_LIGHT (1 * 6)
#define PO_LINE_START (1) // polyobj line start special
#define PO_LINE_EXPLICIT (5)
#define PO_ANCHOR_DOOMEDNUM (3000)
#define PO_SPAWN_DOOMEDNUM (3001)
#define PO_SPAWNCRUSH_DOOMEDNUM (3002)
#define SEQTYPE_NUMSEQ (10)
static uint PolyLineCount;
static uint validCount = 0; // Used for Polyobj LineDef collection.
static int compareMaterialNames(const void* a, const void* b)
{
materialref_t* refA = *(materialref_t**)a;
materialref_t* refB = *(materialref_t**)b;
return stricmp(refA->name, refB->name);
}
static const materialref_t* getMaterial(const char* regName,
struct materialref_s ***list, size_t size)
{
size_t bottomIdx, topIdx, pivot;
const materialref_t* m;
boolean isDone;
char name[9];
int result;
if(size == 0)
return NULL;
if(map->format == MF_DOOM64)
{
int idx = *((int*) regName);
sprintf(name, "UNK%05i", idx);
name[8] = '\0';
}
else
{
strncpy(name, regName, 8);
name[8] = '\0';
}
bottomIdx = 0;
topIdx = size-1;
m = NULL;
isDone = false;
while(bottomIdx <= topIdx && !isDone)
{
pivot = bottomIdx + (topIdx - bottomIdx)/2;
result = stricmp((*list)[pivot]->name, name);
if(result == 0)
{ // Found.
m = (*list)[pivot];
isDone = true;
}
else
{
if(result > 0)
{
if(pivot == 0)
{ // Not present.
isDone = true;
}
else
{
topIdx = pivot - 1;
}
}
else
{
bottomIdx = pivot + 1;
}
}
}
return m;
}
const materialref_t* GetMaterial(const char* name, boolean isFlat)
{
return getMaterial(name, (isFlat? &map->flats : &map->textures),
isFlat? map->numFlats : map->numTextures);
}
static void addMaterialToList(materialref_t* m, materialref_t*** list, size_t* size)
{
size_t n;
// Enlarge the list.
(*list) = (materialref_t**)realloc((*list), sizeof(m) * ++(*size));
// Find insertion point.
n = 0;
for(size_t i = 0; i < (*size) - 1; ++i)
{
if(compareMaterialNames(&(*list)[i], &m) > 0)
{
n = i;
break;
}
}
// Shift the rest over.
if((*size) > 1)
memmove(&(*list)[n+1], &(*list)[n], sizeof(m) * ((*size)-1-n));
// Insert the new element.
(*list)[n] = m;
}
const materialref_t* RegisterMaterial(const char* name, boolean isFlat)
{
const materialref_t* m;
// Check if this material has already been registered.
if((m = GetMaterial(name, isFlat)))
{
return m; // Already registered.
}
else
{
/**
* A new material.
*/
materialref_t* m = (materialref_t*)malloc(sizeof(*m));
if(map->format == MF_DOOM64)
{
int uniqueId = *((int*) name);
sprintf(m->name, "UNK%05i", uniqueId);
m->name[8] = '\0';
m->id = DD_MaterialForTextureUniqueId((isFlat? TN_FLATS : TN_TEXTURES), uniqueId);
}
else
{
memcpy(m->name, name, 8);
m->name[8] = '\0';
// 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(!isFlat && !stricmp(m->name, "-"))
{
// All we need do is make this a null-reference as the engine will
// determine the best course of action.
m->id = NOMATERIALID;
}
else
{
// First try the prefered namespace, then any.
ddstring_t path; Str_Init(&path);
Str_PercentEncode(Str_Set(&path, m->name));
Uri* uri = Uri_NewWithPath2(Str_Text(&path), RC_NULL);
Uri_SetScheme(uri, isFlat? MN_FLATS_NAME : MN_TEXTURES_NAME);
Str_Free(&path);
m->id = Materials_ResolveUri(uri);
if(m->id == NOMATERIALID)
{
Uri_SetScheme(uri, "");
m->id = Materials_ResolveUri(uri);
}
Uri_Delete(uri);
}
}
// Add it to the list of known materials.
addMaterialToList(m, isFlat? &map->flats : &map->textures,
isFlat? &map->numFlats : &map->numTextures);
return m;
}
}
/**
* Attempts to load the BLOCKMAP data resource.
*
* If the map is too large (would overflow the size limit of
* the BLOCKMAP lump in a WAD therefore it will have been truncated),
* it's zero length or we are forcing a rebuild - we'll have to
* generate the blockmap data ourselves.
*/
#if 0 // Needs updating.
static boolean loadBlockmap(tempmap_t *map, maplumpinfo_t *maplump)
{
#define MAPBLOCKUNITS 128
boolean generateBMap = (createBMap == 2)? true : false;
VERBOSE2( Con_Message("WadMapConverter::loadBlockmap: Processing...\n") )
// Do we have a lump to process?
if(maplump->lumpNum == -1 || maplump->length == 0)
generateBMap = true; // We'll HAVE to generate it.
// Are we generating new blockmap data?
if(generateBMap)
{
// Only announce if the user has choosen to always generate
// new data (we will have already announced it if the lump
// was missing).
if(maplump->lumpNum != -1)
VERBOSE( Con_Message("loadBlockMap: Generating NEW blockmap...\n") )
}
else
{ // No, the existing data is valid - so load it in.
uint startTime;
blockmap_t *blockmap;
uint x, y, width, height;
float v[2];
vec2f_t bounds[2];
long *lineListOffsets, i, n, numBlocks, blockIdx;
short *blockmapLump;
VERBOSE2(
Con_Message("loadBlockMap: Converting existing blockmap...\n"));
startTime = Sys_GetRealTime();
blockmapLump =
(short *) W_CacheLump(maplump->lumpNum, PU_GAMESTATIC);
v[VX] = (float) SHORT(blockmapLump[0]);
v[VY] = (float) SHORT(blockmapLump[1]);
width = ((SHORT(blockmapLump[2])) & 0xffff);
height = ((SHORT(blockmapLump[3])) & 0xffff);
numBlocks = (long) width * (long) height;
/**
* Expand WAD blockmap into a larger one, by treating all
* offsets except -1 as unsigned and zero-extending them.
* This potentially doubles the size of blockmaps allowed
* because DOOM originally considered the offsets as always
* signed.
*/
lineListOffsets = M_Malloc(sizeof(long) * numBlocks);
n = 4;
for(i = 0; i < numBlocks; ++i)
{
short t = SHORT(blockmapLump[n++]);
lineListOffsets[i] = (t == -1? -1 : (long) t & 0xffff);
}
/**
* Finally, convert the blockmap into our internal representation.
* We'll ensure the blockmap is formed correctly as we go.
*
* \todo We could gracefully handle malformed blockmaps by
* by cleaning up and then generating our own.
*/
V2f_Set(bounds[0], v[VX], v[VY]);
v[VX] += (float) (width * MAPBLOCKUNITS);
v[VY] += (float) (height * MAPBLOCKUNITS);
V2f_Set(bounds[1], v[VX], v[VY]);
blockmap = P_BlockmapCreate(bounds[0], bounds[1],
width, height);
blockIdx = 0;
for(y = 0; y < height; ++y)
for(x = 0; x < width; ++x)
{
long offset = lineListOffsets[blockIdx];
long idx;
uint count;
#if _DEBUG
if(SHORT(blockmapLump[offset]) != 0)
{
Con_Error("loadBlockMap: Offset (%li) for block %u [%u, %u] "
"does not index the beginning of a line list!\n",
offset, blockIdx, x, y);
}
#endif
// Count the number of lines in this block.
count = 0;
while((idx = SHORT(blockmapLump[offset + 1 + count])) != -1)
count++;
if(count > 0)
{
linedef_t **lines, **ptr;
// A NULL-terminated array of pointers to lines.
lines = Z_Malloc((count + 1) * sizeof(linedef_t *),
PU_MAPSTATIC, NULL);
// Copy pointers to the array, delete the nodes.
ptr = lines;
count = 0;
while((idx = SHORT(blockmapLump[offset + 1 + count])) != -1)
{
#if _DEBUG
if(idx < 0 || idx >= (long) map->numLines)
{
Con_Error("loadBlockMap: Invalid linedef id %li\n!", idx);
}
#endif
*ptr++ = &map->lines[idx];
count++;
}
// Terminate.
*ptr = NULL;
// Link it into the BlockMap.
P_BlockmapSetBlock(blockmap, x, y, lines, NULL);
}
blockIdx++;
}
// Don't need this anymore.
M_Free(lineListOffsets);
map->blockMap = blockmap;
// How much time did we spend?
VERBOSE2(Con_Message
("loadBlockMap: Done in %.2f seconds.\n",
(Sys_GetRealTime() - startTime) / 1000.0f));
}
return true;
#undef MAPBLOCKUNITS
}
#endif
#if 0
/**
* The REJECT resource is a LUT that provides the results of trivial
* line-of-sight tests between sectors. This is done with a matrix of sector
* pairs i.e. if a monster in sector 4 can see the player in sector 2; the
* inverse should be true.
*
* Note however, some PWADS have carefully constructed REJECT data to create
* special effects. For example it is possible to make a player completely
* invissible in certain sectors.
*
* The format of the table is a simple matrix of boolean values, a (true)
* value indicates that it is impossible for mobjs in sector A to see mobjs
* in sector B (and vice-versa). A (false) value indicates that a
* line-of-sight MIGHT be possible and a more accurate (thus more expensive)
* calculation will have to be made.
*
* The table itself is constructed as follows:
*
* X = sector num player is in
* Y = sector num monster is in
*
* X
*
* 0 1 2 3 4 ->
* 0 1 - 1 - -
* Y 1 - - 1 - -
* 2 1 1 - - 1
* 3 - - - 1 -
* \|/
*
* These results are read left-to-right, top-to-bottom and are packed into
* bytes (each byte represents eight results). As are all lumps in WAD the
* data is in little-endian order.
*
* Thus the size of a valid REJECT lump can be calculated as:
*
* ceiling(numSectors^2)
*
* For now we only do very basic reject processing, limited to determining
* all isolated sector groups (islands that are surrounded by void space).
*
* \note Algorithm:
* Initially all sectors are in individual groups. Next, we scan the linedef
* list. For each 2-sectored line, merge the two sector groups into one.
*/
static void buildReject(gamemap_t *map)
{
/**
* \todo We can do something much better now that we are building the BSP.
*/
int i;
int group;
int *secGroups;
int view, target;
size_t rejectSize;
byte *matrix;
secGroups = M_Malloc(sizeof(int) * numSectors);
for(i = 0; i < numSectors; ++i)
{
sector_t *sec = LookupSector(i);
secGroups[i] = group++;
sec->rejNext = sec->rejPrev = sec;
}
for(i = 0; i < numLinedefs; ++i)
{
linedef_t *line = LookupLinedef(i);
sector_t *sec1, *sec2, *p;
if(!line->sideDefs[FRONT] || !line->sideDefs[BACK])
continue;
sec1 = line->sideDefs[FRONT]->sector;
sec2 = line->sideDefs[BACK]->sector;
if(!sec1 || !sec2 || sec1 == sec2)
continue;
// Already in the same group?
if(secGroups[sec1->index] == secGroups[sec2->index])
continue;
// Swap sectors so that the smallest group is added to the biggest
// group. This is based on the assumption that sector numbers in
// wads will generally increase over the set of linedefs, and so
// (by swapping) we'll tend to add small groups into larger
// groups, thereby minimising the updates to 'rej_group' fields
// that is required when merging.
if(secGroups[sec1->index] > secGroups[sec2->index])
{
p = sec1;
sec1 = sec2;
sec2 = p;
}
// Update the group numbers in the second group
secGroups[sec2->index] = secGroups[sec1->index];
for(p = sec2->rejNext; p != sec2; p = p->rejNext)
secGroups[p->index] = secGroups[sec1->index];
// Merge 'em baby...
sec1->rejNext->rejPrev = sec2;
sec2->rejNext->rejPrev = sec1;
p = sec1->rejNext;
sec1->rejNext = sec2->rejNext;
sec2->rejNext = p;
}
rejectSize = (numSectors * numSectors + 7) / 8;
matrix = Z_Calloc(rejectSize, PU_MAPSTATIC, 0);
for(view = 0; view < numSectors; ++view)
for(target = 0; target < view; ++target)
{
int p1, p2;
if(secGroups[view] == secGroups[target])
continue;
// For symmetry, do two bits at a time.
p1 = view * numSectors + target;
p2 = target * numSectors + view;
matrix[p1 >> 3] |= (1 << (p1 & 7));
matrix[p2 >> 3] |= (1 << (p2 & 7));
}
M_Free(secGroups);
}
#endif
/**
* Create a temporary polyobj (read from the original map data).
*/
static boolean createPolyobj(mline_t** lineList, uint num, uint* poIdx,
int tag, int sequenceType, int16_t anchorX, int16_t anchorY)
{
if(!lineList || num == 0)
return false;
// Allocate the new polyobj.
mpolyobj_t* po = (mpolyobj_t*)calloc(1, sizeof(*po));
/**
* Link the new polyobj into the global list.
*/
mpolyobj_t** newList = (mpolyobj_t**)malloc(((++map->numPolyobjs) + 1) * sizeof(mpolyobj_t*));
// Copy the existing list.
uint n = 0;
for(uint i = 0; i < map->numPolyobjs - 1; ++i, n++)
{
newList[i] = map->polyobjs[i];
}
newList[n++] = po; // Add the new polyobj.
newList[n] = NULL; // Terminate.
if(map->numPolyobjs-1 > 0)
free(map->polyobjs);
map->polyobjs = newList;
po->idx = map->numPolyobjs-1;
po->tag = tag;
po->seqType = sequenceType;
po->anchor[VX] = anchorX;
po->anchor[VY] = anchorY;
po->lineCount = num;
po->lineIndices = (uint*)malloc(sizeof(uint) * num);
for(uint i = 0; i < num; ++i)
{
mline_t* line = lineList[i];
line->aFlags |= LAF_POLYOBJ;
/**
* Due a logic error in hexen.exe, when the column drawer is
* presented with polyobj segs built from two-sided linedefs;
* clipping is always calculated using the pegging logic for
* single-sided linedefs.
*
* Here we emulate this behavior by automatically applying
* bottom unpegging for two-sided linedefs.
*/
if(line->sides[LEFT] != 0)
line->ddFlags |= DDLF_DONTPEGBOTTOM;
po->lineIndices[i] = line - map->lines;
}
if(poIdx)
*poIdx = po->idx;
return true; // Success!
}
/**
* @param lineList @c NULL, will cause IterFindPolyLines to count
* the number of lines in the polyobj.
*/
static void iterFindPolyLines(coord_t x, coord_t y, mline_t** lineList)
{
uint i;
for(i = 0; i < map->numLines; ++i)
{
mline_t* line = &map->lines[i];
coord_t v1[2], v2[2];
if(line->aFlags & LAF_POLYOBJ) continue;
if(line->validCount == validCount) continue;
v1[VX] = map->vertexes[(line->v[0] - 1) * 2];
v1[VY] = map->vertexes[(line->v[0] - 1) * 2 + 1];
v2[VX] = map->vertexes[(line->v[1] - 1) * 2];
v2[VY] = map->vertexes[(line->v[1] - 1) * 2 + 1];
if(FEQUAL(v1[VX], x) && FEQUAL(v1[VY], y))
{
line->validCount = validCount;
if(!lineList)
PolyLineCount++;
else
*lineList++ = line;
iterFindPolyLines(v2[VX], v2[VY], lineList);
}
}
}
/**
* @todo This terribly inefficent (naive) algorithm may need replacing
* (it is far outside an exceptable polynominal range!).
*/
static mline_t** collectPolyobjLineDefs(mline_t* lineDef, uint* num)
{
mline_t** lineList;
coord_t v1[2], v2[2];
lineDef->xType = 0;
lineDef->xArgs[0] = 0;
v1[VX] = map->vertexes[(lineDef->v[0]-1) * 2];
v1[VY] = map->vertexes[(lineDef->v[0]-1) * 2 + 1];
v2[VX] = map->vertexes[(lineDef->v[1]-1) * 2];
v2[VY] = map->vertexes[(lineDef->v[1]-1) * 2 + 1];
PolyLineCount = 1;
validCount++;
lineDef->validCount = validCount;
iterFindPolyLines(v2[VX], v2[VY], NULL);
lineList = (mline_t**)malloc((PolyLineCount+1) * sizeof(mline_t*));
lineList[0] = lineDef; // Insert the first line.
validCount++;
lineDef->validCount = validCount;
iterFindPolyLines(v2[VX], v2[VY], lineList + 1);
lineList[PolyLineCount] = 0; // Terminate.
*num = PolyLineCount;
return lineList;
}
/**
* Find all linedefs marked as belonging to a polyobject with the given tag
* and attempt to create a polyobject from them.
*
* @param tag Line tag of linedefs to search for.
*
* @return @c true = successfully created polyobj.
*/
static boolean findAndCreatePolyobj(int16_t tag, int16_t anchorX, int16_t anchorY)
{
#define MAXPOLYLINES 32
uint i;
for(i = 0; i < map->numLines; ++i)
{
mline_t* line = &map->lines[i];
if(line->aFlags & LAF_POLYOBJ) continue;
if(!(line->xType == PO_LINE_START && line->xArgs[0] == tag)) continue;
uint num;
mline_t** lineList = collectPolyobjLineDefs(line, &num);
if(lineList)
{
uint poIdx;
byte seqType;
boolean result;
seqType = line->xArgs[2];
if(seqType >= SEQTYPE_NUMSEQ)
seqType = 0;
result = createPolyobj(lineList, num, &poIdx, tag, seqType, anchorX, anchorY);
free(lineList);
if(result) return true;
}
}
/**
* Didn't find a polyobj through PO_LINE_START.
* We'll try another approach...
*/
mline_t* polyLineList[MAXPOLYLINES];
uint lineCount = 0;
uint j, psIndex, psIndexOld;
psIndex = 0;
for(j = 1; j < MAXPOLYLINES; ++j)
{
psIndexOld = psIndex;
for(i = 0; i < map->numLines; ++i)
{
mline_t* line = &map->lines[i];
if(line->aFlags & LAF_POLYOBJ) continue;
if(line->xType == PO_LINE_EXPLICIT &&
line->xArgs[0] == tag)
{
if(!line->xArgs[1])
{
Con_Error("WadMapConverter::findAndCreatePolyobj: Explicit line missing order number "
"(probably %d) in poly %d.\n", j + 1, tag);
}
if(line->xArgs[1] == j)
{
// Add this line to the list.
polyLineList[psIndex] = line;
lineCount++;
psIndex++;
if(psIndex > MAXPOLYLINES)
{
Con_Error("WadMapConverter::findAndCreatePolyobj: psIndex > MAXPOLYLINES\n");
}
// Clear out any special.
line->xType = 0;
line->xArgs[0] = 0;
line->aFlags |= LAF_POLYOBJ;
}
}
}
if(psIndex == psIndexOld)
{
// Check if an explicit line order has been skipped
// A line has been skipped if there are any more explicit
// lines with the current tag value
for(i = 0; i < map->numLines; ++i)
{
mline_t* line = &map->lines[i];
if(line->xType == PO_LINE_EXPLICIT && line->xArgs[0] == tag)
{
Con_Error("WadMapConverter::findAndCreatePolyobj: Missing explicit line %d for poly %d\n",
j, tag);
}
}
}
}
if(lineCount)
{
const int seqType = polyLineList[0]->xArgs[3];
uint poIdx;
if(createPolyobj(polyLineList, lineCount, &poIdx, tag,
seqType, anchorX, anchorY))
{
mline_t* line = polyLineList[0];
// Next, change the polyobjs first line to point to a mirror
// if it exists.
line->xArgs[1] = line->xArgs[2];
return true;
}
}
return false;
#undef MAXPOLYLINES
}
static void findPolyobjs(void)
{
VERBOSE2( Con_Message("WadMapConverter::findPolyobjs: Processing...\n") )
for(uint i = 0; i < map->numThings; ++i)
{
mthing_t* thing = &map->things[i];
if(thing->doomEdNum == PO_ANCHOR_DOOMEDNUM)
{
// A polyobj anchor.
const int tag = thing->angle;
findAndCreatePolyobj(tag, thing->origin[VX], thing->origin[VY]);
}
}
}
void AnalyzeMap(void)
{
if(map->format == MF_HEXEN)
{
findPolyobjs();
}
}
int IsSupportedFormat(maplumpinfo_t* lumpInfos[NUM_LUMP_TYPES])
{
DENG_ASSERT(lumpInfos);
bool recognised = false;
// Assume DOOM format by default.
map->format = MF_DOOM;
// Check for format specific lumps.
for(uint i = 0; i < (uint)NUM_LUMP_TYPES; ++i)
{
const maplumpinfo_t* info = lumpInfos[i];
if(!info) continue;
switch(info->lumpType)
{
case ML_BEHAVIOR: map->format = MF_HEXEN; break;
case ML_MACROS:
case ML_LIGHTS:
case ML_LEAFS: map->format = MF_DOOM64; break;
default: break;
}
}
for(uint i = 0; i < (uint)NUM_LUMP_TYPES; ++i)
{
const maplumpinfo_t* info = lumpInfos[i];
if(!info) continue;
// Determine the number of map data objects of each data type.
uint* elmCountAddr = NULL;
size_t elmSize = 0; // Num of bytes.
switch(info->lumpType)
{
case ML_VERTEXES:
elmCountAddr = &map->numVertexes;
elmSize = (map->format == MF_DOOM64? SIZEOF_64VERTEX : SIZEOF_VERTEX);
break;
case ML_THINGS:
elmCountAddr = &map->numThings;
elmSize = (map->format == MF_DOOM64? SIZEOF_64THING : map->format == MF_HEXEN? SIZEOF_XTHING : SIZEOF_THING);
break;
case ML_LINEDEFS:
elmCountAddr = &map->numLines;
elmSize = (map->format == MF_DOOM64? SIZEOF_64LINEDEF : map->format == MF_HEXEN? SIZEOF_XLINEDEF : SIZEOF_LINEDEF);
break;
case ML_SIDEDEFS:
elmCountAddr = &map->numSides;
elmSize = (map->format == MF_DOOM64? SIZEOF_64SIDEDEF : SIZEOF_SIDEDEF);
break;
case ML_SECTORS:
elmCountAddr = &map->numSectors;
elmSize = (map->format == MF_DOOM64? SIZEOF_64SECTOR : SIZEOF_SECTOR);
break;
case ML_LIGHTS:
elmCountAddr = &map->numLights;
elmSize = SIZEOF_LIGHT;
break;
default: break;
}
if(elmCountAddr)
{
if(0 != info->length % elmSize)
{
return false; // What is this??
}
*elmCountAddr += info->length / elmSize;
}
}
if(map->numVertexes > 0 && map->numLines > 0 && map->numSides > 0 && map->numSectors > 0)
{
recognised = true;
}
return (int)recognised;
}
static void freeMapData(void)
{
if(map->vertexes)
{
free(map->vertexes);
map->vertexes = NULL;
}
if(map->lines)
{
free(map->lines);
map->lines = NULL;
}
if(map->sides)
{
free(map->sides);
map->sides = NULL;
}
if(map->sectors)
{
free(map->sectors);
map->sectors = NULL;
}
if(map->things)
{
free(map->things);
map->things = NULL;
}
if(map->polyobjs)
{
for(uint i = 0; i < map->numPolyobjs; ++i)
{
mpolyobj_t* po = map->polyobjs[i];
free(po->lineIndices);
free(po);
}
free(map->polyobjs);
map->polyobjs = NULL;
}
if(map->lights)
{
free(map->lights);
map->lights = NULL;
}
/*if(map->macros)
{
free(map->macros);
map->macros = NULL;
}*/
if(map->textures)
{
for(size_t i = 0; i < map->numTextures; ++i)
{
materialref_t* m = map->textures[i];
free(m);
}
free(map->textures);
map->textures = NULL;
}
if(map->flats)
{
for(size_t i = 0; i < map->numFlats; ++i)
{
materialref_t* m = map->flats[i];
free(m);
}
free(map->flats);
map->flats = NULL;
}
}
static boolean loadVertexes(const uint8_t* buf, size_t len)
{
VERBOSE2( Con_Message("WadMapConverter::loadVertexes: Processing...\n") )
size_t elmSize = (map->format == MF_DOOM64? SIZEOF_64VERTEX : SIZEOF_VERTEX);
uint num = len / elmSize;
switch(map->format)
{
default:
case MF_DOOM: {
const uint8_t* ptr = buf;
for(uint n = 0; n < num; ++n)
{
map->vertexes[n * 2] = (coord_t)SHORT(*((const int16_t*) (ptr)));
map->vertexes[n * 2 + 1] = (coord_t)SHORT(*((const int16_t*) (ptr+2)));
ptr += elmSize;
}
break; }
case MF_DOOM64: {
const uint8_t* ptr = buf;
for(uint n = 0; n < num; ++n)
{
map->vertexes[n * 2] = (coord_t)FIX2FLT(LONG(*((const int32_t*) (ptr))));
map->vertexes[n * 2 + 1] = (coord_t)FIX2FLT(LONG(*((const int32_t*) (ptr+4))));
ptr += elmSize;
}
break; }
}
return true;
}
/**
* Interpret linedef flags.
*/