Exemple #1
0
def update_waymarks(): # pylint: disable=R0912

    places = places_storage.all()

    roads = roads_storage.all_exists_roads()

    places_len = len(places)

    paths = [ [ Path() for place in xrange(places_len) ] for i in xrange(places_len)] # pylint: disable=W0612

    p2i = dict( (place.id, i) for i, place in enumerate(places))

    for i in xrange(len(places)):
        paths[i][i].update_path(0, None)

    for road in roads:
        i = p2i[road.point_1_id]
        j = p2i[road.point_2_id]
        paths[i][j].update_path(road.length, road.id)
        paths[j][i].update_path(road.length, road.id)

    for k in xrange(places_len):
        for i in xrange(places_len):
            for j in xrange(places_len):
                new_len = min(paths[i][j].length,
                              paths[i][k].length + paths[k][j].length)
                paths[i][j].update_path(new_len, paths[i][k].road_id)

    for row in paths:
        res = []
        for el in row:
            res.append(el.road_id)


    for i in xrange(places_len):
        for j in xrange(places_len):
            if paths[i][j].road_id is not None:
                road = roads_storage[paths[i][j].road_id]
            else:
                road = None

            waymark = waymarks_storage.look_for_road(point_from=places[i].id, point_to=places[j].id)

            if waymark:
                waymark.road = road
                waymark.length = paths[i][j].length
                waymark.save()
            else:
                waymark = WaymarkPrototype.create(point_from=places[i],
                                                  point_to=places[j],
                                                  road=road,
                                                  length=paths[i][j].length)

    waymarks_storage.update_version()
Exemple #2
0
    def get_available_positions(
        cls,
        center_x,
        center_y,
        building_position_radius=places_settings.BUILDING_POSITION_RADIUS):  # pylint: disable=R0914
        from the_tale.game.map.places.storage import places_storage, buildings_storage
        from the_tale.game.map.roads.storage import roads_storage
        from the_tale.game.map.roads.relations import PATH_DIRECTION

        positions = set()

        for i in xrange(0, building_position_radius + 1):
            for j in xrange(0, building_position_radius + 1):
                positions.add((center_x + i, center_y + j))
                positions.add((center_x - i, center_y + j))
                positions.add((center_x + i, center_y - j))
                positions.add((center_x - i, center_y - j))

        positions = set(pos for pos in positions
                        if 0 <= pos[0] < map_settings.WIDTH
                        and 0 <= pos[1] < map_settings.HEIGHT)

        removed_positions = set()

        for place in places_storage.all():
            removed_positions.add((place.x, place.y))

        for building in buildings_storage.all():
            removed_positions.add((building.x, building.y))

        for road in roads_storage.all_exists_roads():
            x, y = road.point_1.x, road.point_1.y
            for direction in road.path:
                if direction == PATH_DIRECTION.LEFT.value: x -= 1
                elif direction == PATH_DIRECTION.RIGHT.value: x += 1
                elif direction == PATH_DIRECTION.UP.value: y -= 1
                elif direction == PATH_DIRECTION.DOWN.value: y += 1

                removed_positions.add((x, y))

        result = positions - removed_positions

        return result if result else cls.get_available_positions(
            center_x,
            center_y,
            building_position_radius=building_position_radius + 1)
Exemple #3
0
    def get_available_positions(cls, center_x, center_y, building_position_radius=places_settings.BUILDING_POSITION_RADIUS): # pylint: disable=R0914
        from the_tale.game.map.places.storage import places_storage, buildings_storage
        from the_tale.game.map.roads.storage import roads_storage
        from the_tale.game.map.roads.relations import PATH_DIRECTION

        positions = set()

        for i in xrange(0, building_position_radius+1):
            for j in xrange(0, building_position_radius+1):
                positions.add((center_x+i, center_y+j))
                positions.add((center_x-i, center_y+j))
                positions.add((center_x+i, center_y-j))
                positions.add((center_x-i, center_y-j))

        positions =  set(pos for pos in positions
                         if 0 <= pos[0] < map_settings.WIDTH and 0 <= pos[1] < map_settings.HEIGHT)

        removed_positions = set()

        for place in places_storage.all():
            removed_positions.add((place.x, place.y))

        for building in buildings_storage.all():
            removed_positions.add((building.x, building.y))

        for road in roads_storage.all_exists_roads():
            x, y = road.point_1.x, road.point_1.y
            for direction in road.path:
                if direction == PATH_DIRECTION.LEFT.value: x -= 1
                elif direction == PATH_DIRECTION.RIGHT.value: x += 1
                elif direction == PATH_DIRECTION.UP.value: y -= 1
                elif direction == PATH_DIRECTION.DOWN.value: y += 1

                removed_positions.add((x, y))

        result = positions - removed_positions

        return result if result else cls.get_available_positions(center_x, center_y, building_position_radius=building_position_radius+1)
Exemple #4
0
 def roads_map(self):
     from the_tale.game.map.generator import drawer
     return drawer.get_roads_map(w=conf.map_settings.WIDTH, h=conf.map_settings.HEIGHT, roads=roads_storage.all_exists_roads())