Skip to content

Commit

Permalink
fix: Avoid infinitly long map generation for generals.io style
Browse files Browse the repository at this point in the history
  • Loading branch information
strakam committed Jan 18, 2025
1 parent 6e35fd1 commit 5a54c9e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions generals/core/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,20 @@ def create_distance_mask(distances, max_distance):
g1 = (self.rng.integers(grid_height), self.rng.integers(grid_width))
distances_from_g1 = bfs_distance(g1, map)

# pick a tile with distance from g1 >= 15
while True:
g2 = (self.rng.integers(grid_height), self.rng.integers(grid_width))
if distances_from_g1[g2] >= min_generals_distance and distances_from_g1[g2] != float("inf"):
max_attempts = 20
g2 = None
for _ in range(max_attempts):
candidate_g2 = (self.rng.integers(grid_height), self.rng.integers(grid_width))
if distances_from_g1[candidate_g2] >= min_generals_distance and distances_from_g1[candidate_g2] != float(
"inf"
):
g2 = candidate_g2
break

if g2 is None:
# If we couldn't place g2 after max attempts, start over with a new grid
return self.generate_generalsio_grid()

general_positions = [g1, g2]

distances_from_g2 = bfs_distance(g2, map)
Expand All @@ -197,7 +205,6 @@ def create_distance_mask(distances, max_distance):
# Randomly select one position from the valid positions
idx = self.rng.integers(0, len(valid_positions[0]))
city_pos = (valid_positions[0][idx], valid_positions[1][idx])

# Generate city value (0 - 9 or 'x')
city_cost = self.rng.choice([str(i) for i in range(10)] + ["x"])
map[city_pos] = city_cost
Expand Down

0 comments on commit 5a54c9e

Please sign in to comment.