forked from JBontes/Life32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeLoad.pas
1327 lines (1244 loc) · 44 KB
/
LifeLoad.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 LifeLoad;
(* 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/. *)
interface
uses
WinTypes, WinProcs, Classes, SysUtils, Dialogs, Graphics, LifeGen,
LifeUtil, LifeConst;
type
TCharSet = set of ansichar;
TStringTokenizer = class(TObject)
private
FString: string;
FPos: integer;
FSeparators: TCharSet;
protected
procedure SetString(Value: string);
public
constructor Create(AString: string);
destructor Destroy; override;
function NextToken: string;
function NextNumber: integer;
property Pos: integer read FPos write FPos;
//properties are a Delphi thing.
//On the outside (see implementation) they look just like a variable.
//In fact "Pos" in fact is just that.
property TokenString: string read FString write SetString;
//This property will map to the variable FString when read,
//But when written to, the procedure SetString will be called which
//loads FString in turn, and does some other stuff too.
property Separators: TCharSet write FSeparators;
end;
//returns "true" if succesfull, false if not.
function ReadProLife(AFile: string; AUniverse: TUniverse): Boolean;
function ReadBitmap(ABitmap: TBitmap; AUniverse: TUniverse): Boolean;
function ReadLife(LifeLines: TStringList; AUniverse: TUniverse): Boolean;
function WriteLife(LifeLines: TStringList; AUniverse: TUniverse;
FileFormat: integer; IncludeTorusData: boolean): Boolean;
function WriteBitmap(ABitmap: TBitmap; AUniverse: TUniverse): Boolean;
function GetDescription(LifeLines: TStringList): TStringList;
function GetRules(LifeLines: TStringList): string;
//the above functions are public, because they are listed outside an object
//in the interface section.
var
//some initialized variables.
//In older versions of Pascal this would be a typed constant.
//In C++ it would be a static variable.
MaxPictureWidth: integer = 80;
MaxPictureHeight: integer = 80;
LastFileRules: string = DefaultRules;
DarkTreshhold: integer = 127;
implementation
uses
Forms, Unit1;
type
//Define the enumeration TLifeType.
TLifeType = (XLife, Life105, RLE, Bell, Life106, Bitmap, MCell);
const
//Defined the LifeConst.pas unit. In fact DefaultRule = '23/3'
DefaultRule = DefaultRules;
//Maximum width of a RLE pattern text file when *writing*.
RLEMAX = 70;
//TRect is defined as
//TRect = record
// Left, Top, Right, Bottom: Int16;
//end;
function IsEmptyRect(ARect: TRect): boolean;
begin
with ARect do Result:= (Left = Right) or (Top = Bottom);
end;
function GetLifeType(LifeLines: TStringList): TLifeType; forward;
procedure ReadTorusData(ALine: string; AUniverse: TUniverse); forward;
function ReadLife105(LifeLines: TStringList; AUniverse: TUniverse):Boolean; forward;
function ReadRLE(LifeLines: TStringList; AUniverse: TUniverse):Boolean; forward;
function ReadBell(LifeLines: TStringList; AUniverse: TUniverse):Boolean; forward;
function ReadXLife(LifeLines: TStringList; AUniverse: TUniverse): Boolean; forward;
function ReadMCell(LifeLines: TStringList; AUniverse: TUniverse):Boolean; forward;
procedure WriteTorusData(LifeLines: TStringList; AUniverse: TUniverse); forward;
function WriteRLE (LifeLines: TStringList; AUniverse: TUniverse;
BoundingRect: TRect; IncludeTorusData: boolean): Boolean; forward;
function WriteXLife(LifeLines: TStringList; AUniverse: TUniverse;
BoundingRect: TRect; IncludeTorusData: boolean): Boolean; forward;
function WriteLife105(LifeLines: TStringList; AUniverse: TUniverse;
BoundingRect: TRect; IncludeTorusData: boolean): Boolean; forward;
function WriteLife106(LifeLines: TStringList; AUniverse: TUniverse;
BoundingRect: TRect; IncludeTorusData: boolean): Boolean; forward;
const
Digits = ['-','0','1','2','3','4','5','6','7','8','9'];
DefaultSeparators = [' ',',','.',';',#10,#13];
SpecialChars = ['-','='];
CRLF = #13+#10;
//C++ should have a tokenizer build in. I'm not 100% sure it will
//work equivilant though.
constructor TStringTokenizer.Create(AString: string);
begin
FString:= AString;
FSeparators:= DefaultSeparators;
FPos:= 1;
end;
destructor TStringTokenizer.Destroy;
begin
inherited Destroy;
end;
function TStringTokenizer.NextNumber: integer;
var
TokenStart: integer;
TokenLength: integer;
begin
//Result:= 0;
try
while CharInSet(FString[FPos],FSeparators) and (FPos < Length(FString)) do Inc(Fpos);
while not CharInSet(FString[FPos],Digits) and (FPos < Length(FString)) do Inc(FPos);
TokenStart:= FPos;
if Length(FString) > FPos then begin
Inc(FPos);
while (Length(FString) >= FPos) and
CharInSet(FString[FPos],Digits) do Inc(FPos);
TokenLength:= FPos - TokenStart;
Result:= StrToInt(Copy(FString,TokenStart,TokenLength));
end
else if CharInSet(FString[FPos],Numbers) then Result:= StrToInt(FString[FPos])
//If something goes wrong, raise an exception that will be caught
//in the caller. (This must never reach the user).
//Some perfectly valid patterns cause this exception, but still
//show a valid pattern.
else raise EConvertError.Create('Error in NextNumber');
//Do not catch the exception here, but pass it on to the caller.
except on {EConvertError} exception do raise;
end; {try}
end;
function TStringTokenizer.NextToken: string;
var
TokenStart: integer;
TokenLength: integer;
begin
try
while (FPos < Length(FString)) and CharInSet(FString[FPos],FSeparators) do Inc(FPos);
TokenStart:= FPos;
if Length(FString) > FPos then begin
Inc(FPos);
while (Length(FString) >= FPos) and
not CharInSet(FString[FPos],FSeparators) and
not CharInSet(FString[FPos],SpecialChars) and
not CharInSet(FString[FPos],Digits)
do Inc(FPos);
TokenLength:= FPos - TokenStart;
Result:= Copy(FString,TokenStart,TokenLength);
end
else Result:= FString[FPos];
//if we read past the end of the string or whatever else goes wrong
//just give nothing as the result.
except on Exception do Result:= '';
end; {try}
end;
procedure TStringTokenizer.SetString(Value: string);
begin
FString:= Value;
FPos:= 1;
end;
//Try to read the text file and get some clue as to what kind of file it is.
//BTW, notice that LifeLoad works with stringlists, not files.
//This way the exact same code can be used for reading files and
//reading stuff being dragged or pasted in.
//
//The stringlist is just a collection of strings. One string
//for each line in the file.
//In Delphi each string can be 2 gigabyte in size.
function GetLifeType(LifeLines: TStringList):TLifeType;
var
i: integer;
RLEfound: boolean;
StartLine: string;
begin
//Note there's no exception handler, I'm just really carefull
//not to read past the end of the string, or the stringlist.
Result:= XLife;
i:= 0;
StartLine:= '';
while (i < LifeLines.count) and (StartLine = '') do begin
StartLine:= Trim(LifeLines[i]);
inc(i);
end; {while}
if Length(StartLine) > 0 then begin
if CharInSet(StartLine[1],['X','x']) then Result:= RLE
else if (Pos('LIFE 1.0',UpperCase(StartLine)) <> 0) then Result:= Life105
else if (Pos('MCELL',UpperCase(StartLine)) <> 0) then Result:= MCell
else if CharInSet(StartLine[1],['!']) or
(Pos('@!',StartLine) <> 0) and not CharInSet(StartLine[1],['#']) then
Result:= Bell
else begin
RLEfound:= false;
i:= 0;
while (i < LifeLines.Count) and not(RLEfound) do begin
if (Length(LifeLines[i]) > 0) and (LifeLines[i][1] <> '#') then begin
if ((Pos('B',UpperCase(LifeLines[i])) <> 0) and
(Pos('O',UpperCase(LifeLines[i])) <> 0) and
((Pos('$',UpperCase(LifeLines[i])) <> 0)) or (Pos('!',UpperCase(LifeLines[i])) <> 0)) then RLEFound:= true;
end;
inc(i);
end; {while}
//for one line patterns (no '$'), not terminated by a '!'.
//it still could be an RLE pattern, lets look for one more tellsign.
i:= 0;
while not(RLEFound) and (i < LifeLines.Count) do begin
if (Length(LifeLines[i])>0) and (Upcase(LifeLines[i][1]) = 'X') then
RLEFound:= true;
inc(i);
end; {while}
if RLEfound then Result:= RLE
else Result:= XLife;
end; {else}
end;
end;
function ReadLife(LifeLines: TStringList; AUniverse: TUniverse):Boolean;
begin
Result:= true;
try
//do *not* Default to life-rules.
//AUniverse.RuleString:= '';
case GetLifeType(LifeLines) of
RLE: Result:= ReadRLE(LifeLines,AUniverse);
XLife: Result:= ReadXLife(LifeLines,AUniverse);
Life105: Result:= ReadLife105(LifeLines,AUniverse);
Bell: Result:= ReadBell(LifeLines,AUniverse);
MCell: Result:= ReadMCell(LifeLines,AUniverse);
end; {case}
//This will happen very rarely. The loader takes just about anything.
//Even random text will be accepted. The user is quite able to detect
//from the result on the screen that an error occured, we don't need an
//error box for that!
except on E:exception do
ShowMessage('Error reading file '+#10+#13+
'Picture may be damaged'+#10+#13+
'Error: '+E.message);
end; {try}
end;
//This function filters out the description from a pattern.
//Usefull for the preview function.
//It also filters out the rule if there is any clue in the pattern file.
//This is stored in the unit variable "LastFileRules".
function GetDescription(LifeLines: TStringList): TStringList;
var
i,a: integer;
LifeType: TLifeType;
DescHeader: string;
PastRLE: boolean;
ALine: string;
begin
Result:= TStringList.Create;
LastFileRules:= DefaultRules;
try
PastRLE:= false;
LifeType:= GetLifeType(LifeLines);
for i:=0 to LifeLines.Count-1 do begin
ALine:= LifeLines[i]+' ';
DescHeader:= UpperCase(Copy(ALine,1,2));
if (DescHeader[1] = '#') and not(PastRLE) then case DescHeader[2] of
'D','C','O': Result.Add(Copy(ALine,3,Length(ALine)-4));
'N': begin
if Length(Trim(ALine)) > 2 then
Result.Add(Copy(ALine,3,Length(ALine)-4))
else LastFileRules:= DefaultRules;
end; {'N'}
'R': begin
if (LifeType <> XLife) or (upcase(ALine[2]) = 'R') then LastFileRules:= Copy(ALine,3,Length(ALine)-4)
else LastFileRules:= DefaultRules;
end;
end; {if case}
if (LifeType = Bell) or (LifeType = RLE) then begin
if (CharInSet(ALine[1],['!'])) then Result.Add(Copy(ALine,2,Length(ALine)-3))
else if not ((LifeType = RLE) and (PastRLE)) then begin
a:= Pos('RULE',Uppercase(ALine));
if a <> 0 then LastFileRules:= Copy(ALine,a+4,Length(ALine)-(a+5));
end; {else}
end;
if (LifeType = RLE) and (PastRLE) then begin
DescHeader:= UpperCase(Copy(ALine,1,2));
if (DescHeader[1] = '#') then case DescHeader[2] of
'D','C','O': Result.Add(Copy(ALine,3,Length(ALine)-4));
'N': if Length(Trim(LifeLines[1])) > 2 then
Result.Add(Copy(ALine,3,Length(ALine)-4));
end {if case}
else Result.Add(Copy(ALine,1,Length(ALine)-2));
end; {if}
if (LifeType = RLE) and (Pos('!',ALine)<>0) and (DescHeader[1]<> '#') then begin
PastRLE:= true;
end; {if}
end; {for}
except begin
Result.Free;
Result:= nil;
end; {except}
end; {try}
end;
//This function only works OK when GetDescription has been called first.
//On the pattern at hand.
function GetRules(LifeLines: TStringList): string;
var
i: integer;
begin
Result:= LastFileRules;
i:= Pos('=',Result);
if i <> 0 then Delete(Result,i,1);
Result:= Trim(Result);
end;
//the LifeLoad unit enforces the rule, that it will accept anything
//you throw at it when reading.
//But it is very strict when writing files.
//Read a Run-length encoded description from a stringlist.
function ReadRLE(LifeLines: TStringList; AUniverse: TUniverse): Boolean;
var
st: TStringTokenizer;
AWidth, AHeight: integer;
AVal: integer;
i: integer;
APos: integer;
MinX,x,y: integer;
ready, TorusActive: boolean;
begin
TorusActive:= false;
st:= TStringTokenizer.Create('');
//default in case there is no leading line.
//In reality the AHeight and AWidth lines are ignored.
//I just use them to figure out what the middle of the pattern
//is, so I can center the pattern at coordinate (0,0).
//Hence the 2,2 (1 div 2 = 0, no good).
AHeight:= 2;
AWidth:= 2;
try
Result:= true;
i:= 0;
//Skip empty lines.
while Length(TrimLeft(LifeLines[i])) = 0 do Inc(i);
st.TokenString:= Trim(LifeLines[i]);
//first get rid of all '#' lines.
if st.TokenString[1] = '#' then begin
while (st.TokenString[1] = '#') and (i < LifeLines.count) do begin
if (Length(st.TokenString) > 2) and (st.TokenString[2] = 'T') then begin
TorusActive:= true;
ReadTorusData(st.TokenString, AUniverse);
end;
Inc(i);
st.TokenString:= Trim(LifeLines[i]);
end; {while}
//Dec(i);
end;
//Every RLE file should begin with an "X".
if (Upcase(st.NextToken[1]) = 'X') then begin
if st.NextToken[1] <> '=' then AWidth:= 2
else try
AWidth:= st.NextNumber;
if (AWidth <= 0) then AWidth:= 2;
except AWidth:= 2;
end; {try}
if (Upcase(st.NextToken[1]) = 'Y') then begin
if st.NextToken <> '=' then AHeight:= 2
else try
AHeight:= st.NextNumber;
if (AHeight <= 0) then AHeight:= 2;
except AHeight:= 2;
end; {try}
end;
if st.NextToken = 'rule' then begin
if st.NextToken = '=' then
AUniverse.RuleString:= (Trim(Copy(st.Tokenstring,st.Pos,Length(st.TokenString)-st.Pos+1)));
end; {if}
Inc(i);
end; {if}
if not TorusActive then begin
y:= 0 - (AHeight div 2);
x:= 0 - (AWidth div 2);
end
else begin
x:= AWidth;
y:= AHeight;
end;
MinX:= x;
Ready:= false;
while (i < LifeLines.Count) and not(Ready) do begin
st.TokenString:= LifeLines[i];
APos:= 1;
while (APos <= Length(LifeLines[i])) and not(Ready) do begin
AVal:= 1;
if CharInSet(st.TokenString[APos],Digits) then begin
st.Pos:= APos;
AVal:= st.NextNumber;
APos:= st.Pos;
end;
case st.TokenString[APos] of
'b','B':Inc(x,AVal);
'$':begin
Inc(y,AVal);
x:= MinX;
end;
' ',#9,#10,#13:; {do nothing}
'!': Ready:= true;
//Also accept other chars then "O" and "o" as on cells,
//because some patterns also "X" and even more bizarre
//chars for the on cell.
else {'o','O': and others} while AVal > 0 do begin
AUniverse.ChangeCel(x,y,true);
Inc(x); Dec(AVal);
end;
end; {case}
Inc(APos);
end; {while}
Inc(i);
end; {while}
//make sure st gets cleaned up. Even if an A-bomb hits our CPU.
//see the "try" above.
finally st.Free;
end;
end;
procedure ReadTorusData(ALine: string; AUniverse: TUniverse);
var
ARect: TRect;
st: TStringTokenizer;
Option: string;
begin
st:= TStringTokenizer.Create(ALine);
try
if length(ALine) < 3 then exit;
st.NextToken; {skip the #Tx part}
case Upcase(ALine[3]) of
'D': begin
try
with ARect do begin
Left:= st.NextNumber; Top:= st.NextNumber;
Right:= Left + st.NextNumber - 1;
Bottom:= Top + st.NextNumber - 1;
AUniverse.Limit:= ARect;
AUniverse.IsLimited:= true;
end;
except {ignore}
end;
end; {D:}
'O': begin
try
repeat
option:= Uppercase(st.NextToken);
if (option = 'WRAP') then begin
if st.NextToken <> '=' then AUniverse.DeadEdges:= false
else try
AUniverse.DeadEdges:= not(boolean(st.NextNumber));
except AUniverse.DeadEdges:= false;
end; {try}
end; {if WRAP}
if (option = 'KIND') then begin
if st.NextToken <> '=' then AUniverse.TorusKind:= tk_All
else try
AUniverse.TorusKind:= st.NextNumber;
except AUniverse.TorusKind:= tk_All;
end; {try}
end; {if KIND}
until Length(Option) < 4;
except {ignore}
end;
end; {O:}
end; {case}
finally
st.Free;
end; {try}
end;
//This procedure will just try to interpret every line as best it can.
//In many ways it goes way beyond the Life 1.05 and 1.06 spec.
//This is really a copy of the ReadXLife routines, with some adjustments
//to facilitate the differences between XLife and Life 1.05.
function ReadLife105(LifeLines: TStringList; AUniverse: TUniverse):Boolean;
var
Start: string;
x1,y1: integer;
a,b,i,j: integer;
st: TStringTokenizer;
c: Char;
begin
st:= TStringTokenizer.Create('');
try
y1:= 0; x1:= 0; //just to kill compiler warnings
Result:= true;
//Remove #Life 1.0x line.
//We know for sure it will be there, otherwise we would never
//have gotten here.
LifeLines.Delete(0);
for i:= 0 to LifeLines.Count -1 do begin
//first clean up the trash
LifeLines[i]:= Trim(LifeLines[i]);
a:= Length(LifeLines[i]);
if a > 0 then begin
if a=1 then Start:= LifeLines[i][1]+' '
else Start:= LifeLines[i][1] + LifeLines[i][2];
st.TokenString:= LifeLines[i];
if Start[1]='#' then begin
st.NextToken;
case Start[2] of
'T','t': begin
//TorusActive:= true;
ReadTorusData(st.TokenString, AUniverse);
end;
'D','d': begin {comments:ignore} end; {'D'}
'P','p': begin
try
x1:= st.NextNumber;
except on EConvertError do x1:= 0;
end; {try}
try
y1:= st.NextNumber;
except on EConvertError do y1:= 0;
end; {try}
end; {'P':}
'N','n': AUniverse.RuleString:= DefaultRules;
'R','r': try
AUniverse.RuleString:= (Copy(st.TokenString,st.Pos,(Length(st.TokenString)-(st.Pos-1))));
except AUniverse.RuleString:= DefaultRules;
LastFileRules:= AUniverse.RuleString;
end; {'R'}
'S','s': begin end; {this is the playback speed, ignore for now}
'C','c': begin end; {XLife style comments, ignore. }
end; {case}
end
//*,O,o = On, this does not comply with Xlife 3.5 (i think)
//Only '.' is off, this is also not the same as Xlife.
else if CharInSet(Start[1],['.','*','o','O','$']) then begin
b:= 0;
for j:= 1 to Length(LifeLines[i]) do begin
c:= LifeLines[i][j];
if CharInSet(c,['*','o','O']) then AUniverse.ChangeCel(x1+j+b,y1,true)
else if c = '$' then Inc(b,10);
end; {for j}
inc(y1);
end {else if}
else if not CharInSet(start[1],['!']) then begin
//let's try if these are normal coordinates, ignore otherwise.
try
x1:= st.NextNumber;
y1:= st.NextNumber;
AUniverse.ChangeCel(x1,y1,true);
except on EConvertError do begin {do nothing} end;
end; {try}
end; {if}
end; {if}
end; {for i}
finally st.Free;
end; {try}
end;
// In MCell an RLE format is used.
//every pattern line is preceded by a '#L' header
//and use '.' for off
//'A'..'X' for on
//$ and numbers as usual. no terminating '!' is used.
function ReadMCell(LifeLines: TStringList; AUniverse: TUniverse):Boolean;
type
TGameType = (Life, Unsupported);
var
MinX: integer;
i,NoOfTokens: integer;
p: integer;
st: TStringTokenizer;
ALine: string;
APos: integer;
AVal: integer;
x,y: integer;
GameType: TGameType;
Speed: integer;
begin
st:= TStringTokenizer.Create('');
//st.Separators:= DefaultSeparators - ['.'];
//Default to Life games.
GameType:= Life;
try
//just to kill compiler warnings
Result:= true;
//Find the #Game header line.
i:= 0;
while (i < LifeLines.Count) and (Pos('#GAME',Uppercase(LifeLines[i])) = 0)
do Inc(i);
if i < LifeLines.Count then begin
st.TokenString:= LifeLines[i];
p:= Pos('GAME',Uppercase(LifeLines[i]));
if p <> 0 then begin
st.Pos:= p + Length('Game');
if Uppercase(st.NextToken) = 'LIFE' then GameType:= Life
else if Uppercase(Trim(LifeLines[i])) = '#GAME' then GameType:= Life
else GameType:= Unsupported;
end; {if}
end; {if}
if GameType = Life then begin
//Find the #Rule header line.
i:= 0;
NoOfTokens:= 2; //search for 2 tokens.
while (NoOfTokens > 0) do begin
while (i < LifeLines.Count) and (
(Pos('#RULE',Uppercase(LifeLines[i])) = 0) and
(Pos('#SPEED',Uppercase(LifeLines[i])) = 0))
do Inc(i);
if i < LifeLines.Count then begin
Dec(NoOfTokens);
st.TokenString:= LifeLines[i];
p:= Pos('RULE',Uppercase(LifeLines[i]));
if p <> 0 then begin
st.Pos:= p + Length('RULE');
AUniverse.RuleString:= (Trim(Copy(st.Tokenstring,st.Pos,Length(st.TokenString)-st.Pos+1)));
end; {if}
p:= Pos('SPEED',Uppercase(LifeLines[i]));
if p <> 0 then begin
st.Pos:= p + Length('SPEED');
Speed:= st.NextNumber;
try
TLife32MainForm(Application.MainForm).PlaySpeed:= Speed;
except {ignore}
end;
end; {if}
inc(i);
end {if}
else NoOfTokens:= 0;
end;
//Now read the Extended RLE lines.
i:= 0;
while (i < LifeLines.Count) and (Pos('#L',Uppercase(LifeLines[i])) = 0) do Inc(i);
y:= 0;
x:= 0;
MinX:= x;
while (i < LifeLines.Count) do begin
if (Pos('#L',Uppercase(LifeLines[i])) <> 0) then begin
//Cut of #L from line
ALine:= Copy(LifeLines[i]+' ',3,Length(LifeLines[i]));
st.TokenString:= ALine;
APos:= 1;
while (APos <= Length(ALine)) do begin
AVal:= 1;
if CharInSet(st.TokenString[APos],Digits) then begin
st.Pos:= APos;
AVal:= st.NextNumber;
APos:= st.Pos;
end;
case st.TokenString[APos] of
'.':Inc(x,AVal);
'$':begin
Inc(y,AVal);
x:= MinX;
end;
' ',#9,#10,#13,'!':; {do nothing}
//Also accept other chars then "A" and "a" as on cells,
//because some patterns also "X" and even more bizarre
//chars for the on cell.
else {'A','a': and others} while AVal > 0 do begin
AUniverse.ChangeCel(x,y,true);
Inc(x); Dec(AVal);
end;
end; {case}
Inc(APos);
end; {while}
end; {if '#L' in line}
Inc(i);
end; {while}
end; {if FameType = Life}
finally st.Free;
end; {try}
end;
function ReadBell(LifeLines: TStringList; AUniverse: TUniverse):Boolean;
var
MinX,x1,y1: integer;
a,i,j,k: integer;
p: integer;
st: TStringTokenizer;
Blanks: integer;
begin
st:= TStringTokenizer.Create('');
try
y1:= 0; x1:= 0; MinX:= 0; //just to kill compiler warnings
Result:= true;
i:= 0;
//Skip any leading empty or comment lines.
while (i < LifeLines.Count) and
(Pos('CELLS',Uppercase(LifeLines[i])) = 0) do inc(i);
if i < LifeLines.Count then begin
st.TokenString:= LifeLines[i];
p:= Pos('LENGTH',Uppercase(st.TokenString));
if p <> 0 then begin
st.Pos:= p + Length('Length');
try
y1:= -(st.NextNumber div 2);
except y1:= 0;
end; {try}
end; {if}
p:= Pos('WIDTH',Uppercase(st.TokenString));
if p <> 0 then begin
st.Pos:= p + Length('Width');
try
MinX:= -(st.NextNumber div 2);
except MinX:= 0;
end; {try}
x1:= MinX;
end; {if}
end; {if}
for i:= 0 to LifeLines.Count -1 do begin
//first clean up the trash
LifeLines[i]:= TrimLeft(LifeLines[i]);
a:= Length(LifeLines[i]);
if a > 0 then begin
if (LifeLines[i][1] <> '!') then begin
j:= 1;
while j <= Length(LifeLines[i]) do begin
case LifeLines[i][j] of
'0'..'9': begin
Blanks:= 0;
while (j < Length(LifeLines[i])) and CharInSet(LifeLines[i][j],Digits) do begin
Blanks:= Blanks * 10;
Blanks:= Blanks + byte(LifeLines[i][j]) - byte('0');
Inc(j);
end; {while}
case LifeLines[i][j] of
'0'..'9': begin //This means line ends with digits, this many blank lines.
Blanks:= Blanks * 10;
Blanks:= Blanks + byte(LifeLines[i][j]) - byte('0');
Inc(y1,Blanks-1);
end; {'0'..'9'}
' ','.':Inc(x1,Blanks);
'*','O','o':for k:= 1 to Blanks do begin
AUniverse.ChangeCel(x1,y1,true);
Inc(x1);
end; {'*'..}
//ignore this stuff.
//'k','K':if (Pos('@!',LifeLines[i]) <> 0) then Inc(MinX,Blanks);
//'j','J':if (Pos('@!',LifeLines[i]) <> 0) then Dec(MinX,Blanks);
//'h','H':if (Pos('@!',LifeLines[i]) <> 0) then Dec(y1,Blanks);
//'l','L':if (Pos('@!',LifeLines[i]) <> 0) then Inc(y1,Blanks);
end; {case}
end; {Digits}
' ','.':Inc(x1);
'*','o','O':begin
AUniverse.ChangeCel(x1,y1,true);
Inc(x1);
end; {'*'..}
end; {case}
Inc(j);
end; {while}
x1:= MinX;
inc(y1);
end {if}
end; {if}
end; {for i}
finally st.Free;
end; {try}
end;
type
TLoadMode = (lm_Absolute, lm_Relative, lm_Picture);
//For now this will only read XLife 2.0 files (and stuff that looks a lot
//like it). Not #I XLife files.
//Note that Life32 supports this, but #I really is hard to code.
//If anyone can help me with this, please do.
function ReadXLife(LifeLines: TStringList; AUniverse: TUniverse):Boolean;
var
Start: string;
Temp: string;
x1,y1,XOffset,YOffset: integer;
a,i,j: integer;
st: TStringTokenizer;
LoadMode: TLoadMode;
EmptyLine: boolean;
begin
st:= TStringTokenizer.Create('');
try
XOffset:= 0; YOffset:= 0;
Result:= true;
LoadMode:= lm_Absolute; //Pick a default.
for i:= 0 to LifeLines.Count -1 do begin
//first clean up the trash
//LifeLines[i]:= Trim(LifeLines[i]);
st.TokenString:= LifeLines[i];
//remove '>' in front of picture files, so pattern gets read correctly.
Temp:= LifeLines[i] + ' ';
repeat
a:= Pos('>',Temp);
if (a <> 0) and (Pos('#',Temp) = 0) then Delete(Temp,a,1);
until a = 0;
LifeLines[i]:= temp;
//now start doing normal stuff.
Temp:= trim(Temp);
a:= Length(Temp);
if a=0 then Start:= ' '
else if a=1 then Start:= Temp[1]+' '
else Start:= Temp[1] + Temp[2];
//'!' is used by dbLife (oh those non standard file formats blues).
//'@' is used by Noam Elkies to highlight stuff in his emails <sigh>
//'N' is used because Noam somethings sets his initials slam bam in the middle
//of the pattern text, program reads letters NDE and blanks then, so pattern
//runs correctly.
if CharInSet(Upcase(Start[1]),['.','*','o','O','!','$','@',' ','N']) then LoadMode:= lm_Picture;
if Start[1] = '#' then begin
st.NextToken; //Get rid of #? 'cause that is already in Start.
case Start[2] of
'T': begin
//TorusActive:= true;
ReadTorusData(st.TokenString, AUniverse);
end;
'N':;//PatternName:= st.NextToken;
'P':begin
LoadMode:= lm_Picture;
try
XOffset:= st.NextNumber;
YOffset:= st.NextNumber;
except begin XOffset:= 0; YOffset:= 0; end;
end; {try}
end; {'P':}
'R':begin
//Test to see if this really could be a rule line.
//Uppercase matters here, but I'm using a workaround for that.
//#r ./.. is use rule "./.." and #R is loadmode relative, or the other
//way around. Anyway, if #R is alone it's the Relative loadmode thing.
//Otherwise something is listed, and then we'll use that something
//for a rule. The clue here is the "/" char, which is only used in
//a rule.
//This avoids an incompatibility between XLife and Life 1.05.
if Pos('/',st.TokenString) <> 0 then try
AUniverse.RuleString:= (Copy(st.TokenString,st.Pos,(Length(st.TokenString)-(st.Pos-1))));
except AUniverse.RuleString:= DefaultRules;
end {if}
else begin
LoadMode:= lm_Relative;
try
XOffset:= st.NextNumber;
YOffset:= st.NextNumber;
except begin XOffset:= 0; YOffset:= 0; end;
end; {try}
end; {else}
end; {'R':}
'A':LoadMode:= lm_Absolute;
'B':begin {start a pattern block} end;
'E':begin {end a pattern block} end;
//This exception will make it to the user.
//A dialog is shown with the text below.
'I':raise Exception.Create('Can''t read XLife #I format yet');
'r':try
AUniverse.RuleString:= (Copy(st.TokenString,st.Pos,(Length(st.TokenString)-(st.Pos-1))));
except AUniverse.RuleString:= DefaultRules;
end; {'r':}
end; {case}
end {if}
else if not CharInSet(Start[1],['!']) then case LoadMode of
lm_Absolute:try
x1:= st.NextNumber;
y1:= st.NextNumber;
AUniverse.changeCel(x1,y1,true);
except {ignore this line}
end; {lm_absolute}
lm_Relative:try
x1:= st.NextNumber;
y1:= st.NextNumber;
AUniverse.ChangeCel(XOffset+x1,YOffset+y1,true);
except {ignore this line}
end; {lm_Relative}
lm_Picture:begin
x1:= 0;
EmptyLine:= true;
for j:= 1 to Length(st.TokenString) do begin
case st.TokenString[j] of
'$':begin //replace a "$" with 10 blanks.
Inc(x1,10);
EmptyLine:= false;
end; {'$':}
//Noam Elkies initials inside pattern description should be
//treaded as blanks.
'.',' ','N','D','E':begin
Inc(x1);
EmptyLine:= false;
end; {'.':}
'O','o','*','@':begin //'@' used by NDE.
EmptyLine:= false;
AUniverse.ChangeCel(XOffset+x1,YOffset,true);
Inc(x1);
end; {'*':}
end; {case}
end; {for j}
if not EmptyLine then Inc(YOffset);
end; {lm_Picture}
end; {case}
end; {for i}
finally st.Free;
end; {try}
end;
//ProLife does not use a text file, but a binary one.
//So any binary file is send to this reader, which checks to see
//if it really is a valid ProLife file.
function ReadProLife(AFile: string; AUniverse: TUniverse): Boolean;
var
BoundaryCondition: word;
x,xMax,y,yMax: word;
LifeField: array[0..19,0..159] of word;
LifeFile: file of word;
Mask: word;
i: integer;
begin
Result:= false;
try
AssignFile(LifeFile,AFile);
Reset(LifeFile);
//The above two statements open the file for READING.
//do some checking to ensure we have a valid ProLife file.
Read(LifeFile,BoundaryCondition);
Read(LifeFile,xMax);
if (XMax > 19) then Abort;
Read(LifeFile,yMax);
if (YMax > 159) then Abort;
i:= FileSize(LifeFile) * SizeOf(Word);
if i <> (((XMax+1) * (YMax+1) * 2)+ (2* 3)) then Abort;
//All seems OK, so lets set the rules to conway's Default;
//ProLife only supports Conway's default, so there is no doubt.
AUniverse.RuleString:= DefaultRules;
LastFileRules:= DefaultRules;
//Read the stuff into a B I T map.
for y:= 0 to yMax do begin
for x:= 0 to xMax do begin
Read(LifeFile,LifeField[x,y]);
end; {for x}
end; {for y}
CloseFile(LifeFile);
//Now we read the lifefield bit by bit and store it into the universe.
for y:= 0 to yMax do begin
for x:= 0 to xMax do begin
Mask:= $8000;
for i:= 0 to 15 do begin
if (LifeField[x,y] and Mask) <> 0 then AUniverse.ChangeCel((x*16)+i,y,true);
Mask:= Mask shr 1;
end; {for i}
end; {for x}
end; {for y}
Result:= true; //succes.
//Catch any exceptions here.
except CloseFile(LifeFile);
end; {try}
end;
type
TBGR = array[0..3] of byte;
function ReadBitmap(ABitmap: TBitmap; AUniverse: TUniverse): boolean;