44import numpy
55from tinterface import *
66from concurrent .futures import ThreadPoolExecutor
7- minimap_limits = 400 ,300
7+ minimap_limits = 0.4 ,0.2
8+ defaultres = [1024 , 768 ]
89
910
1011
@@ -89,6 +90,19 @@ def load_content(imagename):
8990 return tex , walltex , npc_tex , air , gborder , rborder , gfill , rfill
9091
9192
93+ def adjust_minimap (target_rel_size , resolution , base_image ):
94+ mi_size = base_image .get_size ()
95+ mi_scale = 1
96+ scaled_limits = [x * y for x ,y in zip (minimap_limits , resolution )]
97+ if mi_size [0 ] > scaled_limits [0 ]:
98+ mi_scale = 1 / (mi_size [0 ]/ scaled_limits [0 ])
99+ if mi_size [1 ] > scaled_limits [1 ]:
100+ mi_scale = min (1 / (mi_size [1 ]/ scaled_limits [1 ]), mi_scale )
101+ if mi_scale != 1 :
102+ print ("Scaling minimap with factor " + str (mi_scale ))
103+ mapimage = pygame .transform .rotozoom (base_image , 0 , mi_scale )
104+
105+ return mapimage , mi_scale
92106
93107def run (header , path , mapping , data , mappingfolder = None ):
94108 header , pos = data
@@ -98,16 +112,8 @@ def run(header, path, mapping, data, mappingfolder = None):
98112 texture_loader = threadpool .submit (load )
99113 try :
100114 imageloc = get_myterraria () / "WorldImages" / path .with_suffix ('.png' ).name
101- mapimage = pygame .image .load (str (imageloc ))
102- mi_size = mapimage .get_size ()
103- mi_scale = 1
104- if mi_size [0 ] > minimap_limits [0 ]:
105- mi_scale = 1 / (mi_size [0 ]/ minimap_limits [0 ])
106- if mi_size [1 ] > minimap_limits [1 ]:
107- mi_scale = min (1 / (mi_size [1 ]/ minimap_limits [1 ]), mi_scale )
108- print ("Scaling minimap with factor " + str (mi_scale ))
109- if mi_scale != 1 :
110- mapimage = pygame .transform .rotozoom (mapimage , 0 , mi_scale )
115+ base_image = pygame .image .load (str (imageloc ))
116+ mapimage , mi_scale = adjust_minimap (minimap_limits , defaultres , base_image )
111117 mi_size = mapimage .get_size ()
112118
113119 except :
@@ -121,7 +127,7 @@ def run(header, path, mapping, data, mappingfolder = None):
121127 f .seek (pos )
122128 get = get_tile_buffered_12_masked if header ["version" ] > 100 else get_tile_buffered
123129 pygame .display .set_caption ("Loading World.." )
124- loadbar_width = minimap_limits [ 0 ]
130+ loadbar_width = 200
125131 if mapimage :
126132 if mi_size [0 ] > 200 :loadbar_width = mi_size [0 ]
127133 surface = pygame .display .set_mode ((loadbar_width , 20 + mi_size [1 ]))
@@ -132,9 +138,7 @@ def run(header, path, mapping, data, mappingfolder = None):
132138 if not skip :
133139 print ("loading and converting world data" )
134140
135- b = [0 ]
136141 rect = pygame .Rect (0 , 0 , 0 , 20 )
137- nw = 0
138142 tup = (rect ,)
139143 tiles = numpy .empty ((header ["width" ], header ["height" ]), dtype = tuple )
140144 w , h = header ["width" ], header ["height" ]
@@ -144,9 +148,8 @@ def run(header, path, mapping, data, mappingfolder = None):
144148 data , b = get (f )
145149 tiles [xi , yi :yi + b ] = (data ,)* b
146150 yi += b
147- nw = int (minimap_limits [0 ] * xi / w )
148- if nw != rect .w :
149- rect .w = nw
151+ if xi % 16 == 0 :
152+ rect .w = int (xi * loadbar_width / w )
150153 pygame .draw .rect (surface , (200 , 200 , 200 ), rect )
151154 pygame .display .update (tup )
152155
@@ -156,21 +159,8 @@ def run(header, path, mapping, data, mappingfolder = None):
156159 import sys
157160 sys .exit ()
158161
159- #chests = [get_chest(f) for x in range(1000)]
160- #signs = [get_sign(f) for x in range(1000)]
161- #print (chests)
162162 npcs = []
163- #while 1:
164- # npc = get_npc(f)
165- # if not npc: break
166- # else: npcs.append(npc)
167- #names = get_npc_names(f)
168- #print (names, npcs)
169- #trail = get_trail(f)
170- #if trail[0] and trail[1] == header["name"] and trail[2] == header["ID"]:
171- # print ("World Signature test passed")
172- #else:
173- # print ("World Signature test failed, information possibly corrupted")
163+
174164 f .close ()
175165
176166 rmap = numpy .random .randint (3 , size = (header ["width" ], header ["height" ]))
@@ -194,7 +184,7 @@ def run(header, path, mapping, data, mappingfolder = None):
194184 area = [1600 , 1600 ]
195185 os .chdir (".." )
196186 else :
197- res = [ 1024 , 768 ]
187+ res = list ( defaultres )
198188 dis = pygame .display .set_mode (res , pygame .RESIZABLE )
199189 s = pygame .surface .Surface (res )
200190 def relmove (rel ):
@@ -263,6 +253,8 @@ def relmove(rel):
263253 res = event .size
264254 dis = pygame .display .set_mode (res , pygame .RESIZABLE )
265255 s = pygame .surface .Surface (res )
256+ mapimage , mi_scale = adjust_minimap (minimap_limits , res , base_image )
257+ mi_size = mapimage .get_size ()
266258 dirty .append (pygame .rect .Rect (0 , 0 , res [0 ], res [1 ]))
267259 elif event .type == pygame .MOUSEBUTTONDOWN :
268260 if mapimage and event .button == 1 :
@@ -333,10 +325,8 @@ def relmove(rel):
333325 return
334326
335327 if (mx + 1 ) * area [0 ] > wi and not (mx * area [0 ] > wi ):
336-
337328 res [0 ] = - mx * area [0 ] + wi
338329 if (my + 1 ) * area [1 ] > he and not (my * area [1 ] > he ):
339-
340330 res [1 ] = - my * area [1 ] + he
341331
342332 dirty = [pygame .rect .Rect (0 , 0 , res [0 ], res [1 ])]
@@ -349,7 +339,7 @@ def relmove(rel):
349339 dirty = []
350340 dis .blit (s , (0 ,0 ))
351341 if mapimage :
352- dis .blit (mapimage , (res [0 ]- mi_size [0 ], 1 ))
342+ dis .blit (mapimage , (res [0 ]- mi_size [0 ], 0 ))
353343 #draw minimap viewport borders:
354344 bpos = pos [0 ]// 16 , pos [1 ]// 16
355345 topleft = bpos [0 ]* mi_scale + res [0 ]- mi_size [0 ], bpos [1 ]* mi_scale
@@ -363,14 +353,14 @@ def relmove(rel):
363353
364354
365355if __name__ == "__main__" :
366- worlds = get_worlds (False )
356+ worlds = list ( get_worlds (False ) )
367357 b = {}
368358 x = 0
369359 for w in worlds :
370360 with w .open ( "rb" ) as f :
371361 header = get_header (f )[0 ]
372362 b [w ] = header , f .tell ()
373- name = header ["name" ]
363+ name = header ["name" ]. decode ()
374364
375365 print (x , ":" , name )
376366 x += 1
@@ -381,4 +371,4 @@ def relmove(rel):
381371 image = True
382372 else :
383373 image = False
384- run (os . path . join ( path , world ), image , b [world ])
374+ run (str ( world ), image , b [world ])
0 commit comments