|
| 1 | +/* RNG Program |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <format> |
| 8 | +#include <string> |
| 9 | +#include "Common/Cpp/Exceptions.h" |
| 10 | +#include "CommonFramework/Exceptions/OperationFailedException.h" |
| 11 | +#include "Pokemon/Pokemon_Strings.h" |
| 12 | +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h" |
| 13 | +#include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" |
| 14 | +#include "PokemonFRLG_RngProgram.h" |
| 15 | + |
| 16 | +namespace PokemonAutomation{ |
| 17 | +namespace NintendoSwitch{ |
| 18 | +namespace PokemonFRLG{ |
| 19 | + |
| 20 | + |
| 21 | +namespace{ |
| 22 | + |
| 23 | +std::string not_enough_free_slots_message(size_t party_size, size_t free_slots_needed){ |
| 24 | + if (free_slots_needed == 1){ |
| 25 | + return std::format( |
| 26 | + "Your party is full. Leave a party slot free so the program can check the {} it catches or receives.", |
| 27 | + Pokemon::STRING_POKEMON |
| 28 | + ); |
| 29 | + } |
| 30 | + return std::format( |
| 31 | + "Your party has {} {}. Leave at least {} party slots free for the {} the program catches or receives.", |
| 32 | + party_size, Pokemon::STRING_POKEMON, free_slots_needed, Pokemon::STRING_POKEMON |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +} // namespace |
| 37 | + |
| 38 | + |
| 39 | +void RngProgramInstance::check_free_party_slots( |
| 40 | + SingleSwitchProgramEnvironment& env, |
| 41 | + ProControllerContext& context |
| 42 | +){ |
| 43 | + const size_t free_slots_needed = free_party_slots_needed(); |
| 44 | + if (free_slots_needed == 0){ |
| 45 | + return; |
| 46 | + } |
| 47 | + Logger& logger = env.console.logger(); |
| 48 | + logger.log("Checking for " + std::to_string(free_slots_needed) + " free party slot(s)..."); |
| 49 | + try{ |
| 50 | + open_party_menu_from_overworld(env.console, context); |
| 51 | + }catch (OperationFailedException&){ |
| 52 | + logger.log("Unable to open the party menu. Skipping the free party slot check.", COLOR_RED); |
| 53 | + return; |
| 54 | + } |
| 55 | + context.wait_for_all_requests(); |
| 56 | + constexpr size_t MAX_PARTY_SIZE = 6; |
| 57 | + const size_t party_size = static_cast<size_t>(detect_last_occupied_party_slot(env.console)) + 1; |
| 58 | + if (MAX_PARTY_SIZE - party_size < free_slots_needed){ |
| 59 | + throw UserSetupError(logger, not_enough_free_slots_message(party_size, free_slots_needed)); |
| 60 | + } |
| 61 | + logger.log("Party has enough free slots."); |
| 62 | +} |
| 63 | + |
| 64 | +void RngProgramInstance::run_precheck( |
| 65 | + SingleSwitchProgramEnvironment& env, |
| 66 | + ProControllerContext& context |
| 67 | +){ |
| 68 | + home_black_border_check(env.console, context); |
| 69 | + check_free_party_slots(env, context); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace PokemonFRLG |
| 73 | +} // namespace NintendoSwitch |
| 74 | +} // namespace PokemonAutomation |
0 commit comments