| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  | import dataclasses | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | import os | 
					
						
							|  |  |  | import typing | 
					
						
							|  |  |  | import math | 
					
						
							|  |  |  | import threading | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from BaseClasses import Item, MultiWorld, Tutorial, ItemClassification | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  | from Options import PerGameCommonOptions | 
					
						
							| 
									
										
										
										
											2024-05-21 18:19:37 -04:00
										 |  |  | import Patch | 
					
						
							|  |  |  | import settings | 
					
						
							|  |  |  | from worlds.AutoWorld import WebWorld, World | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .Client import DKC3SNIClient | 
					
						
							| 
									
										
										
										
											2022-08-20 10:46:44 -04:00
										 |  |  | from .Items import DKC3Item, ItemData, item_table, inventory_table, junk_table | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | from .Levels import level_list | 
					
						
							| 
									
										
										
										
											2024-05-21 18:19:37 -04:00
										 |  |  | from .Locations import DKC3Location, all_locations, setup_locations | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | from .Names import ItemName, LocationName | 
					
						
							| 
									
										
										
										
											2024-05-21 18:19:37 -04:00
										 |  |  | from .Options import DKC3Options, dkc3_option_groups | 
					
						
							|  |  |  | from .Regions import create_regions, connect_regions | 
					
						
							| 
									
										
										
										
											2022-07-28 19:51:22 -04:00
										 |  |  | from .Rom import LocalRom, patch_rom, get_base_rom_path, DKC3DeltaPatch | 
					
						
							| 
									
										
										
										
											2024-05-21 18:19:37 -04:00
										 |  |  | from .Rules import set_rules | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  | class DK3Settings(settings.Group): | 
					
						
							|  |  |  |     class RomFile(settings.UserFilePath): | 
					
						
							|  |  |  |         """File name of the DKC3 US rom""" | 
					
						
							|  |  |  |         copy_to = "Donkey Kong Country 3 - Dixie Kong's Double Trouble! (USA) (En,Fr).sfc" | 
					
						
							|  |  |  |         description = "DKC3 (US) ROM File" | 
					
						
							|  |  |  |         md5s = [DKC3DeltaPatch.hash] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rom_file: RomFile = RomFile(RomFile.copy_to) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | class DKC3Web(WebWorld): | 
					
						
							|  |  |  |     theme = "jungle" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setup_en = Tutorial( | 
					
						
							|  |  |  |         "Multiworld Setup Guide", | 
					
						
							|  |  |  |         "A guide to setting up the Donkey Kong Country 3 randomizer connected to an Archipelago Multiworld.", | 
					
						
							|  |  |  |         "English", | 
					
						
							|  |  |  |         "setup_en.md", | 
					
						
							|  |  |  |         "setup/en", | 
					
						
							|  |  |  |         ["PoryGone"] | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-05-21 18:19:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |     tutorials = [setup_en] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-21 18:19:37 -04:00
										 |  |  |     option_groups = dkc3_option_groups | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DKC3World(World): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Donkey Kong Country 3 is an action platforming game. | 
					
						
							|  |  |  |     Play as Dixie Kong and her baby cousin Kiddy as they try to solve the | 
					
						
							|  |  |  |     mystery of why Donkey Kong and Diddy disappeared while on vacation. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     game: str = "Donkey Kong Country 3" | 
					
						
							| 
									
										
										
										
											2023-07-05 22:39:35 +02:00
										 |  |  |     settings: typing.ClassVar[DK3Settings] | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     options_dataclass = DKC3Options | 
					
						
							|  |  |  |     options: DKC3Options | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |     topology_present = False | 
					
						
							|  |  |  |     #hint_blacklist = {LocationName.rocket_rush_flag} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item_name_to_id = {name: data.code for name, data in item_table.items()} | 
					
						
							|  |  |  |     location_name_to_id = all_locations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     active_level_list: typing.List[str] | 
					
						
							|  |  |  |     web = DKC3Web() | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     def __init__(self, world: MultiWorld, player: int): | 
					
						
							|  |  |  |         self.rom_name_available_event = threading.Event() | 
					
						
							|  |  |  |         super().__init__(world, player) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							| 
									
										
										
										
											2023-02-16 00:28:02 +01:00
										 |  |  |     def stage_assert_generate(cls, multiworld: MultiWorld): | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         rom_file = get_base_rom_path() | 
					
						
							|  |  |  |         if not os.path.exists(rom_file): | 
					
						
							|  |  |  |             raise FileNotFoundError(rom_file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _get_slot_data(self): | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |             #"death_link": self.options.death_link.value, | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |             "active_levels": self.active_level_list, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def fill_slot_data(self) -> dict: | 
					
						
							|  |  |  |         slot_data = self._get_slot_data() | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         for option_name in (attr.name for attr in dataclasses.fields(DKC3Options) | 
					
						
							|  |  |  |                             if attr not in dataclasses.fields(PerGameCommonOptions)): | 
					
						
							|  |  |  |             option = getattr(self.options, option_name) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |             slot_data[option_name] = option.value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return slot_data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 21:11:49 -04:00
										 |  |  |     def create_regions(self): | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         location_table = setup_locations(self) | 
					
						
							|  |  |  |         create_regions(self, location_table) | 
					
						
							| 
									
										
										
										
											2023-07-13 21:11:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Not generate basic | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         self.topology_present = self.options.level_shuffle.value | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         itempool: typing.List[DKC3Item] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Levels | 
					
						
							| 
									
										
										
										
											2022-07-25 15:34:31 -04:00
										 |  |  |         total_required_locations = 159 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         number_of_banana_birds = 0 | 
					
						
							|  |  |  |         # Rocket Rush Cog | 
					
						
							|  |  |  |         total_required_locations -= 1 | 
					
						
							|  |  |  |         number_of_cogs = 4 | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |         self.multiworld.get_location(LocationName.rocket_rush_flag, self.player).place_locked_item(self.create_item(ItemName.krematoa_cog)) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         number_of_bosses = 8 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         if self.options.goal == "knautilus": | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |             self.multiworld.get_location(LocationName.kastle_kaos, self.player).place_locked_item(self.create_item(ItemName.victory)) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |             number_of_bosses = 7 | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |             self.multiworld.get_location(LocationName.banana_bird_mother, self.player).place_locked_item(self.create_item(ItemName.victory)) | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |             number_of_banana_birds = self.options.number_of_banana_birds | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Bosses | 
					
						
							|  |  |  |         total_required_locations += number_of_bosses | 
					
						
							| 
									
										
										
										
											2022-08-20 10:46:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         # Secret Caves | 
					
						
							|  |  |  |         total_required_locations += 13 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         if self.options.kongsanity: | 
					
						
							| 
									
										
										
										
											2022-08-20 10:46:44 -04:00
										 |  |  |             total_required_locations += 39 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         ## Brothers Bear | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         if False:#self.options.include_trade_sequence: | 
					
						
							| 
									
										
										
										
											2022-07-25 15:34:31 -04:00
										 |  |  |             total_required_locations += 10 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         number_of_bonus_coins = (self.options.krematoa_bonus_coin_cost * 5) | 
					
						
							|  |  |  |         number_of_bonus_coins += math.ceil((85 - number_of_bonus_coins) * self.options.percentage_of_extra_bonus_coins / 100) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 10:42:33 +02:00
										 |  |  |         itempool += [self.create_item(ItemName.bonus_coin) for _ in range(number_of_bonus_coins)] | 
					
						
							|  |  |  |         itempool += [self.create_item(ItemName.dk_coin) for _ in range(41)] | 
					
						
							|  |  |  |         itempool += [self.create_item(ItemName.banana_bird) for _ in range(number_of_banana_birds)] | 
					
						
							|  |  |  |         itempool += [self.create_item(ItemName.krematoa_cog) for _ in range(number_of_cogs)] | 
					
						
							|  |  |  |         itempool += [self.create_item(ItemName.progressive_boat) for _ in range(3)] | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         total_junk_count = total_required_locations - len(itempool) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-20 10:46:44 -04:00
										 |  |  |         junk_pool = [] | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |         for item_name in self.multiworld.random.choices(list(junk_table.keys()), k=total_junk_count): | 
					
						
							| 
									
										
										
										
											2022-10-20 10:42:33 +02:00
										 |  |  |             junk_pool.append(self.create_item(item_name)) | 
					
						
							| 
									
										
										
										
											2022-08-20 10:46:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         itempool += junk_pool | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.active_level_list = level_list.copy() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         if self.options.level_shuffle: | 
					
						
							|  |  |  |             self.random.shuffle(self.active_level_list) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         connect_regions(self, self.active_level_list) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |         self.multiworld.itempool += itempool | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def generate_output(self, output_directory: str): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             rom = LocalRom(get_base_rom_path()) | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |             patch_rom(self, rom, self.active_level_list) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             self.active_level_list.append(LocationName.rocket_rush_region) | 
					
						
							| 
									
										
										
										
											2022-07-28 19:51:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 22:40:31 -04:00
										 |  |  |             rompath = os.path.join(output_directory, f"{self.multiworld.get_out_file_name_base(self.player)}.sfc") | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |             rom.write_to_file(rompath) | 
					
						
							|  |  |  |             self.rom_name = rom.name | 
					
						
							| 
									
										
										
										
											2022-07-28 19:51:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |             patch = DKC3DeltaPatch(os.path.splitext(rompath)[0]+DKC3DeltaPatch.patch_file_ending, player=self.player, | 
					
						
							|  |  |  |                                    player_name=self.multiworld.player_name[self.player], patched_path=rompath) | 
					
						
							| 
									
										
										
										
											2022-07-28 19:51:22 -04:00
										 |  |  |             patch.write() | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         except: | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |             self.rom_name_available_event.set()  # make sure threading continues and errors are collected | 
					
						
							| 
									
										
										
										
											2022-07-28 19:51:22 -04:00
										 |  |  |             if os.path.exists(rompath): | 
					
						
							|  |  |  |                 os.unlink(rompath) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def modify_multidata(self, multidata: dict): | 
					
						
							|  |  |  |         import base64 | 
					
						
							|  |  |  |         # wait for self.rom_name to be available. | 
					
						
							|  |  |  |         self.rom_name_available_event.wait() | 
					
						
							|  |  |  |         rom_name = getattr(self, "rom_name", None) | 
					
						
							|  |  |  |         # we skip in case of error, so that the original error in the output thread is the one that gets raised | 
					
						
							|  |  |  |         if rom_name: | 
					
						
							|  |  |  |             new_name = base64.b64encode(bytes(self.rom_name)).decode() | 
					
						
							| 
									
										
										
										
											2022-10-31 21:41:21 -05:00
										 |  |  |             multidata["connect_names"][new_name] = multidata["connect_names"][self.multiworld.player_name[self.player]] | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |     def extend_hint_information(self, hint_data: typing.Dict[int, typing.Dict[int, str]]): | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |         if self.topology_present: | 
					
						
							|  |  |  |             world_names = [ | 
					
						
							|  |  |  |             LocationName.lake_orangatanga_region, | 
					
						
							|  |  |  |             LocationName.kremwood_forest_region, | 
					
						
							|  |  |  |             LocationName.cotton_top_cove_region, | 
					
						
							|  |  |  |             LocationName.mekanos_region, | 
					
						
							|  |  |  |             LocationName.k3_region, | 
					
						
							|  |  |  |             LocationName.razor_ridge_region, | 
					
						
							|  |  |  |             LocationName.kaos_kore_region, | 
					
						
							|  |  |  |             LocationName.krematoa_region, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |             er_hint_data = {} | 
					
						
							|  |  |  |             for world_index in range(len(world_names)): | 
					
						
							|  |  |  |                 for level_index in range(5): | 
					
						
							| 
									
										
										
										
											2024-04-11 18:53:52 -04:00
										 |  |  |                     level_id: int = world_index * 5 + level_index | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if level_id >= len(self.active_level_list): | 
					
						
							|  |  |  |                         break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     level_region = self.multiworld.get_region(self.active_level_list[level_id], self.player) | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |                     for location in level_region.locations: | 
					
						
							|  |  |  |                         er_hint_data[location.address] = world_names[world_index] | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             hint_data[self.player] = er_hint_data | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def create_item(self, name: str, force_non_progression=False) -> Item: | 
					
						
							|  |  |  |         data = item_table[name] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if force_non_progression: | 
					
						
							|  |  |  |             classification = ItemClassification.filler | 
					
						
							|  |  |  |         elif data.progression: | 
					
						
							|  |  |  |             classification = ItemClassification.progression | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             classification = ItemClassification.filler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         created_item = DKC3Item(name, classification, data.code, self.player) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return created_item | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 01:54:05 -04:00
										 |  |  |     def get_filler_item_name(self) -> str: | 
					
						
							|  |  |  |         return self.multiworld.random.choice(list(junk_table.keys())) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 01:02:25 -04:00
										 |  |  |     def set_rules(self): | 
					
						
							| 
									
										
										
										
											2024-02-27 22:53:13 -05:00
										 |  |  |         set_rules(self) |