Ejemplo n.º 1
0
def main():
    if len(sys.argv) < 2:
        sys.stderr.write("Usage: transform_tileset <tileset>\n")
        return 1

    logging.getLogger().setLevel(logging.DEBUG)

    conf = load_config(sys.argv[1])

    validate_tileset_config(conf, set(['city', 'field', 'road']))


    new_conf = {}
    new_conf['tiles'] = {}
    for name, tile in conf['tiles'].items():
        new_conf['tiles'][name] = copy.deepcopy(conf['tiles'][name])
        del new_conf['tiles'][name]['positions']
        new_conf['tiles'][name]['positions'] = {}
        #print "analyzing %s"  % (name)
        type_to_con = {}
        con_to_type = {}
        for what, data in tile['edges'].items():
            type = data['type']
            c = data['connections']
            if len(c) == 3:
                type_to_con.setdefault(type, set()).add(c[1])
                con_to_type[c[1]] = type
                type_to_con.setdefault('field', set()).add(c[0])
                type_to_con.setdefault('field', set()).add(c[2])
                con_to_type[c[0]] = 'field'
                con_to_type[c[2]] = 'field'
                
            else:
                type_to_con.setdefault(type, set()).add(c[0])
                con_to_type[c[0]] = type
        #print type_to_con
        #print con_to_type

        analyzed_connections = {}
        for c in tile['connections']:
            cset = set()
            for i in c:
                cset.add(i)
            if type_to_con[con_to_type[c[0]]] == cset:
                #print "connection is of type %s" % con_to_type[c[0]]
                analyzed_connections[con_to_type[c[0]]] = c[0]
            #else:
                #print "connection is ambigious"

        for pos, role in conf['tiles'][name]['positions'].items():
            new_conf['tiles'][name]['positions'][pos] = {}
            new_conf['tiles'][name]['positions'][pos]['role'] = role
            if role_to_type[role] in analyzed_connections:
                new_conf['tiles'][name]['positions'][pos]['connection'] = analyzed_connections[role_to_type[role]]
            else:
                new_conf['tiles'][name]['positions'][pos]['connection'] = '?'
            #new_conf['tiles'][name]['positions'][pos]['connection'] = role


    print json.dumps(new_conf, sort_keys=True, indent=4)
Ejemplo n.º 2
0
def main():
    if len(sys.argv) < 2:
        sys.stderr.write("Usage: validate_tileset <file>\n")
        return 1

    logging.getLogger().setLevel(logging.DEBUG)

    json = load_config(sys.argv[1])

    validate_tileset_config(json, set(['city', 'field', 'road']))

    sys.stdout.write('File %s is a valid tileset.\n' % (sys.argv[1]))
Ejemplo n.º 3
0
def main():
    if len(sys.argv) < 2:
        sys.stderr.write("Usage: transform_tileset <tileset>\n")
        return 1

    logging.getLogger().setLevel(logging.DEBUG)

    conf = load_config(sys.argv[1])

    validate_tileset_config(conf, set(['city', 'field', 'road']))


    
#    for tilenum, tilename in json['tiles'].items():
#        tile = json['base_tiles'][tilename]
#        if 'edges' in conf:
#            edges = {}
#            startnum = 1
#            for edge, material in conf['edges'].items():
#                edges[edge] = {}
#                edges[edge]['type'] = material
#                edges[edge]['connections'] = get_connections(material, startnum)
#                startnum += len(edges[edge]['connections'])
#            new_json['base_tiles'][tile] = {}
#            new_json['base_tiles'][tile]['edges'] = edges
#
#    print new_json

    b = Board(conf)
    new_conf = {}
    new_conf['tiles'] = {}
    for tilenum, ptile in b.boardtiles.items():
        tile = {}
        new_conf['tiles'][tilenum] = tile
        tile['name'] = ptile.tile.name
        tile['positions'] = {}
        for pos, role in ptile.tile.positions:
            tile['positions'][pos] = role
        tile['edges'] = {}
        startnum = 0
        for i, edge  in enumerate(ptile.tile.edges):
            pos = EDGES.by_ordinal(i)
            tile['edges'][pos] = {}
            tile['edges'][pos]['type'] = MATERIALS.by_ordinal(edge)
            tile['edges'][pos]['connections'] = get_connections(MATERIALS.by_ordinal(edge), startnum)
            startnum += len(tile['edges'][pos]['connections'])

    print json.dumps(new_conf, sort_keys=True, indent=4)
Ejemplo n.º 4
0
def main():
    if len(sys.argv) < 2:
        sys.stderr.write("Usage: transform_tileset <tileset>\n")
        return 1

    logging.getLogger().setLevel(logging.DEBUG)

    conf = load_config(sys.argv[1])

    validate_tileset_config(conf, set(['city', 'field', 'road']))


    new_conf = {}
    new_conf['tiles'] = {}
    new_conf['base_tiles'] = {}
    for name, tile in conf['tiles'].items():
        new_conf['base_tiles'][tile['name']] = copy.deepcopy(tile)
        del new_conf['base_tiles'][tile['name']]['name']
        new_conf['tiles'][name] = tile['name'] 

    for name, tile in new_conf['base_tiles'].items():
        positions = []
        for pos, data in tile['positions'].items():
            #print pos, data
            if 'connection' in data:
                #print pos
                x, y = Renderer.pos_to_tile_xy(pos, 0)
                #print x, y
                xp = int(float(x)/float(Renderer.tile_width) * 100.0)
                yp = int(float(y)/float(Renderer.tile_height) * 100.0)
                #print xp, yp
                this_pos = {}
                this_pos['connection'] = data['connection']
                this_pos['x'] = xp
                this_pos['y'] = yp
                positions.append(this_pos)
        del tile['positions']
        tile['positions'] = positions

    print json.dumps(new_conf, sort_keys=True, indent=4)
Ejemplo n.º 5
0
def main():
    if len(sys.argv) < 2:
        sys.stderr.write("Usage: construct_random_board <tileset>\n")
        return 1

    logging.getLogger().setLevel(logging.DEBUG)

    print "Loading ..."
    json = load_config(sys.argv[1])

    validate_tileset_config(json, set(['city', 'field', 'road']))


    seed = int(time.time())
    #seed = 1333213878
    print "Constructing with seed %s ..." % (seed)

    random.seed(seed)

    b = Board(json)
    failed_count = 0
    while len(b.tilesleft) > 0:
        tile = random.sample(b.tilesleft, 1)[0]
        locations = b.playable_locations(tile)

        location = None
        playable_locations = []
        location_neighbour_count = 0
        for l in locations:
            count = count_neighbours(b, l)

            if count > location_neighbour_count:
                location_neighbour_count = count
                playable_locations = []
                playable_locations.append(l)
                location = l
            elif count == location_neighbour_count:
                playable_locations.append(l)

        #location = random.sample(locations, 1)[0]
        if not playable_locations:
            logging.debug('Could not play tile %s\n%s' % (tile, b))
            failed_count += 1
            continue

        most_playable = 0
        location = None
        for l in playable_locations:
            count = 0
            for r in ROTATIONS.values():
                if b.is_legal_on_location(tile, l, ROTATIONS[r]):
                    count += 1

            if count >= most_playable:
                most_playable = count
                location = l

        #location = random.sample(playable_locations, 1)[0]

        for r in ROTATIONS.values():
            if b.is_legal_on_location(tile, location, ROTATIONS[r]):
                b.add_to_board(tile, location, ROTATIONS[r])
                break

    print 'Done, had to retry %d tiles' % failed_count

    print b
    d = b.dimensions()
    print 'Board size: %dx%d' % (d[0], d[1])

    filename = 'board.html'
    print "Rendering to %s" % (filename)
    html = HtmlRenderer.render(b)
    file = open(filename, 'w')
    file.write(html)
    file.close()