-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapDraw.py
161 lines (124 loc) · 4.83 KB
/
mapDraw.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
# vim:set encoding=utf-8
import os
from PIL import Image, ImageDraw, ImageFont
import labyrinthe
import random
class CMapper():
def __init__(self):
self.loadTiles()
def loadTiles(self, path='./img_map/'):
self.tiles = {}
for i in range(2**4):
s = '{0:04b}'.format(i)
s = s.replace('0','o')
s = s.replace('1','x')
try:
img = Image.open(path+'%s.png'%(s))
self.tiles[s] = img
img = Image.open(path+'%s_d.png'%(s))
self.tiles[s+'_d'] = img
except FileNotFoundError:
img = None
self.arrow = Image.open(path+'arrow.png')
def drawMap(self, roomDict, mapName = 'the_map.png', withCoord=False):
keylist = list(roomDict.keys())
xMin = min([i for i,j in keylist])
xMax = max([i for i,j in keylist])+1
yMin = min([j for i,j in keylist])
yMax = max([j for i,j in keylist])+1
self.xMin = xMin
self.xMax = xMax
self.yMin = yMin
self.yMax = yMax
dx, dy = self.tiles['oooo'].size # tile png size
self.fnt = ImageFont.truetype('font/Arial.ttf', 10)
self.fnt2 = ImageFont.truetype('font/arialbd.ttf', 12)
image = Image.new('RGB', (dx*(xMax-xMin), dy*(yMax-yMin)))
drawObj = ImageDraw.Draw(image)
for j in range(yMax-1, yMin-1,-1):
for i in range(xMin, xMax):
try:
r = roomDict[(i,j)]
s = ''
for d in r.doors:
if d == 0:
s += 'x' # wall
else:
s += 'o' # door
if not r.BigRoomId:
s += '_d'
im = self.tiles[s]
except KeyError:
im = self.tiles['xxxx']
r = None
x,y = dx*(i-xMin), dy*(yMax-j-1)
image.paste(im, (x, y))
if withCoord:
if (i,j) == (0,0):
drawObj.text((x+dx/2-10,y+dy/2-10), "%d %d"%(i,j), font=self.fnt2, fill=(255, 0, 0))
else:
drawObj.text((x+dx/2-10,y+dy/2-10), "%d %d"%(i,j), font=self.fnt, fill=(0, 0, 0))
i,j = 0,0
msg = 'GO'
x,y = dx*(i-xMin), dy*(yMax-j-1)
w, h = drawObj.textsize(msg, font=self.fnt2)
drawObj.text((x+dx/2-w/2,y+dy/2-h/2), msg, font=self.fnt2, fill=(255, 0, 0))
self.image = image
self.drawObj = drawObj
def writeInRoom(self, msg, coord, fill=(0, 255, 0)):
i,j = coord
dx, dy = self.tiles['oooo'].size
x,y = dx*(i-self.xMin), dy*(self.yMax-j-1)
w, h = self.drawObj.textsize(msg, font=self.fnt2)
self.drawObj.text((x+dx/2-w/2,y+dy/2-h/2), msg, font=self.fnt2, fill=fill)
def drawInRoom(self, direction, coord):
i,j = coord
dx, dy = self.tiles['oooo'].size
x,y = dx*(i-self.xMin), dy*(self.yMax-j-1)
im = self.arrow.rotate( 90*direction, expand=1 )
self.image.paste(im, (x, y), im)
def saveMap(self, mapName = 'the_map.png'):
self.image.save(mapName)
if __name__ == '__main__':
N = 50
seed = 8
seed = random.randint(1,256)
laby = labyrinthe.CLaby()
mapper = CMapper()
filelist = [ f for f in os.listdir("mapsOut") if f.endswith(".png") ]
for f in filelist:
os.remove(os.path.join("mapsOut", f))
filelist = [ f for f in os.listdir("mapsSpread") if f.endswith(".png") ]
for f in filelist:
os.remove(os.path.join("mapsSpread", f))
laby.initRandom(seed)
for no, (rooms, (i,j)) in enumerate(laby.stepGen(N)):
mapper.drawMap(rooms)
mapper.saveMap('mapsOut/the_map_%03d.png'%(no))
laby.initRandom(seed)
laby.gen(N)
mapper.drawMap(laby.laby)
mapper.writeInRoom( 'START', (0,0) )
mapper.writeInRoom( 'END', laby.end )
mapper.saveMap('the_map.png')
aMap = laby.spread(laby.end)
for k,v in aMap.items():
mapper.writeInRoom( '%d'%(v), k )
mapper.saveMap('the_map_with_distance.png')
no = 0
for frontiere, aMap in laby.spreadGen(laby.end):
mapper.drawMap(laby.laby)
for k,v in aMap.items():
if k in frontiere:
mapper.writeInRoom( '%d'%(v), k, (255, 0, 0))
else:
mapper.writeInRoom( '%d'%(v), k )
mapper.saveMap('mapsSpread/the_map_%03d.png'%(no))
no += 1
mapper.drawMap(laby.laby)
for aRoom in laby.laby.values():
mapper.drawInRoom(laby.dirToEnd(aRoom.pos), aRoom.pos)
mapper.writeInRoom( '%d'%(laby.dirToEnd(aRoom.pos)), aRoom.pos )
mapper.writeInRoom( 'START', (0,0) )
mapper.writeInRoom( 'END', laby.end )
mapper.saveMap('the_map_with_hint.png')