1 from naja.constants import ACTION_GLYPHS, BITS, CHESS_PIECES
2 from naja.utils import bit_glyphs, move_glyph
5 class LocationAction(object):
7 An action that may be performed on a location.
14 def __init__(self, required_bits, **data):
15 self.required_bits = required_bits
18 def get_text(self, location=None):
19 substitutions = self.data.copy()
21 if 'direction' in self.data:
22 substitutions['rowcol'] = {
27 }[self.data['direction']]
28 substitutions['direction'] = '{%s}' % (substitutions['direction'],)
30 if 'chesspiece' in self.data:
31 substitutions['chesspiece_name'] = move_glyph(
32 self.data['chesspiece'])
35 substitutions['location_bits'] = 'bits specified by this location'
37 substitutions['location_bits'] = bit_glyphs(
38 location.bitwise_operand)
40 return self.TEXT % substitutions
42 def check_available(self, player):
43 return player.bits.check_bits(self.required_bits)
45 def perform_action(self, board, location):
46 raise NotImplementedError(
47 "%s does not implement perform_action()." % (type(self).__name__,))
49 def check_and_clear_MSB(self, player):
50 if player.bits.check_bit(BITS.MSB):
51 player.bits.clear_bit(BITS.MSB)
57 return {'required_bits': list(self.required_bits),
59 'action_class': self.__class__.__name__}
62 class DoNothing(LocationAction):
64 GLYPHS = (ACTION_GLYPHS.NOTHING,)
66 def perform_action(self, board, location):
70 class LoseHealthOrMSB(LocationAction):
71 TEXT = "Lose {HEALTH} or {MSB}."
72 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
74 def perform_action(self, board, location):
75 if not self.check_and_clear_MSB(board.player):
79 class SetBits(LocationAction):
80 TEXT = "Set %(location_bits)s."
81 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
83 def perform_action(self, board, location):
84 board.player.bits.set_bits(location.bitwise_operand)
87 class ToggleBits(LocationAction):
88 TEXT = "Toggle %(location_bits)s."
89 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
91 def perform_action(self, board, location):
92 board.player.bits.toggle_bits(location.bitwise_operand)
95 class LoseHealthOrMSBAndSetBits(LocationAction):
96 TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
97 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
98 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
100 def perform_action(self, board, location):
101 if not self.check_and_clear_MSB(board.player):
103 board.player.bits.set_bits(location.bitwise_operand)
106 class AcquireWinToken(LocationAction):
107 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
108 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
110 def perform_action(self, board, location):
111 board.acquire_win_token()
112 board.player.bits.clear_bits(set([
113 BITS.RED, BITS.GREEN, BITS.BLUE,
117 class GainHealthAndClearBitsOrMSB(LocationAction):
118 TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
119 GLYPHS = (ACTION_GLYPHS.HEAL,)
120 MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
122 def perform_action(self, board, location):
124 if not self.check_and_clear_MSB(board.player):
125 board.player.bits.clear_bits(location.bitwise_operand)
128 class ShiftLocations(LocationAction):
129 TEXT = "Shift current %(rowcol)s %(direction)s."
130 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
132 def perform_action(self, board, location):
133 board.shift_locations(self.data['direction'])
136 class AllowChessMove(LocationAction):
137 TEXT = "Move like a %(chesspiece_name)s for one turn."
138 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
140 def perform_action(self, board, location):
141 if self.data['chesspiece'] in CHESS_PIECES:
142 chesspiece = CHESS_PIECES[self.data['chesspiece']]
143 board.allow_chess_move(chesspiece)