test_bomb_index_and_boss_cache.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """Second-batch regression checks without starting the game or writing saves."""
  2. import math
  3. import random
  4. from types import SimpleNamespace
  5. import unittest
  6. from unittest.mock import Mock
  7. from test_render_optimizations import load_definitions, pixels, CountingSurface, ROOT
  8. import pygame
  9. class BombIndexTest(unittest.TestCase):
  10. def setUp(self):
  11. ns = load_definitions("client_local/level/level.py", {}, classes={"Level": {"get_bomb_instance"}})
  12. self.cl = ns["Level"]()
  13. self.cl.bomb_instances = []
  14. self.cl._bomb_position_index = {}
  15. self.cl._bomb_index_dirty = True
  16. self.cl.map_x = self.cl.map_y = 20
  17. self.cl.map_x_pos = self.cl.map_y_pos = 800
  18. self.cl.obstacle_instances = {}
  19. self.level_module = SimpleNamespace(current_level=self.cl)
  20. self.ticks = 0
  21. ns = {"level": self.level_module, "pygame": SimpleNamespace(time=SimpleNamespace(get_ticks=lambda: self.ticks)),
  22. "G": SimpleNamespace(GAME_SQUARE=40, HALF_GAME_SQUARE=20), "math": math,
  23. "updatable": SimpleNamespace(current_grid=lambda x, y: (int(x//40), int(y//40))),
  24. "bomb_direction": {"R": 1}}
  25. load_definitions("client_local/sprite/bomb_instance.py", ns, classes={"BombInstance": {
  26. "x", "y", "throwing", "_set_indexed_value", "setup", "uninstall", "set_restoration", "update_pos"}})
  27. load_definitions("client_local/sprite/throwable.py", ns, functions={"cal_y"},
  28. classes={"Throwable": {"throw_to", "throw", "get_points"}})
  29. self.Bomb = ns["BombInstance"]
  30. for name in ("throw_to", "throw", "get_points"):
  31. setattr(self.Bomb, name, getattr(ns["Throwable"], name))
  32. def bomb(self, x, y):
  33. b = self.Bomb()
  34. b.x, b.y, b.throwing = x, y, False
  35. b.x_pos, b.y_pos = x*40+20, y*40+20
  36. b.bomb_instances_list = self.cl.bomb_instances
  37. b.player = SimpleNamespace(restore_a_bomb=Mock())
  38. b.current_point = 0
  39. b.update_time_old = 0
  40. b.setup()
  41. return b
  42. def assert_index_matches_scan(self):
  43. for x in range(6):
  44. for y in range(6):
  45. expected = [b for b in self.cl.bomb_instances if b.x == x and b.y == y and not b.throwing]
  46. self.assertEqual(self.cl.get_bomb_instance(x, y), expected)
  47. def test_stacked_bombs_order_snapshot_and_chain_removal(self):
  48. a, b, c = [self.bomb(1, 2) for _ in range(3)]
  49. snapshot = self.cl.get_bomb_instance(1, 2)
  50. self.assertEqual(snapshot, [a, b, c])
  51. self.cl.get_bomb_instance(1, 2).clear()
  52. self.assertEqual(self.cl.get_bomb_instance(1, 2), snapshot)
  53. for bomb in snapshot:
  54. bomb.uninstall()
  55. self.assertNotIn(bomb, self.cl.get_bomb_instance(1, 2))
  56. bomb.player.restore_a_bomb.assert_called_once_with()
  57. self.assertEqual(self.cl.get_bomb_instance(1, 2), [])
  58. def test_throw_start_destination_and_landing_invalidate_immediately(self):
  59. for target in (3, 22): # Includes wrapped landing coordinates.
  60. bomb = self.bomb(1, 2)
  61. self.assertEqual(self.cl.get_bomb_instance(1, 2), [bomb])
  62. bomb.throw_to(target, 2, "R", True)
  63. self.assertEqual(self.cl.get_bomb_instance(1, 2), [])
  64. self.assertEqual(self.cl.get_bomb_instance(target % 20, 2), [])
  65. self.ticks += 100000
  66. bomb.throw()
  67. bomb.throw()
  68. self.assertFalse(bomb.throwing)
  69. self.assertEqual(self.cl.get_bomb_instance(target % 20, 2), [bomb])
  70. bomb.uninstall()
  71. def test_conveyor_and_floating_cross_grid_boundaries(self):
  72. for conveyor in (True, False):
  73. bomb = self.bomb(1, 2)
  74. self.cl.get_bomb_instance(1, 2)
  75. self.cl.block = object()
  76. self.cl.slide_orientation = {(1, 2): (1, 5)} if conveyor else {}
  77. bomb.floating = not conveyor
  78. bomb.floating_direction = "R"
  79. bomb.floating_speed = 5
  80. bomb.get_ruld_block = lambda: (0,) * 8
  81. def slide(*args):
  82. bomb.x_pos, bomb.y_pos = 201, 201
  83. bomb.slide = slide
  84. bomb.update_pos(100)
  85. self.assertEqual(self.cl.get_bomb_instance(1, 2), [])
  86. self.assertEqual(self.cl.get_bomb_instance(5, 5), [bomb])
  87. bomb.uninstall()
  88. def test_random_mutations_match_linear_reference(self):
  89. rng = random.Random(13)
  90. for _ in range(300):
  91. live = self.cl.bomb_instances
  92. action = rng.randrange(5)
  93. if not live or action == 0:
  94. self.bomb(rng.randrange(6), rng.randrange(6))
  95. else:
  96. b = rng.choice(live)
  97. if action == 1:
  98. b.x, b.y = rng.randrange(6), rng.randrange(6)
  99. elif action == 2:
  100. b.throwing = not b.throwing
  101. elif action == 3:
  102. b.uninstall()
  103. else:
  104. b.x, b.y = b.x, b.y
  105. self.assert_index_matches_scan()
  106. def test_unchanged_queries_reuse_index_and_old_level_stays_isolated(self):
  107. bomb = self.bomb(1, 2)
  108. self.cl.get_bomb_instance(1, 2)
  109. index = self.cl._bomb_position_index
  110. bomb.x, bomb.y, bomb.throwing = 1, 2, False
  111. self.assertFalse(self.cl._bomb_index_dirty)
  112. for _ in range(20):
  113. self.cl.get_bomb_instance(3, 4)
  114. self.assertIs(self.cl._bomb_position_index, index)
  115. new_level = SimpleNamespace(_bomb_index_dirty=False)
  116. self.level_module.current_level = new_level
  117. bomb.x = 5
  118. self.assertTrue(self.cl._bomb_index_dirty)
  119. self.assertFalse(new_level._bomb_index_dirty)
  120. class BossBarCacheTest(unittest.TestCase):
  121. def setUp(self):
  122. self.ticks = 0
  123. pg = SimpleNamespace(Surface=pygame.Surface, SRCALPHA=pygame.SRCALPHA,
  124. time=SimpleNamespace(get_ticks=lambda: self.ticks))
  125. self.ns = load_definitions("client_local/ui/game/blood_bar.py", {"pygame": pg, "math": math},
  126. functions={"_render_boss_blood_sheet"}, classes={
  127. "BossBloodBar": {"update", "set_blood", "set_max_blood"},
  128. "BossLongBloodBar": {"update", "set_blood", "set_max_blood"}})
  129. self.image = pygame.image.load(str(ROOT / "res/img/ui/game/img_bossLife2.png"))
  130. def bar(self, long=True):
  131. bar = self.ns["BossLongBloodBar" if long else "BossBloodBar"]()
  132. bar.rect = pygame.Rect(0, 0, 562 if long else 162, 20)
  133. bar.sheet = CountingSurface(bar.rect.size)
  134. bar.blood_slice_img = self.image
  135. bar.blood_slice_idx = 554 if long else 154
  136. bar.max_blood = 1000
  137. bar.blood_slice_last_update_time = 0
  138. bar.user = SimpleNamespace(remain_blood=1000, fake_blood=0)
  139. return bar
  140. def reference(self, bar, length):
  141. sheet = pygame.Surface(bar.rect.size, pygame.SRCALPHA, 32)
  142. for i in range(length):
  143. sheet.blit(bar.blood_slice_img, (i+2, 4))
  144. return sheet
  145. def test_every_visible_length_matches_original_overlapping_pixels(self):
  146. for long in (False, True):
  147. bar = self.bar(long)
  148. for length in range(-1, bar.rect.width + 4):
  149. bar.blood_slice_idx = length
  150. bar.set_blood(1000)
  151. self.assertEqual(pixels(bar.sheet), pixels(self.reference(bar, length)), (long, length))
  152. def test_animation_timing_healing_damage_and_fake_blood_unchanged(self):
  153. for long in (False, True):
  154. bar = self.bar(long)
  155. bar.set_blood(1000)
  156. last_time, length = 0, bar.blood_slice_idx
  157. expected = pixels(self.reference(bar, length))
  158. for tick in range(0, 30000, 25):
  159. self.ticks = tick
  160. bar.user.remain_blood = 200 if tick < 10000 else 900
  161. bar.user.fake_blood = 100 if tick > 20000 else 0
  162. if tick - last_time > (50 if long else 100):
  163. last_time = tick
  164. expected = pixels(self.reference(bar, length))
  165. target = math.ceil((bar.user.remain_blood - bar.user.fake_blood) * (554 if long else 154) / bar.max_blood)
  166. if target != length:
  167. delta = max(1, int(abs(length-target)/5)) if long else 1
  168. length += delta if target > length else -delta
  169. bar.update()
  170. self.assertEqual(bar.blood_slice_idx, length)
  171. self.assertEqual(pixels(bar.sheet), expected)
  172. self.assertTrue(bar.redraw_trigger)
  173. def test_stable_bar_does_not_allocate_or_clear_sheet(self):
  174. bar = self.bar()
  175. bar.set_blood(1000)
  176. sheet, strip = bar.sheet, bar._boss_strip
  177. fills = sheet.fills
  178. for tick in range(101, 3001, 101):
  179. self.ticks = tick
  180. bar.update()
  181. self.assertIs(bar.sheet, sheet)
  182. self.assertIs(bar._boss_strip, strip)
  183. self.assertEqual(sheet.fills, fills)
  184. def test_short_translucent_tail_and_replaced_sheet_or_texture(self):
  185. bar = self.bar()
  186. texture = pygame.Surface((5, 12), pygame.SRCALPHA)
  187. texture.fill((81, 160, 223, 91))
  188. bar.blood_slice_img = texture
  189. for length in (0, 1, 2, 3, 4, 5, 100):
  190. bar.blood_slice_idx = length
  191. bar.set_blood(1000)
  192. self.assertEqual(pixels(bar.sheet), pixels(self.reference(bar, length)))
  193. bar.sheet = pygame.Surface(bar.rect.size, pygame.SRCALPHA)
  194. bar.set_blood(1000)
  195. self.assertEqual(pixels(bar.sheet), pixels(self.reference(bar, 100)))
  196. if __name__ == "__main__":
  197. unittest.main()