def randfill_mash(rectcoords, amt): newstate = g.getoption("drawingstate") for i in range(rectcoords[0], rectcoords[0] + rectcoords[2]): for j in range(rectcoords[1], rectcoords[1] + rectcoords[3]): if (100 * random.random() < amt): g.setcell(i, j, dict_fill[rule][g.getcell(i, j)]) else: # g.setcell(i, j, 0) continue
def randfill(rectcoords, amt, amt2=100, secondary_state=1): statelist = [g.getoption("drawingstate"), secondary_state] for i in range(rectcoords[0], rectcoords[0] + rectcoords[2]): for j in range(rectcoords[1], rectcoords[1] + rectcoords[3]): if (100 * random.random() < amt): newstate = (100 * random.random() > amt2) g.setcell(i, j, statelist[newstate]) else: # g.setcell(i, j, 0) continue
def randfill_mash(rectcoords, amt, statemap): newstate = g.getoption("drawingstate") # g.note('%s'%statemap) for i in range(rectcoords[0], rectcoords[0] + rectcoords[2]): for j in range(rectcoords[1], rectcoords[1] + rectcoords[3]): if (100 * random.random() < amt): try: g.show('processing %i,%i' % (i, j)) g.setcell(i, j, statemap[g.getcell(i, j)]) except: dict_lc = [1] * g.numstates() dict_lc[0] = 0 dict_lc[1:3] = [2, 2] g.setcell(i, j, dict_lc[g.getcell(i, j)]) else: # g.setcell(i, j, 0) continue
def adj_hash(pbox): def parse_list(clist): newlist=[] for i in range(len(clist[0::3])): if 3*i+3 <= len(clist): temp=clist[3*i:3*i+3] newlist.append(temp) else: break return(newlist) def adjacencymatrix(parsed_clist,pbox=pbox,torus=0): adjacencymatrix=[] if torus: adjacency_x=[0,1,pbox[2]-1] adjacency_y=[0,1,pbox[3]-1] else: adjacency_x=[0,1] adjacency_y=[0,1] # fclist=full_clist(parsed_clist,pbox) for i in parsed_clist: row=[] for j in parsed_clist: if i!=j and \ (abs(i[0]-j[0]) in adjacency_x) and \ (abs(i[1]-j[1]) in adjacency_y) and \ [i[2],j[2]]==[state,state]: row.append(1) else: row.append(0) adjacencymatrix.append(row) return adjacencymatrix clist=getquad(pbox) pbox[2]=2*pbox[2] pbox[3]=2*pbox[3] parsed_clist=parse_list(clist) state = g.getoption("drawingstate") adjmat=adjacencymatrix(parsed_clist,pbox,torus=1) eigval=np.linalg.eigvalsh(adjmat) eigval =np.round( eigval,decimals=5) h=hash(tuple(eigval)) return h
if started and len(mousepos) == 0: # erase old line if mouse is not over grid if len(oldline) > 0: eraseline(oldline) oldline = [] g.update() elif started and len(mousepos) > 0 and mousepos != oldmouse: # mouse has moved, so erase old line (if any) and draw new line if len(oldline) > 0: eraseline(oldline) x, y = mousepos.split() oldline = drawline(startx, starty, int(x), int(y)) oldmouse = mousepos # ------------------------------------------------------------------------------ g.show("Click where to start line...") oldcursor = g.getcursor() g.setcursor("Draw") drawstate = g.getoption("drawingstate") oldline = [] firstcell = [] # pos and state of the 1st cell clicked try: drawlines() finally: g.setcursor(oldcursor) if len(oldline) > 0: eraseline(oldline) if len(firstcell) > 0: x, y, s = firstcell g.setcell(x, y, s)
def fill(rectcoords): newstate = g.getoption("drawingstate") for i in range(rectcoords[0],rectcoords[0]+rectcoords[2]): for j in range(rectcoords[1],rectcoords[1]+rectcoords[3]): g.setcell(i, j, newstate)
temp = clist[3 * i:3 * i + 3] for x in [pbox[2] * i for i in range(n)]: for y in [pbox[3] * i for i in range(n)]: newlist.append(temp[0] + x) newlist.append(temp[1] + y) newlist.append(temp[2]) newlist.append(0) return newlist input = g.getstring( 'what cell state to screen for/ \n \ treat selection as torus?/ \n \ how many repeating units?', '%s/%s/%s' % (g.getoption("drawingstate"), '1', '2')) state = int(input.split('/')[0]) torus = int(input.split('/')[1]) n = int(input.split('/')[2]) pbox = g.getselrect() clist = getquad(pbox, n=n) g.setclipstr(str(clist)) parsed_clist = parse_list(clist) g.show('clist %i %s, parsed_clist %i %s' % (len(clist), str(clist), len(parsed_clist), str(parsed_clist))) fclist = full_clist(parsed_clist, pbox) adjmat = adjacencymatrix(parsed_clist, pbox, torus, n=n)
clist[3*i] = clist[3*i]-sel[0] clist[3*i+1] = clist[3*i+1]-sel[1] clist.insert(0,sel[2]) clist.insert(1,sel[3]) tile.append(clist) pboxlist.append(pbox) return tile input=g.getstring('maxnum/step/boxwidth','100/1/40') maxnum=int(input.split('/')[0]) step=int(input.split('/')[1]) boxwidth=int(input.split('/')[2]) input=g.getstring('what cell state to screen for/ \n \ treat selection as torus?/ \n \ how many repeating units?','%s/%s/%s'%(g.getoption("drawingstate"),'1','2')) state=int(input.split('/')[0]) torus=int(input.split('/')[1]) n=int(input.split('/')[2]) # box=makebox(boxwidth) box=makebox(boxwidth) pboxlist=[] tile=loadtopo(maxnum,step,boxwidth) adjmatlist=[] for i in range(len(tile)): pbox=pboxlist[i] clist=getquad(pbox=pbox,n=n)
# Run the current pattern for a given number of steps (using current # step size) and create a plot of population vs time in separate layer. # Author: Andrew Trevorrow ([email protected]), May 2007. import golly as g from glife import * from glife.text import make_text from time import time from random import random import math clist = [] fill = g.getoption("drawingstate") def rp_plot(input, fill=3): m = len(input) for i in range(m): for j in range(m): d = norm(input[i], input[j]) if d <= dmax: g.setcell(i, -j, fill) # clist.append(i) # clist.append(j) # clist.append(input[i]==input[j]) #g.putcells(clist,0,0) def norm(a, b): return abs(a - b)
def burp(): test_signal=pattern(""" 40bo$41bo$39b3o17$40bo4bo4bo4bo4bo$41bo4bo4bo4bo4bo$39b3o2b3o2b3o2b3o 2b3o3$40bo4bo4bo4bo4bo$41bo4bo4bo4bo4bo$39b3o2b3o2b3o2b3o2b3o3$40bo4bo 4bo4bo4bo$41bo4bo4bo4bo4bo$39b3o2b3o2b3o2b3o2b3o3$40bo4bo4bo4bo4bo$41b o4bo4bo4bo4bo$39b3o2b3o2b3o2b3o2b3o3$bo38bo4bo4bo4bo4bo18bo$2bo38bo4bo 4bo4bo4bo18bo$3o36b3o2b3o2b3o2b3o2b3o16b3o37$40bo$41bo$39b3o!""") prepare_burp() ticks=0 tickstep=5 last_signal=-99999 viewport_speed=16 sel_speed=0.0 delta_sel=0.0 delay=-1.0 run_flag=False ch="" selx=600.0 sely=365.0 place_signal=3 helpstring="""Use ENTER and SPACE to run or halt the pattern; use + and - to change the step size or delay value; use arrow keys and mouse tools to pan and zoom in any pane; T toggles between a tiled view and a single-pane view; S creates another signal fleet near the detection mechanism; R resets the Heisenburp device to its initial state; Q quits out of the script and restores original settings.""" instr="Press H for help. " g.show(instr + str(tickstep)) g.select([selx,sely,50,50]) # keyboard handling while ch<>"q": if place_signal>0: if ticks-last_signal>1846: test_signal.put(-150,60) last_signal=ticks place_signal-=1 if place_signal>0 and run_flag==True: show_status_text("Next signal placement in " \ + str(1847 - ticks + last_signal) + " ticks. " + instr, delay, tickstep) else: show_status_text(instr,delay,tickstep) event = g.getevent() if event.startswith("key"): evt, ch, mods = event.split() else: ch = "" if ch=="r": prepare_burp() ticks=0 last_signal=-99999 viewport_speed=16 sel_speed=0 run_flag=False selx=600.0 sely=365.0 place_signal=3 g.select([selx,sely,50,50]) elif ch=="h": g.note(helpstring) elif ch=="t": g.setoption("tilelayers",1-g.getoption("tilelayers")) elif ch=="=" or ch=="+": if delay>.01: delay/=2 elif delay==.01: delay=-1 else: if tickstep==1: tickstep=3 elif tickstep<250: tickstep=(tickstep-1)*2+1 elif ch=="-" or ch=="_": if delay==-1: if tickstep==1: delay=.01 else: if tickstep==3: tickstep=1 else: tickstep=(tickstep-1)/2+1 else: if delay<1: delay*=2 elif ch=="space": run_flag=False elif ch=="return": run_flag=not run_flag elif ch=="s": place_signal+=1 else: # just pass any other keyboard/mouse event through to Golly g.doevent(event) # generation and selection speed handling if ch=="space" or run_flag==True: g.run(tickstep) currlayer = g.getlayer() if currlayer != 4: # user has switched layer so temporarily change it back # so we only change location of bottom right layer g.check(False) g.setlayer(4) x, y = getposint() oldticks=ticks ticks+=tickstep delta_view=int(ticks/viewport_speed) - int(oldticks/viewport_speed) if delta_view <> 0: # assumes diagonal motion for now setposint(x + delta_view, y + delta_view) sel = g.getselrect() if len(sel)<>0: x, y, w, h = sel if int(selx)<>x: # user changed selection # prepare to move new selection instead (!?!) # -- use floating-point selx, sely to account for fractional speeds selx=x sely=y else: selx+=sel_speed*tickstep sely+=sel_speed*tickstep g.select([selx, sely, w, h]) else: g.select([selx, sely, 50, 50]) if currlayer != 4: g.setlayer(currlayer) g.check(True) # change viewport speed at appropriate times to follow the action if oldticks<4570 and ticks>=4570: sel_speed=.666667 # one-time correction to catch up with the signal at high sim speeds g.select([600+(ticks-4570)*1.5, 365+(ticks-4570)*1.5, w, h]) if selx>2125: sel_speed=0 g.select([2125, sely+2125-selx, w, h]) if oldticks<4750 and ticks>=4750: viewport_speed=1.5 if oldticks<6125 and ticks>=6125: viewport_speed=16 if oldticks>=11995: viewport_speed=99999.9 # stop automatically after the last signal passes through the device if oldticks - last_signal<8705 and ticks - last_signal>=8705: run_flag=False if run_flag==True and delay>0: sleep(delay) g.update()
def floodfill(): newstate = g.getoption("drawingstate") oldstate = newstate # wait for user to click a cell g.show("Click the region you wish to fill... (hit escape to abort)") while oldstate == newstate: event = g.getevent() if event.startswith("click"): # event is a string like "click 10 20 left none" evt, xstr, ystr, butt, mods = event.split() x = int(xstr) y = int(ystr) if x < minx or x > maxx or y < miny or y > maxy: # click is outside pattern's bounding box in unbounded universe g.warn("Click within the pattern's bounding box\n"+ "otherwise the fill will be unbounded.") else: # note that user might have changed drawing state newstate = g.getoption("drawingstate") oldstate = g.getcell(x, y) if oldstate == newstate: g.warn("The clicked cell must have a different state\n"+ "to the current drawing state.") else: g.doevent(event) # tell Golly to handle all further keyboard/mouse events g.getevent(False) # do flood fill starting with clicked cell g.show("Filling clicked region... (hit escape to stop)") clist = [ (x,y) ] g.setcell(x, y, newstate) oldsecs = time() while len(clist) > 0: # remove cell from start of clist x, y = clist.pop(0) newsecs = time() if newsecs - oldsecs >= 0.5: # show changed pattern every half second oldsecs = newsecs g.update() # check if any orthogonal neighboring cells are in oldstate if checkneighbor( x, y-1, oldstate): g.setcell( x, y-1, newstate) clist.append( (x, y-1) ) if checkneighbor( x, y+1, oldstate): g.setcell( x, y+1, newstate) clist.append( (x, y+1) ) if checkneighbor( x+1, y, oldstate): g.setcell( x+1, y, newstate) clist.append( (x+1, y) ) if checkneighbor( x-1, y, oldstate): g.setcell( x-1, y, newstate) clist.append( (x-1, y) ) # diagonal neighbors are more complicated because we don't # want to cross a diagonal line of live cells if checkneighbor( x+1, y+1, oldstate) and (g.getcell(x, y+1) == 0 or g.getcell(x+1, y) == 0): g.setcell( x+1, y+1, newstate) clist.append( (x+1, y+1) ) if checkneighbor( x+1, y-1, oldstate) and (g.getcell(x, y-1) == 0 or g.getcell(x+1, y) == 0): g.setcell( x+1, y-1, newstate) clist.append( (x+1, y-1) ) if checkneighbor( x-1, y+1, oldstate) and (g.getcell(x, y+1) == 0 or g.getcell(x-1, y) == 0): g.setcell( x-1, y+1, newstate) clist.append( (x-1, y+1) ) if checkneighbor( x-1, y-1, oldstate) and (g.getcell(x, y-1) == 0 or g.getcell(x-1, y) == 0): g.setcell( x-1, y-1, newstate) clist.append( (x-1, y-1) )
def floodfill(): newstate = g.getoption("drawingstate") oldstate = newstate # wait for user to click a cell g.show("Click the region you wish to fill... (hit escape to abort)") while oldstate == newstate: event = g.getevent() if event.startswith("click"): # event is a string like "click 10 20 left none" evt, xstr, ystr, butt, mods = event.split() x = int(xstr) y = int(ystr) if x < minx or x > maxx or y < miny or y > maxy: # click is outside pattern's bounding box in unbounded universe g.warn("Click within the pattern's bounding box\n" + "otherwise the fill will be unbounded.") else: # note that user might have changed drawing state newstate = g.getoption("drawingstate") oldstate = g.getcell(x, y) if oldstate == newstate: g.warn("The clicked cell must have a different state\n" + "to the current drawing state.") else: g.doevent(event) # tell Golly to handle all further keyboard/mouse events g.getevent(False) # do flood fill starting with clicked cell g.show("Filling clicked region... (hit escape to stop)") clist = [(x, y)] g.setcell(x, y, newstate) oldsecs = time() while len(clist) > 0: # remove cell from start of clist x, y = clist.pop(0) newsecs = time() if newsecs - oldsecs >= 0.5: # show changed pattern every half second oldsecs = newsecs g.update() # check if any orthogonal neighboring cells are in oldstate if checkneighbor(x, y - 1, oldstate): g.setcell(x, y - 1, newstate) clist.append((x, y - 1)) if checkneighbor(x, y + 1, oldstate): g.setcell(x, y + 1, newstate) clist.append((x, y + 1)) if checkneighbor(x + 1, y, oldstate): g.setcell(x + 1, y, newstate) clist.append((x + 1, y)) if checkneighbor(x - 1, y, oldstate): g.setcell(x - 1, y, newstate) clist.append((x - 1, y)) # diagonal neighbors are more complicated because we don't # want to cross a diagonal line of live cells if checkneighbor(x + 1, y + 1, oldstate) and (g.getcell( x, y + 1) == 0 or g.getcell(x + 1, y) == 0): g.setcell(x + 1, y + 1, newstate) clist.append((x + 1, y + 1)) if checkneighbor(x + 1, y - 1, oldstate) and (g.getcell( x, y - 1) == 0 or g.getcell(x + 1, y) == 0): g.setcell(x + 1, y - 1, newstate) clist.append((x + 1, y - 1)) if checkneighbor(x - 1, y + 1, oldstate) and (g.getcell( x, y + 1) == 0 or g.getcell(x - 1, y) == 0): g.setcell(x - 1, y + 1, newstate) clist.append((x - 1, y + 1)) if checkneighbor(x - 1, y - 1, oldstate) and (g.getcell( x, y - 1) == 0 or g.getcell(x - 1, y) == 0): g.setcell(x - 1, y - 1, newstate) clist.append((x - 1, y - 1))
# Run the current pattern for a given number of steps (using current # step size) and create a plot of population vs time in separate layer. # Author: Andrew Trevorrow ([email protected]), May 2007. import golly as g from glife import * from glife.text import make_text from time import time from random import random import math clist=[] fill = int(g.getoption("drawingstate")) def rp_plot(a,b,fill=fill): ma=max(a) mb=max(b) l=range(min(len(a),min(b))) # g.note('%i'%min(8,8)) for i in range(min(len(a),len(b))): # for i in range(ma+1): # for j in range(mb+1): # if i==a[i] and j==b[j]: g.setcell(a[i],-b[i],fill) # d=norm(input[i],input[j]) # if d<=dmax: # clist.append(i) # clist.append(j) # clist.append(input[i]==input[j])
else: row.append(0) adjacencymatrix.append(row) return adjacencymatrix def mathematica(s): s = s.replace('[', '{') s = s.replace(']', '}') return s state = int( g.getstring('what cell state to screen for', '%s' % g.getoption("drawingstate"))) torus = int(g.getstring('treat selection as torus? 1 for True', '0')) pbox = g.getselrect() clist = g.getcells(pbox) parsed_clist = parse_list(g.getcells(pbox)) adjmat = adjacencymatrix(parsed_clist, pbox, torus) eigval = np.linalg.eigvalsh(adjmat) eigval = np.round(eigval, decimals=1) h = hash(tuple(eigval)) # a=eigval # b = a.view(np.uint8) # h=hashlib.sha1(b).hexdigest() # h=hash(eigval)