forked from MildarAA/NetGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetGenerator.py
359 lines (323 loc) · 9.12 KB
/
NetGenerator.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
import random
from pynput import keyboard
INTERIOR_LOBBY = [
("File DV6", "📂6"),
("Password DV6", "🔑6"),
("Password DV8", "🔑8"),
("Skunk", "🦨"),
("Wisp", "👻"),
("Killer", "🔪"),
]
INTERIOR_BASIC = [
("Hellhound", "🐺"),
("Sabertooth", "😾"),
("Raven x2", "🐧🐧"),
("Hellhound", "🐺"),
("Wisp", "👻"),
("Raven", "🐧"),
("Password DV6", "🔑6"),
("File DV6", "📂6"),
("Control Node DV6", "🎛6"),
("Password DV6", "🔑6"),
("Skunk", "🦨"),
("Asp", "🐍"),
("Scorpion", "🦂"),
("Killer, Skunk", "🔪🦨"),
("Wisp x3", "👻👻👻"),
("Liche", "💀"),
]
INTERIOR_STANDARD = [
("Hellhound x2", "🐺🐺"),
("Hellhound, Killer", "🐺🔪"),
("Skunk x2", "🦨🦨"),
("Sabertooth", "😾"),
("Scorpion", "🦂"),
("Hellhound", "🐺"),
("Password DV8", "🔑8"),
("File DV8", "📂8"),
("Control Node DV8", "🎛8"),
("Password DV8", "🔑8"),
("Asp", "🐍"),
("Killer", "🔪"),
("Liche", "💀"),
("Asp", "🐍"),
("Raven x3", "🐧🐧🐧"),
("Liche, Raven", "💀🐧"),
]
INTERIOR_UNCOMMON = [
("Kraken", "🐙"),
("Hellhound, Scorpion", "🐺🦂"),
("Hellhound, Killer", "🐺🔪"),
("Raven x2", "🐧🐧"),
("Sabertooth", "😾"),
("Hellhound", "🐺"),
("Password DV10", "🔑10"),
("File DV10", "📂10"),
("Control Node DV10", "🎛10"),
("Password DV10", "🔑10"),
("Killer", "🔪"),
("Liche", "💀"),
("Dragon", "🐲"),
("Asp, Raven", "🐍🐧"),
("Dragon, Wisp", "🐲👻"),
("Giant", "👹"),
]
INTERIOR_ADVANCED = [
("Hellhound x3", "🐺🐺🐺"),
("Asp x2", "🐍🐍"),
("Hellhound, Liche", "🐺💀"),
("Wisp x3", "👻👻👻"),
("Hellhound, Sabertooth", "🐺😾"),
("Kraken", "🐙"),
("Password DV12", "🔑12"),
("File DV12", "📂12"),
("Control Node DV12", "🎛12"),
("Password DV12", "🔑12"),
("Giant", "👹"),
("Dragon", "🐲"),
("Killer, Scorpion", "🔪🦂"),
("Kraken", "🐙"),
("Raven, Wisp, Hellhound", "🐧👻🐺"),
("Dragon x2", "🐲🐲"),
]
class Floor:
def __init__(self,level):
self.level = level
self.is_edge = True
self.childs = []
self.id = 0
self.occupancy = -1
def addChild(self,floor):
self.is_edge = False
self.childs.append(floor)
class FloorSelector:
def __init__(self):
self.selected = set()
def getRandom(self):
rolled = roll3D6()
while rolled in self.selected:
rolled = roll3D6()
self.selected.add(rolled)
return rolled
def rollD6():
return random.randint(1,6)
def rollD10():
return random.randint(1,10)
def roll3D6():
return rollD6() + rollD6() + rollD6()
def writePrintOffset(floor,currOffset):
floor.offset = currOffset
if floor.is_edge:
return currOffset
if len(floor.childs) > 0:
currOffset = writePrintOffset(floor.childs[0],currOffset)
for i in range(1,len(floor.childs)):
currOffset = writePrintOffset(floor.childs[i],currOffset+1)
return currOffset
# -1 right, -2 down
def printArchitectureMakeConnection(floor, netmap):
netmap[(floor.level-1)*2][floor.offset*2] = floor.id
for child in floor.childs:
for i in range(floor.offset*2+1,child.offset*2):
netmap[(floor.level-1)*2][i] = -1
for j in range((floor.level-1)*2+1,(child.level-1)*2):
netmap[j][child.offset*2] = -2
printArchitectureMakeConnection(child,netmap)
def printArchitecture(floors):
depth = max(floor.level for floor in floors)
print("\nMap:")
# findOffsets
writePrintOffset(floors[0], 0)
# fill grid
width = max(floor.offset for floor in floors) + 1
netmap = [[0] * width * 2 for row in range(depth * 2)]
# -1 right, -2 down
printArchitectureMakeConnection(floors[0],netmap)
for row in netmap:
strRow = ""
for hit in row:
if hit > 0:
strRow += str(getOccupant(floors[hit-1], difficulty if hit > 2 else 0)[1])
if hit == 0:
strRow += " "
if hit == -1:
strRow += "--"
if hit == -2:
strRow += "| "
print (strRow)
'''
# minimal size is 5 for 1 Branche,
1 entry, 2 second floor, 3 last floor, 4 - nonBranched floor, 5 - 1st possible branch
So maximal branches is: size - 4
'''
def getNumberOfBraches(size):
maxBranches = size - 4
n_branches = 0
while (n_branches < maxBranches):
if (rollD10() >= 7):
n_branches +=1
else:
break
return n_branches
def findNodeCandidate(all_floors, depth,can_be_edge,can_be_node):
candidates = []
for floor in all_floors:
if (floor.level == 1) or (floor.level >= depth-1):
continue
if ((floor.is_edge) and (can_be_edge)) or ((not floor.is_edge) and (can_be_node)):
candidates.append(floor)
#it is not perfect If no candidate is found, just deepen it and be done with it
if len(candidates) == 0:
for floor in all_floors:
if floor.level == depth:
return floor
selected_id = random.randint(0,len(candidates)-1)
return candidates[selected_id]
def getArchitecture():
n_floors = roll3D6()
n_branches = getNumberOfBraches(n_floors)
print ("floors:" + str(n_floors) + " branches:" + str(n_branches))
if n_branches > 0:
min_size_of_logest_branch = 4
max_size_of_longest_branch = n_floors - n_branches
#use normal distribution for depth instead linear. lets make edge cases less happening
#Also for dice game lets use dice-like method instead Gauss random
depth = round((random.randint(min_size_of_logest_branch,max_size_of_longest_branch) + random.randint(min_size_of_logest_branch,max_size_of_longest_branch))/2)
else:
depth = n_floors
#construct main brainch
all_floors = []
#entrance
floor = Floor(1)
all_floors.append(floor)
#other levels
level = 1
lastfloor = floor
for i in range(2, depth + 1):
floor = Floor(i)
lastfloor.addChild(floor)
all_floors.append(floor)
lastfloor = floor
# add rest of floors and create branches
if n_branches > 0:
remaining_nodes = n_floors - depth
remaining_branches = n_branches
can_be_node = True
can_be_edge = True
while (remaining_nodes >0):
if remaining_nodes == remaining_branches:
can_be_edge = False
if remaining_branches == 0:
can_be_node = False
lastfloor = findNodeCandidate(all_floors,depth,can_be_edge,can_be_node)
# if no candidate was found the NET got deeper
if lastfloor.level == depth:
depth +=1
if not lastfloor.is_edge:
remaining_branches -=1
floor = Floor(lastfloor.level + 1)
lastfloor.addChild(floor)
all_floors.append(floor)
remaining_nodes -=1
return all_floors
#for floor in all_floors:
# print (f"level:{floor.level},edge:{floor.is_edge},childs:{len(floor.childs)}")
def setID(floor,cnt):
cnt+=1
floor.id = cnt
for child in floor.childs:
cnt = setID(child,cnt)
return cnt
def setIDs(floors):
setID(floors[0],0)
def populateLevel(floorRandom,floors):
nextLevel = []
for floor in floors:
floor.occupancy = floorRandom.getRandom()
nextLevel += floor.childs
if len(nextLevel)>0:
populateLevel(floorRandom,nextLevel)
def populateFloors(floors):
first = rollD6()
second = rollD6()
while (first==second):
second = rollD6()
floor = floors[0]
floor.occupancy = first
floor=floor.childs[0]
floor.occupancy = second
floorRandom = FloorSelector()
populateLevel(floorRandom,floor.childs)
def getDifficulty():
print("Please select difficulty")
print(
"1-Basic Difficulty | DV6 | Normal interface level 2 | Deadly bottom interface level: N/A"
)
print(
"2-Standard Difficulty | DV8 | Normal interface level 4 | Deadly bottom interface level: 2"
)
print(
"3-Uncommon Difficulty | DV10 | Normal interface level 6 | Deadly bottom interface level: 4"
)
print(
"4-Advanced Difficulty | DV12 | Normal interface level 8 | Deadly bottom interface level: 6"
)
try:
difficulty = int(input("Difficulty:"))
if not (difficulty >= 1 and difficulty <= 4):
raise ValueError
except ValueError:
print("\n!!ERROR!!")
print("Provide a number between 1 and 4")
print("!!ERROR!!\n")
return getDifficulty()
return difficulty
def getOccupantsFromDifficulty(difficulty):
difficultyMap = {
0: INTERIOR_LOBBY,
1: INTERIOR_BASIC,
2: INTERIOR_STANDARD,
3: INTERIOR_UNCOMMON,
4: INTERIOR_ADVANCED,
}
return difficultyMap[difficulty]
def getOccupant(floor, difficulty):
occupant = getOccupantsFromDifficulty(difficulty)[floor.occupancy - (3 if difficulty else 1)]
return occupant
def printLegendRecursive(floor, difficulty):
occupant = getOccupant(floor, difficulty)
print(f"{occupant[1]}: {occupant[0]}")
for child in floor.childs:
printLegendRecursive(child,difficulty)
def printLegend(floors, difficulty):
print("Legend:")
occupancy = INTERIOR_LOBBY
floor = floors[0]
print(f"{occupancy[floor.occupancy-1][1]}: {occupancy[floor.occupancy-1][0]}")
floor = floor.childs[0]
print(f"{occupancy[floor.occupancy-1][1]}: {occupancy[floor.occupancy-1][0]}")
for child in floor.childs:
printLegendRecursive(child,difficulty)
def on_press(key):
if key == keyboard.Key.esc:
# Stop listener
global exitApp
exitApp = True
return False
if key == keyboard.Key.enter:
# Stop listener
input()
return False
else:
return
exitApp = False
while not exitApp:
difficulty = getDifficulty()
all_floors = getArchitecture()
setIDs(all_floors)
populateFloors(all_floors)
printArchitecture(all_floors)
printLegend(all_floors,difficulty)
print("Press ESC to leave, press ENTER to generate new...\n")
with keyboard.Listener(on_press=on_press) as listener:
listener.join()