Optimization for the loop placing algorithm

This commit is contained in:
Charles Peyrat 2021-01-21 03:26:54 +01:00
parent 38878687c4
commit 5981927dac
1 changed files with 10 additions and 9 deletions

View File

@ -21,37 +21,38 @@ DEFAULT_PARAMS = {
"large_circular_room": .10, "large_circular_room": .10,
"circular_holes": .5, "circular_holes": .5,
"loop_tries": 40, "loop_tries": 40,
"loop_max": 5, "loop_max": 8,
"loop_threshold": 15, "loop_threshold": 15,
"spawn_per_region": [1, 2], "spawn_per_region": [1, 2],
"room_chances" : { "room_chances" : {
"circular" : 3, "circular" : 1,
"chunks" : 2, "chunks" : 1,
"rectangle" : 1, "rectangle" : 1,
}, },
} }
def dist(level, y1, x1, y2, x2): def test_dist(level, y1, x1, y2, x2, threshold):
""" """
Compute the minimum walking distance between points (y1, x1) and (y2, x2) on a Tile grid Returns whether the minimum walking distance between points (y1, x1) and
(y2, x2) on the Tile grid level is greater than threshold
""" """
# simple breadth first search # simple breadth first search
copy = [[t for t in row] for row in level] copy = [[t for t in row] for row in level]
dist = -1 dist = -1
queue, next_queue = [[y1, x1]], [0] queue, next_queue = [[y1, x1]], [0]
while next_queue: while next_queue and dist < threshold:
next_queue = [] next_queue = []
dist += 1 dist += 1
while queue: while queue:
y, x = queue.pop() y, x = queue.pop()
copy[y][x] = Tile.EMPTY copy[y][x] = Tile.EMPTY
if y == y2 and x == x2: if y == y2 and x == x2:
return dist return False
for y, x in Map.neighbourhood(copy, y, x): for y, x in Map.neighbourhood(copy, y, x):
if copy[y][x].can_walk(): if copy[y][x].can_walk():
next_queue.append([y, x]) next_queue.append([y, x])
queue = next_queue queue = next_queue
return -1 return True
class Generator: class Generator:
@ -148,7 +149,7 @@ class Generator:
return True return True
# if adding the path would make the two tiles significantly closer # if adding the path would make the two tiles significantly closer
# and its sides don't touch already placed terrain, build it # and its sides don't touch already placed terrain, build it
if dist(level, y1, x1, y2, x2) < 20 and verify_sides(): if test_dist(level, y1, x1, y2, x2, 20) and verify_sides():
y, x = y1 + dy, x1 + dx y, x = y1 + dy, x1 + dx
while level[y][x] == Tile.EMPTY: while level[y][x] == Tile.EMPTY:
level[y][x] = Tile.FLOOR level[y][x] = Tile.FLOOR