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