Browse Source

Python 2-ized the numpy in dungeon gen

master
Josh Gordon 4 years ago
parent
commit
6e5cb29d89
  1. 59
      app/dungeon.py
  2. 2
      app/routes.py

59
app/dungeon.py

@ -4,6 +4,7 @@ import numpy as np
import cv2
import base64
import sys
#algorithm: http://blankhead.fi/blog/index.php/2019/06/01/cell-flow-dungeon-layout-generation/
@ -146,7 +147,9 @@ class Grid(object):
for y in range(self.h):
if not self.spaces[x,y] == 0:
#print(self.cells[self.spaces[x,y]-1].getColor())
imBase[x,y,:] = self.cells[self.spaces[x,y]-1].getColor()/255
imBase[x,y,:] = self.cells[self.spaces[x,y]-1].getColor()
if sys.version_info.major >= 3:
imBase /= 255
imBase = np.kron(imBase,np.ones((WALL_SCALE,WALL_SCALE,1)))
return imBase
@ -239,33 +242,40 @@ def drawWalls(im,grid):
def carveDoors(im,grid):
newim = im.copy()
for cell in grid.getCells():
color = cell.getColor()
if sys.version_info.major >= 3:
print(color)
color = color / 255
for (cy1,cx1,cy0,cx0) in cell.getFlow():
p1 = (cx1*WALL_SCALE + WALL_SCALE//2,cy1*WALL_SCALE + WALL_SCALE//2)
p0 = (cx0*WALL_SCALE + WALL_SCALE//2,cy0*WALL_SCALE + WALL_SCALE//2)
cv2.line(newim,p1,p0,cell.getColor()/255,1)
cv2.line(newim,p1,p0,color,1)
return newim
def canonicalize(im):
w,h,_ = im.shape
gray = cv2.cvtColor(im.astype(np.float32),cv2.COLOR_BGR2GRAY)
canonBW = cv2.inRange(gray,1/255,254/255)
canonBW = cv2.inRange(gray,1/255,254/255) if sys.version_info.major >= 3 else cv2.inRange(gray,1,254)
canon = cv2.cvtColor(canonBW,cv2.COLOR_GRAY2BGR)
return canon,canonBW
def drawGrid(im,mask): #draws grid on canonicalized image
w,h,_ = im.shape
newim = im.copy()
c = GRID_COLOR
if sys.version_info.major >= 3:
c = c / 255
for x in range(w//MAP_SCALE):
cv2.line(newim,
(x*MAP_SCALE,0),
(x*MAP_SCALE,h-1),
GRID_COLOR/255,
c,
1) #thicc
for y in range(h//MAP_SCALE):
cv2.line(newim,
(0,y*MAP_SCALE),
(w-1,y*MAP_SCALE),
GRID_COLOR/255,
c,
1)
gridded = cv2.bitwise_and(newim,newim,mask=mask)
return gridded
@ -275,17 +285,21 @@ def genGridDungeonB64(gSizeX,gSizeY,mScale,imScale,numCells=None):
global WALL_SCALE
MAP_SCALE = mScale
WALL_SCALE = imScale//MAP_SCALE
g = Grid(gSizeX,gSizeY)
if numCells == None:
numCells = (gSizeX+gSizeY)//10
for _ in range(numCells):
g.addCell()
im = g.exportImage()
im = drawWalls(im,g)
im = carveDoors(im,g)
im = scaleMap(im)
im,mask = canonicalize(im)
im = drawGrid(im,mask)
try:
g = Grid(gSizeX,gSizeY)
if numCells == None:
numCells = (gSizeX+gSizeY)//10
for _ in range(numCells):
g.addCell()
im = g.exportImage()
im = drawWalls(im,g)
im = carveDoors(im,g)
im = scaleMap(im)
im,mask = canonicalize(im)
im = drawGrid(im,mask)
except:
print("Rip?")
return "__ERROR_"
#cv2.imshow("here",im)
#cv2.waitKey(0)
#base64 encode as jpg
@ -296,22 +310,19 @@ def genGridDungeonB64(gSizeX,gSizeY,mScale,imScale,numCells=None):
#s = genGridDungeonB64(10,10,8,24)
#print(s)
"""
#"""
#driver code
g = Grid(GRID_SIZE,GRID_SIZE)
g.addCell()
g.addCell()
g.addCell()
g.addCell()
g.addCell()
print(g)
g.addCell()
g.addCell()
print(g)
im = g.exportImage()
#print(im)
cv2.imshow("grid export",im)
im = drawWalls(im,g)
cv2.imshow("draw walls",im)
im = carveDoors(im,g)
im = scaleMap(im)
#drawFlow(im,g)
@ -328,4 +339,4 @@ print(d)
res = d.exportImage()
cv2.imshow("res",res)
cv2.waitKey(0)
"""
#"""

2
app/routes.py

@ -177,6 +177,8 @@ def genDungeon():
s = int(request.form['s'])
c = int(request.form['c'])
enc = str(genGridDungeonB64(x,y,m,s,c))[2:-1]
#if sys.version_info.major >= 3: #xxx oof?
# enc = enc[2:-1]
return render_template('image.html',data=enc)
if __name__ == "__main__":

Loading…
Cancel
Save