| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | import random | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  | from typing import Set | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  | from BaseClasses import get_seed | 
					
						
							|  |  |  | from . import SVTestCase, complete_options_with_default | 
					
						
							| 
									
										
										
										
											2023-10-10 15:30:20 -05:00
										 |  |  | from ..options import EntranceRandomization, ExcludeGingerIsland | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  | from ..regions import vanilla_regions, vanilla_connections, randomize_connections, RandomizationFlag, create_final_connections_and_regions | 
					
						
							|  |  |  | from ..strings.entrance_names import Entrance as EntranceName | 
					
						
							|  |  |  | from ..strings.region_names import Region as RegionName | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  | connections_by_name = {connection.name for connection in vanilla_connections} | 
					
						
							|  |  |  | regions_by_name = {region.name for region in vanilla_regions} | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestRegions(unittest.TestCase): | 
					
						
							|  |  |  |     def test_region_exits_lead_somewhere(self): | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |         for region in vanilla_regions: | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  |             with self.subTest(region=region): | 
					
						
							|  |  |  |                 for exit in region.exits: | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                     self.assertIn(exit, connections_by_name, | 
					
						
							|  |  |  |                                   f"{region.name} is leading to {exit} but it does not exist.") | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_connection_lead_somewhere(self): | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |         for connection in vanilla_connections: | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  |             with self.subTest(connection=connection): | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                 self.assertIn(connection.destination, regions_by_name, | 
					
						
							|  |  |  |                               f"{connection.name} is leading to {connection.destination} but it does not exist.") | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  | def explore_connections_tree_up_to_blockers(blocked_entrances: Set[str], connections_by_name, regions_by_name): | 
					
						
							|  |  |  |     explored_entrances = set() | 
					
						
							|  |  |  |     explored_regions = set() | 
					
						
							|  |  |  |     entrances_to_explore = set() | 
					
						
							|  |  |  |     current_node_name = "Menu" | 
					
						
							|  |  |  |     current_node = regions_by_name[current_node_name] | 
					
						
							|  |  |  |     entrances_to_explore.update(current_node.exits) | 
					
						
							|  |  |  |     while entrances_to_explore: | 
					
						
							|  |  |  |         current_entrance_name = entrances_to_explore.pop() | 
					
						
							|  |  |  |         current_entrance = connections_by_name[current_entrance_name] | 
					
						
							|  |  |  |         current_node_name = current_entrance.destination | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         explored_entrances.add(current_entrance_name) | 
					
						
							|  |  |  |         explored_regions.add(current_node_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if current_entrance_name in blocked_entrances: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         current_node = regions_by_name[current_node_name] | 
					
						
							|  |  |  |         entrances_to_explore.update({entrance for entrance in current_node.exits if entrance not in explored_entrances}) | 
					
						
							|  |  |  |     return explored_regions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestEntranceRando(SVTestCase): | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |     def test_entrance_randomization(self): | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |         for option, flag in [(EntranceRandomization.option_pelican_town, RandomizationFlag.PELICAN_TOWN), | 
					
						
							|  |  |  |                              (EntranceRandomization.option_non_progression, RandomizationFlag.NON_PROGRESSION), | 
					
						
							|  |  |  |                              (EntranceRandomization.option_buildings, RandomizationFlag.BUILDINGS)]: | 
					
						
							|  |  |  |             sv_options = complete_options_with_default({ | 
					
						
							|  |  |  |                 EntranceRandomization.internal_name: option, | 
					
						
							|  |  |  |                 ExcludeGingerIsland.internal_name: ExcludeGingerIsland.option_false | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             seed = get_seed() | 
					
						
							|  |  |  |             rand = random.Random(seed) | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |             with self.subTest(flag=flag, msg=f"Seed: {seed}"): | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                 entrances, regions = create_final_connections_and_regions(sv_options) | 
					
						
							|  |  |  |                 _, randomized_connections = randomize_connections(rand, sv_options, regions, entrances) | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 for connection in vanilla_connections: | 
					
						
							|  |  |  |                     if flag in connection.flag: | 
					
						
							|  |  |  |                         connection_in_randomized = connection.name in randomized_connections | 
					
						
							|  |  |  |                         reverse_in_randomized = connection.reverse in randomized_connections | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                         self.assertTrue(connection_in_randomized, f"Connection {connection.name} should be randomized but it is not in the output.") | 
					
						
							|  |  |  |                         self.assertTrue(reverse_in_randomized, f"Connection {connection.reverse} should be randomized but it is not in the output.") | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 self.assertEqual(len(set(randomized_connections.values())), len(randomized_connections.values()), | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                                  f"Connections are duplicated in randomization.") | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_entrance_randomization_without_island(self): | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |         for option, flag in [(EntranceRandomization.option_pelican_town, RandomizationFlag.PELICAN_TOWN), | 
					
						
							|  |  |  |                              (EntranceRandomization.option_non_progression, RandomizationFlag.NON_PROGRESSION), | 
					
						
							|  |  |  |                              (EntranceRandomization.option_buildings, RandomizationFlag.BUILDINGS)]: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             sv_options = complete_options_with_default({ | 
					
						
							|  |  |  |                 EntranceRandomization.internal_name: option, | 
					
						
							|  |  |  |                 ExcludeGingerIsland.internal_name: ExcludeGingerIsland.option_true | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             seed = get_seed() | 
					
						
							|  |  |  |             rand = random.Random(seed) | 
					
						
							|  |  |  |             with self.subTest(option=option, flag=flag, seed=seed): | 
					
						
							|  |  |  |                 entrances, regions = create_final_connections_and_regions(sv_options) | 
					
						
							|  |  |  |                 _, randomized_connections = randomize_connections(rand, sv_options, regions, entrances) | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                 for connection in vanilla_connections: | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  |                     if flag in connection.flag: | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                         if RandomizationFlag.GINGER_ISLAND in connection.flag: | 
					
						
							|  |  |  |                             self.assertNotIn(connection.name, randomized_connections, | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                                              f"Connection {connection.name} should not be randomized but it is in the output.") | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                             self.assertNotIn(connection.reverse, randomized_connections, | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                                              f"Connection {connection.reverse} should not be randomized but it is in the output.") | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                         else: | 
					
						
							|  |  |  |                             self.assertIn(connection.name, randomized_connections, | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                                           f"Connection {connection.name} should be randomized but it is not in the output.") | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                             self.assertIn(connection.reverse, randomized_connections, | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                                           f"Connection {connection.reverse} should be randomized but it is not in the output.") | 
					
						
							| 
									
										
										
										
											2023-02-26 19:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 14:26:38 -04:00
										 |  |  |                 self.assertEqual(len(set(randomized_connections.values())), len(randomized_connections.values()), | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |                                  f"Connections are duplicated in randomization.") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_cannot_put_island_access_on_island(self): | 
					
						
							|  |  |  |         sv_options = complete_options_with_default({ | 
					
						
							|  |  |  |             EntranceRandomization.internal_name: EntranceRandomization.option_buildings, | 
					
						
							|  |  |  |             ExcludeGingerIsland.internal_name: ExcludeGingerIsland.option_false | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for i in range(0, 100 if self.skip_long_tests else 10000): | 
					
						
							|  |  |  |             seed = get_seed() | 
					
						
							|  |  |  |             rand = random.Random(seed) | 
					
						
							|  |  |  |             with self.subTest(msg=f"Seed: {seed}"): | 
					
						
							|  |  |  |                 entrances, regions = create_final_connections_and_regions(sv_options) | 
					
						
							|  |  |  |                 randomized_connections, randomized_data = randomize_connections(rand, sv_options, regions, entrances) | 
					
						
							|  |  |  |                 connections_by_name = {connection.name: connection for connection in randomized_connections} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 blocked_entrances = {EntranceName.use_island_obelisk, EntranceName.boat_to_ginger_island} | 
					
						
							|  |  |  |                 required_regions = {RegionName.wizard_tower, RegionName.boat_tunnel} | 
					
						
							|  |  |  |                 self.assert_can_reach_any_region_before_blockers(required_regions, blocked_entrances, connections_by_name, regions) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def assert_can_reach_any_region_before_blockers(self, required_regions, blocked_entrances, connections_by_name, regions_by_name): | 
					
						
							|  |  |  |         explored_regions = explore_connections_tree_up_to_blockers(blocked_entrances, connections_by_name, regions_by_name) | 
					
						
							|  |  |  |         self.assertTrue(any(region in explored_regions for region in required_regions)) | 
					
						
							| 
									
										
										
										
											2023-07-19 19:20:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 00:18:33 +02:00
										 |  |  | class TestEntranceClassifications(SVTestCase): | 
					
						
							| 
									
										
										
										
											2023-07-19 19:20:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_non_progression_are_all_accessible_with_empty_inventory(self): | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  |         for option, flag in [(EntranceRandomization.option_pelican_town, RandomizationFlag.PELICAN_TOWN), | 
					
						
							|  |  |  |                              (EntranceRandomization.option_non_progression, RandomizationFlag.NON_PROGRESSION)]: | 
					
						
							|  |  |  |             world_options = { | 
					
						
							|  |  |  |                 EntranceRandomization.internal_name: option | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             with self.solo_world_sub_test(world_options=world_options, flag=flag) as (multiworld, sv_world): | 
					
						
							| 
									
										
										
										
											2023-07-19 19:20:52 -04:00
										 |  |  |                 ap_entrances = {entrance.name: entrance for entrance in multiworld.get_entrances()} | 
					
						
							|  |  |  |                 for randomized_entrance in sv_world.randomized_entrances: | 
					
						
							|  |  |  |                     if randomized_entrance in ap_entrances: | 
					
						
							|  |  |  |                         ap_entrance_origin = ap_entrances[randomized_entrance] | 
					
						
							|  |  |  |                         self.assertTrue(ap_entrance_origin.access_rule(multiworld.state)) | 
					
						
							|  |  |  |                     if sv_world.randomized_entrances[randomized_entrance] in ap_entrances: | 
					
						
							|  |  |  |                         ap_entrance_destination = multiworld.get_entrance(sv_world.randomized_entrances[randomized_entrance], 1) | 
					
						
							|  |  |  |                         self.assertTrue(ap_entrance_destination.access_rule(multiworld.state)) | 
					
						
							| 
									
										
										
										
											2024-03-15 15:05:14 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_no_ginger_island_entrances_when_excluded(self): | 
					
						
							|  |  |  |         world_options = { | 
					
						
							|  |  |  |             EntranceRandomization.internal_name: EntranceRandomization.option_disabled, | 
					
						
							|  |  |  |             ExcludeGingerIsland.internal_name: ExcludeGingerIsland.option_true | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         with self.solo_world_sub_test(world_options=world_options) as (multiworld, _): | 
					
						
							|  |  |  |             ap_entrances = {entrance.name: entrance for entrance in multiworld.get_entrances()} | 
					
						
							|  |  |  |             entrance_data_by_name = {entrance.name: entrance for entrance in vanilla_connections} | 
					
						
							|  |  |  |             for entrance_name in ap_entrances: | 
					
						
							|  |  |  |                 entrance_data = entrance_data_by_name[entrance_name] | 
					
						
							|  |  |  |                 with self.subTest(f"{entrance_name}: {entrance_data.flag}"): | 
					
						
							|  |  |  |                     self.assertFalse(entrance_data.flag & RandomizationFlag.GINGER_ISLAND) |