Coverage for src/retrospeedlab/games/quilt_night/patterns.py: 100%

105 statements  

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

1import math 

2from collections.abc import Callable 

3 

4import pyxel 

5 

6from retrospeedlab.games.quilt_night.codes import ( 

7 CATHEDRAL_WINDOW, 

8 DRUNKARDS_PATH, 

9 FLYING_GEESE, 

10 LOG_CABIN, 

11 LONE_STAR, 

12 NINE_PATCH, 

13 WEDDING_RING, 

14) 

15from retrospeedlab.games.quilt_night.palette import ( 

16 CREAM, 

17 DUSK, 

18 GOLD, 

19 MOON, 

20 NIGHT, 

21 OCHRE, 

22 PLUM, 

23 ROSE, 

24 RUST, 

25 SAGE, 

26 SKY, 

27 SLATE, 

28 TEAL, 

29) 

30 

31Painter = Callable[[int, int, int, int, int], int] 

32HEX = "0123456789abcdef" 

33LIGHTS = (CREAM, GOLD, SAGE, MOON) 

34DARKS = (PLUM, TEAL, SKY, RUST) 

35GEESE = (RUST, OCHRE, GOLD, ROSE, PLUM, TEAL) 

36WINDOWS = (ROSE, SKY, SAGE, GOLD, PLUM) 

37RINGS = (ROSE, GOLD, SKY, SAGE) 

38STAR_BANDS = (GOLD, OCHRE, RUST, ROSE, PLUM, SKY, TEAL, SAGE) 

39EIGHTH = math.pi / 4 

40 

41 

42def log_cabin(x: int, y: int, block: int, width: int, height: int) -> int: 

43 across, down = x // block, y // block 

44 u, v = x % block, y % block 

45 if (across + down) % 2: 

46 u = block - 1 - u 

47 strip = max(1, block // 8) 

48 reach = max(abs(2 * u + 1 - block), abs(2 * v + 1 - block)) // 2 

49 if reach < strip: 

50 return RUST 

51 ring = (reach - strip) // strip 

52 if u + v < block - 1: 

53 return LIGHTS[ring % len(LIGHTS)] 

54 return DARKS[ring % len(DARKS)] 

55 

56 

57def flying_geese(x: int, y: int, block: int, width: int, height: int) -> int: 

58 half = block // 2 

59 u, v = x % block, y % half 

60 lean = abs(2 * u + 1 - block) - 2 * v 

61 if lean <= 0: 

62 return GEESE[(x // block + y // half) % len(GEESE)] if lean < -1 else CREAM 

63 return SKY 

64 

65 

66def nine_patch(x: int, y: int, block: int, width: int, height: int) -> int: 

67 across, down = x // block, y // block 

68 u, v = x % block, y % block 

69 if (across + down) % 2 == 0: 

70 third = block // 3 

71 return ROSE if (u // third + v // third) % 2 == 0 else CREAM 

72 stitch = max(4, block // 3) 

73 return CREAM if (u + v) % stitch == 0 and u % 2 == 0 else SAGE 

74 

75 

76def drunkards_path(x: int, y: int, block: int, width: int, height: int) -> int: 

77 across, down = x // block, y // block 

78 u, v = x % block, y % block 

79 turn = (across * 3 + down * 5 + across * down) % 4 

80 corner_x = block * (turn % 2) 

81 corner_y = block * (turn // 2) 

82 radius = block * 2 // 3 

83 distance = math.hypot(u + 0.5 - corner_x, v + 0.5 - corner_y) 

84 swap = (across + down) % 2 

85 inside = distance < radius 

86 if abs(distance - radius) < 0.8: 

87 return GOLD 

88 return (TEAL if inside else CREAM) if swap else (CREAM if inside else TEAL) 

89 

90 

91def cathedral_window(x: int, y: int, block: int, width: int, height: int) -> int: 

92 across, down = x // block, y // block 

93 u, v = x % block, y % block 

94 radius = block / 2 + 1 

95 nearest = min(math.hypot(u + 0.5 - cx, v + 0.5 - cy) for cx in (0, block) for cy in (0, block)) 

96 if nearest < radius - 1: 

97 return CREAM 

98 if nearest < radius: 

99 return MOON 

100 return WINDOWS[(across * 2 + down * 3) % len(WINDOWS)] 

101 

102 

103def wedding_ring(x: int, y: int, block: int, width: int, height: int) -> int: 

104 radius = block * 0.62 

105 hits: list[int] = [] 

106 for across in (x // block - 1, x // block, x // block + 1): 

107 for down in (y // block - 1, y // block, y // block + 1): 

108 cx, cy = (across + 0.5) * block, (down + 0.5) * block 

109 distance = math.hypot(x + 0.5 - cx, y + 0.5 - cy) 

110 if abs(distance - radius) < 1.6: 

111 quarter = int((math.atan2(y + 0.5 - cy, x + 0.5 - cx) + math.pi) / (math.pi / 2)) % 4 

112 hits.append(RINGS[(quarter + across + down) % len(RINGS)]) 

113 if len(hits) > 1: 

114 return RUST 

115 if hits: 

116 return hits[0] 

117 return MOON if (x // 2 + y // 2) % 6 == 0 and (x + y) % 2 == 0 else CREAM 

118 

119 

120def lone_star(x: int, y: int, block: int, width: int, height: int) -> int: 

121 dx, dy = x + 0.5 - width / 2, y + 0.5 - height / 2 

122 distance = math.hypot(dx, dy) 

123 outer = min(width, height) * 0.48 

124 inner = outer * 0.42 

125 turn = (math.atan2(dy, dx) % EIGHTH) / EIGHTH 

126 reach = inner + (outer - inner) * (1 - abs(2 * turn - 1)) 

127 if distance < reach: 

128 band = max(2, block // 4) 

129 if int(distance) % band == 0: 

130 return CREAM 

131 return STAR_BANDS[int(distance) // band % len(STAR_BANDS)] 

132 sparkle = (x * 7 + y * 13) % 53 == 0 

133 return CREAM if sparkle else TEAL 

134 

135 

136def night_cloth(x: int, y: int, block: int, width: int, height: int) -> int: 

137 if (x + y) % block == 0 or (x - y) % block == 0: 

138 return DUSK 

139 if (x * 5 + y * 3) % 97 == 0: 

140 return SLATE 

141 return NIGHT 

142 

143 

144PAINTERS: dict[int, Painter] = { 

145 LOG_CABIN: log_cabin, 

146 FLYING_GEESE: flying_geese, 

147 NINE_PATCH: nine_patch, 

148 DRUNKARDS_PATH: drunkards_path, 

149 CATHEDRAL_WINDOW: cathedral_window, 

150 WEDDING_RING: wedding_ring, 

151 LONE_STAR: lone_star, 

152} 

153 

154 

155def paint(image: int, spot: tuple[int, int], size: tuple[int, int], block: int, painter: Painter) -> None: 

156 width, height = size 

157 rows = ["".join(HEX[painter(x, y, block, width, height)] for x in range(width)) for y in range(height)] 

158 pyxel.images[image].set(spot[0], spot[1], rows)