def fit_if_not_visible(): try: r = rect(g.getrect()) if (not r.empty) and (not r.visible()): g.fit() except: # getrect failed because pattern is too big g.fit()
def calculate_density(): bbox = gl.rect(g.getrect()) if bbox.empty: d = 0 else: d = float(g.getpop()) / float(bbox.wd*bbox.ht) return d
def is_static(): global prev_world bbox = rect(g.getrect()) if bbox.empty: return False cur_world = g.hash(g.getrect()) if prev_world and prev_world == cur_world: return True else: prev_world = cur_world
if g.empty(): g.exit("There is no pattern.") if g.numstates() == 256: g.exit("No room for extra state.") # check that a layer is available for the histogram histname = "histogram" histlayer = -1 for i in xrange(g.numlayers()): if g.getname(i) == histname: histlayer = i break if histlayer == -1 and g.numlayers() == g.maxlayers(): g.exit("You need to delete a layer.") # use selection rect if it exists, otherwise use pattern bounds label = "Selection" r = rect(g.getselrect()) if r.empty: label = "Pattern" r = rect(g.getrect()) # count all cell states in r g.show("Counting cell states...") counted = 0 totalcells = r.wd * r.ht statecount = [0] * g.numstates() oldsecs = time() for row in xrange(r.top, r.top + r.height): for col in xrange(r.left, r.left + r.width): counted += 1 statecount[g.getcell(col, row)] += 1 newsecs = time()
# 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 <*****@*****.**> 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'
def oscillating(): # return True if the pattern is empty, stable or oscillating # first get current pattern's bounding box csel = g.getselrect() g.shrink() prect = g.getselrect() g.select(csel) pbox = rect(prect) if pbox.empty: g.show("The pattern is empty.") return True # get current pattern and create hash of "normalized" version -- ie. shift # its top left corner to 0,0 -- so we can detect spaceships and knightships ## currpatt = pattern( g.getcells(prect) ) ## h = hash( tuple( currpatt(-pbox.left, -pbox.top) ) ) # use Golly's hash command (3 times faster than above code) h = g.hash(prect) # check if outer-totalistic rule has B0 but not S8 rule = g.getrule().split(":")[0] hasB0notS8 = rule.startswith("B0") and (rule.find("/") > 1) and not rule.endswith("8") # determine where to insert h into hashlist pos = 0 listlen = len(hashlist) while pos < listlen: if h > hashlist[pos]: pos += 1 elif h < hashlist[pos]: # shorten lists and append info below del hashlist[pos:listlen] del genlist[pos:listlen] del poplist[pos:listlen] del boxlist[pos:listlen] break else: # h == hashlist[pos] so pattern is probably oscillating, but just in # case this is a hash collision we also compare pop count and box size if (pbox.wd == boxlist[pos].wd) and \ (pbox.ht == boxlist[pos].ht): # (int(g.getpop()) == poplist[pos]) period = int(g.getgen()) - genlist[pos] if hasB0notS8 and (period % 2 > 0) and (pbox == boxlist[pos]): # ignore this hash value because B0-and-not-S8 rules are # emulated by using different rules for odd and even gens, # so it's possible to have identical patterns at gen G and # gen G+p if p is odd return False if period == 1: if pbox == boxlist[pos]: g.show("The pattern is stable.") else: show_spaceship_speed(1, 0, 0) elif pbox == boxlist[pos]: g.show("Oscillator detected (period = " + str(period) + ")") else: deltax = abs(boxlist[pos].x - pbox.x) deltay = abs(boxlist[pos].y - pbox.y) show_spaceship_speed(period, deltax, deltay) return True else: # look at next matching hash value or insert if no more pos += 1 # store hash/gen/pop/box info at same position in various lists hashlist.insert(pos, h) genlist.insert(pos, int(g.getgen())) poplist.insert(pos, int(g.getpop())) boxlist.insert(pos, pbox) return False
def fit_if_not_visible(): # fit pattern in viewport if not empty and not completely visible r = rect(g.getselrect()) if (not r.empty) and (not r.visible()): g.fit()
def oscillating(): # return True if the pattern is empty, stable or oscillating # first get current pattern's bounding box prect = g.getrect() pbox = rect(prect) if pbox.empty: g.show("The pattern is empty.") return True # get current pattern and create hash of "normalized" version -- ie. shift # its top left corner to 0,0 -- so we can detect spaceships and knightships ## currpatt = pattern( g.getcells(prect) ) ## h = hash( tuple( currpatt(-pbox.left, -pbox.top) ) ) # use Golly's hash command (3 times faster than above code) h = g.hash(prect) # check if outer-totalistic rule has B0 but not S8 rule = g.getrule().split(":")[0] hasB0notS8 = rule.startswith("B0") and (rule.find("/") > 1) and not rule.endswith("8") # determine where to insert h into hashlist pos = 0 listlen = len(hashlist) while pos < listlen: if h > hashlist[pos]: pos += 1 elif h < hashlist[pos]: # shorten lists and append info below del hashlist[pos : listlen] del genlist[pos : listlen] del poplist[pos : listlen] del boxlist[pos : listlen] break else: # h == hashlist[pos] so pattern is probably oscillating, but just in # case this is a hash collision we also compare pop count and box size if (int(g.getpop()) == poplist[pos]) and \ (pbox.wd == boxlist[pos].wd) and \ (pbox.ht == boxlist[pos].ht): period = int(g.getgen()) - genlist[pos] if hasB0notS8 and (period % 2 > 0) and (pbox == boxlist[pos]): # ignore this hash value because B0-and-not-S8 rules are # emulated by using different rules for odd and even gens, # so it's possible to have identical patterns at gen G and # gen G+p if p is odd return False if period == 1: if pbox == boxlist[pos]: g.show("The pattern is stable.") else: show_spaceship_speed(1, 0, 0) elif pbox == boxlist[pos]: g.show("Oscillator detected (period = " + str(period) + ")") else: deltax = abs(boxlist[pos].x - pbox.x) deltay = abs(boxlist[pos].y - pbox.y) show_spaceship_speed(period, deltax, deltay) return True else: # look at next matching hash value or insert if no more pos += 1 # store hash/gen/pop/box info at same position in various lists hashlist.insert(pos, h) genlist.insert(pos, int(g.getgen())) poplist.insert(pos, int(g.getpop())) boxlist.insert(pos, pbox) return False
def fit_if_not_visible(): # fit pattern in viewport if not empty and not completely visible r = rect(g.getrect()) if (not r.empty) and (not r.visible()): g.fit()
# Change the entire pattern from emulated-Margolus states to N-state: from glife import rect import golly as g r = rect( g.getrect() ) if r.empty: g.exit("There is no pattern.") for row in xrange(r.top, r.top + r.height): for col in xrange(r.left, r.left + r.width): s = g.getcell(col,row) g.setcell(col, row, (s+s%2)/2-1)
# Calculates the density of live cells in the current pattern. # Author: Andrew Trevorrow ([email protected]), March 2006. # Updated to use exit command, Nov 2006. from glife import rect import golly as g bbox = rect( g.getrect() ) if bbox.empty: g.exit("The pattern is empty.") d = float( g.getpop() ) / ( float(bbox.wd) * float(bbox.ht) ) if d < 0.000001: g.show("Density = %.1e" % d) else: g.show("Density = %.6f" % d)
# Note: many compilers allow you to use the "0b" prefix for # writing numbers in binary. from glife import rect, validint import golly as g def getrow(x, y, width): s = "" for i in range(width): if g.getcell(x + i, y): s += "o" else: s += "." return s firstrow = rect(g.getselrect()) if len(firstrow) == 0: g.exit("There is no selection.") if firstrow.height != 1: g.exit("Incorrect selection dimensions (height must be 1)") width = firstrow.width period = g.getstring("Enter the period.") if not validint(period): g.exit("Period should be a positive integer.") period = int(period) if period <= 0: g.exit("Period should be a positive integer.") offset = g.getstring("Enter the offset (translation).") if not validint(offset): g.exit("Offset should be a positive integer.") offset = int(offset) if offset <= 0: g.exit("Offset should be a positive integer.") if period <= offset: g.exit("Period must be greater than offset.")
# Change the entire pattern from emulated-Margolus states to N-state: from glife import rect import golly as g r = rect(g.getrect()) if r.empty: g.exit("There is no pattern.") for row in xrange(r.top, r.top + r.height): for col in xrange(r.left, r.left + r.width): s = g.getcell(col, row) g.setcell(col, row, (s + s % 2) / 2 - 1)