2 # -*- coding: windows-1252 -*-
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.
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
18 # Requires Python Imaging Library: http://www.pythonware.com/products/pil/
20 # Some speed-up: ...lol, actually it slows it down.
21 #import psyco ; psyco.full()
22 #from psyco.classes import *
25 import Image, ImageDraw, ImageFont, ImageColor
33 CONTENT_WATER = [2, 9]
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
73 return "%03X" % (i + 4096)
79 return "%04X" % (i + 65536)
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"
111 sector_xmin = -1500 / 16
112 sector_xmax = 1500 / 16
113 sector_zmin = -1500 / 16
114 sector_zmax = 1500 / 16
117 if o in ("-h", "--help"):
120 elif o in ("-i", "--input"):
122 elif o in ("-o", "--output"):
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":
135 elif o == "--drawplayers":
137 elif o == "--draworigin":
140 assert False, "unhandled option"
142 if path[-1:]!="/" and path[-1:]!="\\":
145 # Load color information for the blocks.
147 f = file("colors.txt")
149 values = string.split(line)
150 colors[int(values[0], 16)] = (int(values[1]), int(values[2]), int(values[3]))
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:
164 if z < sector_zmin or z > sector_zmax:
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:
175 if z < sector_zmin or z > sector_zmax:
185 w = (maxx - minx) * 16 + 16
186 h = (maxz - minz) * 16 + 16
188 print "w="+str(w)+" h="+str(h)
190 im = Image.new("RGB", (w + border, h + border), bgcolor)
191 draw = ImageDraw.Draw(im)
196 starttime = time.time()
199 return d in [126, 127, 254]
201 def read_blocknum(mapdata, version, datapos):
203 if mapdata[datapos] < 0x80:
204 return mapdata[datapos]
206 return (mapdata[datapos] << 4) | (mapdata[datapos + 0x2000] >> 4)
207 elif 16 <= version < 20:
208 return TRANSLATION_TABLE.get(mapdata[datapos], mapdata[datapos])
210 raise Exception("Unsupported map format: " + str(version))
212 def read_mapdata(f, version, pixellist, water):
213 global stuff # oh my :-)
215 dec_o = zlib.decompressobj()
217 mapdata = array.array("B", dec_o.decompress(f.read()))
223 if(len(mapdata) < 4096):
224 print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + str(len(mapdata))
226 chunkxpos = xpos * 16
227 chunkypos = ypos * 16
228 chunkzpos = zpos * 16
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:
238 # Add dummy stuff for drawing sea without seabed
239 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, blocknum, water[(x, z)])
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)])
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)
248 # Go through all sectors.
249 for n in range(len(xlist)):
253 nowtime = time.time()
254 dtime = nowtime - starttime
256 n_per_second = 1.0 * n / dtime
257 except ZeroDivisionError:
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)")
273 xhex = int_to_hex3(xpos)
274 zhex = int_to_hex3(zpos)
275 xhex4 = int_to_hex4(xpos)
276 zhex4 = int_to_hex4(zpos)
278 sector1 = xhex4.lower() + zhex4.lower()
279 sector2 = xhex.lower() + "/" + zhex.lower()
286 for filename in os.listdir(path + "sectors/" + sector1):
287 if(filename != "meta"):
288 pos = int(filename, 16)
296 if sectortype != "old":
298 for filename in os.listdir(path + "sectors2/" + sector2):
299 if(filename != "meta"):
300 pos = int(filename, 16)
314 # Make a list of pixels of the sector that are to be looked for.
319 pixellist.append((x, z))
322 # Go through the Y axis from top to bottom.
324 for ypos in reversed(ylist):
326 yhex = int_to_hex4(ypos)
329 if sectortype == "old":
330 filename = path + "sectors/" + sector1 + "/" + yhex.lower()
332 filename = path + "sectors2/" + sector2 + "/" + yhex.lower()
334 f = file(filename, "rb")
336 # Let's just memorize these even though it's not really necessary.
337 version = ord(f.read(1))
340 # Checking day and night differs -flag
341 if not ord(flags) & 2:
342 ylist2.append((ypos,filename))
346 read_mapdata(f, version, pixellist, water)
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):
352 if len(pixellist) > 0:
353 for (ypos, filename) in ylist2:
354 f = file(filename, "rb")
356 version = ord(f.read(1))
359 read_mapdata(f, version, pixellist, water)
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):
365 print "Drawing image"
366 # Drawing the picture
367 starttime = time.time()
369 for (x, z) in stuff.iterkeys():
371 nowtime = time.time()
372 dtime = nowtime - starttime
374 n_per_second = 1.0 * n / dtime
375 except ZeroDivisionError:
377 if n_per_second != 0:
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)")
390 (r, g, b) = colors[stuff[(x,z)][1]]
391 # Comparing heights of a couple of adjacent blocks and changing brightness accordingly.
393 c1 = stuff[(x - 1, z)][1]
394 c2 = stuff[(x, z + 1)][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]
401 d = ((y - y1) + (y - y2)) * 12
408 r = limit(r + d, 0, 255)
409 g = limit(g + d, 0, 255)
410 b = limit(b + d, 0, 255)
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)
420 impix[x - minx * 16 + border, h - 1 - (z - minz * 16) + border] = (r, g, b)
424 draw.ellipse((minx * -16 - 5 + border, h - minz * -16 - 6 + border, minx * -16 + 5 + border, h - minz * -16 + 4 + border), outline = origincolor)
426 font = ImageFont.load_default()
429 draw.text((24, 0), "X", font = font, fill = scalecolor)
430 draw.text((2, 24), "Z", font = font, fill = scalecolor)
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)
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)
442 for filename in os.listdir(path + "players"):
443 f = file(path + "players/" + filename)
444 lines = f.readlines()
448 p = string.split(line)
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)