comparison gamelib/level.py @ 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 a356e57529ea
children 3ed6c011106d
comparison
equal deleted inserted replaced
467:fdda0f3c956b 468:d5e4959cfe7a
1 # level.py 1 # level.py
2 2
3 import constants 3 import constants
4 import serializer
4 import data 5 import data
5 import os 6 import os
6 from animal import DEFAULT_FOX_WEIGHTINGS 7 from animal import DEFAULT_FOX_WEIGHTINGS
7 from ConfigParser import RawConfigParser 8 from ConfigParser import RawConfigParser
8 9
9 class Level(object): 10 class Level(serializer.Simplifiable):
10 """Container for level details""" 11 """Container for level details"""
11 12
12 def __init__(self, level_name): 13 SIMPLIFY = [
14 'config_name',
15 ]
16
17 def __init__(self, config_name):
18 self.config_name = config_name
13 self.level_file = None 19 self.level_file = None
14 default_map = '%s.tga' % level_name 20 default_map = '%s.tga' % config_name
15 for poss_file in ['levels/%s.conf' % level_name, '%s.conf' % level_name, 21 for poss_file in ['levels/%s.conf' % config_name, '%s.conf' % config_name,
16 'levels/%s' % level_name, level_name]: 22 'levels/%s' % config_name, config_name]:
17 cand = data.filepath(poss_file) 23 cand = data.filepath(poss_file)
18 if os.path.exists(cand): 24 if os.path.exists(cand):
19 self.level_file = cand 25 self.level_file = cand
20 break 26 break
21 if not self.level_file: 27 if not self.level_file:
22 raise RuntimeError('Unable to load %s' % level_name) 28 raise RuntimeError('Unable to load %s' % config_name)
23 # Load the level info file 29 # Load the level info file
24 # setup defaults 30 # setup defaults
25 defaults = { 31 defaults = {
26 'map' : default_map, 32 'map' : default_map,
27 'level name' : level_name, 33 'level name' : config_name,
28 'sell price chicken' : constants.DEFAULT_SELL_PRICE_CHICKEN, 34 'sell price chicken' : constants.DEFAULT_SELL_PRICE_CHICKEN,
29 'sell price egg' : constants.DEFAULT_SELL_PRICE_EGG, 35 'sell price egg' : constants.DEFAULT_SELL_PRICE_EGG,
30 'sell price dead fox' : constants.DEFAULT_SELL_PRICE_DEAD_FOX, 36 'sell price dead fox' : constants.DEFAULT_SELL_PRICE_DEAD_FOX,
31 'turn limit' : constants.DEFAULT_TURN_LIMIT, 37 'turn limit' : constants.DEFAULT_TURN_LIMIT,
32 'goal' : constants.DEFAULT_GOAL_DESC, 38 'goal' : constants.DEFAULT_GOAL_DESC,
59 self.fox_weightings = [] 65 self.fox_weightings = []
60 for animal, _prob in DEFAULT_FOX_WEIGHTINGS: 66 for animal, _prob in DEFAULT_FOX_WEIGHTINGS:
61 self.fox_weightings.append((animal, config.getint('Fox probablities', 67 self.fox_weightings.append((animal, config.getint('Fox probablities',
62 animal.CONFIG_NAME))) 68 animal.CONFIG_NAME)))
63 69
70 def unsimplify(cls, *args, **kwargs):
71 """Override default Simplifiable unsimplification."""
72 obj = super(Level, cls).unsimplify(*args, **kwargs)
73 obj.__init__(obj.config_name)
74 return obj
75 unsimplify = classmethod(unsimplify)
76
64 # Utility functions, so we can make things more flexible later 77 # Utility functions, so we can make things more flexible later
65 78
66 def is_last_day(self, days): 79 def is_last_day(self, days):
67 """Check if we're the last day""" 80 """Check if we're the last day"""
68 return days == self.turn_limit 81 return days == self.turn_limit