Advertisement
Guest User

Untitled

a guest
May 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.31 KB | None | 0 0
  1. import sys
  2. import struct
  3. import math
  4.  
  5. class WRPError:
  6.     def __init__(self, value):
  7.         self.value = value
  8.    
  9.     def __str__(self):
  10.         return repr(self.value)
  11.  
  12. def Rotate(angle):
  13.     a = math.radians(angle)
  14.     s = math.sin(a)
  15.     c = math.cos(a)
  16.     return (c, 0.0, s,  0.0, 1.0, 0.0,  -s, 0.0, c)
  17.  
  18. class WRPObject:
  19.     def __init__(self,  rotation,  position,  index,  path):
  20.         if rotation:
  21.             self.rotation = rotation
  22.         else:
  23.             self.rotation = (1.0, 0.0, 0.0,  0.0, 1.0, 0.0,  0.0, 0.0, 1.0)
  24.         self.position = position
  25.         self.index = index
  26.         self.path = path
  27.        
  28.     def pack(self):
  29.         tmp = ("9f3fi76s", ) + self.rotation + self.position + (self.index,self.path)
  30.         return struct.pack( *tmp )
  31.    
  32.     def __str__(self):
  33.         return "WRPObject(%s, %s, %s, %s)" % (self.rotation, self.position, self.index, self.path)
  34.        
  35.     def __repr__(self):
  36.         return "WRPObject(%r, %r, %r, %r)" % (self.rotation, self.position, self.index, self.path)
  37.        
  38.        
  39. def shortToFloat(packed): return float(packed) / 22.222;
  40. def floatToShort(number): return int( max( -1800.0, min(1800.0,  number) ) * 22.222 )
  41.        
  42. class WRPFile:
  43.     def __init__(self):
  44.         self.width = 1
  45.         self.height = 1
  46.         self.textures = []
  47.         self.elevations = []
  48.         self.textureIndices = []
  49.         self.objects = []
  50.        
  51.        
  52.     def readFromPath(self, path):
  53.         file = open(path, "rb")
  54.         self.readFromFile(file)
  55.         file.close()
  56.        
  57.        
  58.     def readFromFile(self, file, close=False):
  59.         magic = struct.unpack("4s",  file.read(4))[0]
  60.         if magic == "4WVR":
  61.             self.read4WVR(file, close)
  62.         elif magic == "8WVR":
  63.             raise WRPError, "8WVR is not supported yet!"
  64.         else:
  65.             raise WRPError,  "Only 4WVR WRP files are supported, not '%s'!" % magic
  66.            
  67.     def read4WVR(self, file, close):
  68.         self.width,  self.height = struct.unpack("2i",  file.read(8))
  69.         if self.width != self.height:
  70.             raise WRPError,  "Width and height are supposed to be the same, not %d x %d!" % (self.width,  self.height)
  71.            
  72.         cellcount = self.width * self.height
  73.        
  74.         elevReader = ( struct.unpack("h", file.read(2))[0] for x in xrange(cellcount) )
  75.         self.elevations = [ float(x)/22.222 for x in elevReader ]
  76.        
  77.         # now the same number of texture indices!
  78.         self.textureIndices = [ struct.unpack("H",  file.read(2))[0] for x in xrange(cellcount) ]
  79.        
  80.         # now the texture paths!
  81.         # Every path is 32 byte 0-terminated/padded string; there are always 512!
  82.         self.textures = [ struct.unpack("32s",  file.read(32))[0] for x in xrange(512) ]
  83.        
  84.         # now comes the tricky part! The objects have the following layout
  85.         #   float[9] rotation; /// rotation matrix
  86.         #   float x, z, y;  /// the position of the object
  87.         #   uint index; /// the index on the map
  88.         #   char[76] path; /// the path to the object
  89.         #
  90.         # those are 13*4 + 76 bytes = 128
  91.         # now we read 128 bytes as long as
  92.         buf = file.read(128)
  93.         while len(buf) == 128:
  94.             obj = struct.unpack("9f3fi76s",  buf)
  95.             self.objects.append( WRPObject( obj[0:9],  obj[9:12],  obj[12],  obj[13] ) )
  96.             buf = file.read(128)
  97.            
  98.         if close:
  99.             file.close()
  100.        
  101.        
  102.     def writeToPath(self, path):
  103.         file = open(path, "wb")
  104.         self.writeToFile(file)
  105.         file.close()
  106.        
  107.        
  108.     def writeToFile(self, file, close=False):
  109.         file.write("4WVR")
  110.         file.write( struct.pack("ii",  self.width,  self.height) )
  111.         for elev in self.elevations:
  112.             file.write( struct.pack("h",  floatToShort(elev) ) )
  113.            
  114.         for index in self.textureIndices:
  115.             file.write( struct.pack("H",  index) )
  116.            
  117.         # first texture should be '\data\more_anim.01.pac'
  118.         for texture in self.textures:
  119.             file.write( struct.pack("32s", texture) )
  120.            
  121.         # if there were not enough textures, fill them up!
  122.         for i in xrange( 512 - len(self.textures) ):
  123.             file.write( struct.pack("32s", "") )
  124.            
  125.         # now the objects...
  126.         for obj in self.objects:
  127.             file.write( obj.pack() )
  128.        
  129.         if close:
  130.             file.close()
  131.        
  132.     def generate(self, size, defaultHeight):
  133.         self.width = size
  134.         self.height = size
  135.         self.textures = ['\\data\\more_anim.01.pac']
  136.         self.elevations = [ defaultHeight for x in xrange(size*size) ]
  137.         self.textureIndices = [ 1 for x in xrange(size*size) ]
  138.        
  139.     def printInfo(self):
  140.         print "Size: %d x %d" % (self.width,  self.height)
  141.         print "Number of elevations: %d" % len(self.elevations)
  142.         print "Number of texture indices: %d" % len(self.textureIndices)
  143.         print "Number of (non-empty) textures: %d" % len(self.textures)
  144.         print "Number of objects: %d" % len(self.objects)
  145.        
  146.     def addObject(self, angle, pos, path):
  147.         obj = WrpObject(Rotate(angle), pos, len(self.objects), path)
  148.         self.objects.append(obj)
  149.         return obj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement