changeset 468:d5e4959cfe7a

Start of support for restoring level information.
author Simon Cross <hodgestar@gmail.com>
date Tue, 24 Nov 2009 21:20:38 +0000
parents fdda0f3c956b
children e0b11d684ee8
files gamelib/gameboard.py gamelib/level.py
diffstat 2 files changed, 22 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/gameboard.py	Tue Nov 24 21:20:12 2009 +0000
+++ b/gamelib/gameboard.py	Tue Nov 24 21:20:38 2009 +0000
@@ -76,7 +76,9 @@
     BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
 
     SIMPLIFY = [
+        'level',
         'tv',
+        'max_foxes',
         'chickens',
         'buildings',
         'foxes',
--- a/gamelib/level.py	Tue Nov 24 21:20:12 2009 +0000
+++ b/gamelib/level.py	Tue Nov 24 21:20:38 2009 +0000
@@ -1,30 +1,36 @@
 # level.py
 
 import constants
+import serializer
 import data
 import os
 from animal import DEFAULT_FOX_WEIGHTINGS
 from ConfigParser import RawConfigParser
 
-class Level(object):
+class Level(serializer.Simplifiable):
     """Container for level details"""
 
-    def __init__(self, level_name):
+    SIMPLIFY = [
+        'config_name',
+    ]
+
+    def __init__(self, config_name):
+        self.config_name = config_name
         self.level_file = None
-        default_map = '%s.tga' % level_name
-        for poss_file in ['levels/%s.conf' % level_name, '%s.conf' % level_name,
-                'levels/%s' % level_name, level_name]:
+        default_map = '%s.tga' % config_name
+        for poss_file in ['levels/%s.conf' % config_name, '%s.conf' % config_name,
+                'levels/%s' % config_name, config_name]:
             cand = data.filepath(poss_file)
             if os.path.exists(cand):
                 self.level_file = cand
                 break
         if not self.level_file:
-            raise RuntimeError('Unable to load %s' % level_name)
+            raise RuntimeError('Unable to load %s' % config_name)
         # Load the level info file
         # setup defaults
         defaults = {
                 'map' : default_map,
-                'level name' : level_name,
+                'level name' : config_name,
                 'sell price chicken' : constants.DEFAULT_SELL_PRICE_CHICKEN,
                 'sell price egg' : constants.DEFAULT_SELL_PRICE_EGG,
                 'sell price dead fox' : constants.DEFAULT_SELL_PRICE_DEAD_FOX,
@@ -61,6 +67,13 @@
             self.fox_weightings.append((animal, config.getint('Fox probablities',
                 animal.CONFIG_NAME)))
 
+    def unsimplify(cls, *args, **kwargs):
+        """Override default Simplifiable unsimplification."""
+        obj = super(Level, cls).unsimplify(*args, **kwargs)
+        obj.__init__(obj.config_name)
+        return obj
+    unsimplify = classmethod(unsimplify)
+
     # Utility functions, so we can make things more flexible later
 
     def is_last_day(self, days):