def Save(self, filename, **kwargs): self.filename = filename self.tileTypes = [] examined = [] hashMap = {} self.typeMap = {} self.type2TID = {} self.instances = [] self.serialize_cleanly = kwargs.get('clean', True) self.dump_inherited = kwargs.get('inherited', False) # Preprocess and assign IDs. start = perf_counter() idlen = 0 it = self.map.Locations() lz = -1 last_str = None start = perf_counter() maxid = 0 for tile in it: # print(str(tile)) if lz != it.z: if start: self.log.info(' -> Took {}'.format(getElapsed(start))) self.log.info(' * Consolidating level {}...'.format(it.z + 1)) start = perf_counter() lz = it.z strt = tile.GetHash() # str(tile) dbg = False # if strt == 'e18826df83665059358c177f43f6ac72': # dbg=True if last_str == strt: # if dbg: print('Continuing: Same as last examined.') continue if strt in examined: # if dbg: print('Continuing: Hash already examined.') last_str = strt continue # tile.ID = len(examined) examined += [strt] tid = self.GetTID(tile) if tile.origID=='aaa': self.log.info('{} -> {}'.format(tile.origID,tid)) if tid in self.typeMap: if hashMap.get(strt, None) == tid: if dbg: self.log.debug('Continuing: TID {} in typeMap, hashes match.'.format(tid)) continue tile.origID = '' tid = len(self.typeMap) while tid in self.typeMap: tid += 1 self.log.info('{} assigned to new TID {}'.format(strt,tid)) maxid = max(maxid, tid) self.typeMap[tid] = (strt, self.SerializeTile(tile)) last_str = strt hashMap[strt] = tid self.log.info(' * Preprocessing completed in {}'.format(getElapsed(start))) idlen = len(self.ID2String(maxid)) tmpfile = filename + '.tmp' self.log.info('Opening {} for write...'.format(tmpfile)) start = perf_counter() with open(tmpfile, 'w') as f: for tid in sorted(self.typeMap.keys()): stid = self.ID2String(tid, idlen) strt, serdata = self.typeMap[tid] if serdata == '(/turf/space,/area)': self.log.debug('{} -> {}'.format(tid,stid)) f.write('"{}" = {}\n'.format(stid, serdata)) self.type2TID[strt] = stid self.log.info(' Wrote types in {}...'.format(getElapsed(start))) lap = perf_counter() for z in range(len(self.map.zLevels)): self.log.debug(' Writing z={}...'.format(z)) f.write('\n(1,1,{0}) = {{"\n'.format(z + 1)) zlevel = self.map.zLevels[z] for y in range(zlevel.height): for x in range(zlevel.width): tile = self.map.GetTileAt(x, y, z) thash = tile.GetHash() f.write(self.type2TID[thash]) f.write("\n") f.write('"}\n') self.log.info(' Wrote tiles in {}...'.format(getElapsed(lap))) if os.path.isfile(filename): os.remove(filename) os.rename(tmpfile, filename) self.log.info('-> {} in {}'.format(filename, getElapsed(start)))
def Save(self, filename, **kwargs): self.filename = filename self.tileTypes = [] examined = [] hashMap = {} self.typeMap = {} self.type2TID = {} self.instances = [] self.serialize_cleanly = kwargs.get('clean', True) self.dump_inherited = kwargs.get('inherited', False) # Preprocess and assign IDs. start = clock() idlen = 0 it = self.map.Tiles() lz = -1 last_str = None start = None maxid = 0 for tile in it: # print(str(tile)) if lz != it.z: if start: print(' -> Took {}'.format(getElapsed(start))) print(' * Consolidating level {}...'.format(it.z + 1)) start = clock() lz = it.z strt = tile.GetHash() # str(tile) dbg = False # if strt == 'e18826df83665059358c177f43f6ac72': # dbg=True if last_str == strt: # if dbg: print('Continuing: Same as last examined.') continue if strt in examined: # if dbg: print('Continuing: Hash already examined.') last_str = strt continue # tile.ID = len(examined) examined += [strt] tid = self.GetTID(tile) if tid in self.typeMap: if hashMap.get(strt, None) == tid: if dbg: print('Continuing: TID {} in typeMap, hashes match.'.format(tid)) continue tile.origID = '' tid = len(self.typeMap) while tid in self.typeMap: tid += 1 # print('{} assigned to new TID {}'.format(strt,tid)) maxid = max(maxid, tid) self.typeMap[tid] = (strt, self.SerializeTile(tile)) last_str = strt hashMap[strt] = tid print(' * Preprocessing completed in {}'.format(getElapsed(start))) idlen = len(self.ID2String(maxid)) tmpfile = filename + '.tmp' print('Opening {} for write...'.format(tmpfile)) start = clock() with open(tmpfile, 'w') as f: for tid in sorted(self.typeMap.keys()): stid = self.ID2String(tid, idlen) strt, serdata = self.typeMap[tid] f.write('"{}" = {}\n'.format(stid, serdata)) self.type2TID[strt] = stid print(' Wrote types in {}...'.format(getElapsed(start))) lap = clock() for z in xrange(len(self.map.zLevels)): print(' Writing z={}...'.format(z)) f.write('\n(1,1,{0}) = {{"\n'.format(z + 1)) zlevel = self.map.zLevels[z] for y in xrange(zlevel.height): for x in xrange(zlevel.width): tile = self.map.GetTileAt(x, y, z) thash = tile.GetHash() f.write(self.type2TID[thash]) f.write("\n") f.write('"}\n') print(' Wrote tiles in {}...'.format(getElapsed(lap))) if os.path.isfile(filename): os.remove(filename) os.rename(tmpfile, filename) print('-> {} in {}'.format(filename, getElapsed(start)))
def consumeTileMap(self, f): zLevel = None y = 0 z = 0 inZLevel = False width = 0 height = 0 while True: self.lineNumber += 1 line = f.readline() if line == '': return log_prefix="{}:{}: ".format(self.filename,self.lineNumber) # (1,1,1) = {" if line.startswith('('): coordChunk = line[1:line.index(')')].split(',') # print(repr(coordChunk)) z = int(coordChunk[2]) inZLevel = True y = 0 width = 0 height = 0 start = perf_counter() #print(log_prefix+' START z={}'.format(z)) continue if line.strip() == '"}': zLevel.initial_load=False inZLevel = False if height == 0: height = y # self.map.zLevels[z] = zLevel zLevel = None self.log.info(' * Added map layer {0} ({1}x{2}, {3})'.format(z, height, width, getElapsed(start))) continue if inZLevel: if zLevel is None: zLevel = self.map.CreateZLevel(height, width) # , z-1) zLevel.initial_load=True #print(log_prefix+' CREATED z={}'.format(z)) if width == 0: width = len(line) / self.idlen self.log.debug('Width detected as {}.'.format(width)) zLevel.Resize(width, width) if width > 255: self.log.warn("%sLine is {} blocks long!", log_prefix, width) x = 0 for chunk in chunker(line.strip(), self.idlen): chunk = ''.join(chunk) tid = self.oldID2NewID[chunk] #if tid == 1: #print('[{},{}] Chunk: {}, tid: {}'.format(x,y,chunk,tid)) zLevel.SetTileID(x, y, tid) x += 1 y += 1
def consumeTileMap(self, f): zLevel = None y = 0 z = 0 inZLevel = False width = 0 height = 0 while True: self.lineNumber += 1 line = f.readline() if line == '': return log_prefix="{}:{}: ".format(self.filename,self.lineNumber) # (1,1,1) = {" if line.startswith('('): coordChunk = line[1:line.index(')')].split(',') # print(repr(coordChunk)) z = int(coordChunk[2]) inZLevel = True y = 0 width = 0 height = 0 start = clock() #print(log_prefix+' START z={}'.format(z)) continue if line.strip() == '"}': zLevel.initial_load=False inZLevel = False if height == 0: height = y # self.map.zLevels[z] = zLevel zLevel = None self.log.info(' * Added map layer {0} ({1}x{2}, {3})'.format(z, height, width, getElapsed(start))) continue if inZLevel: if zLevel is None: zLevel = self.map.CreateZLevel(height, width) # , z-1) zLevel.initial_load=True #print(log_prefix+' CREATED z={}'.format(z)) if width == 0: width = len(line) / self.idlen self.log.debug('Width detected as {}.'.format(width)) zLevel.Resize(width, width) if width > 255: self.log.warn("%sLine is {} blocks long!", log_prefix, width) x = 0 for chunk in chunker(line.strip(), self.idlen): chunk = ''.join(chunk) tid = self.oldID2NewID[chunk] #if tid == 1: #print('[{},{}] Chunk: {}, tid: {}'.format(x,y,chunk,tid)) zLevel.SetTileID(x, y, tid) x += 1 y += 1