Browse Source

Fix wall bug in dungeon generator

master
Josh Gordon 4 years ago
parent
commit
0cc847de15
  1. 53
      app/dungeon.py

53
app/dungeon.py

@ -60,6 +60,9 @@ class Cell(object):
def getFlow(self):
return self.flow
def getSpaces(self):
return self.spaces
class Grid(object):
def __init__(self,w,h):
@ -154,32 +157,35 @@ def drawFlow(im,grid):
for (y1,x1,y0,x0) in cell.getFlow():
cv2.arrowedLine(im,(x1*SCALE_FACTOR + SCALE_FACTOR//2,y1*SCALE_FACTOR + SCALE_FACTOR//2),(x0*SCALE_FACTOR + SCALE_FACTOR//2,y0*SCALE_FACTOR + SCALE_FACTOR//2),100)
def drawWalls(im):
def drawWalls(im,grid):
newim = im.copy()
w,h,_ = im.shape
for x in range(w):
for y in range(h):
if np.array_equal(im[x,y,:],np.array([0,0,0])):
pass
elif x == 0 or y == 0 or x == w-1 or y == h-1:
newim[x,y,:] = WALL_COLOR
else:
color = im[x,y,:]
floor = True
for i in [-1,0,1]:
for j in [-1,0,1]:
if not np.array_equal(im[x+i,y+j,:],color):
if not np.array_equal(im[x+i,y+j,:],WALL_COLOR):
floor = False
#If this pixel has a neighbor with a different color,
#it is a floor only if that different color is the wall color
#THIS NEEDS TO BE DEBUGGED, START HERE TOMORROW
if not floor:
newim[x,y,:] = WALL_COLOR
for cell in grid.getCells():
for (cx,cy) in cell.getSpaces():
for dx in range(WALL_SCALE):
for dy in range(WALL_SCALE):
x = cx*WALL_SCALE+dx
y = cy*WALL_SCALE+dy
print(x,y)
if np.array_equal(im[x,y,:],np.array([0,0,0])):
pass
elif x == 0 or y == 0 or x == w-1 or y == h-1:
newim[x,y,:] = WALL_COLOR
else:
color = im[x,y,:]
floor = True
for i in [-1,0,1]:
for j in [-1,0,1]: #this works horizontally
if not np.array_equal(im[x+i,y+j,:],color): #if a neighbor has a different color
if not np.array_equal(newim[x+i,y+j,:],WALL_COLOR): #and that color is not a wall
floor = False #become a wall
else:
print("Debug")
if not floor:
newim[x,y,:] = WALL_COLOR
return newim
#driver code
g = Grid(GRID_SIZE,GRID_SIZE)
g.addCell()
@ -187,16 +193,17 @@ g.addCell()
g.addCell()
g.addCell()
g.addCell()
g.addCell()
print(g)
im = g.exportImage()
#print(im)
im = drawWalls(im)
im = drawWalls(im,g)
im = scaleMap(im)
drawFlow(im,g)
cv2.imshow("Here",im)
cv2.waitKey(0)
cv2.imwrite("dungeon.png",im)
cv2.imwrite("generated_dungeon.png",im)
Loading…
Cancel
Save