All pastes #61194 Raw Edit

Something

public text v1 · immutable
#61194 ·published 2006-06-02 21:15 UTC
rendered paste body
#!/usr/bin/python

import win32file

class geodata:
########################################################################################################
#
# class for handling geodata
# needs geodata file as param
# new_geodata = geodata('filename-without-.dat')
#
########################################################################################################

    def _writel(self,some_string):
    ####################################################################################################
    #
    # writes some_string to the logfile
    #
    ####################################################################################################
        self.log_file.write(str(some_string)+'\n')
        return 0

    def _write(self,some_string):
    ####################################################################################################
    #
    # writes some_string to the logfile
    #
    ####################################################################################################
        self.log_file.write(str(some_string))
        return 0


    def _convert_to_hex(self,some_byte):
    ####################################################################################################
    #
    # converts bytes hex values
    #
    ####################################################################################################
        if len(some_byte)==0:
            return '00'
        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


    def _setmaxz(self,z):
        # getting max z
        if z!=0:
            if self.maxz == None:
                self.maxz = z
            else:
                if self.maxz < z:
                    self.maxz = z

    def _setminz(self,z):
        # getting max z
        if self.minz == None:
            self.minz = z
        else:
            if self.minz > z:
                self.minz = z
                

    def _handle_block0000(self):
    ####################################################################################################
    #
    # block 0000 handler
    # z1 min-z z2 max-z <-- ????????? IF SO 100% i could make some things very faster and smaller
    # if z1 != z2 then its a water zone else its a floor
    #
    ####################################################################################################
        # getting the bytes for z1/z2
        z1_2 = self._convert_to_hex(self.conv_file.read(1))
        z1_1 = self._convert_to_hex(self.conv_file.read(1))
        z2_2 = self._convert_to_hex(self.conv_file.read(1))
        z2_1 = self._convert_to_hex(self.conv_file.read(1))                                   
                                                                        
        # z1 = first 2 bytes / z2 = next 2 bytes
        z1 = int(z1_1+z1_2,16)
        z2 = int(z2_1+z2_2,16)

        # get the real z1
        if z1 > 55000:
            z1 = z1 - 65336

        #get the real z2
        if z2 > 55000:
            z2 = z2 - 65336

        # getting min/max z
        self._setminz(z1)
        self._setmaxz(z2)

        # returning realz1,realz2
        if z1!=z2:
            self._writel(' |- block0000 - Water from ocean floor ' + str(z1) + ' to surface ' + str(z2))
        else:
            self._writel(' |- block0000 - Floor %s ' %(str(z1))) #,str(z2)))            
        return z1,z2


    def _handle_block0040(self):
    ####################################################################################################
    #
    # block 0040 handler
    #
    ####################################################################################################
        self._write(" |- block0040 ")
        for item in range(0,64):
            z1_2 = self.conv_file.read(1)
            z1_1 = self.conv_file.read(1)
            z = self._convert_to_hex(z1_1) + self._convert_to_hex(z1_2)

            z = int(z,16) / 2
            
            # get the real z1
            if z > 55000:
                z = z - 65336

            # getting min/max z
            self._setminz(z)
            self._setmaxz(z)
            
            self._write( str(z)+ ' ')
        self._writel('')
        return 0


    def _handle_block0041(self,header):
    ####################################################################################################
    #
    # block 0041 handler
    #
    ####################################################################################################
        self._write(" |- block00"+ header +" ")
        z1 = self.conv_file.read(1)
        z2 = self.conv_file.read(1)
        for item in range(0,64):
            #layer = 0
            z1 = self.conv_file.read(1)
            z2 = self.conv_file.read(1)
            self._write(self._convert_to_hex(z1))
            self._write(self._convert_to_hex(z2))
            z1_1 = self.conv_file.read(1)
            z1_2 = self.conv_file.read(1)
            self._write(self._convert_to_hex(z1_1))
            self._write(self._convert_to_hex(z1_2))
        self._writel('')
        return 0


    def decode(self):
    ####################################################################################################
    #
    # Decodes the geodata
    #
    ####################################################################################################
        self.conv_file = open(self.filename+'.dat','rb')
        self.csv_file  = open(self.filename+'.csv','w')
        self.log_file  = open(self.filename+'.log','wb')
        self.data_all = {}
        self.conv_file.seek(18) # header dont need that currently
        data=[]
        for x in range(0,256):
            #self._writel('x:'+str(x))
            for y in range(0,256):
                self._writel('x' + str(x) + ' y:' +str(y))
                block1 = self.conv_file.read(1)
                block2 = self.conv_file.read(1)
                block1_hex = self._convert_to_hex(block1)
                if block1_hex   == 'ff': self._writel("error")
                elif block1_hex == '00':
                    z1,z2 = self._handle_block0000()
                    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': self._handle_block0040()
                else: self._handle_block0041(block1_hex)
#        self._writel(str(data))

        self._writel(' |-- minz = %s' %(str(self.minz)))
        self._writel(' |-- maxz = %s' %(str(self.maxz)))
        
        self.conv_file.close()
        self.csv_file.close()
        self.log_file.close()
        return 0

    def __init__(self,filename):
        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('22_22_conv')
#tower_of_insolence.decode()