forked from JBontes/Life32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lifegen.pas
4088 lines (3598 loc) · 124 KB
/
Lifegen.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 Lifegen;
(* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
{$undef LifeDebug}
interface
uses
Classes, SysUtils, WinTypes, WinProcs, Controls,Graphics,
RotatBit, LifeCel, LifeHash, LifeRules,
StdCtrls, Dialogs, ClipBrd, Snapshot, LifeUtil, LifeConst, GifImage;
type
TUniverse = class;
TRuleChangeEvent = procedure(Sender: TUniverse) of object;
TSaveProgressEvent = procedure(Sender: TObject; Progress: integer; var Cancel: Boolean) of object;
TUniverse = class(TObject)
private
living: TLifeCel; // start of live list.
hibernating: TLifeCel;// p1 or p2 blocks
morgue: TLifeCel; // empty blocks, not quite ready for cleanup.
caretaker: TLifeCel; // startingpoint in morgue for deletion.
FHashTable: TLifeHash; // lookuptable on coordinate.
FNeighborhood: integer; //defaults to nbMoore.
//FRuleString: string;
FRules: TLifeRules; // rules of the game.
FCounter: Integer;
FIsCounting: boolean;
FUseCount: integer;
FWriteLock: Boolean;
FDescription: TStringList;
FOnRuleChange: TRuleChangeEvent;
FOnSaveProgress: TSaveProgressEvent;
FClipRect: TRect;
FLimit: TRect;
FIsLimited: boolean;
FTorusKind: TTorusKind;
FDeadEdges: Boolean;
FLimitChanged: Boolean;
FCheckPeriod: boolean;
FTerminated: boolean;
FPatternName: string;
FChecksumList: TList;
FCenterPoint: TPoint;
procedure Generate_p;
procedure Generate_q;
//function convertRules(ARule: string): string;
procedure ImplementRules;
procedure KillCage(c: TLifeCel);
procedure TranquilizeCage(c: TLifeCel);
function RattleCage(c: TLifeCel): boolean;
procedure InsertCage(c: TLifeCel);
function AllocateCage(x,y: Integer): TLifeCel;
function GetBlockBitmask(x,y: integer): integer;
function GetLifeCelSlowly(x,y: integer; force:boolean): TLifeCel;
function GetLifeCel(x,y: integer; force:boolean): TLifeCel;
function GetBlockIndex(x,y: integer):integer;
procedure IncinerateCages(beNice: boolean);
procedure SetRules(ARuleString: string);
procedure SetNeighborhood(Value: integer);
procedure SetPatternName(Value: string);
function GetRules: string;
procedure ShrinkSelRectFast(var ARect: TRect);
procedure CorrectUniverse;
procedure LimitTorus(TorusKind: TTorusKind; DeadEdges: boolean);
function GetChecksum: integer;
procedure DoChecksum;
procedure ClearSmallRect(ARect: TRect);
procedure ClearBigRect(ARect: TRect);
procedure FillSmallRect(ARect: TRect);
procedure FillBigRect(ARect: TRect);
procedure InvertSmallRect(ARect: TRect);
procedure InvertBigRect(ARect: TRect);
procedure SetWriteLock(Value: Boolean);
procedure SetLimit(Value: TRect);
function GetLimitChanged: boolean;
property WriteLock: Boolean read FWriteLock write SetWriteLock;
public
Display:TLifeCel; // lijst met blokken in het zichtbare gebied.
Qcycle: boolean; // false = p kant nu, true = q kant nu.
BackCorrect: boolean;
constructor Create(MyRules: string; ANeighborhood: integer);
destructor Destroy; override;
procedure Release; virtual;
procedure AddRef; virtual;
procedure RemoveFromDisplay(c: TLifeCel);
procedure ClearDisplay;
procedure Generate(Direction: Boolean);
procedure AddToDisplay(c: TLifeCel);
procedure ChangeCelFast(x,y: integer; state: boolean);
procedure ChangeCel(x, y:integer; state: boolean);
function CelState(x, y: integer): boolean;
procedure DrawLine(x1,y1,x2,y2: integer; DragState: TLineDrawState);
function GetBoundingBoxFast: TRect;
function GetBoundingBox: TRect;
procedure ShrinkSelRect(var ARect: TRect);
function IsSelEmpty(ARect: TRect): boolean;
procedure ResetBoundingBox;
procedure LoadFromStringList(AList: TStringList);
function SaveToStringList(FileFormat: integer; IncludeTorusData: boolean): TStringList;
procedure LoadFromBitmap(ABitmap: TBitmap);
function SaveToBitmap: TBitmap; overload;
function SaveToBitmap(const ClipRect: TRect): TBitmap; overload;
function SaveToGifbitmap: TGifImage;
procedure LoadFromFile(AFile: string);
procedure SaveToFile(AFile: string; FileFormat: integer; IncludeTorusData: boolean);
function MakeSnapshotCopy: TSnapshot;
procedure RewindToSnapshot(AStream: TStream);
function IsUniverseEqual(Universe2: TUniverse): boolean;
procedure FillRandom(ARect: TRect; APercentage, PasteMode: integer);
//function SnapShotRect(ARect: TRect): TLifeShape;
function CutRect(ARect: TRect; UseClipboard: boolean): TUniverse;
function CopyRect(ARect: TRect; UseClipboard: boolean): TUniverse;
function InsertShape(AUniverse: TUniverse; APos: TPoint; PasteMode: TPasteMode): boolean;
procedure PasteRect(AUniverse: TUniverse; APos: TPoint; PasteMode: TPasteMode);
function Clone: TUniverse;
procedure DrawBox(ARect: TRect);
procedure FillRect(ARect: TRect; DoWhat: TFillAction);
procedure Clear(ClearDesc: boolean);
procedure RattleAllCages;
procedure FreshenView;
procedure StepBack;
procedure FlipX;
procedure FlipY;
//procedure BackFlip;
procedure Rotate90;
procedure Rotate180;
procedure Rotate270;
function CanPlay: boolean;
function IsEmpty: boolean;
function IsValidRule(ARule: string): boolean;
function CountCelsNow: cardinal;
function SaveProgress(Progress: Integer): Boolean;
//function GetCelCount(OnReady: TNotifyEvent): integer;
property OnRuleChange: TRuleChangeEvent read FOnRuleChange write FOnRuleChange;
property OnSaveProgress: TSaveProgressEvent read FOnSaveProgress write FOnSaveProgress;
property Locked: Boolean read FWriteLock;
property RuleString: string read GetRules write SetRules;
property Counter: integer read FCounter write FCounter;
property ClipRect: TRect read FClipRect write FClipRect;
property Description: TStringlist read FDescription write FDescription;
property Limit: TRect read FLimit write SetLimit;
property IsLimited: boolean read FIsLimited write FIsLimited;
property LimitChanged: boolean read GetLimitChanged;
property TorusKind: TTorusKind read FTorusKind write FTorusKind;
property DeadEdges: boolean read FDeadEdges write FDeadEdges;
property Neighborhood: integer read FNeighborhood write SetNeighborhood;
property PatternName: string read FPatternName write SetPatternName;
property CheckPeriod: boolean read FCheckPeriod write FCheckPeriod;
property ChecksumList: TList read FChecksumList;
property CenterPoint: TPoint read FCenterPoint write FCenterPoint;
property IsCounting: boolean read FIsCounting;
property Terminated: boolean read FTerminated write FTerminated;
end;
TChecksum = class(TObject)
private
FChecksum: integer;
FGeneration: integer;
public
constructor Create(AChecksum, AGen: integer);
property Checksum: integer read FChecksum;
property Generation: integer read FGeneration;
end;
var
StopSnapshot: boolean = false;
implementation
uses
LifeLoad, System.Types;
{ The Crunch en Munch tables process a 4x4 block to get the
inner 2x2 block. Four results are stored in eacg entry.
This requires some bit-masking in order to get results
but the alternative is four seperate tables }
type
PLifeCel = ^TLifeCel;
var
Crunch: array[0..65535] of smallint; // p --> q lookup rules
Munch: array[0..65535] of smallint; // p <-- q lookup rules
Rules: TRuleArray; // lookuptable for 1 cell.
constructor TChecksum.Create(AChecksum, AGen: integer);
begin
inherited Create;
FChecksum:= AChecksum;
FGeneration:= AGen;
end;
//The underlying commented out code does not work, why ???
(*
procedure TUniverse.CorrectUniverse;
procedure CorrectCelNeighbors(c: TLifeCel);
var
x2,y2: integer;
x,y: integer;
begin
RattleCage(c);
x:= (c.Coor.x * 16); //pin
y:= (c.coor.y * 16); //pin
if(Qcycle) then begin
x2:= (x+16); //pin
y2:= (y+16); //pin
if not Assigned(c.E) then allocateCage(x2,y)
else rattleCage(c.E);
if not Assigned(c.SE) then allocateCage(x2,y2)
else rattleCage(c.SE);
if not Assigned(c.S) then allocateCage(x,y2)
else rattleCage(c.S);
end {if}
else begin //pcycle
x2:=(x-16); //pin
y2:=(y-16); //pin
if not Assigned(c.W) then allocateCage(x2,y)
else rattleCage(c.W);
if not Assigned(c.NW) then allocateCage(x2,y2)
else rattleCage(c.NW);
if not Assigned(c.N) then allocateCage(x,y2)
else rattleCage(c.N);
end; {else}
end;
var
c: TLifeCel;
begin
c:= living.next;
while c <> living do begin
CorrectCelNeighbors(c);
c:= c.Next;
end; {while}
end; (**)
//this one does, although I don't know why.
procedure TUniverse.CorrectUniverse;
var
c: TLifeCel;
x1,y1: integer;
begin
c:= living.next;
while c <> living do begin
x1:= (c.coor.x * 16);// + integer(QCycle);
y1:= (c.coor.y * 16);// + integer(QCycle);
changecel(x1+15,y1,CelState(x1+15,y1));
changecel(x1+15,y1+15,CelState(x1+15,y1+15));
changecel(x1,y1,CelState(x1,y1));
changecel(x1,y1+15,CelState(x1,y1+15));
c:= c.Next;
end; {while}
end;
procedure TUniverse.FlipX;
var
c: TLifeCel;
Temp: integer;
a: integer;
begin
WriteLock:= true;
a:= Integer(QCycle)*2;
//First get everyone in the living list.
RattleAllCages;
//Next dismantle the hashtable
FHashTable.Clear;
//Now visit every cel and mirror it around it's X axis.
c:= living.next;
while c <> living do begin
c.FlipX(QCycle);
FHashTable.Store(c);
c:= c.Next;
end; {while}
CorrectUniverse;
with FClipRect do begin
//in p cycle the rotation axes is 7.5 in q cycle it is 8.5
Temp:= -Left+16+a;
Left:= -Right+16+a;
Right:= Temp;
end; { with }
WriteLock:= false;
end;
procedure TUniverse.FlipY;
var
c: TLifeCel;
Temp: integer;
a: integer;
begin
WriteLock:= true;
a:= Integer(QCycle)*2;
//First get everyone in the living list.
RattleAllCages;
//Next dismantle the hashtable
FHashTable.Clear;
//Now visit every cel and mirror it around it's X axis.
c:= living.next;
while c <> living do begin
c.FlipY(QCycle);
FHashTable.Store(c);
c:= c.Next;
end;
CorrectUniverse;
//in p cycle the rotation axes is 7.5 in q cycle it is 8.5
with FClipRect do begin
Temp:= -Top+16+a;
Top:= -Bottom+16+a;
Bottom:= Temp;
end; { with }
WriteLock:= false;
end;
(*procedure TUniverse.BackFlip;
var
c: TLifeCel;
x1,y1: integer;
Temp: integer;
begin
//First get everyone in the living list.
RattleAllCages;
//Next dismantle the hashtable
FHashTable.Clear;
//Now visit every cel and mirror it around it's X axis.
c:= living.next;
while c <> living do begin
c.BackFlip(QCycle);
FHashTable.Store(c);
c:= c.Next;
end;
c:= living.next;
CorrectUniverse;
with FClipRect do begin
Temp:= -Top+15;
Top:= -Bottom+15;
Bottom:= Temp;
Temp:= -Left+15;
Left:= -Right+15;
Right:= Temp;
end; { with }
end;(**)
procedure TUniverse.Rotate90;
var
c: TLifeCel;
Temp: integer;
a: integer;
begin
WriteLock:= true;
a:= integer(QCycle)*2;
RattleAllCages;
FHashTable.Clear;
c:= living.next;
while c <> living do begin
c.FlipX(QCycle);
c.BackFlip(QCycle);
FHashTable.Store(c);
c:= c.Next;
end;
CorrectUniverse;
//in p cycle the rotation axes is 7.5 in q cycle it is 8.5
with FClipRect do begin
Temp:= Left;
Left:= -Bottom+16+a;
Bottom:= Right;
Right:= -Top+16+a;
Top:= Temp;
end; { with }
with FLimit do begin
Temp:= Left;
Left:= -Bottom+16+a;
Bottom:= Right;
Right:= -Top+16+a;
Top:= Temp;
end; { with }
WriteLock:= false;
end;
procedure TUniverse.Rotate180;
var
c: TLifeCel;
Temp: integer;
a: integer;
begin
WriteLock:= true;
a:= integer(QCycle)*2;
RattleAllCages;
FHashTable.Clear;
c:= living.next;
while c <> living do begin
c.FlipY(QCycle);
c.FlipX(QCycle);
FHashTable.Store(c);
c:= c.Next;
end;
CorrectUniverse;
//in p cycle the rotation axes is 7.5 in q cycle it is 8.5
with FClipRect do begin
Temp:= Left;
Left:= -Right +16+a;
Right:= -Temp + 16+a;
Temp:= Top;
Top:= -Bottom + 16+a;
Bottom:= -Temp +16+a
end; { with }
with FLimit do begin
Temp:= Left;
Left:= -Right +16+a;
Right:= -Temp + 16+a;
Temp:= Top;
Top:= -Bottom + 16+a;
Bottom:= -Temp +16+a
end; { with }
WriteLock:= false;
end;
procedure TUniverse.Rotate270;
var
c: TLifeCel;
Temp: integer;
a,b: integer;
TempRect: TRect;
begin
WriteLock:= true;
a:= integer(QCycle);
b:= a*2;
RattleAllCages;
FHashTable.Clear;
c:= living.next;
while c <> living do begin
c.FlipY(QCycle);
c.BackFlip(QCycle);
FHashTable.Store(c);
c:= c.Next;
end;
CorrectUniverse;
//in p cycle the rotation axes is 7.5 in q cycle it is 8.5
with FClipRect do begin
Temp:= Left;
Left:= Top;
Top:= -Right + 16+b;
Right:= Bottom;
Bottom:= -Temp+16+b;
end; { with }
with Limit do begin
Temp:= Left;
TempRect.Left:= Top;
TempRect.Top:= -Right + 16+b;
TempRect.Right:= Bottom;
TempRect.Bottom:= -Temp+16+b;
end; { with }
Limit:= TempRect;
WriteLock:= false;
end;
constructor TUniverse.Create(MyRules: string; ANeighborhood: integer);
begin
inherited Create;
Qcycle:= false;
BackCorrect:=false;
FUseCount:= 1;
FPatternName:= '';
living:= TLifeCel.Create;
living.next:= living;
living.prev:= living;
living.displaynext:= living;
living.displayprev:= living;
morgue:= TLifeCel.Create;
morgue.next:= morgue;
morgue.prev:= morgue;
morgue.displaynext:= morgue;
morgue.displayprev:= morgue;
hibernating:= TLifeCel.Create;
hibernating.next:= hibernating;
hibernating.prev:= hibernating;
hibernating.displaynext:= hibernating;
hibernating.displayprev:= hibernating;
display:= TLifeCel.Create;
display.next:= display;
display.prev:= display;
display.DisplayNext:= Display;
display.DisplayPrev:= display;
caretaker:=morgue;
FHashTable:= TLifeHash.Create;
FDescription:= TStringList.Create;
FChecksumList:= TList.Create;
FTorusKind:= DefaultTorusKind;
FDeadEdges:= DefaultDeadEdges;
FNeighborhood:= ANeighborhood;
FRules:= TLifeRules.Create;
SetRules(MyRules);
ImplementRules;
end;
destructor TUniverse.Destroy;
begin
Clear(true);
FRules.Free;
FHashTable.Free;
FDescription.Free;
morgue.FreeSlow;
living.FreeSlow;
hibernating.FreeSlow;
display.FreeSlow;
inherited Destroy;
end;
procedure TUniverse.Release;
begin
Dec(FUseCount);
if FUseCount = 0 then Destroy;
end;
procedure TUniverse.AddRef;
begin
Inc(FUseCount);
end;
procedure TUniverse.SetWriteLock(Value: Boolean);
begin
try
//if we try to get a writelock while one is still on, raise an exception
if (FWriteLock and Value) then begin
FWriteLock:= false; //don't stop all action.
LifeCel.MyDDSurface.DirectDrawEnabled:=
LifeCel.MyDDSurface.DirectDrawEnabled or ddTempDisabled;
//raise Exception.Create('Universe is locked for writing');
end
else FWriteLock:= Value;
finally
LifeCel.MyDDSurface.DirectDrawEnabled:=
LifeCel.MyDDSurface.DirectDrawEnabled and not ddTempDisabled;
end;
end;
procedure TUniverse.SetLimit(Value: TRect);
begin
if not(CompareMem(@FLimit, @Value, SizeOf(Value))) then begin
FLimit:= Value;
FLimitChanged:= true;
end;
end;
function TUniverse.GetLimitChanged: boolean;
begin
Result:= FLimitChanged;
FLimitChanged:= false;
end;
//The Hexlife Rule
//--------
//
//With that in mind, I started playing around with rules based on
//different-shaped neighborhoods, but insisting on symmetry. Taking symmetries
//into account, there are three ways each of getting 2, 3, and 4 neighbors,
//for a total of 13 different neighborhoods. I tried a lot of variations, but
//the one I settled on only distinguishes between different ways of getting two
//neighbors. For these, one can use the organic chemists' naming convention
//for benzene-ring disubstitutions: ortho, meta, and para. Rich Schroeppel and
//Achim Flammenkamp suggested this, and it may be a useful mnemonic for some
//people. It has the disadvantage that there is no standard extension to
//subsets of 3 or 4 neighbors. Fortunately, we don't need this for the rule
//used here. So let's call these neighborhoods 2o, 2m, and 2p
//as follows:
//
// O O O . O .
// 2o: . x . 2m: . x O 2p: . x .
// . . . . . O
//
//Extending the birth/survival convention used above, the rule is B2o/S2m34.
//
//That is, we always have survival but not birth on 3 or 4 neighbors. On
//two neighbors we have birth but not survival if the neighbors are adjacent (2o),
//and survival but not birth if there is one empty neighbor cell in between (2m).
//If they are opposite each other (2p), there is no birth or survival (which
//is also the case for 0, 1, 5, or 6 neighbors).
//
//I settled on these rules in order to get a glider. It uses the only really
//small mechanism I'm aware of. I consider it crucial that gliders show
//up spontaneously from random starting states.
//About the Just-friends rule:
//The most interesting rule I found was this one:
//
//A cell stays alive only if it has 1 or 2 live neighbors (in any position).
//A cell is born only if it has exactly two live neighbors which are not
//adjacent vertically or horizontally.
//
//So in the following figure, the central cell is born if there are live
//cells at any two cells marked ac, ad, ae, af, ag, bd, be, bf, bg, bh,
//ce, cf, cg, ch, df, dg, dh, eg, eh, or fh.
// abc 876
// hid 543
// gfe 210
(*function TUniverse.convertRules(ARule: string): string;
const
none = ' ';
var
NewRule: string;
s: array [0..9] of boolean;
b: array [0..9] of boolean;
ch, chold: char;
HexS,HexB: char; //handle special HexLife additions, see hexrule above
unknown: boolean;
s_first: boolean;
slash: boolean;
i: integer;
neighbors: integer;
MaxNeighbors: char;
Prefix: string;
NewNeighborhood: integer;
begin
HexS:= none; HexB:= none; //assume no special hexlife handling required.
if ARule = '' then ARule:= DefaultRules;
NewRule:= '';
unknown:= true; // unknown whether S comes first, or B
s_first:= true; // S comes first, by default
slash:= false; // past the slash yet?
//strip off prefix
i:= Pos(':',ARule);
if i = 0 then Prefix:= '' //default is Moore neighborhood.
else begin
Prefix:= Copy(ARule,1,i);
Delete(Arule,1,i);
end; {else}
NewNeighborhood:= StrToNeighborhood(Prefix);
if NewNeighborhood = nbUnchanged then NewNeighborhood:= Neighborhood;
MaxNeighbors:= IntToStr(MaxNeighborsInNeighborhood(NewNeighborhood))[1];
for i:= 0 to 9 do begin
s[i]:= false;
b[i]:= false;
end; {for i}
chold:= ' ';
for i:= 1 to Length(ARule) do begin
ch:= ARule[i];
if (ch ='b') or (ch ='B') then begin
if (unknown) then begin
unknown:=false;
if (slash) then s_first:= true
else s_first:= false;
end {if}
else begin
if ((slash and not(s_first)) or (not(slash) and s_first)) then begin
Result:= newrule; // conflicting data
Exit;
end; {if}
end {else}
end {if}
else if (ch ='s') or (ch ='S') then begin
if (unknown) then begin
unknown:= false;
if(slash) then s_first:=false
else s_first:= true;
end {if}
else begin
if ((slash and s_first) or (not(slash) and not(s_first))) then begin
Result:= newrule; // conflicting data
exit;
end; {if}
end; {else}
end {else if}
else if(ch in ['/','\']) then begin
if(slash) then begin
Result:= newrule; // more than 1 slash
exit
end;
slash:= true;
end {else if}
else if (ch =' ') then break // ignore the rest
//if rule is HexLife then characters m,o,p are also allowed, see hexrule above
else if (NewNeighborhood = nbHex) and (chold = '2') and (Upcase(ch) in ['M','O','P']) then
begin
if s_first and not (slash) then HexS:= UpCase(ch)
else if s_first and (slash) then HexB:= Upcase(ch)
else if not(s_first) and not (slash) then HexB:= Upcase(ch)
else HexS:= Upcase(ch);
end {else}
else if (ch <'0') or (ch >=Succ(MaxNeighbors)) then begin
Result:= newrule; // invalid character
exit;
end; {else if}
chold:= ch; //remember previous char
end; {for i}
for i:= 1 to Length(ARule) do begin
ch:=ARule[i];
if (ch >='0') and (ch<= MaxNeighbors) then begin
if (s_first) then s[integer(ch)-$30]:= true
else b[integer(ch)-$30]:= true;
end {if}
else if(ch in ['/','\']) then s_first:= not(s_first);
end; {for i}
if (b[0]) then begin
Result:= newrule; // no 0 allowed in born list
exit;
end; {if}
for i:= 0 to 9 do if s[i] then begin
NewRule:= NewRule + Chr(i+$30);
if (i = 2) and (HexS <> ' ') then NewRule:= NewRule + HexS;
end; {for i}
newrule:= NewRule + '/';
for i:= 1 to 9 do if b[i] then begin
NewRule:= NewRule + Chr(i+$30);
if (i = 2) and (HexB <> ' ') then NewRule:= NewRule + HexB;
end; {for i}
for i:= 0 to 511 do begin
neighbors:= CountNeighbors(i,NewNeighborhood);
if NewNeighborhood = nbJustFriends then begin
Rules[i]:= ((Neighbors = 1) or (Neighbors = 2)) and JustFriendsAlive(i,NewNeighborhood);
end {if}
else begin
if (Bool(i and bit4)) then begin
Rules[i]:= s[neighbors]; {-1}
if (Neighbors = 2) and (HexS <> ' ') and
(HexRule2mop(i,NewNeighborhood) <> HexS) then Rules[i]:= false;
end
else begin
Rules[i]:= b[neighbors];
if (Neighbors = 2) and (HexB <> ' ') and
(HexRule2mop(i,NewNeighborhood) <> HexB) then Rules[i]:= false;
end; {else}
end; {else}
end; {for i}
Result:= lowercase(prefix+newrule);
end; (**)
(*function TUniverse.AreRulesOK: boolean;
var
i: integer;
Neighbors: integer;
begin
Result:= true;
for i:= 0 to 511 do begin
Neighbors:= CountNeighbors(i);
if Rules[i] then begin
if (Neighbors < 2) then Result:= false;
if (Neighbors > 3) then Result:= false;
if (not((i and $0010)>0) and (Neighbors = 2)) then Result:= false;
end
else begin
if (Neighbors = 3) then Result:= false;
if ((i and $0010)>0) and (Neighbors = 2) then Result:= false;
end; {else}
end; {for i}
end; (**)
function TUniverse.IsValidRule(ARule: string): boolean;
begin
Result:= FRules.IsValidRule(ARule);
end;
procedure TUniverse.generate_p;
var
c, cnext, cS, cSE, cE: TLifeCel;
cqstate: integer;
cSpstate, cSEpstate, cEpstate: integer;
x,y,xp, yp: Integer;
xor1,xor2,xor3: integer;
ix00, ix02, ix10, ix12, // 4x4 neighborhoods
ix01, ix03, ix11, ix13, // extracted from a
ix20, ix22, ix30, ix32, // full 8x8 block,
ix21, ix23, ix31, ix33: integer; // for table lookups.
n0, n1, n2, n3: integer; // table lookup results
begin
//This should never be called directly, so no check for writelock here.
{$ifdef lifeDebug}
DebugMemo.Lines.Add('*** P->Q generate_p');
{$endif}
{ For each cage: }
c:= Living.next;
while c <> living do begin
cnext:= c.next;
cS:=c.S;
cE:=c.E;
cSE:=c.SE;
cqstate:= c.qstate;
if Assigned(cS) then cSpstate:=cS.pstate
else cSpstate:= -1;
if Assigned(cE) then cEpstate:=cE.pstate
else cEpstate:= -1;
if Assigned(cSE) then cSEpstate:=cSE.pstate
else cSEpstate:= -1;
//if c.pstate hibernating and Spstate toprow hibernating and.
//Epstate rightedge hibernating and SEpstate corner hibernating than,
//have a closer look.
if (((c.pstate and $08080808)= $08080808) and ((cSpstate and $02000200)= $02000200)
and ((cEpstate and $04040000)= $04040000) and ((cSEpstate and $01000000)=$01000000))
then begin
//if c and neighbors are dead than
if(((c.pstate and $80808080)=$80808080) and ((cSpstate and $20002000)=$20002000)
and ((cEpstate and $40400000)=$40400000) and ((cSEpstate and $10000000)=$10000000))
then begin
//p and q in morgue
c.flags:= c.flags or $c000;
//if p and q in morgue and rattling bit is off, than destroy c.
if((c.flags and $f800)=$f000) then killCage(c);
//all dead.
cqstate:= $ffffffff;
end {if}
//if hiberanting, but not empty, then put to sleep next time.
else begin
c.flags:= c.flags or $4000;
if((c.flags and $5800)=$5000) then tranquilizeCage(c);
//all sleeping.
cqstate:= cqstate or $0f0f0f0f;
end; {else}
c.flags:= c.flags and $f7ff; // Reset the Rattling bit
end {if}
else begin
c.flags:= c.flags and $07ff;
x:= c.coor.x * 16; {maal 16}
y:= c.coor.y * 16; {maal 16}
xp:= (x+16); //pin
yp:= (y+16); //pin
// if ##|
// --+ not hibernating then.
if((c.pstate and $08020401) <> $08020401) then begin // first 8x8 active
ix00:= (c.p[0]) and $ffff; // darn signed arithmetic! // 00 11
ix10:= (c.p[1]) and $ffff; // 00 11
ix20:= (c.p[2]) and $ffff; // 22 33
ix30:= (c.p[3]) and $ffff; // 22 33
ix02:= (ix00 and $00ff) or (ix10 and $ff00); // 01 18
ix12:= (ix10 and $00ff) or (c.p[8] and $ff00); // 01 18
ix22:= (ix20 and $00ff) or (ix30 and $ff00); // 23 3A
ix32:= (ix30 and $00ff) or (c.p[10] and $ff00); // 23 3A
ix01:= (ix00 and $0f0f) or (ix20 and $f0f0); // 00 11
ix11:= (ix10 and $0f0f) or (ix30 and $f0f0); // 22 33
ix21:= (ix20 and $0f0f) or (c.p[4] and $f0f0); // 22 33
ix31:= (ix30 and $0f0f) or (c.p[5] and $f0f0); // 44 55
ix03:= (ix01 and $00ff) or (ix11 and $ff00); // 01 18
ix13:= (ix12 and $0f0f) or (ix32 and $f0f0); // 23 3A
ix23:= (ix21 and $00ff) or (ix31 and $ff00); // 23 3A
ix33:= (ix32 and $0f0f) or (ix31 and $00f0) or (c.p[12] and $f000); // 45 5C
n0:= (($f000 and Crunch[ix00])
or ($0f00 and Crunch[ix01])
or ($00f0 and Crunch[ix02])
or ($000f and Crunch[ix03]));
n1:= (($f000 and Crunch[ix10])
or ($0f00 and Crunch[ix11])
or ($00f0 and Crunch[ix12])
or ($000f and Crunch[ix13]));
n2:= (($f000 and Crunch[ix20])
or ($0f00 and Crunch[ix21])
or ($00f0 and Crunch[ix22])
or ($000f and Crunch[ix23]));
n3:= (($f000 and Crunch[ix30])
or ($0f00 and Crunch[ix31])
or ($00f0 and Crunch[ix32])
or ($000f and Crunch[ix33]));
// qstate bitmap
// My 8x8 or Ver.2x8 or Hor.8x2 or Corner
xor1:= c.q[1] xor n1;
xor2:= c.q[2] xor n2;
xor3:= c.q[3] xor n3;
if ((xor3 and $000f) = 0) then begin
if ((n3 and $000f) = 0) then // SE 2x2 corner
cqstate:= cqstate or $11000000 // morgue and hiber
else cqstate:= cqstate or $01000000; // just hibernation
if(((xor2 or xor3) and $0f0f) = 0) then begin // S 8x2 Horizontal border
if(((n2 or n3) and $0f0f) = 0) then cqstate:= cqstate or $22000000
else cqstate:= cqstate or $02000000;
end {if}
else cqstate:= cqstate and $55ffffff;
if(((xor1 or xor3) and $00ff) = 0) then begin // E 2x8 Vertical border
if(((n1 or n3) and $00ff) = 0) then cqstate:= cqstate or $44000000
else cqstate:= cqstate or $04000000;
if((xor1 or xor2 or xor3 or (c.q[0] xor n0)) = 0) then begin // whole 8x8 block
if((n0 or n1 or n2 or n3) = 0) then cqstate:= cqstate or $88000000
else cqstate:= cqstate or $08000000;
end {if}
else cqstate:= cqstate and $77ffffff;
end {if}
else cqstate:= cqstate and $33ffffff;
end {if}
else cqstate:= cqstate and $00ffffff;
c.q[0]:=n0; c.q[2]:=n2;
c.q[1]:=n1; c.q[3]:=n3;
end {if}
else begin
cqstate:= cqstate or $0f000000;
if((c.q[3] and $000f) = 0) then // SE 2x2 corner
cqstate:= cqstate or $11000000; // morgue and hiber
if(((c.q[2] or c.q[3]) and $0f0f) = 0) then
cqstate:= cqstate or $22000000;
if(((c.q[1] or c.q[3]) and $00ff) = 0) then
cqstate:= cqstate or $44000000;
if((c.q[0] or c.q[1] or c.q[2] or c.q[3]) = 0) then
cqstate:= cqstate or $88000000;
end; {else}
if(((c.pstate and $00080004) <> $00080004) or
((cSpstate and $02000100) <> $02000100)) then begin // second 8x8 (lower left):
ix00:= (c.p[4]) and $ffff;
ix10:= (c.p[5]) and $ffff;
ix20:= (c.p[6]) and $ffff;
ix30:= (c.p[7]) and $ffff;
ix02:= (ix00 and $00ff) or (ix10 and $ff00);
ix12:= (ix10 and $00ff) or (c.p[12] and $ff00);
ix22:= (ix20 and $00ff) or (ix30 and $ff00);
ix32:= (ix30 and $00ff) or (c.p[14] and $ff00);
ix01:= (ix00 and $0f0f) or (ix20 and $f0f0);
ix11:= (ix10 and $0f0f) or (ix30 and $f0f0);
if Assigned(cS) then begin // there's a Southern neighbor (hi y'all!)
ix21:= (ix20 and $0f0f) or (cS.p[0] and $f0f0);
ix31:= (ix30 and $0f0f) or (cS.p[1] and $f0f0);
ix33:= (ix32 and $0f0f) or (ix31 and $00f0) or (cS.p[8] and $f000);
end {if}
else begin
ix21:= (ix20 and $0f0f);
ix31:= (ix30 and $0f0f);
ix33:= (ix32 and $0f0f) or (ix31 and $00f0);