-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathliferules.cpp
executable file
·1062 lines (923 loc) · 29.1 KB
/
liferules.cpp
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
// This file is part of Golly.
// See docs/License.html for the copyright notice.
#include "liferules.h"
#include "util.h"
#include <cstdlib>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#if defined(WIN32) || defined(WIN64)
#define strncasecmp _strnicmp
#endif
liferules::liferules() {
int i ;
// base64 encoding characters
base64_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
// all valid rule letters
valid_rule_letters = "012345678ceaiknjqrytwz-" ;
// rule letters per neighbor count
rule_letters[0] = "ce" ;
rule_letters[1] = "ceaikn" ;
rule_letters[2] = "ceaiknjqry" ;
rule_letters[3] = "ceaiknjqrytwz" ;
// isotropic neighborhoods per neighbor count
static int entry0[2] = { 1, 2 } ;
static int entry1[6] = { 5, 10, 3, 40, 33, 68 } ;
static int entry2[10] = { 69, 42, 11, 7, 98, 13, 14, 70, 41, 97 } ;
static int entry3[13] = { 325, 170, 15, 45, 99, 71, 106, 102, 43, 101, 105, 78, 108 } ;
rule_neighborhoods[0] = entry0 ;
rule_neighborhoods[1] = entry1 ;
rule_neighborhoods[2] = entry2 ;
rule_neighborhoods[3] = entry3 ;
// bit offset for suvival part of rule
survival_offset = 9 ;
// bit in letter bit mask indicating negative
negative_bit = 13 ;
// maximum number of letters per neighbor count
max_letters[0] = 0 ;
max_letters[1] = (int) strlen(rule_letters[0]) ;
max_letters[2] = (int) strlen(rule_letters[1]) ;
max_letters[3] = (int) strlen(rule_letters[2]) ;
max_letters[4] = (int) strlen(rule_letters[3]) ;
max_letters[5] = max_letters[3] ;
max_letters[6] = max_letters[2] ;
max_letters[7] = max_letters[1] ;
max_letters[8] = max_letters[0] ;
for (i = 0 ; i < survival_offset ; i++) {
max_letters[i + survival_offset] = max_letters[i] ;
}
// canonical letter order per neighbor count
static int order0[1] = { 0 } ;
static int order1[2] = { 0, 1 } ;
static int order2[6] = { 2, 0, 1, 3, 4, 5 } ;
static int order3[10] = { 2, 0, 1, 3, 6, 4, 5, 7, 8, 9 } ;
static int order4[13] = { 2, 0, 1, 3, 6, 4, 5, 7, 8, 10, 11, 9, 12 } ;
order_letters[0] = order0 ;
order_letters[1] = order1 ;
order_letters[2] = order2 ;
order_letters[3] = order3 ;
order_letters[4] = order4 ;
order_letters[5] = order3 ;
order_letters[6] = order2 ;
order_letters[7] = order1 ;
order_letters[8] = order0 ;
for (i = 0 ; i < survival_offset ; i++) {
order_letters[i + survival_offset] = order_letters[i] ;
}
// initialize
initRule() ;
}
liferules::~liferules() {
}
// returns a count of the number of bits set in given int
static int bitcount(int v) {
int r = 0 ;
while (v) {
r++ ;
v &= v - 1 ;
}
return r ;
}
// initialize
void liferules::initRule() {
// default to Moore neighbourhood totalistic rule
neighbormask = MOORE ;
neighbors = 8 ;
wolfram = -1 ;
totalistic = true ;
using_map = false ;
// one bit for each neighbor count
// s = survival, b = birth
// bit: 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
// meaning: s8 s7 s6 s5 s4 s3 s2 s1 s0 b8 b7 b6 b5 b4 b3 b2 b1 b0
rulebits = 0 ;
// one bit for each letter per neighbor count
// N = negative bit
// bit: 13 12 11 10 9 8 7 6 5 4 3 2 1 0
// meaning: N z w t y r q j n k i a e c
memset(letter_bits, 0, sizeof(letter_bits)) ;
// two 4x4 rule maps (second used for B0-not-Smax rule emulation)
memset(rule0, 0, sizeof(rule0)) ;
memset(rule1, 0, sizeof(rule1)) ;
// 3x3 rule map
memset(rule3x3, 0, sizeof(rule3x3)) ;
// canonical rule string
memset(canonrule, 0, sizeof(canonrule)) ;
}
// set 3x3 grid based on totalistic value
void liferules::setTotalistic(int value, bool survival) {
int mask = 0 ;
int nbrs = 0 ;
int nhood = 0 ;
int i = 0 ;
int j = 0 ;
int offset = 0 ;
// check if this value has already been processed
if (survival) {
offset = survival_offset ;
}
if ((rulebits & (1 << (value + offset))) == 0) {
// update the rulebits
rulebits |= 1 << (value + offset) ;
// update the mask if survival
if (survival) {
mask = 0x10 ;
}
// fill the array based on totalistic value
for (i = 0 ; i < ALL3X3 ; i += 32) {
for (j = 0 ; j < 16 ; j++) {
nbrs = 0 ;
nhood = (i+j) & neighbormask ;
while (nhood > 0) {
nbrs += (nhood & 1) ;
nhood >>= 1 ;
}
if (value == nbrs) {
rule3x3[i+j+mask] = 1 ;
}
}
}
}
}
// flip bits
int liferules::flipBits(int x) {
return ((x & 0x07) << 6) | ((x & 0x1c0) >> 6) | (x & 0x38) ;
}
// rotate 90
int liferules::rotateBits90Clockwise(int x) {
return ((x & 0x4) << 6) | ((x & 0x20) << 2) | ((x & 0x100) >> 2)
| ((x & 0x2) << 4) | (x & 0x10) | ((x & 0x80) >> 4)
| ((x & 0x1) << 2) | ((x & 0x8) >> 2) | ((x & 0x40) >> 6) ;
}
// set symmetrical neighborhood into 3x3 map
void liferules::setSymmetrical512(int x, int b) {
int y = x ;
int i = 0 ;
// process each of the 4 rotations
for (i = 0 ; i < 4 ; i++) {
rule3x3[y] = (char) b ;
y = rotateBits90Clockwise(y) ;
}
// flip
y = flipBits(y) ;
// process each of the 4 rotations
for (i = 0 ; i < 4 ; i++) {
rule3x3[y] = (char) b ;
y = rotateBits90Clockwise(y) ;
}
}
// set symmetrical neighborhood
void liferules::setSymmetrical(int value, bool survival, int lindex, int normal) {
int xorbit = 0 ;
int nindex = value - 1 ;
int x = 0 ;
int offset = 0 ;
// check for homogeneous bits
if (value == 0 || value == 8) {
setTotalistic(value, survival) ;
}
else {
// update the rulebits
if (survival) {
offset = survival_offset ;
}
rulebits |= 1 << (value + offset) ;
// reflect the index if in second half
if (nindex > 3) {
nindex = 6 - nindex ;
xorbit = 0x1ef ;
}
// update the letterbits
letter_bits[value + offset] |= 1 << lindex ;
if (!normal) {
// set the negative bit
letter_bits[value + offset] |= 1 << negative_bit ;
}
// lookup the neighborhood
x = rule_neighborhoods[nindex][lindex] ^ xorbit ;
if (survival) {
x |= 0x10 ;
}
setSymmetrical512(x, normal) ;
}
}
// set totalistic birth or survival rule from a string
void liferules::setTotalisticRuleFromString(const char *rule, bool survival) {
char current ;
// process each character in the rule string
while ( *rule ) {
current = *rule ;
rule++ ;
// convert the digit to an integer
current -= '0' ;
// set totalistic
setTotalistic(current, survival) ;
}
}
// set rule from birth or survival string
void liferules::setRuleFromString(const char *rule, bool survival) {
// current and next character
char current ;
char next ;
// whether character normal or inverted
int normal = 1 ;
// letter index
char *letterindex = 0 ;
int lindex = 0 ;
int nindex = 0 ;
// process each character
while ( *rule ) {
current = *rule ;
rule++ ;
// find the index in the valid character list
letterindex = strchr((char*) valid_rule_letters, current) ;
lindex = letterindex ? int(letterindex - valid_rule_letters) : -1 ;
// check if it is a digit
if (lindex >= 0 && lindex <= 8) {
// determine what follows the digit
next = *rule ;
nindex = -1 ;
if (next) {
letterindex = strchr((char*) rule_letters[3], next) ;
if (letterindex) {
nindex = int(letterindex - rule_letters[3]) ;
}
}
// is the next character a digit or minus?
if (nindex == -1) {
setTotalistic(lindex, survival) ;
}
// check for inversion
normal = 1 ;
if (next == '-') {
rule++ ;
next = *rule ;
// invert following character meanings
normal = 0 ;
}
// process non-totalistic characters
if (next) {
letterindex = strchr((char*) rule_letters[3], next) ;
nindex = -1 ;
if (letterindex) {
nindex = int(letterindex - rule_letters[3]) ;
}
while (nindex >= 0) {
// set symmetrical
setSymmetrical(lindex, survival, nindex, normal) ;
// get next character
rule++ ;
next = *rule ;
nindex = -1 ;
if (next) {
letterindex = strchr((char*) rule_letters[3], next) ;
if (letterindex) {
nindex = int(letterindex - rule_letters[3]) ;
}
}
}
}
}
}
}
// create the rule map from Wolfram number
void liferules::createWolframMap() {
int i = 0 ;
// clear the rule array
memset(rule3x3, 0, ALL3X3) ;
// set in the 3x3 map
for (i = 0 ; i < ALL3X3 ; i++) {
if ((wolfram & (1 << (i & 7))) || (i & 16)) {
rule3x3[i] = 1 ;
}
}
}
// create the rule map from the base64 encoded map
void liferules::createRuleMapFromMAP(const char *base64) {
// set the number of characters to read
int power2 = 1 << (neighbors + 1) ;
int fullchars = power2 / 6 ;
int remainbits = power2 % 6 ;
// create an array to read the MAP bits
char bits[ALL3X3] ;
// decode the base64 string
int i = 0 ;
char c = 0 ;
int j = 0 ;
const char *index = 0 ;
for (i = 0 ; i < fullchars ; i++) {
// convert character to base64 index
index = strchr(base64_characters, *base64) ;
base64++ ;
c = index ? (char)(index - base64_characters) : 0 ;
// decode the character
bits[j] = c >> 5 ;
j++ ;
bits[j] = (c >> 4) & 1 ;
j++ ;
bits[j] = (c >> 3) & 1 ;
j++ ;
bits[j] = (c >> 2) & 1 ;
j++ ;
bits[j] = (c >> 1) & 1 ;
j++ ;
bits[j] = c & 1 ;
j++ ;
}
// decode remaining bits from final character
if (remainbits > 0) {
index = strchr(base64_characters, *base64) ;
c = index ? (char)(index - base64_characters) : 0 ;
int b = 5 ;
while (remainbits > 0) {
bits[j] = (c >> b) & 1 ;
b-- ;
j++ ;
remainbits-- ;
}
}
// copy into rule array using the neighborhood mask
int k, m ;
for (i = 0 ; i < ALL3X3 ; i++) {
k = 0 ;
m = neighbors ;
for (j = 8 ; j >= 0 ; j--) {
if (neighbormask & (1 << j)) {
if (i & (1 << j)) {
k |= (1 << m) ;
}
m-- ;
}
}
rule3x3[i] = bits[k] ;
}
}
// create the rule map from birth and survival strings
void liferules::createRuleMap(const char *birth, const char *survival) {
// clear the rule array
memset(rule3x3, 0, ALL3X3) ;
// check for totalistic rule
if (totalistic) {
// set the totalistic birth rule
setTotalisticRuleFromString(birth, false) ;
// set the totalistic surivival rule
setTotalisticRuleFromString(survival, true) ;
}
else {
// set the non-totalistic birth rule
setRuleFromString(birth, false) ;
// set the non-totalistic survival rule
setRuleFromString(survival, true) ;
}
}
// add canonical letter representation
int liferules::addLetters(int count, int p) {
int bits ; // bitmask of letters defined at this count
int negative = 0 ; // whether negative
int setbits ; // how many bits are defined
int maxbits ; // maximum number of letters at this count
int letter = 0 ;
int j ;
// check if letters are defined for this neighbor count
if (letter_bits[count]) {
// get the bit mask
bits = letter_bits[count] ;
// check for negative
if (bits & (1 << negative_bit)) {
// letters are negative
negative = 1 ;
bits &= ~(1 << negative_bit) ;
}
// compute the number of bits set
setbits = bitcount(bits) ;
// get the maximum number of allowed letters at this neighbor count
maxbits = max_letters[count] ;
// do not invert if not negative and seven letters
if (!(!negative && setbits == 7 && maxbits == 13)) {
// if maximum letters minus number used is greater than number used then invert
if (setbits + negative > (maxbits >> 1)) {
// invert maximum letters for this count
bits = ~bits & ((1 << maxbits) - 1) ;
if (bits) {
negative = !negative ;
}
}
}
// if negative and no letters then remove neighborhood count
if (negative && !bits) {
canonrule[p] = 0 ;
p-- ;
}
else {
// check whether to output minus
if (negative) {
canonrule[p++] = '-' ;
}
// add defined letters
for (j = 0 ; j < maxbits ; j++) {
// lookup the letter in order
letter = order_letters[count][j] ;
if (bits & (1 << letter)) {
canonrule[p++] = rule_letters[3][letter] ;
}
}
}
}
return p ;
}
// convert the 3x3 map to the 4x4 map
void liferules::convertTo4x4Map(char *which) {
int i = 0 ;
int v = 0 ;
// create every possible cell combination for 4x4
for (i = 0 ; i < ALL4X4 ; i ++) {
// perform 4 lookups in the 3x3 map to create the 4x4 entry
// 15 14 13 x 7 6 5
// 11 10 9 x -> 11 10 9 -> 10' x 0 0 x x
// 7 6 5 x 15 14 13
// x x x x
v = rule3x3[((i & 57344) >> 13) | ((i & 3584) >> 6) | ((i & 224) << 1)] << 5 ;
// x 14 13 12 6 5 4
// x 10 9 8 -> 10 9 8 -> x 9' 0 0 x x
// x 6 5 4 14 13 12
// x x x x
v |= rule3x3[((i & 28672) >> 12) | ((i & 1792) >> 5) | ((i & 112) << 2)] << 4 ;
// x x x x
// 11 10 9 x -> 3 2 1 -> x x 0 0 6' x
// 7 6 5 x 7 6 5
// 3 2 1 x 11 10 9
v |= rule3x3[((i & 3584) >> 9) | ((i & 224) >> 2) | ((i & 14) << 5)] << 1 ;
// x x x x
// x 10 9 8 -> 2 1 0 -> x x 0 0 x 5'
// x 6 5 4 6 5 4
// x 2 1 0 10 9 8
v |= rule3x3[((i & 1792) >> 8) | ((i & 112) >> 1) | ((i & 7) << 6)] ;
// save the entry
which[i] = (char) v ;
}
}
// save the rule (and handle B0)
void liferules::saveRule() {
int i = 0 ;
char tmp ;
if (wolfram == -1) {
// check for B0
if (rule3x3[0]) {
// check for Smax
if (rule3x3[ALL3X3 - 1]) {
// B0 with Smax: rule -> NOT(reverse(bits))
for (i = 0 ; i < ALL3X3 / 2 ; i++) {
tmp = rule3x3[i] ;
rule3x3[i] = 1 - rule3x3[ALL3X3 - i - 1] ;
rule3x3[ALL3X3 - i - 1] = 1 - tmp ;
}
}
else {
// B0 without Smax needs two rules: one for odd and one for even generations
alternate_rules = true ;
// odd rule -> reverse(bits)
for (i = 0 ; i < ALL3X3 / 2 ; i++) {
tmp = rule3x3[i] ;
rule3x3[i] = rule3x3[ALL3X3 - i - 1] ;
rule3x3[ALL3X3 - i - 1] = tmp ;
}
convertTo4x4Map(rule1) ;
// even rule -> NOT(bits)
for (i = 0 ; i < ALL3X3 / 2 ; i++) {
tmp = rule3x3[i] ;
// need to reverse then invert due to even rule above
rule3x3[i] = 1 - rule3x3[ALL3X3 - i - 1] ;
rule3x3[ALL3X3 - i - 1] = 1 - tmp ;
}
}
}
}
// convert to 4x4 map
convertTo4x4Map(rule0) ;
}
// remove character from a string in place
void liferules::removeChar(char *string, char skip) {
int src = 0 ;
int dst = 0 ;
char c = string[src++] ;
// copy characters other than skip
while ( c ) {
if (c != skip) {
string[dst++] = c ;
}
c = string[src++] ;
}
// ensure null terminated
string[dst] = 0 ;
}
// check whether non-totalistic letters are valid for defined neighbor counts
bool liferules::lettersValid(const char *part) {
char c ;
int nindex = 0 ;
int currentCount = -1 ;
// get next character
while ( *part ) {
c = *part ;
if (c >= '0' && c <= '8') {
currentCount = c - '0' ;
nindex = currentCount - 1 ;
if (nindex > 3) {
nindex = 6 - nindex ;
}
}
else {
// ignore minus
if (c != '-') {
// not valid if 0 or 8
if (currentCount == 0 || currentCount == 8) {
return false ;
}
// check against valid rule letters for this neighbor count
if (strchr((char*) rule_letters[nindex], c) == 0) {
return false ;
}
}
}
part++ ;
}
return true ;
}
// set rule
const char *liferules::setrule(const char *rulestring, lifealgo *algo) {
char *r = (char *)rulestring ;
char tidystring[MAXRULESIZE] ; // tidy version of rule string
char *t = (char *)tidystring ;
char *end = r + strlen(r) ; // end of rule string
char c ;
char *charpos = 0 ;
int digit ;
int maxdigit = 0 ; // maximum digit value found
char *colonpos = 0 ; // position of colon
char *slashpos = 0 ; // position of slash
char *underscorepos = 0 ; // position of underscore
char *bpos = 0 ; // position of b
char *spos = 0 ; // position of s
// initialize rule type
initRule() ;
// we might need to emulate B0 rule by using two different rules for odd/even gens
alternate_rules = false ;
// check if rule is too long
if (strlen(rulestring) > (size_t) MAXRULESIZE) {
return "Rule name is too long." ;
}
// check for colon
colonpos = strchr(r, ':') ;
if (colonpos) {
// only process up to the colon
end = colonpos ;
}
// skip any whitespace
while (*r == ' ') {
r++ ;
}
// check for map
if (strncasecmp(r, "map", 3) == 0) {
// attempt to decode map
r += 3 ;
bpos = r ;
// terminate at the colon if one is present
if (colonpos) *colonpos = 0 ;
// check the length of the map
int maplen = (int) strlen(r) ;
// replace the colon if one was present
if (colonpos) *colonpos = ':' ;
// check if there is base64 padding
if (maplen > 2 && !strncmp(r + maplen - 2, "==", 2)) {
// remove padding
maplen -= 2 ;
}
// check if the map length is valid for Moore, Hexagonal or von Neumann neighborhoods
if (!(maplen == MAP512LENGTH || maplen == MAP128LENGTH || maplen == MAP32LENGTH)) {
return "MAP rule needs 6, 22 or 86 base64 characters." ;
}
// validate the characters
spos = r + maplen ;
while (r < spos) {
if (!strchr(base64_characters, *r)) {
return "MAP contains illegal base64 character." ;
}
r++ ;
}
// set the neighborhood based on the map length
if (maplen == MAP128LENGTH) {
neighbormask = HEXAGONAL ;
neighbors = 6 ;
} else {
if (maplen == MAP32LENGTH) {
neighbormask = VON_NEUMANN ;
neighbors = 4 ;
}
}
// map looks valid
using_map = true ;
}
else {
// create lower case version of rule name without spaces
while (r < end) {
// get the next character and convert to lowercase
c = (char) tolower(*r) ;
// process the character
switch (c) {
// birth
case 'b':
if (bpos) {
// multiple b found
return "Only one B allowed." ;
}
bpos = t ;
*t = c ;
t++ ;
break ;
// survival
case 's':
if (spos) {
// multiple s found
return "Only one S allowed." ;
}
spos = t ;
*t = c ;
t++ ;
break ;
// slash
case '/':
if (slashpos) {
// multiple slashes found
return "Only one slash allowed." ;
}
slashpos = t ;
*t = c ;
t++ ;
break ;
// underscore
case '_':
if (underscorepos) {
// multiple underscores found
return "Only one underscore allowed." ;
}
underscorepos = t ;
*t = c ;
t++ ;
break ;
// hex
case 'h':
if (neighbormask != MOORE || wolfram != -1) {
// multiple neighborhoods specified
return "Only one neighborhood allowed." ;
}
neighbormask = HEXAGONAL ;
neighbors = 6 ;
*t = c ;
t++ ;
break ;
// von neumann
case 'v':
if (neighbormask != MOORE || wolfram != -1) {
// multiple neighborhoods specified
return "Only one neighborhood allowed." ;
}
neighbormask = VON_NEUMANN ;
neighbors = 4 ;
*t = c ;
t++ ;
break ;
// wolfram
case 'w':
// check if at beginning of string
if (t == tidystring) {
if (neighbormask != MOORE || wolfram != -1) {
// multiple neighborhoods specified
return "Only one neighborhood allowed." ;
}
wolfram = 0 ;
}
else {
// copy character
*t = c ;
t++ ;
totalistic = false ;
}
break ;
// minus
case '-':
// check if previous character is a digit
if (t == tidystring || *(t-1) < '0' || *(t-1) > '8') {
// minus can only follow a digit
return "Minus can only follow a digit." ;
}
*t = c ;
t++ ;
totalistic = false ;
break ;
// other characters
default:
// ignore space
if (c != ' ') {
// check character is valid
charpos = strchr((char*) valid_rule_letters, c) ;
if (charpos) {
// copy character
*t = c ;
t++ ;
// check if totalistic (i.e. found a valid non-digit)
digit = int(charpos - valid_rule_letters) ;
if (digit > 8) {
totalistic = false ;
}
else {
// update maximum digit found
if (digit > maxdigit) {
maxdigit = digit ;
}
}
}
else if (wolfram == 0 && c == '9') {
*t = c ;
t++ ;
}
else {
return "Bad character found.";
}
}
break ;
}
// next character
r++ ;
}
// ensure null terminated
*t = 0 ;
// don't allow empty rule string
t = tidystring ;
if (*t == 0) {
return "Rule cannot be empty string." ;
}
// can't have slash and underscore
if (underscorepos && slashpos) {
return "Can't have slash and underscore." ;
}
// underscore only valid for non-totalistic rules
if (underscorepos && totalistic) {
return "Underscore not valid for totalistic rules, use slash." ;
}
// if underscore defined then set the slash position
if (underscorepos) {
slashpos = underscorepos ;
}
// check for Wolfram
if (wolfram == 0) {
// parse Wolfram 1D rule
while (*t >= '0' && *t <= '9') {
wolfram = 10 * wolfram + *t - '0' ;
t++ ;
}
if (wolfram < 0 || wolfram > 254 || wolfram & 1) {
return "Wolfram rule must be an even number from 0 to 254." ;
}
if (*t) {
return "Bad character in Wolfram rule." ;
}
}
else {
// if neighborhood specified then must be last character
if (neighbormask != MOORE) {
size_t len = strlen(t) ;
if (len) {
c = t[len - 1] ;
if (!((c == 'h') || (c == 'v'))) {
return "Neighborhood must be at end of rule." ;
}
// remove character
t[len - 1] = 0 ;
}
}
// at least one of slash, b or s must be present
if (!(slashpos || bpos || spos)) {
return "Rule must contain a slash or B or S." ;
}
// digits can not be greater than the number of neighbors for the defined neighborhood
if (maxdigit > neighbors) {
return "Digit greater than neighborhood allows." ;
}
// if slash present and both b and s then one must be each side of the slash
if (slashpos && bpos && spos) {
if ((bpos < slashpos && spos < slashpos) || (bpos > slashpos && spos > slashpos)) {
return "B and S must be either side of slash." ;
}
}
// check if there was a slash to divide birth from survival
if (!slashpos) {
// check if both b and s exist
if (bpos && spos) {
// determine whether b or s is first
if (bpos < spos) {
// skip b and cut the string using s
bpos++ ;
*spos = 0 ;
spos++ ;
}
else {
// skip s and cut the string using b
spos++ ;
*bpos = 0 ;
bpos++ ;
}
}
else {
// just bpos
if (bpos) {
bpos = t ;
removeChar(bpos, 'b') ;
spos = bpos + strlen(bpos) ;
}
else {
// just spos
spos = t ;
removeChar(spos, 's') ;
bpos = spos + strlen(spos) ;
}
}
}
else {
// slash exists so set determine which part is b and which is s
*slashpos = 0 ;
// check if b or s are defined
if (bpos || spos) {
// check for birth first
if ((bpos && bpos < slashpos) || (spos && spos > slashpos)) {
// birth then survival
bpos = t ;
spos = slashpos + 1 ;
}
else {
// survival then birth
bpos = slashpos + 1 ;
spos = t ;
}
// remove b or s from rule parts
removeChar(bpos, 'b') ;
removeChar(spos, 's') ;
}
else {
// no b or s so survival first
spos = t ;
bpos = slashpos + 1 ;
}
}