-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
426 lines (344 loc) · 12.1 KB
/
generator.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
415
416
417
418
419
420
421
422
423
424
#Generator.py
#Date : 15/10/2014
import xml.etree.ElementTree as ET
import random
import os, os.path
import xml.dom.minidom as minidom
import imp
from math import sqrt, floor
import StringIO
from maze import *
from mazeView import *
class Generator:
'''
generation of xml file
'''
def __init__(self, config="defaultMuseum"):
self.root = ET.Element('xml')
self.texturePath = "datas/textures/"
self.paintingPath = self.texturePath + "paintings/"
self.listPaintings = os.listdir(self.paintingPath)
self.texturesWallPath = self.texturePath + "wall/"
self.listTexWall = os.listdir(self.texturesWallPath)
self.texturesCeilingPath = self.texturePath + "ceiling/"
self.listTexCeiling = os.listdir(self.texturesCeilingPath)
self.texturesGroundPath = self.texturePath + "ground/"
self.listTexGround= os.listdir(self.texturesGroundPath)
self.defaultTypeDoors = ["big","normal","void"]
self.prepareDefaultParameters()
self.generateNewMuseum()
self.exportToFile(config)
def prepareDefaultParameters(self):
'''
Write defaut parameters on top of the tree
'''
tree = ET.parse("museum.xml") ##Default params
doc = tree.getroot()
defaultParams = doc.find('default')
dimensions = defaultParams.find('dimensions')
self.defaultDimensionsNb = int (sqrt(int(dimensions.get("nb"))))
paintings = defaultParams.find('paintings')
self.defaultPaintingsPath = paintings.get("path")
textures = defaultParams.find('textures')
groundAndCeil = textures.findall("texture")
self.defaultGroundTexture = groundAndCeil[0].get("path")
self.defaultCeilTexture = groundAndCeil[1].get("path")
walls = textures.find('walls')
listWall = walls.findall('wall')
self.defaultWallsTextures = []
for i in range(len(listWall)):
self.defaultWallsTextures.append(listWall[i].get('path'))
signalisation = defaultParams.find('signalisation')
self.defaultSignalisationDirection = signalisation.get("direction")
self.root.append(defaultParams)
def generateNewMuseum(self):
'''
Generate a new random museum
'''
rowsAsked = self.defaultDimensionsNb
colsAsked = self.defaultDimensionsNb
#Generate a new maze with minimum size
searchedPath = floor((rowsAsked * colsAsked) * 0.7)
print "Number of row : " + str(rowsAsked)
print "Number of columns : " + str(colsAsked)
print "Minimum size asked for the path: " + str(searchedPath)
generated = maze_search(rowsAsked,colsAsked, searchedPath)
maze = self.memoMaze = generated[0]
cellList = maze.maze
path = generated[1]
prepareWall = self.generateWallProperty(maze);
rooms = ET.Element('rooms')
# fetch row
for i in range(maze.rows):
#fetch columns
for j in range(maze.cols):
#listWall = self.createWallProperty(maze, i,j)
listWall = prepareWall[i][j]
#Select a set of paintings, get path to textures directory
paintingSelected = self.choosePaintingSet()
#Select a set of textures for ceiling, ground and wa;;s
roomTextureSet = self.chooseRoomTexture()
#calculate new id
actualID = i * maze.rows + j
#Signalisation
signalisation = self.obtainSignalisation(path,i,j)
#generate xml
room = self.generateRoomXml(actualID, listWall, paintingSelected, roomTextureSet, signalisation)
#add it to root xml
rooms.append(room)
self.root.append(rooms)
def generateWallProperty (self, mazeValue):
cellList = mazeValue.maze
wallTypes = [[["void",None,"void",None] for j in range(mazeValue.cols)] for i in range(mazeValue.rows)]
for i in range(mazeValue.rows):
for j in range(mazeValue.cols):
#override left parameters if on the first raw
if (j == 0):
wallTypes[i][j][0] = "wall"
if (i == 0):
wallTypes[i][j][2] = "wall"
if not cellList[i][j][0]: #Bottom
wallTypes[i][j][3] = self.chooseDoorType()
else:
wallTypes[i][j][3] = "wall"
if not cellList[i][j][1]: #Right
wallTypes[i][j][1] = self.chooseDoorType()
else:
wallTypes[i][j][1] = "wall"
return wallTypes
def getOppositeDirection(self, direction):
'''
return the opposite direction for the direction given in parameters
'''
if (direction == 0):
return 1
elif (direction == 1):
return 0
elif (direction == 2):
return 3
elif (direction == 3):
return 2
else:
return 10 #none
def choosePaintingSet(self):
'''
Return path to paintings for the a room using random choice from the list
Remove the paintings from the original list to prevent duplication
'''
resultRand = random.randint(0, len(self.listPaintings)-1)
paintingSet = self.listPaintings[resultRand]
#del self.listPaintings[resultRand]
return paintingSet
def chooseRoomTexture(self):
'''
Return textures data for the current room
format return [pathToGround, pathToCeiling, [left,right,up,down]]
'''
resultRand = random.randint(0, len(self.listTexGround)-1)
pathToGround = self.listTexGround[resultRand]
resultRand = random.randint(0, len(self.listTexCeiling)-1)
pathToCeiling = self.listTexCeiling[resultRand]
wallList = []
for i in range(4):
resultRand = random.randint(0, len(self.listTexWall)-1)
wallList.append(self.listTexWall[resultRand])
return [pathToGround,pathToCeiling,wallList]
def chooseDoorType(self):
'''
return a door type choosen randomy
'''
resultRand = random.randint(0, len(self.defaultTypeDoors)-1)
doorChoosen = self.defaultTypeDoors[resultRand]
return doorChoosen
def generateRoomXml(self,id,listWall, paintingSelected, roomTextureSet, signal):
'''
Generate XMl tree of a room using the given parameters
'''
room = ET.Element('room')
room.set("id",str(id))
############Doors part##############
doors = ET.Element('doors')
#check all walls
for w in range( len(listWall)):
typeDoorCurrent = listWall[w]
if (typeDoorCurrent != "wall"):
door = ET.Element('door')
door.set("direction",self.varToStr(w))
door.set("type",typeDoorCurrent)
doors.append(door)
room.append(doors)
###########Paintings Part###########
if (paintingSelected != self.defaultPaintingsPath): #Default Parameters
paintings = ET.Element('paintings')
#old version ; number of textures in files
#counter = len([name for name in os.listdir(self.defautPaintingPath + paintingSelected) if os.path.isfile(os.path.join(self.defautPaintingPath + paintingSelected, name))])
#new version ; randint
counter = random.randrange(4, 15)
paintings.set("nb",str(counter))
paintings.set("path",paintingSelected)
room.append(paintings)
##########Room Texture set##########
textures = ET.Element('textures')
isTexturesFilled = False
isWallsFilled = False
if roomTextureSet[0] != self.defaultGroundTexture :
ground = ET.Element("texture")
ground.set("path",roomTextureSet[0])
ground.set("type", "ground")
textures.append(ground)
isTexturesFilled = True
if roomTextureSet[1] != self.defaultCeilTexture :
ceil = ET.Element("texture")
ceil.set("path",roomTextureSet[1])
ceil.set("type", "ceiling")
textures.append(ceil)
isTexturesFilled = true
walls = ET.Element("walls")
for i in range(len(roomTextureSet[2])):
if roomTextureSet[2][i] != self.defaultWallsTextures[i] :
wall = ET.Element("wall")
wall.set("path",roomTextureSet[2][i])
wall.set("type",self.varToStr(i))
walls.append(wall)
isWallsFilled = True
if isWallsFilled : textures.append(walls)
if isTexturesFilled | isWallsFilled: room.append(textures)
###########Signalisation############
if (signal != self.defaultSignalisationDirection): #default parameters, don't write
signalisation = ET.Element('signalisation')
signalisation.set("direction",signal)
room.append(signalisation)
return room
def varToStr(self, value):
"""
Return string corresponding to the given value
"""
if (value == 2):
return "up"
elif (value == 3):
return "down"
elif (value == 1):
return "right"
elif (value == 0):
return "left"
else:
return "unknow"
def obtainSignalisation(self, path, x, y ):
"""
Get the corresponding signalisation ( direction up/down/right/left ) for the actual cell
"""
for index, item in enumerate(path):
if (x,y) == item:
#We are on the optimal path, searching for the next cell to get signalisation
if (index == 0) :
return "begin"
else :
if (index + 1 < len(path)):
nextItem = path[index+1]
if ((x+1,y) == nextItem):
return "right"
elif ((x-1,y) == nextItem):
return "left"
elif ((x,y+1) == nextItem):
return "down"
elif ((x,y-1) == nextItem):
return "up"
else :
return "WTF"
else :
return "end"
return "N/A"
def exportToFile(self, nameFile = "defaultMuseum"):
fileDirectory = ""
fileName = nameFile + ".xml"
fileDirectory = "datas/generated/" + nameFile + "/"
self.writeDirectory = fileDirectory
print "Exporting XML Tree to file : " + fileName + " in directory :" + fileDirectory
#check if generated dir exist
if not os.path.exists("datas/generated"):
os.makedirs("datas/generated")
#Generated of the directory for given museum if not exist
if not os.path.exists(fileDirectory):
os.makedirs(fileDirectory)
#delete all files into the directory ( remove img and xml file )
listFiles = os.listdir(fileDirectory)
for i in xrange(len(listFiles) ):
os.remove(fileDirectory + listFiles[i])
#format the xml nicely
rough_string = ET.tostring(self.root, method="xml", encoding='UTF-8' )
buf = StringIO.StringIO(rough_string)
bufferLine = buf.readline()
resultString = ""
while len (bufferLine ) > 0:
list_char = list(bufferLine)
sizeList = len (list_char)
while ( sizeList > 0):
if list_char[0] == '<' :
if (list_char[sizeList-1] == '\n') :
del list_char[-1]
resultString += "".join(list_char)
break;
else :
list_char.pop(0)
sizeList = len (list_char)
bufferLine = buf.readline()
reparsed = minidom.parseString(resultString)
reparsed = reparsed.toprettyxml(indent="\t")
#Write to file
f = open(fileDirectory+ fileName, 'w')
f.write(reparsed)
f.close()
print "Exporting done, new museum generated!\n\n"
def visualizeMuseum(self):
"""
Gestion of the museum's map generation & visualisation
NOTE : you need pygame to run this !
"""
print "\n#######VISUALISATION########\nTrying to launch visualisation of the map of the museum "
try:
print "Visualisation loaded, you can close it by closing the window"
visualize(self.memoMaze, self.writeDirectory)
except ImportError:
print "<!> ERROR : Visualisation failed, you need to have pygame installed to visualise the map!"
if __name__ == "__main__":
usage = "Usage : python generator.py -v param -n 'nameHere'\n\n-v Y/N - Permit visualisation of the output using pygame\n\n-n 'nameHere' - Permit to give a name to your generated file, if not explicitly put it override the default museum\n\n\n NOTE: you must have a pair number of parameters or the script will stop"
if ( len(argv) <= 1):
print usage
else:
launchParameters = argv
launchParameters.pop(0) #remove name of the script
nbOptions = len(launchParameters)
index = 0
listCommands = {}
for x in range(0,nbOptions,2):
if (x+1 < nbOptions):
listCommands[launchParameters[x]] =launchParameters[x+1]
else :
print usage
exit(0)
#DEFAULT parameters
nameMuseum = "defaultMuseum"
if "-n" in listCommands :
nameMuseum = listCommands["-n"]
if nameMuseum == "defaultMuseum":
print "Vous etes sur le point de remplacer la configuration xml de base, etes vous sur? ( Y/N)"
answer = raw_input("Continuer : ")
if answer != "Y":
print "aborting...."
exit(0)
elif os.path.exists("datas/generated/" + nameMuseum):
print "Un musee portant ce nom existe deja, voulez-vous le remplacer?"
answer = raw_input("Remplacer (Y/N) : ")
if answer != "Y":
print "aborting...."
exit(0)
else :
print "Vous etes sur le point de remplacer la configuration xml de base, etes vous sur? ( Y/N)"
answer = raw_input("Continuer : ")
if answer != "Y":
print "aborting...."
exit(0)
m = Generator(nameMuseum)
if "-v" in listCommands :
if listCommands["-v"] == 'Y':
m.visualizeMuseum()