This repository was archived by the owner on Oct 20, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathwadprocessor.cpp
More file actions
1279 lines (942 loc) · 35.8 KB
/
Copy pathwadprocessor.cpp
File metadata and controls
1279 lines (942 loc) · 35.8 KB
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
#include "wadprocessor.h"
#include "doomtypes.h"
#include "mus2mid.h"
#include "gusbank.h"
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
WadProcessor::WadProcessor(WadFile& wad, QObject *parent)
: QObject(parent), wadFile(wad)
{
}
bool WadProcessor::ProcessWad()
{
//Figure out if our IWAD is Doom or Doom2. (E1M1 or MAP01)
Lump mapLump;
RemoveOverriddenMaps();
RemoveUnusedLumps();
ProcessTextureColumns();
ProcessSounds();
ProcessMusic();
int lumpNum = wadFile.GetLumpByName("MAP01", mapLump);
if(lumpNum != -1)
{
return ProcessD2Levels();
}
else
{
int lumpNum = wadFile.GetLumpByName("E1M1", mapLump);
//Can't find any maps.
if(lumpNum == -1)
return false;
}
return ProcessD1Levels();
}
bool WadProcessor::ProcessD2Levels()
{
for(int m = 1; m <= 32; m++)
{
Lump l;
QString mapName = QString("MAP%1").arg(m, 2, 10, QChar('0'));
int lumpNum = wadFile.GetLumpByName(mapName, l);
if(lumpNum != -1)
{
ProcessLevel(lumpNum);
}
}
return true;
}
bool WadProcessor::ProcessD1Levels()
{
for(int e = 1; e <= 4; e++)
{
for(int m = 1; m <= 9; m++)
{
Lump l;
QString mapName = QString("E%1M%2").arg(e).arg(m);
int lumpNum = wadFile.GetLumpByName(mapName, l);
if(lumpNum != -1)
{
ProcessLevel(lumpNum);
}
}
}
return true;
}
bool WadProcessor::ProcessLevel(quint32 lumpNum)
{
ProcessVertexes(lumpNum);
ProcessLines(lumpNum);
ProcessSegs(lumpNum);
ProcessSides(lumpNum);
ProcessBlockmap(lumpNum);
ProcessReject(lumpNum);
ProcessSectorLines(lumpNum);
//Lines and segs now hold their own vertex coordinates, so the
//engine doesn't need VERTEXES. Its map lump enum has no
//ML_VERTEXES, so the lumps after it move down one slot.
wadFile.RemoveLump(lumpNum+ML_VERTEXES);
return true;
}
bool WadProcessor::ProcessVertexes(quint32 lumpNum)
{
quint32 vtxLumpNum = lumpNum+ML_VERTEXES;
Lump vxl;
if(!wadFile.GetLumpByNum(vtxLumpNum, vxl))
return false;
if(vxl.length == 0)
return false;
quint32 vtxCount = vxl.length / sizeof(mapvertex_t);
vertex_t* newVtx = new vertex_t[vtxCount];
const mapvertex_t* oldVtx = reinterpret_cast<const mapvertex_t*>(vxl.data.constData());
for(quint32 i = 0; i < vtxCount; i++)
{
newVtx[i].x = (oldVtx[i].x << 16);
newVtx[i].y = (oldVtx[i].y << 16);
}
Lump newVxl;
newVxl.name = vxl.name;
newVxl.length = vtxCount * sizeof(vertex_t);
newVxl.data = QByteArray(reinterpret_cast<const char*>(newVtx), newVxl.length);
delete[] newVtx;
wadFile.ReplaceLump(vtxLumpNum, newVxl);
return true;
}
bool WadProcessor::ProcessLines(quint32 lumpNum)
{
quint32 lineLumpNum = lumpNum+ML_LINEDEFS;
Lump lines;
if(!wadFile.GetLumpByNum(lineLumpNum, lines))
return false;
if(lines.length == 0)
return false;
quint32 lineCount = lines.length / sizeof(maplinedef_t);
line_t* newLines = new line_t[lineCount];
const maplinedef_t* oldLines = reinterpret_cast<const maplinedef_t*>(lines.data.constData());
//We need vertexes for this...
quint32 vtxLumpNum = lumpNum+ML_VERTEXES;
Lump vxl;
if(!wadFile.GetLumpByNum(vtxLumpNum, vxl))
return false;
if(vxl.length == 0)
return false;
const vertex_t* vtx = reinterpret_cast<const vertex_t*>(vxl.data.constData());
Lump mapLump;
wadFile.GetLumpByNum(lumpNum, mapLump);
//lineno and sidenums are 16 bit; 0xffff is NO_INDEX.
if(lineCount >= NO_INDEX)
{
qCritical("%s: %u lines, the engine supports at most %u.", qPrintable(mapLump.name), lineCount, NO_INDEX - 1u);
exit(1);
}
for(unsigned int i = 0; i < lineCount; i++)
{
//vtx is in fixed point (ProcessVertexes), lines keep map units.
const int x1 = vtx[oldLines[i].v1].x >> FRACBITS;
const int y1 = vtx[oldLines[i].v1].y >> FRACBITS;
const int x2 = vtx[oldLines[i].v2].x >> FRACBITS;
const int y2 = vtx[oldLines[i].v2].y >> FRACBITS;
//dx and dy are shorts in the engine.
if(x2 - x1 != qint16(x2 - x1) || y2 - y1 != qint16(y2 - y1))
{
qCritical("%s: line %u is too long (dx %d, dy %d). Lines can be at most 32767 units in x and y.", qPrintable(mapLump.name), i, x2 - x1, y2 - y1);
exit(1);
}
newLines[i].v1.x = x1;
newLines[i].v1.y = y1;
newLines[i].v2.x = x2;
newLines[i].v2.y = y2;
newLines[i].special = oldLines[i].special;
newLines[i].flags = oldLines[i].flags;
newLines[i].tag = oldLines[i].tag;
newLines[i].dx = x2 - x1;
newLines[i].dy = y2 - y1;
newLines[i].sidenum[0] = oldLines[i].sidenum[0];
newLines[i].sidenum[1] = oldLines[i].sidenum[1];
newLines[i].bbox[BOXLEFT] = (x1 < x2 ? x1 : x2);
newLines[i].bbox[BOXRIGHT] = (x1 < x2 ? x2 : x1);
newLines[i].bbox[BOXTOP] = (y1 < y2 ? y2 : y1);
newLines[i].bbox[BOXBOTTOM] = (y1 < y2 ? y1 : y2);
newLines[i].lineno = i;
}
Lump newLine;
newLine.name = lines.name;
newLine.length = lineCount * sizeof(line_t);
newLine.data = QByteArray(reinterpret_cast<const char*>(newLines), newLine.length);
delete[] newLines;
wadFile.ReplaceLump(lineLumpNum, newLine);
return true;
}
bool WadProcessor::ProcessSegs(quint32 lumpNum)
{
quint32 segsLumpNum = lumpNum+ML_SEGS;
Lump segs;
if(!wadFile.GetLumpByNum(segsLumpNum, segs))
return false;
if(segs.length == 0)
return false;
quint32 segCount = segs.length / sizeof(mapseg_t);
seg_t* newSegs = new seg_t[segCount];
const mapseg_t* oldSegs = reinterpret_cast<const mapseg_t*>(segs.data.constData());
//We need vertexes for this...
quint32 vtxLumpNum = lumpNum+ML_VERTEXES;
Lump vxl;
if(!wadFile.GetLumpByNum(vtxLumpNum, vxl))
return false;
if(vxl.length == 0)
return false;
const vertex_t* vtx = reinterpret_cast<const vertex_t*>(vxl.data.constData());
//And LineDefs. Must process lines first.
quint32 linesLumpNum = lumpNum+ML_LINEDEFS;
Lump lxl;
if(!wadFile.GetLumpByNum(linesLumpNum, lxl))
return false;
if(lxl.length == 0)
return false;
const line_t* lines = reinterpret_cast<const line_t*>(lxl.data.constData());
//And sides too...
quint32 sidesLumpNum = lumpNum+ML_SIDEDEFS;
Lump sxl;
if(!wadFile.GetLumpByNum(sidesLumpNum, sxl))
return false;
if(sxl.length == 0)
return false;
const mapsidedef_t* sides = reinterpret_cast<const mapsidedef_t*>(sxl.data.constData());
//****************************
for(unsigned int i = 0; i < segCount; i++)
{
//vtx is in fixed point (ProcessVertexes), segs keep map units.
newSegs[i].v1.x = vtx[oldSegs[i].v1].x >> FRACBITS;
newSegs[i].v1.y = vtx[oldSegs[i].v1].y >> FRACBITS;
newSegs[i].v2.x = vtx[oldSegs[i].v2].x >> FRACBITS;
newSegs[i].v2.y = vtx[oldSegs[i].v2].y >> FRACBITS;
newSegs[i].angle = oldSegs[i].angle;
newSegs[i].offset = oldSegs[i].offset;
newSegs[i].linenum = oldSegs[i].linedef;
const line_t* ldef = &lines[newSegs[i].linenum];
int side = oldSegs[i].side;
newSegs[i].sidenum = ldef->sidenum[side];
if(newSegs[i].sidenum != NO_INDEX)
{
newSegs[i].frontsectornum = sides[newSegs[i].sidenum].sector;
}
else
{
newSegs[i].frontsectornum = NO_INDEX;
}
newSegs[i].backsectornum = NO_INDEX;
if(ldef->flags & ML_TWOSIDED)
{
if(ldef->sidenum[side^1] != NO_INDEX)
{
newSegs[i].backsectornum = sides[ldef->sidenum[side^1]].sector;
}
}
}
Lump newSeg;
newSeg.name = segs.name;
newSeg.length = segCount * sizeof(seg_t);
newSeg.data = QByteArray(reinterpret_cast<const char*>(newSegs), newSeg.length);
delete[] newSegs;
wadFile.ReplaceLump(segsLumpNum, newSeg);
return true;
}
//Rebuilds BLOCKMAP: header (orgx, orgy, width, height), one word offset
//per cell, then the line lists, each ending in 0xffff. The leading 0
//of each list (never used by the engine) is dropped, each distinct
//list is stored once, and a list that is the tail of a longer one
//points into it. Each cell keeps its lines in the original order.
bool WadProcessor::ProcessBlockmap(quint32 lumpNum)
{
quint32 bmLumpNum = lumpNum+ML_BLOCKMAP;
Lump bml;
if(!wadFile.GetLumpByNum(bmLumpNum, bml))
return false;
if(bml.length < 8)
return false;
const quint16* oldBm = reinterpret_cast<const quint16*>(bml.data.constData());
const quint32 oldWords = bml.length / 2;
const quint32 cellCount = oldBm[2] * oldBm[3];
if(4 + cellCount > oldWords)
return false;
std::vector<std::vector<quint16>> cells(cellCount);
for(quint32 c = 0; c < cellCount; c++)
{
quint32 pos = oldBm[4+c];
//Skip the leading 0.
if(pos < oldWords && oldBm[pos] != 0xffff)
pos++;
while(pos < oldWords && oldBm[pos] != 0xffff)
cells[c].push_back(oldBm[pos++]);
}
//Longest lists first, so shorter ones can find themselves as a tail.
std::vector<std::vector<quint16>> lists(cells);
std::sort(lists.begin(), lists.end(), [](const std::vector<quint16>& a, const std::vector<quint16>& b)
{
return a.size() != b.size() ? a.size() > b.size() : a < b;
});
std::vector<quint16> newBm(oldBm, oldBm + 4);
newBm.resize(4 + cellCount);
//Every stored list and each of its tails -> word offset.
std::map<std::vector<quint16>, quint32> placed;
for(const std::vector<quint16>& l : lists)
{
if(placed.count(l))
continue;
const quint32 start = newBm.size();
newBm.insert(newBm.end(), l.begin(), l.end());
newBm.push_back(0xffff);
for(size_t i = 0; i <= l.size(); i++)
placed.emplace(std::vector<quint16>(l.begin() + i, l.end()), start + i);
}
if(newBm.size() > 0x10000)
{
qWarning("%s: BLOCKMAP needs %u words, offsets only reach 65536. Left unchanged.", qPrintable(bml.name), quint32(newBm.size()));
return false;
}
for(quint32 c = 0; c < cellCount; c++)
newBm[4+c] = placed[cells[c]];
Lump newBml;
newBml.name = bml.name;
newBml.length = newBm.size() * sizeof(quint16);
newBml.data = QByteArray(reinterpret_cast<const char*>(newBm.data()), newBml.length);
wadFile.ReplaceLump(bmLumpNum, newBml);
return true;
}
//An all-zero REJECT never rejects anything, so it is emptied (kept as a
//zero-length lump so BLOCKMAP stays at its ML_* offset) and the engine
//skips the check.
bool WadProcessor::ProcessReject(quint32 lumpNum)
{
quint32 rejectLumpNum = lumpNum+ML_REJECT;
Lump rl;
if(!wadFile.GetLumpByNum(rejectLumpNum, rl))
return false;
if(rl.length == 0 || rl.data.count('\0') != rl.data.size())
return true;
Lump emptyRl;
emptyRl.name = rl.name;
emptyRl.length = 0;
wadFile.ReplaceLump(rejectLumpNum, emptyRl);
return true;
}
//Builds SECLINES, inserted after BLOCKMAP: each sector's lines, in the
//order the engine's P_GroupLines used to make them (a line goes to its
//front sector, and to its back sector if that is different).
//Format: u32 start[sectors+1], then u16 line numbers; sector i has
//lines[start[i] .. start[i+1]-1]. Needs processed lines and sides.
bool WadProcessor::ProcessSectorLines(quint32 lumpNum)
{
Lump mapLump, lines, sides, sectors, segs, ssectors;
if(!wadFile.GetLumpByNum(lumpNum, mapLump) || !wadFile.GetLumpByNum(lumpNum+ML_LINEDEFS, lines) ||
!wadFile.GetLumpByNum(lumpNum+ML_SIDEDEFS, sides) || !wadFile.GetLumpByNum(lumpNum+ML_SECTORS, sectors) ||
!wadFile.GetLumpByNum(lumpNum+ML_SEGS, segs) || !wadFile.GetLumpByNum(lumpNum+ML_SSECTORS, ssectors))
return false;
const line_t* lineData = reinterpret_cast<const line_t*>(lines.data.constData());
const sidedef_t* sideData = reinterpret_cast<const sidedef_t*>(sides.data.constData());
const seg_t* segData = reinterpret_cast<const seg_t*>(segs.data.constData());
const quint16* ssData = reinterpret_cast<const quint16*>(ssectors.data.constData()); //numsegs, firstseg
const quint32 lineCount = lines.length / sizeof(line_t);
const quint32 sideCount = sides.length / sizeof(sidedef_t);
const quint32 sectorCount = sectors.length / sizeof(mapsector_t);
const quint32 segCount = segs.length / sizeof(seg_t);
const quint32 ssCount = ssectors.length / (2 * sizeof(quint16));
//The engine takes a subsector's sector from its first seg.
for(quint32 i = 0; i < ssCount; i++)
{
const quint16 firstSeg = ssData[i*2 + 1];
if(firstSeg >= segCount || segData[firstSeg].frontsectornum >= sectorCount)
qWarning("%s: subsector %u is a part of no sector.", qPrintable(mapLump.name), i);
}
std::vector<std::vector<quint16>> secLines(sectorCount);
for(quint32 i = 0; i < lineCount; i++)
{
const quint16 s0 = lineData[i].sidenum[0];
const quint16 s1 = lineData[i].sidenum[1];
if(s0 >= sideCount)
{
qWarning("%s: line %u has no front side.", qPrintable(mapLump.name), i);
continue;
}
const quint16 front = sideData[s0].sector;
secLines[front].push_back(i);
if(s1 < sideCount && quint16(sideData[s1].sector) != front)
secLines[quint16(sideData[s1].sector)].push_back(i);
}
std::vector<quint32> start;
std::vector<quint16> lineNums;
for(const std::vector<quint16>& l : secLines)
{
start.push_back(lineNums.size());
lineNums.insert(lineNums.end(), l.begin(), l.end());
}
start.push_back(lineNums.size());
Lump secLinesLump;
secLinesLump.name = "SECLINES";
secLinesLump.data = QByteArray(reinterpret_cast<const char*>(start.data()), start.size() * sizeof(quint32));
secLinesLump.data.append(reinterpret_cast<const char*>(lineNums.data()), lineNums.size() * sizeof(quint16));
secLinesLump.length = secLinesLump.data.size();
wadFile.InsertLump(lumpNum+ML_SECLINES, secLinesLump);
return true;
}
bool WadProcessor::ProcessSides(quint32 lumpNum)
{
quint32 sidesLumpNum = lumpNum+ML_SIDEDEFS;
Lump sides;
if(!wadFile.GetLumpByNum(sidesLumpNum, sides))
return false;
if(sides.length == 0)
return false;
//Lines and segs are already processed. Their sidenums get remapped.
quint32 linesLumpNum = lumpNum+ML_LINEDEFS;
quint32 segsLumpNum = lumpNum+ML_SEGS;
quint32 sectorsLumpNum = lumpNum+ML_SECTORS;
Lump mapLump, lines, segs, sectors;
if(!wadFile.GetLumpByNum(lumpNum, mapLump) || !wadFile.GetLumpByNum(linesLumpNum, lines) || !wadFile.GetLumpByNum(segsLumpNum, segs) || !wadFile.GetLumpByNum(sectorsLumpNum, sectors))
return false;
quint32 sideCount = sides.length / sizeof(mapsidedef_t);
quint32 lineCount = lines.length / sizeof(line_t);
quint32 segCount = segs.length / sizeof(seg_t);
quint32 sectorCount = sectors.length / sizeof(mapsector_t);
std::vector<sidedef_t> convSides(sideCount);
const mapsidedef_t* oldSides = reinterpret_cast<const mapsidedef_t*>(sides.data.constData());
for(unsigned int i = 0; i < sideCount; i++)
{
convSides[i].textureoffset = oldSides[i].textureoffset;
convSides[i].rowoffset = oldSides[i].rowoffset;
convSides[i].toptexture = GetTextureNumForName(oldSides[i].toptexture);
convSides[i].bottomtexture = GetTextureNumForName(oldSides[i].bottomtexture);
convSides[i].midtexture = GetTextureNumForName(oldSides[i].midtexture);
convSides[i].sector = oldSides[i].sector;
if(quint16(convSides[i].sector) >= sectorCount)
{
qWarning("%s: sidedef %u has out-of-range sector %u. Using sector 0.", qPrintable(mapLump.name), i, quint16(convSides[i].sector));
convSides[i].sector = 0;
}
}
//The engine reads sidedefs from ROM. Side 0 of a line with a special
//can be changed by the game (switch textures, scrolling), so each of
//those keeps its own entry, numbered from 0; the engine copies them
//to RAM. The rest are read only, so identical ones are merged.
std::vector<quint16> remap(sideCount, NO_INDEX);
std::vector<sidedef_t> newSides;
line_t* newLines = reinterpret_cast<line_t*>(lines.data.data());
seg_t* newSegs = reinterpret_cast<seg_t*>(segs.data.data());
for(quint32 i = 0; i < lineCount; i++)
{
const quint16 s = newLines[i].sidenum[0];
if(newLines[i].special && s < sideCount && remap[s] == NO_INDEX)
{
remap[s] = newSides.size();
newSides.push_back(convSides[s]);
}
}
std::map<QByteArray, quint16> merged;
for(quint32 i = 0; i < sideCount; i++)
{
if(remap[i] != NO_INDEX)
continue;
const QByteArray key(reinterpret_cast<const char*>(&convSides[i]), sizeof(sidedef_t));
auto it = merged.find(key);
if(it != merged.end())
{
remap[i] = it->second;
}
else
{
remap[i] = newSides.size();
merged.emplace(key, remap[i]);
newSides.push_back(convSides[i]);
}
}
for(quint32 i = 0; i < lineCount; i++)
{
for(int s = 0; s < 2; s++)
{
if(newLines[i].sidenum[s] < sideCount)
newLines[i].sidenum[s] = remap[newLines[i].sidenum[s]];
}
}
for(quint32 i = 0; i < segCount; i++)
{
if(newSegs[i].sidenum < sideCount)
newSegs[i].sidenum = remap[newSegs[i].sidenum];
}
Lump newSide;
newSide.name = sides.name;
newSide.length = newSides.size() * sizeof(sidedef_t);
newSide.data = QByteArray(reinterpret_cast<const char*>(newSides.data()), newSide.length);
wadFile.ReplaceLump(sidesLumpNum, newSide);
wadFile.ReplaceLump(linesLumpNum, lines);
wadFile.ReplaceLump(segsLumpNum, segs);
return true;
}
int WadProcessor::GetTextureNumForName(const char* tex_name)
{
const int *maptex1, *maptex2;
int numtextures1, numtextures2 = 0;
const int *directory1, *directory2;
//Convert name to uppercase for comparison.
char tex_name_upper[9];
strncpy(tex_name_upper, tex_name, 8);
tex_name_upper[8] = 0; //Ensure null terminated.
for (int i = 0; i < 8; i++)
{
tex_name_upper[i] = toupper(tex_name_upper[i]);
}
Lump tex1lump;
wadFile.GetLumpByName("TEXTURE1", tex1lump);
maptex1 = (const int*)tex1lump.data.constData();
numtextures1 = *maptex1;
directory1 = maptex1+1;
Lump tex2lump;
if (wadFile.GetLumpByName("TEXTURE2", tex2lump) != -1)
{
maptex2 = (const int*)tex2lump.data.constData();
directory2 = maptex2+1;
numtextures2 = *maptex2;
}
else
{
maptex2 = NULL;
directory2 = NULL;
}
const int *directory = directory1;
const int *maptex = maptex1;
int numtextures = (numtextures1 + numtextures2);
for (int i=0 ; i<numtextures; i++, directory++)
{
if (i == numtextures1)
{
// Start looking in second texture file.
maptex = maptex2;
directory = directory2;
}
int offset = *directory;
const maptexture_t* mtexture = (const maptexture_t *) ( (const quint8*)maptex + offset);
if(!strncmp(tex_name_upper, mtexture->name, 8))
{
return i;
}
}
return 0;
}
//Composite every texture into 128 byte columns (the renderer wraps
//texture rows at 128), dedupe them and write:
// TEXCOLS: u32 numtextures, u32 tableofs[numtextures], then for each
// texture u16 colid[width], u16 runid[width]. Offsets are
// from the lump start.
// COLPOOL: the unique columns, 128 bytes each.
// COLRUNS: the unique run lists for masked drawing: (topdelta, length)
// byte pairs of the rows patches cover, ended by (0xff, 0xff).
// A runid is the list's offset / 2.
//Every texture gets a table, in TEXTURE1/TEXTURE2 order, so switches and
//animations (which just swap texture numbers) need nothing special.
//After this the wall patches and PNAMES are not needed and are removed.
bool WadProcessor::ProcessTextureColumns()
{
static const int colHeight = 128;
Lump pnamesLump;
if(wadFile.GetLumpByName("PNAMES", pnamesLump) == -1)
return false;
const char* pnamesData = pnamesLump.data.constData();
const quint32 pnameCount = *((const quint32*)pnamesData);
QVector<QByteArray> patches(pnameCount);
for(quint32 i = 0; i < pnameCount; i++)
{
char n[9] = {0};
strncpy(n, &pnamesData[4 + i*8], 8);
Lump patchLump;
if(wadFile.GetLumpByName(QString(QLatin1String(n)), patchLump) != -1)
patches[i] = patchLump.data;
}
QList<QByteArray> texLumps;
Lump tex1lump;
if(wadFile.GetLumpByName("TEXTURE1", tex1lump) == -1)
return false;
texLumps.append(tex1lump.data);
Lump tex2lump;
if(wadFile.GetLumpByName("TEXTURE2", tex2lump) != -1)
texLumps.append(tex2lump.data);
QByteArray pool;
QHash<QByteArray, quint16> poolIds;
QByteArray runs;
QHash<QByteArray, quint16> runIds;
QVector<quint32> tableOfs;
QByteArray tables;
quint32 totalColumns = 0;
for(const QByteArray& texLump : texLumps)
{
const char* maptex = texLump.constData();
const qint32 numTextures = *((const qint32*)maptex);
const qint32* directory = ((const qint32*)maptex) + 1;
for(qint32 t = 0; t < numTextures; t++)
{
const maptexture_t* mtexture = (const maptexture_t*)(maptex + directory[t]);
const int width = mtexture->width;
const int height = qMin<int>(mtexture->height, colHeight);
if(mtexture->height > colHeight)
qWarning("Texture %.8s is %d high. Rows past %d are dropped.", mtexture->name, mtexture->height, colHeight);
tableOfs.append(tables.size());
QByteArray texRunIds;
for(int x = 0; x < width; x++)
{
QByteArray col(colHeight, 0);
quint8* dest = (quint8*)col.data();
bool covered[colHeight] = {false};
//Same compositing as R_DrawColumnInCache in GBADoom.
for(int p = 0; p < mtexture->patchcount; p++)
{
const mappatch_t* mpatch = &mtexture->patches[p];
if(mpatch->patch < 0 || mpatch->patch >= (int)pnameCount || patches[mpatch->patch].isEmpty())
continue;
const quint8* patch = (const quint8*)patches[mpatch->patch].constData();
const int patchWidth = *((const qint16*)patch);
const int px = x - mpatch->originx;
if(px < 0 || px >= patchWidth)
continue;
const quint8* post = patch + ((const qint32*)(patch + 8))[px];
while(post[0] != 0xff)
{
const quint8* source = post + 3;
int count = post[1];
int position = mpatch->originy + post[0];
if(position < 0)
{
source -= position;
count += position;
position = 0;
}
if(position + count > height)
count = height - position;
if(count > 0)
{
memcpy(dest + position, source, count);
for(int y = position; y < position + count; y++)
covered[y] = true;
}
post += post[1] + 4;
}
}
//Run list of the covered rows, for masked drawing.
QByteArray colRuns;
for(int y = 0; y < height; )
{
if(!covered[y])
{
y++;
continue;
}
int start = y;
while(y < height && covered[y])
y++;
colRuns.append(char(start));
colRuns.append(char(y - start));
}
colRuns.append(char(0xff));
colRuns.append(char(0xff));
quint16 runId;
auto rit = runIds.constFind(colRuns);
if(rit != runIds.constEnd())
{
runId = rit.value();
}
else
{
if((runs.size() / 2) > 0xffff)
{
qCritical("Too many unique texture run lists.");
return false;
}
runId = runs.size() / 2;
runIds.insert(colRuns, runId);
runs.append(colRuns);
}
texRunIds.append((const char*)&runId, sizeof(runId));
//Fill holes with the nearest covered pixel (above on a tie),
//like the pad bytes around patch posts, so the edge rows the
//column drawer can overrun into look right.
for(int y = 0; y < height; y++)
{
if(covered[y])
continue;
for(int d = 1; d < height; d++)
{
if(y - d >= 0 && covered[y - d])
{
dest[y] = dest[y - d];
break;
}
if(y + d < height && covered[y + d])
{
dest[y] = dest[y + d];
break;
}
}
}
//Repeat short textures so they tile.
for(int y = height; height > 0 && y < colHeight; y++)
dest[y] = dest[y % height];
quint16 id;
auto it = poolIds.constFind(col);
if(it != poolIds.constEnd())
{
id = it.value();
}
else
{
if(poolIds.size() > 0xffff)
{
qCritical("Too many unique texture columns.");
return false;
}
id = poolIds.size();
poolIds.insert(col, id);
pool.append(col);
}
tables.append((const char*)&id, sizeof(id));
}
tables.append(texRunIds);
totalColumns += width;
}
}
const quint32 numTextures = tableOfs.size();
const quint32 headerSize = 4 + (numTextures * 4);
QByteArray texCols;
texCols.append((const char*)&numTextures, 4);
for(quint32 i = 0; i < numTextures; i++)
{
quint32 ofs = headerSize + tableOfs[i];
texCols.append((const char*)&ofs, 4);
}
texCols.append(tables);
Lump texColsLump;
texColsLump.name = "TEXCOLS";
texColsLump.length = texCols.size();
texColsLump.data = texCols;
Lump poolLump;
poolLump.name = "COLPOOL";
poolLump.length = pool.size();
poolLump.data = pool;
Lump runsLump;
runsLump.name = "COLRUNS";
runsLump.length = runs.size();
runsLump.data = runs;
wadFile.InsertLump(wadFile.LumpCount(), texColsLump);
wadFile.InsertLump(wadFile.LumpCount(), poolLump);
wadFile.InsertLump(wadFile.LumpCount(), runsLump);
qInfo("Texture columns: %u textures, %u columns, %d unique, COLPOOL %d bytes, TEXCOLS %d bytes, COLRUNS %d bytes.",
numTextures, totalColumns, int(poolIds.size()), int(pool.size()), int(texCols.size()), int(runs.size()));
return RemovePatches();
}
//Remove PNAMES and everything in the patch namespace (P_START..P_END,
//including P1_START etc. inside it). Only textures use them, and those
//now come from COLPOOL.
bool WadProcessor::RemovePatches()
{
static const QRegularExpression startMarker("^P[P0-9]?_START$");
static const QRegularExpression endMarker("^P[P0-9]?_END$");