| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- """Second-batch regression checks without starting the game or writing saves."""
- import math
- import random
- from types import SimpleNamespace
- import unittest
- from unittest.mock import Mock
- from test_render_optimizations import load_definitions, pixels, CountingSurface, ROOT
- import pygame
- class BombIndexTest(unittest.TestCase):
- def setUp(self):
- ns = load_definitions("client_local/level/level.py", {}, classes={"Level": {"get_bomb_instance"}})
- self.cl = ns["Level"]()
- self.cl.bomb_instances = []
- self.cl._bomb_position_index = {}
- self.cl._bomb_index_dirty = True
- self.cl.map_x = self.cl.map_y = 20
- self.cl.map_x_pos = self.cl.map_y_pos = 800
- self.cl.obstacle_instances = {}
- self.level_module = SimpleNamespace(current_level=self.cl)
- self.ticks = 0
- ns = {"level": self.level_module, "pygame": SimpleNamespace(time=SimpleNamespace(get_ticks=lambda: self.ticks)),
- "G": SimpleNamespace(GAME_SQUARE=40, HALF_GAME_SQUARE=20), "math": math,
- "updatable": SimpleNamespace(current_grid=lambda x, y: (int(x//40), int(y//40))),
- "bomb_direction": {"R": 1}}
- load_definitions("client_local/sprite/bomb_instance.py", ns, classes={"BombInstance": {
- "x", "y", "throwing", "_set_indexed_value", "setup", "uninstall", "set_restoration", "update_pos"}})
- load_definitions("client_local/sprite/throwable.py", ns, functions={"cal_y"},
- classes={"Throwable": {"throw_to", "throw", "get_points"}})
- self.Bomb = ns["BombInstance"]
- for name in ("throw_to", "throw", "get_points"):
- setattr(self.Bomb, name, getattr(ns["Throwable"], name))
- def bomb(self, x, y):
- b = self.Bomb()
- b.x, b.y, b.throwing = x, y, False
- b.x_pos, b.y_pos = x*40+20, y*40+20
- b.bomb_instances_list = self.cl.bomb_instances
- b.player = SimpleNamespace(restore_a_bomb=Mock())
- b.current_point = 0
- b.update_time_old = 0
- b.setup()
- return b
- def assert_index_matches_scan(self):
- for x in range(6):
- for y in range(6):
- expected = [b for b in self.cl.bomb_instances if b.x == x and b.y == y and not b.throwing]
- self.assertEqual(self.cl.get_bomb_instance(x, y), expected)
- def test_stacked_bombs_order_snapshot_and_chain_removal(self):
- a, b, c = [self.bomb(1, 2) for _ in range(3)]
- snapshot = self.cl.get_bomb_instance(1, 2)
- self.assertEqual(snapshot, [a, b, c])
- self.cl.get_bomb_instance(1, 2).clear()
- self.assertEqual(self.cl.get_bomb_instance(1, 2), snapshot)
- for bomb in snapshot:
- bomb.uninstall()
- self.assertNotIn(bomb, self.cl.get_bomb_instance(1, 2))
- bomb.player.restore_a_bomb.assert_called_once_with()
- self.assertEqual(self.cl.get_bomb_instance(1, 2), [])
- def test_throw_start_destination_and_landing_invalidate_immediately(self):
- for target in (3, 22): # Includes wrapped landing coordinates.
- bomb = self.bomb(1, 2)
- self.assertEqual(self.cl.get_bomb_instance(1, 2), [bomb])
- bomb.throw_to(target, 2, "R", True)
- self.assertEqual(self.cl.get_bomb_instance(1, 2), [])
- self.assertEqual(self.cl.get_bomb_instance(target % 20, 2), [])
- self.ticks += 100000
- bomb.throw()
- bomb.throw()
- self.assertFalse(bomb.throwing)
- self.assertEqual(self.cl.get_bomb_instance(target % 20, 2), [bomb])
- bomb.uninstall()
- def test_conveyor_and_floating_cross_grid_boundaries(self):
- for conveyor in (True, False):
- bomb = self.bomb(1, 2)
- self.cl.get_bomb_instance(1, 2)
- self.cl.block = object()
- self.cl.slide_orientation = {(1, 2): (1, 5)} if conveyor else {}
- bomb.floating = not conveyor
- bomb.floating_direction = "R"
- bomb.floating_speed = 5
- bomb.get_ruld_block = lambda: (0,) * 8
- def slide(*args):
- bomb.x_pos, bomb.y_pos = 201, 201
- bomb.slide = slide
- bomb.update_pos(100)
- self.assertEqual(self.cl.get_bomb_instance(1, 2), [])
- self.assertEqual(self.cl.get_bomb_instance(5, 5), [bomb])
- bomb.uninstall()
- def test_random_mutations_match_linear_reference(self):
- rng = random.Random(13)
- for _ in range(300):
- live = self.cl.bomb_instances
- action = rng.randrange(5)
- if not live or action == 0:
- self.bomb(rng.randrange(6), rng.randrange(6))
- else:
- b = rng.choice(live)
- if action == 1:
- b.x, b.y = rng.randrange(6), rng.randrange(6)
- elif action == 2:
- b.throwing = not b.throwing
- elif action == 3:
- b.uninstall()
- else:
- b.x, b.y = b.x, b.y
- self.assert_index_matches_scan()
- def test_unchanged_queries_reuse_index_and_old_level_stays_isolated(self):
- bomb = self.bomb(1, 2)
- self.cl.get_bomb_instance(1, 2)
- index = self.cl._bomb_position_index
- bomb.x, bomb.y, bomb.throwing = 1, 2, False
- self.assertFalse(self.cl._bomb_index_dirty)
- for _ in range(20):
- self.cl.get_bomb_instance(3, 4)
- self.assertIs(self.cl._bomb_position_index, index)
- new_level = SimpleNamespace(_bomb_index_dirty=False)
- self.level_module.current_level = new_level
- bomb.x = 5
- self.assertTrue(self.cl._bomb_index_dirty)
- self.assertFalse(new_level._bomb_index_dirty)
- class BossBarCacheTest(unittest.TestCase):
- def setUp(self):
- self.ticks = 0
- pg = SimpleNamespace(Surface=pygame.Surface, SRCALPHA=pygame.SRCALPHA,
- time=SimpleNamespace(get_ticks=lambda: self.ticks))
- self.ns = load_definitions("client_local/ui/game/blood_bar.py", {"pygame": pg, "math": math},
- functions={"_render_boss_blood_sheet"}, classes={
- "BossBloodBar": {"update", "set_blood", "set_max_blood"},
- "BossLongBloodBar": {"update", "set_blood", "set_max_blood"}})
- self.image = pygame.image.load(str(ROOT / "res/img/ui/game/img_bossLife2.png"))
- def bar(self, long=True):
- bar = self.ns["BossLongBloodBar" if long else "BossBloodBar"]()
- bar.rect = pygame.Rect(0, 0, 562 if long else 162, 20)
- bar.sheet = CountingSurface(bar.rect.size)
- bar.blood_slice_img = self.image
- bar.blood_slice_idx = 554 if long else 154
- bar.max_blood = 1000
- bar.blood_slice_last_update_time = 0
- bar.user = SimpleNamespace(remain_blood=1000, fake_blood=0)
- return bar
- def reference(self, bar, length):
- sheet = pygame.Surface(bar.rect.size, pygame.SRCALPHA, 32)
- for i in range(length):
- sheet.blit(bar.blood_slice_img, (i+2, 4))
- return sheet
- def test_every_visible_length_matches_original_overlapping_pixels(self):
- for long in (False, True):
- bar = self.bar(long)
- for length in range(-1, bar.rect.width + 4):
- bar.blood_slice_idx = length
- bar.set_blood(1000)
- self.assertEqual(pixels(bar.sheet), pixels(self.reference(bar, length)), (long, length))
- def test_animation_timing_healing_damage_and_fake_blood_unchanged(self):
- for long in (False, True):
- bar = self.bar(long)
- bar.set_blood(1000)
- last_time, length = 0, bar.blood_slice_idx
- expected = pixels(self.reference(bar, length))
- for tick in range(0, 30000, 25):
- self.ticks = tick
- bar.user.remain_blood = 200 if tick < 10000 else 900
- bar.user.fake_blood = 100 if tick > 20000 else 0
- if tick - last_time > (50 if long else 100):
- last_time = tick
- expected = pixels(self.reference(bar, length))
- target = math.ceil((bar.user.remain_blood - bar.user.fake_blood) * (554 if long else 154) / bar.max_blood)
- if target != length:
- delta = max(1, int(abs(length-target)/5)) if long else 1
- length += delta if target > length else -delta
- bar.update()
- self.assertEqual(bar.blood_slice_idx, length)
- self.assertEqual(pixels(bar.sheet), expected)
- self.assertTrue(bar.redraw_trigger)
- def test_stable_bar_does_not_allocate_or_clear_sheet(self):
- bar = self.bar()
- bar.set_blood(1000)
- sheet, strip = bar.sheet, bar._boss_strip
- fills = sheet.fills
- for tick in range(101, 3001, 101):
- self.ticks = tick
- bar.update()
- self.assertIs(bar.sheet, sheet)
- self.assertIs(bar._boss_strip, strip)
- self.assertEqual(sheet.fills, fills)
- def test_short_translucent_tail_and_replaced_sheet_or_texture(self):
- bar = self.bar()
- texture = pygame.Surface((5, 12), pygame.SRCALPHA)
- texture.fill((81, 160, 223, 91))
- bar.blood_slice_img = texture
- for length in (0, 1, 2, 3, 4, 5, 100):
- bar.blood_slice_idx = length
- bar.set_blood(1000)
- self.assertEqual(pixels(bar.sheet), pixels(self.reference(bar, length)))
- bar.sheet = pygame.Surface(bar.rect.size, pygame.SRCALPHA)
- bar.set_blood(1000)
- self.assertEqual(pixels(bar.sheet), pixels(self.reference(bar, 100)))
- if __name__ == "__main__":
- unittest.main()
|