SHARE
TWEET

Python Level Generator

a guest Oct 21st, 2015 114 Never
  1. from random import randint
  2. import time
  3. class Zone():
  4.         def __init__(self, x, y):
  5.                 self.rooms = [(x,y)]
  6.                 self.connections = {}
  7.                 self.id = time.clock()
  8.         def connect(self, coordinates, x, y, level):
  9.                 target = level[coordinates[0]][coordinates[1]]
  10.                 resultant = Zone(x,y)
  11.                 resultant.connections[(x,y)] = [coordinates]
  12.                 # print("Connections:",self.connections)
  13.                 for k in self.connections:
  14.                         v = self.connections[k]
  15.                         if not resultant.connections.get(k, False):
  16.                                 resultant.connections[k] = v
  17.                         else:
  18.                                 resultant.connections[k]+=v
  19.                 for k in target.connections:
  20.                         v = target.connections[k]
  21.                         if not resultant.connections.get(k, False):
  22.                                 resultant.connections[k] =v
  23.                         else:
  24.                                 resultant.connections[k]+=v
  25.                 resultant.rooms = resultant.rooms+self.rooms+target.rooms
  26.                 # print(resultant.connections)
  27.                 for c in resultant.rooms:
  28.                         level[c[0]][c[1]] = resultant
  29.                 return level
  30.  
  31.  
  32.  
  33.  
  34. def start(x, y, s):
  35.         level = []
  36.         for a in range(0,x):
  37.                 level.append([])
  38.                 for b in range(0,y):
  39.                         level[a].append(Zone(a,b))
  40.         unconnected = True
  41.         while unconnected:
  42.                 unconnected = False
  43.                 for a in range(0,x):
  44.                         for b in range(0,y):
  45.                                 # print(level[a][b].connections)
  46.                                 adjacent = adjacent_zones(a,b,level)
  47.                                 if not adjacent:
  48.                                         unconnected = unconnected or False
  49.                                         continue
  50.                                 else:
  51.                                         unconnected = True
  52.                                        
  53.                                 target = randint(0, len(adjacent)-1)
  54.                                 level = level[a][b].connect(adjacent[target], a,b, level)
  55.         #So we  got the level set up, now we print to a file
  56.  
  57.         grand_zone = level[0][0]
  58.         for a in level:
  59.                 for b in a:
  60.                         if b.id != grand_zone.id:
  61.                                 print("Fail")
  62.                                 raise Exception
  63.         # graphic = [["0"]*(5*y)]*(5*x)
  64.         graphic = []
  65.         for i in range(0,x*s):
  66.                 graphic.append([])
  67.                 for j in range(0,y*s):
  68.                         graphic[i].append("0")
  69.         for a in range(0,x*s):
  70.                 if(a%s)==0:
  71.                         graphic[a] = ["#"]*(s*y)
  72.                 for b in range(0,y):
  73.                         graphic[a][b*s] = "#"
  74.                 graphic[a][y*s-1] = "#"
  75.         graphic[x*s-1] = ["#"]*(s*y)
  76.         for k in grand_zone.connections.keys():
  77.                 for i in grand_zone.connections[k]:
  78.                         x = k[0]-i[0]
  79.                         y = k[1]-i[1]
  80.                         x*=s
  81.                         y*=s
  82.                         if x<0:
  83.                                 x*=-1
  84.                         elif x==0:
  85.                                 x= randint(2,s-2)
  86.                         if y<0:
  87.                                 y*=-1
  88.                         elif y == 0:
  89.                                 y = randint(2,s-2)
  90.                         x+= min(k[0], i[0])*s
  91.                         y+= min(k[1], i[1])*s
  92.                         if x>=len(graphic):
  93.                                 x-=s
  94.                         if y>=len(graphic[0]):
  95.                                 y-=s
  96.  
  97.                         graphic[x][y] = "D"
  98.         print("::::::::::::::::::::::::::::::::::::::::::::::::")
  99.         for i in graphic:
  100.                 print(i)
  101.         #Now we technically have the level, but it's fucking boring, below this we fix that
  102.         structure = []
  103.         for a in range(0, len(level)):
  104.                 structure.append([])
  105.                 for b in range(0,len(level[0])):
  106.                         structure[a].append(False)
  107.         connect_counter = 0
  108.         for a in range(0, len(level)):
  109.                 for b in range(0,len(level[0])):
  110.                         if not structure[a][b]:
  111.                                 #0-5 we leave the room alone
  112.                                 #6-8 we remove a wall
  113.                                 #9-10 we turn it into a hallway
  114.                                 room_mode = randint(0, 11)
  115.                                 #range is inclusive on the bottom, not on top
  116.                                 if room_mode in range(6, 8):
  117.                                         print("connecting")
  118.                                         structure[a][b] = "connect-{}".format(connect_counter)
  119.                                         connections = level[0][0].connections.get((a,b), [])
  120.                                         if len(connections) !=0:
  121.                                                 x = randint(1, len(connections))
  122.                                                 for i in range(0,x):
  123.                                                         structure[connections[i][0]][connections[i][1]] = "connect-{}".format(connect_counter)
  124.                                                 connect_counter +=1
  125.                                 elif room_mode in range(8, 12):
  126.                                         door_count = len(grand_zone.connections.get((a,b), []))
  127.                                         if door_count > 1:
  128.                                                 structure[a][b] = "hallway"
  129.                                         else:
  130.                                                 for k in grand_zone.connections.keys():
  131.                                                         if (a,b) in grand_zone.connections[k]:
  132.                                                                 door_count +=1
  133.                                                                 print(grand_zone.connections[k])
  134.                                                                 if door_count > 1:
  135.                                                                         structure[a][b] = "hallway"
  136.                                                                         break
  137.         for i in range(0, len(structure)):
  138.                 for j in range(0, len(structure[0])):
  139.                         if not  structure[i][j]:
  140.                                 continue
  141.                         elif structure[i][j] == "hallway":
  142.                                 graphic = create_hallway(i,j,s,graphic)
  143.                         elif structure[i][j].split("-")[0] == "connect":
  144.                                 #pass
  145.                                 res = connect(i,j,s,graphic,structure)
  146.                                 graphic = res[0]
  147.                                 structure = res[1]
  148.         print(":FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:FINAL:")
  149.         for i in graphic:
  150.                 print(i)
  151.  
  152. def connect(x,y,s,graphic,structure):
  153.         group = structure[x][y].split("-")[1]
  154.         for g in [(1,0), (0,1), (-1,0), (0,-1)]:
  155.                 try:
  156.                         nx = x+g[0]
  157.                         ny = y+g[1]
  158.                         try:
  159.                                 structure[nx][ny]
  160.                         except:
  161.                                 print("excepted in test")
  162.                                 continue
  163.                         if structure[nx][ny].split("-")[1] == group:
  164.                                 dx = g[0]
  165.                                 dy = g[1]
  166.                                 if dx == 0: #the broken wall is on the left/right
  167.                                         dx = min(x,nx)
  168.                                         dy = min(y,ny)
  169.                                         dy+=1
  170.                                         for i in range(dx*s+1, dx*s+s):
  171.                                                 graphic[i][dy*s] = "0"
  172.                                 else:
  173.                                         dx = min(x,nx)
  174.                                         dy = min(y,ny)
  175.                                         dx+=1
  176.                                         for i in range(dy*s+1, dy*s+s):
  177.                                                 graphic[dx*s][i] = "0"
  178.                 except (AttributeError, IndexError) as e:
  179.                         print(e)
  180.                         pass
  181.         print("connecting_______________")
  182.         for i in graphic:
  183.                 print(i)
  184.         return (graphic, structure)
  185.  
  186. def create_hallway(x,y,s,graphic):
  187.         rx = x*s #r = range : how big our rooms are
  188.         ry = y*s
  189.         doors = []
  190.  
  191.         for i in range(rx, rx+s+1):
  192.                 string = ""
  193.                 for j in range(ry, ry+s+1):
  194.                         try:
  195.                                 if graphic[i][j] == "D":
  196.                                         doors.append((i,j))
  197.                         except:
  198.                                 pass
  199.         if len(doors) < 2:
  200.                 print("Not enough doors ({}, {})".format(x,y))
  201.                 return graphic
  202.         origional_doors = doors
  203.  
  204.         go = True
  205.         while go:
  206.                 go = False
  207.                 counter = 0
  208.                 for d in doors:
  209.                         counter +=1
  210.                         go = go or  d
  211.                         if d:
  212.                                 found = False
  213.                                 choices = [(1,0), (0,1), (-1,0), (0,-1)]
  214.                                 while not found:
  215.                                         if len(choices) >0:
  216.                                                 rand = randint(0, len(choices)-1)
  217.                                                 direction = choices[rand]
  218.                                                 del(choices[rand])
  219.                                                 d = (d[0] + direction[0], d[1]+direction[1])
  220.                                                 try:
  221.                                                         if d[0] not in range(rx, rx+s+1) or d[1] not in range(ry, ry+s+1):
  222.                                                                 d = (d[0] - direction[0], d[1]-direction[1])
  223.                                                         elif graphic[d[0]][d[1]] == "0":
  224.                                                                 graphic[d[0]][d[1]] = "{}".format(counter)
  225.                                                                 for i in [(1,0), (0,1), (-1,0), (0,-1)]:
  226.                                                                         try:
  227.                                                                                 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":
  228.  
  229.                                                                                         doors[int(graphic[d[0]+i[0]][d[1]+i[1]])-1] = False
  230.                                                                                         d = False
  231.                                                                                         doors[counter-1] = False
  232.                                                                                         # return graphic
  233.                                                                         except:
  234.                                                                                 pass
  235.                                                                 found = True
  236.                                                         elif graphic[d[0]][d[1]] == "{}".format(counter):
  237.                                                                 doors[counter-1] = d
  238.                                                                 choices = [(1,0), (0,1), (-1,0), (0,-1)]
  239.                                                                 found = True
  240.                                                         elif graphic[d[0]][d[1]] != "#" and graphic[d[0]][d[1]] !="D":
  241.                                                                 doors[int(graphic[d[0]][d[1]])-1] = False
  242.                                                                 graphic[d[0]][d[1]] = "{}".format(counter)
  243.                                                                 d = False
  244.                                                                 doors[counter-1] = False
  245.                                                                 found = True
  246.                                                         else:
  247.                                                                 doors[counter-1] = d
  248.                                                 except IndexError:
  249.                                                         pass
  250.                                         else:
  251.                                                 d = origional_doors[counter-1]
  252.                                                 choices =  [(1,0), (0,1), (-1,0), (0,-1)]
  253.  
  254.         for i in range(rx+1, rx+s):
  255.                 # string = ""
  256.                 for j in range(ry +1, ry+s):
  257.                         # string +=graphic[i][j]
  258.                         if graphic[i][j] == "0":
  259.                                 graphic[i][j] = "#"
  260.                 # print string
  261.  
  262.         for i in range(rx+1, rx+s):
  263.                 for j in range(ry +1, ry+s):
  264.                         try:
  265.                                 int(graphic[i][j])
  266.                                 graphic[i][j] = "0"
  267.                         except:
  268.                                 pass
  269.         return graphic
  270.  
  271.  
  272.  
  273.  
  274.  
  275. def adjacent_zones(x,y,level):
  276.         res = []
  277.         start_zone = level[x][y]
  278.         print("Adj {} {}".format(x,y))
  279.         if x<len(level)-1:
  280.                 if(level[x+1][y] != start_zone):
  281.                         res.append((x+1,y))
  282.         if y<len(level[x])-1:
  283.                 if(level[x][y+1]!=start_zone):
  284.                         res.append((x,y+1))
  285.         if x>0:
  286.                 if(level[x-1][y]!=start_zone):
  287.                         res.append((x-1,y))
  288.         if y>0:
  289.                 if(level[x][y-1]!=start_zone):
  290.                         res.append((x,y-1))
  291.         print(res)
  292.         if len(res)==0:
  293.                 return False
  294.         else:
  295.                 return res
  296.  
  297. start(4,3, 7)
RAW Paste Data
Top