* support for content types extension in minetestmapper
[oweals/minetest.git] / util / minetestmapper.py
1 #!/usr/bin/python2
2 # -*- coding: windows-1252 -*-
3
4 # This program is free software. It comes without any warranty, to
5 # the extent permitted by applicable law. You can redistribute it
6 # and/or modify it under the terms of the Do What The Fuck You Want
7 # To Public License, Version 2, as published by Sam Hocevar. See
8 # COPYING for more details.
9
10 # Made by Jogge, modified by celeron55
11 # 2011-05-29: j0gge: initial release
12 # 2011-05-30: celeron55: simultaneous support for sectors/sectors2, removed 
13 # 2011-06-02: j0gge: command line parameters, coordinates, players, ...
14 # 2011-06-04: celeron55: added #!/usr/bin/python2 and converted \r\n to \n
15 #                        to make it easily executable on Linux
16 # 2011-07-30: WF: Support for content types extension, refactoring
17
18 # Requires Python Imaging Library: http://www.pythonware.com/products/pil/
19
20 # Some speed-up: ...lol, actually it slows it down.
21 #import psyco ; psyco.full() 
22 #from psyco.classes import *
23
24 import zlib
25 import Image, ImageDraw, ImageFont, ImageColor
26 import os
27 import string
28 import time
29 import getopt
30 import sys
31 import array
32
33 CONTENT_WATER = [2, 9]
34
35 TRANSLATION_TABLE = {
36         1: 0x800, # CONTENT_GRASS
37         4: 0x801, # CONTENT_TREE
38         5: 0x802, # CONTENT_LEAVES
39         6: 0x803, # CONTENT_GRASS_FOOTSTEPS
40         7: 0x804, # CONTENT_MESE
41         8: 0x805, # CONTENT_MUD
42         10: 0x806, # CONTENT_CLOUD
43         11: 0x807, # CONTENT_COALSTONE
44         12: 0x808, # CONTENT_WOOD
45         13: 0x809, # CONTENT_SAND
46         18: 0x80a, # CONTENT_COBBLE
47         19: 0x80b, # CONTENT_STEEL
48         20: 0x80c, # CONTENT_GLASS
49         22: 0x80d, # CONTENT_MOSSYCOBBLE
50         23: 0x80e, # CONTENT_GRAVEL
51         24: 0x80f, #CONTENT_SANDSTONE
52         25: 0x810, #CONTENT_CACTUS
53         26: 0x811, #CONTENT_BRICK
54         27: 0x812, #CONTENT_CLAY
55         28: 0x813, #CONTENT_PAPYRUS
56         29: 0x814 #CONTENT_BOOKSHELF
57 }
58
59 def hex_to_int(h):
60         i = int(h, 16)
61         if(i > 2047):
62                 i -= 4096
63         return i
64
65 def hex4_to_int(h):
66         i = int(h, 16)
67         if(i > 32767):
68                 i -= 65536
69         return i
70
71 def int_to_hex3(i):
72         if(i < 0):
73                 return "%03X" % (i + 4096)
74         else:
75                 return "%03X" % i
76
77 def int_to_hex4(i):
78         if(i < 0):
79                 return "%04X" % (i + 65536)
80         else:
81                 return "%04X" % i
82
83 def limit(i, l, h):
84         if(i > h):
85                 i = h
86         if(i < l):
87                 i = l
88         return i
89
90 def usage():
91         print "TODO: Help"
92 try:
93         opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=", "output=", "bgcolor=", "scalecolor=", "origincolor=", "playercolor=", "draworigin", "drawplayers", "drawscale"])
94 except getopt.GetoptError, err:
95         # print help information and exit:
96         print str(err) # will print something like "option -a not recognized"
97         usage()
98         sys.exit(2)
99
100 path = "../world/"
101 output = "map.png"
102 border = 0
103 scalecolor = "black"
104 bgcolor = "white"
105 origincolor = "red"
106 playercolor = "red"
107 drawscale = False
108 drawplayers = False
109 draworigin = False
110
111 sector_xmin = -1500 / 16
112 sector_xmax = 1500 / 16
113 sector_zmin = -1500 / 16
114 sector_zmax = 1500 / 16
115
116 for o, a in opts:
117         if o in ("-h", "--help"):
118                 usage()
119                 sys.exit()
120         elif o in ("-i", "--input"):
121                 path = a
122         elif o in ("-o", "--output"):
123                 output = a
124         elif o == "--bgcolor":
125                 bgcolor = ImageColor.getrgb(a)
126         elif o == "--scalecolor":
127                 scalecolor = ImageColor.getrgb(a)
128         elif o == "--playercolor":
129                 playercolor = ImageColor.getrgb(a)
130         elif o == "--origincolor":
131                 origincolor = ImageColor.getrgb(a)
132         elif o == "--drawscale":
133                 drawscale = True
134                 border = 40
135         elif o == "--drawplayers":
136                 drawplayers = True
137         elif o == "--draworigin":
138                 draworigin = True
139         else:
140                 assert False, "unhandled option"
141         
142 if path[-1:]!="/" and path[-1:]!="\\":
143         path = path + "/"
144
145 # Load color information for the blocks.
146 colors = {}
147 f = file("colors.txt")
148 for line in f:
149         values = string.split(line)
150         colors[int(values[0], 16)] = (int(values[1]), int(values[2]), int(values[3]))
151 f.close()
152
153 xlist = []
154 zlist = []
155
156 # List all sectors to memory and calculate the width and heigth of the resulting picture.
157 if os.path.exists(path + "sectors2"):
158         for filename in os.listdir(path + "sectors2"):
159                 for filename2 in os.listdir(path + "sectors2/" + filename):
160                         x = hex_to_int(filename)
161                         z = hex_to_int(filename2)
162                         if x < sector_xmin or x > sector_xmax:
163                                 continue
164                         if z < sector_zmin or z > sector_zmax:
165                                 continue
166                         xlist.append(x)
167                         zlist.append(z)
168
169 if os.path.exists(path + "sectors"):
170         for filename in os.listdir(path + "sectors"):
171                 x = hex4_to_int(filename[:4])
172                 z = hex4_to_int(filename[-4:])
173                 if x < sector_xmin or x > sector_xmax:
174                         continue
175                 if z < sector_zmin or z > sector_zmax:
176                         continue
177                 xlist.append(x)
178                 zlist.append(z)
179
180 minx = min(xlist)
181 minz = min(zlist)
182 maxx = max(xlist)
183 maxz = max(zlist)
184
185 w = (maxx - minx) * 16 + 16
186 h = (maxz - minz) * 16 + 16
187
188 print "w="+str(w)+" h="+str(h)
189
190 im = Image.new("RGB", (w + border, h + border), bgcolor)
191 draw = ImageDraw.Draw(im)
192 impix = im.load()
193
194 stuff = {}
195
196 starttime = time.time()
197
198 def data_is_air(d):
199         return d in [126, 127, 254]
200
201 def read_blocknum(mapdata, version, datapos):
202         if version == 20:
203                 if mapdata[datapos] < 0x80:
204                         return mapdata[datapos]
205                 else:
206                         return (mapdata[datapos] << 4) | (mapdata[datapos + 0x2000] >> 4)
207         elif 16 <= version < 20:
208                 return TRANSLATION_TABLE.get(mapdata[datapos], mapdata[datapos])
209         else:
210                 raise Exception("Unsupported map format: " + str(version))
211
212 def read_mapdata(f, version, pixellist, water):
213         global stuff  # oh my :-)
214         
215         dec_o = zlib.decompressobj()
216         try:
217                 mapdata = array.array("B", dec_o.decompress(f.read()))
218         except:
219                 mapdata = []
220                 
221         f.close()
222         
223         if(len(mapdata) < 4096):
224                 print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + str(len(mapdata))
225         else:
226                 chunkxpos = xpos * 16
227                 chunkypos = ypos * 16
228                 chunkzpos = zpos * 16
229                 blocknum = 0
230                 datapos = 0
231                 for (x, z) in reversed(pixellist):
232                         for y in reversed(range(16)):
233                                 datapos = x + y * 16 + z * 256
234                                 blocknum = read_blocknum(mapdata, version, datapos)
235                                 if not data_is_air(blocknum) and blocknum in colors:
236                                         if blocknum in CONTENT_WATER:
237                                                 water[(x, z)] += 1
238                                                 # Add dummy stuff for drawing sea without seabed
239                                                 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, blocknum, water[(x, z)])
240                                         else:
241                                                 pixellist.remove((x, z))
242                                                 # Memorize information on the type and height of the block and for drawing the picture.
243                                                 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, blocknum, water[(x, z)])
244                                                 break
245                                 elif not data_is_air(blocknum) and blocknum not in colors:
246                                         print "strange block: %s/%s/%s x: %d y: %d z: %d block id: %x" % (xhex, zhex, yhex, x, y, z, blocknum)
247
248 # Go through all sectors.
249 for n in range(len(xlist)):
250         #if n > 500:
251         #       break
252         if n % 200 == 0:
253                 nowtime = time.time()
254                 dtime = nowtime - starttime
255                 try:
256                         n_per_second = 1.0 * n / dtime
257                 except ZeroDivisionError:
258                         n_per_second = 0
259                 if n_per_second != 0:
260                         seconds_per_n = 1.0 / n_per_second
261                         time_guess = seconds_per_n * len(xlist)
262                         remaining_s = time_guess - dtime
263                         remaining_minutes = int(remaining_s / 60)
264                         remaining_s -= remaining_minutes * 60;
265                         print("Processing sector "+str(n)+" of "+str(len(xlist))
266                                         +" ("+str(round(100.0*n/len(xlist), 1))+"%)"
267                                         +" (ETA: "+str(remaining_minutes)+"m "
268                                         +str(int(remaining_s))+"s)")
269
270         xpos = xlist[n]
271         zpos = zlist[n]
272         
273         xhex = int_to_hex3(xpos)
274         zhex = int_to_hex3(zpos)
275         xhex4 = int_to_hex4(xpos)
276         zhex4 = int_to_hex4(zpos)
277         
278         sector1 = xhex4.lower() + zhex4.lower()
279         sector2 = xhex.lower() + "/" + zhex.lower()
280         
281         ylist = []
282         
283         sectortype = ""
284         
285         try:
286                 for filename in os.listdir(path + "sectors/" + sector1):
287                         if(filename != "meta"):
288                                 pos = int(filename, 16)
289                                 if(pos > 32767):
290                                         pos -= 65536
291                                 ylist.append(pos)
292                                 sectortype = "old"
293         except OSError:
294                 pass
295         
296         if sectortype != "old":
297                 try:
298                         for filename in os.listdir(path + "sectors2/" + sector2):
299                                 if(filename != "meta"):
300                                         pos = int(filename, 16)
301                                         if(pos > 32767):
302                                                 pos -= 65536
303                                         ylist.append(pos)
304                                         sectortype = "new"
305                 except OSError:
306                         pass
307         
308         if sectortype == "":
309                 continue
310
311         ylist.sort()
312         
313         
314         # Make a list of pixels of the sector that are to be looked for.
315         pixellist = []
316         water = {}
317         for x in range(16):
318                 for z in range(16):
319                         pixellist.append((x, z))
320                         water[(x, z)] = 0
321         
322         # Go through the Y axis from top to bottom.
323         ylist2=[]
324         for ypos in reversed(ylist):
325                 
326                 yhex = int_to_hex4(ypos)
327
328                 filename = ""
329                 if sectortype == "old":
330                         filename = path + "sectors/" + sector1 + "/" + yhex.lower()
331                 else:
332                         filename = path + "sectors2/" + sector2 + "/" + yhex.lower()
333
334                 f = file(filename, "rb")
335
336                 # Let's just memorize these even though it's not really necessary.
337                 version = ord(f.read(1))
338                 flags = f.read(1)
339                 
340                 # Checking day and night differs -flag
341                 if not ord(flags) & 2:
342                         ylist2.append((ypos,filename))
343                         f.close()
344                         continue
345
346                 read_mapdata(f, version, pixellist, water)
347                 
348                 # After finding all the pixels in the sector, we can move on to the next sector without having to continue the Y axis.
349                 if(len(pixellist) == 0):
350                         break
351         
352         if len(pixellist) > 0:
353                 for (ypos, filename) in ylist2:
354                         f = file(filename, "rb")
355
356                         version = ord(f.read(1))
357                         flags = f.read(1)
358                         
359                         read_mapdata(f, version, pixellist, water)
360                         
361                         # After finding all the pixels in the sector, we can move on to the next sector without having to continue the Y axis.
362                         if(len(pixellist) == 0):
363                                 break
364
365 print "Drawing image"
366 # Drawing the picture
367 starttime = time.time()
368 n = 0
369 for (x, z) in stuff.iterkeys():
370         if n % 500000 == 0:
371                 nowtime = time.time()
372                 dtime = nowtime - starttime
373                 try:
374                         n_per_second = 1.0 * n / dtime
375                 except ZeroDivisionError:
376                         n_per_second = 0
377                 if n_per_second != 0:
378                         listlen = len(stuff)
379                         seconds_per_n = 1.0 / n_per_second
380                         time_guess = seconds_per_n * listlen
381                         remaining_s = time_guess - dtime
382                         remaining_minutes = int(remaining_s / 60)
383                         remaining_s -= remaining_minutes * 60;
384                         print("Drawing pixel "+str(n)+" of "+str(listlen)
385                                         +" ("+str(round(100.0*n/listlen, 1))+"%)"
386                                         +" (ETA: "+str(remaining_minutes)+"m "
387                                         +str(int(remaining_s))+"s)")
388         n += 1
389
390         (r, g, b) = colors[stuff[(x,z)][1]]
391         # Comparing heights of a couple of adjacent blocks and changing brightness accordingly.
392         try:
393                 c1 = stuff[(x - 1, z)][1]
394                 c2 = stuff[(x, z + 1)][1]
395                 c = stuff[(x, z)][1]
396                 if c1 not in CONTENT_WATER and c2 not in CONTENT_WATER and c not in CONTENT_WATER:
397                         y1 = stuff[(x - 1, z)][0]
398                         y2 = stuff[(x, z + 1)][0]
399                         y = stuff[(x, z)][0]
400                         
401                         d = ((y - y1) + (y - y2)) * 12
402                 else:
403                         d = 0
404                 
405                 if(d > 36):
406                         d = 36
407                         
408                 r = limit(r + d, 0, 255)
409                 g = limit(g + d, 0, 255)
410                 b = limit(b + d, 0, 255)
411         except:
412                 pass
413         
414         # Water
415         if(stuff[(x,z)][2] > 0):
416                 r=int(r * .15 + colors[2][0] * .85)
417                 g=int(g * .15 + colors[2][1] * .85)
418                 b=int(b * .15 + colors[2][2] * .85)
419                 
420         impix[x - minx * 16 + border, h - 1 - (z - minz * 16) + border] = (r, g, b)
421
422
423 if draworigin:
424         draw.ellipse((minx * -16 - 5 + border, h - minz * -16 - 6 + border, minx * -16 + 5 + border, h - minz * -16 + 4 + border), outline = origincolor)
425
426 font = ImageFont.load_default()
427
428 if drawscale:
429         draw.text((24, 0), "X", font = font, fill = scalecolor)
430         draw.text((2, 24), "Z", font = font, fill = scalecolor)
431
432         for n in range(int(minx / -4) * -4, maxx, 4):
433                 draw.text((minx * -16 + n * 16 + 2 + border, 0), str(n * 16), font = font, fill = scalecolor)
434                 draw.line((minx * -16 + n * 16 + border, 0, minx * -16 + n * 16 + border, border - 1), fill = scalecolor)
435
436         for n in range(int(maxz / 4) * 4, minz, -4):
437                 draw.text((2, h - 1 - (n * 16 - minz * 16) + border), str(n * 16), font = font, fill = scalecolor)
438                 draw.line((0, h - 1 - (n * 16 - minz * 16) + border, border - 1, h - 1 - (n * 16 - minz * 16) + border), fill = scalecolor)
439
440 if drawplayers:
441         try:
442                 for filename in os.listdir(path + "players"):
443                         f = file(path + "players/" + filename)
444                         lines = f.readlines()
445                         name=""
446                         position=[]
447                         for line in lines:
448                                 p = string.split(line)
449                                 if p[0] == "name":
450                                         name = p[2]
451                                         print filename + ": name = " + name
452                                 if p[0] == "position":
453                                         position = string.split(p[2][1:-1], ",")
454                                         print filename + ": position = " + p[2]
455                         if len(name) > 0 and len(position) == 3:
456                                 x=(int(float(position[0]) / 10 - minx * 16))
457                                 z=int(h - (float(position[2]) / 10 - minz * 16))
458                                 draw.ellipse((x - 2 + border, z - 2 + border, x + 2 + border, z + 2 + border), outline = playercolor)
459                                 draw.text((x + 2 + border, z + 2 + border), name, font = font, fill = playercolor)
460                         f.close()
461         except OSError:
462                 pass
463
464 print "Saving"
465 im.save(output)