test_district_teleport.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from types import SimpleNamespace
  2. import unittest
  3. from unittest.mock import Mock
  4. from test_render_optimizations import load_definitions
  5. class DistrictTeleportTest(unittest.TestCase):
  6. def setUp(self):
  7. self.cl = SimpleNamespace(district_idx=1, district_all_finished=False, map_x=5, map_y=4,
  8. district_square_grid=dict(x1=1, y1=1, x2=8, y2=7),
  9. obstacle_instances={(1, 3): object()}, recal_npc_paths=False,
  10. get_bomb_instance=lambda x, y: [(x, y)] if (x, y) == (2, 3) else [])
  11. self.npc = SimpleNamespace(x=4, y=3, effects_front=[], set_xy=Mock())
  12. self.hero = SimpleNamespace(x=2, y=1, district_locked=True)
  13. self.sound = Mock()
  14. class Skill:
  15. def __init__(self, user, to, skill_instances):
  16. self.user, self.to = user, to
  17. self.effect_instances = []
  18. self.choose = Mock(side_effect=lambda points: points[-1])
  19. namespace = load_definitions("client_local/skill/parametric2_skill.py", {
  20. "Skill": Skill, "level": SimpleNamespace(current_level=self.cl),
  21. "random": SimpleNamespace(choice=self.choose), "EffectInstance": Mock(), "sound_player": self.sound,
  22. }, classes={"CustomRandomInDistrictTeleportEscape": None,
  23. "CustomRandomInDistrictTeleportEscapeClosest": None})
  24. self.teleport = namespace["CustomRandomInDistrictTeleportEscape"]
  25. self.teleport_closest = namespace["CustomRandomInDistrictTeleportEscapeClosest"]
  26. def cast(self):
  27. self.teleport(self.npc, self.hero, {"avoid_range": 1}, [])
  28. def test_uses_active_offline_district_and_filters_landing_tiles(self):
  29. self.cast()
  30. candidates, = self.choose.call_args.args
  31. self.assertEqual(set(candidates), {(3, 3), (4, 1), (4, 2)})
  32. self.npc.set_xy.assert_called_once_with(4, 2)
  33. self.assertTrue(self.cl.recal_npc_paths)
  34. self.sound.play.assert_called_once_with("X08_01")
  35. def test_does_not_cast_before_entering_or_after_finishing_or_outside_district(self):
  36. self.cl.district_idx = 0
  37. self.cast()
  38. self.cl.district_idx, self.hero.district_locked = 1, False
  39. self.cast()
  40. self.hero.district_locked, self.cl.district_all_finished = True, True
  41. self.cast()
  42. self.cl.district_all_finished, self.hero.x = False, 0
  43. self.cast()
  44. self.choose.assert_not_called()
  45. self.npc.set_xy.assert_not_called()
  46. def test_closest_randomizes_only_over_equally_near_legal_tiles(self):
  47. self.cl.map_x = self.cl.map_y = 7
  48. self.cl.district_square_grid = dict(x1=1, y1=1, x2=6, y2=6)
  49. self.cl.obstacle_instances = {(1, 3): object()}
  50. self.cl.get_bomb_instance = lambda x, y: [(x, y)] if (x, y) == (3, 5) else []
  51. self.hero.x = self.hero.y = 3
  52. self.npc.x = self.npc.y = 6
  53. self.teleport_closest(self.npc, self.hero, {"avoid_range": 1}, [])
  54. candidates, = self.choose.call_args.args
  55. self.assertEqual(set(candidates), {(3, 1), (5, 3)})
  56. self.npc.set_xy.assert_called_once_with(5, 3)
  57. self.cl.obstacle_instances.update({(3, 1): object(), (5, 3): object()})
  58. self.teleport_closest(self.npc, self.hero, {"avoid_range": 1}, [])
  59. candidates, = self.choose.call_args.args
  60. self.assertEqual(set(candidates), {(1, 2), (1, 4), (2, 1), (2, 5), (4, 1), (4, 5), (5, 2), (5, 4)})