2 # -*- coding: utf-8 -*-
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
17 # 2011-07-30: erlehmann: PEP 8 compliance.
19 # Requires Python Imaging Library: http://www.pythonware.com/products/pil/
21 # Some speed-up: ...lol, actually it slows it down.
22 #import psyco ; psyco.full()
23 #from psyco.classes import *
32 from PIL import Image, ImageDraw, ImageFont, ImageColor
34 CONTENT_WATER = [2, 9]
37 1: 0x800, # CONTENT_GRASS
38 4: 0x801, # CONTENT_TREE
39 5: 0x802, # CONTENT_LEAVES
40 6: 0x803, # CONTENT_GRASS_FOOTSTEPS
41 7: 0x804, # CONTENT_MESE
42 8: 0x805, # CONTENT_MUD
43 10: 0x806, # CONTENT_CLOUD
44 11: 0x807, # CONTENT_COALSTONE
45 12: 0x808, # CONTENT_WOOD
46 13: 0x809, # CONTENT_SAND
47 18: 0x80a, # CONTENT_COBBLE
48 19: 0x80b, # CONTENT_STEEL
49 20: 0x80c, # CONTENT_GLASS
50 22: 0x80d, # CONTENT_MOSSYCOBBLE
51 23: 0x80e, # CONTENT_GRAVEL
52 24: 0x80f, # CONTENT_SANDSTONE
53 25: 0x810, # CONTENT_CACTUS
54 26: 0x811, # CONTENT_BRICK
55 27: 0x812, # CONTENT_CLAY
56 28: 0x813, # CONTENT_PAPYRUS
57 29: 0x814} # CONTENT_BOOKSHELF
60 def __init__(self, stream):
64 # So you can use files also
65 if hasattr(self.stream, 'read'):
66 self.read = self.stream.read
69 return len(self.stream)
71 def read(self, length = None):
75 return self.stream[self.pos - length:self.pos]
95 return "%03X" % (i + 4096)
102 return "%04X" % (i + 65536)
107 def getBlockAsInteger(p):
108 return p[2]*16777216 + p[1]*4096 + p[0]
110 def unsignedToSigned(i, max_positive):
114 return i - 2*max_positive
116 def getIntegerAsBlock(i):
117 x = unsignedToSigned(i % 4096, 2048)
118 i = int((i - x) / 4096)
119 y = unsignedToSigned(i % 4096, 2048)
120 i = int((i - y) / 4096)
121 z = unsignedToSigned(i % 4096, 2048)
135 opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=",
136 "output=", "bgcolor=", "scalecolor=", "origincolor=",
137 "playercolor=", "draworigin", "drawplayers", "drawscale"])
138 except getopt.GetoptError, err:
139 # print help information and exit:
140 print str(err) # will print something like "option -a not recognized"
155 sector_xmin = -1500 / 16
156 sector_xmax = 1500 / 16
157 sector_zmin = -1500 / 16
158 sector_zmax = 1500 / 16
161 if o in ("-h", "--help"):
164 elif o in ("-i", "--input"):
166 elif o in ("-o", "--output"):
168 elif o == "--bgcolor":
169 bgcolor = ImageColor.getrgb(a)
170 elif o == "--scalecolor":
171 scalecolor = ImageColor.getrgb(a)
172 elif o == "--playercolor":
173 playercolor = ImageColor.getrgb(a)
174 elif o == "--origincolor":
175 origincolor = ImageColor.getrgb(a)
176 elif o == "--drawscale":
179 elif o == "--drawplayers":
181 elif o == "--draworigin":
184 assert False, "unhandled option"
186 if path[-1:] != "/" and path[-1:] != "\\":
189 # Load color information for the blocks.
192 f = file("colors.txt")
194 f = file(os.path.join(os.path.dirname(__file__), "colors.txt"))
196 values = string.split(line)
197 colors[int(values[0], 16)] = (
206 # List all sectors to memory and calculate the width and heigth of the
211 if os.path.exists(path + "map.sqlite"):
213 conn = sqlite3.connect(path + "map.sqlite")
216 cur.execute("SELECT `pos` FROM `blocks`")
222 x, y, z = getIntegerAsBlock(r[0])
224 if x < sector_xmin or x > sector_xmax:
226 if z < sector_zmin or z > sector_zmax:
232 if os.path.exists(path + "sectors2"):
233 for filename in os.listdir(path + "sectors2"):
234 for filename2 in os.listdir(path + "sectors2/" + filename):
235 x = hex_to_int(filename)
236 z = hex_to_int(filename2)
237 if x < sector_xmin or x > sector_xmax:
239 if z < sector_zmin or z > sector_zmax:
244 if os.path.exists(path + "sectors"):
245 for filename in os.listdir(path + "sectors"):
246 x = hex4_to_int(filename[:4])
247 z = hex4_to_int(filename[-4:])
248 if x < sector_xmin or x > sector_xmax:
250 if z < sector_zmin or z > sector_zmax:
256 xlist, zlist = zip(*sorted(set(zip(xlist, zlist))))
263 w = (maxx - minx) * 16 + 16
264 h = (maxz - minz) * 16 + 16
266 print "w=" + str(w) + " h=" + str(h)
268 im = Image.new("RGB", (w + border, h + border), bgcolor)
269 draw = ImageDraw.Draw(im)
274 starttime = time.time()
278 return d in [126, 127, 254]
281 def read_blocknum(mapdata, version, datapos):
283 if mapdata[datapos] < 0x80:
284 return mapdata[datapos]
286 return (mapdata[datapos] << 4) | (mapdata[datapos + 0x2000] >> 4)
287 elif 16 <= version < 20:
288 return TRANSLATION_TABLE.get(mapdata[datapos], mapdata[datapos])
290 raise Exception("Unsupported map format: " + str(version))
293 def read_mapdata(f, version, pixellist, water):
294 global stuff # oh my :-)
296 dec_o = zlib.decompressobj()
298 mapdata = array.array("B", dec_o.decompress(f.read()))
304 if(len(mapdata) < 4096):
305 print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + \
308 chunkxpos = xpos * 16
309 chunkypos = ypos * 16
310 chunkzpos = zpos * 16
313 for (x, z) in reversed(pixellist):
314 for y in reversed(range(16)):
315 datapos = x + y * 16 + z * 256
316 blocknum = read_blocknum(mapdata, version, datapos)
317 if not data_is_air(blocknum) and blocknum in colors:
318 if blocknum in CONTENT_WATER:
320 # Add dummy stuff for drawing sea without seabed
321 stuff[(chunkxpos + x, chunkzpos + z)] = (
322 chunkypos + y, blocknum, water[(x, z)])
324 pixellist.remove((x, z))
325 # Memorize information on the type and height of
326 # the block and for drawing the picture.
327 stuff[(chunkxpos + x, chunkzpos + z)] = (
328 chunkypos + y, blocknum, water[(x, z)])
330 elif not data_is_air(blocknum) and blocknum not in colors:
331 print "strange block: %s/%s/%s x: %d y: %d z: %d \
332 block id: %x" % (xhex, zhex, yhex, x, y, z, blocknum)
334 # Go through all sectors.
335 for n in range(len(xlist)):
339 nowtime = time.time()
340 dtime = nowtime - starttime
342 n_per_second = 1.0 * n / dtime
343 except ZeroDivisionError:
345 if n_per_second != 0:
346 seconds_per_n = 1.0 / n_per_second
347 time_guess = seconds_per_n * len(xlist)
348 remaining_s = time_guess - dtime
349 remaining_minutes = int(remaining_s / 60)
350 remaining_s -= remaining_minutes * 60
351 print("Processing sector " + str(n) + " of " + str(len(xlist))
352 + " (" + str(round(100.0 * n / len(xlist), 1)) + "%)"
353 + " (ETA: " + str(remaining_minutes) + "m "
354 + str(int(remaining_s)) + "s)")
359 xhex = int_to_hex3(xpos)
360 zhex = int_to_hex3(zpos)
361 xhex4 = int_to_hex4(xpos)
362 zhex4 = int_to_hex4(zpos)
364 sector1 = xhex4.lower() + zhex4.lower()
365 sector2 = xhex.lower() + "/" + zhex.lower()
372 ps = getBlockAsInteger((xpos, 0, zpos))
373 cur.execute("SELECT `pos` FROM `blocks` WHERE `pos`>=? AND `pos`<?", (ps, ps + 4096))
378 pos = getIntegerAsBlock(r[0])[1]
380 sectortype = "sqlite"
382 for filename in os.listdir(path + "sectors/" + sector1):
383 if(filename != "meta"):
384 pos = int(filename, 16)
394 for filename in os.listdir(path + "sectors2/" + sector2):
395 if(filename != "meta"):
396 pos = int(filename, 16)
408 ylist = sorted(set(ylist))
410 # Make a list of pixels of the sector that are to be looked for.
415 pixellist.append((x, z))
418 # Go through the Y axis from top to bottom.
420 for ypos in reversed(ylist):
422 yhex = int_to_hex4(ypos)
425 if sectortype == "sqlite":
426 ps = getBlockAsInteger((xpos, ypos, zpos))
427 cur.execute("SELECT `data` FROM `blocks` WHERE `pos`==? LIMIT 1", (ps,))
433 if sectortype == "old":
434 filename = path + "sectors/" + sector1 + "/" + yhex.lower()
436 filename = path + "sectors2/" + sector2 + "/" + yhex.lower()
438 f = file(filename, "rb")
440 # Let's just memorize these even though it's not really necessary.
441 version = ord(f.read(1))
444 # Checking day and night differs -flag
445 if not ord(flags) & 2:
446 ylist2.append((ypos, filename))
450 read_mapdata(f, version, pixellist, water)
452 # After finding all the pixels in the sector, we can move on to
453 # the next sector without having to continue the Y axis.
454 if(len(pixellist) == 0):
457 if len(pixellist) > 0:
458 for (ypos, filename) in ylist2:
459 ps = getBlockAsInteger((xpos, ypos, zpos))
460 cur.execute("SELECT `data` FROM `blocks` WHERE `pos`==? LIMIT 1", (ps,))
466 version = ord(f.read(1))
469 read_mapdata(f, version, pixellist, water)
471 # After finding all the pixels in the sector, we can move on
472 # to the next sector without having to continue the Y axis.
473 if(len(pixellist) == 0):
476 print "Drawing image"
477 # Drawing the picture
478 starttime = time.time()
480 for (x, z) in stuff.iterkeys():
482 nowtime = time.time()
483 dtime = nowtime - starttime
485 n_per_second = 1.0 * n / dtime
486 except ZeroDivisionError:
488 if n_per_second != 0:
490 seconds_per_n = 1.0 / n_per_second
491 time_guess = seconds_per_n * listlen
492 remaining_s = time_guess - dtime
493 remaining_minutes = int(remaining_s / 60)
494 remaining_s -= remaining_minutes * 60
495 print("Drawing pixel " + str(n) + " of " + str(listlen)
496 + " (" + str(round(100.0 * n / listlen, 1)) + "%)"
497 + " (ETA: " + str(remaining_minutes) + "m "
498 + str(int(remaining_s)) + "s)")
501 (r, g, b) = colors[stuff[(x, z)][1]]
502 # Comparing heights of a couple of adjacent blocks and changing
503 # brightness accordingly.
505 c1 = stuff[(x - 1, z)][1]
506 c2 = stuff[(x, z + 1)][1]
508 if c1 not in CONTENT_WATER and c2 not in CONTENT_WATER and \
509 c not in CONTENT_WATER:
510 y1 = stuff[(x - 1, z)][0]
511 y2 = stuff[(x, z + 1)][0]
514 d = ((y - y1) + (y - y2)) * 12
521 r = limit(r + d, 0, 255)
522 g = limit(g + d, 0, 255)
523 b = limit(b + d, 0, 255)
528 if(stuff[(x, z)][2] > 0):
529 r = int(r * .15 + colors[2][0] * .85)
530 g = int(g * .15 + colors[2][1] * .85)
531 b = int(b * .15 + colors[2][2] * .85)
533 impix[x - minx * 16 + border, h - 1 - (z - minz * 16) + border] = (r, g, b)
537 draw.ellipse((minx * -16 - 5 + border, h - minz * -16 - 6 + border,
538 minx * -16 + 5 + border, h - minz * -16 + 4 + border),
541 font = ImageFont.load_default()
544 draw.text((24, 0), "X", font=font, fill=scalecolor)
545 draw.text((2, 24), "Z", font=font, fill=scalecolor)
547 for n in range(int(minx / -4) * -4, maxx, 4):
548 draw.text((minx * -16 + n * 16 + 2 + border, 0), str(n * 16),
549 font=font, fill=scalecolor)
550 draw.line((minx * -16 + n * 16 + border, 0,
551 minx * -16 + n * 16 + border, border - 1), fill=scalecolor)
553 for n in range(int(maxz / 4) * 4, minz, -4):
554 draw.text((2, h - 1 - (n * 16 - minz * 16) + border), str(n * 16),
555 font=font, fill=scalecolor)
556 draw.line((0, h - 1 - (n * 16 - minz * 16) + border, border - 1,
557 h - 1 - (n * 16 - minz * 16) + border), fill=scalecolor)
561 for filename in os.listdir(path + "players"):
562 f = file(path + "players/" + filename)
563 lines = f.readlines()
567 p = string.split(line)
570 print filename + ": name = " + name
571 if p[0] == "position":
572 position = string.split(p[2][1:-1], ",")
573 print filename + ": position = " + p[2]
574 if len(name) > 0 and len(position) == 3:
575 x = (int(float(position[0]) / 10 - minx * 16))
576 z = int(h - (float(position[2]) / 10 - minz * 16))
577 draw.ellipse((x - 2 + border, z - 2 + border,
578 x + 2 + border, z + 2 + border), outline=playercolor)
579 draw.text((x + 2 + border, z + 2 + border), name,
580 font=font, fill=playercolor)