All pastes #61474 Raw Edit

Something

public python v1 · immutable
#61474 ·published 2006-06-03 22:41 UTC
rendered paste body
#!/usr/bin/pythonimport Image########################################################################################################## class for handling geodata# needs geodata file as param# new_geodata = geodata('filename-without-.dat')#########################################################################################################class geodata:    ####################################################################################################    #    # writes some_string to the logfile and then a linebreak \n    #    ####################################################################################################    def _writel(self,some_string):        if self.dump_log:            self.log_file.write(str(some_string)+'\n')        else:            print(str(some_string))        return 0    ####################################################################################################    #    # writes some_string to the logfile    #    ####################################################################################################    def _write(self,some_string):        if self.dump_log:            self.log_file.write(str(some_string))        else:            print(str(some_string))        return 0    ####################################################################################################    #    # converts bytes hex values    #    ####################################################################################################    def _convert_to_hex(self,some_byte):        if len(some_byte)==0:            return 'FF'        charval = ord(some_byte)        hexval = hex(charval)        hexstrlen = len(hexval)        startpos = hexstrlen - 2        if hexval[startpos] == 'x':            startpos += 1            hexval = '0'+hexval[startpos]        else:            hexval = hexval[startpos:hexstrlen]        return hexval    ####################################################################################################    #    # test if z is > maxz if yes set maxz = z    #    ####################################################################################################    def _maxz(self,z=None):        if z:            # getting max z            if z!=0:                if self.maxz == None:                    self.maxz = z                else:                    if self.maxz < z:                        self.maxz = z        return self.maxz    ####################################################################################################    #    # test if z is < minz if yes set minz = z    #    ####################################################################################################    def _minz(self,z=None):        if z:            # getting min z            if self.minz == None:                self.minz = z            else:                if self.minz > z:                    self.minz = z        return self.minz                    ####################################################################################################    #    # getz z value from file    #    ####################################################################################################    def _getz(self):        # read bytes        z_2 = self._convert_to_hex(self.conv_file.read(1))        z_1 = self._convert_to_hex(self.conv_file.read(1))        # converts to int                                                                        z = int(z_1+z_2,16)        # get the real z1        if z > 55000:            z = z - 65336        # checking min/max z        self._minz(z)        self._maxz(z)        # return value        return z            ####################################################################################################    #    # block 0000 handler    # z1 min-z z2 max-z    # if z1 != z2 then its a water zone else its a floor    #    ####################################################################################################    def _handle_block0000(self):        # getting the bytes for z1/z2        z1 = self._getz()        z2 = self._getz()        # check if water -- debug output        if z1!=z2:            self._writel(' |- block0000 - Water from ocean floor %s to surface %s' %(str(z1),str(z2)))        else:            self._writel(' |- block0000 - Floor %s ' %(str(z1)))        return z1,z2    ####################################################################################################    #    # block 0040 handler    #    ####################################################################################################    def _handle_block0040(self):        self._write(" |- block0040 ")        # loop 64 times        return_data = []        for item in range(0,64):            z = self._getz() / 2             # debug output            self._write( str(z)+ ' ')            return_data.append(z)        self._writel('')        return return_data    ####################################################################################################    #    # block 0041 handler    #    ####################################################################################################    def _handle_block0041(self,header):        return_data_sub = []        return_data = []        self._writel(" |- block00"+ header +" ")        # loop 64 times        for item in range(0,64):            # get count of layers            layercount = self._getz()            return_data_sub.append(layercount)            # debug output            self._write(' |-- layercount ' + str(layercount) + ' -- ')            # loop layercount times            if layercount > 20: # whats max layercount ?                z = layercount / 2            else:                for i in range(0,layercount):                    z = self._getz() / 2                    return_data_sub.append(z)                    # debug output                    self._write(z)                    self._write(' ')            return_data.append(return_data_sub)            self._writel('')        return return_data    ####################################################################################################    #    # Decodes the geodata    #    ####################################################################################################    def decode(self):        # open files        self.conv_file = open(self.filename+'.dat','rb')        self.log_file  = open(self.filename+'.log','wb')        # the image        self.geoimage = Image.new('RGB', (2048,2048), (92390))         # will be used soon        self.data_all = {}        # we dont need header for now        self.conv_file.seek(18)        data=[]        # make x loop        for x in range(0,2048,8):            # make y loop            for y in range(0,2048,8):                # debug output                self._writel('x' + str(x) + ' y:' +str(y))                # reads data header                block1 = self.conv_file.read(1)                block2 = self.conv_file.read(1)                block1_hex = self._convert_to_hex(block1)                # handles the data                if block1_hex   == 'ff':                    self._writel("error")                elif block1_hex == '00':                    # get data                    z1,z2 = self._handle_block0000()                    color_value = z1+50000                    for s_x in range(0,8):                        for s_y in range(0,8):                            self.geoimage.putpixel((x+s_x,y+s_y),color_value)                    # check for water                    if z1==z2:                        data.append([x,y,z1,'F'])                    else:                        for item in range(z1,z2,32):                            data.append([x,y,item,'W'])                elif block1_hex == '40':                    z_data = self._handle_block0040()                    counter = 0                    for s_x in range(0,8):                        for s_y in range(0,8):                            color_value= z_data[counter]+50000                            counter+=1                                                        self.geoimage.putpixel((x+s_x,y+s_y),color_value)                                    elif int(block1_hex,16)>=64 and int(block1_hex,16)<=121:                    z_data = self._handle_block0041(block1_hex)                    counter = 0                    for s_x in range(0,8):                        for s_y in range(0,8):                            count=z_data[counter][0]                            counter+=1                                                        data = z_data[s_x*s_y]                            color_value = max(data[1:count+1])+50000                            self.geoimage.putpixel((x+s_x,y+s_y),color_value)                                    else: self._writel('block00' + block1_hex + ' ???')        # self._writel(str(data))        # debug output        self._writel(' |-- minz = %s' %(str(self._minz())))        self._writel(' |-- maxz = %s' %(str(self._maxz())))        # close files        self.conv_file.close()        self.log_file.close()        self.geoimage.save(self.filename+'.png')        return 'foobar'    def __init__(self,filename,log=True,image=True):        # init basic vars        self.dump_log=log        self.dump_image=image        self.filename=filename        self.minz=None        self.maxz=None########################################################################################################## Main Function#########################################################################################################field_of_silence = geodata('22_23_conv')field_of_silence.decode()#giants_cave = geodata('25_19_conv')#giants_cave.decode()#tower_of_insolence = geodata('23_18_conv')#tower_of_insolence.decode()