This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelEngine.pas
More file actions
2869 lines (2743 loc) · 80.2 KB
/
LevelEngine.pas
File metadata and controls
2869 lines (2743 loc) · 80.2 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
unit LevelEngine;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
D3DUtils, DirectX, UniDib, DibToolsNE, Dib, DXDraws, DXClass, JPEG, Math;
const
lvPoly=0;
lvGrass=1;
lvStart=2;
lvExit=3;
lvKiller=4;
lvFood=5;
lvPicture=6;
lvVertex=7;
lvHighlight=8;
lvBackground=9;
lvCrosshair=10;
//
ltNormal=0;
ltAcross=1;
ltOther=2;
//
ftNone=0;
ftPicture=1;
ftObject=2;
type
TRPoint=record
x,y:double;
Selected:boolean;
end;
PPolygon=^TPolygon;
TPolygon=record
Grass:dword;
Layer:dword;
Orientation:boolean;
Vertices:array of TRPoint;
Triangles:array of TRPoint;
end;
TEObject=record
Pos:TRPoint;
typ:dword;
TofF:dword;
AnimNumber:dword;
end;
TEPicture=record
Name:string[10];
Texture:string[10];
Mask:string[10];
Pos:TRPoint;
Distance:dword;
Clipping:dword;
end;
TAPolygon=array of TPolygon;
TAObject=array of TEObject;
TAPicture=array of TEPicture;
TALEImage=record
Texture:TDirect3DTexture2;
Bmp:TBitmap;
end;
TBGPicture=record
Texture:TDirect3DTexture2;
srect:TRect;
x1,y1,x2,y2:double;
end;
TPic=record
Name:string;
tip:dword;
Distance,Clipping:dword;
Width,Height:integer;
Image:TALEImage;
end;
TPC=array of TPic;
TTool=class;
TBackup=class
Polygons:TAPolygon;
Objects:TAObject;
Pics:TAPicture;
name:string[51];
LGR:string[16];
Ground:string[10];
Sky:string[10];
end;
TLevel=class
private
fModified: boolean;
FAnimIndex: integer;
fCurrentLevel:TFileName;
fZoom: double;
fMode:integer;
IGround,ISky:integer;
FLGR: ShortString;
fFillPolygons:boolean;
LoadingPictures:boolean;
FTopChecked: boolean;
procedure SetModified(const Value: boolean);
procedure SetAnimIndex(const Value: integer);
procedure SetMode(const Value: integer);
procedure SetCurLevel(const Value: TFileName);
procedure SetZoom(const Value: double);
procedure GetVertices(srect, drect:TRect;w,h :real; var Vertices:array of TD3DVertex);
function GetTC(x,y,w,h :real;speed:real):TD3DVertex;
function GetTexture(FileName:TFileName; mult:boolean; var w,h:integer;f:TFileStream;Size:integer;Pos:integer):TALEImage;
procedure SetLGR(const Value: ShortString);
procedure SetFillPolygons(const Value: boolean);
procedure SetTopChecked(const Value: boolean);
function IsImageHere(const Tex:TALEImage; x, y: integer; FramedType: integer):boolean;
public
//Ñâîéñòâà
property FillPolygons:boolean read fFillPolygons write SetFillPolygons;
property TopChecked:boolean read FTopChecked write SetTopChecked;
public
ShowGrass:boolean;
ShowPictures:boolean;
FillTextures:boolean;
FullTopChecked:boolean;
FramePictures:boolean;
FrameObjects:boolean;
ShowTextures:boolean;
ZoomTextures:boolean;
HighLightVertices:boolean;
HighLightPO:boolean;
CrossHair:boolean;
ShowPolygons:boolean;
ShowObjects:boolean;
MapMode:boolean;
NoCheckOrientation:boolean;
ShowGrid,SnapToGrid:boolean;
ShowInfo:boolean;
UseNormalCapture:boolean;
UseSmartCapture:boolean;
//end
//Level
Polygons:TAPolygon;
Objects:TAObject;
Pics:TAPicture;
Name:string[51];
Ground:string[10];
Sky:string[10];
//end
//Palette
Palette:array[0..10] of Cardinal;
MapCLoaded:boolean;
MapColors:array[0..8] of Cardinal;
//end;
RNear:integer;
GridSize:double;
Width,Height:Integer;
Center:TRPoint;
DXDraw:TDXDraw;
PC:TPC;
Tool:TTool;
//Images
QExit,QKiller,QMoto:TALEImage;
QFood:array[1..9] of TALEImage;
BGPicture:TBGPicture;
//end
// DelOld:boolean;
//Backups
Backups:array of TBackup;
CurCopy:integer;
//end
constructor Create(DD:TDXDraw;w,h:integer);
destructor Destroy;override;
procedure New;
procedure Open(Filename: TFileName);
procedure Save(Filename: TFileName; Locked: boolean; OnlySelected: boolean);
procedure Draw;
procedure SetSize(w,h:integer);
procedure ZoomFill;
procedure IncZoom;
procedure DecZoom;
procedure MoveCenter(dx,dy:integer);
function CheckTopology(var p:TRPoint;Full: boolean):boolean;
procedure MouseDown(Shift: TShiftState;nx,ny:integer);
procedure MouseMove(Shift: TShiftState;nx,ny:integer);
procedure MouseUp(Shift: TShiftState;nx,ny:integer);
procedure KeyPressed(key:word);
procedure WhatHere(p:TRPoint;var npol,n,tp:integer);
function StatusLine(p:TPoint):string;
procedure SortPictures;
function PromtOnSave:integer;
procedure SwapBackup(index:integer);
procedure Undo;
procedure Redo;
procedure InitUndoRedoVisible;
// procedure UnPackLgr(NLgr: string);
procedure InitTransText;
procedure ClearBestTimes;
procedure AddObject(obj:TEObject);
procedure AddPicture(pic:TEPicture);
procedure AddPolygon(pol:TPolygon);
function DeleteObject(index: integer):boolean;
procedure DeletePicture(index:integer);
procedure DeletePolygon(index:integer);
function DeleteVertex(npol, n: integer):boolean;
procedure AddMessage(mess:string;index:integer);
procedure ExportBMP(FileName:TFileName);
function SwapColors(t:Cardinal):Cardinal;
function GetX(x:double):double;
function GetY(y:double):double;
function GetOX(x:double):integer;
function GetOY(y:double):integer;
function GetRX(x:integer):double;
function GetRY(y:integer):double;
function SearchPic(name:string):integer;
procedure DrawObject(Ob:TEObject);
procedure DrawPicture(Pic:TEPicture);
procedure DrawPolygon(Poly:PPolygon);
procedure DrawVertices(Ver:TRPoint);
procedure LoadPictures(pPC:TPC);
procedure LoadBackGroundPic(FileName:TFileName);
procedure ClearBackGroundPic;
procedure GetSize(var w,h:double);
procedure CheckChangingLGR;
procedure CalcOrientation(Poly:PPolygon);
function PtInPol(p:TRPoint;pol:PPolygon):boolean;
function GetRecInfo(FileName:TFileName;var LevelName:TFileName):double; function GetLevInfo(FileName:TFileName;out LevelName,LgrName,GroundName,SkyName:string):integer;
function TimeToStr(time:integer):string;
function FramesToTime(frames:double):integer;
procedure Normalize(var V:TRPoint);
procedure SendToBack(npol:integer);
procedure BringToFront(npol:integer);
function Intersect(p1,p2,r1,r2:TRPoint;var t:TRPoint):boolean;
//Properties
property Mode:integer read fmode write SetMode;
property CurrentLevel:TFileName read fCurrentLevel write SetCurLevel;
property Zoom:double read fZoom write SetZoom;
property Modified:boolean read fModified write SetModified;
property AnimIndex:integer read FAnimIndex write SetAnimIndex;
property LGR:shortstring read FLGR write SetLGR;
end;
TTool=class
constructor Create(Par:TLevel);virtual;
procedure Draw;virtual;abstract;
procedure MouseDown(Shift: TShiftState;nx,ny:integer);virtual;abstract;
procedure MouseMove(Shift: TShiftState;nx,ny:integer);virtual;abstract;
procedure MouseUp(Shift: TShiftState;nx,ny:integer);virtual;abstract;
procedure KeyPressed(key:word);virtual;
protected
Fstate: integer;
Parent:TLevel;
x,y:integer;
procedure Setstate(const Value: integer);virtual;
property State:integer read Fstate write Setstate;
end;
implementation
uses Unit1, Tools, Unit9;
const
empty:array[0..171] of dword=(
$B76A0515,$C459ED89,$738FFF48,$C070BC76,$2FB457DF,$BC079E0D,$8A6F0863,
$38AD2809,$A0F973E0,$8A000080,$60696F37,$41770917,$34268CAC,$98A702AC,
$80FEA5C9,$B497C0DF,$CA1DD750,$47FB4144,$3860D8F7,$824CCB84,$919A2431,
$6B0EC976,$CCFC75CC,$9874A5F8,$68BB028E,$BD4CCB44,$F434C499,$684C7A8B,
$709A5358,$CC2B4FCD,$80E01AD1,$34738F2E,$46D40EE7,$9A02BD1D,$842733BF,
$A4115806,$751DE410,$2F78B02A,$EF8606E6,$FCF9D5A4,$74542E91,$B191F85D,
$F955A2F6,$8E47FB23,$E7F4450F,$8C39545D,$388F2ED1,$68BB8FBF,$C8D338A0,
$3B914989,$880302BD,$9C7F2CE5,$575F93B5,$4BE4A1A0,$6BFD5C11,$FCC63484,
$0917B191,$A5963B91,$BD3F9874,$89A67D5E,$52E02784,$A2A5A764,$734F4090,
$90049834,$A5B8CD1F,$C4884381,$19CCC9D8,$65971E2D,$5835FC46,$2D2AD732,
$98164AFD,$9D84DA81,$B08C5B19,$BE551585,$94FEA538,$DAE3CB95,$04692256,
$46078DB1,$34A66C93,$F8D0545D,$E11F9084,$ED67B6C3,$F6609838,$025BC860,
$A7C21C5F,$CF7EA35D,$7F1BA724,$A742DE7B,$ABAFF6D3,$0364A3CC,$BE15232C,
$454F40AE,$7E0550F4,$FDCBF7E9,$528FFF8C,$1D5347FB,$ABBC7633,$E0B840BF,
$0D00EF17,$13663E42,$8EF653F6,$50B4B51C,$B81EBACA,$4D10021B,$EC8489E6,
$DD03F1E1,$BFB89EEB,$CFFE32A9,$2CC32180,$C04E267F,$E3AD6848,$11BAEC15,
$74D44E48,$289A2471,$98B4B5AD,$D3561A04,$D094BEE2,$57B0FB12,$56ABBC65,
$3BE20677,$453EF55B,$AFE51501,$216F0469,$34D531E4,$240F30AC,$3CA706D5,
$2AB55C91,$846BCE68,$C8603641,$FCB9B413,$B39F63AB,$D8C4193B,$A31DA411,
$CC89B708,$00009EEB,$C51ECBF3,$287C77E7,$894880A0,$88D41B49,$CE0655C4,
$8FFFAAA6,$91382DBB,$8BA3BBB1,$ECC4B759,$A0E84A30,$B9E37ADC,$C2ED38A0,
$2838BED5,$CF6D9816,$4B135573,$A9F2194C,$42EB9D04,$4BD7218D,$DB9B072B,
$826E5023,$3404D8D5,$ED38BE15,$53B610B5,$3258F579,$D6E92088,$97DEEA98,
$A36A962A,$2FF80C5D,$566BACC5,$D25179C6,$ED672529,$389CFF59,$CB84A7D3,
$C56F4804,$36C19722,$95C314AF,$4CE960D8);
{ TLevel }
procedure TLevel.AddMessage(mess: string;index:integer);
begin
with ALEForm,ComboBoxEx1 do begin
ItemsEx.AddItem(mess,index,index,-1,0,nil);//.Index:=ItemsEx.Count-1;
ItemIndex:=ItemsEx.Count-1;
// ItemsEx.ComboItems[0].Index
end;
end;
procedure TLevel.AddObject(obj: TEObject);
begin
SetLength(Objects,Length(Objects)+1);
Objects[High(Objects)]:=Obj;
end;
procedure TLevel.AddPicture(pic: TEPicture);
begin
SetLength(Pics,Length(Pics)+1);
Pics[High(Pics)]:=pic;
end;
procedure TLevel.AddPolygon(pol: TPolygon);
begin
if Length(Pol.Vertices)<=2 then exit;
SetLength(Polygons,Length(Polygons)+1);
SetLength(Polygons[High(Polygons)].Vertices,Length(Pol.Vertices));
with Polygons[High(Polygons)] do begin
Vertices:=Copy(Pol.Vertices);
Grass:=Pol.Grass;
end;
end;
procedure TLevel.BringToFront(npol: integer);
var
Pol:TPolygon;
i:integer;
begin
if npol=0 then exit;
Modified:=true;
Pol:=Polygons[npol];
for i:=npol downto 1 do Polygons[i]:=Polygons[i-1];
Polygons[0]:=Pol;
end;
procedure TLevel.CalcOrientation(Poly: PPolygon);
var
angle,da,dx1,dy1,dx2,dy2:double;
i:integer;
begin
asm finit end;
//angle - ïëîùàäü
with Poly^ do begin
dx2:=Vertices[1].x-Vertices[0].x;
dy2:=Vertices[1].y-Vertices[0].y;
angle:=0;
for i:=2 to High(Vertices) do
//if (Vertices[j].x-Vertices[0].x<>0) or (Vertices[j].y-Vertices[0].y<>0) then
begin
dx1:=dx2;dy1:=dy2;
dx2:=Vertices[i].x-Vertices[0].x;
dy2:=Vertices[i].y-Vertices[0].y;
da:=dx1*dy2-dy1*dx2;
angle:=angle+da;
end;
if angle<0 then Orientation:=orReverse
else Orientation:=orNormal;
end;
end;
procedure TLevel.CheckChangingLGR;
var
i,j:integer;
lgrChanged,nopic:boolean;
begin
i:=0;
lgrchanged:=false;
while (i<=High(Pics)) do with Pics[i] do begin
nopic:=false;
if (Pics[i].Name='') then begin
if SearchPic(Texture)<0 then nopic:=true
else if SearchPic(Mask)<0 then begin
AddMessage(Format(MessageString[0],[Texture]),iWarning);
j:=0;
while (j<=High(PC)) do begin
if PC[j].tip=ptMask then break;
end;
if PC[j].tip=ptMask then Mask:=PC[j].Name
else nopic:=true;
end;
end else begin
if SearchPic(Pics[i].Name)<0 then nopic:=true;
end;
if nopic then begin
for j:=i to High(Pics)-1 do
Pics[j]:=Pics[j+1];
SetLength(Pics,High(Pics));
end else inc(i);
lgrchanged:=lgrchanged or nopic;
end;
if lgrchanged then AddMessage(MessageString[1],iWarning);
end;
function TLevel.CheckTopology(var p: TRPoint; Full: boolean): boolean;
var
i,j,k,l:integer;
tmp:TPolygon;
cnt:integer;
w,h:double;
Vertex:TRPoint;
sorted:boolean;
vers: integer;
procedure Triangulate(const ver:array of TRPoint;np:integer);
var
nver:array of TRpoint;
i,n,l,j,n2:integer;
a,v1,v2,v:TRpoint;
p1,p2,r1,r2,p,rOld,k1,k2:double;
begin
if Length(ver)<3 then exit;
if Length(ver)=3 then begin
Polygons[np].Triangles[cnt*3]:=ver[0];
Polygons[np].Triangles[cnt*3+1]:=ver[1];
Polygons[np].Triangles[cnt*3+2]:=ver[2];
inc(cnt);
exit;
end;
//Îïðåäåëåíèå âåðøèíû ñ íóæíûì èçãèáîì
j:=-1;
l:=Length(ver);
repeat
if j=high(ver) then begin
//ShowMessage('Yo');
//Ïîëèãîí â îäíó ëèíèþ
exit;
end;
inc(j);
a:=ver[j];
v1.x:=ver[(j+1) mod l].x-a.x;
v1.y:=ver[(j+1) mod l].y-a.y;
v2.x:=ver[(j+l-1) mod l].x-a.x;
v2.y:=ver[(j+l-1) mod l].y-a.y;
p:=v1.x*v2.y-v1.y*v2.x;
until ((p>0) xor Polygons[np].Orientation) and (p<>0);
//Íîðìèðîâàíèå
r1:=sqrt(sqr(v1.x)+sqr(v1.y));
if r1=0 then r1:=1e-10;
if r1<>0 then begin
v1.x:=v1.x/r1;
v1.y:=v1.y/r1;
end;
r2:=sqrt(sqr(v2.x)+sqr(v2.y));
if r2=0 then r2:=1e-10;
if r2<>0 then begin
v2.x:=v2.x/r2;
v2.y:=v2.y/r2;
end;
//Ïîèñê ñå÷åíèÿ
n:=-1;
rOld:=0;
p:=v1.x*v2.x+v1.y*v2.y;
if p=1 then p:=p-1e-10;
if p=-1 then p:=p+1e-10;
for i:=j+2 to l+j-2 do begin
v.x:=ver[i mod l].x-a.x;
v.y:=ver[i mod l].y-a.y;
p1:=(v1.x*v.x+v1.y*v.y);
p2:=(v2.x*v.x+v2.y*v.y);
// if (1-p*p)=0 then ShowMessage('Yo');
k1:=(p1-p*p2)/(1-p*p)/r1;
k2:=(p2-p*p1)/(1-p*p)/r2;
if (k1>=0) and (k2>=0) and (k1+k2<=1) then begin
if (n<0) or ((n>=0) and (k1+k2<rOld)) then begin
n:=i mod l;
rOld:=k1+k2;
end;
end;
end;
if n<0 then begin
SetLength(nver,3);
for i:=0 to 2 do nver[i]:=ver[(i+j-1+l) mod l];
Triangulate(nver,np);
SetLength(nver,length(ver)-1);
//nver[0]:=ver[(j-1+l) mod l];
for i:=0 to high(nver) do nver[i]:=ver[(i+j+1) mod l];
Triangulate(nver,np);
end else begin
n2:=(n-j+1+l) mod l;
SetLength(nver,n2);
for i:=0 to n2-1 do nver[i]:=ver[(i+j) mod l];
Triangulate(nver,np);
SetLength(nver,l-n2+2);
for i:=0 to l-n2+1 do nver[i]:=ver[(i+n) mod l];
Triangulate(nver,np);
end;
end;
begin
asm finit end;
//Ïðîâåðêà òîïîëîãèè
for i:=0 to High(Polygons) do
if Polygons[i].Grass<>1 then
for j:=0 to High(Polygons) do
if (Polygons[j].Grass<>1)then begin
for k:=0 to High(Polygons[i].Vertices) do
for l:=0 to High(Polygons[j].Vertices) do begin
if not ((abs(k-l)=1) or (abs(k-l)=High(Polygons[i].Vertices)) or (k=l))
or (i<>j) then
if Intersect(Polygons[i].Vertices[k],
Polygons[i].Vertices[(k+1) mod length(Polygons[i].Vertices)],
Polygons[j].Vertices[l],
Polygons[j].Vertices[(l+1) mod length(Polygons[j].Vertices)],
p)
then begin
Result:=false;
TopChecked:=false;
exit;
end;
end;
end;
TopChecked:=true;
//Ïðîâåðêà, ÷òî ñòàðò íàõîäèòñÿ âíóòðè íåáà
i:=0;
cnt:=1;
while (i<High(Objects)) do begin
if Objects[i].typ=4 then break;
inc(i);
end;
if Length(Objects)>0 then
if Objects[i].typ=4 then begin
cnt:=0;
for k:=0 to High(Polygons) do
if (Polygons[k].Grass=0) then begin
if PtInPol(Objects[i].Pos,@Polygons[k])
then inc(cnt);
end;
end;
if not odd(cnt) then begin
Result:=false;
AddMessage(MessageString[61],iWarning);
end else Result:=true;
if Length(Polygons)>300 then begin
AddMessage(Format(MessageString[2],[Length(Polygons),300]),iWarning);
Result:=false;
end;
vers:=0;
for i:=0 to High(Polygons) do begin
if Length(Polygons[i].Vertices)>1000 then begin
AddMessage(Format(MessageString[80],[Length(Polygons[i].Vertices),i,1000]),iWarning);
Result:=false;
end;
vers:=vers+Length(Polygons[i].Vertices);
end;
if vers>5130 then begin
AddMessage(Format(MessageString[81],[vers,5130]),iWarning);
Result:=false;
end;
if Length(Objects)>52 then begin
AddMessage(Format(MessageString[3],[Length(Objects),52]),iWarning);
Result:=false;
end;
if Length(Pics)>5000 then begin
AddMessage(Format(MessageString[4],[Length(Pics),5000]),iWarning);
Result:=false;
end;
GetSize(w,h);
if w>=188 then begin
AddMessage(Format(MessageString[5],[round(w),188]),iWarning);
Result:=false;
end;
if h>=188 then begin
AddMessage(Format(MessageString[6],[round(w),188]),iWarning);
Result:=false;
end;
//Îïðåäåëåíèå ñëîÿ
for i:=0 to High(Polygons) do with Polygons[i] do
if Grass=0 then begin
Layer:=0;
for k:=0 to High(Polygons) do
if (k<>i) and (Polygons[k].Grass=0) then begin
if PtInPol(Vertices[0],@Polygons[k])
then Layer:=Layer+1;
end;
end;
//Îïðåäåëåíèå îðèåíòàöèè
for i:=0 to High(Polygons) do with Polygons[i] do
if Grass=0 then begin
CalcOrientation(@Polygons[i]);
if not NoCheckOrientation then
if (Orientation=orReverse) xor odd(Layer) then begin
Orientation:=not Orientation;
for j:=0 to (High(Vertices)-1) div 2 do begin
Vertex:=Vertices[j];
Vertices[j]:=Vertices[High(Vertices)-j];
Vertices[High(Vertices)-j]:=Vertex;
end;
end;
end;
if not Full then exit;
//Ñîðòèðîâêà ïî ñëîþ
for i:=0 to High(Polygons)-1 do begin
sorted:=true;
for j:=0 to High(Polygons)-i-1 do
if ((Polygons[j].Grass=0) and (Polygons[j+1].Grass=0) and (Polygons[j].Layer>Polygons[j+1].Layer))
or ((Polygons[j].Grass=1) and (Polygons[j+1].Grass=0))
then begin
tmp:=Polygons[j];
Polygons[j]:=Polygons[j+1];
Polygons[j+1]:=tmp;
sorted:=false;
end;
if sorted then continue;
end;
//Òðèàíãóëÿöèÿ
for i:=0 to High(Polygons) do with Polygons[i] do
if Grass=0 then begin
SetLength(Triangles,(Length(Vertices)-2)*3);
cnt:=0;
Triangulate(Vertices,i);
end else SetLength(Triangles,0);
FullTopChecked:=true;
end;
procedure TLevel.ClearBackGroundPic;
begin
BGPicture.Texture.Free;
BGPicture.Texture:=nil;
end;
procedure TLevel.ClearBestTimes;
var
i:integer;
begin
for i:=0 to 687 do BestTimes[i]:=0;
end;
constructor TLevel.Create(DD:TDXDraw;w,h:integer);
begin
Inherited Create;
{ DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_MIPMAPLODBIAS,D3DTFP_LINEAR);
DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_MIPFILTER,D3DTFP_LINEAR);}
LoadingPictures:=false;
//Canvas:=Can;
DXDraw:=DD;
Center.x:=0;
Center.y:=0;
Zoom:=3;
Width:=w;
Height:=h;
Palette[lvPoly ]:=0;
Palette[lvGrass ]:=32768;
Palette[lvStart ]:=255;
Palette[lvExit ]:=16777215;
Palette[lvKiller ]:=0;
Palette[lvFood ]:=16711680;
Palette[lvPicture ]:=128;
Palette[lvVertex ]:=255;
Palette[lvHighlight ]:=16711680;
Palette[lvBackground]:=12632256;
Palette[lvCrosshair ]:=255;
//for i:=0 to High(Palette) do Palette[i]:=SwapColors(Palette[i]);
{Bitmap:=TBitmap.Create;
Bitmap.Width:=Width;
Bitmap.Height:=Height;}
// Bitmap.PixelFormat:=pf24bit;
Modified:=false;
TopChecked:=true;
Polygons:=nil;
Objects:=nil;
Tool:=nil;
//Obj:=TUniDib.Create(40,40,8,SBU_NONE);
//Obj.PixelFormat:=pf24bit;
//Obj.Width:=40;
//Obj.Height:=40;
AnimIndex:=0;
fLgr:='';
SetLength(Backups,0);
CurCopy:=-1;
//New;
InitUndoRedoVisible;
ClearBackGroundPic;
if DXDraw<>nil then QMoto:=GetTexture('QMoto',false,w,h,nil,0,0);
end;
procedure TLevel.DecZoom;
begin
Zoom:=Zoom/1.5;
Draw;
end;
function TLevel.DeleteObject(index: integer):boolean;
//true åñëè îáúåêò áûë óäàëåí
var
i,j:integer;
tp:integer;
procedure Del;
var
i:integer;
begin
for i:=index to High(Objects)-1 do
Objects[i]:=Objects[i+1];
SetLength(Objects,High(Objects));
Result:=true;
end;
begin
Result:=false;
if (index<0) or (index>High(Objects)) then exit;
with Objects[index] do begin
tp:=typ;
if tp=4 then begin
AddMessage(MessageString[7],iWarning);
exit;
end;
if (tp=2) or (tp=3) then Del;
if tp=1 then begin
j:=0;
for i:=0 to High(Objects) do
if Objects[i].typ=1 then inc(j);
if j>=2 then Del
else AddMessage(MessageString[8],iWarning);
end;
end;
end;
procedure TLevel.DeletePicture(index: integer);
var
i:integer;
begin
if (index<0) or (index>High(Pics)) then exit;
for i:=index to High(Pics)-1 do
Pics[i]:=Pics[i+1];
SetLength(Pics,High(Pics));
end;
procedure TLevel.DeletePolygon(index: integer);
var
i:integer;
begin
SetLength(Polygons[index].Vertices,0);
for i:=index to High(Polygons)-1 do
Polygons[i]:=Polygons[i+1];
if index<=High(Polygons) then SetLength(Polygons,High(Polygons));
end;
function TLevel.DeleteVertex(npol, n: integer):boolean;
//true åñëè áûëà ïîïûòêà óäàëèòü ïîëèãîí
var
i:integer;
begin
with Polygons[npol] do
if Length(Vertices)>3 then begin
for i:=n to High(Vertices)-1 do
Vertices[i]:=Vertices[i+1];
if n<=High(Vertices) then SetLength(Vertices,High(Vertices));
Result:=false;
end else begin
if Length(Polygons)>1 then DeletePolygon(npol)
else AddMessage(MessageString[9],iWarning);
Result:=true;
end;
end;
destructor TLevel.Destroy;
var
i:integer;
begin
SetLength(Objects,0);
SetLength(Polygons,0);
SetLength(Pics,0);
for i:=0 to High(PC) do begin
PC[i].Image.Texture.Free;
PC[i].Image.Bmp.Free;
end;
SetLength(PC,0);
QExit.Texture.Free;
QExit.Bmp.Free;
for i:=1 to 9 do begin
QFood[i].Texture.Free;
QFood[i].Bmp.Free;
end;
QKiller.Texture.Free;
QKiller.Bmp.Free;
QMoto.Texture.Free;
for i:=0 to High(Backups) do Backups[i].Free;
SetLength(Backups,0);
ClearBackGroundPic;
Inherited;
end;
procedure TLevel.Draw;
var
i,j,w,h,old:integer;
p,p2:TPoint;
r:TRect;
vp: TD3DViewport7;
mtrl: TD3DMaterial7;
matProj, matView: TD3DMatrix;
r2: TD3DRect;
Vertices:array[0..3] of TD3DVertex;
begin
if DXDraw=nil then exit;
if not DXDraw.CanDraw then exit;
if LoadingPictures then exit;
asm finit end;
//View matrix
D3DUtil_SetViewMatrix(matView,MakeD3DVector(0,0,-1),MakeD3DVector(0,0,1),MakeD3DVector(0,0,0));
DXDraw.D3DDevice7.SetTransform( D3DTRANSFORMSTATE_VIEW, matView);
//Viewport
FillChar(vp, SizeOf(vp), 0);
vp.dwX := 0;
vp.dwY := 0;
vp.dwWidth := DXDraw.SurfaceWidth;
vp.dwHeight := DXDraw.SurfaceHeight;
vp.dvMinZ := 0.0;
vp.dvMaxZ := 1.0;
DXDraw.D3DDevice7.SetViewport(vp);
//Material
FillChar(mtrl, SizeOf(mtrl), 0);
mtrl.ambient.r := 1.0;
mtrl.ambient.g := 1.0;
mtrl.ambient.b := 1.0;
mtrl.diffuse.r := 1.0;
mtrl.diffuse.g := 1.0;
mtrl.diffuse.b := 1.0;
DXDraw.D3DDevice7.SetMaterial(mtrl);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_AMBIENT, $ffffffff);
//Projection
D3DUtil_SetProjectionMatrix(matProj,2*arctan(DXDraw.SurfaceHeight/2),DXDraw.SurfaceHeight/DXDraw.SurfaceWidth,-1,1);
DXDraw.D3DDevice7.SetTransform(D3DTRANSFORMSTATE_PROJECTION, matProj );
// if Polygons=nil then exit;
// DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_CULLMODE, Ord(D3DCULL_NONE));
DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_MAGFILTER,ord(D3DTFG_ANISOTROPIC));
// DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_MINFILTER,ord(D3DTFN_ANISOTROPIC));
// DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_MIPFILTER,ord(D3DTFP_LINEAR));
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, 0);
DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_COLOROP,D3DTA_TEXTURE);
DXDraw.D3DDevice7.SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTA_TEXTURE);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_CULLMODE, Ord(D3DCULL_NONE));
{DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_ZWRITEENABLE,0);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_ZENABLE,0);}
if Objects=nil then exit;
with DXDraw,Surface.Canvas,D3DDevice7 do begin
DXDraw.D3DDevice7.BeginScene;
IGround:=-1;
ISky:=-1;
if FillPolygons and TopChecked and (Mode=mdView) then begin
if MapMode then IGround:=MForeGround
else begin
IGround:=SearchPic(Ground);
ISky:=SearchPic(Sky);
if IGround>=0 then
SetTexture(0,PC[IGround].Image.Texture.Surface.IDDSurface7);
end;
end;
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_COLORKEYENABLE, 0);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, 0);
r2.x1 := 0;
r2.y1 := 0;
r2.x2 := DXDraw.SurfaceWidth;
r2.y2 := DXDraw.SurfaceHeight;
DXDraw.D3DDevice7.Clear(1, r2, D3DCLEAR_STENCIL or D3DCLEAR_ZBUFFER, 0, 1, 0);
if (IGround<0) or (MapMode and (IGround>=0)) then begin
//Clear Screen
if IGround>=0 then
DXDraw.D3DDevice7.Clear(1, r2, D3DCLEAR_TARGET, SwapColors(MapColors[IGround]), 1, 0)
else DXDraw.D3DDevice7.Clear(1, r2, D3DCLEAR_TARGET, Self.Palette[lvBackground], 1, 0);
end else begin
w:=PC[IGround].Width;
h:=PC[IGround].Height;
Vertices[0]:=GetTC(0, 0,w,h,SlowSpeed);
Vertices[1]:=GetTC(DXDraw.SurfaceWidth-1, 0,w,h,SlowSpeed);
Vertices[2]:=GetTC(0, DXDraw.SurfaceHeight-1,w,h,SlowSpeed);
Vertices[3]:=GetTC(DXDraw.SurfaceWidth-1, DXDraw.SurfaceHeight-1,w,h,SlowSpeed);
DXDraw.D3DDevice7.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, Vertices[0], 4, 0);
end;
//Ôîíîâûé ðèñóíîê
with BGPicture do
if Texture<>nil then begin
r.Left:=GetOX(x1);
r.Right:=GetOX(x2);
r.Top:=GetOY(y1);
r.Bottom:=GetOY(y2);
GetVertices(srect,r,Texture.Surface.Width,Texture.Surface.Height,Vertices);
DXDraw.D3DDevice7.SetRenderState(D3DRENDERSTATE_COLORKEYENABLE, 0);
SetTexture(0,BGPicture.Texture.Surface.IDDSurface7);
DXDraw.D3DDevice7.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, Vertices[0], 4, 0);
end;
//end
//Grid
if ShowGrid then begin
Pen.Color:=SwapColors(Self.Palette[lvPoly]);
Pen.Style:=psDot;
Pen.Mode:=pmMergeNotPen;
Brush.Style:=bsClear;
for i:=Round((Center.x-Width/2/Zoom)/GridSize)-1 to Round((Center.x+Width/2/Zoom)/GridSize)+1 do begin
MoveTo(GetOX(i*GridSize),0);
LineTo(GetOX(i*GridSize),Height-1);
end;
for i:=Round((Center.y-Height/2/Zoom)/GridSize)-1 to Round((Center.y+Height/2/Zoom)/GridSize)+1 do begin
MoveTo(0,GetOY(i*GridSize));
LineTo(Width-1,GetOY(i*GridSize));
end;
Pen.Mode:=pmCopy;
Release;
end;
//end
//Ñîäðàíî èç èíåòà - âêëþ÷àåì çàïèñü â òðàôàðåò
{if Mode=mdView then begin
SetRenderState(D3DRENDERSTATE_STENCILENABLE,1);
SetRenderState(D3DRENDERSTATE_STENCILFUNC, D3DPCMPCAPS_ALWAYS);
SetRenderState(D3DRENDERSTATE_STENCILREF, $1 );
SetRenderState(D3DRENDERSTATE_STENCILMASK, $ffffffff );
SetRenderState(D3DRENDERSTATE_STENCILWRITEMASK,$ffffffff );
SetRenderState(D3DRENDERSTATE_STENCILZFAIL, D3DSTENCILCAPS_KEEP );
SetRenderState(D3DRENDERSTATE_STENCILFAIL, D3DSTENCILCAPS_KEEP );
SetRenderState(D3DRENDERSTATE_STENCILPASS, D3DSTENCILCAPS_REPLACE );
end;}
//Êîíåö
if ShowPolygons then
for i:=0 to High(Polygons) do DrawPolygon(@Polygons[i]);
//Ñîäðàíî èç èíåòà - ðèñóåì ñ èñïîëüçîâàíèåì òðàôàðåòà
{if Mode=mdView then begin
SetRenderState(D3DRENDERSTATE_STENCILENABLE,0);
SetRenderState(D3DRENDERSTATE_STENCILFUNC, D3DPCMPCAPS_EQUAL );
SetRenderState(D3DRENDERSTATE_STENCILPASS, D3DSTENCILCAPS_KEEP );
SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DPBLENDCAPS_DESTCOLOR );
SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DPBLENDCAPS_ZERO );
end;}
//Êîíåö
j:=High(Pics)+1;
//Õèòðûé ïîðÿäîê ðèñîâàíèå - â ýëìå òàêîé :(
i:=0;
while i<=High(Pics) do begin
old:=i;
if Pics[old].Distance>=500 then begin
while (Pics[i].Distance=Pics[old].Distance) do begin
inc(i);
if (i=High(Pics)+1) then break;
end;
for j:=i-1 downto old do DrawPicture(Pics[j]);
j:=0;
end else begin
j:=i;
break;
end;
end;
{if Mode=mdView then
SetRenderState(D3DRENDERSTATE_STENCILENABLE,0);
SetRenderState(D3DRENDERSTATE_STENCILENABLE,0);}
//DXDraw.Surface.Canvas.Release;
if ShowObjects then
for i:=0 to High(Objects) do DrawObject(Objects[i]);
i:=j;
while i<=High(Pics) do begin
old:=i;
while (Pics[i].Distance=Pics[old].Distance) do begin
inc(i);
if (i=High(Pics)+1) then break;
end;
for j:=i-1 downto old do DrawPicture(Pics[j]);
end;
//DXDraw.Surface.Canvas.Release;
if (Tool<>nil) and (Mode=mdEdit) then Tool.Draw;
if CrossHair then begin
GetCursorPos(p);
r:=ALEForm.PaintBox1.ClientRect;
r.TopLeft:=ALEForm.PaintBox1.ClientToScreen(r.TopLeft);
r.BottomRight:=ALEForm.PaintBox1.ClientToScreen(r.BottomRight);
if PtInRect(r,p) then begin
p2:=ALEForm.PaintBox1.ClientToScreen(Point(0,0));
p.x:=p.x-p2.x;
p.y:=p.y-p2.y;
Pen.Color:=Level.SwapColors(Self.Palette[lvCrosshair]);
Pen.Style:=psDot;
Brush.Style:=bsClear;
MoveTo(0,p.y);
LineTo(Width,p.y);
MoveTo(p.x,0);
LineTo(p.x,Height);
end;
end;
Release;
DXDraw.D3DDevice7.EndScene;
end;
DXDraw.Flip;
asm finit end;
//Canvas.Draw(0,0,Bitmap);
end;
procedure TLevel.DrawObject(Ob: TEObject);
const
OFaces=12;
var
r,n,i:integer;
srect,drect:TRect;
Tmp:TALEImage;
Vertices:array[0..OFaces] of TD3DVertex;
Color:Cardinal;
begin
with DXDraw.D3DDevice7{,DXDraw.Surface.Canvas},Ob do begin
Color:=$000000;
r:=round(Radius*Zoom);
if not MapMode or (Mode=mdEdit) then begin
Tmp.Texture:=nil;
drect:=Bounds(GetOX(Pos.x)-r,GetOY(Pos.y)-r,2*r,2*r);
srect:=Bounds(0,0,64,64);
end;
case typ of