-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit2.pas
1222 lines (1157 loc) · 41 KB
/
Unit2.pas
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
unit Unit2;
interface
uses
unit3, // TTRProject type
Vcl.Graphics, // TBitmap
System.Generics.Collections, // TList<...>
Vcl.Samples.Gauges; // TGauge
type
TFloorType = (Floor,door,tilt,roof,trigger,lava,climb,split1,split2,split3,split4,
nocol1,nocol2,nocol3,nocol4,nocol5,nocol6,nocol7,nocol8,monkey,trigtrig,beetle);
type
TParsedFloorData = record
tipo : TFloorType;
case TFloorType of
Floor,trigger,lava,monkey,trigtrig,beetle:();
door:(toRoom:UInt16);
tilt,roof:(addX,addZ:int8);
climb:(n,s,e,w:Boolean);
split1,split2,split3,split4,nocol1,nocol2,nocol3,nocol4,
nocol5,nocol6,nocol7,nocol8:(triHLO,triHHi:integer;corners:array[0..3] of UInt16);
end;
type
TSector = packed record
FDindex,BoxIndex :UInt16;
RoomBelow :UInt8;
Floor :Int8;
RoomAbove:UInt8;
Ceiling:int8;
floorInfo : TList<TParsedFloorData>;
hasFD : Boolean;
end;
type
TBox = packed record
xmin,xmax,zmin,zmax :UInt8;
truefloor,overlapindex : Int16;
end;
type
TVertex = packed record
x,y,z : Int16;
end;
type
TPortal = packed record
toRoom : UInt16;
normal : TVertex;
vertices : array[0..3] of TVertex;
end;
type
TPortalArray = array of TPortal;
type
TPortalHelper = record helper for TPortal
function Adjoins(other:TPortal; x,z,x1,z1:Int32):Boolean;
function Equals(other:TPortal):Boolean;
end;
type
TRoomColour = packed record
b,g,r,a : UInt8;
end;
type
TRoom = record
x,z,yBottom,yTop : Int32;
numZ,numX : UInt16;
Sectors:array of TSector;
num_portals : UInt16;
Portals: TPortalArray; //array of TPortal;
colour : TRoomColour;
altroom : Int16;
flags:UInt16;
waterscheme,reverb,altgroup:UInt8;
isFlipRoom : Boolean; // extra fields for convenience
original_room : Int16;
end;
type
TTRLevel = class
private
procedure parseFloorData(FDindex:UInt16;out list:TList<TParsedFloorData>);
public
bmp : TBitmap;
file_version: uint32;
num_room_textiles, num_obj_textiles, num_bump_textiles: uint16;
num_sounds: uint32;
num_rooms: uint16;
num_animations, num_state_changes, num_anim_dispatches, num_anim_commands,
num_meshtrees, size_keyframes, num_moveables, num_statics: uint32;
num_floordata : UInt32;
num_boxes : UInt32;
floordata : array of UInt16;
rooms : array of TRoom;
boxes : array of TBox;
constructor Create;
destructor Destroy;override;
function Load(filename: string;out gauge:TGauge): uint8;
function ConvertToPRJ(const filename:string;SaveTGA:Boolean=True;fixFDIVS:Boolean=True): TTRProject;
procedure MakeDoors(var p:TTRProject; tr2prjlinks:Boolean);
end;
implementation
uses
System.SysUtils, Vcl.Dialogs, System.UITypes,
System.Classes, //TMemoryStream, TBinaryReader
System.ZLib, // ZDecompressStream() - decompress TR4 data
Imaging, ImagingTypes, ImagingComponents, // Vampyre Imaging Library for .TGA support
System.Math; // MaxIntValue()
procedure TTRLevel.parseFloorData(FDindex:UInt16;out list:TList<TParsedFloorData>);
procedure parseFloorType(arg:UInt16;out f,sub,e:uint16);
begin
f:=(arg and $001f);
sub:=(arg and $7f00) shr 8;
e:=(arg and $8000) shr 15;
end;
var
fd:TParsedFloorData;
data : UInt16;
f,sub,e :UInt16;
k:integer;
begin
if FDindex=0 then
begin
fd.tipo:=Floor;
list.Add(fd);
Exit;
end;
e := 0;
k :=0;
while (e=0) and (FDIndex+k < num_floordata) do
begin
data:= floordata[FDindex+k];
Inc(k);
parseFloorType(data,f,sub,e);
fd.tipo:=TFloorType(f);
if fd.tipo = door then
begin
fd.toRoom:=floordata[FDindex+k];
Inc(k);
end
else if (fd.tipo = tilt) or (fd.tipo=roof) then
begin
fd.addX:= int8((floordata[FDindex+k] and $ff00) shr 8);
fd.addZ:= int8(floordata[FDindex+k] and $00ff);
Inc(k);
end
else if fd.tipo = trigger then
begin
repeat //skip trigger floordata
data := floordata[FDIndex+k];
Inc(k);
until ((data and $8000) = $8000);
end
else if fd.tipo = climb then
begin
if (sub and $0001) = $0001 then fd.e :=True else fd.e :=False;
if (sub and $0002) = $0002 then fd.s :=True else fd.s :=False;
if (sub and $0004) = $0004 then fd.w :=True else fd.w :=False;
if (sub and $0008) = $0008 then fd.n :=True else fd.n :=False;
end
else if fd.tipo in [split1..nocol8] then
begin
fd.triHLo := (data and $03e0) shr 5;
fd.triHHi := (data and $7c00) shr 10;
data := floordata[FDindex+k];
Inc(k);
fd.corners[0]:=data and $000f;
fd.corners[1]:=(data and $00f0) shr 4;
fd.corners[2]:=(data and $0f00) shr 8;
fd.corners[3]:=(data and $f000) shr 12;
end;
list.Add(fd);
end;
end;
constructor TTRLevel.Create;
begin
inherited;
file_version := 0;
end;
destructor TTRLevel.Destroy;
var
i,j : Integer;
begin
bmp.Free;
for i:=0 to High(rooms) do
begin
for j:=0 to High(rooms[i].Sectors) do
begin
if Assigned(rooms[i].Sectors[j].floorInfo) then
rooms[i].Sectors[j].floorInfo.Free;
end;
end;
inherited;
end;
function TTRLevel.Load(filename: string; out gauge:TGauge): uint8;
const
spr = 'spr';
tex = 'tex';
var
version: uint32;
resultado: uint8;
f: file;
memfile: TMemoryStream;
br, br2,br3: TBinaryReader;
size, size2, size3: uint32;
geometry1: TMemoryStream;
geometry: TMemoryStream;
tex32 : TMemoryStream;
i, j: integer;
s: string;
r:TRoom;
b:TBitmap;
col :TColor;
rd,gr,bl,al:UInt8;
sectorFD:TList<TParsedFloorData>;
file_size : Int64;
height : Integer;
begin
resultado := 0;
gauge.Progress:=0;
if FileExists(filename) then
begin
FileMode := 0;
assignfile(f, filename);
Reset(f, 1);
BlockRead(f, version, 4);
CloseFile(f);
if (version <> $00345254) and (version <> $63345254) then
begin
version := 0;
resultado := 2;
end;
end
else
begin
version := 0;
resultado := 1;
end;
if version = $63345254 then
begin
resultado := 3;
version := 0;
end;
gauge.Progress:=1;
if resultado <> 0 then
begin
Result := resultado;
Exit;
end;
memfile := TMemoryStream.Create;
try
memfile.LoadFromFile(filename);
file_size := memfile.Size;
br := TBinaryReader.Create(memfile);
try
file_version := br.ReadUInt32;
num_room_textiles := br.ReadUInt16;
num_obj_textiles := br.ReadUInt16;
num_bump_textiles := br.ReadUInt16;
memfile.seek(4, soCurrent);
size := br.ReadUInt32;
geometry1 := TMemoryStream.Create;
geometry1.CopyFrom(memfile,size);
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
geometry1.Position:=0;
tex32:=TMemoryStream.Create;
ZDecompressStream(geometry1,tex32);
tex32.Position:=0;
geometry1.Free;
if Assigned(bmp) then bmp.Free;
b:=TBitmap.Create;
b.PixelFormat:=pf24bit;
b.Width:=256;
b.Height:=num_room_textiles * 256; //tex32.Size div 256 div 4;
br3:=TBinaryReader.Create(tex32);
for i:=0 to b.Height-1 do
begin
if i mod (256*2) = 0 then gauge.AddProgress(1);
for j := 0 to 255 do
begin
bl:=br3.ReadByte;
gr:=br3.ReadByte;
rd:=br3.ReadByte;
al:=br3.ReadByte;
if al=0 then // make alpha pixels magenta
begin
bl:=255;
rd:=255;
gr:=0;
end;
col := 0;
col := col or (bl shl 16);
col := col or (gr shl 8);
col := col or rd;
b.Canvas.Pixels[j,i]:= col;
end;
end;
if num_bump_textiles > 0 then
begin
height := b.Height;
b.Height:= b.Height + ((num_bump_textiles div 2) * 256);
tex32.Seek(num_obj_textiles*256*256*4, soCurrent);
for i:=height to b.Height-1 do
begin
if i mod (256*2) = 0 then gauge.AddProgress(1);
for j := 0 to 255 do
begin
bl:=br3.ReadByte;
gr:=br3.ReadByte;
rd:=br3.ReadByte;
al:=br3.ReadByte;
if al=0 then // make alpha pixels magenta
begin
bl:=255;
rd:=255;
gr:=0;
end;
col := 0;
col := col or (bl shl 16);
col := col or (gr shl 8);
col := col or rd;
b.Canvas.Pixels[j,i]:= col;
end;
end;
end;
br3.Free;
tex32.Free;
bmp:=b;
memfile.Seek(4, soCurrent);
size := br.ReadUInt32;
memfile.Seek(size, soCurrent); //skip the textiles16
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
memfile.Seek(4, soCurrent);
size := br.ReadUInt32;
memfile.Seek(size, soCurrent); //skip the font and sky
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
size2 := br.ReadUInt32;
size := br.ReadUInt32;
geometry1 := TMemoryStream.Create;
geometry1.CopyFrom(memfile, size);
geometry1.Position := 0;
geometry := TMemoryStream.Create;
ZDecompressStream(geometry1, geometry);
geometry.Position := 0;
geometry1.Free;
num_sounds := br.ReadUInt32;
br2 := TBinaryReader.Create(geometry);
geometry.Seek(4, soCurrent);
num_rooms := br2.ReadUInt16;
SetLength(rooms,num_rooms);
for i := 0 to High(rooms) do
begin
gauge.AddProgress(1);
r.z := br2.ReadInt32;
r.x := br2.ReadInt32;
r.yBottom := br2.ReadInt32;
r.yTop := br2.ReadInt32;
size := br2.ReadUInt32; //num room mesh data words
geometry.Seek(size * 2, soCurrent); //skip verts, quads, tris, sprites
r.num_portals := br2.ReadUInt16; //num portals
SetLength(r.Portals,r.num_portals);
for j := 1 to r.num_portals do
begin
geometry.ReadBuffer(r.Portals[j-1],32);
end;
r.numX := br2.ReadUInt16; // X is east west in RoomEdit - opposite of what TRosetta stone says!
r.numZ := br2.ReadUInt16; // Z is north south as per RoomEdit grid
SetLength(r.Sectors, r.numX*r.numZ);
for j := 1 to r.numX * r.numZ do
begin
geometry.ReadBuffer(r.Sectors[j-1],8); // X columns, Z rows
r.Sectors[j-1].hasFD:=False;
end;
geometry.ReadBuffer(r.colour,4);
size := br2.ReadUInt16; //num lights
for j := 1 to size do
begin
geometry.Seek(46, soCurrent); //skip lights
end;
size := br2.ReadUInt16; //num static meshes
for j := 1 to size do
begin
geometry.Seek(20, soCurrent); //skip static meshes
end;
r.altroom := br2.ReadInt16;
r.flags := br2.ReadUInt16;
r.waterscheme := br2.ReadByte;
r.reverb := br2.ReadByte;
r.altgroup := br2.ReadByte;
r.isFlipRoom := False;
r.original_room := -1;
rooms[i] := r;
end;
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
num_floordata := br2.ReadUInt32;
SetLength(floordata,num_floordata);
geometry.ReadBuffer(floordata[0],num_floordata*2);
size := br2.ReadUInt32; //num object mesh data words
geometry.Seek(size * 2, soCurrent); //skip object meshes
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
size := br2.ReadUInt32; //num mesh pointers
geometry.Seek(size * 4, soCurrent); //skip mesh pointers
num_animations := br2.ReadUInt32;
geometry.Seek(num_animations * 40, soCurrent);
num_state_changes := br2.ReadUInt32;
geometry.Seek(num_state_changes * 6, soCurrent);
num_anim_dispatches := br2.ReadUInt32;
geometry.Seek(num_anim_dispatches * 8, soCurrent);
num_anim_commands := br2.ReadUInt32;
geometry.Seek(num_anim_commands * 2, soCurrent);
num_meshtrees := br2.ReadUInt32;
geometry.Seek(num_meshtrees * 4, soCurrent);
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
size_keyframes := br2.ReadUInt32;
geometry.Seek(size_keyframes * 2, soCurrent);
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
num_moveables := br2.ReadUInt32;
geometry.Seek(num_moveables * 18, soCurrent);
num_statics := br2.ReadUInt32;
geometry.Seek(num_statics * 32, soCurrent);
s := br2.ReadChar;
s := s + br2.ReadChar;
s := s + br2.ReadChar;
s := LowerCase(s);
if s <> spr then
begin
MessageDlg('SPR landmark not read correctly!',mtError,[mbOK],0);
resultado:=4;
end;
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
size := br2.ReadUInt32; // num sprite tex
geometry.Seek(size * 16, soCurrent); //skip sprite tex
size := br2.ReadUInt32; // num sprite seq
geometry.Seek(size * 8, soCurrent); //skip sprite seq
size := br2.ReadUInt32; // num cameras
geometry.Seek(size * 16, soCurrent); //skip cameras
size := br2.ReadUInt32; // num flyby cams
geometry.Seek(size * 40, soCurrent); //skip flyby cams
size := br2.ReadUInt32; // num sound sources
geometry.Seek(size * 16, soCurrent); //skip sound sources
num_boxes := br2.ReadUInt32; // num Boxes
SetLength(boxes,num_boxes);
geometry.ReadBuffer(boxes[0],num_boxes *8);
gauge.Progress:= Trunc(memfile.Position / file_size * gauge.MaxValue);
size := br2.ReadUInt32; // num overlaps
geometry.Seek(size * 2, soCurrent); //skip overlaps
geometry.Seek(num_boxes * 20, soCurrent); //skip zones
size := br2.ReadUInt32; // num anim tex words
geometry.Seek(size * 2, soCurrent); //skip anim tex words
geometry.Seek(1,soCurrent); // skip uv ranges
s := br2.ReadChar;
s := s + br2.ReadChar;
s := s + br2.ReadChar;
s := LowerCase(s);
if s <> tex then
begin
MessageDlg('TEX landmark not read correctly!',mtError,[mbOK],0);
resultado := 4;
end;
size := br2.ReadUInt32; // num textures
geometry.Seek(size * 38, soCurrent); //skip textures
gauge.Progress:= gauge.MaxValue;
// rest of TR4 not read
br2.Free;
geometry.Free;
finally
br.Free;
end;
finally
memfile.Free;
end;
if resultado = 0 then
begin
for i:=0 to High(rooms) do
begin
for j:=0 to High(rooms[i].Sectors) do
begin
if rooms[i].Sectors[j].FDindex =0 then Continue;
sectorFD := TList<TParsedFloorData>.Create;
rooms[i].Sectors[j].hasFD:=True;
parseFloorData(rooms[i].Sectors[j].FDindex,sectorFD);
rooms[i].Sectors[j].floorInfo:=sectorFD;
end;
if rooms[i].altroom <> -1 then
if rooms[i].altroom <= High(rooms) then
begin
rooms[rooms[i].altroom].isFlipRoom := True;
rooms[rooms[i].altroom].original_room := i;
end;
end;
end;
Result := resultado;
end;
function TTRLevel.ConvertToPRJ(const filename :string;SaveTGA:Boolean=True;fixFDIVS:Boolean=True): TTRProject;
var
p:TTRProject;
slots:uint32;
i,j,k,ii,jj,temp:Integer;
r1:TRoom;
sector:TSector;
s:string;
img:TImageData;
b:Integer;
fd:TParsedFloorData;
maxCorner:Word;
a:array[0..3]of integer;
po:TPortal;
isHorizontalDoor : Boolean;
bx:Uint32;
begin
if (num_rooms <= 100) then slots:=100
else if ((num_rooms>100) and (num_rooms<=200)) then slots:=200
else slots := 300;
p := TTRProject.Create(num_rooms,slots);
if SaveTGA and (bmp.Width>0) and (bmp.Height>0) then
begin
s := ChangeFileExt(filename,'.tga');
InitImage(img);
img.Format := ifR8G8B8;
ConvertBitmapToData(bmp,img); // make sure bitmap's pixelformat was set to 24!!
SaveImageToFile(s,img);
FreeImage(img);
p.TGAFilePath := ExtractShortPathName(s);
if LowerCase(p.TGAFilePath)='.tga' then MessageDlg('PRJ TGA filename error',mtError,[mbOK],0);
p.TGAFilePath := p.TGAFilePath+' '; // don't forget space
end;
for i:=0 to High(rooms) do
begin
r1:=rooms[i];
if r1.isFlipRoom then StrPCopy(p.Rooms[i].name, Format('Flipped Room%d',[r1.original_room]));
p.Rooms[i].x := r1.x;
// p.Rooms[i].y := r1.yBottom;
p.Rooms[i].z := r1.z;
p.Rooms[i].xsize := r1.numX;
p.Rooms[i].zsize := r1.numZ;
p.Rooms[i].xoffset := (20 - r1.numX) div 2;
p.Rooms[i].zoffset := (20 - r1.numZ) div 2;
p.Rooms[i].xpos := r1.x div 1024;
p.Rooms[i].zpos := r1.z div 1024;
p.Rooms[i].ambient.r := r1.colour.r;
p.Rooms[i].ambient.g := r1.colour.g;
p.Rooms[i].ambient.b := r1.colour.b;
p.Rooms[i].ambient.a := r1.colour.a;
p.Rooms[i].fliproom := r1.altroom;
p.rooms[i].flags1 := r1.flags; // TODO: check they're the same
if r1.isFlipRoom then p.Rooms[i].flags1 := p.Rooms[i].flags1 or $2;
p.Rooms[i].isFlipRoom := r1.isFlipRoom;
p.Rooms[i].flags2 := p.Rooms[i].flags2 or r1.altgroup;
p.Rooms[i].yBottom := -r1.yBottom div 256;
p.Rooms[i].yTop := -r1.yTop div 256;
SetLength(p.Rooms[i].blocks,r1.numZ*r1.numX);
for j:=0 to r1.numZ-1 do // rows
begin
for k:=0 to r1.numX-1 do // columns
begin
b := j*r1.numX+k; // index to block array and sector array
sector := r1.Sectors[b];
p.Rooms[i].blocks[b].id := $1; // default is floor type
p.Rooms[i].blocks[b].Floor := -sector.Floor;
p.Rooms[i].blocks[b].ceiling := -sector.Ceiling;
if (p.Rooms[i].blocks[b].Floor<>127) and fixFDIVS then
begin
for ii:=0 to 3 do
begin
temp:=-Abs(p.Rooms[i].blocks[b].Floor-(-r1.yBottom div 256));
p.Rooms[i].blocks[b].fdiv[ii]:= temp;
end;
end;
if (p.Rooms[i].blocks[b].ceiling<>127) and fixFDIVS then
begin
for ii:=0 to 3 do
begin
temp:=(-r1.yTop div 256)- p.Rooms[i].blocks[b].ceiling;
p.Rooms[i].blocks[b].cdiv[ii]:= Abs(temp);
end;
end;
// corner grey blocks can be any heights I guess since never seen
// aktrekker makes them same as corner blocks of inner blocks
// floor and ceil heights for grey blocks/walls are division lines
if ((k=0)and(j=0)) or ((k=r1.numX-1)and(j=0)) or ((k=0)and(j=r1.numZ-1)) or ((k=r1.numX-1)and(j=r1.numZ-1)) then
begin
p.Rooms[i].blocks[b].id := $1e;
p.Rooms[i].blocks[b].Floor := 0;
p.Rooms[i].blocks[b].ceiling := 20;
end
// other grey outer blocks
else if (j=0) or (j=r1.numZ-1) or (k=0) or (k=r1.numX-1) then
begin
p.Rooms[i].blocks[b].id := $1e;
p.Rooms[i].blocks[b].Floor := -r1.yBottom div 256;
p.Rooms[i].blocks[b].ceiling := -r1.yTop div 256;
end
// green wall blocks except those with door FloorData
// NGLE sometimes switches wall block adjacent to doors to floor block
// no idea why, definitely wall block in prj file
// may need to set heights same as neighbours instead of ybottom and ytop
else if (sector.Floor = -127) then
begin
p.Rooms[i].blocks[b].id := $e;
p.Rooms[i].blocks[b].Floor := -r1.yBottom div 256;
p.Rooms[i].blocks[b].ceiling := -r1.yTop div 256;
end
// black door blocks - no good without door arrays implemented
// no collision blocks need to be marked as id 1, changed later in floordata loop
else if (sector.RoomBelow<>255) and (sector.RoomAbove<>255) then
begin
p.Rooms[i].blocks[b].id := $7;
end
else if (sector.RoomBelow<>255) then
begin
p.Rooms[i].blocks[b].id := $3;
end
else if (sector.RoomAbove<>255) then
begin
p.Rooms[i].blocks[b].id := $5;
end;
bx:=(sector.BoxIndex and $7FF0) shr 4;
if (bx <> $7FF) and (bx < num_boxes) then
begin
if (boxes[bx].overlapindex and $8000)=$8000 then
p.Rooms[i].blocks[b].flags1 := p.Rooms[i].blocks[b].flags1 or $0020;
end;
if sector.hasFD then // need to check since no list of floordata for FDindex=0 sectors
begin
for ii :=0 to sector.floorInfo.Count-1 do
begin
fd:=sector.floorInfo[ii];
if fd.tipo in [trigger] then Continue; // not implemented
if fd.tipo = door then
begin
isHorizontalDoor:=True;
for jj := 0 to High(r1.Portals) do
begin
po:=r1.Portals[jj];
// check if door toRoom is above by checking portals
// if it's not a vertical door then it is a horizontal one
if po.normal.y = 0 then // check vertical portals
begin
if po.toRoom = fd.toRoom then
begin
isHorizontalDoor := False;
Break;
end;
end;
end;
if isHorizontalDoor then // it's a green wall block not a floor block
begin
if p.Rooms[i].blocks[b].id = $1 then p.Rooms[i].blocks[b].id := $e;
if p.Rooms[i].blocks[b].id = $1e then
begin
p.Rooms[i].blocks[b].Floor := -sector.Floor;
p.Rooms[i].blocks[b].ceiling := -sector.Ceiling;
end;
end
else
if p.Rooms[i].blocks[b].id = $1e then
begin
p.Rooms[i].blocks[b].id := $6;
p.Rooms[i].blocks[b].Floor := -sector.Floor; // these values wrong
p.Rooms[i].blocks[b].ceiling := -sector.Ceiling; // fixed later when doors created
end;
Continue;
end;
if fd.tipo=beetle then
begin
p.Rooms[i].blocks[b].flags2:= p.Rooms[i].blocks[b].flags2 or $0040;
Continue;
end;
if fd.tipo=trigtrig then
begin
p.Rooms[i].blocks[b].flags2:= p.Rooms[i].blocks[b].flags2 or $0020;
Continue;
end;
if fd.tipo=climb then
begin
if fd.n then p.Rooms[i].blocks[b].flags1:=p.Rooms[i].blocks[b].flags1 or $0200;
if fd.s then p.Rooms[i].blocks[b].flags1:=p.Rooms[i].blocks[b].flags1 or $0080;
if fd.w then p.Rooms[i].blocks[b].flags1:=p.Rooms[i].blocks[b].flags1 or $0100;
if fd.e then p.Rooms[i].blocks[b].flags1:=p.Rooms[i].blocks[b].flags1 or $0040;
Continue;
end;
if fd.tipo=monkey then
begin
p.Rooms[i].blocks[b].flags1:=p.Rooms[i].blocks[b].flags1 or $4000;
Continue;
end;
if fd.tipo=lava then
begin
p.Rooms[i].blocks[b].flags1:=p.Rooms[i].blocks[b].flags1 or $0010;
Continue;
end;
if fd.tipo=tilt then
begin // can have both X and Z tilt
if fd.addX>=0 then
begin
p.Rooms[i].blocks[b].floorcorner[2]:=fd.addX;
p.Rooms[i].blocks[b].floorcorner[3]:=fd.addX;
end
else
begin
p.Rooms[i].blocks[b].floorcorner[0]:= -fd.addX;
p.Rooms[i].blocks[b].floorcorner[1]:= -fd.addX;
end;
if fd.addZ>=0 then
begin
p.Rooms[i].blocks[b].floorcorner[0]:=p.Rooms[i].blocks[b].floorcorner[0]+fd.addZ;
p.Rooms[i].blocks[b].floorcorner[3]:=p.Rooms[i].blocks[b].floorcorner[3]+fd.addZ;
end
else
begin
p.Rooms[i].blocks[b].floorcorner[2]:=p.Rooms[i].blocks[b].floorcorner[2]+Abs(fd.addZ);
p.Rooms[i].blocks[b].floorcorner[1]:=p.Rooms[i].blocks[b].floorcorner[1]+Abs(fd.addZ);
end;
p.Rooms[i].blocks[b].Floor := p.Rooms[i].blocks[b].Floor-(Abs(fd.addX)+abs(fd.addZ));
if fixFDIVS then
begin
p.Rooms[i].blocks[b].fdiv[0]:= -Abs(p.Rooms[i].blocks[b].Floor-(-r1.yBottom div 256));
p.Rooms[i].blocks[b].fdiv[1]:= -Abs(p.Rooms[i].blocks[b].Floor-(-r1.yBottom div 256));
p.Rooms[i].blocks[b].fdiv[2]:= -Abs(p.Rooms[i].blocks[b].Floor-(-r1.yBottom div 256));
p.Rooms[i].blocks[b].fdiv[3]:= -Abs(p.Rooms[i].blocks[b].Floor-(-r1.yBottom div 256));
end;
Continue;
end; // tilt
if fd.tipo=roof then
begin // can have both X and Z roof
if fd.addX>=0 then
begin
p.Rooms[i].blocks[b].ceilcorner[0]:=-fd.addX;
p.Rooms[i].blocks[b].ceilcorner[1]:=-fd.addX;
end
else
begin
p.Rooms[i].blocks[b].ceilcorner[2]:=fd.addX;
p.Rooms[i].blocks[b].ceilcorner[3]:=fd.addX;
end;
if fd.addZ>=0 then
begin
p.Rooms[i].blocks[b].ceilcorner[1]:=p.Rooms[i].blocks[b].ceilcorner[1]-fd.addZ;
p.Rooms[i].blocks[b].ceilcorner[2]:=p.Rooms[i].blocks[b].ceilcorner[2]-fd.addZ;
end
else
begin
p.Rooms[i].blocks[b].ceilcorner[0]:=p.Rooms[i].blocks[b].ceilcorner[0]+fd.addZ;
p.Rooms[i].blocks[b].ceilcorner[3]:=p.Rooms[i].blocks[b].ceilcorner[3]+fd.addZ;
end;
p.Rooms[i].blocks[b].Ceiling := p.Rooms[i].blocks[b].ceiling+Abs(fd.addX)+abs(fd.addZ);
if fixFDIVS then
begin
p.Rooms[i].blocks[b].cdiv[0]:= Abs((-r1.yTop div 256)- p.Rooms[i].blocks[b].ceiling);
p.Rooms[i].blocks[b].cdiv[1]:= Abs((-r1.yTop div 256)- p.Rooms[i].blocks[b].ceiling);
p.Rooms[i].blocks[b].cdiv[2]:= Abs((-r1.yTop div 256)- p.Rooms[i].blocks[b].ceiling);
p.Rooms[i].blocks[b].cdiv[3]:= Abs((-r1.yTop div 256)- p.Rooms[i].blocks[b].ceiling);
end;
Continue;
end; // roof
if fd.tipo in [split1,split2,nocol1..nocol4] then // floor splits
begin
// no good for corner heights > 15!!! maybe need to check geometry vertices
p.Rooms[i].blocks[b].floorcorner[0]:=fd.corners[0];
p.Rooms[i].blocks[b].floorcorner[1]:=fd.corners[1];
p.Rooms[i].blocks[b].floorcorner[2]:=fd.corners[2];
p.Rooms[i].blocks[b].floorcorner[3]:=fd.corners[3];
a[0]:=fd.corners[0];
a[1]:=fd.corners[1];
a[2]:=fd.corners[2];
a[3]:=fd.corners[3];
maxCorner:=maxIntvalue(a);
p.Rooms[i].blocks[b].Floor := p.Rooms[i].blocks[b].Floor-maxCorner;
if fd.tipo in [split2,nocol3,nocol4] then // function 8 splits
begin
// if corners indicate opposite diagonal
if (a[1]>Max(a[0],a[2])) or (a[3]>Max(a[0],a[2])) or (a[1]<Min(a[0],a[2])) or (a[3]<Min(a[0],a[2])) then
p.Rooms[i].blocks[b].flags3:=p.Rooms[i].blocks[b].flags3 or $1;
if fd.tipo = nocol3 then
p.Rooms[i].blocks[b].flags2:=p.Rooms[i].blocks[b].flags2 or $4;
if fd.tipo = nocol4 then
p.Rooms[i].blocks[b].flags2:=p.Rooms[i].blocks[b].flags2 or $2;
end;
if fd.tipo in [split1,nocol1,nocol2] then // function 7 splits
begin
// if corners indicate opposite diagonal
if (a[0]>Max(a[1],a[3])) or (a[2]>Max(a[1],a[3])) or (a[0]<Min(a[1],a[3])) or (a[2]<Min(a[1],a[3])) then
p.Rooms[i].blocks[b].flags3:=p.Rooms[i].blocks[b].flags3 or $1;
if fd.tipo = nocol1 then
p.Rooms[i].blocks[b].flags2:=p.Rooms[i].blocks[b].flags2 or $4;
if fd.tipo = nocol2 then
p.Rooms[i].blocks[b].flags2:=p.Rooms[i].blocks[b].flags2 or $2;
end;
if fd.tipo in [nocol1..nocol4] then
begin
if p.Rooms[i].blocks[b].id = $3 then p.Rooms[i].blocks[b].id := $1;
if p.Rooms[i].blocks[b].id = $7 then p.Rooms[i].blocks[b].id := $5;//todo check this
end;
Continue;
end; //floor splits
if fd.tipo in [split3,split4,nocol5..nocol8] then // ceiling splits
begin
// no good for corner heights > 15!!! maybe need to check geometry vertices
p.Rooms[i].blocks[b].ceilcorner[0]:=-fd.corners[0];
p.Rooms[i].blocks[b].ceilcorner[1]:=-fd.corners[1];
p.Rooms[i].blocks[b].ceilcorner[2]:=-fd.corners[2];
p.Rooms[i].blocks[b].ceilcorner[3]:=-fd.corners[3];
a[0]:=fd.corners[0];
a[1]:=fd.corners[1];
a[2]:=fd.corners[2];
a[3]:=fd.corners[3];
maxCorner:=maxIntvalue(a);
p.Rooms[i].blocks[b].ceiling := p.Rooms[i].blocks[b].ceiling+maxCorner;
if fd.tipo in [nocol5, nocol7] then
p.Rooms[i].blocks[b].flags2:=p.Rooms[i].blocks[b].flags2 or $10;
if fd.tipo in [nocol6, nocol8] then
p.Rooms[i].blocks[b].flags2:=p.Rooms[i].blocks[b].flags2 or $8;
if fd.tipo in [nocol5..nocol8] then
begin
if p.Rooms[i].blocks[b].id = $5 then p.Rooms[i].blocks[b].id := $1;
if p.Rooms[i].blocks[b].id = $7 then p.Rooms[i].blocks[b].id := $3;//todo check this
end;
Continue;
end; // ceiling splits
end; // loop thru FData
end; // has FData
end; // loop thru X block columns
end; // loop thru Z block rows
end; // loop thru rooms
Result := p;
{ todo: REHome demo level produces block.floor = 127 causes display problem.
Floor in tr4 is -126}
end;
procedure TTRLevel.MakeDoors(var p: TTRProject; tr2prjlinks:Boolean);
var
i, j, k, room, previous_room, firstroom : Integer;
minx, maxx, minz, maxz : Integer;
portal : TPortal;
portalarray : TPortalArray; //array of TPortal;
r : TRoom;
d, dd : TDoor;
doorcount : Integer;
bloks, bloks2 : TWords;
found:Boolean;
rm : Word;
begin
// Uses aktrekker's method of using portals to determine doors
// aktrekker's TR2PRJ crashes making doors if duplicate portals - see aldwych level
// so check and remove duplicate portals if any
for i := 0 to High(rooms) do
begin
r := rooms[i];
if r.num_portals = 0 then Continue;
SetLength(portalarray, 0);
for j := 0 to (r.num_portals-1)-1 do
begin
k := j+1;
found := False;
while (k <= (r.num_portals-1)) and (found = False) do
begin
if r.Portals[j].Equals(r.Portals[k]) then
found := True
else
inc(k);
end;
if not found then
begin
SetLength(portalarray, length(portalarray)+1);
portalarray[High(portalarray)] := r.Portals[j];
end;
end; // loop thru portals
SetLength(portalarray, length(portalarray)+1);
portalarray[High(portalarray)] := r.Portals[r.num_portals-1];
if Length(portalarray) < Length(r.Portals) then
begin
{$IFDEF DEBUG}
MessageDlg(Format('Room %d - duplicate portal removed',[i]), mtWarning,[mbOK],0);
{$ENDIF}
rooms[i].num_portals := Length(portalarray);
rooms[i].Portals := portalarray;
end;
end; // loop thru rooms
// make doors
doorcount := 0;
for i := 0 to High(rooms) do
begin
r := rooms[i];
p.Rooms[i].numdoors := r.num_portals;
SetLength(p.Rooms[i].doors, r.num_portals);
SetLength(p.Rooms[i].doorthingindex, r.num_portals);
for j := Low(r.Portals) to High(r.Portals) do
begin
portal := r.Portals[j];
minx := portal.vertices[0].x;
maxx := minx;
minz := portal.vertices[0].z;
maxz := minz;
for k := 1 to 3 do
begin
if portal.vertices[k].x < minx then minx := portal.vertices[k].x;
if portal.vertices[k].x > maxx then maxx := portal.vertices[k].x;
if portal.vertices[k].z < minz then minz := portal.vertices[k].z;
if portal.vertices[k].z > maxz then maxz := portal.vertices[k].z;
end;
d.room := i;
d.yclickabovefloor := 0;
FillChar(d.filler,SizeOf(d.filler),0);
d.filler[0] := portal.toRoom; // use filler[0] as toRoom temporarily
p.Rooms[i].doorthingindex[j] := doorcount;
// north //for room mesh though z east/west and x north/south opposite of room edit
if portal.normal.x = 1 then
begin
d.id := 2;
d.zpos := 0;
d.zsize := 1;
d.xpos := minz div 1024;
d.xsize := (maxz - minz) div 1024;
end;
// south
if portal.normal.x = -1 then
begin
d.id := $fffd;
d.zpos := minx div 1024;
d.zsize := 1;
d.xpos := minz div 1024;
d.xsize := (maxz - minz) div 1024;
end;
// west
if portal.normal.z = 1 then
begin
d.id := 1;
d.xpos := 0;
d.xsize := 1;
d.zpos := minx div 1024;
d.zsize := (maxx - minx) div 1024;
end;
// east
if portal.normal.z = -1 then
begin
d.id := $fffe;
d.xpos := minz div 1024;
d.xsize := 1;
d.zpos := minx div 1024;
d.zsize := (maxx - minx) div 1024;
end;
// pit
if portal.normal.y = -1 then
begin
d.id := 4;
d.xpos := minz div 1024;
d.xsize := (maxz - minz) div 1024;
d.zpos := minx div 1024;
d.zsize := (maxx - minx) div 1024;
end;
// sky
if portal.normal.y = 1 then
begin
d.id := $fffb;
d.xpos := minz div 1024;
d.xsize := (maxz - minz) div 1024;
d.zpos := minx div 1024;
d.zsize := (maxx - minx) div 1024;
end;
if not r.isFlipRoom then Inc(doorcount); // need to correct doorindex for flipped rooms later
p.Rooms[i].doors[j] := d;
end; // loop thru portals
end; //loop thru rooms
// update number of things
p.NumThings := doorcount;
// determine slot value
for i := 0 to High(rooms) do
begin
for j := 0 to High(rooms[i].Portals) do
begin