Pastebin PRO Accounts SPRING SPECIAL! For a limited time only get 50% discount on a LIFETIME PRO account! Offer Ends April 8th!
SHARE
TWEET
Python Level Generator
a guest
Oct 21st, 2015
114
Never
- from random import randint
- import time
- class Zone():
- def __init__(self, x, y):
- self.rooms = [(x,y)]
- self.connections = {}
- self.id = time.clock()
- def connect(self, coordinates, x, y, level):
- target = level[coordinates[0]][coordinates[1]]
- resultant = Zone(x,y)
- resultant.connections[(x,y)] = [coordinates]
- # print("Connections:",self.connections)
- for k in self.connections:
- v = self.connections[k]
- if not resultant.connections.get(k, False):
- resultant.connections[k] = v
- else:
- resultant.connections[k]+=v
- for k in target.connections:
- v = target.connections[k]
- if not resultant.connections.get(k, False):
- resultant.connections[k] =v
- else:
- resultant.connections[k]+=v
- resultant.rooms = resultant.rooms+self.rooms+target.rooms
- # print(resultant.connections)
- for c in resultant.rooms:
- level[c[0]][c[1]] = resultant
- return level
- def start(x, y, s):
- level = []
- for a in range(0,x):
- level.append([])
- for b in range(0,y):
- level[a].append(Zone(a,b))
- unconnected = True
- while unconnected:
- unconnected = False
- for a in range(0,x):
- for b in range(0,y):
- # print(level[a][b].connections)
- adjacent = adjacent_zones(a,b,level)
- if not adjacent:
- unconnected = unconnected or False
- continue
- else:
- unconnected = True
- target = randint(0, len(adjacent)-1)
- level = level[a][b].connect(adjacent[target], a,b, level)
- #So we got the level set up, now we print to a file
- grand_zone = level[0][0]
- for a in level:
- for b in a:
- if b.id != grand_zone.id:
- print("Fail")
- raise Exception
- # graphic = [["0"]*(5*y)]*(5*x)
- graphic = []
- for i in range(0,x*s):
- graphic.append([])
- for j in range(0,y*s):
- graphic[i].append("0")
- for a in range(0,x*s):
- if(a%s)==0:
- graphic[a] = ["#"]*(s*y)
- for b in range(0,y):
- graphic[a][b*s] = "#"
- graphic[a][y*s-1] = "#"
- graphic[x*s-1] = ["#"]*(s*y)
- for k in grand_zone.connections.keys():
- for i in grand_zone.connections[k]:
- x = k[0]-i[0]
- y = k[1]-i[1]
- x*=s
- y*=s
- if x<0:
- x*=-1
- elif x==0:
- x= randint(2,s-2)
- if y<0:
- y*=-1
- elif y == 0:
- y = randint(2,s-2)
- x+= min(k[0], i[0])*s
- y+= min(k[1], i[1])*s
- if x>=len(graphic):
- x-=s
- if y>=len(graphic[0]):
- y-=s
- graphic[x][y] = "D"
- print("::::::::::::::::::::::::::::::::::::::::::::::::")
- for i in graphic:
- print(i)
- #Now we technically have the level, but it's fucking boring, below this we fix that
- structure = []
- for a in range(0, len(level)):
- structure.append([])
- for b in range(0,len(level[0])):
- structure[a].append(False)
- connect_counter = 0
- for a in range(0, len(level)):
- for b in range(0,len(level[0])):
- if not structure[a][b]:
- #0-5 we leave the room alone
- #6-8 we remove a wall
- #9-10 we turn it into a hallway
- room_mode = randint(0, 11)
- #range is inclusive on the bottom, not on top
- if room_mode in range(6, 8):
- print("connecting")
- structure[a][b] = "connect-{}".format(connect_counter)
- connections = level[0][0].connections.get((a,b), [])
- if len(connections) !=0:
- x = randint(1, len(connections))
- for i in range(0,x):
- structure[connections[i][0]][connections[i][1]] = "connect-{}".format(connect_counter)
- connect_counter +=1
- elif room_mode in range(8, 12):
- door_count = len(grand_zone.connections.get((a,b), []))
- if door_count > 1:
- structure[a][b] = "hallway"
- else:
- for k in grand_zone.connections.keys():
- if (a,b) in grand_zone.connections[k]:
- door_count +=1
- print(grand_zone.connections[k])
- if door_count > 1:
- structure[a][b] = "hallway"
- break
- for i in range(0, len(structure)):
- for j in range(0, len(structure[0])):
- if not structure[i][j]:
- continue
- elif structure[i][j] == "hallway":
- graphic = create_hallway(i,j,s,graphic)
- elif structure[i][j].split("-")[0] == "connect":
- #pass
- res = connect(i,j,s,graphic,structure)
- graphic = res[0]
- structure = res[1]
- print(":FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:")
- for i in graphic:
- print(i)
- def connect(x,y,s,graphic,structure):
- group = structure[x][y].split("-")[1]
- for g in [(1,0), (0,1), (-1,0), (0,-1)]:
- try:
- nx = x+g[0]
- ny = y+g[1]
- try:
- structure[nx][ny]
- except:
- print("excepted in test")
- continue
- if structure[nx][ny].split("-")[1] == group:
- dx = g[0]
- dy = g[1]
- if dx == 0: #the broken wall is on the left/right
- dx = min(x,nx)
- dy = min(y,ny)
- dy+=1
- for i in range(dx*s+1, dx*s+s):
- graphic[i][dy*s] = "0"
- else:
- dx = min(x,nx)
- dy = min(y,ny)
- dx+=1
- for i in range(dy*s+1, dy*s+s):
- graphic[dx*s][i] = "0"
- except (AttributeError, IndexError) as e:
- print(e)
- pass
- print("connecting_______________")
- for i in graphic:
- print(i)
- return (graphic, structure)
- def create_hallway(x,y,s,graphic):
- rx = x*s #r = range : how big our rooms are
- ry = y*s
- doors = []
- for i in range(rx, rx+s+1):
- string = ""
- for j in range(ry, ry+s+1):
- try:
- if graphic[i][j] == "D":
- doors.append((i,j))
- except:
- pass
- if len(doors) < 2:
- print("Not enough doors ({}, {})".format(x,y))
- return graphic
- origional_doors = doors
- go = True
- while go:
- go = False
- counter = 0
- for d in doors:
- counter +=1
- go = go or d
- if d:
- found = False
- choices = [(1,0), (0,1), (-1,0), (0,-1)]
- while not found:
- if len(choices) >0:
- rand = randint(0, len(choices)-1)
- direction = choices[rand]
- del(choices[rand])
- d = (d[0] + direction[0], d[1]+direction[1])
- try:
- if d[0] not in range(rx, rx+s+1) or d[1] not in range(ry, ry+s+1):
- d = (d[0] - direction[0], d[1]-direction[1])
- elif graphic[d[0]][d[1]] == "0":
- graphic[d[0]][d[1]] = "{}".format(counter)
- for i in [(1,0), (0,1), (-1,0), (0,-1)]:
- try:
- if graphic[d[0]+i[0]][d[1]+i[1]] != "{}".format(counter) and graphic[d[0]+i[0]][d[1]+i[1]] not in "D#0":
- doors[int(graphic[d[0]+i[0]][d[1]+i[1]])-1] = False
- d = False
- doors[counter-1] = False
- # return graphic
- except:
- pass
- found = True
- elif graphic[d[0]][d[1]] == "{}".format(counter):
- doors[counter-1] = d
- choices = [(1,0), (0,1), (-1,0), (0,-1)]
- found = True
- elif graphic[d[0]][d[1]] != "#" and graphic[d[0]][d[1]] !="D":
- doors[int(graphic[d[0]][d[1]])-1] = False
- graphic[d[0]][d[1]] = "{}".format(counter)
- d = False
- doors[counter-1] = False
- found = True
- else:
- doors[counter-1] = d
- except IndexError:
- pass
- else:
- d = origional_doors[counter-1]
- choices = [(1,0), (0,1), (-1,0), (0,-1)]
- for i in range(rx+1, rx+s):
- # string = ""
- for j in range(ry +1, ry+s):
- # string +=graphic[i][j]
- if graphic[i][j] == "0":
- graphic[i][j] = "#"
- # print string
- for i in range(rx+1, rx+s):
- for j in range(ry +1, ry+s):
- try:
- int(graphic[i][j])
- graphic[i][j] = "0"
- except:
- pass
- return graphic
- def adjacent_zones(x,y,level):
- res = []
- start_zone = level[x][y]
- print("Adj {} {}".format(x,y))
- if x<len(level)-1:
- if(level[x+1][y] != start_zone):
- res.append((x+1,y))
- if y<len(level[x])-1:
- if(level[x][y+1]!=start_zone):
- res.append((x,y+1))
- if x>0:
- if(level[x-1][y]!=start_zone):
- res.append((x-1,y))
- if y>0:
- if(level[x][y-1]!=start_zone):
- res.append((x,y-1))
- print(res)
- if len(res)==0:
- return False
- else:
- return res
- start(4,3, 7)
RAW Paste Data
