forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyles.Base.pas
1139 lines (1014 loc) · 38.6 KB
/
Styles.Base.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 Styles.Base;
{$include lem_directives.inc}
// The abstract style system. descendants in Styles.Dos, Styles.User
interface
uses
Classes, SysUtils, Contnrs, Generics.Collections, Character,
GR32,
Base.Utils, Base.Bitmaps,
Dos.Consts, Dos.Compression, Dos.Bitmaps, Dos.Structures,
Meta.Structures,
Level.Base, Level.Hash, Level.Loader,
Prog.Types, Prog.Base, Prog.Data;
type
TStyle = class;
TLemmingAnimationSet = class;
TGraphicSet = class;
TLevelSystem = class;
TSection = class;
TLevelLoadingInformation = class;
TLevelSystemClass = class of TLevelSystem;
TSectionList = class(TFastObjectList<TSection>);
TLevelLoadingInformationList = class(TFastObjectList<TLevelLoadingInformation>);
TStyle = class abstract
private
fIsByFactory: Boolean;
function GetRootPath: string; inline;
strict protected
fMainDatFileName : string; // main.dat
fLemmingAnimationSet : TLemmingAnimationSet;
fStyleInformation : Consts.TStyleInformation;
fLevelSystem : TLevelSystem;
fMechanics : TMechanics;
fName : string;
fDef : TStyleDef;
fIsPooledByFactory : Boolean;
protected
function GetLevelSystemClass: TLevelSystemClass; virtual; abstract;
function GetMechanics: TMechanics; virtual; abstract;
function GetMainDatFileName: string; virtual; // default 'main.dat'
public
constructor Create(const aName: string); virtual;
procedure BeforeDestruction; override; final;
destructor Destroy; override;
procedure AfterConstruction; override; final;
property StyleInformation: Consts.TStyleInformation read fStyleInformation;
property MainDatFileName: string read fMainDatFileName; // default 'main.dat'
property LemmingAnimationSet: TLemmingAnimationSet read fLemmingAnimationSet;
property LevelSystem: TLevelSystem read fLevelSystem;
property Mechanics: TMechanics read fMechanics;
property Name: string read fName;
property RootPath: string read GetRootPath;
property Def: TStyleDef read fDef;
property IsPooledByFactory: Boolean read fIsByFactory write fIsPooledByFactory;
end;
TLemmingAnimationSet = class sealed
public
const
LTR = False;
RTL = True;
const
{-------------------------------------------------------------------------------
dos animations ordered by their appearance in main.dat
the constants below show the exact order
-------------------------------------------------------------------------------}
WALKING = 0;
JUMPING = 1;
WALKING_RTL = 2;
JUMPING_RTL = 3;
DIGGING = 4;
CLIMBING = 5;
CLIMBING_RTL = 6;
DROWNING = 7;
HOISTING = 8;
HOISTING_RTL = 9;
BRICKLAYING = 10;
BRICKLAYING_RTL = 11;
BASHING = 12;
BASHING_RTL = 13;
MINING = 14;
MINING_RTL = 15;
FALLING = 16;
FALLING_RTL = 17;
UMBRELLA = 18;
UMBRELLA_RTL = 19;
SPLATTING = 20;
EXITING = 21;
FRIED = 22;
BLOCKING = 23;
SHRUGGING = 24;
SHRUGGING_RTL = 25;
OHNOING = 26;
EXPLOSION = 27;
AnimationIndices : array[TLemmingAction, LTR..RTL] of Integer = (
(0, 0),
(WALKING, WALKING_RTL), // baWalk,
(JUMPING, JUMPING_RTL), // baJumping,
(DIGGING, DIGGING), // baDigging,
(CLIMBING, CLIMBING_RTL), // baClimbing,
(DROWNING, DROWNING), // baDrowning,
(HOISTING, HOISTING_RTL), // baHoisting,
(BRICKLAYING, BRICKLAYING_RTL), // baBricklaying,
(BASHING, BASHING_RTL), // baBashing,
(MINING, MINING_RTL), // baMining,
(FALLING, FALLING_RTL), // baFalling,
(UMBRELLA, UMBRELLA_RTL), // baUmbrella,
(SPLATTING, SPLATTING), // baSplatting,
(EXITING, EXITING), // baExiting,
(FRIED, FRIED), // baFried,
(BLOCKING, BLOCKING), // baBlocking,
(SHRUGGING, SHRUGGING_RTL), // baShrugging,
(OHNOING, OHNOING), // baOhnoing,
(EXPLOSION, EXPLOSION) // baExploding
);
strict private
fStyle : TStyle; // owner style
// metadata
fMetaLemmingAnimationList : TMetaLemmingAnimationList;
fMetaExtraAnimationList : TMetaExtraAnimationList;
// data
fLemmingBitmaps : TBitmaps;
fExtraBitmaps : TBitmaps;
fAnimationPalette : TArrayOfColor32;
fExplosionMaskBitmap : TBitmap32; // ref
fBashMasksBitmap : TBitmap32; // ref
fBashMasksRTLBitmap : TBitmap32; // ref
fMineMasksBitmap : TBitmap32; // ref
fMineMasksRTLBitmap : TBitmap32; // ref
fCountDownDigitsBitmap : TBitmap32; // ref
procedure InitMetadata;
public
constructor Create(aStyle: TStyle);
destructor Destroy; override;
procedure Load;
property Style: TStyle read fStyle;
property MetaLemmingAnimationList: TMetaLemmingAnimationList read fMetaLemmingAnimationList;
property MetaExtraAnimationList: TMetaExtraAnimationList read fMetaExtraAnimationList;
property LemmingBitmaps: TBitmaps read fLemmingBitmaps;
// easy references, these point to the MaskExtraAnimationList
property ExplosionMaskBitmap : TBitmap32 read fExplosionMaskBitmap;
property BashMasksBitmap : TBitmap32 read fBashMasksBitmap;
property BashMasksRTLBitmap : TBitmap32 read fBashMasksRTLBitmap;
property MineMasksBitmap : TBitmap32 read fMineMasksBitmap;
property MineMasksRTLBitmap : TBitmap32 read fMineMasksRTLBitmap;
property CountDownDigitsBitmap : TBitmap32 read fCountDownDigitsBitmap;
property AnimationPalette : TArrayOfColor32 read fAnimationPalette write fAnimationPalette;
end;
TGraphicSet = class sealed
strict private
fStyle : TStyle; // ref to style
// metadata
fGraphicSetId : Integer; // number identifier
fGraphicSetIdExt : Integer; // extended graphics identifier (vgaspec)
fMetaDataFile : string; // ground?.dat
fGraphicFile : string; // vgagr?.dat
fGraphicExtFile : string; // vgaspec?.dat
fMetaObjectList : TMetaObjectList;
fMetaTerrainList : TMetaTerrainList;
// data
fTerrainBitmaps : TBitmaps;
fObjectBitmaps : TBitmaps;
fSpecialBitmap : TBitmap32;
fPaletteCustom : TArrayOfColor32; // 8
fPaletteStandard : TArrayOfColor32; // 8
fPalettePreview : TArrayOfColor32; // 8
fPalette : TArrayOfColor32; // 16
fBrickColor : TColor32;
procedure Clear;
procedure LoadMetaData;
procedure LoadData;
public
constructor Create(aStyle: TStyle);
destructor Destroy; override;
procedure Load(aId, aIdExt: Integer);
// properties
property Style: TStyle read fStyle;
property GraphicSetId: Integer read fGraphicSetId;
property GraphicSetIdExt: Integer read fGraphicSetIdExt;
property MetaTerrainList: TMetaTerrainList read fMetaTerrainList;
property MetaObjectList: TMetaObjectList read fMetaObjectList;
property TerrainBitmaps: TBitmaps read fTerrainBitmaps;
property SpecialBitmap: TBitmap32 read fSpecialBitmap;
property ObjectBitmaps: TBitmaps read fObjectBitmaps;
property PaletteCustom: TArrayOfColor32 read fPaletteCustom;
property PaletteStandard: TArrayOfColor32 read fPaletteStandard;
property PalettePreview: TArrayOfColor32 read fPalettePreview;
property Palette: TArrayOfColor32 read fPalette;
property BrickColor: TColor32 read fBrickColor;
end;
TLevelSystem = class
strict private
fStyle: TStyle; // owner
fSectionList: TSectionList;
private
procedure DoAddSection(aSection: TSection); // internal
strict protected
fOddTableFileName : string;
protected
procedure GetFileNamesForGraphicSet(aGraphicSetId, aGraphicSetIdExt: Integer; out aMetaDataFileName, aGraphicsFileName, aSpecialGraphicsFileName: string); virtual;
procedure DoInitializeLevelSystem; virtual;
public
constructor Create(aStyle: TStyle); virtual;
destructor Destroy; override;
procedure AfterConstruction; override; final;
// default graphics filenames
class procedure GetDefaultNamesForGraphics(aGraphicSetId, aGraphicSetIdExt: Integer; out aMetaDataFileName, aGraphicsFileName, aSpecialGraphicsFileName: string); static;
// level finding
function FirstLevelOrDefault: TLevelLoadingInformation; inline;
function FirstLevel: TLevelLoadingInformation; inline;
function LastLevel: TLevelLoadingInformation; inline;
function FindLevelByIndex(const aSectionIndex, aLevelIndex: Integer): TLevelLoadingInformation;
function FindLevelBySectionNameAndNumber(const aCode: string): TLevelLoadingInformation;
function SelectRandomLevel: TLevelLoadingInformation;
// properties
property OddTableFileName: string read fOddTableFileName;
property Style: TStyle read fStyle;
property SectionList: TSectionList read fSectionList;
end;
TSection = class sealed
private
fLevelSystem: TLevelSystem; // owner
fSectionIndex: Integer;
fSectionName: string;
fPrev: TSection;
fNext: TSection;
fLevelLoadingInformationList: TLevelLoadingInformationList;
function GetStyle: TStyle; inline;
procedure DoAddLevelInfo(aLevelInfo: TLevelLoadingInformation);
public
constructor Create(aLevelSystem: TLevelSystem);
destructor Destroy; override;
property Style: TStyle read GetStyle;
property SectionIndex: Integer read fSectionIndex;
property SectionName: string read fSectionName write fSectionName;
property LevelSystem: TLevelSystem read fLevelSystem;
property LevelLoadingInformationList: TLevelLoadingInformationList read fLevelLoadingInformationList;
property Prev: TSection read fPrev;
property Next: TSection read fNext;
end;
TLevelLoadingInformation = class sealed
private
fSection : TSection;
fLevelIndex : Integer;
fSourceFileName : string; // loading info
fSectionIndexInSourceFile : Integer; // loading info
fUseOddTable : Boolean; // loading info
fOddTableIndex : Integer; // loading info
fIsRawLVLFile : Boolean; // loading info
fIsLemminiFile : Boolean; // loading info
fPrev : TLevelLoadingInformation;
fNext : TLevelLoadingInformation;
fCachedLVL : PLVLRec;
fCachedHash : UInt64;
fCachedLevelCode : string;
fMusicFileName : string;
fMusicStreamType : TMusicStreamType;
function GetStyle: TStyle; inline;
function GetLevelSystem: TLevelSystem; inline;
function GetSectionIndex: Integer; inline;
function GetIsCached: Boolean; inline;
procedure CacheLVL; inline;
procedure SetMusicFilename(const s: string);
public
constructor Create(aSection: TSection);
destructor Destroy; override;
procedure LoadLevel(aLevel: TLevel);
function GetLevelHash: UInt64;
function GetLevelCode: string;
function GetRawLVL: TLVLRec;
function GetRawLVLTitle: TLVLTitle;
function GetLevelTitle(trimmed: Boolean = True): string;
// properties
property Style: TStyle read GetStyle;
property LevelSystem: TLevelSystem read GetLevelSystem;
property CachedLVL: PLVLRec read fCachedLVL;
property Section: TSection read fSection;
property SectionIndex: Integer read GetSectionIndex;
property LevelIndex: Integer read fLevelIndex;
property SourceFileName: string read fSourceFileName write fSourceFileName; // loading info
property SectionIndexInSourceFile: Integer read fSectionIndexInSourceFile write fSectionIndexInSourceFile; // loading info
property UseOddTable: Boolean read fUseOddTable write fUseOddTable; // loading info
property OddTableIndex: Integer read fOddTableIndex write fOddTableIndex; // loading info
property IsRawLVLFile: Boolean read fIsRawLVLFile write fIsRawLVLFile; // loading info
property IsLemminiFile: Boolean read fIsLemminiFile write fIsLemminiFile; // loading info
property MusicFileName: string read fMusicFileName write SetMusicFileName; // loading info
property MusicStreamType: TMusicStreamType read fMusicStreamType; // loading info
property Prev: TLevelLoadingInformation read fPrev;
property Next: TLevelLoadingInformation read fNext;
property IsCached: Boolean read GetIsCached;
end;
type
TLevelFactory = class sealed
private
class procedure LoadLVL(aInfo: TLevelLoadingInformation; var LVL: TLVLRec); static;
end;
implementation
{ TStyle }
procedure TStyle.BeforeDestruction;
begin
if fIsPooledByFactory then
Throw('Pooled style destruction error', 'BeforeDestruction')
end;
constructor TStyle.Create(const aName: string);
var
info: Consts.TStyleInformation;
begin
if GetLevelSystemClass = nil then
Throw('TStyle.Create (' + ClassName + '): undefined levelsystemclass');
// name and def first
fName := aName;
// set the styledef
for info in Consts.StyleInformationlist do
if info.Name = fName then begin
fStyleInformation := info;
fDef := info.StyleDef;
Break;
end;
end;
procedure TStyle.AfterConstruction;
begin
inherited AfterConstruction;
fMainDatFileName := GetMainDatFileName;
fMechanics := GetMechanics;
fLemmingAnimationSet := TLemmingAnimationSet.Create(Self);
fLevelSystem := GetLevelSystemClass.Create(Self);
end;
destructor TStyle.Destroy;
begin
fLevelSystem.Free;
fLemmingAnimationSet.Free;
inherited;
end;
function TStyle.GetMainDatFileName: string;
begin
Result := 'main.dat';
end;
function TStyle.GetRootPath: string;
begin
Result := Consts.PathToStyle[fName];
end;
{ TLemmingAnimationSet }
constructor TLemmingAnimationSet.Create(aStyle: TStyle);
begin
fStyle := aStyle;
fMetaLemmingAnimationList := TMetaLemmingAnimationList.Create;
fMetaExtraAnimationList := TMetaExtraAnimationList.Create;
fLemmingBitmaps := TBitmaps.Create;
fExtraBitmaps := TBitmaps.Create;
InitMetaData;
end;
destructor TLemmingAnimationSet.Destroy;
begin
fMetaLemmingAnimationList.Free;
fMetaExtraAnimationList.Free;
fLemmingBitmaps.Free;
fExtraBitmaps.Free;
inherited;
end;
procedure TLemmingAnimationSet.InitMetadata;
{-------------------------------------------------------------------------------
We dont have to read. It's fixed in this order in the main.dat.
foot positions from ccexpore's emails, see lemming_mechanics pseudo code
o make lemming animations
o make mask animations metadata
-------------------------------------------------------------------------------}
procedure Lem(aImageLocation: Integer; const aDescription: string; aFrameCount, aWidth, aHeight, aBPP, aFootX, aFootY: Integer; aAnimType: TLemmingAnimationType);
var
A: TMetaLemmingAnimation;
begin
A := TMetaLemmingAnimation.Create(aDescription, aFrameCount, aWidth, aHeight, aBPP, aImageLocation, aAnimType, aFootX, aFootY);
fMetaLemmingAnimationList.Add(A);
end;
procedure Msk(aImageLocation: Integer; const aDescription: string; aFrameCount, aWidth, aHeight, aBPP: Integer);
var
M: TMetaExtraAnimation;
begin
M := TMetaExtraAnimation.Create(aDescription, aFrameCount, aWidth, aHeight, aBPP, aImageLocation);
fMetaExtraAnimationList.Add(M);
end;
begin
// place description F W H BPP FX FY animationtype
Lem($0000, 'Walking' , 8, 16, 10, 2, 8, 10, TLemmingAnimationType.Loop); // 0
Lem($0140, 'Jumping' , 1, 16, 10, 2, 8, 10, TLemmingAnimationType.Once);
Lem($0168, 'Walking (rtl)' , 8, 16, 10, 2, 8, 10, TLemmingAnimationType.Loop);
Lem($02A8, 'Jumping (rtl)' , 1, 16, 10, 2, 8, 10, TLemmingAnimationType.Once);
Lem($02D0, 'Digging' , 16, 16, 14, 3, 8, 12, TLemmingAnimationType.Loop);
Lem($0810, 'Climbing' , 8, 16, 12, 2, 8, 12, TLemmingAnimationType.Loop);
Lem($0990, 'Climbing (rtl)' , 8, 16, 12, 2, 8, 12, TLemmingAnimationType.Loop);
Lem($0B10, 'Drowning' , 16, 16, 10, 2, 8, 10, TLemmingAnimationType.Once);
Lem($0D90, 'Hoisting' , 8, 16, 12, 2, 8, 12, TLemmingAnimationType.Once);
Lem($0F10, 'Hoisting (rtl)' , 8, 16, 12, 2, 8, 12, TLemmingAnimationType.Once);
Lem($1090, 'Building' , 16, 16, 13, 3, 8, 13, TLemmingAnimationType.Loop); // 10
Lem($1570, 'Building (rtl)' , 16, 16, 13, 3, 8, 13, TLemmingAnimationType.Loop);
Lem($1A50, 'Bashing' , 32, 16, 10, 3, 8, 10, TLemmingAnimationType.Loop);
Lem($21D0, 'Bashing (rtl)' , 32, 16, 10, 3, 8, 10, TLemmingAnimationType.Loop);
Lem($2950, 'Mining' , 24, 16, 13, 3, 8, 13, TLemmingAnimationType.Loop);
Lem($30A0, 'Mining (rtl)' , 24, 16, 13, 3, 8, 13, TLemmingAnimationType.Loop);
Lem($37F0, 'Falling' , 4, 16, 10, 2, 8, 10, TLemmingAnimationType.Loop);
Lem($3890, 'Falling (rtl)' , 4, 16, 10, 2, 8, 10, TLemmingAnimationType.Loop);
Lem($3930, 'Umbrella' , 8, 16, 16, 3, 8, 16, TLemmingAnimationType.Loop);
Lem($3C30, 'Umbrella (rtl)' , 8, 16, 16, 3, 8, 16, TLemmingAnimationType.Loop);
Lem($3F30, 'Splatting' , 16, 16, 10, 2, 8, 10, TLemmingAnimationType.Once); // 20
Lem($41B0, 'Exiting' , 8, 16, 13, 2, 8, 13, TLemmingAnimationType.Once);
Lem($4350, 'Vaporizing' , 14, 16, 14, 4, 8, 14, TLemmingAnimationType.Once);
Lem($4970, 'Blocking' , 16, 16, 10, 2, 8, 10, TLemmingAnimationType.Loop);
Lem($4BF0, 'Shrugging' , 8, 16, 10, 2, 8, 10, TLemmingAnimationType.Once);
Lem($4D30, 'Shrugging (rtl)' , 8, 16, 10, 2, 8, 10, TLemmingAnimationType.Once);
Lem($4E70, 'Oh-No-ing' , 16, 16, 10, 2, 8, 10, TLemmingAnimationType.Once);
Lem($50F0, 'Exploding' , 1, 32, 32, 3, 16, 25, TLemmingAnimationType.Once); // 27
// place description F W H BPP
Msk($0000, 'Bashmasks' , 4, 16, 10, 1);
Msk($0050, 'Bashmasks (rtl)' , 4, 16, 10, 1);
Msk($00A0, 'Minemasks' , 2, 16, 13, 1);
Msk($00D4, 'Minemasks (rtl)' , 2, 16, 13, 1);
Msk($0108, 'Explosionmask' , 1, 16, 22, 1);
Msk($0154, 'Countdown digits' , 5, 8, 8, 1); // we only take the digits 5 downto zero
end;
procedure TLemmingAnimationSet.Load;
var
Fn: string;
Mem: TMemoryStream;
Bmp: TBitmap32;
TempBitmap: TBitmap32;
Sections: TDosDatSectionList;
Decompressor: TDosDatDecompressor;
iFrame: Integer;
MLA: TMetaLemmingAnimation;
MA: TMetaExtraAnimation;
Y: Integer;
Pal: TArrayOfColor32;
LocalStream: TStream;
begin
fLemmingBitmaps.Clear;
fExtraBitmaps.Clear;
// fried and or vaporizing have high color indices
Assert(Length(AnimationPalette) >= 16);
Pal := Copy(fAnimationPalette);
Fn := Style.MainDatFileName;
TempBitmap := TBitmap32.Create;
Sections := TDosDatSectionList.Create;
Decompressor := TDosDatDecompressor.Create;
try
LocalStream := TData.CreateDataStream(Style.Name, Fn, TDataType.LemmingData);
try
Decompressor.LoadSectionList(LocalStream, Sections, False);
finally
LocalStream.Free;
end;
Decompressor.DecompressSection(Sections[0].CompressedData, Sections[0].DecompressedData);
Mem := Sections[0].DecompressedData;
for MLA in fMetaLemmingAnimationList do begin
Bmp := TBitmap32.Create;
fLemmingBitmaps.Add(Bmp);
TDosPlanarBitmap.LoadAnimationFromStream(Mem, Bmp, MLA.ImageLocation, MLA.Width, MLA.Height, MLA.FrameCount, MLA.BitsPerPixel, Pal);
//bmp.ReplaceColor(Color32(64,64,224), cllime32);
end;
Pal[0] := 0;
Pal[1] := clMask32; // the ugly mask pink.
Decompressor.DecompressSection(Sections[1].CompressedData, Sections[1].DecompressedData);
Mem := Sections[1].DecompressedData;
for MA in fMetaExtraAnimationList do begin
Y := 0;
Bmp := TBitmap32.Create;
fExtraBitmaps.Add(Bmp);
// also: here the direct LoadAnimationFromStream does not work correctly. there are masks inbetween i think.
Bmp.Width := MA.Width;
Bmp.Height := MA.FrameCount * MA.Height;
Bmp.Clear(0);
Mem.Seek(MA.ImageLocation, soFromBeginning);
for iFrame := 0 to MA.FrameCount - 1 do
begin
TDosPlanarBitmap.LoadFromStream(Mem, TempBitmap, -1, MA.Width, MA.Height, MA.BitsPerPixel, Pal);
TempBitmap.DrawTo(Bmp, 0, Y);
Inc(Y, MA.Height);
end;
end;
// refer the "easy access" bitmaps
fBashMasksBitmap := fExtraBitmaps[0];
fBashMasksRTLBitmap := fExtraBitmaps[1];
fMineMasksBitmap := fExtraBitmaps[2];
fMineMasksRTLBitmap := fExtraBitmaps[3];
fExplosionMaskBitmap := fExtraBitmaps[4];
fCountDownDigitsBitmap := fExtraBitmaps[5];
fCountDownDigitsBitmap.ReplaceColor(clMask32, Pal[3]);
finally
Sections.Free;
Decompressor.Free;
TempBitmap.Free;
end;
end;
{ TGraphicSet }
constructor TGraphicSet.Create(aStyle: TStyle);
begin
fStyle := aStyle;
fGraphicSetId := -1;
fGraphicSetIdExt := -1;
fMetaObjectList := TMetaObjectList.Create;
fMetaTerrainList := TMetaTerrainList.Create;
fTerrainBitmaps := TBitmaps.Create;
fObjectBitmaps := TBitmaps.Create;
fSpecialBitmap := TBitmap32.Create;
end;
destructor TGraphicSet.Destroy;
begin
fMetaObjectList.Free;
fMetaTerrainList.Free;
fTerrainBitmaps.Free;
fObjectBitmaps.Free;
fSpecialBitmap.Free;
inherited;
end;
procedure TGraphicSet.Clear;
begin
fMetaObjectList.Clear;
fMetaTerrainList.Clear;
fTerrainBitmaps.Clear;
fObjectBitmaps.Clear;
fSpecialBitmap.SetSize(0, 0);
SetLength(fPaletteCustom, 0);
SetLength(fPaletteStandard, 0);
SetLength(fPalettePreview, 0);
SetLength(fPalette, 0);
fBrickColor := 0;
fMetaDataFile := '';
fGraphicFile := '';
fGraphicExtFile := '';
end;
procedure TGraphicSet.LoadMetaData;
// Read dos metadata and translate to lemmix classes
procedure LoadDosMetaData(var aData: TDosGroundRec);
var
D: TStream;
begin
D := TData.CreateDataStream(Style.Name, fMetaDataFile, TDataType.LevelGraphics);
try
D.ReadBuffer(aData, SizeOf(aData));
finally
D.Free;
end;
end;
function LoadSpecPalette: TDosVGAPalette8;
var
SpecBmp: TVgaSpecBitmap;
DataStream: TStream;
begin
SpecBmp := TVgaSpecBitmap.Create;
DataStream := TData.CreateDataStream(Style.Name, fGraphicExtFile, TDataType.LevelSpecialGraphics);
try
SpecBmp.LoadPaletteFromStream(DataStream, Result);
finally
SpecBmp.Free;
DataStream.Free;
end;
end;
procedure AssemblePalette;
// Concatenate the fixed palette and the loaded custompalette.
// Then copy the first customcolor to the last fixed color (bridges, minimap).
// For special graphics we use a hardcoded color for now.
var
i: Integer;
begin
SetLength(fPalette, 16);
for i := 0 to 7 do
fPalette[i] := DosPaletteEntryToColor32(DosInlevelPalettes[Consts.ChristmasPalette][i]);
for i := 8 to 15 do
fPalette[i] := fPaletteCustom[i - 8];
if fGraphicSetIdExt > 0 then
fPalette[8] := Color32(124, 124, 0, 0);
fPalette[7] := fPalette[8];
fBrickColor := fPalette[7];
end;
var
DosData: TDosGroundRec;
O: TDosMetaObject;
MO: TMetaObject;
T: TDosMetaTerrain;
MT: TMetaTerrain;
begin
LoadDosMetaData(DosData);
// convert palettes
DosVgaPalette8ToLemmixPalette(DosData.VGA_PaletteCustom, fPaletteCustom);
DosVgaPalette8ToLemmixPalette(DosData.VGA_PaletteStandard, fPaletteStandard);
DosVgaPalette8ToLemmixPalette(DosData.VGA_PalettePreview, fPalettePreview);
// if special graphic then overwrite the custom palette
if fGraphicSetIdExt > 0 then
DosVgaPalette8ToLemmixPalette(LoadSpecPalette, fPaletteCustom);
// make 16 color palette
AssemblePalette;
// meta objects
for O in DosData.ObjectInfoArray do begin
if O.oWidth = 0 then
Break; // the rest is empty
MO := TMetaObject.Create;
fMetaObjectList.Add(MO);
MO.AssignFromDos(O);
end;
// if extended graphic then no terrain
if fGraphicSetIdExt > 0 then
Exit;
// meta terrains
for T in DosData.TerrainInfoArray do begin
if T.tWidth = 0 then
Break; // the rest is empty
MT := TMetaTerrain.Create;
fMetaTerrainList.Add(MT);
MT.AssignFromDos(T);
end;
end;
procedure TGraphicSet.LoadData;
// read all terrain- and object bitmaps from dos file and translate to lemmix classes
var
DosSections: TDosDatSectionList;
Decompressor: TDosDatDecompressor;
MemStream: TMemoryStream;
DataStream: TStream;
MT: TMetaTerrain;
Bmp: TBitmap32;
SpecBmp: TVgaSpecBitmap;
MO: TMetaObject;
y: Integer;
Loc: Integer;
framebitmap: TBitmap32;
f: Integer;
begin
DosSections := TDosDatSectionList.Create;
try
// first decompress all data into the sectionlist
DataStream := nil;
Decompressor := TDosDatDecompressor.Create;
try
DataStream := TData.CreateDataStream(Style.Name, fGraphicFile, TDataType.LevelGraphics);
Decompressor.LoadSectionList(DataStream, DosSections, True);
finally
Decompressor.Free;
DataStream.Free;
end;
// get terrains from the first section if this is a normal graphicset (#0)
if fGraphicSetIdExt <= 0 then begin
MemStream := DosSections[0].DecompressedData;
for MT in fMetaTerrainList do begin
Bmp := TBitmap32.Create;
fTerrainBitmaps.Add(Bmp);
TDosPlanarBitmap.LoadFromStream(MemStream, Bmp, MT.ImageLocation, MT.Width, MT.Height, 4, fPalette);
end;
end
// or get the one terrainbitmap from the vgaspec
else begin
SpecBmp := TVgaSpecBitmap.Create;
try
DataStream := TData.CreateDataStream(Style.Name, fGraphicExtFile, TDataType.LevelSpecialGraphics);
try
SpecBmp.LoadFromStream(DataStream, fSpecialBitmap);
finally
DataStream.Free;
end;
finally
SpecBmp.Free;
end;
end;
// get objects from the second section (#1)
// loadanimationfromstream is not really impossible. maybe there are gaps, maybe some offset error
// get objects from the second section
MemStream := DosSections[1].DecompressedData;
for MO in fMetaObjectList do begin
Bmp := TBitmap32.Create;
Bmp.SetSize(MO.Width, MO.Height * MO.AnimationFrameCount);
fObjectBitmaps.Add(Bmp);
y := 0;
Loc := MO.AnimationFramesBaseLoc;
framebitmap := TBitmap32.Create;
// load all animation frames and glue together
for f := 0 to MO.AnimationFrameCount - 1 do begin
TDosPlanarBitmap.LoadFromStream(MemStream, FrameBitmap, Loc, MO.Width, MO.Height, 4, fPalette);
FrameBitmap.DrawTo(Bmp, 0, Y);
Inc(Y, MO.Height);
Inc(Loc, MO.AnimationFrameDataSize);
end;
framebitmap.Free;
end;
finally
DosSections.Free;
end;
end;
procedure TGraphicSet.Load(aId, aIdExt: Integer);
begin
Clear;
fGraphicSetId := aId;
fGraphicSetIdExt := aIdExt;
// get the filenames from which we load the metadata and data
Style.LevelSystem.GetFileNamesForGraphicSet(fGraphicSetId, fGraphicSetIdExt, {out} fMetaDataFile, {out} fGraphicFile, {out} fGraphicExtFile);
LoadMetaData;
LoadData;
end;
{ TLevelSystem }
constructor TLevelSystem.Create(aStyle: TStyle);
begin
fStyle := aStyle;
fSectionList := TSectionList.Create;
end;
procedure TLevelSystem.AfterConstruction;
var
section: TSection;
begin
inherited AfterConstruction;
DoInitializeLevelSystem; // let descendant implement sections and levels
// do a little validation on the descendants, who are responsibly for filling sections and levelinformation
if SectionList.IsEmpty then
Throw('No game sections implemented', 'AfterConstruction');
for section in Sectionlist do
if section.LevelLoadingInformationList.IsEmpty then
Throw('No levelinformation found in one of the sections', 'AfterConstruction');
end;
destructor TLevelSystem.Destroy;
begin
fSectionList.Free;
inherited;
end;
class procedure TLevelSystem.GetDefaultNamesForGraphics(aGraphicSetId, aGraphicSetIdExt: Integer; out aMetaDataFileName, aGraphicsFileName, aSpecialGraphicsFileName: string);
// all our 6 built in styles use the same naming convention
begin
aMetaDataFileName := 'ground' + aGraphicSetId.ToString + 'o.dat';
aGraphicsFileName := 'vgagr' + aGraphicSetId.ToString + '.dat';
aSpecialGraphicsFileName := '';
if aGraphicSetIdExt > 0 then
aSpecialGraphicsFileName := 'vgaspec' + pred(aGraphicSetIdExt).ToString + '.dat' // 1 maps to 0 for filename
end;
procedure TLevelSystem.GetFileNamesForGraphicSet(aGraphicSetId, aGraphicSetIdExt: Integer; out aMetaDataFileName, aGraphicsFileName, aSpecialGraphicsFileName: string);
// this is default and only overwritten for userstyle if needed
begin
GetDefaultNamesForGraphics(aGraphicSetId, aGraphicSetIdExt, aMetaDataFileName, aGraphicsFileName, aSpecialGraphicsFileName);
end;
procedure TLevelSystem.DoAddSection(aSection: TSection);
var
ix: Integer;
thePrev: TSection;
begin
aSection.fSectionIndex := fSectionList.Count;
fSectionList.Add(aSection);
ix := aSection.fSectionIndex;
if ix > 0 then begin
thePrev := fSectionList[ix - 1];
thePrev.fNext := aSection;
aSection.fPrev := thePrev;
end
else begin
thePrev := aSection.fPrev;
if Assigned(thePrev) and thePrev.fLevelLoadingInformationList.HasItems then begin
thePrev.fNext := aSection;
aSection.fPrev := thePrev;
end;
end;
end;
procedure TLevelSystem.DoInitializeLevelSystem;
begin
// must override
end;
function TLevelSystem.FirstLevelOrDefault: TLevelLoadingInformation;
begin
if SectionList.IsEmpty or SectionList.First.LevelLoadingInformationList.IsEmpty then
Exit(nil);
Result := SectionList.First.LevelLoadingInformationList.First;
end;
function TLevelSystem.FirstLevel: TLevelLoadingInformation;
begin
{$ifdef paranoid}
if SectionList.IsEmpty or SectionList.First.LevelLoadingInformationList.IsEmpty then
Throw('No levels in system');
{$endif}
Result := SectionList.First.LevelLoadingInformationList.First;
end;
function TLevelSystem.LastLevel: TLevelLoadingInformation;
begin
{$ifdef paranoid}
if SectionList.IsEmpty or SectionList.Last.LevelLoadingInformationList.IsEmpty then
Throw('No levels in system');
{$endif}
Result := SectionList.Last.LevelLoadingInformationList.Last;
end;
function TLevelSystem.FindLevelByIndex(const aSectionIndex, aLevelIndex: Integer): TLevelLoadingInformation;
begin
if (aSectionIndex < fSectionList.Count) and (aLevelIndex < fSectionList[aSectionIndex].LevelLoadingInformationList.Count) then
Result := fSectionList[aSectionIndex].LevelLoadingInformationList[aLevelIndex]
else
Result := nil;
end;
function TLevelSystem.FindLevelBySectionNameAndNumber(const aCode: string): TLevelLoadingInformation;
var
sectionstring: string;
levelstring: string;
foundSection: TSection;
charsFound, digitsFound: Boolean;
C: Char;
section: TSection;
ix: Integer;
begin
Result := nil;
foundSection := nil;
charsFound := False;
digitsFound := False;
if Length(aCode) < 2 then
Exit;
sectionstring := '';
levelstring := '';
for C in aCode do
if IsLetter(C) then begin
if digitsFound then
Exit;
charsFound := True;
sectionstring := sectionstring + C;
end
else if IsDigit(C) then begin
if not CharsFound then
Exit;
digitsFound := True;
levelstring := levelstring + C;
end;
for section in SectionList do
if SameText(section.SectionName, sectionstring) then begin
foundSection := section;
Break;
end;
if foundSection = nil then
Exit;
if not TryStrToInt(levelstring, ix) then
Exit;
if (ix < 1) or (ix > foundSection.LevelLoadingInformationList.Count) then
Exit;
Dec(ix);
Result := foundSection.LevelLoadingInformationList[ix];
end;
function TLevelSystem.SelectRandomLevel: TLevelLoadingInformation;
var
s, l: Integer;
begin
s := Random(fSectionList.Count);
l := Random(fSectionList[s].fLevelLoadingInformationList.Count);
Result := FindLevelByIndex(s, l);
end;
{ TSection }
constructor TSection.Create(aLevelSystem: TLevelSystem);
begin
fLevelSystem := aLevelSystem;
fLevelLoadingInformationList := TLevelLoadingInformationList.Create;
fLevelSystem.DoAddSection(Self);
end;
destructor TSection.Destroy;
begin
fLevelLoadingInformationList.Free;
inherited;
end;
procedure TSection.DoAddLevelInfo(aLevelInfo: TLevelLoadingInformation);
var
ix: Integer;
thePrev: TLevelLoadingInformation;
begin
aLevelInfo.fLevelIndex := fLevelLoadingInformationList.Count;
fLevelLoadingInformationList.Add(aLevelInfo);
ix := aLevelInfo.fLevelIndex;
if ix > 0 then begin
thePrev := fLevelLoadingInformationList[ix - 1];
thePrev.fNext := aLevelInfo;
aLevelInfo.fPrev := thePrev;
end
else begin
if Assigned(fPrev) and fPrev.LevelLoadingInformationList.HasItems then begin
thePrev := fPrev.LevelLoadingInformationList.Last;
thePrev.fNext := aLevelInfo;
aLevelInfo.fPrev := thePrev;
end;
end;
end;
function TSection.GetStyle: TStyle;
begin
Result := fLevelSystem.Style;
end;
{ TLevelLoadingInformation }
constructor TLevelLoadingInformation.Create(aSection: TSection);
begin
fSection := aSection;
fSection.DoAddLevelInfo(Self);
end;
destructor TLevelLoadingInformation.Destroy;
begin
if fCachedLVL <> nil then
Dispose(fCachedLVL);
inherited;
end;
procedure TLevelLoadingInformation.SetMusicFilename(const s: string);
var
ext: string;
begin
fMusicFileName := s;
ext := ToUpper(ExtractFileExt(s));
if ext = '.MOD' then
fMusicStreamType := TMusicStreamType.&MOD
else if ext = '.MP3' then
fMusicStreamType := TMusicStreamType.MP3
else