import os import golly as g from glife import * selrect = g.getselrect() if len(selrect) == 0: g.exit("There is no selection.") # check that a layer is available for the metafied pattern; # if metafied layer already exists then we'll use that layername = "metafied" metalayer = -1 for i in xrange(g.numlayers()): if g.getname(i) == layername: metalayer = i break if metalayer < 0 and g.numlayers() == g.maxlayers(): g.exit("You need to delete a layer.") # note that getrule returns canonical rule string rulestr = g.getrule().split(":")[0] if (not rulestr.startswith("B")) or (rulestr.find("/S") == -1): g.exit("This script only works with B*/S* rules.") # get the current selection slist = g.getcells(selrect) selwidth = selrect[2] selheight = selrect[3] # create a 2D list of 0s and 1s representing entire selection livecell = [[0 for y in xrange(selheight)] for x in xrange(selwidth)] for i in xrange(0, len(slist), 2):
t.put(barwd * (state + 1) - barwd / 2 - twd / 2, -barht - tht - 3) # -------------------------------------------------------------------- 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()
else: popfunc = lambda: g.getpop() for i in range(numsteps): g.run(1) if g.empty(): break poplist.append(int(popfunc())) h = int(g.hash(g.getrect())) if h in hashlist: break hashlist.append(h) layername = "recurrent plot" poplayer = -1 for i in xrange(g.numlayers()): if g.getname(i) == layername: poplayer = i break if poplayer == -1 and g.numlayers() == g.maxlayers(): g.exit("You need to delete a layer.") if poplayer == -1: poplayer = g.addlayer() else: g.setlayer(poplayer) g.new(layername) rp_plot(poplist)
g.setlayer(currindex) startindex = currindex - 1 envindex = currindex - 2 elif currindex + 1 < g.numlayers() \ and g.getname(currindex) == startname \ and g.getname(currindex - 1) == envname : # switch from starting layer to current layer and continue currindex += 1 g.setlayer(currindex) startindex = currindex - 1 envindex = currindex - 2 else: # start a new envelope using pattern in current layer if g.numlayers() + 1 > g.maxlayers(): g.exit("You need to delete a couple of layers.") if g.numlayers() + 2 > g.maxlayers(): g.exit("You need to delete a layer.") # get current layer's starting pattern startpatt = g.getcells(g.getrect()) envindex = g.addlayer() # create layer for remembering all live cells g.setcolors([-1,100,100,100]) # set all states to darkish gray g.putcells(startpatt) # copy starting pattern into this layer startindex = g.addlayer() # create layer for starting pattern g.setcolors([-1,0,255,0]) # set all states to green g.putcells(startpatt) # copy starting pattern into this layer
def export_icons(iconsection, rulename): global multi_color_icons if multi_color_icons: # prepend a new @COLORS section with the average colors in each icon iconsection = create_average_colors(iconsection) + iconsection # replace any illegal filename chars with underscores filename = rulename.replace("/", "_").replace("\\", "_") # we will only create/update a .rule file in the user's rules folder # (ie. we don't modify the supplied Rules folder) rulepath = g.getdir("rules") + filename + ".rule" fileexists = os.path.isfile(rulepath) if fileexists: # .rule file already exists so replace or add @ICONS section rulefile = open(rulepath, "rU") # create a temporary file for writing new rule info temphdl, temppath = mkstemp() tempfile = open(temppath, "w") wroteicons = False skiplines = False for line in rulefile: if line.startswith("@ICONS"): # replace the existing @ICONS section tempfile.write(iconsection) wroteicons = True skiplines = True elif line.startswith("@COLORS") and multi_color_icons: # skip the existing @COLORS section # (iconsection contains a new @COLORS section) skiplines = True elif skiplines and line.startswith("@"): if wroteicons: tempfile.write("\n") skiplines = False if not skiplines: tempfile.write(line) if not wroteicons: # .rule file had no @ICONS section tempfile.write("\n") tempfile.write(iconsection) # close files rulefile.close() tempfile.flush() tempfile.close() os.close(temphdl) # remove original .rule file and rename temporary file os.remove(rulepath) move(temppath, rulepath) else: # .rule file doesn't exist so create it rulefile = open(rulepath, "w") rulefile.write("@RULE " + filename + "\n\n") if not multi_color_icons: # grayscale icons, so check if Rules/filename.rule exists # and if so copy any existing @COLORS section suppliedrule = g.getdir("app") + "Rules/" + filename + ".rule" if os.path.isfile(suppliedrule): colordata = get_color_section(suppliedrule) if len(colordata) > 0: rulefile.write(colordata) rulefile.write(iconsection) rulefile.flush() rulefile.close() # create another layer for displaying the new icons if g.numlayers() < g.maxlayers(): g.addlayer() g.new("icon test") g.setrule(rulename) for i in range(g.numstates() - 1): g.setcell(i, 0, i + 1) g.fit() g.setoption("showicons", True) g.update() if fileexists: g.note("Updated the icon data in " + rulepath) else: g.note("Created " + rulepath)
if state > 0: g.setcell(x+col, y+row, state) # -------------------------------------------------------------------- def multi_color_icons(iconcolors): # return True if at least one icon color isn't a shade of gray for R,G,B in iconcolors: if R != G or G != B: return True # grayscale return False # -------------------------------------------------------------------- # check that a layer is available if g.numlayers() == g.maxlayers(): g.exit("You need to delete a layer.") # WARNING: changing this prefix will require same change in icon-exporter.py layerprefix = "imported icons for " if g.getname().startswith(layerprefix): g.exit("You probably meant to run icon-exporter.py.") g.addlayer() rulename = g.getrule().split(":")[0] # search for rulename.rule and import any icon data (also builds iconcolors) import_icons(rulename) if len(iconcolors) == 0 and ("-" in rulename) and not (rulename.endswith("-shared")): # rulename.rule has no icons and rulename contains a hyphen, so
except: # getrect failed because pattern is too big g.fit() # -------------------------------------------------------------------- if g.empty(): g.exit("There is no pattern.") # check that a layer is available for population plot layername = "population plot" poplayer = -1 for i in xrange(g.numlayers()): if g.getname(i) == layername: poplayer = i break if poplayer == -1 and g.numlayers() == g.maxlayers(): g.exit("You need to delete a layer.") # prompt user for number of steps numsteps = xlen s = g.getstring("Enter the number of steps:", str(numsteps), "Population plotter") if len(s) > 0: numsteps = int(s) if numsteps <= 0: g.exit() # generate pattern for given number of steps poplist = [ int(g.getpop()) ] genlist = [ int(g.getgen()) ] oldsecs = time() for i in xrange(numsteps): g.step()
g.setlayer(currindex) startindex = currindex - 1 envindex = currindex - 2 elif currindex + 1 < g.numlayers() \ and g.getname(currindex) == startname \ and g.getname(currindex - 1) == envname : # switch from starting layer to current layer and continue currindex += 1 g.setlayer(currindex) startindex = currindex - 1 envindex = currindex - 2 else: # start a new envelope using pattern in current layer if g.numlayers() + 1 > g.maxlayers(): g.exit("You need to delete a couple of layers.") if g.numlayers() + 2 > g.maxlayers(): g.exit("You need to delete a layer.") # get current layer's starting pattern startpatt = g.getcells(g.getrect()) envindex = g.addlayer() # create layer for remembering all live cells g.setcolors([-1, 100, 100, 100]) # set all states to darkish gray g.putcells(startpatt) # copy starting pattern into this layer startindex = g.addlayer() # create layer for starting pattern g.setcolors([-1, 0, 255, 0]) # set all states to green g.putcells(startpatt) # copy starting pattern into this layer
def export_icons(iconsection, rulename): global multi_color_icons if multi_color_icons: # prepend a new @COLORS section with the average colors in each icon iconsection = create_average_colors(iconsection) + iconsection # replace any illegal filename chars with underscores filename = rulename.replace("/","_").replace("\\","_") # we will only create/update a .rule file in the user's rules folder # (ie. we don't modify the supplied Rules folder) rulepath = g.getdir("rules") + filename + ".rule" fileexists = os.path.isfile(rulepath) if fileexists: # .rule file already exists so replace or add @ICONS section rulefile = open(rulepath,"rU") # create a temporary file for writing new rule info temphdl, temppath = mkstemp() tempfile = open(temppath,"w") wroteicons = False skiplines = False for line in rulefile: if line.startswith("@ICONS"): # replace the existing @ICONS section tempfile.write(iconsection) wroteicons = True skiplines = True elif line.startswith("@COLORS") and multi_color_icons: # skip the existing @COLORS section # (iconsection contains a new @COLORS section) skiplines = True elif skiplines and line.startswith("@"): if wroteicons: tempfile.write("\n") skiplines = False if not skiplines: tempfile.write(line) if not wroteicons: # .rule file had no @ICONS section tempfile.write("\n") tempfile.write(iconsection) # close files rulefile.close() tempfile.flush() tempfile.close() os.close(temphdl) # remove original .rule file and rename temporary file os.remove(rulepath) move(temppath, rulepath) else: # .rule file doesn't exist so create it rulefile = open(rulepath,"w") rulefile.write("@RULE " + filename + "\n\n") if not multi_color_icons: # grayscale icons, so check if Rules/filename.rule exists # and if so copy any existing @COLORS section suppliedrule = g.getdir("app") + "Rules/" + filename + ".rule" if os.path.isfile(suppliedrule): colordata = get_color_section(suppliedrule) if len(colordata) > 0: rulefile.write(colordata) rulefile.write(iconsection) rulefile.flush() rulefile.close() # create another layer for displaying the new icons if g.numlayers() < g.maxlayers(): g.addlayer() g.new("icon test") g.setrule(rulename) for i in xrange(g.numstates()-1): g.setcell(i, 0, i+1) g.fit() g.setoption("showicons",True) g.update() if fileexists: g.note("Updated the icon data in " + rulepath) else: g.note("Created " + rulepath)
# -------------------------------------------------------------------- def multi_color_icons(iconcolors): # return True if at least one icon color isn't a shade of gray for R, G, B in iconcolors: if R != G or G != B: return True # grayscale return False # -------------------------------------------------------------------- # check that a layer is available if g.numlayers() == g.maxlayers(): g.exit("You need to delete a layer.") # WARNING: changing this prefix will require same change in icon-exporter.py layerprefix = "imported icons for " if g.getname().startswith(layerprefix): g.exit("You probably meant to run icon-exporter.py.") g.addlayer() rulename = g.getrule().split(":")[0] # search for rulename.rule and import any icon data (also builds iconcolors) import_icons(rulename) if len(iconcolors) == 0 and ( "-" in rulename) and not (rulename.endswith("-shared")):