Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
2.4 KiB

import numpy as np
import cv2
from random import randint, choice, shuffle
import sys
import struct
CAVE_W = 1080//4
CAVE_H = 1920//4
DIG_SPEED = 400
MINER_SPAWN = 10
STOP_LOOP = CAVE_W * CAVE_H
OPEN = np.array([255,255,255])
CLOSE = np.array([0,0,0])
def caveIsFinished(cave):
return np.sum(cave)/255 > CAVE_W*CAVE_H/4
def randomMiner(cave):
x = randint(0,CAVE_W-1)
y = randint(0,CAVE_H-1)
return [(x,y)]
def genCave():
cave = np.zeros((CAVE_W,CAVE_H,3))
miners = [(CAVE_W//2,CAVE_H//2)] #maybe do random?
progress = 0
while progress < CAVE_W*CAVE_H//4:
print("Dug {} spaces".format(progress))
for i in range(DIG_SPEED):
newMiners = []
if len(miners) == 0:
miners = randomMiner(cave)
x,y = miners.pop(0)
neighbors = []
for i in [-1,0,1]:
for j in [-1,0,1]:
dx = x+i
dy = y+j
if (i == 0 and j == 0) or dx < 0 or dx >= CAVE_W or dy < 0 or dy >= CAVE_H:
pass
elif np.array_equal(cave[dx,dy,:],CLOSE):
neighbors.append((dx,dy))
#print("Neighbors: " + str(neighbors))
if len(neighbors) > 0:
shuffle(neighbors)
nx,ny = neighbors.pop(0)
newMiners.append((nx,ny))
cave[nx,ny,:] = OPEN
progress += 1
if randint(1,100) < MINER_SPAWN and len(neighbors) > 0:
newMiners.append(neighbors.pop(0))
miners = newMiners
#clean up with morph
kernel = np.ones((3,3),np.uint8)
upscale = np.kron(cave,np.ones((4,4,1)))
dilation = cv2.dilate(upscale,kernel,iterations=2)
return dilation
def writeDab(fname,im):
with open(fname,'wb+') as f:
w,h,c = im.shape
f.write(struct.pack('i',w))
f.write(struct.pack('i',h))
for i in range(w):
for j in range(h):
if(i % 10 == 0):
print("Writing data...")
if(np.sum(im[i,j,:]) == 0):
d = 0
elif(np.sum(im[i,j,:]) == 255*3):
d = 1
db = struct.pack('i',d)
f.write(db)
def driver():
c = genCave()
#writeDab("Cave.dab",c)
cv2.imwrite("Cave.png",c)
driver()