-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLifeUtil.pas
549 lines (490 loc) · 13.2 KB
/
LifeUtil.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
unit LifeUtil;
(* 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
Windows, Classes, SysUtils, Controls, Graphics, Forms, Menus, LifeConst,
registry, inifiles;
function OpenReg: TCustomIniFile;
function Max(a,b: integer): integer;
function Min(a,b: integer): integer;
function FastRandom(MaxVal: integer): integer;
procedure SwapInt(var a,b: integer);
function IntToOct(value:longint; digits:integer):string;
function OctToInt(value:string):longint;
function ProcentToInt(S: string): integer;
procedure WaitCursor;
procedure DefaultCursor;
function OffsetRect(var ARect: TRect; dx,dy: integer): Bool;
function UnionRect(var Dest: TRect; Rect1,Rect2: TRect): bool;
function IsMyRectEmpty(ARect: TRect): boolean;
function CorrectRect(ARect: TRect): TRect;
function Proper(Astr: string): string;
procedure StripLFCR(var AStr: string; var Column: integer);
//procedure PictureMenu(AMenu: TMenuItem; Refresh: boolean);
procedure DebugBeep;
function MyGetTickCount: integer;
function GetPrefix(ARule: string): string;
function StripPrefix(ARule: string): string;
function StrToNeighborhood(S: string): integer;
function NeighborhoodToStr(N: integer): string;
function NeighborhoodToStrLong(N: integer): string;
function CountNeighbors(Lookup, Neighborhood: integer): integer;
function JustFriendsAlive(Lookup, Neighborhood: integer): boolean;
function HexRule2mop(Lookup, Neighborhood: integer): char;
function InsertPrefix(ARule, APrefix:string): string;
function MaxNeighborsInNeighborhood(N: integer): integer;
type
TStartDragHack = class(TThread)
protected
procedure Execute; override;
public
constructor Create;
end;
implementation
uses
TickTime;
var
BitmapList: TList = nil;
TagList: TList = nil;
IntelInstalled: boolean = false;
TickTimer: TTickTimer;
constructor TStartDragHack.Create;
begin
//Start executing right away.
inherited Create(false);
end;
//move the mouse a little back and forth to enter into the
//Drag and drop loop.
procedure TStartDragHack.Execute;
var
APos: TPoint;
begin
//First wait awhile, so we don't move the mouse too soon.
Sleep(100);
//Now get the position of the mouse.
GetCursorPos(APos);
//move it a little forth
SetCursorPos(APos.x+1,APos.y);
Sleep(50);
//and back.
SetCursorPos(APos.x,APos.y);
end;
function OpenReg: TCustomInifile;
const
DontCreate = false;
MakeIt = true;
begin
try
//Result:= TIniFile.Create('Life32.ini');
Result:= TRegistryIniFile.Create('Software\Life32');
with Result do begin
//Rootkey:= HKEY_LOCAL_MACHINE;
//if not(OpenKey('SOFTWARE',DontCreate)) then
// raise Exception.Create('Registry may be damaged');
//if not(OpenKey('JBontes\Life32\Settings',MakeIt)) then
// raise Exception.Create('Can''t save settings to registry');
end; {with}
except
Result:= nil;
end;
end;
function Max(a,b: Integer): integer; assembler;
asm
cmp EAX,EDX
jg @@aMax
mov EAX,EDX
@@aMax:
end;
function Min(a,b: Integer): integer; assembler;
asm
cmp EAX,EDX
jl @@aMin
mov EAX,EDX
@@aMin:
end;
function FastRandom(MaxVal: integer): integer;
const
X: integer = 0;
m= 65535;
a= 37;
c= 13;
begin
Result:= (a*X+c) mod M;
X:= Result;
Result:= Result mod MaxVal;
end;
procedure SwapInt(var a,b: integer);
{$ifdef win32}
//EAX = a; EDX=b
asm
push EBX
mov ECX,[EAX]
mov EBX,[EDX]
mov [EDX],ECX
mov [EAX],EBX
pop EBX
end;
{$else}
var
Temp: integer;
begin
temp:= a;
a:= b;
b:= temp;
end;
{$endif}
//convert an Integer to an octal number
function IntToOct(value:longint; digits:integer):string;
const
Z_Car = ord('0');
var
i: integer;
begin
Result:= '';
repeat
Result:= char(Z_Car + (Value and $07)) + Result;
Value:= Value shr 3;
until Value = 0;
if Length(Result) < digits then for i:= Length(Result)+1 to Digits do
Result:= '0' + Result;
end;
//convert an octal number to integer ?
function OctToInt(value:string):longint;
var
i : integer;
a : integer;
int: integer;
begin
Int:=0;
for i:=1 to length(value) do begin
a:= Ord(value[i]) - Ord('0');
if (a > 7) or (a < 0) then EconvertError.Create('Not a valid octal value');
Int:=Int * 8 + a;
end;
Result:=Int;
end;
function ProcentToInt(S: string): integer;
var
i: integer;
begin
i:= Pos('%',S);
if i <> 0 then Delete(S,i,1);
Result:= StrToInt(s);
end;
function UnionRect(var Dest: TRect; Rect1,Rect2: TRect): bool;
begin
if IsMyRectEmpty(Rect1) then Dest:= Rect2
else if IsMyRectEmpty(Rect2) then Dest:= Rect1
else begin
Dest.Left:= Min(Rect1.Left,Rect2.Left);
Dest.Top:= Min(Rect1.Top,Rect2.Top);
Dest.Right:= Max(Rect1.Right,Rect2.Right);
Dest.Bottom:= Max(Rect1.Bottom,Rect2.Bottom);
end;
Result:= not(IsMyRectEmpty(Dest));
end;
function OffsetRect(var ARect: TRect; dx,dy: integer): Bool;
begin
Result:= true;
with ARect do begin
Inc(Left,dx);
Inc(Right,dx);
Inc(Top,dy);
Inc(Bottom,dy);
end; {with}
end;
function IsMyRectEmpty(ARect: TRect): boolean;
begin
Result:= (ARect.Left = ARect.Right) and (ARect.Top = ARect.Bottom);
end;
function CorrectRect(ARect: TRect): TRect;
begin
with ARect do begin
if Left > Right then SwapInt(Left,Right);
if Top > Bottom then SwapInt(Top,Bottom);
end;
Result:= ARect;
end;
function Proper(Astr: string): string;
begin
if AStr = Uppercase(AStr) then Astr:= lowercase(AStr);
if length(Astr) >=1 then Astr[1]:= Upcase(Astr[1]);
Result:= Astr;
end;
procedure StripLFCR(var AStr: string; var Column: integer);
var
i: integer;
removed: boolean;
begin
try
i:= 1;
AStr:= AStr + ' '; //add a space so we can look ahead.
while i < Length(AStr) do begin
//replace whitespace with ' '.
if CharInSet(Astr[i],[#1..#31,'"','''']) then Astr[i]:= ' ';
inc(i);
end; {while}
i:= 1;
while i < Length(AStr) do begin
//now remove double spaces.
removed:= false;
if (AStr[i] = ' ') and (AStr[i+1] = ' ') then begin
Delete(AStr,i,1);
if (Column >= i) then Dec(Column);
removed:= true;
end;
if not removed then Inc(i);
end;
//remove the trailing space.
Delete(AStr,Length(AStr),1)
except
AStr:= ' ';
end;
end;
procedure WaitCursor;
begin
Screen.Cursor:= crHourGlass;
end;
procedure DefaultCursor;
begin
//Screen.Cursor:= crDefault;
Screen.Cursor:= crTransparentArrow;
end;
procedure DebugBeep;
//var
//i: integer;
begin
//for i:= 0 to 5 do MessageBeep(0);
end;
function MyGetTickCount: integer;
begin
if IntelInstalled then Result:= TickTimer.MilliTimePast
else Result:= Windows.GetTickCount;
end;
function GetPrefix(ARule: string): string;
var
i: integer;
begin
Result:= '';
i:= Pos(':',ARule);
if i <> 0 then Result:= Copy(ARule,1,i);
Result:= uppercase(Result);
end;
function StripPrefix(ARule: string): string;
var
MOP: integer;
Prefix: string;
begin
Result:= ARule;
Prefix:= GetPrefix(ARule);
if Prefix = 'M:' then Delete(Result,Pos(':',ARule)-1,Length(Prefix));
if Prefix = 'H:' then begin
mop:= Pos('2m',lowercase(result)) + Pos('2o',lowercase(result)) +
Pos('2p',lowercase(result));
if mop > 0 then Delete(Result,Pos(':',ARule)-1,Length(Prefix));
end; {if}
end;
function InsertPrefix(ARule, APrefix:string): string;
var
i: integer;
begin
Result:= ARule;
i:= Pos(':',ARule);
if i <> 0 then Delete(ARule,1,i);
i:= Pos(':',APrefix);
if i <> 0 then Delete(APrefix,i,1);
if APrefix = '' then APrefix:= NeighBorHoodToStr(nbDefault)
else APrefix:= APrefix + ':';
Result:= APrefix + ARule;
end;
function CountNeighbors(Lookup, Neighborhood: integer): integer;
begin
Result:= 0;
Lookup:= (Lookup and Neighborhood);
if Bool(Lookup and bit0) then Inc(Result); //%000000001
if Bool(Lookup and bit1) then Inc(Result); //%000000010
if Bool(Lookup and bit2) then Inc(Result); //%000000100
if Bool(Lookup and bit3) then Inc(Result); //%000001000
if Bool(Lookup and bit4) then Inc(Result); //%000010000
if Bool(Lookup and bit5) then Inc(Result); //%000100000
if Bool(Lookup and bit6) then Inc(Result); //%001000000
if Bool(Lookup and bit7) then Inc(Result); //%010000000
if Bool(Lookup and bit8) then Inc(Result); //%100000000
end;
const
JustFriendsLookup: array [0..19] of integer = ($140,$108,$101,$102,$104,$88,
$81,$82,$84,$a0,$41,$42,$44,
$60,$28,$21,$22,$0a,$0c,$05);
function JustFriendsAlive(Lookup, Neighborhood: integer): boolean;
var
i: integer;
begin
i:= Low(JustFriendsLookup);
Result:= Bool(Lookup and bit4);
while (i <= High(JustFriendsLookup)) and not Result do begin
Result:= (Lookup = JustFriendsLookup[i]);
Inc(i);
end; {else for i}
end;
function HexRule2mop(Lookup, Neighborhood: integer): char;
const
none = ' ';
m: array [0..5] of integer = ($21,$102,$a0,$108,$81,$0a);
o: array [0..5] of integer = ($03,$22,$120,$180,$88,$09);
//first 3 items in p are repeated.
p: array [0..5] of integer = ($101,$82,$28,$101,$82,$28);
var
i: integer;
begin
if Neighborhood <> nbHex then result:= none
else begin
Result:= none;
//leave out non-relevant bits for the hex rule.
lookup:= lookup and not bit4 and not bit2 and not bit6;
i:= low(m);
while (i <= high(m)) and (result = none) do begin
if lookup = m[i] then Result:= 'm'
else if Lookup = o[i] then Result:= 'o'
else if Lookup = p[i] then Result:= 'p';
inc(i);
end; {while}
end; {else}
end;
function MaxNeighborsInNeighborhood(N: integer): integer;
var
i: integer;
begin
Result:= 0;
for i:= 0 to 8 do begin
if Odd(N) then Inc(Result);
N:= N shr 1;
end; {for i}
end;
function StrToNeighborhood(S: string): integer;
var
i: integer;
begin
Result:= nbUnchanged; //special case: don't alter neighborhood;
i:= Pos(':',S);
if i <> 0 then begin
S:= copy(S,1,i-1);
if (S <> '') then case Upcase(S[1]) of
'M': result:= nbMoore;
'H': result:= nbHex;
'V': result:= nbVonNeuman;
'X': result:= nbX;
'0'..'7': try
Result:= OctToInt(S); //S must be in Oct notation
except Result:= nbMoore;
end; {try}
end; {if case}
end; {if}
end;
function NeighborhoodToStr(N: integer): string;
begin
case N of
nbMoore: Result:= 'M:';
nbX: Result:= 'X:';
nbVonNeuman: Result:= 'V:';
nbHex: Result:= 'H:';
nbJustFriends: Result:= 'J:';
else Result:= IntToOct(N,0)+':';
end; {case}
end;
function NeighborhoodToStrLong(N: integer): string;
begin
case N of
nbUnchanged: Result:= '';
nbMoore: Result:= 'M:Moore';
nbX: Result:= 'X:';
nbVonNeuman: Result:= 'V:Von Neuman';
nbHex: Result:= 'H:HexLife';
else Result:= IntToOct(N,0)+':';
end; {case}
end;
procedure PictureMenu(AMenu: TMenuItem; Refresh: boolean);
//var
//i: integer;
begin
//i:= 1;
end; (**)
(*
procedure PictureMenu(AMenu: TMenuItem; Refresh: boolean);
var
ABitmap: TBitmap;
BBitmap: TBitmap;
i,j: integer;
ID: integer;
OldTag: integer;
begin
if not Assigned(BitmapList) then BitmapList:= TList.Create;
if not Assigned(TagList) then TagList:= TList.Create;
ABitmap:= nil; //kill warnings.
BBitmap:= nil;
i:= 0;
while i < AMenu.Count do with AMenu.Items[i] do begin
ID:= Tag;
if (ID <= MaxZoom) then ID:= menuZoomCheck;
if (ID >= 1000) then try
BBitmap:= nil;
if not(Refresh) then begin
if FindResource(HInstance,PChar('#'+IntToStr(ID)),RT_BITMAP) <> 0 then begin
ABitmap:= TBitmap.Create;
ABitmap.LoadFromResourceID(HInstance,ID);
end {if}
else ABitmap:= nil;
if ID >= 2000 then begin
if FindResource(HInstance,PChar('#'+IntToStr(ID+1000)),RT_BITMAP) <> 0 then begin
BBitmap:= TBitmap.Create;
BBitmap.LoadFromResourceID(HInstance,ID+1000);
end; {if}
end; {if}
end {if}
else begin //Refresh
j:= 0;
while (j < TagList.Count) do begin
OldTag:= integer(TagList[j]);
if OldTag = ID then begin
ABitmap:= TBitmap(BitmapList[j]);
if ID >= 2000 then BBitmap:= TBitmap(BitmapList[j+1]);
j:= TagList.Count;
end; {if}
Inc(j);
end; { while }
end; {else}
if Assigned(ABitmap) then begin
if Visible then begin
if not(Assigned(BBitmap)) then BBitmap:= ABitmap;
if not(SetMenuItemBitmaps(AMenu.Handle,Command,mf_bycommand,
ABitmap.Handle,BBitmap.Handle)) then
raise Exception.Create('');
end; {if}
if not(Refresh) then begin
BitmapList.Add(ABitmap);
TagList.Add(pointer(ID));
if ID >= 2000 then begin
BitmapList.Add(BBitmap);
TagList.Add(pointer(ID+1000));
end; {if}
end; {if}
end; {if}
except begin
if BBitmap <> ABitmap then BBitmap.Free;
ABitmap.Free;
end;
end; {if try}
Inc(i);
end; {while}
end; (**)
initialization
IntelInstalled:= TickTime.IsIntelInstalled;
//IntelInstalled:= false;
if IntelInstalled then begin
TickTimer:= TTickTimer.Create;
TickTimer.StartNow;
end;
finalization
if IntelInstalled then TickTimer.Free;
end.