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
17 # Requires Python Imaging Library: http://www.pythonware.com/products/pil/
19 # Some speed-up: ...lol, actually it slows it down.
20 #import psyco ; psyco.full()
21 #from psyco.classes import *
24 import Image, ImageDraw, ImageFont, ImageColor
45 return "%03X" % (i + 4096)
51 return "%04X" % (i + 65536)
65 opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=", "output=", "bgcolor=", "scalecolor=", "origincolor=", "playercolor=", "draworigin", "drawplayers", "drawscale"])
66 except getopt.GetoptError, err:
67 # print help information and exit:
68 print str(err) # will print something like "option -a not recognized"
83 sector_xmin = -1500 / 16
84 sector_xmax = 1500 / 16
85 sector_zmin = -1500 / 16
86 sector_zmax = 1500 / 16
89 if o in ("-h", "--help"):
92 elif o in ("-i", "--input"):
94 elif o in ("-o", "--output"):
96 elif o == "--bgcolor":
97 bgcolor = ImageColor.getrgb(a)
98 elif o == "--scalecolor":
99 scalecolor = ImageColor.getrgb(a)
100 elif o == "--playercolor":
101 playercolor = ImageColor.getrgb(a)
102 elif o == "--origincolor":
103 origincolor = ImageColor.getrgb(a)
104 elif o == "--drawscale":
107 elif o == "--drawplayers":
109 elif o == "--draworigin":
112 assert False, "unhandled option"
114 if path[-1:]!="/" and path[-1:]!="\\":
117 # Load color information for the blocks.
119 f = file("colors.txt")
121 values = string.split(line)
122 colors[int(values[0])] = (int(values[1]), int(values[2]), int(values[3]))
128 # List all sectors to memory and calculate the width and heigth of the resulting picture.
130 for filename in os.listdir(path + "sectors2"):
131 for filename2 in os.listdir(path + "sectors2/" + filename):
132 x = hex_to_int(filename)
133 z = hex_to_int(filename2)
134 if x < sector_xmin or x > sector_xmax:
136 if z < sector_zmin or z > sector_zmax:
143 for filename in os.listdir(path + "sectors"):
144 x = hex4_to_int(filename[:4])
145 z = hex4_to_int(filename[-4:])
146 if x < sector_xmin or x > sector_xmax:
148 if z < sector_zmin or z > sector_zmax:
160 w = (maxx - minx) * 16 + 16
161 h = (maxz - minz) * 16 + 16
163 print "w="+str(w)+" h="+str(h)
165 im = Image.new("RGB", (w + border, h + border), bgcolor)
166 draw = ImageDraw.Draw(im)
171 starttime = time.time()
173 # Go through all sectors.
174 for n in range(len(xlist)):
178 nowtime = time.time()
179 dtime = nowtime - starttime
181 n_per_second = 1.0 * n / dtime
182 except ZeroDivisionError:
184 if n_per_second != 0:
185 seconds_per_n = 1.0 / n_per_second
186 time_guess = seconds_per_n * len(xlist)
187 remaining_s = time_guess - dtime
188 remaining_minutes = int(remaining_s / 60)
189 remaining_s -= remaining_minutes * 60;
190 print("Processing sector "+str(n)+" of "+str(len(xlist))
191 +" ("+str(round(100.0*n/len(xlist), 1))+"%)"
192 +" (ETA: "+str(remaining_minutes)+"m "
193 +str(int(remaining_s))+"s)")
198 xhex = int_to_hex3(xpos)
199 zhex = int_to_hex3(zpos)
200 xhex4 = int_to_hex4(xpos)
201 zhex4 = int_to_hex4(zpos)
203 sector1 = xhex4.lower() + zhex4.lower()
204 sector2 = xhex.lower() + "/" + zhex.lower()
211 for filename in os.listdir(path + "sectors/" + sector1):
212 if(filename != "meta"):
213 pos = int(filename, 16)
221 if sectortype != "old":
223 for filename in os.listdir(path + "sectors2/" + sector2):
224 if(filename != "meta"):
225 pos = int(filename, 16)
238 # Make a list of pixels of the sector that are to be looked for.
243 pixellist.append((x, z))
246 # Go through the Y axis from top to bottom.
248 for ypos in reversed(ylist):
250 yhex = int_to_hex4(ypos)
253 if sectortype == "old":
254 filename = path + "sectors/" + sector1 + "/" + yhex.lower()
256 filename = path + "sectors2/" + sector2 + "/" + yhex.lower()
258 f = file(filename, "rb")
263 # Checking day and night differs -flag
264 if not ord(flags) & 2:
265 ylist2.append((ypos,filename))
269 dec_o = zlib.decompressobj()
271 mapdata = dec_o.decompress(f.read())
277 if(len(mapdata) < 4096):
278 print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + str(len(mapdata))
280 chunkxpos = xpos * 16
281 chunkypos = ypos * 16
282 chunkzpos = zpos * 16
283 for (x, z) in reversed(pixellist):
284 for y in reversed(range(16)):
285 datapos = x + y * 16 + z * 256
286 if(ord(mapdata[datapos]) != 254 and ord(mapdata[datapos]) in colors):
287 if(ord(mapdata[datapos]) == 2 or ord(mapdata[datapos]) == 9):
289 # Add dummy stuff for drawing sea without seabed
290 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, ord(mapdata[datapos]), water[(x, z)])
292 pixellist.remove((x, z))
293 # Memorize information on the type and height of the block and for drawing the picture.
294 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, ord(mapdata[datapos]), water[(x, z)])
296 elif(ord(mapdata[datapos]) != 254 and ord(mapdata[datapos]) not in colors):
297 print "strange block: " + xhex + "/" + zhex + "/" + yhex + " x: " + str(x) + " y: " + str(y) + " z: " + str(z) + " palikka: " + str(ord(mapdata[datapos]))
299 # After finding all the pixels in the sector, we can move on to the next sector without having to continue the Y axis.
300 if(len(pixellist) == 0):
303 if len(pixellist) > 0:
304 for (ypos, filename) in ylist2:
305 f = file(filename, "rb")
310 dec_o = zlib.decompressobj()
312 mapdata = dec_o.decompress(f.read())
318 if(len(mapdata) < 4096):
319 print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + str(len(mapdata))
321 chunkxpos = xpos * 16
322 chunkypos = ypos * 16
323 chunkzpos = zpos * 16
324 for (x, z) in reversed(pixellist):
325 for y in reversed(range(16)):
326 datapos = x + y * 16 + z * 256
327 if(ord(mapdata[datapos]) != 254 and ord(mapdata[datapos]) in colors):
328 if(ord(mapdata[datapos]) == 2 or ord(mapdata[datapos]) == 9):
330 # Add dummy stuff for drawing sea without seabed
331 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, ord(mapdata[datapos]), water[(x, z)])
333 pixellist.remove((x, z))
334 # Memorize information on the type and height of the block and for drawing the picture.
335 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, ord(mapdata[datapos]), water[(x, z)])
337 elif(ord(mapdata[datapos]) != 254 and ord(mapdata[datapos]) not in colors):
338 print "outo palikka: " + xhex + "/" + zhex + "/" + yhex + " x: " + str(x) + " y: " + str(y) + " z: " + str(z) + " palikka: " + str(ord(mapdata[datapos]))
340 # After finding all the pixels in the sector, we can move on to the next sector without having to continue the Y axis.
341 if(len(pixellist) == 0):
344 print "Drawing image"
345 # Drawing the picture
346 starttime = time.time()
348 for (x, z) in stuff.iterkeys():
350 nowtime = time.time()
351 dtime = nowtime - starttime
353 n_per_second = 1.0 * n / dtime
354 except ZeroDivisionError:
356 if n_per_second != 0:
358 seconds_per_n = 1.0 / n_per_second
359 time_guess = seconds_per_n * listlen
360 remaining_s = time_guess - dtime
361 remaining_minutes = int(remaining_s / 60)
362 remaining_s -= remaining_minutes * 60;
363 print("Drawing pixel "+str(n)+" of "+str(listlen)
364 +" ("+str(round(100.0*n/listlen, 1))+"%)"
365 +" (ETA: "+str(remaining_minutes)+"m "
366 +str(int(remaining_s))+"s)")
369 (r, g, b) = colors[stuff[(x,z)][1]]
370 # Comparing heights of a couple of adjacent blocks and changing brightness accordingly.
372 c1 = stuff[(x - 1, z)][1]
373 c2 = stuff[(x, z + 1)][1]
375 if c1 != 2 and c1 != 9 and c2 != 2 and c2 != 9 and c != 2 and c != 9:
376 y1 = stuff[(x - 1, z)][0]
377 y2 = stuff[(x, z + 1)][0]
380 d = ((y - y1) + (y - y2)) * 12
387 r = limit(r + d, 0, 255)
388 g = limit(g + d, 0, 255)
389 b = limit(b + d, 0, 255)
394 if(stuff[(x,z)][2] > 0):
395 r=int(r * .15 + colors[2][0] * .85)
396 g=int(g * .15 + colors[2][1] * .85)
397 b=int(b * .15 + colors[2][2] * .85)
399 impix[x - minx * 16 + border, h - 1 - (z - minz * 16) + border] = (r, g, b)
403 draw.ellipse((minx * -16 - 5 + border, h - minz * -16 - 6 + border, minx * -16 + 5 + border, h - minz * -16 + 4 + border), outline = origincolor)
405 font = ImageFont.load_default()
408 draw.text((24, 0), "X", font = font, fill = scalecolor)
409 draw.text((2, 24), "Z", font = font, fill = scalecolor)
411 for n in range(int(minx / -4) * -4, maxx, 4):
412 draw.text((minx * -16 + n * 16 + 2 + border, 0), str(n * 16), font = font, fill = scalecolor)
413 draw.line((minx * -16 + n * 16 + border, 0, minx * -16 + n * 16 + border, border - 1), fill = scalecolor)
415 for n in range(int(maxz / 4) * 4, minz, -4):
416 draw.text((2, h - 1 - (n * 16 - minz * 16) + border), str(n * 16), font = font, fill = scalecolor)
417 draw.line((0, h - 1 - (n * 16 - minz * 16) + border, border - 1, h - 1 - (n * 16 - minz * 16) + border), fill = scalecolor)
421 for filename in os.listdir(path + "players"):
422 f = file(path + "players/" + filename)
423 lines = f.readlines()
427 p = string.split(line)
430 print filename + ": name = " + name
431 if p[0] == "position":
432 position = string.split(p[2][1:-1], ",")
433 print filename + ": position = " + p[2]
434 if len(name) > 0 and len(position) == 3:
435 x=(int(float(position[0]) / 10 - minx * 16))
436 z=int(h - (float(position[2]) / 10 - minz * 16))
437 draw.ellipse((x - 2 + border, z - 2 + border, x + 2 + border, z + 2 + border), outline = playercolor)
438 draw.text((x + 2 + border, z + 2 + border), name, font = font, fill = playercolor)