Hook up shift glyph
[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             board.lose_health()
86
87
88 class SetBits(LocationAction):
89     TEXT = "Set %(location_bits)s."
90     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
91
92     def perform_action(self, board, location):
93         board.player.bits.set_bits(location.bitwise_operand)
94
95
96 class ToggleBits(LocationAction):
97     TEXT = "Toggle %(location_bits)s."
98     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
99
100     def perform_action(self, board, location):
101         board.player.bits.toggle_bits(location.bitwise_operand)
102
103
104 class ShiftBits(LocationAction):
105     TEXT = "Barrel-shift player bits %(shift_glyph)s %(shift)s."
106     GLYPHS = (ACTION_GLYPHS.SHIFT_LEFT,)
107
108     def perform_action(self, board, location):
109         shift = self.data['shift']
110         if self.data['direction'] == 'left':
111             board.player.bits.shift_bits_left(shift)
112         else:
113             board.player.bits.shift_bits_right(shift)
114
115
116 class LoseHealthOrMSBAndSetBits(LocationAction):
117     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
118     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
119     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
120
121     def perform_action(self, board, location):
122         if not self.check_and_clear_MSB(board.player):
123             board.lose_health()
124         board.player.bits.set_bits(location.bitwise_operand)
125
126
127 class AcquireWinToken(LocationAction):
128     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
129     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
130
131     def perform_action(self, board, location):
132         board.acquire_win_token()
133         board.player.bits.clear_bits(set([
134             BITS.RED, BITS.GREEN, BITS.BLUE,
135         ]))
136
137
138 class GainHealth(LocationAction):
139     TEXT = "Gain {HEALTH}."
140     GLYPHS = (ACTION_GLYPHS.HEAL,)
141
142     def perform_action(self, board, location):
143         board.gain_health()
144
145
146 class GainHealthAndClearBitsOrMSB(LocationAction):
147     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
148     GLYPHS = (ACTION_GLYPHS.HEAL,)
149     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
150
151     def perform_action(self, board, location):
152         board.gain_health()
153         if not self.check_and_clear_MSB(board.player):
154             board.player.bits.clear_bits(location.bitwise_operand)
155
156
157 class ShiftLocations(LocationAction):
158     TEXT = "Shift current %(rowcol)s %(direction)s."
159     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
160
161     def perform_action(self, board, location):
162         sound.play_sound('grind.ogg')
163         board.shift_locations(self.data['direction'])
164
165
166 class RotateLocations(LocationAction):
167     TEXT = "Rotate adjacent locations %(rot_direction_name)s."
168     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
169
170     def perform_action(self, board, location):
171         sound.play_sound('grind.ogg')
172         board.rotate_locations(self.data['rot_direction'])
173
174
175 class AllowChessMove(LocationAction):
176     TEXT = "Move like a %(chesspiece_name)s for one turn."
177     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
178
179     def perform_action(self, board, location):
180         if self.data['chesspiece'] in CHESS_PIECES:
181             chesspiece = CHESS_PIECES[self.data['chesspiece']]
182             board.allow_chess_move(chesspiece)