From 7fa0403a2887309d42a5ccbaef678f379ccd1c49 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 24 Jul 2025 00:48:51 -0400 Subject: [PATCH] Pulling the pathing code out into a separate module is better. --- phase_13/ecs.py | 42 ++---------------------------------------- phase_13/pathing.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 phase_13/pathing.py diff --git a/phase_13/ecs.py b/phase_13/ecs.py index c1c8386..39d56d4 100644 --- a/phase_13/ecs.py +++ b/phase_13/ecs.py @@ -1,5 +1,6 @@ import utils import numpy as np +import pathing class Player: def __init__(self, x, y): @@ -48,50 +49,11 @@ class Systems: self.width = self.map.width self.paths = np.full((self.height, self.width), utils.PATH_LIMIT, dtype=int) - def add_neighbors(self, neighbors, closed, near_y, near_x): - points = utils.compass(near_x, near_y) - - for x,y in points: - if self.map.inbounds(x,y) and closed[y, x] == utils.SPACE: - closed[y, x] = utils.WALL - neighbors.append([x,y]) - - def path_enemies(self, in_grid): - self.paths.fill(utils.PATH_LIMIT) - closed = self.map.map.copy() - starting_pixels = [] - open_pixels = [] - - counter = 0 - 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] = utils.WALL - starting_pixels.append([x,y]) - counter += 1 - - for x, y in starting_pixels: - self.add_neighbors(open_pixels, closed, y, x) - - counter = 1 - while counter < utils.PATH_LIMIT and open_pixels: - next_open = [] - for x,y in open_pixels: - 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 move_enemies(self): 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) + pathing.calculate_paths(self.map, self.paths, in_grid) # for every enemy (actors[0] is player) for enemy in self.actors[1:]: nearby = utils.compass(enemy.x, enemy.y) diff --git a/phase_13/pathing.py b/phase_13/pathing.py new file mode 100644 index 0000000..9b15181 --- /dev/null +++ b/phase_13/pathing.py @@ -0,0 +1,41 @@ +import utils + +def add_neighbors(the_map, neighbors, closed, near_y, near_x): + points = utils.compass(near_x, near_y) + + for x,y in points: + if the_map.inbounds(x,y) and closed[y, x] == utils.SPACE: + closed[y, x] = utils.WALL + neighbors.append([x,y]) + +def calculate_paths(the_map, paths, in_grid): + height, width = paths.shape + paths.fill(utils.PATH_LIMIT) + closed = the_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: + paths[y, x] = 0 + closed[y, x] = utils.WALL + starting_pixels.append([x,y]) + counter += 1 + + for x, y in starting_pixels: + add_neighbors(the_map, open_pixels, closed, y, x) + + counter = 1 + while counter < utils.PATH_LIMIT and open_pixels: + next_open = [] + for x,y in open_pixels: + paths[y, x] = counter + add_neighbors(the_map, next_open, closed, y, x) + open_pixels = next_open + counter += 1 + + for x, y in open_pixels: + paths[y, x] = counter