Still a few spots in the pathing code that didn't use numpyt.

master
Zed A. Shaw 3 days ago
parent 11df4c1a4f
commit a4dec0e952
  1. 53
      phase_12.py

@ -208,31 +208,31 @@ class GameEngine:
self.map = Map(ui.width, ui.height - ui.status_height) self.map = Map(ui.width, ui.height - ui.status_height)
self.ui = ui self.ui = ui
ui.set_map(self.map) 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): def add_neighbors(self, neighbors, closed, near_y, near_x):
points = compass(near_x, near_y) points = compass(near_x, near_y)
for x,y in points: for x,y in points:
if self.map.inbounds(x,y) and closed[y][x] == SPACE: if self.map.inbounds(x,y) and closed[y, x] == SPACE:
closed[y][x] = WALL closed[y, x] = WALL
neighbors.append([x,y]) neighbors.append([x,y])
def path_enemies(self, in_grid): def path_enemies(self, in_grid):
height = self.map.height self.paths.fill(PATH_LIMIT)
width = self.map.width closed = self.map.map.copy()
self.paths = [[PATH_LIMIT] * width for x in range(0, height)]
closed = [list(row) for row in self.map.map]
starting_pixels = [] starting_pixels = []
open_pixels = [] open_pixels = []
counter = 0 counter = 0
while counter < height * width: while counter < self.height * self.width:
x = counter % width x = counter % self.width
y = counter // width y = counter // self.width
if in_grid[y][x] == 0: if in_grid[y, x] == 0:
self.paths[y][x] = 0 self.paths[y, x] = 0
closed[y][x] = WALL closed[y, x] = WALL
starting_pixels.append([x,y]) starting_pixels.append([x,y])
counter += 1 counter += 1
@ -243,43 +243,28 @@ class GameEngine:
while counter < PATH_LIMIT and open_pixels: while counter < PATH_LIMIT and open_pixels:
next_open = [] next_open = []
for x,y in open_pixels: for x,y in open_pixels:
self.paths[y][x] = counter self.paths[y, x] = counter
self.add_neighbors(next_open, closed, y, x) self.add_neighbors(next_open, closed, y, x)
open_pixels = next_open open_pixels = next_open
counter += 1 counter += 1
for x, y in open_pixels: for x, y in open_pixels:
self.paths[y][x] = counter 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
def move_enemies(self): def move_enemies(self):
in_grid = [[1] * self.map.width for x in range(0, self.map.height)] in_grid = np.full((self.map.height, self.map.width), 1, dtype=int)
in_grid[self.player.y][self.player.x] = 0 in_grid[self.player.y, self.player.x] = 0
self.path_enemies(in_grid) self.path_enemies(in_grid)
# for every enemy (actors[0] is player) # for every enemy (actors[0] is player)
for enemy in self.actors[1:]: for enemy in self.actors[1:]:
nearby = compass(enemy.x, enemy.y) 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 if our_path > enemy.hearing_distance: continue
for x, y in nearby: 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.x = x
enemy.y = y enemy.y = y
break break

Loading…
Cancel
Save