Fix loading save games (by telling the loaded player about the board they're on)...
[naja.git] / naja / gameboard.py
1 from random import choice
2
3 from naja.constants import(
4     BITS, DIRECTION_BITS, CONDITION_BITS, PLAYER_DEFAULTS,
5     ACT, EXAMINE, ROTATION)
6 from naja.options import options
7 from naja.player import Player
8 from naja import actions
9 from naja.sound import sound
10 from naja.utils import parse_bits
11 import random
12
13
14 class GameBoard(object):
15     """
16     A representation of the game board.
17     """
18
19     def __init__(self, state, player, board_locations):
20         self.max_health = state['max_health']
21         self.wins_required = state['wins_required']
22         self.health = state['health']
23         self.wins = state['wins']
24         self.locations = [item.copy() for item in state['locations']]
25         self.puzzle = state.get('puzzle', False)
26         self.player = player
27         self.board_locations = board_locations
28         self.player_mode = state.get('player_mode', EXAMINE)
29         self.has_cheated = state.get('cheater', options.cheat_enabled)
30         self.clock_count = state.get('clock_count', 0)
31         self.replacement_params = state.get('replacement_params', None)
32
33     @classmethod
34     def new_game(cls, deck, initial_bits=None, initial_pos=None,
35                  max_health=None, wins_required=None):
36
37         defaults = {
38             'initial_bits': PLAYER_DEFAULTS.INITIAL_BITS,
39             'initial_pos': PLAYER_DEFAULTS.INITIAL_POS,
40             'max_health': PLAYER_DEFAULTS.MAX_HEALTH,
41             'wins_required': PLAYER_DEFAULTS.WINS_REQUIRED,
42         }
43
44         puzzle = deck.get('puzzle', False)
45
46         if puzzle:
47             puzzle_defaults = deck.get('defaults', {})
48             for k, v in puzzle_defaults.iteritems():
49                 if isinstance(v, list):
50                     puzzle_defaults[k] = tuple(v)
51             defaults.update(puzzle_defaults)
52
53         if initial_bits is None:
54             initial_bits = defaults['initial_bits']
55         if initial_pos is None:
56             initial_pos = defaults['initial_pos']
57         if max_health is None:
58             max_health = defaults['max_health']
59         if wins_required is None:
60             wins_required = defaults['wins_required']
61
62         # Overriden by command line
63         if options.initial_bits:
64             initial_bits = options.initial_bits
65
66         state = {
67             'max_health': max_health,
68             'health': max_health,
69             'wins_required': wins_required,
70             'wins': 0,
71             'locations': deck['cards'],
72             'puzzle': puzzle,
73             'clock_count': 0,
74             'replacement_params': deck.get('replacement_params', None),
75         }
76         player = Player(initial_bits, initial_pos)
77         board_locations = cls.import_board_locations(
78             cls.generate_board(deck))
79         board = cls(state, player, board_locations)
80         player.set_gameboard(board)
81         return board
82
83     @classmethod
84     def import_game(cls, definition):
85         state = definition.copy()
86         player = Player.import_player(state.pop('player'))
87         board_locations = cls.import_board_locations(
88             state.pop('board_locations'))
89         board = cls(state, player, board_locations)
90         player.set_gameboard(board)
91         return board
92
93     def export(self):
94         data = {
95             'max_health': self.max_health,
96             'health': self.health,
97             'wins_required': self.wins_required,
98             'wins': self.wins,
99             'locations': [item.copy() for item in self.locations],
100             'puzzle': self.puzzle,
101             'player': self.player.export(),
102             'board_locations': self.export_board_locations(),
103             'player_mode': self.player_mode,
104             'clock_count': self.clock_count,
105             'replacement_params': self.replacement_params,
106         }
107         if options.cheat_enabled:
108             self.has_cheated = True
109         if self.has_cheated:
110             data['cheater'] = True
111         return data
112
113     @classmethod
114     def import_locations(cls, locations_definition):
115         return [
116             LocationCard.import_location(definition)
117             for definition in locations_definition]
118
119     def export_board_locations(self):
120         return sorted(
121             (position, location.export())
122             for position, location in self.board_locations.iteritems())
123
124     @classmethod
125     def import_board_locations(cls, board_locations_definition):
126         return dict(
127             (tuple(position), LocationCard.import_location(definition))
128             for position, definition in board_locations_definition)
129
130     @classmethod
131     def generate_board(cls, deck):
132         if deck.get('puzzle', False):
133             return cls.generate_puzzle_board(deck)
134         else:
135             return cls.generate_random_board(deck)
136
137     @classmethod
138     def generate_puzzle_board(cls, deck):
139         assert len(deck['cards']) == 5 * 5
140         replacement_params = deck.get('replacement_params', None)
141         board_locations = [
142             [(i % 5, i // 5),
143              LocationCard.new_location(
144                  card.copy(), replacement_params, puzzle=True).export()]
145             for i, card in enumerate(deck['cards'])
146         ]
147         return board_locations
148
149     @classmethod
150     def generate_random_board(cls, deck):
151         board_locations = []
152         replacement_params = deck.get('replacement_params', None)
153         for x in range(5):
154             for y in range(5):
155                 new_choice = cls.choose_card(deck['cards'], board_locations)
156                 board_location = LocationCard.new_location(
157                     new_choice.copy(), replacement_params)
158                 board_locations.append([(x, y), board_location.export()])
159         return board_locations
160
161     def lose_health(self):
162         self.health -= 1
163         if self.health <= 0:
164             self.end_game(win=False)
165
166     def gain_health(self):
167         if self.health < self.max_health:
168             self.health += 1
169
170     def acquire_win_token(self):
171         self.wins += 1
172         if self.wins >= self.wins_required:
173             self.end_game(win=True)
174
175     def card_used(self, position):
176         if not self.puzzle:
177             self.replace_card(position)
178
179     def replace_card(self, position):
180         new_choice = self.choose_card(self.locations,
181                                       self.board_locations.items(),
182                                       position)
183         location = LocationCard.new_location(new_choice.copy(),
184                                              self.replacement_params)
185         self.board_locations[position] = location
186
187     @classmethod
188     def choose_card(cls, cards, board_locations, position=None):
189         # Find which cards are at their maximum and exclude them from
190         # the choice list
191         counts = {}
192         choices = {card['card_name']: card for card in cards}
193         for pos, card in board_locations:
194             if pos == position:
195                 # skip the card we're replacing if appropriate
196                 continue
197             if isinstance(card, LocationCard):
198                 key = card.card_name
199                 max_num = card.max_number
200             else:
201                 key = card['card_name']
202                 max_num = card.get('max_number', 25)
203             counts.setdefault(key, 0)
204             counts[key] += 1
205             if counts[key] >= max_num:
206                 if key in choices:
207                     del choices[key]
208         return choice(choices.values())
209
210     def shift_location_row(self, change, is_vertical):
211         px, py = self.player.position
212         shifted_locations = {}
213         mkpos = lambda i: (px, i) if is_vertical else (i, py)
214
215         for i in range(5):
216             if (px, py) == mkpos(i):
217                 continue
218             new_i = (i + change) % 5
219             if (px, py) == mkpos(new_i):
220                 new_i = (new_i + change) % 5
221             shifted_locations[mkpos(new_i)] = self.board_locations[mkpos(i)]
222
223         self.board_locations.update(shifted_locations)
224
225     def shift_locations(self, direction):
226         if BITS[direction] == BITS.NORTH:
227             self.shift_location_row(-1, is_vertical=True)
228         elif BITS[direction] == BITS.SOUTH:
229             self.shift_location_row(1, is_vertical=True)
230         elif BITS[direction] == BITS.EAST:
231             self.shift_location_row(1, is_vertical=False)
232         elif BITS[direction] == BITS.WEST:
233             self.shift_location_row(-1, is_vertical=False)
234
235     def rotate_locations(self, direction):
236         px, py = self.player.position
237         locations_to_rotate = []
238         rotated_locations = {}
239
240         if py > 0:
241             for i in range(max(0, px - 1), min(5, px + 2)):
242                 locations_to_rotate.append((i, py - 1))
243
244         if px < 4:
245             locations_to_rotate.append((px + 1, py))
246
247         if py < 4:
248             for i in reversed(range(max(0, px - 1), min(5, px + 2))):
249                 locations_to_rotate.append((i, py + 1))
250
251         if px > 0:
252             locations_to_rotate.append((px - 1, py))
253
254         if ROTATION[direction] == ROTATION.CLOCKWISE:
255             new_positions = locations_to_rotate[1:] + [locations_to_rotate[0]]
256         elif ROTATION[direction] == ROTATION.ANTICLOCKWISE:
257             new_positions = (
258                 [locations_to_rotate[-1]] + locations_to_rotate[:-1])
259
260         for old, new in zip(locations_to_rotate, new_positions):
261             rotated_locations[new] = self.board_locations[old]
262
263         self.board_locations.update(rotated_locations)
264
265     def allow_chess_move(self, chesspiece):
266         self.player.allow_chess_move(chesspiece)
267
268     def change_mode(self, new_mode):
269         """Advance to the next mode"""
270         if new_mode == self.player_mode:
271             raise RuntimeError("Inconsistent state. Setting mode %s to itself"
272                                % self.player_mode)
273         elif new_mode in (ACT, EXAMINE):
274             self.player_mode = new_mode
275             if new_mode is EXAMINE:
276                 self.board_update()
277         else:
278             raise RuntimeError("Illegal player mode %s" % self.player_mode)
279
280     def board_update(self):
281         self.clock_count += 1
282         for position, location in self.board_locations.iteritems():
283             location.timer_action(position, self)
284
285     def end_game(self, win):
286         # TODO: Find a way to not have UI stuff in game logic stuff.
287         from naja.events import SceneChangeEvent
288         from naja.scenes.lose import LoseScene
289         from naja.scenes.win import WinScene
290         sound.stop()
291         if win:
292             SceneChangeEvent.post(WinScene)
293         else:
294             SceneChangeEvent.post(LoseScene)
295
296
297 class LocationCard(object):
298     """
299     A particular set of options available on a location.
300     """
301
302     def __init__(self, card_name, bitwise_operand, location_actions,
303                  replacement_time=None, max_number=25):
304         self.card_name = card_name
305         self.bitwise_operand = bitwise_operand
306         self.actions = location_actions
307         self.max_number = max_number
308         self.replacement_time = replacement_time
309
310     @classmethod
311     def import_location(cls, state):
312         location_actions = [
313             cls.build_action(definition) for definition in state['actions']]
314         return cls(state['card_name'], state['bitwise_operand'],
315                    location_actions, state['replacement_time'],
316                    state['max_number'])
317
318     @classmethod
319     def build_action(cls, definition):
320         action_class = getattr(actions, definition['action_class'])
321         required_bits = parse_bits(definition['required_bits'])
322         data = definition.get('data', {})
323         return action_class(required_bits, **data)
324
325     @classmethod
326     def new_location(cls, definition, replacement_params=None, puzzle=False):
327         if 'bits' in definition:
328             bits = parse_bits(definition['bits'])
329         else:
330             bits = cls.generate_bitwise_operand()
331
332         if 'replacement_time' in definition:
333             replacement_time = definition['replacement_time']
334         else:
335             replacement_time = cls.generate_replacement_time(
336                 replacement_params)
337
338         max_number = definition.get('max_number', 25)
339         card_name = definition['card_name']
340         location = cls.import_location({
341             'bitwise_operand': bits,
342             'actions': definition['actions'],
343             'max_number': max_number,
344             'card_name': card_name,
345             'replacement_time': replacement_time,
346         })
347         if not puzzle:
348             location.check_actions()
349         return location
350
351     def export(self):
352         return {
353             'bitwise_operand': sorted(self.bitwise_operand),
354             'actions': [action.export() for action in self.actions],
355             'max_number': self.max_number,
356             'card_name': self.card_name,
357             'replacement_time': self.replacement_time,
358         }
359
360     def check_actions(self):
361         if not self.actions:
362             print "Warning: Location has no actions."
363             self.insert_default_default_action()
364         if self.actions[0].required_bits:
365             self.insert_default_default_action()
366
367     def insert_default_default_action(self):
368         self.actions.insert(0, self.build_action({
369             'action_class': 'DoNothing',
370             'required_bits': [],
371         }))
372
373     @staticmethod
374     def generate_bitwise_operand():
375         """
376         Generate a set of two or three bits. At least one direction and one
377         condition bit will be included. There is a low probability of choosing
378         a third bit from the complete set.
379         """
380         bits = set()
381         bits.add(choice(DIRECTION_BITS.values()))
382         bits.add(choice(CONDITION_BITS.values()))
383         # One in three chance of adding a third bit, with a further one in four
384         # chance that it will match a bit already chosen.
385         if choice(range(3)) == 0:
386             bits.add(choice(BITS.values()))
387         return frozenset(bits)
388
389     @staticmethod
390     def generate_replacement_time(replacement_params):
391         if replacement_params is None:
392             return None
393         else:
394             if replacement_params['chance'] > random.random():
395                 return random.randint(replacement_params['min'],
396                                       replacement_params['max'])
397             else:
398                 return None
399
400     def timer_action(self, position, board):
401         if self.replacement_time is not None:
402             self.replacement_time -= 1
403             if self.replacement_time <= 0:
404                 board.replace_card(position)