1 from naja.constants import BITS
4 class LocationAction(object):
6 An action that may be performed on a location.
12 def __init__(self, required_bits, **data):
14 for bit in required_bits:
15 # Convert names to numbers if applicable.
16 bits.add(BITS.get(bit, bit))
17 self.required_bits = frozenset(bits)
21 return self.TEXT % self.data
23 def check_available(self, player):
24 return player.bits.check_bits(self.required_bits)
26 def perform_action(self, board, location):
27 raise NotImplementedError("TODO")
29 def check_and_clear_MSB(self, player):
30 if player.bits.check_bit(BITS.MSB):
31 player.bits.clear_bit(BITS.MSB)
37 return {'required_bits': list(self.required_bits),
39 'action_class': self.__class__.__name__}
42 class DoNothing(LocationAction):
45 def perform_action(self, board, location):
49 class LoseHealthOrMSB(LocationAction):
50 TEXT = "Lose health or MSB."
53 def perform_action(self, board, location):
54 if not self.check_and_clear_MSB(board.player):
58 class SetBits(LocationAction):
59 TEXT = "Set bits specified by this location."
61 def perform_action(self, board, location):
62 board.player.bits.set_bits(location.bitwise_operand)
65 class ToggleBits(LocationAction):
66 TEXT = "Toggle bits specified by this location."
68 def perform_action(self, board, location):
69 board.player.bits.toggle_bits(location.bitwise_operand)
72 class LoseHealthOrMSBAndSetBits(LocationAction):
73 TEXT = "Lose health or MSB, then set bits specified by this location."
76 def perform_action(self, board, location):
77 if not self.check_and_clear_MSB(board.player):
79 board.player.bits.set_bits(location.bitwise_operand)
82 class AcquireWinToken(LocationAction):
83 TEXT = "Acquire a win token, then clear all high bits."
86 def perform_action(self, board, location):
87 if self.check_and_clear_MSB(board.player):
88 board.acquire_win_token()
89 board.player.bits.clear_bits(set([
90 BITS.CYAN, BITS.MAGENTA, BITS.YELLOW,
94 class GainHealthAndClearBitsOrMSB(LocationAction):
95 TEXT = "Gain health, then clear bits specified by this location or MSB."
98 def perform_action(self, board, location):
100 if not self.check_and_clear_MSB(board.player):
101 board.player.bits.clear_bits(location.bitwise_operand)
104 class ShiftLocations(LocationAction):
105 TEXT = "Shift board locations %(direction)s."
107 def perform_action(self, board, location):
108 board.shift_locations(self.data['direction'])