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