From a4dec0e9523f4cab62e023435b07ae40b4742a10 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 24 Jul 2025 00:33:09 -0400 Subject: [PATCH] Still a few spots in the pathing code that didn't use numpyt. --- phase_12.py | 53 +++++++++++++++++++---------------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/phase_12.py b/phase_12.py index ff494a7..04314dc 100644 --- a/phase_12.py +++ b/phase_12.py @@ -208,31 +208,31 @@ class GameEngine: self.map = Map(ui.width, ui.height - ui.status_height) self.ui = ui ui.set_map(self.map) + self.height = self.map.height + self.width = self.map.width + self.paths = np.full((self.height, self.width), PATH_LIMIT, dtype=int) def add_neighbors(self, neighbors, closed, near_y, near_x): points = compass(near_x, near_y) for x,y in points: - if self.map.inbounds(x,y) and closed[y][x] == SPACE: - closed[y][x] = WALL + if self.map.inbounds(x,y) and closed[y, x] == SPACE: + closed[y, x] = WALL neighbors.append([x,y]) def path_enemies(self, in_grid): - height = self.map.height - width = self.map.width - - self.paths = [[PATH_LIMIT] * width for x in range(0, height)] - closed = [list(row) for row in self.map.map] + self.paths.fill(PATH_LIMIT) + closed = self.map.map.copy() starting_pixels = [] open_pixels = [] counter = 0 - while counter < height * width: - x = counter % width - y = counter // width - if in_grid[y][x] == 0: - self.paths[y][x] = 0 - closed[y][x] = WALL + while counter < self.height * self.width: + x = counter % self.width + y = counter // self.width + if in_grid[y, x] == 0: + self.paths[y, x] = 0 + closed[y, x] = WALL starting_pixels.append([x,y]) counter += 1 @@ -243,43 +243,28 @@ class GameEngine: while counter < PATH_LIMIT and open_pixels: next_open = [] for x,y in open_pixels: - self.paths[y][x] = counter + self.paths[y, x] = counter self.add_neighbors(next_open, closed, y, x) open_pixels = next_open counter += 1 for x, y in open_pixels: - self.paths[y][x] = counter - - def debug_paths(self): - debug_map = [] - - for y in range(0, self.map.height): - row = list(self.map.map[y]) - for x in range(0, self.map.width): - level = self.paths[y][x] - if row[x] != WALL: - if level < 10: - row[x] = str(level) - else: - row[x] = '*' - debug_map.append("".join(row)) - return debug_map + self.paths[y, x] = counter def move_enemies(self): - in_grid = [[1] * self.map.width for x in range(0, self.map.height)] - in_grid[self.player.y][self.player.x] = 0 + in_grid = np.full((self.map.height, self.map.width), 1, dtype=int) + in_grid[self.player.y, self.player.x] = 0 self.path_enemies(in_grid) # for every enemy (actors[0] is player) for enemy in self.actors[1:]: nearby = compass(enemy.x, enemy.y) - our_path = self.paths[enemy.y][enemy.x] + our_path = self.paths[enemy.y, enemy.x] if our_path > enemy.hearing_distance: continue for x, y in nearby: - if self.paths[y][x] <= our_path and not self.actor_collision(enemy, x, y): + if self.paths[y, x] <= our_path and not self.actor_collision(enemy, x, y): enemy.x = x enemy.y = y break