]> git.lizzy.rs Git - dragonfireclient.git/blob - util/minetestmapper.py
added minetestmapper in utils/
[dragonfireclient.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
17 # Requires Python Imaging Library: http://www.pythonware.com/products/pil/
18
19 # Some speed-up: ...lol, actually it slows it down.
20 #import psyco ; psyco.full() 
21 #from psyco.classes import *
22
23 import zlib
24 import Image, ImageDraw, ImageFont, ImageColor
25 import os
26 import string
27 import time
28 import getopt
29 import sys
30
31 def hex_to_int(h):
32         i = int(h, 16)
33         if(i > 2047):
34                 i -= 4096
35         return i
36
37 def hex4_to_int(h):
38         i = int(h, 16)
39         if(i > 32767):
40                 i -= 65536
41         return i
42
43 def int_to_hex3(i):
44         if(i < 0):
45                 return "%03X" % (i + 4096)
46         else:
47                 return "%03X" % i
48
49 def int_to_hex4(i):
50         if(i < 0):
51                 return "%04X" % (i + 65536)
52         else:
53                 return "%04X" % i
54
55 def limit(i, l, h):
56         if(i > h):
57                 i = h
58         if(i < l):
59                 i = l
60         return i
61
62 def usage():
63         print "TODO: Help"
64 try:
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"
69         usage()
70         sys.exit(2)
71
72 path = "../world/"
73 output = "uloste.png"
74 border = 0
75 scalecolor = "black"
76 bgcolor = "white"
77 origincolor = "red"
78 playercolor = "red"
79 drawscale = False
80 drawplayers = False
81 draworigin = False
82
83 sector_xmin = -1500 / 16
84 sector_xmax = 1500 / 16
85 sector_zmin = -1500 / 16
86 sector_zmax = 1500 / 16
87
88 for o, a in opts:
89         if o in ("-h", "--help"):
90                 usage()
91                 sys.exit()
92         elif o in ("-i", "--input"):
93                 path = a
94         elif o in ("-o", "--output"):
95                 output = a
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":
105                 drawscale = True
106                 border = 40
107         elif o == "--drawplayers":
108                 drawplayers = True
109         elif o == "--draworigin":
110                 draworigin = True
111         else:
112                 assert False, "unhandled option"
113         
114 if path[-1:]!="/" and path[-1:]!="\\":
115         path = path + "/"
116
117 # Load color information for the blocks.
118 colors = {}
119 f = file("colors.txt")
120 for line in f:
121         values = string.split(line)
122         colors[int(values[0])] = (int(values[1]), int(values[2]), int(values[3]))
123 f.close()
124
125 xlist = []
126 zlist = []
127
128 # List all sectors to memory and calculate the width and heigth of the resulting picture.
129 try:
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:
135                                 continue
136                         if z < sector_zmin or z > sector_zmax:
137                                 continue
138                         xlist.append(x)
139                         zlist.append(z)
140 except OSError:
141         pass
142 try:
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:
147                         continue
148                 if z < sector_zmin or z > sector_zmax:
149                         continue
150                 xlist.append(x)
151                 zlist.append(z)
152 except OSError:
153         pass
154
155 minx = min(xlist)
156 minz = min(zlist)
157 maxx = max(xlist)
158 maxz = max(zlist)
159
160 w = (maxx - minx) * 16 + 16
161 h = (maxz - minz) * 16 + 16
162
163 print "w="+str(w)+" h="+str(h)
164
165 im = Image.new("RGB", (w + border, h + border), bgcolor)
166 draw = ImageDraw.Draw(im)
167 impix = im.load()
168
169 stuff = {}
170
171 starttime = time.time()
172
173 # Go through all sectors.
174 for n in range(len(xlist)):
175         #if n > 500:
176         #       break
177         if n % 200 == 0:
178                 nowtime = time.time()
179                 dtime = nowtime - starttime
180                 try:
181                         n_per_second = 1.0 * n / dtime
182                 except ZeroDivisionError:
183                         n_per_second = 0
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)")
194
195         xpos = xlist[n]
196         zpos = zlist[n]
197         
198         xhex = int_to_hex3(xpos)
199         zhex = int_to_hex3(zpos)
200         xhex4 = int_to_hex4(xpos)
201         zhex4 = int_to_hex4(zpos)
202         
203         sector1 = xhex4.lower() + zhex4.lower()
204         sector2 = xhex.lower() + "/" + zhex.lower()
205         
206         ylist = []
207         
208         sectortype = ""
209         
210         try:
211                 for filename in os.listdir(path + "sectors/" + sector1):
212                         if(filename != "meta"):
213                                 pos = int(filename, 16)
214                                 if(pos > 32767):
215                                         pos -= 65536
216                                 ylist.append(pos)
217                                 sectortype = "old"
218         except OSError:
219                 pass
220         
221         if sectortype != "old":
222                 try:
223                         for filename in os.listdir(path + "sectors2/" + sector2):
224                                 if(filename != "meta"):
225                                         pos = int(filename, 16)
226                                         if(pos > 32767):
227                                                 pos -= 65536
228                                         ylist.append(pos)
229                                         sectortype = "new"
230                 except OSError:
231                         pass
232         
233         if sectortype == "":
234                 continue
235
236         ylist.sort()
237         
238         # Make a list of pixels of the sector that are to be looked for.
239         pixellist = []
240         water = {}
241         for x in range(16):
242                 for z in range(16):
243                         pixellist.append((x, z))
244                         water[(x, z)] = 0
245         
246         # Go through the Y axis from top to bottom.
247         ylist2=[]
248         for ypos in reversed(ylist):
249                 
250                 yhex = int_to_hex4(ypos)
251
252                 filename = ""
253                 if sectortype == "old":
254                         filename = path + "sectors/" + sector1 + "/" + yhex.lower()
255                 else:
256                         filename = path + "sectors2/" + sector2 + "/" + yhex.lower()
257
258                 f = file(filename, "rb")
259
260                 version = f.read(1)
261                 flags = f.read(1)
262                 
263                 # Checking day and night differs -flag
264                 if not ord(flags) & 2:
265                         ylist2.append((ypos,filename))
266                         f.close()
267                         continue
268
269                 dec_o = zlib.decompressobj()
270                 try:
271                         mapdata = dec_o.decompress(f.read())
272                 except:
273                         mapdata = []
274                         
275                 f.close()
276                 
277                 if(len(mapdata) < 4096):
278                         print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + str(len(mapdata))
279                 else:
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):
288                                                         water[(x, z)] += 1
289                                                         # Add dummy stuff for drawing sea without seabed
290                                                         stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, ord(mapdata[datapos]), water[(x, z)])
291                                                 else:
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)])
295                                                         break
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]))
298                 
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):
301                         break
302         
303         if len(pixellist) > 0:
304                 for (ypos, filename) in ylist2:
305                         f = file(filename, "rb")
306
307                         version = f.read(1)
308                         flags = f.read(1)
309
310                         dec_o = zlib.decompressobj()
311                         try:
312                                 mapdata = dec_o.decompress(f.read())
313                         except:
314                                 mapdata = []
315                                 
316                         f.close()
317                         
318                         if(len(mapdata) < 4096):
319                                 print "bad: " + xhex + "/" + zhex + "/" + yhex + " " + str(len(mapdata))
320                         else:
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):
329                                                                 water[(x, z)] += 1
330                                                                 # Add dummy stuff for drawing sea without seabed
331                                                                 stuff[(chunkxpos + x, chunkzpos + z)] = (chunkypos + y, ord(mapdata[datapos]), water[(x, z)])
332                                                         else:
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)])
336                                                                 break
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]))
339                         
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):
342                                 break
343
344 print "Drawing image"
345 # Drawing the picture
346 starttime = time.time()
347 n = 0
348 for (x, z) in stuff.iterkeys():
349         if n % 500000 == 0:
350                 nowtime = time.time()
351                 dtime = nowtime - starttime
352                 try:
353                         n_per_second = 1.0 * n / dtime
354                 except ZeroDivisionError:
355                         n_per_second = 0
356                 if n_per_second != 0:
357                         listlen = len(stuff)
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)")
367         n += 1
368
369         (r, g, b) = colors[stuff[(x,z)][1]]
370         # Comparing heights of a couple of adjacent blocks and changing brightness accordingly.
371         try:
372                 c1 = stuff[(x - 1, z)][1]
373                 c2 = stuff[(x, z + 1)][1]
374                 c = stuff[(x, z)][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]
378                         y = stuff[(x, z)][0]
379                         
380                         d = ((y - y1) + (y - y2)) * 12
381                 else:
382                         d = 0
383                 
384                 if(d > 36):
385                         d = 36
386                         
387                 r = limit(r + d, 0, 255)
388                 g = limit(g + d, 0, 255)
389                 b = limit(b + d, 0, 255)
390         except:
391                 pass
392         
393         # Water
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)
398                 
399         impix[x - minx * 16 + border, h - 1 - (z - minz * 16) + border] = (r, g, b)
400
401
402 if draworigin:
403         draw.ellipse((minx * -16 - 5 + border, h - minz * -16 - 6 + border, minx * -16 + 5 + border, h - minz * -16 + 4 + border), outline = origincolor)
404
405 font = ImageFont.load_default()
406
407 if drawscale:
408         draw.text((24, 0), "X", font = font, fill = scalecolor)
409         draw.text((2, 24), "Z", font = font, fill = scalecolor)
410
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)
414
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)
418
419 if drawplayers:
420         try:
421                 for filename in os.listdir(path + "players"):
422                         f = file(path + "players/" + filename)
423                         lines = f.readlines()
424                         name=""
425                         position=[]
426                         for line in lines:
427                                 p = string.split(line)
428                                 if p[0] == "name":
429                                         name = p[2]
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)
439                         f.close()
440         except OSError:
441                 pass
442
443 print "Saving"
444 im.save(output)