def evolve_playfield(playfield, new_pf, alpaca, verbose=False): if playfield.min_y is None: return bb = BoundingBox(0, 0, 0, 0) fit_bounding_box(alpaca, bb) y = playfield.min_y - bb.max_dy while y <= playfield.max_y - bb.min_dy: x = playfield.min_x - bb.max_dx while x <= playfield.max_x - bb.min_dx: state_id = playfield.get(x, y) if verbose: print "state at (%d,%d): %s" % (x, y, state_id) state_ast = find_state_defn(alpaca, state_id) if verbose: print " => %r" % state_ast classes = state_ast.classes rules = state_ast.rules new_state_id = apply_rules(alpaca, playfield, x, y, rules, classes, verbose=verbose) if new_state_id is None: new_state_id = state_id if verbose: print "new state: %s" % new_state_id new_pf.set(x, y, new_state_id) x += 1 y += 1
def compile(self): bb = BoundingBox(0, 0, 0, 0) fit_bounding_box(self.alpaca, bb) self.file.write("""\ /* * This file was AUTOMATICALLY generated from an ALPACA description. * EDIT AT YOUR OWN RISK! */ function in_nbhd_pred(pf, x, y, pred, nbhd) { var count = 0; for (var i = 0; i < nbhd.length; i++) { if (pred(pf.get(x+nbhd[i][0], y+nbhd[i][1]))) { count++; } } return count; } function in_nbhd_eq(pf, x, y, stateId, nbhd) { return in_nbhd_pred(pf, x, y, function(x) { return x === stateId; }, nbhd); } function evolve_playfield(pf, new_pf) { pf.map(new_pf, evalState, %d, %d, %d, %d); } """ % ( -1 * bb.max_dx, -1 * bb.max_dy, -1 * bb.min_dx, -1 * bb.min_dy, )) # write the CA's load and dump mappers repr_map = construct_representation_map(self.alpaca) self.file.write("function loadMapper(c) {\n") for (char, state_id) in repr_map.iteritems(): self.file.write(" if (c === '%s') return '%s';\n" % (char, state_id)) self.file.write("};\n") self.file.write("function dumpMapper(s) {\n") for (char, state_id) in repr_map.iteritems(): self.file.write(" if (s === '%s') return '%s';\n" % (state_id, char)) self.file.write("};\n") class_map = get_class_map(self.alpaca) for (class_id, state_set) in class_map.iteritems(): self.file.write("function is_%s(st) {\n" % class_id) self.file.write(" return ") for state_id in state_set: self.file.write("(st === '%s') || " % state_id) self.file.write("0;\n}\n\n") for defn in self.alpaca.defns: if defn.type == 'ClassDefn': self.compile_class_defn(defn) for defn in self.alpaca.defns: if defn.type == 'StateDefn': self.compile_state_defn(defn) self.write_evalstate_function() pf = get_defined_playfield(self.alpaca) if pf is not None: self.file.write("var defaultCell = '%s';\n" % pf.default) self.file.write("var initialPlayfield = [{}];\n".format(','.join( "[%d, %d, '%s']" % (x, y, c) for (x, y, c) in pf.iteritems()))) return True
def compile(self): bb = BoundingBox(0, 0, 0, 0) fit_bounding_box(self.alpaca, bb) self.file.write("""\ /* * This file was AUTOMATICALLY generated from an ALPACA description. * EDIT AT YOUR OWN RISK! */ """) if self.options is not None and \ self.options.include_yoob_playfield_inline: self.file.write(YOOB_PLAYFIELD_JS) self.file.write(""" function in_nbhd_pred(pf, x, y, pred, nbhd) { var count = 0; for (var i = 0; i < nbhd.length; i++) { if (pred(pf.get(x+nbhd[i][0], y+nbhd[i][1]))) { count++; } } return count; } function in_nbhd_eq(pf, x, y, stateId, nbhd) { return in_nbhd_pred(pf, x, y, function(x) { return x === stateId; }, nbhd); } function evolve_playfield(pf, new_pf) { pf.map(new_pf, evalState, %d, %d, %d, %d); } """ % (-1 * bb.max_dx, -1 * bb.max_dy, -1 * bb.min_dx, -1 * bb.min_dy,)) # write the CA's load and dump mappers repr_map = construct_representation_map(self.alpaca) self.file.write("function loadMapper(c) {\n") for (char, state_id) in repr_map.iteritems(): self.file.write(" if (c === '%s') return '%s';\n" % (char, state_id)) self.file.write("};\n") self.file.write("function dumpMapper(s) {\n") for (char, state_id) in repr_map.iteritems(): self.file.write(" if (s === '%s') return '%s';\n" % (state_id, char)) self.file.write("};\n") class_map = get_class_map(self.alpaca) for (class_id, state_set) in class_map.iteritems(): self.file.write("function is_%s(st) {\n" % class_id) self.file.write(" return "); for state_id in state_set: self.file.write("(st === '%s') || " % state_id) self.file.write("0;\n}\n\n") defns = self.alpaca.children[0] for defn in defns.children: if defn.type == 'ClassDefn': self.compile_class_defn(defn) for defn in defns.children: if defn.type == 'StateDefn': self.compile_state_defn(defn) self.write_evalstate_function() pf = get_defined_playfield(self.alpaca) if pf is not None: self.file.write(""" pf = new yoob.Playfield(); pf.setDefault('%s'); """ % pf.default) for (x, y, c) in pf.iteritems(): self.file.write("pf.putDirty(%d, %d, '%s');\n" % (x, y, c)) self.file.write("pf.recalculateBounds();\n") self.file.write(r""" newPf = new yoob.Playfield(); newPf.setDefault('%s'); evolve_playfield(pf, newPf); console.log('-----'); console.log(newPf.dump(dumpMapper).replace(/\n$/, "")); console.log('-----'); """ % pf.default) return True
def compile(self): bb = BoundingBox(0, 0, 0, 0) fit_bounding_box(self.alpaca, bb) self.file.write("""\ /* * This file was AUTOMATICALLY generated from an ALPACA description. * EDIT AT YOUR OWN RISK! */ function in_nbhd_pred(pf, x, y, pred, nbhd) { var count = 0; for (var i = 0; i < nbhd.length; i++) { if (pred(pf.get(x+nbhd[i][0], y+nbhd[i][1]))) { count++; } } return count; } function in_nbhd_eq(pf, x, y, stateId, nbhd) { return in_nbhd_pred(pf, x, y, function(x) { return x === stateId; }, nbhd); } function evolve_playfield(pf, new_pf) { pf.map(new_pf, evalState, %d, %d, %d, %d); } """ % (-1 * bb.max_dx, -1 * bb.max_dy, -1 * bb.min_dx, -1 * bb.min_dy,)) # write the CA's load and dump mappers repr_map = construct_representation_map(self.alpaca) self.file.write("function loadMapper(c) {\n") for (char, state_id) in repr_map.iteritems(): self.file.write(" if (c === '%s') return '%s';\n" % (char, state_id)) self.file.write("};\n") self.file.write("function dumpMapper(s) {\n") for (char, state_id) in repr_map.iteritems(): self.file.write(" if (s === '%s') return '%s';\n" % (state_id, char)) self.file.write("};\n") class_map = get_class_map(self.alpaca) for (class_id, state_set) in class_map.iteritems(): self.file.write("function is_%s(st) {\n" % class_id) self.file.write(" return "); for state_id in state_set: self.file.write("(st === '%s') || " % state_id) self.file.write("0;\n}\n\n") for defn in self.alpaca.defns: if defn.type == 'ClassDefn': self.compile_class_defn(defn) for defn in self.alpaca.defns: if defn.type == 'StateDefn': self.compile_state_defn(defn) self.write_evalstate_function() pf = get_defined_playfield(self.alpaca) if pf is not None: self.file.write("var defaultCell = '%s';\n" % pf.default) self.file.write("var initialPlayfield = [{}];\n".format( ','.join("[%d, %d, '%s']" % (x, y, c) for (x, y, c) in pf.iteritems()) )) return True