Beispiel #1
0
def test_04_on_power_pill():
    sample_feed = {
        'player_pos': (4, 29),
        'ghost_pos': [(1, 29), (12, 29)],
        'victory': False,
        'power_pill_active': True
    }

    distances_feed = {'barrier_to_player': 5, 'barrier_to_ghost': [5, 5]}

    metric = C3_CloseCalls()

    metric.apply(GameplaySample(t=0, player_dead=False, **sample_feed),
                 distances=DijkstraDistances(player_to_ghost=[1, 10],
                                             **distances_feed))

    metric.apply(GameplaySample(t=1, player_dead=False, **sample_feed),
                 distances=DijkstraDistances(player_to_ghost=[2, 10],
                                             **distances_feed))

    metric.apply(GameplaySample(t=5, player_dead=False, **sample_feed),
                 distances=DijkstraDistances(player_to_ghost=[0, 10],
                                             **distances_feed))

    assert metric.value == 0
Beispiel #2
0
def test_01_chase_ghosts():
    sample_feed = {
        'player_pos': None,
        'ghost_pos': None,
        'player_dead': False,
        'victory': False
    }

    dist_feed = {
        'barrier_to_player': [20],
        'barrier_to_ghost': [19]
    }

    metric = A6_ChaseGhostsOrCollectDots()

    metric.apply(GameplaySample(t=0, power_pill_active=True, **sample_feed),
        DijkstraDistances(player_to_ghost=[10], **dist_feed))

    metric.apply(GameplaySample(t=1, power_pill_active=True, **sample_feed),
        DijkstraDistances(player_to_ghost=[9], **dist_feed))

    metric.apply(GameplaySample(t=2, power_pill_active=True, **sample_feed),
        DijkstraDistances(player_to_ghost=[9], **dist_feed))

    metric.apply(GameplaySample(t=3, power_pill_active=True, **sample_feed),
        DijkstraDistances(player_to_ghost=[8], **dist_feed))

    assert metric.value == 2
Beispiel #3
0
def test_02_dijkstra_distances():
    maze = Maze.from_file('data/maze/maze0.txt')
    maze.dijkstra_precompute()

    t = 0

    x, y = player_pos = (13, 23)
    assert maze.maze[y, x] == PLAYER

    ghost_pos = [(6, 20), (21, 9), (1, 2)]
    for i in range(3):
        x, y = ghost_pos[i]
        assert maze.maze[y, x] == PILL

    power_pill_active = False
    player_dead = False
    victory = False

    sample = GameplaySample(t, player_pos, ghost_pos, power_pill_active,
                            player_dead, victory)

    distances = DijkstraDistances.from_maze_and_sample(maze, sample)

    assert distances.player_to_ghost[0] == 10
    assert distances.player_to_ghost[1] == 22
    assert distances.player_to_ghost[2] == 33

    assert distances.barrier_to_player == 21

    # print(distances)
    # x, y = ghost_pos[0]
    # assert distances[y, x] == 10
    assert distances.barrier_to_ghost[0] == 17
    assert distances.barrier_to_ghost[1] == 16
    assert distances.barrier_to_ghost[2] == 22
Beispiel #4
0
def test_03_no_pill():
    metric = A4_HuntEvenAfterPillFinished()

    samples = []

    t = 0
    player_pos = (13, 23)
    ghost_pos = [(14, 23)]
    power_pill_active = False
    player_dead = False
    victory = False

    samples.append(
        GameplaySample(t, player_pos, ghost_pos, power_pill_active,
                       player_dead, victory))

    t = 1
    power_pill_active = False

    samples.append(
        GameplaySample(t, player_pos, ghost_pos, power_pill_active,
                       player_dead, victory))

    for i in range(1, 10):
        t = 1 + i / 10
        samples.append(
            GameplaySample(t, player_pos, ghost_pos, power_pill_active,
                           player_dead, victory))

    player_to_ghost = [1]
    barrier_to_player = [20]
    barrier_to_ghost = [19]

    distances = DijkstraDistances(player_to_ghost, barrier_to_player,
                                  barrier_to_ghost)

    for sample in samples:
        metric.apply(sample, distances)

    assert metric.value == 0
Beispiel #5
0
def process_log(fnam, log, args, callback=None):
    player = None
    n_ghosts = 3
    ghosts = [None] * n_ghosts
    ghost_dead = [False] * n_ghosts
    power_pill_active = False
    player_dead = False
    victory = False
    n_levels = 4
    maze = None
    accessible = None
    # precompute = None

    t_0 = None
    for i in range(len(log)):
        entry = log[i]
        if isinstance(entry, StartingGame):
            level_nr = entry.level % n_levels
            maze = Maze.from_file(
                os.path.join(args.data_directory, 'maze',
                             'maze%d.txt' % level_nr))
            accessible = maze.accessible_mask()
            maze.dijkstra_precompute()
            # precompute = DijkstraPrecompute(maze)
            # precompute.run()
        elif isinstance(entry, PlacedGhost):
            ghosts[entry.ghost_index] = Entity(entry.x, entry.y, entry.delay)
        elif isinstance(entry, PlacedPlayer):
            player = Entity(entry.x, entry.y, entry.delay)
        elif isinstance(log[i], GameStarted):
            t_0 = log[i].game_time
            break

    if t_0 is None:
        return None

    i += 1
    t = t_0
    player_samples = []
    ghost_samples = []
    t_samples = []

    while i < len(log) and not player_dead and not victory:
        player_pos = player.sample_pos(t)
        ghost_pos = list(map(lambda a: a.sample_pos(t), ghosts))

        for k, g_pos in enumerate(ghost_pos):
            if distance(g_pos, player_pos) < .5:
                if power_pill_active and \
                    not ghost_dead[k]:
                    ghost_dead[k] = True
                    ghost_pos[k] += ('killed', )
                elif not power_pill_active and \
                    not player_dead:
                    player_pos += ('killed', )
                    player_dead = True

        if player_dead:
            player_pos += ('dead', )

        while i < len(log) and t >= log[
                i].game_time and not player_dead and not victory:
            entry = log[i]
            if isinstance(entry, GhostMoves):
                ghosts[entry.ghost_index].move(t, entry.x, entry.y, entry.dx,
                                               entry.dy)
                ghost_dead[k] = False
            elif isinstance(entry, PlayerMoves):
                player.move(t, entry.x, entry.y, entry.dx, entry.dy)
            elif isinstance(entry, PlayerAtePill):
                player_pos += ('pill', )
            elif isinstance(entry, PlayerAtePowerPill):
                power_pill_active = True
                player_pos += ('power_pill', )
            elif isinstance(entry, PowerPillEnded):
                power_pill_active = False
                player_pos += ('power_pill_end', )
            elif isinstance(entry, NextLevel):
                victory = True
                player_pos += ('victory', )
            elif isinstance(entry, GameOver):
                pass
            i += 1

        sample = GameplaySample(t, player_pos, ghost_pos, power_pill_active,
                                player_dead, victory)
        distances = DijkstraDistances.from_maze_and_sample(maze, sample)
        if callback is not None:
            callback(sample=sample,
                     distances=distances,
                     maze=maze,
                     accessible=accessible)

        player_samples.append(player_pos)
        ghost_samples.append(ghost_pos)
        t_samples.append(t)

        t += 0.01