Beispiel #1
0
    def accessible_land(self, grid, from_, minx=1, maxx=118, miny=1, maxy=118):
        """
    Returns a random land tile that is accessible from the given tile
    :Parameters:
      grid : MapGrid
        A MapGrid object created from the world map grid
      fromx : int
        The x coordinate the tile must be accessible from
      fromy : int
        The y coordinate the tile must be accessible from
      minx : int
        The minimum x coordinate (default 1)
      maxx : int
        The maximum x coordinate (default 118)
      miny : int
        The minimum x coordinate (default 1)
      maxy : int
        The maximum y coordinate (default 118)


    rtype: tuple
    return: An x and y coordinate on the map.
    """
        came_from = {}
        x = y = 0
        while not (x, y) in came_from.keys():
            x, y = random.randint(minx, maxx), random.randint(miny, maxy)
            came_from, cost_so_far = pathfinding.a_star_search(
                grid, from_, (x, y))
        return x, y
Beispiel #2
0
  def accessible_land(self, grid, from_, minx=1, maxx=118, miny=1, maxy=118):
    """
    Returns a random land tile that is accessible from the given tile
    :Parameters:
      grid : MapGrid
        A MapGrid object created from the world map grid
      fromx : int
        The x coordinate the tile must be accessible from
      fromy : int
        The y coordinate the tile must be accessible from
      minx : int
        The minimum x coordinate (default 1)
      maxx : int
        The maximum x coordinate (default 118)
      miny : int
        The minimum x coordinate (default 1)
      maxy : int
        The maximum y coordinate (default 118)


    rtype: tuple
    return: An x and y coordinate on the map.
    """
    came_from = {}
    x = y = 0
    while not (x, y) in came_from.keys():
      x, y = random.randint(minx, maxx), random.randint(miny, maxy)
      came_from, cost_so_far = pathfinding.a_star_search(grid, from_, (x, y))
    return x, y
def get_path(the_map):
    path = pathfinding.a_star_search(the_map, None)
    if path is None:
        path = []

    #pathfinding.pull_string(the_map, path)

    return path
Beispiel #4
0
  def is_accessible(self, grid, from_, to):
    """
    Determines if a particular tile is accessible from another
    :Parameters:
      grid : MapGrid
        A MapGrid object created from the world map grid
      from : tuple(int)
        x and y coordinates of the starting point.

    rtype: bool
    return: Whether or not the player is able to walk between the 2 coordinates.
    """
    came_from, cost_so_far = pathfinding.a_star_search(grid, from_, to)
    return to in came_from.keys()
Beispiel #5
0
    def is_accessible(self, grid, from_, to):
        """
    Determines if a particular tile is accessible from another
    :Parameters:
      grid : MapGrid
        A MapGrid object created from the world map grid
      from : tuple(int)
        x and y coordinates of the starting point.

    rtype: bool
    return: Whether or not the player is able to walk between the 2 coordinates.
    """
        came_from, cost_so_far = pathfinding.a_star_search(grid, from_, to)
        return to in came_from.keys()
Beispiel #6
0
  def plot_size(self, grid, point):
    """
    Determines if a particular tile is accessible from another
    :Parameters:
      grid : MapGrid
        A MapGrid object created from the world map grid
      from_ : tuple(int)
        x and y coordinates of the starting point.
      to : tuple(int)
        x and y coordinates of the ending point.

    rtype: bool
    return: Whether or not the player is able to walk between the 2 coordinates.
    """
    came_from, cost_so_far = \
      pathfinding.a_star_search(grid, point, (self.map_width, self.map_height))
    return len(came_from.keys())
Beispiel #7
0
    def plot_size(self, grid, point):
        """
    Determines if a particular tile is accessible from another
    :Parameters:
      grid : MapGrid
        A MapGrid object created from the world map grid
      from_ : tuple(int)
        x and y coordinates of the starting point.
      to : tuple(int)
        x and y coordinates of the ending point.

    rtype: bool
    return: Whether or not the player is able to walk between the 2 coordinates.
    """
        came_from, cost_so_far = \
          pathfinding.a_star_search(grid, point, (self.map_width, self.map_height))
        return len(came_from.keys())
Beispiel #8
0
def _update_resource_cell_selected_route_creation(self, op):
    if op == input_retrieval.CANCEL:
        self.route_attempts.clear()
        self.selected = None
        self.action_desc = ""
        self.state_type = STANDARD
    #with resource cell selected, still only recalculate on cursor update
    if op in input_retrieval.CARDDRS:
        #Always clear attempts?
        self.route_attempts.clear()
        self.action_desc = ""

        attempted_path_from_start, pathfinding_cost = pathfinding.a_star_search(
            self.game_map, self.selected.position, self.cursor)
        if attempted_path_from_start:
            path_from_start_tiles = []
            for pt in attempted_path_from_start:
                if not self.game_map.get_tile(pt):
                    path_from_start_tiles.append(game_map.RouteAttemptTile(pt))
            attempted_start_route = game_map.RouteAttempt(
                path_from_start_tiles)
            self.route_attempts.append(attempted_start_route)
            self.action_desc = "About to create {0:0} route tiles, with cost {1:0} out of available {2:0}. PF Cost {3:0}".format(
                len(path_from_start_tiles),
                OUTPOST_CREATION_RESOURCE_COST * len(path_from_start_tiles),
                self.selected.cluster.get_total_amount(), pathfinding_cost)
            #print(attempted_path_from_start)
    if op == input_retrieval.SELECT:
        #Attempt route creation
        if len(self.route_attempts):
            ra = self.route_attempts[0]
            route_creation_cost = len(
                ra.attempt_tiles) * OUTPOST_CREATION_RESOURCE_COST
            if self.selected.cluster.remove_amount(route_creation_cost):
                for tile in ra.attempt_tiles:
                    outpost = game_map.Outpost(tile.position)
                    self.game_map.outposts.append(outpost)
                    self.game_map.add_tile(outpost, outpost.position)
            self.route_attempts.clear()
            self.selected = None
            self.action_desc = ""
            self.state_type = STANDARD
Beispiel #9
0
    def load_map(self, name, entrance_name, display_size):
        filename = get_map(name)

        # load data from pytmx
        tmx_data = load_pygame(filename)

        # create new data source for pyscroll
        map_data = pyscroll.data.TiledMapData(tmx_data)

        # create new renderer (camera)
        self.map_layer = pyscroll.BufferedRenderer(map_data,
                                                   display_size,
                                                   clamp_camera=True,
                                                   tall_sprites=1)
        self.map_layer.zoom = 2

        # pyscroll supports layered rendering.  our map has 3 'under' layers
        # layers begin with 0, so the layers are 0, 1, and 2.
        # since we want the sprite to be on top of layer 1, we set the default
        # layer for sprites as 2
        self.group = PyscrollGroup(map_layer=self.map_layer, default_layer=2)

        # put the hero in tile with name matching entrance_name
        player_start = tmx_data.get_object_by_name(entrance_name)
        self._engine.hero.position = [player_start.x, player_start.y]

        # add our hero to the group
        self.group.add(self._engine.hero)

        # setup level geometry with simple pygame rects, loaded from pytmx
        self.walls = list()
        self.npcs = list()
        self.enemies = list()
        self.triggers = list()
        temp_npcs = list()
        # Also a pathfinding grid
        self.pathfinding_grid = pathfinding.weighted_grid(
            tmx_data.width, tmx_data.height)
        for object in tmx_data.objects:
            if object.type == 'wall':
                self.walls.append(
                    pygame.Rect(object.x, object.y, object.width,
                                object.height))
                # Add walls to the pathfinding grid
                grid_x = int(object.x / tmx_data.tilewidth)
                grid_y = int(object.y / tmx_data.tileheight)
                grid_width = int(object.width / tmx_data.tilewidth)
                grid_height = int(object.height / tmx_data.tileheight)
                for y in range(0, grid_height):
                    for x in range(0, grid_width):
                        self.pathfinding_grid.walls.append(
                            (grid_x + x, grid_y + y))
            elif object.type == 'npc' or object.type == 'enemy':
                # Process NPCs and enemies after walls are determined
                temp_npcs.append(object)
            elif object.type == 'trigger':
                self.triggers.append(
                    trigger.trigger(object.x, object.y, object.width,
                                    object.height, object.properties))

        # Process NPCs and enemies
        for object in temp_npcs:
            target_grid_x = int(object.target_x)
            target_grid_y = int(object.target_y)
            target_x = target_grid_x * tmx_data.tilewidth
            target_y = target_grid_y * tmx_data.tileheight
            origin_grid_x = int(object.x / tmx_data.tilewidth)
            origin_grid_y = int(object.y / tmx_data.tileheight)
            # Pathfinding
            came_from, cost_so_far = pathfinding.a_star_search(
                self.pathfinding_grid, (origin_grid_x, origin_grid_y),
                (target_grid_x, target_grid_y))
            path = pathfinding.reconstruct_path(came_from,
                                                (origin_grid_x, origin_grid_y),
                                                (target_grid_x, target_grid_y))
            path = [(t[0] * tmx_data.tilewidth, t[1] * tmx_data.tileheight)
                    for t in path]
            # Load sprite from JSON
            if object.type == 'npc':
                npc = character.npc(object.name, path)
                self.npcs.append(npc)
                self.group.add(npc)
            elif object.type == 'enemy':
                enemy = character.enemy(object.name, path)
                self.enemies.append(enemy)
                self.group.add(enemy)

        # Play background music
        if 'background_music' in tmx_data.properties:
            self._engine.play_music(tmx_data.properties['background_music'])

        # Initialise map
        self.update(0)
Beispiel #10
0
def do_it(start, goal):
    grid = pf.GridFromImage(_imname_low)
    start = int(start[0]/_scale), int(start[1]/_scale)
    goal = int(goal[0]/_scale), int(goal[1]/_scale)
    came_from1, cost_so_far1 = pf.a_star_search(grid, start, goal)
    grid.show_path(start, goal, came_from1, _imname_high, _scale, save=True)
Beispiel #11
0
import parse
import pathfinding
import heuristics
import resource
from pathfinding import a_star_search
from solvable_state import fill_solvable
from solvable import solvable
import sys

if __name__ == '__main__':
    data = parse.get_input()
    puzzle, size, args = data
    HEURISTIC = heuristics.H[args.f]
    y = puzzle.index(0)
    solved = fill_solvable(size)
    if solvable(puzzle, size, solved) != 'solvable':
        print('this puzzle is unsolvable')
        sys.exit(0)
    res = a_star_search(puzzle, solved, size, HEURISTIC)