-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.py
414 lines (389 loc) · 19.2 KB
/
level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
__author__ = 'DWA'
import random,pickle
from room import Room
from hall import Hall
from connector import Connector
from configparser import ConfigParser
config=ConfigParser()
config.read('config.ini')
VERBOSE_ROOMS=config.getboolean('DEBUG','VERBOSE_ROOMS')
VERBOSE_HALLS=config.getboolean('DEBUG','VERBOSE_HALLS')
VERBOSE_CONNECTORS=config.getboolean('DEBUG','VERBOSE_CONNECTORS')
class Level(object):
def __init__(self):
self.width = 2*(config.getint('LEVEL PARAMS','gWidth')//2)+1 # ODD ONLY
self.height = 2*(config.getint('LEVEL PARAMS','gHeight')//2)+1 # ODD ONLY
self.maxRoomsPerLevel = config.getint('LEVEL PARAMS','maxRoomsPerLevel')
self.maxRoomAttempts = config.getint('LEVEL PARAMS','maxRoomPlacementAttempts')
self.maxRoomWidth = 2*(config.getint('LEVEL PARAMS','maxRmWidth')//2)+1 # ODD ONLY
self.minRoomWidth = 2*(config.getint('LEVEL PARAMS','minRmWidth')//2)+1 # ODD ONLY
self.maxRoomHeight = 2*(config.getint('LEVEL PARAMS','maxRmHeight')//2)+1 # ODD ONLY
self.minRoomHeight = 2*(config.getint('LEVEL PARAMS','minRmHeight')//2)+1 # ODD ONLY
self.minRoomGap = 2*(config.getint('LEVEL PARAMS','minRoomGap')//2)+1 # ODD ONLY
self.mapBorder = 2*(config.getint('LEVEL PARAMS','mapBorder')//2)+1 # ODD ONLY
self.createSecretDoors = config.getboolean('LEVEL PARAMS','createSecretDoors')
self.miniMapGM = config.getboolean('GOD MODE','miniMapGM')
self.tiles = [ [ 0 for x in range(self.height) ] for y in range(self.width) ]
self.visitedTiles = [ [ self.miniMapGM for x in range(self.height) ] for y in range(self.width) ]
self.rooms = []
self.addRooms()
self.halls = []
self.ci = 0
self.cj = 0
self.addHalls()
self.connectors = []
self.addConnectors()
self.promoteConnectorsToDoors()
if self.createSecretDoors == True:
self.promoteConnectorsToSecretDoors()
self.removeUnusedConnectors()
self.removeDeadEnds()
self.createFalseRooms()
self.createHallSeparators()
self.printLevelStatsAsCLI()
def get_maxRooms(self):
return self.maxRoomAttempts
def addRooms(self):
for r in range(self.maxRoomAttempts):
if (len(self.rooms)==self.maxRoomsPerLevel):
break
randomW=2*(random.randint(self.minRoomWidth,self.maxRoomWidth)//2)+1 # ODD ONLY
randomH=2*(random.randint(self.minRoomWidth,self.maxRoomHeight)//2)+1 # ODD will not change
randomX=2*(random.randint(self.mapBorder,self.width-randomW-self.mapBorder)//2)+1 # but EVEN will increase by 1
randomY=2*(random.randint(self.mapBorder,self.height-randomH-self.mapBorder)//2)+1
if self.validateNewRoom(randomX,randomY,randomW,randomH) == True:
self.rooms.append(Room(len(self.rooms)+1,randomX,randomY,randomW,randomH))
for i in range(randomX,randomX+randomW):
for j in range(randomY,randomY+randomH):
self.tiles[i][j]=len(self.rooms)
def validateNewRoom(self,x,y,w,h):
newRoomIsValid=True
for i in range(x-self.minRoomGap,x+w+self.minRoomGap):
for j in range (y-self.minRoomGap,y+h+self.minRoomGap):
if i<0: i=0
if i>=self.width: i=self.width-1
if j<0: j=0
if j>=self.height: j=self.height-1
if self.tiles[i][j] != 0:
newRoomIsValid=False
return newRoomIsValid
def addHalls(self):
while self.seedHall()==True:
self.carveHall()
cid=len(self.halls)
while len(self.halls[cid-1].crumbs)>0:
self.reverseCarveHall()
def seedHall(self):
foundSeed=False
for i in range(self.mapBorder,self.width-self.mapBorder,2):
for j in range(self.mapBorder,self.height-self.mapBorder,2):
if self.tiles[i][j]==0 and foundSeed==False:
cid=len(self.halls)+1
self.halls.append(Hall(cid))
self.tiles[i][j]=-cid
self.ci=i
self.cj=j
foundSeed=True
return foundSeed
def carveHall(self):
keepCarving=True
while keepCarving:
i=self.ci
j=self.cj
validDirs=[]
n=j-2
s=j+2
w=i-2
e=i+2
if (n>=self.mapBorder and n<self.width-self.mapBorder and self.tiles[i][n]==0):
validDirs.append("n")
if (s>=self.mapBorder and s<self.width-self.mapBorder and self.tiles[i][s]==0):
validDirs.append("s")
if (w>=self.mapBorder and w<self.width-self.mapBorder and self.tiles[w][j]==0):
validDirs.append("w")
if (e>=self.mapBorder and e<self.width-self.mapBorder and self.tiles[e][j]==0):
validDirs.append("e")
if len(validDirs)==0:
keepCarving=False
cid=len(self.halls)
self.halls[cid-1].crumbs.append((self.ci,self.cj))
self.halls[cid-1].paths.append((self.ci,self.cj))
else:
dir=random.choice(validDirs)
cid=len(self.halls)
self.halls[cid-1].crumbs.append((self.ci,self.cj))
self.halls[cid-1].paths.append((self.ci,self.cj))
cid=len(self.halls)
if dir=="n":
self.tiles[i][j-1]=-cid
self.tiles[i][j-2]=-cid
self.ci=i
self.cj=j-2
if dir=="s":
self.tiles[i][j+1]=-cid
self.tiles[i][j+2]=-cid
self.ci=i
self.cj=j+2
if dir=="w":
self.tiles[i-1][j]=-cid
self.tiles[i-2][j]=-cid
self.ci=i-2
self.cj=j
if dir=="e":
self.tiles[i+1][j]=-cid
self.tiles[i+2][j]=-cid
self.ci=i+2
self.cj=j
cid=len(self.halls)
self.halls[cid-1].crumbs.pop()
lc=len(self.halls[cid-1].crumbs)
if lc>0:
self.ci=self.halls[cid-1].crumbs[lc-1][0]
self.cj=self.halls[cid-1].crumbs[lc-1][1]
def reverseCarveHall(self):
keepCarving=True
while keepCarving==True:
i=self.ci
j=self.cj
validDirs=[]
n=j-2
s=j+2
w=i-2
e=i+2
if (n>=self.mapBorder and n<self.height-self.mapBorder and self.tiles[i][n]==0):
validDirs.append("n")
if (s>=self.mapBorder and s<self.height-self.mapBorder and self.tiles[i][s]==0):
validDirs.append("s")
if (w>=self.mapBorder and w<self.width-self.mapBorder and self.tiles[w][j]==0):
validDirs.append("w")
if (e>=self.mapBorder and e<self.width-self.mapBorder and self.tiles[e][j]==0):
validDirs.append("e")
if len(validDirs)==0:
keepCarving=False
else:
dir=random.choice(validDirs)
cid=len(self.halls)
self.halls[cid-1].crumbs.append((self.ci,self.cj))
cid=len(self.halls)
if dir=="n":
self.tiles[i][j-1]=-cid
self.tiles[i][j-2]=-cid
self.ci=i
self.cj=j-2
self.halls[cid-1].paths.append((self.ci,self.cj))
if dir=="s":
self.tiles[i][j+1]=-cid
self.tiles[i][j+2]=-cid
self.ci=i
self.cj=j+2
self.halls[cid-1].paths.append((self.ci,self.cj))
if dir=="w":
self.tiles[i-1][j]=-cid
self.tiles[i-2][j]=-cid
self.ci=i-2
self.cj=j
self.halls[cid-1].paths.append((self.ci,self.cj))
if dir=="e":
self.tiles[i+1][j]=-cid
self.tiles[i+2][j]=-cid
self.ci=i+2
self.cj=j
self.halls[cid-1].paths.append((self.ci,self.cj))
cid=len(self.halls)
self.halls[cid-1].crumbs.pop()
lc=len(self.halls[cid-1].crumbs)
if lc>0:
self.ci=self.halls[cid-1].crumbs[lc-1][0]
self.cj=self.halls[cid-1].crumbs[lc-1][1]
def addConnectors(self):
for i in range(self.mapBorder,self.width-self.mapBorder):
for j in range(self.mapBorder,self.height-self.mapBorder):
for k in range(-1,2,2):
if self.tiles[i-1][j]!=self.tiles[i+1][j] and self.tiles[i][j]==0 and self.tiles[i-1][j]!=0 and self.tiles[i+1][j]!=0 and self.tiles[i-1][j]!=1000 and self.tiles[i+1][j]!=1000:
self.tiles[i][j]=1000
self.connectors.append(Connector(len(self.connectors)+1,i,j,self.tiles[i-1][j],self.tiles[i+1][j],0))
if self.tiles[i][j-1]!=self.tiles[i][j+1] and self.tiles[i][j]==0 and self.tiles[i][j-1]!=0 and self.tiles[i][j+1]!=0 and self.tiles[i][j-1]!=1000 and self.tiles[i][j+1]!=1000:
self.tiles[i][j]=1000
self.connectors.append(Connector(len(self.connectors)+1,i,j,self.tiles[i][j+1],self.tiles[i][j-1],0))
def promoteConnectorsToDoors(self):
unConnectedRegions=[]
for ur in range(len(self.rooms)):
unConnectedRegions.append(self.rooms[ur].id)
for uh in range(len(self.halls)):
unConnectedRegions.append(-self.rooms[uh].id)
connectedRegions=[]
regionSeed=random.choice(unConnectedRegions)
connectedRegions.append(regionSeed)
while len(unConnectedRegions) > 0:
possibleConnectors=[]
for c in range(len(self.connectors)):
pc=self.connectors[c]
if (pc.reg1 in connectedRegions and pc.reg2 not in connectedRegions) or (pc.reg2 in connectedRegions and pc.reg1 not in connectedRegions):
possibleConnectors.append(pc)
cpc=random.choice(possibleConnectors)
cpc.type=1
self.tiles[cpc.x][cpc.y]=1001
if cpc.reg1 in unConnectedRegions:
unConnectedRegions.remove(cpc.reg1)
if cpc.reg2 in unConnectedRegions:
unConnectedRegions.remove(cpc.reg2)
connectedRegions.append(cpc.reg1)
connectedRegions.append(cpc.reg2)
def promoteConnectorsToSecretDoors(self):
maxCount=100
for r in range(len(self.connectors)):
randInt=random.randint(0,maxCount)
if self.connectors[r].type==0 and randInt<maxCount//10 and self.tiles[self.connectors[r].x+1][self.connectors[r].y]<1001 and self.tiles[self.connectors[r].x-1][self.connectors[r].y]<1001 and self.tiles[self.connectors[r].x][self.connectors[r].y+1]<1001 and self.tiles[self.connectors[r].x][self.connectors[r].y-1]<1001:
self.connectors[r].type=2
self.tiles[self.connectors[r].x][self.connectors[r].y]=1002
def removeUnusedConnectors(self):
for c in range(len(self.connectors)):
pc=self.connectors[c]
if pc.type==-1 or pc.type==0:
# type -1 are invalid connectors
# type 0 connectors are initially valid but later discarded
self.tiles[pc.x][pc.y]=0
def removeDeadEnds(self):
for i in range(self.mapBorder,self.width-self.mapBorder):
for j in range(self.mapBorder,self.height-self.mapBorder):
numEmpty=0
if self.tiles[i-1][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i-1][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j]==0: numEmpty=numEmpty+1
if self.tiles[i-1][j]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j]!=1001 and self.tiles[i-1][j]!=1001 and self.tiles[i][j+1]!=1001 and self.tiles[i][j-1]!=1001 and self.tiles[i][j]<0 and numEmpty >6:
self.tiles[i][j]=0
for i in range(self.width-self.mapBorder-1,self.mapBorder,-1):
for j in range(self.height-self.mapBorder-1,self.mapBorder,-1):
numEmpty=0
if self.tiles[i-1][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i-1][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j]==0: numEmpty=numEmpty+1
if self.tiles[i-1][j]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j]!=1001 and self.tiles[i-1][j]!=1001 and self.tiles[i][j+1]!=1001 and self.tiles[i][j-1]!=1001 and self.tiles[i][j]<0 and numEmpty >6:
self.tiles[i][j]=0
for h in range(len(self.halls)):
self.halls[h].paths=[]
for i in range(self.mapBorder,self.width-self.mapBorder):
for j in range(self.mapBorder,self.height-self.mapBorder):
if self.tiles[i][j]<0:
self.halls[self.tiles[i][j]].paths.append((i,j))
for h in range(len(self.halls)):
self.halls[h].paths.sort()
def createFalseRooms(self):
for i in range(self.mapBorder,self.width-self.mapBorder):
for j in range(self.mapBorder,self.height-self.mapBorder):
numEmpty=0
if self.tiles[i-1][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i-1][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i][j+1]==0: numEmpty=numEmpty+1
if self.tiles[i][j-1]==0: numEmpty=numEmpty+1
if self.tiles[i+1][j]==0: numEmpty=numEmpty+1
if self.tiles[i-1][j]==0: numEmpty=numEmpty+1
inRoom=False
for di in range(-1,2):
for dj in range(-1,2):
if self.tiles[i+di][j+dj]>0:
inRoom=True
if self.tiles[i][j]==0 and numEmpty==7 and inRoom==False and self.tiles[i+1][j+1]==0 and self.tiles[i-1][j-1]==0 and self.tiles[i+1][j-1]==0 and self.tiles[i-1][j+1]==0:
self.tiles[i][j]=9998
def createHallSeparators(self):
for i in range(self.mapBorder,self.width-self.mapBorder):
for j in range(self.mapBorder,self.height-self.mapBorder):
noDefects=True
if j+2<self.height-self.mapBorder:
if self.tiles[i+1][j+2]!=0: noDefects=False
if self.tiles[i+1][j+1]!=0: noDefects=False
if self.tiles[i+1][j]!=0: noDefects=False
if self.tiles[i+1][j-1]!=0: noDefects=False
if j-2>self.mapBorder:
if self.tiles[i+1][j-2]!=0: noDefects=False
if j+2<self.height-self.mapBorder:
if self.tiles[i-1][j+2]!=0: noDefects=False
if self.tiles[i-1][j+1]!=0: noDefects=False
if self.tiles[i-1][j]!=0: noDefects=False
if self.tiles[i-1][j-1]!=0: noDefects=False
if j-2>self.mapBorder:
if self.tiles[i-1][j-2]!=0: noDefects=False
maxCount=100
randInt=random.randint(0,maxCount)
if randInt<maxCount//4 and self.tiles[i][j]<0 and self.tiles[i][j+1]<0 and self.tiles[i][j-1]<0 and self.tiles[i][j+2]<0 and self.tiles[i][j-2]<0 and noDefects==True:
self.tiles[i][j]=9999
for i in range(self.mapBorder,self.width-self.mapBorder):
for j in range(self.mapBorder,self.height-self.mapBorder):
noDefects=True
if i+2<self.width-self.mapBorder:
if self.tiles[i+2][j+1]!=0: noDefects=False
if self.tiles[i+1][j+1]!=0: noDefects=False
if self.tiles[i][j+1]!=0: noDefects=False
if self.tiles[i+1][j+1]!=0: noDefects=False
if i-2>self.mapBorder:
if self.tiles[i-2][j+1]!=0: noDefects=False
if i-2>self.mapBorder:
if self.tiles[i-2][j-1]!=0: noDefects=False
if self.tiles[i-1][j-1]!=0: noDefects=False
if self.tiles[i][j-1]!=0: noDefects=False
if self.tiles[i+1][j-1]!=0: noDefects=False
if i+2<self.width-self.mapBorder:
if self.tiles[i+2][j-1]!=0: noDefects=False
maxCount=100
randInt=random.randint(0,maxCount)
if randInt<maxCount//4 and self.tiles[i][j]<0 and self.tiles[i+1][j]<0 and self.tiles[i-1][j]<0 and self.tiles[i+2][j]<0 and self.tiles[i-2][j]<0 and noDefects==True:
self.tiles[i][j]=9999
def printLevelStatsAsCLI(self):
r=len(self.rooms)
if VERBOSE_ROOMS == True:
print("Verbose Room Info")
print("")
for i in range(r):
self.rooms[i].printRoomStatsAsCLI()
print("# Rooms: "+str(r))
print("")
h=len(self.halls)
if VERBOSE_HALLS == True:
print("Verbose Hall Info")
print("")
for i in range(h):
self.halls[i].printHallStatsAsCLI()
print("# Halls: "+str(h))
print("")
c=len(self.connectors)
ci=0 #count of invalid connectors
cd=0 #count of door connectors
cs=0 #count of secret connectors
cf=0 #count of failed connectors (valid but unused)
if VERBOSE_CONNECTORS == True:
print("Verbose Connector Info")
print("")
for i in range(c):
if VERBOSE_CONNECTORS == True:
self.connectors[i].printConnStatsAsCLI()
if self.connectors[i].type==-1:
ci=ci+1
if self.connectors[i].type==0:
cf=cf+1
if self.connectors[i].type==1:
cd=cd+1
if self.connectors[i].type==2:
cs=cs+1
print("# Connectors (invalid): "+str(ci))
print("# Connectors (normal): "+str(cd))
print("# Connectors (secret): "+str(cs))
print("# Connectors (failed): "+str(cf))
print("# Connectors: "+str(c))
print("")
def saveLevel(self,levelInstance):
pickle.dump( levelInstance, open( "level.sav", "wb" ) )
def loadLevel(self):
levelInstance = pickle.load( open( "level.sav", "rb" ) )
return levelInstance