Started work on player data classes, simplified field dictionaries
3 files changed, 52 insertions(+), 5 deletions(-)

M common.py
M fixed.py
A => player.py
M common.py +14 -1
@@ 13,6 13,19 @@ class PlanetsData(object):
     FIELDS = ()             # Fields for single object
 
     def __init__(self):
+        # Expand field list where necessary (in case of dictionary fields)
+        fields = []
+        for field in self.FIELDS:
+            # tuple format: ('name', start, end,) start/end must be 'int'
+            if isinstance(field, tuple) and (len(field) == 3):
+                for i in range(field[1], field[1]+field[2]):
+                    fields.append((field[0], i))
+            else:
+                fields.append(field)
+        fields = tuple(fields)
+        if self.__class__.FIELDS is not fields:
+            self.__class__.FIELDS = fields
+        # Initialize fields
         for field in self.FIELDS:
             # Dictionary field: see EngSpec class in fixed.py for an example
             if isinstance(field, tuple):

          
@@ 65,7 78,7 @@ class PlanetsData(object):
         """Reads from file and loads contents in list of object instances"""
         # Data may be preceeded by a WORD indicating how much objects follow
         if has_count:
-            count = struct.unpack('< h', f.read(2))
+            (count,) = struct.unpack('< h', f.read(2))
         # Load "count" objects or predetermined "COUNT" objects or whole file
         if count is not None:
             data = f.read(cls.PACK_LENGTH * count)

          
M fixed.py +2 -4
@@ 26,16 26,14 @@ class EngSpec(PlanetsData):
     FILENAME = 'ENGSPEC.DAT'
     COUNT = 9
     PACK_LENGTH = 66
-    PACK_FORMAT = '< 20s h h h h h i i i i i i i i i'
+    PACK_FORMAT = '< 20s h h h h h 9i'
     FIELDS = ('name',
               'cost',
               'tritanium',
               'duranium',
               'molybdenum',
               'tech',
-              ('mileage', 1,), ('mileage', 2,), ('mileage', 3,),
-              ('mileage', 4,), ('mileage', 5,), ('mileage', 6,),
-              ('mileage', 7,), ('mileage', 8,), ('mileage', 9,))
+              ('mileage', 1, 9,))
         
 class HullSpec(PlanetsData):
     """List of starship hulls"""

          
A => player.py +36 -0
@@ 0,0 1,36 @@ 
+"""
+Classes for reading player data such as found in RST files.
+"""
+
+from common import PlanetsData
+
+class Base(PlanetsData):
+    """List of player's starbases"""
+    # No FILENAME, but typically: BDATAx.DAT or BDATAx.DIS (x=player)
+    # No COUNT, but obviously limited by number of planets
+    PACK_LENGTH = 156
+    PACK_FORMAT = '< h h h h h h h h 9h 20h 10h 10h 10h h h h h h h h h h h h'
+    FIELDS = ('id',
+              'owner',
+              'defense',
+              'damage',
+              'tech_engines',
+              'tech_hulls',
+              'tech_beams',
+              'tech_torpedoes',
+              ('part_engines', 1, 9,),
+              ('part_hulls,', 1, 20,),
+              ('part_beams', 1, 10,),
+              ('part_torpedoes', 1, 10,),
+              ('torpedoes', 1, 10,),
+              'fighters',
+              'ship_id',
+              'ship_action',
+              'mission',
+              'build_hull',
+              'build_engine',
+              'build_beamtype',
+              'build_beamcount',
+              'build_torptype',
+              'build_torpcount',
+              'unused')
  No newline at end of file