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.
11 GLYPHS = (ACTION_GLYPHS.NOTHING,)
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'])
34 if 'rot_direction' in self.data:
35 substitutions['rot_direction_name'] = '{%s}' % (
36 substitutions['rot_direction'],)
39 substitutions['location_bits'] = 'bits specified by this location'
41 substitutions['location_bits'] = bit_glyphs(
42 location.bitwise_operand)
44 return self.TEXT % substitutions
46 def check_available(self, player):
47 return player.bits.check_bits(self.required_bits)
49 def perform_action(self, board, location):
50 raise NotImplementedError(
51 "%s does not implement perform_action()." % (type(self).__name__,))
53 def check_and_clear_MSB(self, player):
54 if player.bits.check_bit(BITS.MSB):
55 player.bits.clear_bit(BITS.MSB)
61 return {'required_bits': list(self.required_bits),
63 'action_class': self.__class__.__name__}
66 class DoNothing(LocationAction):
69 def perform_action(self, board, location):
73 class LoseHealthOrMSB(LocationAction):
74 TEXT = "Lose {HEALTH} or {MSB}."
75 MSB_GLYPH = ACTION_GLYPHS.DAMAGE
77 def perform_action(self, board, location):
78 if not self.check_and_clear_MSB(board.player):
82 class SetBits(LocationAction):
83 TEXT = "Set %(location_bits)s."
84 GLYPHS = (ACTION_GLYPHS.SET_BITS,)
86 def perform_action(self, board, location):
87 board.player.bits.set_bits(location.bitwise_operand)
90 class ToggleBits(LocationAction):
91 TEXT = "Toggle %(location_bits)s."
92 GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
94 def perform_action(self, board, location):
95 board.player.bits.toggle_bits(location.bitwise_operand)
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
103 def perform_action(self, board, location):
104 if not self.check_and_clear_MSB(board.player):
106 board.player.bits.set_bits(location.bitwise_operand)
109 class AcquireWinToken(LocationAction):
110 TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
111 GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
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,
120 class GainHealth(LocationAction):
121 TEXT = "Gain {HEALTH}."
122 GLYPHS = (ACTION_GLYPHS.HEAL,)
124 def perform_action(self, board, location):
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
133 def perform_action(self, board, location):
135 if not self.check_and_clear_MSB(board.player):
136 board.player.bits.clear_bits(location.bitwise_operand)
139 class ShiftLocations(LocationAction):
140 TEXT = "Shift current %(rowcol)s %(direction)s."
141 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
143 def perform_action(self, board, location):
144 board.shift_locations(self.data['direction'])
147 class RotateLocations(LocationAction):
148 TEXT = "Rotate adjacent locations %(rot_direction_name)s."
149 GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
151 def perform_action(self, board, location):
152 board.rotate_locations(self.data['rot_direction'])
155 class AllowChessMove(LocationAction):
156 TEXT = "Move like a %(chesspiece_name)s for one turn."
157 GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
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)