Untitled
from random import randrange as rr #test map - 'p' is start position shuffle = [ ' ',' ',' ', ] map = [ '000000000000000', '000000010000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', '000000000000000', ] map0 = [ '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000100000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', '000000000000000000000000000000', ] class ProcGen: def __init__(self, map): self.map = map.copy() self.mapx = int(len(map[0]))-1 self.mapy = int(len(map))-1 self.lvl = 1 self.mechanics = { 'step':5, 'posx':0, 'posy':0, 'turn':0, #0lt, 1rt, 2up, 3dn 'lvx':0, 'lvy':0, } self.lvlpos = list() self.count = 0 self.rocount = 0 self.ropos = dict() self.times = 0 self.corridors = list() self.chests = list() self.Position() self.Randomize() def Reset(self): self.times = 0 self.count = 0 self.rocount = 0 self.ropos = dict() self.lvlpos = list() self.corridors = list() self.Position() self.Randomize() def Randomize(self): self.Generate(self.mechanics['posx'],self.mechanics['posy'],self.mechanics['turn'],self.mechanics['step']) for i in range(0,rr(25,65)): self.mechanics['turn'] = rr(0,2) if self.mechanics['turn'] in [0,1]: self.mechanics['step'] = rr(1,4) if self.mechanics['turn'] in [0,1]: self.mechanics['step'] = rr(3,9) self.Generate(self.mechanics['posx'],self.mechanics['posy'],self.mechanics['turn'],self.mechanics['step']) self.Position(1) self.Position(2) self.map[self.mechanics['lvy']] = self.map[self.mechanics['lvy']][:self.mechanics['lvx']] + '9' + self.map[self.mechanics['lvy']][self.mechanics['lvx']+1:] self.Position(3) self.Position(3) def Position(self, sec=0): # column and items in column for i, ii in enumerate(self.map): #row and items in row for j, jj in enumerate(ii): #if item in row is 'p' - starting position if jj == '1' and sec == 0: self.mechanics['posx'] = j self.mechanics['posy'] = i break elif sec == 1: if jj == ' ' and self.times == 0: self.map[i] = self.map[i][:j] + 'p' + self.map[i][j+1:] self.times = 1 elif jj == ' ': self.corridors.append([i,j]) if self.lvl % 5 != 0: self.map[i] = self.map[i][:j] + f'{shuffle[rr(0,len(shuffle))]}' + self.map[i][j+1:] elif self.lvl % 5 in [3,4]: self.map[i] = self.map[i][:j] + f'{shuffle[rr(0,len(shuffle))]}' + self.map[i][j+1:] elif self.lvl % 5 == 0: self.map[i] = self.map[i][:j] + f'{shuffle[rr(0,len(shuffle))]}' + self.map[i][j+1:] if jj == ' ' and sec == 2: self.mechanics['lvx'] = j self.mechanics['lvy'] = i self.lvlpos.append([j,i]) try: if sec == 3: #big room if self.map[i][:j] + '00000' + self.map[i][j+5:] == self.map[i]: if self.map[i+1][:j] + '00000' + self.map[i+1][j+5:] == self.map[i+1]: if self.map[i+2][:j] + '00000' + self.map[i+2][j+5:] == self.map[i+2]: if self.map[i+3][:j] + '00000' + self.map[i+3][j+5:] == self.map[i+3]: if self.map[i+4][:j] + '00000' + self.map[i+4][j+5:] == self.map[i+4]: if self.map[i+2 or i+1 or i+3][j+5] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*3 + self.map[i+1][j+4:] self.map[i+2] = self.map[i+2][:j+1] + '.'*3 + self.map[i+2][j+4:] self.map[i+3] = self.map[i+3][:j+1] + '.'*3 + self.map[i+3][j+4:] if self.map[i+2][j+5] not in '90': self.map[i+2] = self.map[i+2][:j+4] + '(' + self.map[i+2][j+5:] elif self.map[i+1][j+5] not in '90': self.map[i+1] = self.map[i+1][:j+4] + '(' + self.map[i+1][j+5:] elif self.map[i+3][j+5] not in '90': self.map[i+3] = self.map[i+3][:j+4] + '(' + self.map[i+3][j+5:] self.ropos[self.rocount] = i+2,j+2 self.rocount += 1 self.chests.append([i,j]) elif self.map[i+2 or i+1 or i+3][j-1] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*3 + self.map[i+1][j+4:] self.map[i+2] = self.map[i+2][:j+1] + '.'*3 + self.map[i+2][j+4:] self.map[i+3] = self.map[i+3][:j+1] + '.'*3 + self.map[i+3][j+4:] if self.map[i+2][j-1] not in '90': self.map[i+2] = self.map[i+2][:j] + '(' + self.map[i+2][j+1:] elif self.map[i+1][j-1] not in '90': self.map[i+1] = self.map[i+1][:j] + '(' + self.map[i+1][j+1:] elif self.map[i+3][j-1] not in '90': self.map[i+3] = self.map[i+3][:j] + '(' + self.map[i+3][j+1:] self.ropos[self.rocount] = i+2,j+2 self.rocount += 1 self.chests.append([i,j]) elif self.map[i-1][j+2 or j+1 or j+3] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*3 + self.map[i+1][j+4:] self.map[i+2] = self.map[i+2][:j+1] + '.'*3 + self.map[i+2][j+4:] self.map[i+3] = self.map[i+3][:j+1] + '.'*3 + self.map[i+3][j+4:] if self.map[i-1][j+2] not in '90': self.map[i] = self.map[i][:j+2] + '(' + self.map[i][j+3:] elif self.map[i-1][j+1] not in '90': self.map[i] = self.map[i][:j+1] + '(' + self.map[i][j+2:] elif self.map[i-1][j+3] not in '90': self.map[i] = self.map[i][:j+3] + '(' + self.map[i][j+4:] self.ropos[self.rocount] = i+2,j+2 self.rocount += 1 self.chests.append([i,j]) elif self.map[i+5][j+2 or j+1 or j+3] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*3 + self.map[i+1][j+4:] self.map[i+2] = self.map[i+2][:j+1] + '.'*3 + self.map[i+2][j+4:] self.map[i+3] = self.map[i+3][:j+1] + '.'*3 + self.map[i+3][j+4:] if self.map[i+5][j+2] not in '90': self.map[i+4] = self.map[i+4][:j+2] + '(' + self.map[i+4][j+3:] elif self.map[i+5][j+1] not in '90': self.map[i+4] = self.map[i+4][:j+1] + '(' + self.map[i+4][j+2:] elif self.map[i+5][j+3] not in '90': self.map[i+4] = self.map[i+4][:j+3] + '(' + self.map[i+4][j+4:] self.ropos[self.rocount] = i+2,j+2 self.rocount += 1 self.chests.append([i,j]) #small room if self.map[i][:j] + '0000' + self.map[i][j+4:] == self.map[i]: if self.map[i+1][:j] + '0000'+ self.map[i+1][j+4:] == self.map[i+1]: if self.map[i+2][:j] + '0000' + self.map[i+2][j+4:] == self.map[i+2]: if self.map[i+3][:j] + '0000' + self.map[i+3][j+4:] == self.map[i+3]: if self.map[i+1 or i+2][j+4] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*2 + self.map[i+1][j+3:] self.map[i+2] = self.map[i+2][:j+1] + '.'*2 + self.map[i+2][j+3:] if self.map[i+2][j+4] not in '90': self.map[i+2] = self.map[i+2][:j+3] + '(' + self.map[i+2][j+4:] elif self.map[i+1][j+4] not in '90': self.map[i+1] = self.map[i+1][:j+3] + '(' + self.map[i+1][j+4:] self.ropos[self.rocount] = i+1,j+1 self.rocount += 1 elif self.map[i+1 or i+2][j-1] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*2 + self.map[i+1][j+3:] self.map[i+2] = self.map[i+2][:j+1] + '.'*2 + self.map[i+2][j+3:] if self.map[i+1][j-1] not in '90': self.map[i+1] = self.map[i+1][:j] + '(' + self.map[i+1][j+1:] elif self.map[i+2][j-1] not in '90': self.map[i+2] = self.map[i+2][:j] + '(' + self.map[i+2][j+1:] self.ropos[self.rocount] = i+1,j+1 self.rocount += 1 elif self.map[i-1][j+1 or j+2] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*2 + self.map[i+1][j+3:] self.map[i+2] = self.map[i+2][:j+1] + '.'*2 + self.map[i+2][j+3:] if self.map[i-1][j+1] not in '90': self.map[i] = self.map[i][:j+1] + '(' + self.map[i][j+2:] elif self.map[i][j+2] not in '90': self.map[i] = self.map[i][:j+2] + '(' + self.map[i][j+3:] self.ropos[self.rocount] = i+1,j+1 self.rocount += 1 elif self.map[i+4][j+1 or j+2] not in '90': self.map[i+1] = self.map[i+1][:j+1] + '.'*2 + self.map[i+1][j+3:] self.map[i+2] = self.map[i+2][:j+1] + '.'*2 + self.map[i+2][j+3:] if self.map[i+4][j+1] not in '90': self.map[i+3] = self.map[i+3][:j+1] + '(' + self.map[i+3][j+2:] elif self.map[i+4][j+2] not in '90': self.map[i+3] = self.map[i+3][:j+2] + '(' + self.map[i+3][j+3:] self.ropos[self.rocount] = i+1,j+1 self.rocount += 1 except IndexError: pass def Generate(self, j, i, t, s): for it in range(s): if self.count == 0: if t == 0 and j - s >= 1: #left self.map[i] = self.map[i][:j-s+1] + ' '*s + self.map[i][j+1:] self.mechanics['posx'] = j-s+1 self.mechanics['posy'] = i break elif t == 1 and j + s <= self.mapx: self.map[i] = self.map[i][:j] + ' '*s + self.map[i][j+s:] self.mechanics['posx'] = j+s-1 self.mechanics['posy'] = i break if self.count == 1: if t == 0 and i - s >= 1: # up self.map[i-it] = self.map[i-it][:j] + ' ' + self.map[i+-it][j+1:] self.mechanics['posx'] = j self.mechanics['posy'] = i-it elif t == 1 and i + s <= self.mapy: self.map[i+it] = self.map[i+it][:j] + ' ' + self.map[i+it][j+1:] self.mechanics['posx'] = j self.mechanics['posy'] = i+it self.count += 1 if self.count >= 2: self.count = 0 a = ProcGen(map) for i in range(50): for i in a.map: print(i) print() a.map = map.copy() a.Reset()
Leave a Comment