Prototype tile glyphs.
[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 = None
12
13     def __init__(self, required_bits, **data):
14         self.required_bits = required_bits
15         self.data = data
16
17     def get_text(self, location=None):
18         substitutions = self.data.copy()
19
20         if 'direction' in self.data:
21             substitutions['rowcol'] = {
22                 'NORTH': 'column',
23                 'SOUTH': 'column',
24                 'EAST': 'row',
25                 'WEST': 'row',
26             }[self.data['direction']]
27             substitutions['direction'] = '{%s}' % (substitutions['direction'],)
28
29         if 'chesspiece' in self.data:
30             substitutions['chesspiece_name'] = move_glyph(
31                 self.data['chesspiece'])
32
33         if location is None:
34             substitutions['location_bits'] = 'bits specified by this location'
35         else:
36             substitutions['location_bits'] = bit_glyphs(
37                 location.bitwise_operand)
38
39         return self.TEXT % substitutions
40
41     def check_available(self, player):
42         return player.bits.check_bits(self.required_bits)
43
44     def perform_action(self, board, location):
45         raise NotImplementedError(
46             "%s does not implement perform_action()." % (type(self).__name__,))
47
48     def check_and_clear_MSB(self, player):
49         if player.bits.check_bit(BITS.MSB):
50             player.bits.clear_bit(BITS.MSB)
51             return True
52         else:
53             return False
54
55     def export(self):
56         return {'required_bits': list(self.required_bits),
57                 'data': self.data,
58                 'action_class': self.__class__.__name__}
59
60
61 class DoNothing(LocationAction):
62     TEXT = "No effect."
63     GLYPHS = (ACTION_GLYPHS.NOTHING,)
64
65     def perform_action(self, board, location):
66         pass
67
68
69 class LoseHealthOrMSB(LocationAction):
70     TEXT = "Lose {HEALTH} or {MSB}."
71     GLYPHS = (ACTION_GLYPHS.MSB, ACTION_GLYPHS.DAMAGE)
72
73     def perform_action(self, board, location):
74         if not self.check_and_clear_MSB(board.player):
75             board.lose_health()
76
77
78 class SetBits(LocationAction):
79     TEXT = "Set %(location_bits)s."
80     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
81
82     def perform_action(self, board, location):
83         board.player.bits.set_bits(location.bitwise_operand)
84
85
86 class ToggleBits(LocationAction):
87     TEXT = "Toggle %(location_bits)s."
88     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
89
90     def perform_action(self, board, location):
91         board.player.bits.toggle_bits(location.bitwise_operand)
92
93
94 class LoseHealthOrMSBAndSetBits(LocationAction):
95     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
96     GLYPHS = (ACTION_GLYPHS.SET_BITS, ACTION_GLYPHS.MSB, ACTION_GLYPHS.DAMAGE)
97
98     def perform_action(self, board, location):
99         if not self.check_and_clear_MSB(board.player):
100             board.lose_health()
101         board.player.bits.set_bits(location.bitwise_operand)
102
103
104 class AcquireWinToken(LocationAction):
105     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
106     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
107
108     def perform_action(self, board, location):
109         board.acquire_win_token()
110         board.player.bits.clear_bits(set([
111             BITS.RED, BITS.GREEN, BITS.BLUE,
112         ]))
113
114
115 class GainHealthAndClearBitsOrMSB(LocationAction):
116     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
117     GLYPHS = (ACTION_GLYPHS.HEAL, ACTION_GLYPHS.MSB, ACTION_GLYPHS.CLEAR_BITS)
118
119     def perform_action(self, board, location):
120         board.gain_health()
121         if not self.check_and_clear_MSB(board.player):
122             board.player.bits.clear_bits(location.bitwise_operand)
123
124
125 class ShiftLocations(LocationAction):
126     TEXT = "Shift current %(rowcol)s %(direction)s."
127     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
128
129     def perform_action(self, board, location):
130         board.shift_locations(self.data['direction'])
131
132
133 class AllowChessMove(LocationAction):
134     TEXT = "Move like a %(chesspiece_name)s for one turn."
135     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
136
137     def perform_action(self, board, location):
138         if self.data['chesspiece'] in CHESS_PIECES:
139             chesspiece = CHESS_PIECES[self.data['chesspiece']]
140             board.allow_chess_move(chesspiece)