Coverage for src/retrospeedlab/games/hushbeat/grid.py: 91%

128 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-12-04 00:01 +0000

1from dataclasses import dataclass 

2from math import lcm 

3 

4from retrospeedlab.games.hushbeat.codes import ( 

5 CASE, 

6 EAST, 

7 EXIT, 

8 FLOOR, 

9 NORTH, 

10 PILLAR, 

11 RUG, 

12 SHADOW, 

13 SOLID, 

14 SOUTH, 

15 STEPS, 

16 WALL, 

17 WEST, 

18) 

19from retrospeedlab.games.hushbeat.plans import Cast, RoomPlan 

20from retrospeedlab.games.hushbeat.tuning import BEATS_PER_BAR, COLUMNS, ROWS 

21 

22TILE_CODES = { 

23 "#": WALL, 

24 "o": PILLAR, 

25 "=": CASE, 

26 "%": SHADOW, 

27 ",": RUG, 

28 ".": FLOOR, 

29 "S": FLOOR, 

30 "$": FLOOR, 

31 "*": FLOOR, 

32 "E": EXIT, 

33} 

34FLOOR_ANCHORS = "0123456789" 

35PEDESTAL_ANCHORS = "abcdefghij" 

36MOVES = {"R": EAST, "L": WEST, "U": NORTH, "D": SOUTH} 

37TURNS = {"r": EAST, "l": WEST, "u": NORTH, "d": SOUTH} 

38WAIT = "." 

39LASER_ON = "X" 

40MAX_LOOT = 8 

41 

42 

43@dataclass(slots=True, frozen=True) 

44class Patrol: 

45 steps: tuple[tuple[int, int], ...] 

46 sight: int 

47 

48 

49@dataclass(slots=True, frozen=True) 

50class Sweep: 

51 centers: tuple[int, ...] 

52 reach: int 

53 

54 

55@dataclass(slots=True, frozen=True) 

56class Beam: 

57 cells: tuple[int, ...] 

58 direction: int 

59 pattern: tuple[bool, ...] 

60 

61 

62@dataclass(slots=True, frozen=True) 

63class Eye: 

64 cell: int 

65 turns: tuple[int, ...] 

66 sight: int 

67 

68 

69@dataclass(slots=True, frozen=True) 

70class Watchers: 

71 patrols: tuple[Patrol, ...] 

72 sweeps: tuple[Sweep, ...] 

73 beams: tuple[Beam, ...] 

74 eyes: tuple[Eye, ...] 

75 period: int 

76 

77 

78@dataclass(slots=True, frozen=True) 

79class Room: 

80 name: str 

81 tiles: tuple[int, ...] 

82 start: int 

83 exit: int 

84 loot: tuple[int, ...] 

85 prize: int 

86 casts: tuple[Watchers, ...] 

87 caged: bool 

88 

89 

90def neighbor(cell: int, direction: int) -> int | None: 

91 column, row = cell % COLUMNS + STEPS[direction][0], cell // COLUMNS + STEPS[direction][1] 

92 if not (0 <= column < COLUMNS and 0 <= row < ROWS): 

93 return None 

94 return row * COLUMNS + column 

95 

96 

97def tile_code(mark: str) -> int: 

98 if mark in FLOOR_ANCHORS: 

99 return FLOOR 

100 if mark in PEDESTAL_ANCHORS: 

101 return PILLAR 

102 if mark not in TILE_CODES: 

103 raise ValueError(f"Unknown room mark {mark!r}") 

104 return TILE_CODES[mark] 

105 

106 

107def marked(plan: RoomPlan, mark: str) -> list[int]: 

108 return [ 

109 row * COLUMNS + column for row, line in enumerate(plan.rows) for column, each in enumerate(line) if each == mark 

110 ] 

111 

112 

113def only(plan: RoomPlan, mark: str) -> int: 

114 cells = marked(plan, mark) 

115 if len(cells) != 1: 

116 raise ValueError(f"{plan.name} needs exactly one {mark!r}, not {len(cells)}") 

117 return cells[0] 

118 

119 

120def stepped(cell: int, direction: int, plan: RoomPlan) -> int: 

121 target = neighbor(cell, direction) 

122 if target is None: 

123 raise ValueError(f"A route in {plan.name} walks off the grid at cell {cell}") 

124 return target 

125 

126 

127def patrol(plan: RoomPlan, tiles: tuple[int, ...], anchor: str, facing: str, route: str, sight: int) -> Patrol: 

128 cell, heading = only(plan, anchor), MOVES[facing] 

129 steps = [(cell, heading)] 

130 for mark in route: 

131 if mark in MOVES: 

132 heading = MOVES[mark] 

133 cell = stepped(cell, heading, plan) 

134 if tiles[cell] in SOLID or tiles[cell] == EXIT: 

135 raise ValueError(f"Guard {anchor} of {plan.name} walks into a solid tile at cell {cell}") 

136 elif mark in TURNS: 

137 heading = TURNS[mark] 

138 elif mark != WAIT: 

139 raise ValueError(f"Unknown route mark {mark!r} in {plan.name}") 

140 steps.append((cell, heading)) 

141 if steps[-1][0] != steps[0][0]: 

142 raise ValueError(f"Guard {anchor} of {plan.name} does not walk back to where it starts") 

143 return Patrol(tuple(steps[:-1]), sight) 

144 

145 

146def sweep(plan: RoomPlan, anchor: str, route: str, reach: int) -> Sweep: 

147 cell = only(plan, anchor) 

148 centers = [cell] 

149 for mark in route: 

150 if mark != WAIT: 

151 cell = stepped(cell, MOVES[mark], plan) 

152 centers.append(cell) 

153 if centers[-1] != centers[0]: 

154 raise ValueError(f"Lamp {anchor} of {plan.name} does not sweep back to where it starts") 

155 return Sweep(tuple(centers[:-1]), reach) 

156 

157 

158def beam(plan: RoomPlan, tiles: tuple[int, ...], anchor: str, direction: str, length: int, pattern: str) -> Beam: 

159 cells = [only(plan, anchor)] 

160 for _ in range(length - 1): 

161 cells.append(stepped(cells[-1], MOVES[direction], plan)) 

162 if any(tiles[cell] in SOLID for cell in cells): 

163 raise ValueError(f"Laser {anchor} of {plan.name} runs through a solid tile") 

164 return Beam(tuple(cells), MOVES[direction], tuple(mark == LASER_ON for mark in pattern)) 

165 

166 

167def watchers(plan: RoomPlan, tiles: tuple[int, ...], cast: Cast) -> Watchers: 

168 patrols = tuple(patrol(plan, tiles, g.anchor, g.facing, g.route, g.sight) for g in cast.guards) 

169 sweeps = tuple(sweep(plan, lamp.anchor, lamp.route, lamp.reach) for lamp in cast.lamps) 

170 beams = tuple(beam(plan, tiles, ray.anchor, ray.direction, ray.length, ray.pattern) for ray in cast.lasers) 

171 eyes = tuple( 

172 Eye(only(plan, eye.anchor), tuple(MOVES[turn] for turn in eye.turns), eye.sight) for eye in cast.cameras 

173 ) 

174 lengths = [len(p.steps) for p in patrols] + [len(s.centers) for s in sweeps] 

175 lengths += [len(b.pattern) for b in beams] + [len(e.turns) for e in eyes] 

176 return Watchers(patrols, sweeps, beams, eyes, lcm(BEATS_PER_BAR, *lengths)) 

177 

178 

179def compile_room(plan: RoomPlan) -> Room: 

180 if len(plan.rows) != ROWS or any(len(line) != COLUMNS for line in plan.rows): 

181 raise ValueError(f"A room is {COLUMNS}x{ROWS} tiles: {plan.name}") 

182 tiles = tuple(tile_code(mark) for line in plan.rows for mark in line) 

183 loot = sorted(marked(plan, "$") + marked(plan, "*")) 

184 if len(loot) > MAX_LOOT: 

185 raise ValueError(f"{plan.name} holds {len(loot)} loot of at most {MAX_LOOT}") 

186 casts = tuple(watchers(plan, tiles, cast) for cast in plan.casts) 

187 if not casts: 

188 raise ValueError(f"{plan.name} has no cast") 

189 return Room(plan.name, tiles, only(plan, "S"), only(plan, "E"), tuple(loot), only(plan, "*"), casts, plan.caged) 

190 

191 

192def hides(room: Room, cell: int) -> bool: 

193 return room.tiles[cell] == SHADOW