forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.pas
4321 lines (3849 loc) · 143 KB
/
Game.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 Game;
{$include lem_directives.inc}
interface
uses
LCLIntf,
Types, Classes, Contnrs, SysUtils, Math, TypInfo, Generics.Collections,
Forms, Dialogs, Controls, Graphics, ClipBrd, //Imaging.PngImage,
GR32, GR32_OrdinalMaps, GR32_Layers,
Base.Utils, Base.Bitmaps,
Dos.Structures, Dos.Consts, Dos.Bitmaps, Prog.Strings,
Meta.Structures,
Level.Base,
Styles.Base, Styles.Dos,
Prog.Base, Prog.Types, Prog.Data,
Game.Rendering, Game.SkillPanel, Game.Sound;
// we pass everything to the game preparation with this record
type
TGameInfoRec = record
Style : TStyle;
Renderer : TRenderer;
TargetBitmap : TBitmap32;
Level : TLevel;
LevelLoadingInfo : TLevelLoadingInformation;
GraphicSet : TGraphicSet;
SoundOpts : TSoundOptions;
UseParticles : Boolean;
UseGradientBridges : Boolean;
ShowReplayMessages : Boolean;
ShowFeedbackMessages : Boolean;
ShowReplayCursor : Boolean;
EnableSkillButtonsWhenPaused : Boolean;
UseShuffledMusic : Boolean;
UsePhotoFlashReplayEffect : Boolean;
OptionalMechanics : TOptionalMechanics;
end;
type
TParticleRec = packed record
DX, DY: ShortInt
end;
TParticleArray = packed array[0..79] of TParticleRec;
TParticleTable = packed array[0..50] of TParticleArray;
const
ParticleColorIndices: array[0..15] of Byte = (
4, 15, 14, 13, 12, 11, 10, 9, 8, 11, 10, 9, 8, 7, 6, 2
);
type
TLemmingGame = class;
TLemming = class
private
function GetLocationBounds: TRect; // paint rect in world
function GetFrameBounds: TRect; // frame rect from animation bitmap
function GetCountDownDigitBounds: TRect; // painting rect countdown digits
function GetRTL: Boolean; inline;
public
SavedMap : array[0..8] of Byte; // saves part of ObjectMap of game when blocking
RectToErase : TRect; // the rectangle of the last drawaction (can include space for countdown digits)
ListIndex : Integer; // index in the lemminglist
xPos : Integer; // the "main" foot x position
yPos : Integer; // the "main" foot y position
xDelta : Integer; // x speed (1 if left to right, -1 if right to left)
Fallen : Integer; // number of pixels a faller has fallen
ExplosionTimer : Integer; // 79 downto 0
LMA : TMetaLemmingAnimation; // ref to Lemming Meta Animation
LAB : TBitmap32; // ref to Lemming Animation Bitmap
Frame : Integer; // current animationframe
MaxFrame : Integer; // copy from LMA
AnimationType : TLemmingAnimationType; // copy from LMA
ParticleTimer : Integer; // @particles, 52 downto 0, after explosion
ParticleFrame : Integer; // the "frame" of the particle drawing algorithm
FrameTopDy : Integer; // = -LMA.FootY (ccexplore code compatible)
FrameLeftDx : Integer; // = -LMA.FootX (ccexplore code compatible)
FloatParametersTableIndex : Integer; // index for floaters
NumberOfBricksLeft : Integer; // for builder
Born : Integer; // game iteration the lemming was created
Action : TLemmingAction; // current action of the lemming
ObjectBelow : Byte;
ObjectInFront : Byte;
EndOfAnimation : Boolean;
IsRemoved : Boolean; // the lemming is not in the level anymore
IsClimber : Boolean;
IsFloater : Boolean;
IsBlocking : Boolean; // not always exactly in sync with the action
IsNewDigger : Boolean;
IsExploded : Boolean; // @particles, set after a Lemming actually exploded, used to control particles-drawing
PhotoFlashForReplay : Boolean;
// easy property
property RTL: Boolean read GetRTL;
end;
// internal object used by game
TInteractiveObjectInfo = class
private
function GetBounds: TRect;
function OnlyOnTerrain: Boolean; inline;
public
MetaObj : TMetaObject;
Obj : TInteractiveObject;
CurrentFrame : Integer;
Triggered : Boolean;
property Bounds: TRect read GetBounds;
end;
// new experimental in-game message
TGameMessage = class
private
fGame : TLemmingGame;
fBuffer : TBitmap32;
fDuration : Integer;
fCurrentFrame : Integer;
fLocation : TPoint;
fDeltaY : integer;
fDeltaX : Integer;
fText : string;
fEnded : Boolean;
procedure NextFrame;
public
constructor Create(aGame: TLemmingGame);
destructor Destroy; override;
property Duration: Integer read fDuration write fDuration default 32;
property CurrentFrame: Integer read fCurrentFrame write fCurrentFrame;
property DeltaY: Integer read fDeltaY write fDeltaY default -2;
property DeltaX: Integer read fDeltaX write fDeltaX;
procedure SetText(const Value: string; aColor: TColor32);
property Buffer: TBitmap32 read fBuffer;
end;
// todo: not implemented yet.
TReplayCursor = class
strict private
fGame: TLemmingGame;
fBitmap: TBitmap32;
fPosition: TPoint;
fCursorPos: TPoint;
fDrawRect: TRect;
fPreviousDrawRect: TRect;
fVisible: Boolean;
fErasable: Boolean;
fFrames: Integer;
fAlpha: Byte;
public
constructor Create(aGame: TLemmingGame);
destructor Destroy; override;
procedure Activate(const aCursorPos: TPoint);
procedure Decrement; inline;
property Bitmap: TBitmap32 read fBitmap;
property Position: TPoint read fPosition;
property DrawRect: TRect read fDrawRect;
property PreviousDrawRect: TRect read fPreviousDrawRect write fPreviousDrawRect;
property Erasable: Boolean read fErasable;
property Visible: Boolean read fVisible;
end;
TLemmingList = class(TFastObjectList<TLemming>);
TInteractiveObjectInfoList = class(TFastObjectList<TInteractiveObjectInfo>);
TGameMessageList = class(TFastObjectList<TGameMessage>);
// Replay stuff
TReplayFileHeaderRec = packed record
Signature : array[0..2] of AnsiChar; // 3 bytes - 3
Version : Byte; // 1 byte - 4
FileSize : Integer; // 4 bytes - 8
HeaderSize : Word; // 2 bytes - 10
Mechanics : TMechanics; // 2 bytes - 12
FirstRecordPos : Integer; // 4 bytes - 16
ReplayRecordSize : Word; // 2 bytes - 18
ReplayRecordCount : Word; // 2 bytes - 20
Hash : UInt64; // 8 bytes - 28 #EL 2020 added levelhash for accurate finding of level (replayversion 2)
ReplayGlitchPauseIterations : Integer; // 4 bytes - 32 #EL 2020: use last reserved int for glitch (replayversion 2)
LevelTitle : TLVLTitle; // 32 bytes - 64
end;
TReplayRec = packed record
Check : AnsiChar; // 1 byte - 1
Iteration : Integer; // 4 bytes - 5
ActionFlags : Word; // 2 bytes - 7
AssignedSkill : Byte; // 1 byte - 8
SelectedButton : Byte; // 1 byte - 9
ReleaseRate : Integer; // 1 byte - 13
LemmingIndex : Integer; // 4 bytes - 17
LemmingX : Integer; // 4 bytes - 21
LemmingY : Integer; // 4 bytes - 25
CursorX : SmallInt; // 2 bytes - 27
CursorY : SmallInt; // 2 bytes - 29
Flags : Byte; // 1 byte - 30 #EL 2020: added for accurate replaying of RightClickGlitch (replayversion 3).
Reserved2 : Byte;
Reserved3 : Byte; // 32 bytes
end;
TReplayItem = class
strict private
fIteration : Integer;
fActionFlags : Word; // raf_xxx
fAssignedSkill : Byte;
fSelectedButton : Byte;
fReleaseRate : Byte;
fLemmingIndex : Integer;
fLemmingX : Integer;
fLemmingY : Integer;
fCursorY : Integer;
fCursorX : Integer;
fFlags : Byte; // rf_xxx
strict private
fHasValidCursorData: Boolean;
public
property Iteration: Integer read fIteration write fIteration;
property ActionFlags: Word read fActionFlags write fActionFlags;
property AssignedSkill: Byte read fAssignedSkill write fAssignedSkill;
property SelectedButton: Byte read fSelectedButton write fSelectedButton;
property ReleaseRate: Byte read fReleaseRate write fReleaseRate;
property LemmingIndex: Integer read fLemmingIndex write fLemmingIndex;
property LemmingX: Integer read fLemmingX write fLemmingX;
property LemmingY: Integer read fLemmingY write fLemmingY;
property CursorX: Integer read fCursorX write fCursorX;
property CursorY: Integer read fCursorY write fCursorY;
property Flags: Byte read fFlags write fFlags;
property HasValidCursorData: Boolean read fHasValidCursorData write fHasValidCursorData;
end;
TRecorder = class
strict private
fWasSaved: Boolean;
fWasLoaded: Boolean;
private
fGame : TLemmingGame;
List : TFastObjectList<TReplayItem>;
fCurrentMechanics: TMechanics;
fRecordedGlitchPauseIterations: Integer;
fCurrentHeader: TReplayFileHeaderRec;
public
constructor Create(aGame: TLemmingGame);
destructor Destroy; override;
function Add: TReplayItem; inline;
procedure Clear; inline;
procedure Truncate(aCount: Integer); inline;
procedure SaveToFile(const aFileName: string);
procedure SaveToStream(S: TStream);
procedure SaveToTxt(const aFileName: string; includeGameResult: Boolean);
function LoadFromFile(const aFileName: string; out error: string): Boolean;
function LoadFromStream(stream: TStream; out error: string): Boolean;
class function LoadTitleAndHashFromHeader(const aFileName: string; out aHash: UInt64; out aTitle: TLVLTitle): Boolean;
property WasLoaded: Boolean read fWasLoaded;
property WasSaved: Boolean read fWasSaved;
property CurrentHeader: TReplayFileHeaderRec read fCurrentHeader;
end;
TLemmingMethod = function (L: TLemming): Boolean of object;
TLemmingMethodArray = array[TLemmingAction] of TLemmingMethod;
TSkillMethod = function (Lemming1, Lemming2: TLemming): TLemming of object;
TSkillMethodArray = array[TLemmingAction] of TSkillMethod;
TLemmingGame = class
private
fSelectedSkill : TSkillPanelButton; // currently selected skill restricted by F3-F9
fMechanics : TMechanics; // mechanic options
fParticles : TParticleTable; // all particle offsets
fParticleColors : array[0..15] of TColor32;
// internal objects
LemmingList : TLemmingList; // the list of lemmings
World : TBitmap32; // actual bitmap that is changed by the lemmings
ObjectMap : TByteMap; // dos compatible 4 pixel resolution map
MiniMap : TBitmap32; // minimap of world
fMinimapBuffer : TBitmap32; // drawing buffer minimap
fRecorder : TRecorder;
MessageList : TGameMessageList;
SoundMgr : TSoundMgr;
ReplayCursor : TReplayCursor;
// reference objects, mostly for easy access
fTargetBitmap : TBitmap32; // ref to the drawing bitmap on the gamewindow
fRenderer : TRenderer; // ref to gameparams.renderer
fInfoPainter : TSkillPanelToolbar; // ref to skillpanel for drawing
fLevel : TLevel; // ref to gameparams.level
fLevelLoadingInfo : TLevelLoadingInformation; // ref to the loading information
fStyle : TStyle; // ref to gameparams.style
fGraph : TGraphicSet; // ref to gameparams.graph
fSoundOpts : TSoundOptions;
CntDownBmp : TBitmap32; // ref to style.animationset.countdowndigits
ExplodeMaskBmp : TBitmap32; // ref to style.animationset.explosionmask
BashMasks : array[Boolean] of TBitmap32;// ref to style.animationset.bashmasks (not RTL, RTL)
MineMasks : array[Boolean] of TBitmap32;// ref to style.animationset.minemasks (not RTL, RTL)
// vars
fCurrentIteration : Integer;
fClockFrame : Integer; // 17 frames is one game-second
fGlitchPauseIterations : Integer; // pause glitch
fStartPauseIteration : Integer; // if pause glitch then 34 otherwise 0
fReplayGlitchPauseIterations : Integer;
LemmingsReleased : Integer; // number of lemmings that were created
LemmingsOut : Integer; // number of lemmings currently walking around
LemmingsIn : integer; // number of lemmings that made it to heaven
LemmingsRemoved : Integer; // number of lemmings removed
fCursorPoint : TPoint;
fRightMouseButtonHeldDown : Boolean;
Minutes : Integer; // minutes left
Seconds : Integer; // seconds left
fPlaying : Boolean; // game in active playing mode?
EntrancesOpened : Boolean;
LemmingMethods : TLemmingMethodArray; // a method for each basic lemming state
SkillMethods : TSkillMethodArray; // a method for assigning jobs (including dummies)
ObjectInfos : TInteractiveObjectInfoList; // list of objects excluding entrances
Entrances : TInteractiveObjectInfoList; // list of entrances
DosEntranceOrderTable : TArray<Integer>; // table(0..3) for entrance release order
fSlowingDownReleaseRate : Boolean;
fSpeedingUpReleaseRate : Boolean;
fPaused : Boolean;
MaxNumLemmings : Integer;
CurrReleaseRate : Integer;
CurrClimberCount : Integer;
CurrFloaterCount : Integer;
CurrBomberCount : Integer;
CurrBlockerCount : Integer;
CurrBuilderCount : Integer;
CurrBasherCount : Integer;
CurrMinerCount : Integer;
CurrDiggerCount : Integer;
UserSetNuking : Boolean;
ExploderAssignInProgress : Boolean;
Index_LemmingToBeNuked : Integer;
fCurrentCursor : Integer; // normal or highlight lemming
BrickPixelColor : TColor32;
BrickPixelColors : array[0..11] of TColor32; // 12 gradient steps
fGameFinished : Boolean;
fGameCheated : Boolean;
NextLemmingCountDown : Integer;
fFastForward : Boolean;
fReplaying : Boolean;
fReplayIndex : Integer;
fLastRecordedRecordReached : Boolean;
fLastCueSoundIteration : Integer;
// fSoundToPlay : Integer;
fSoundsToPlay : TList<Integer>;
fMessagesPlayedCount : Integer;
fFading : Boolean;
fTargetIteration : Integer; // this is used in hyperspeed
fHyperSpeedCounter : Integer; // no screenoutput
fHyperSpeed : Boolean; // we are at hyperspeed no targetbitmap output
fLeavingHyperSpeed : Boolean; // in between state (see UpdateLemmings)
fEntranceAnimationCompleted : Boolean;
fStartupMusicAfterEntrance : Boolean;
fCurrentlyDrawnLemming : TLemming; // needed for pixelcombining bridges in combinebuilderpixels
fUseGradientBridges : Boolean;
fUseParticles : Boolean;
fUseShuffledMusic : Boolean;
fShowReplayMessages : Boolean;
fShowFeedbackMessages : Boolean;
fUsePhotoFlashEffect : Boolean;
fShowReplayCursor : Boolean;
fEnableSkillButtonsWhenPaused: Boolean;
fExplodingPixelsUpdateNeeded : Boolean;
fLastNonPrioritizedLemming : TLemming; // RightClickGlitch emulation (user optional mechanic)
fAssignmentIsRightClickGlitch: Boolean; // storage of the bug. global because we do not want extra parameters in AssignSkill
// sound indices in soundmgr
SFX_BUILDER_WARNING : Integer;
SFX_ASSIGN_SKILL : Integer;
SFX_YIPPEE : Integer;
SFX_SPLAT : Integer;
SFX_LETSGO : Integer;
SFX_ENTRANCE : Integer;
SFX_VAPORIZING : Integer;
SFX_DROWNING : Integer;
SFX_EXPLOSION : Integer;
SFX_HITS_STEEL : Integer;
SFX_OHNO : Integer;
SFX_SKILLBUTTON : Integer;
SFX_ROPETRAP : Integer;
SFX_TENTON : Integer;
SFX_BEARTRAP : Integer;
SFX_ELECTROTRAP : Integer;
SFX_SPINNINGTRAP : Integer;
SFX_SQUISHINGTRAP : Integer;
// music index in soundmgr
MUSIC_INDEX : Integer; // there is one music possible
fParticleFinishTimer : Integer; // extra frames to enable viewing of explosions
// event
fOnFinish : TNotifyEvent;
// pixel combine eventhandlers
procedure CombineDefaultPixels(F: TColor32; var B: TColor32; M: TColor32);
procedure CombineLemmingPixels(F: TColor32; var B: TColor32; M: TColor32);
procedure CombineBuilderPixels(F: TColor32; var B: TColor32; M: TColor32);
procedure CombineLemmingHighlight(F: TColor32; var B: TColor32; M: TColor32);
procedure CombineMaskPixels(F: TColor32; var B: TColor32; M: TColor32);
procedure CombineMinimapWorldPixels(F: TColor32; var B: TColor32; M: TColor32);
// internal methods
procedure AddReplayMessage(L: TLemming; aAction: TLemmingAction; const Suffix: string = '');
procedure AddCustomMessage(const s: string);
procedure ApplyBashingMask(L: TLemming; MaskFrame: Integer);
procedure ApplyExplosionMask(L: TLemming);
procedure ApplyMinerMask(L: TLemming; MaskFrame, X, Y: Integer);
function CalculateNextLemmingCountdown: Integer;
procedure CheckAdjustReleaseRate;
procedure CheckForGameFinished;
procedure CheckForInteractiveObjects(L: TLemming);
function CheckForLevelTopBoundary(L: TLemming; LocalFrameTopDy: Integer = 0): Boolean;
function CheckForOverlappingField(L: TLemming): Boolean;
procedure CheckForPlaySoundEffect;
procedure CheckForReplayAction;
procedure CheckLemmings;
procedure CheckReleaseLemming;
procedure CheckUpdateNuking;
procedure CueSoundEffect(aSoundId: Integer);
function DigOneRow(L: TLemming; Y: Integer): Boolean;
procedure DrawAnimatedObjects;
procedure DrawLemmings;
procedure DrawParticles(L: TLemming);
procedure DrawReplayCursorCheck;
procedure DrawReplayCursor(const P: TPoint);
procedure DrawInitialStatics;
procedure DrawMessages;
procedure EraseLemmings;
procedure EraseMessages;
procedure EraseParticles(L: TLemming);
procedure EraseReplayCursor;
function GetTrapSoundIndex(aDosSoundEffect: Integer): Integer;
function HasPixelAt(X, Y: Integer): Boolean;
function HasPixelAt_ClipY(X, Y, minY: Integer): Boolean;
procedure IncrementIteration;
procedure InitializeBrickColors(aBrickPixelColor: TColor32);
procedure InitializeMiniMap;
procedure InitializeObjectMap;
procedure LayBrick(L: TLemming);
function PrioritizedHitTest(out Lemming1, Lemming2: TLemming; const CP: TPoint; CheckRightMouseButton: Boolean): Integer;
function ReadObjectMap(X, Y: Integer): Byte;
procedure RecordStartPause;
procedure RecordEndPause;
procedure RecordNuke;
procedure RecordReleaseRate(aActionFlag: Byte);
procedure RecordSkillAssignment(L: TLemming; aSkill: TLemmingAction; usedLemming2, rightMouseGlitched: Boolean);
procedure RecordSkillSelection(aSkill: TSkillPanelButton);
procedure RemoveLemming(L: TLemming);
procedure RemovePixelAt(X, Y: Integer);
procedure ReplaySkillAssignment(item: TReplayItem);
procedure ReplaySkillSelection(aReplayItem: TReplayItem);
procedure RestoreMap(L: TLemming);
procedure SaveMap(L: TLemming);
procedure SetBlockerField(L: TLemming);
procedure Transition(L: TLemming; aAction: TLemmingAction; DoTurn: Boolean = False);
procedure TurnAround(L: TLemming);
function UpdateExplosionTimer(L: TLemming): Boolean;
procedure UpdateInteractiveObjects;
procedure UpdateMessages;
procedure WriteObjectMap(X, Y: Integer; aValue: Byte);
// lemming actions
function HandleLemming(L: TLemming): Boolean;
function HandleWalking(L: TLemming): Boolean;
function HandleJumping(L: TLemming): Boolean;
function HandleDigging(L: TLemming): Boolean;
function HandleClimbing(L: TLemming): Boolean;
function HandleDrowning(L: TLemming): Boolean;
function HandleHoisting(L: TLemming): Boolean;
function HandleBuilding(L: TLemming): Boolean;
function HandleBashing(L: TLemming): Boolean;
function HandleMining(L: TLemming): Boolean;
function HandleFalling(L: TLemming): Boolean;
function HandleFloating(L: TLemming): Boolean;
function HandleSplatting(L: TLemming): Boolean;
function HandleExiting(L: TLemming): Boolean;
function HandleVaporizing(L: TLemming): Boolean;
function HandleBlocking(L: TLemming): Boolean;
function HandleShrugging(L: TLemming): Boolean;
function HandleOhNoing(L: TLemming): Boolean;
function HandleExploding(L: TLemming): Boolean;
// skill interaction
function AssignSkill(Lemming1, Lemming2: TLemming; aSkill: TLemmingAction): TLemming;
function AssignClimber(Lemming1, Lemming2: TLemming): TLemming;
function AssignFloater(Lemming1, Lemming2: TLemming): TLemming;
function AssignBomber(Lemming1, Lemming2: TLemming): TLemming;
function AssignBlocker(Lemming1, Lemming2: TLemming): TLemming;
function AssignBuilder(Lemming1, Lemming2: TLemming): TLemming;
function AssignBasher(Lemming1, Lemming2: TLemming): TLemming;
function AssignMiner(Lemming1, Lemming2: TLemming): TLemming;
function AssignDigger(Lemming1, Lemming2: TLemming): TLemming;
procedure SetSoundOpts(const Value: TSoundOptions);
public
GameResultRec: TGameResultsRec;
constructor Create;
destructor Destroy; override;
// callable
procedure AdjustReleaseRate(Delta: Integer);
procedure ChangeMusicVolume(up: Boolean);
procedure Cheat;
procedure CreateLemmingAtCursorPoint;
procedure Finish;
procedure Terminate;
procedure HitTest;
procedure HyperSpeedBegin;
procedure HyperSpeedEnd;
function ProcessSkillAssignment: TLemming;
procedure Prepare(const aInfo: TGameInfoRec);
procedure RegainControl;
procedure Save(includeGameResult: Boolean);
procedure SaveCurrentFrameToPng;
procedure SetGameResult;
procedure SetSelectedSkill(Value: TSkillPanelButton; MakeActive: Boolean = True);
procedure Start(aReplay: Boolean = False);
procedure UpdateLemmings;
// properties
property ClockFrame: Integer read fClockFrame;
property CurrentCursor: Integer read fCurrentCursor;
property CurrentIteration: Integer read fCurrentIteration;
property CursorPoint: TPoint read fCursorPoint write fCursorPoint;
property EnableSkillButtonsWhenPaused: Boolean read fEnableSkillButtonsWhenPaused;
property Fading: Boolean read fFading;
property FastForward: Boolean read fFastForward write fFastForward;
property GameFinished: Boolean read fGameFinished;
property HyperSpeed: Boolean read fHyperSpeed;
property InfoPainter: TSkillPanelToolbar read fInfoPainter write fInfoPainter;
property Level: TLevel read fLevel;
property Mechanics: TMechanics read fMechanics;
property MiniMapBuffer: TBitmap32 read fMiniMapBuffer;
property Paused: Boolean read fPaused write fPaused;
property GlitchPauseIterations: Integer read fGlitchPauseIterations;
property ReplayGlitchPauseIterations: Integer read fReplayGlitchPauseIterations;
property Playing: Boolean read fPlaying write fPlaying;
property Recorder: TRecorder read fRecorder;
property Renderer: TRenderer read fRenderer;
property Replaying: Boolean read fReplaying;
property RightMouseButtonHeldDown: Boolean read fRightMouseButtonHeldDown write fRightMouseButtonHeldDown;
property SlowingDownReleaseRate: Boolean read fSlowingDownReleaseRate;
property SoundOpts: TSoundOptions read fSoundOpts write SetSoundOpts;
property SpeedingUpReleaseRate: Boolean read fSpeedingUpReleaseRate;
property StartPauseIteration: Integer read fStartPauseIteration;
property TargetIteration: Integer read fTargetIteration write fTargetIteration;
// event
property OnFinish: TNotifyEvent read fOnFinish write fOnFinish;
end;
const
//Recorded Action Flags
raf_StartPause = Bit0;
raf_EndPause = Bit1;
raf_Pausing = Bit2;
raf_StartIncreaseRR = Bit3; // only allowed when not pausing
raf_StartDecreaseRR = Bit4; // only allowed when not pausing
raf_StopChangingRR = Bit5; // only allowed when not pausing
raf_SkillSelection = Bit6;
raf_SkillAssignment = Bit7;
raf_Nuke = Bit8; // only allowed when not pausing, as in the game
// Replay Flag
rf_UseLemming2 = Bit0;
rf_RightMouseGlitch = Bit1;
// Recorded Lemming Action
rla_None = 0;
rla_Walking = 1; // recording not allowed
rla_Jumping = 2; // recording not allowed
rla_Digging = 3;
rla_Climbing = 4;
rla_Drowning = 5; // recording not allowed
rla_Hoisting = 6; // recording not allowed
rla_Building = 7;
rla_Bashing = 8;
rla_Mining = 9;
rla_Falling = 10; // recording not allowed
rla_Floating = 11;
rla_Splatting = 12; // recording not allowed
rla_Exiting = 13; // recording not allowed
rla_Vaporizing = 14; // recording not allowed
rla_Blocking = 15;
rla_Shrugging = 16; // recording not allowed
rla_Ohnoing = 17; // recording not allowed
rla_Exploding = 18;
// Recorded Selected Button
rsb_None = 0;
rsb_Slower = 1; // select not allowed
rsb_Faster = 2; // select not allowed
rsb_Climber = 3;
rsb_Umbrella = 4;
rsb_Explode = 5;
rsb_Stopper = 6;
rsb_Builder = 7;
rsb_Basher = 8;
rsb_Miner = 9;
rsb_Digger = 10;
rsb_Pause = 11; // select not allowed
rsb_Nuke = 12; // select not allowed
type
// special frame handling for floaters
TFloatParameterRec = record
Dy: Integer;
AnimationFrameIndex: Integer;
end;
const
{-------------------------------------------------------------------------------
So what's this: A table which describes what to do when floating.
The floaters animation has 8 frames: 0..3 is opening the umbrella
and 4..7 is the actual floating.
This table "fakes" 16 frames of floating and what should happen with
the Y-position of the lemming each frame. Frame zero is missing, because that's
automatically frame zero.
Additionally: after 15 go back to 8
-------------------------------------------------------------------------------}
FloatParametersTable: array[0..15] of TFloatParameterRec = (
(Dy: 3; AnimationFrameIndex: 1),
(Dy: 3; AnimationFrameIndex: 2),
(Dy: 3; AnimationFrameIndex: 3),
(Dy: 3; AnimationFrameIndex: 5),
(Dy: -1; AnimationFrameIndex: 5),
(Dy: 0; AnimationFrameIndex: 5),
(Dy: 1; AnimationFrameIndex: 5),
(Dy: 1; AnimationFrameIndex: 5),
(Dy: 2; AnimationFrameIndex: 5),
(Dy: 2; AnimationFrameIndex: 6),
(Dy: 2; AnimationFrameIndex: 7),
(Dy: 2; AnimationFrameIndex: 7),
(Dy: 2; AnimationFrameIndex: 6),
(Dy: 2; AnimationFrameIndex: 5),
(Dy: 2; AnimationFrameIndex: 4),
(Dy: 2; AnimationFrameIndex: 4)
);
implementation
const
OBJMAPOFFSET = 16;
OBJMAPADD = OBJMAPOFFSET div 4;
const
LEMMIX_REPLAY_VERSION = 3;
MAX_REPLAY_RECORDS = 32768;
MAX_FALLDISTANCE = 60;
const
// values for the (4 pixel resolution) Dos Object Map (for triggereffects)
DOM_NONE = 128 + 0;
DOM_EXIT = 128 + 1;
DOM_FORCELEFT = 128 + 2; // left arm of blocker
DOM_FORCERIGHT = 128 + 3; // right arm of blocker
// DOM_TRAP = 128 + 4; // not used
DOM_WATER = 128 + 5; // causes drowning
DOM_FIRE = 128 + 6; // causes vaporizing
DOM_ONEWAYLEFT = 128 + 7;
DOM_ONEWAYRIGHT = 128 + 8;
DOM_STEEL = 128 + 9;
DOM_BLOCKER = 128 + 10; // the middle part of blocker
HEAD_MIN_Y = -5;
LEMMING_MIN_X = 0;
LEMMING_MAX_X = 1647;
LEMMING_MAX_Y = 163;
PARTICLE_FRAMECOUNT = 52;
function CheckRectCopy(const A, B: TRect): Boolean;
begin
Result := (A.Width = B.Width) and (A.Height = B.Height);
end;
{ TLemming }
function TLemming.GetRTL: Boolean; // inline
begin
Result := xDelta < 0;
end;
function TLemming.GetCountDownDigitBounds: TRect;
begin
Result.Left := xPos - 1;
Result.Top := yPos + FrameTopDy - 12;
Result.Right := Result.Left + 8;
Result.Bottom := Result.Top + 8;
end;
function TLemming.GetFrameBounds: TRect;
begin
Result.Left := 0;
Result.Top := Frame * LMA.Height;
Result.Right := LMA.Width;
Result.Bottom := Result.Top + LMA.Height;
end;
function TLemming.GetLocationBounds: TRect;
begin
Result.Left := xPos - LMA.FootX;
Result.Top := yPos - LMA.FootY;
Result.Right := Result.Left + LMA.Width;
Result.Bottom := Result.Top + LMA.Height;
end;
{ TInteractiveObjectInfo }
function TInteractiveObjectInfo.GetBounds: TRect;
begin
Result.Left := Obj.Left;
Result.Top := Obj.Top;
Result.Right := Result.Left + MetaObj.Height;
Result.Bottom := Result.Top + MetaObj.Width;
end;
function TInteractiveObjectInfo.OnlyOnTerrain: Boolean;
begin
Result := odf_OnlyOnTerrain and Obj.DrawingFlags <> 0;
end;
{ TGameMessage }
constructor TGameMessage.Create(aGame: TLemmingGame);
begin
inherited Create;
fGame := aGame;
fDuration := 32;
fDeltaY := -2;
fBuffer := TBitmap32.Create;
fBuffer.SetSize(1,1);
fBuffer.Font.Height := 12;
fBuffer.Font.Name := 'Arial';
fBuffer.Font.Quality := TFontQuality.fqNonAntialiased;
fBuffer.DrawMode := dmTransparent;
//fBuffer.DrawMode := dmBlend;//Transparent;
end;
destructor TGameMessage.Destroy;
begin
fBuffer.Free;
inherited;
end;
procedure TGameMessage.NextFrame;
begin
Inc(fCurrentFrame);
Inc(fLocation.Y, fDeltaY);
Inc(fLocation.X, fDeltaX);
if (fLocation.Y + fBuffer.Height <= 0)
or (fLocation.X >= GAME_BMPWIDTH)
or (fLocation.X + fBuffer.Width <= 0)
or (fCurrentFrame > fDuration) then
fEnded := True;
//if fBuffer.MasterAlpha >= 8 then
// fBuffer.MasterAlpha := fBuffer.MasterAlpha - 8;
end;
procedure TGameMessage.SetText(const Value: string; aColor: TColor32);
var
size: TSize;
begin
fText := Value;
fBuffer.Font.Color := WinColor(aColor);
size := fBuffer.TextExtent(Ftext);
fbuffer.SetSize(size.cx, size.cy);
fbuffer.Clear(0);
fbuffer.Textout(0, 0, fText);
fbuffer.DrawMode := dmTransparent;
// fbuffer.MasterAlpha := 128;
end;
{ TReplayCursor }
constructor TReplayCursor.Create(aGame: TLemmingGame);
var
bmp: TBitmap;
begin
inherited Create;
fGame := aGame;
fBitmap := TBitmap32.Create;
bmp := TData.CreateCursorBitmap(Consts.StyleName, Consts.FilenameCursorHighlight);
fBitmap.Assign(bmp);
bmp.Free;
fBitmap.ReplaceColor(SetAlpha(clBlack32, 255), 0);
fBitmap.DrawMode := dmBlend;
end;
destructor TReplayCursor.Destroy;
begin
fBitmap.Free;
inherited;
end;
procedure TReplayCursor.Activate(const aCursorPos: TPoint);
begin
// if reset we still need to erase the previous location
if fErasable then
fPreviousDrawRect := fDrawRect
else
fPreviousDrawRect := TRect.Empty;
fCursorPos := aCursorPos;
fPosition := Point(fCursorPos.X - 7, fCursorPos.Y - 7);
fDrawRect.Left := fPosition.X;
fDrawRect.Top := fPosition.Y;
fDrawRect.Right := fDrawRect.Left + fBitmap.Width;
fDrawRect.Bottom := fDrawRect.Top + fBitmap.Height;
fAlpha := 132;
fBitmap.MasterAlpha := fAlpha;//ReplaceAlphaForAllNonZeroColors(fAlpha);
fFrames := 10;
fVisible := True;
fErasable := True;
end;
procedure TReplayCursor.Decrement;
begin
if fFrames >= 0 then begin
Dec(fFrames);
fVisible := fFrames >= 0;
fErasable := fFrames >= -1;
Dec(fAlpha, 12);
fBitmap.MasterAlpha := fAlpha;//ReplaceAlphaForAllNonZeroColors(fAlpha);
end;
end;
{ TLemmingGame }
constructor TLemmingGame.Create;
begin
inherited Create;
Assert(SizeOf(TReplayFileHeaderRec) = 64);
Assert(SizeOf(TMechanics) = 2); // mechanics set has to fit in correctly inside a replay record
LemmingList := TLemmingList.Create;
World := TBitmap32.Create;
ObjectInfos := TInteractiveObjectInfoList.Create;
Entrances := TInteractiveObjectInfoList.Create;
ObjectMap := TByteMap.Create;
MiniMap := TBitmap32.Create;
fMinimapBuffer := TBitmap32.Create;
fRecorder := TRecorder.Create(Self);
SoundMgr := TSoundMgr.Create;
MessageList := TGameMessageList.Create;
ReplayCursor := TReplayCursor.Create(Self);
fSoundsToPlay := TList<Integer>.Create;
LemmingMethods[TLemmingAction.None] := nil;
LemmingMethods[TLemmingAction.Walking] := HandleWalking;
LemmingMethods[TLemmingAction.Jumping] := HandleJumping;
LemmingMethods[TLemmingAction.Digging] := HandleDigging;
LemmingMethods[TLemmingAction.Climbing] := HandleClimbing;
LemmingMethods[TLemmingAction.Drowning] := HandleDrowning;
LemmingMethods[TLemmingAction.Hoisting] := HandleHoisting;
LemmingMethods[TLemmingAction.Building] := HandleBuilding;
LemmingMethods[TLemmingAction.Bashing] := HandleBashing;
LemmingMethods[TLemmingAction.Mining] := HandleMining;
LemmingMethods[TLemmingAction.Falling] := HandleFalling;
LemmingMethods[TLemmingAction.Floating] := HandleFloating;
LemmingMethods[TLemmingAction.Splatting] := HandleSplatting;
LemmingMethods[TLemmingAction.Exiting] := HandleExiting;
LemmingMethods[TLemmingAction.Vaporizing] := HandleVaporizing;
LemmingMethods[TLemmingAction.Blocking] := HandleBlocking;
LemmingMethods[TLemmingAction.Shrugging] := HandleShrugging;
LemmingMethods[TLemmingAction.Ohnoing] := HandleOhNoing;
LemmingMethods[TLemmingAction.Exploding] := HandleExploding;
SkillMethods[TLemmingAction.None] := nil;
SkillMethods[TLemmingAction.Walking] := nil;
SkillMethods[TLemmingAction.Jumping] := nil;
SkillMethods[TLemmingAction.Digging] := AssignDigger;
SkillMethods[TLemmingAction.Climbing] := AssignClimber;
SkillMethods[TLemmingAction.Drowning] := nil;
SkillMethods[TLemmingAction.Hoisting] := nil;
SkillMethods[TLemmingAction.Building] := AssignBuilder;
SkillMethods[TLemmingAction.Bashing] := AssignBasher;
SkillMethods[TLemmingAction.Mining] := AssignMiner;
SkillMethods[TLemmingAction.Falling] := nil;
SkillMethods[TLemmingAction.Floating] := AssignFloater;
SkillMethods[TLemmingAction.Splatting] := nil;
SkillMethods[TLemmingAction.Exiting] := nil;
SkillMethods[TLemmingAction.Vaporizing] := nil;
SkillMethods[TLemmingAction.Blocking] := AssignBlocker;
SkillMethods[TLemmingAction.Shrugging] := nil;
SkillMethods[TLemmingAction.Ohnoing] := nil;
SkillMethods[TLemmingAction.Exploding] := AssignBomber;
// todo: maybe put the filenames in consts
SFX_BUILDER_WARNING := SoundMgr.AddSoundFromFileName(TSoundEffect.BuilderWarning.AsFileName);
SFX_ASSIGN_SKILL := SoundMgr.AddSoundFromFileName(TSoundEffect.AssignSkill.AsFileName);
SFX_YIPPEE := SoundMgr.AddSoundFromFileName(TSoundEffect.Yippee.AsFileName);
SFX_SPLAT := SoundMgr.AddSoundFromFileName(TSoundEffect.Splat.AsFileName);
SFX_LETSGO := SoundMgr.AddSoundFromFileName(TSoundEffect.LetsGo.AsFileName);
SFX_ENTRANCE := SoundMgr.AddSoundFromFileName(TSoundEffect.EntranceOpening.AsFileName);
SFX_VAPORIZING := SoundMgr.AddSoundFromFileName(TSoundEffect.Vaporizing.AsFileName);
SFX_DROWNING := SoundMgr.AddSoundFromFileName(TSoundEffect.Drowning.AsFileName);
SFX_EXPLOSION := SoundMgr.AddSoundFromFileName('Explosion3.WAV'{TSoundEffect.Explosion.AsFileName});
SFX_HITS_STEEL := SoundMgr.AddSoundFromFileName(TSoundEffect.HitsSteel.AsFileName);
SFX_OHNO := SoundMgr.AddSoundFromFileName(TSoundEffect.Ohno.AsFileName);
SFX_SKILLBUTTON := SoundMgr.AddSoundFromFileName(TSoundEffect.SkillButtonSelect.AsFileName);
SFX_ROPETRAP := SoundMgr.AddSoundFromFileName(TSoundEffect.RopeTrap.AsFileName);
SFX_TENTON := SoundMgr.AddSoundFromFileName(TSoundEffect.TenTonTrap.AsFileName);
SFX_BEARTRAP := SoundMgr.AddSoundFromFileName(TSoundEffect.BearTrap.AsFileName);
SFX_ELECTROTRAP := SoundMgr.AddSoundFromFileName(TSoundEffect.ElectroTrap.AsFileName);
SFX_SPINNINGTRAP := SoundMgr.AddSoundFromFileName(TSoundEffect.SpinningTrap.AsFileName);
SFX_SQUISHINGTRAP := SoundMgr.AddSoundFromFileName(TSoundEffect.SquishingTrap.AsFileName);
end;
destructor TLemmingGame.Destroy;
begin
LemmingList.Free;
ObjectInfos.Free;
World.Free;
Entrances.Free;
ObjectMap.Free;
MiniMap.Free;
fMinimapBuffer.Free;
fRecorder.Free;
SoundMgr.Free;
MessageList.Free;
ReplayCursor.Free;
fSoundsToPlay.Free;
inherited Destroy;
end;
procedure TLemmingGame.Prepare(const aInfo: TGameInfoRec);
// #EL 2009-04-02 added options (needed for replay)
// #EL 2020-02-23 decoupled some things: all info is passed to this method
var
Ani: TLemmingAnimationSet;
Bmp: TBitmap32;
i, ix: Integer;
stream: TStream;
info: TLevelLoadingInformation;
begin
// copy info param
fMechanics := aInfo.Style.Mechanics;
fSoundOpts := aInfo.SoundOpts;
fRenderer := aInfo.Renderer;
fTargetBitmap := aInfo.TargetBitmap;
fLevel := aInfo.Level;
fStyle := aInfo.Style;
fGraph := aInfo.GraphicSet;
fLevelLoadingInfo := aInfo.LevelLoadingInfo;
fUseGradientBridges := aInfo.UseGradientBridges;
fUseParticles := aInfo.UseParticles;
fShowReplayMessages := aInfo.ShowReplayMessages;
fShowFeedbackMessages := aInfo.ShowFeedbackMessages;
fUseShuffledMusic := aInfo.UseShuffledMusic;
fEnableSkillButtonsWhenPaused := aInfo.EnableSkillButtonsWhenPaused;
fUsePhotoFlashEffect := aInfo.UsePhotoFlashReplayEffect;
fShowReplayCursor := aInfo.ShowReplayCursor;
fLastNonPrioritizedLemming := nil;
if TOptionalMechanic.NukeGlitch in aInfo.OptionalMechanics then
Include(fMechanics, TMechanic.NukeGlitch);
if TOptionalMechanic.PauseGlitch in aInfo.OptionalMechanics then
Include(fMechanics, TMechanic.PauseGlitch);
if TOptionalMechanic.RighClickGlitch in aInfo.OptionalMechanics then
Include(fMechanics, TMechanic.RightClickGlitch);
fRecorder.fCurrentMechanics := fMechanics;
fMessagesPlayedCount := 0;
fStartupMusicAfterEntrance := True;
{--------------------------------------------------------------------------------------
Initialize the palette of AnimationSet.
Low part is the fixed palette
Hi part comes from the graphicset.
After that let the AnimationSet read the animations.
#EL 2020-02-23: reloading the lemminganimations is needed because of the brickcolor.
The graphicset assembles the palette and is already loaded.
---------------------------------------------------------------------------------------}
Ani := fStyle.LemmingAnimationSet;
Ani.AnimationPalette := Copy(fGraph.Palette);
Ani.Load;
// initialize explosion particle colors
for i := 0 to 15 do
fParticleColors[i] := fGraph.Palette[ParticleColorIndices[i]];
// prepare masks for drawing
CntDownBmp := Ani.CountDownDigitsBitmap;
CntDownBmp.DrawMode := dmCustom;
CntDownBmp.OnPixelCombine := CombineDefaultPixels;
ExplodeMaskBmp := Ani.ExplosionMaskBitmap;
ExplodeMaskBmp.DrawMode := dmCustom;
ExplodeMaskBmp.OnPixelCombine := CombineMaskPixels;
BashMasks[False] := Ani.BashMasksBitmap;
BashMasks[False].DrawMode := dmCustom;
BashMasks[False].OnPixelCombine := CombineMaskPixels;
BashMasks[True] := Ani.BashMasksRTLBitmap;
BashMasks[True].DrawMode := dmCustom;
BashMasks[True].OnPixelCombine := CombineMaskPixels;
MineMasks[False] := Ani.MineMasksBitmap;
MineMasks[False].DrawMode := dmCustom;
MineMasks[False].OnPixelCombine := CombineMaskPixels;
MineMasks[True] := Ani.MineMasksRTLBitmap;
MineMasks[True].DrawMode := dmCustom;
MineMasks[True].OnPixelCombine := CombineMaskPixels;
// prepare animationbitmaps for drawing (set pixelcombine eventhandlers)
ix := 0;
for Bmp in Ani.LemmingBitmaps do begin
Bmp.DrawMode := dmCustom;
if ix in [TLemmingAnimationSet.BRICKLAYING, TLemmingAnimationSet.BRICKLAYING_RTL] then