File: make-Codd-constructor.py

package info (click to toggle)
golly 2.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 10,080 kB
  • sloc: cpp: 41,951; python: 6,339; sh: 3,912; perl: 1,172; java: 49; makefile: 47
file content (72 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download
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
# Make a pattern from states 1 and 0, select it and then
# run this script to make a Codd CA pattern that will then
# construct the pattern. The constructor will then attempt
# to inject sheathing and triggering signals to a point one
# up from the pattern's bottom-left corner.
#
# See example: Patterns/Codd/golly-constructor.rle
#
# Tim Hutton <tim.hutton@gmail.com>

from glife import rect
from time import time
import golly as g

r = rect( g.getselrect() )
if r.empty: g.exit("There is no selection.")

oldsecs = time()
maxstate = g.numstates() - 1

# these are the commands:
extend = '70116011'
extend_left = '4011401150116011'
extend_right = '5011501140116011'
retract = '4011501160116011'
retract_left = '5011601160116011'
retract_right = '4011601160116011'
mark = '701160114011501170116011'
erase = '601170114011501160116011'
sense = '70117011'
cap = '40116011'
inject_sheath = '701150116011'
inject_trigger = '60117011701160116011'
# (sometimes you don't need two blanks after each command but I haven't analysed this fully)

# we first write the commands to a string, and then to the grid
# (we start off facing right, at the bottom-left of the construction area)

# write the cells that are in state 1 (can ignore the zeros)
# (still plenty of room for optimisation here)
tape = '11'
for row in xrange(r.top, r.top + r.height):
   # if large selection then give some indication of progress
   newsecs = time()
   if newsecs - oldsecs >= 1.0:
      oldsecs = newsecs
      g.update()
   for col in xrange(r.left, r.left + r.width):
      if g.getcell(col, row)==1:
         tape += extend*(4+col-r.left) + extend_left + extend*(r.top+r.height-row)
         tape += mark
         tape += retract*(r.top+r.height-row) + retract_left + retract*(4+col-r.left)
      elif g.getcell(col,row)!=0:
	     g.exit('Cells in the selected area must be in states 0 or 1 only.')
	  
# finally we sheath and trigger and retract
tape += extend_left + extend*4 + extend_right + extend
tape += inject_sheath + '1'*50 + inject_trigger
tape += retract*2 + retract_right + retract*4 + retract_left
	
# now write the tape out
x = r.left+r.width+10
y = r.top+r.height+10
g.setcell(x+1,y,2)
for i in tape:
   g.setcell(x,y-1,2)
   g.setcell(x,y,int(i))
   g.setcell(x,y+1,2)
   x-=1