Standardise music volume
[naja.git] / naja / actions.py
1 from naja.constants import ACTION_GLYPHS, BITS, CHESS_PIECES
2 from naja.sound import sound
3 from naja.utils import bit_glyphs, move_glyph
4
5
6 class LocationAction(object):
7     """
8     An action that may be performed on a location.
9     """
10
11     TEXT = None
12     GLYPHS = tuple()
13     MSB_GLYPH = None
14
15     def __init__(self, required_bits, **data):
16         self.required_bits = required_bits
17         self.data = data
18
19     def get_text(self, location=None):
20         substitutions = self.data.copy()
21
22         if 'shift' in self.data:
23             substitutions['shift'] = self.data['shift']
24             substitutions['shift_glyph'] = ('{SHIFT_%s}'
25                                             % self.data['direction'].upper())
26         elif 'direction' in self.data:
27             substitutions['rowcol'] = {
28                 'NORTH': 'column',
29                 'SOUTH': 'column',
30                 'EAST': 'row',
31                 'WEST': 'row',
32             }[self.data['direction']]
33             substitutions['direction'] = '{%s}' % (substitutions['direction'],)
34
35         if 'chesspiece' in self.data:
36             substitutions['chesspiece_name'] = move_glyph(
37                 self.data['chesspiece'])
38
39         if 'rot_direction' in self.data:
40             substitutions['rot_direction_name'] = '{%s}' % (
41                 substitutions['rot_direction'],)
42
43         if location is None:
44             substitutions['location_bits'] = 'bits specified by this location'
45         else:
46             substitutions['location_bits'] = bit_glyphs(
47                 location.bitwise_operand)
48
49         return self.TEXT % substitutions
50
51     def check_available(self, player):
52         return player.bits.check_bits(self.required_bits)
53
54     def perform_action(self, board, location):
55         raise NotImplementedError(
56             "%s does not implement perform_action()." % (type(self).__name__,))
57
58     def check_and_clear_MSB(self, player):
59         if player.bits.check_bit(BITS.MSB):
60             player.bits.clear_bit(BITS.MSB)
61             return True
62         else:
63             return False
64
65     def export(self):
66         return {'required_bits': list(self.required_bits),
67                 'data': self.data,
68                 'action_class': self.__class__.__name__}
69
70
71 class DoNothing(LocationAction):
72     TEXT = "No effect."
73     GLYPHS = (ACTION_GLYPHS.NOTHING,)
74
75     def perform_action(self, board, location):
76         pass
77
78
79 class LoseHealthOrMSB(LocationAction):
80     TEXT = "Lose {HEALTH} or {MSB}."
81     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
82
83     def perform_action(self, board, location):
84         if not self.check_and_clear_MSB(board.player):
85             sound.play_sound('awwww.ogg')
86             board.lose_health()
87
88
89 class SetBits(LocationAction):
90     TEXT = "Set %(location_bits)s."
91     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
92
93     def perform_action(self, board, location):
94         board.player.bits.set_bits(location.bitwise_operand)
95
96
97 class ToggleBits(LocationAction):
98     TEXT = "Toggle %(location_bits)s."
99     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
100
101     def perform_action(self, board, location):
102         board.player.bits.toggle_bits(location.bitwise_operand)
103
104
105 class ShiftBits(LocationAction):
106     TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
107     GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
108
109     def perform_action(self, board, location):
110         shift = self.data['shift']
111         if self.data['direction'] == 'left':
112             board.player.bits.shift_bits_left(shift)
113         else:
114             board.player.bits.shift_bits_right(shift)
115
116
117 class LoseHealthOrMSBAndSetBits(LocationAction):
118     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
119     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
120     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
121
122     def perform_action(self, board, location):
123         if not self.check_and_clear_MSB(board.player):
124             sound.play_sound('awwww.ogg')
125             board.lose_health()
126         board.player.bits.set_bits(location.bitwise_operand)
127
128
129 class AcquireWinToken(LocationAction):
130     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
131     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
132
133     def perform_action(self, board, location):
134         sound.play_sound('yipee.ogg')
135         board.acquire_win_token()
136         board.player.bits.clear_bits(set([
137             BITS.RED, BITS.GREEN, BITS.BLUE,
138         ]))
139
140
141 class GainHealth(LocationAction):
142     TEXT = "Gain {HEALTH}."
143     GLYPHS = (ACTION_GLYPHS.HEAL,)
144
145     def perform_action(self, board, location):
146         sound.play_sound('aha.ogg')
147         board.gain_health()
148
149
150 class GainHealthAndClearBitsOrMSB(LocationAction):
151     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
152     GLYPHS = (ACTION_GLYPHS.HEAL,)
153     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
154
155     def perform_action(self, board, location):
156         sound.play_sound('aha.ogg')
157         board.gain_health()
158         if not self.check_and_clear_MSB(board.player):
159             board.player.bits.clear_bits(location.bitwise_operand)
160
161
162 class ShiftLocations(LocationAction):
163     TEXT = "Shift current %(rowcol)s %(direction)s."
164     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
165
166     def perform_action(self, board, location):
167         sound.play_sound('grind.ogg')
168         board.shift_locations(self.data['direction'])
169
170
171 class RotateLocations(LocationAction):
172     TEXT = "Rotate adjacent locations %(rot_direction_name)s."
173     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
174
175     def perform_action(self, board, location):
176         sound.play_sound('grind.ogg')
177         board.rotate_locations(self.data['rot_direction'])
178
179
180 class AllowChessMove(LocationAction):
181     TEXT = "Move like a %(chesspiece_name)s for one turn."
182     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
183
184     def perform_action(self, board, location):
185         if self.data['chesspiece'] in CHESS_PIECES:
186             chesspiece = CHESS_PIECES[self.data['chesspiece']]
187             board.allow_chess_move(chesspiece)