1919#include " PokemonFRLG_RngNavigation.h"
2020#include " PokemonFRLG_HardReset.h"
2121#include " PokemonFRLG_RngCalibration.h"
22+ #include " PokemonFRLG_RngLoopRoutines.h"
2223#include " PokemonFRLG_GiftRng.h"
2324
2425namespace PokemonAutomation {
@@ -227,10 +228,6 @@ GiftRng::GiftRng()
227228
228229
229230
230- bool GiftRng::have_hit_target (SingleSwitchProgramEnvironment& env, const uint32_t & TARGET_SEED , const AdvRngState& hit){
231- return (hit.seed == TARGET_SEED ) && (hit.advance == ADVANCES );
232- }
233-
234231void GiftRng::program (SingleSwitchProgramEnvironment& env, ProControllerContext& context){
235232 /*
236233 * Settings: Text Speed fast
@@ -324,8 +321,6 @@ void GiftRng::program(SingleSwitchProgramEnvironment& env, ProControllerContext&
324321
325322 static const uint64_t CONTINUE_SCREEN_FRAMES = 200 ;
326323
327- static const double SEED_BUMPS [] = {0 , 1 , -1 , 2 , -2 };
328-
329324 const uint64_t INITIAL_ADVANCES_RADIUS = USE_TEACHY_TV ? 4096 : 1024 ;
330325
331326 const uint8_t MAX_HISTORY_LENGTH = USE_TEACHY_TV ? 2 : 10 ;
@@ -344,24 +339,14 @@ void GiftRng::program(SingleSwitchProgramEnvironment& env, ProControllerContext&
344339 AdvRngSearcher searcher (TARGET_SEED , ADVANCES , AdvRngMethod::Method1);
345340 AdvPokemonResult target_result = searcher.generate_pokemon ();
346341 RNG_TARGET .set_target (target_result, GENDER_THRESHOLD );
347- env.log (" Target PID: " + to_hex_string (target_result.pid ));
348- env.log (" Target Nature: " + nature_to_string (target_result.nature ));
349- env.log (" Target IVs:" );
350- env.log (" HP: " + std::to_string (target_result.ivs .hp ));
351- env.log (" Atk: " + std::to_string (target_result.ivs .attack ));
352- env.log (" Def: " + std::to_string (target_result.ivs .defense ));
353- env.log (" SpA: " + std::to_string (target_result.ivs .spatk ));
354- env.log (" SpD: " + std::to_string (target_result.ivs .spdef ));
355- env.log (" Spe: " + std::to_string (target_result.ivs .speed ));
342+ log_target_pokemon (env.console , target_result);
356343
357344 RngCalibrations calibrations = {
358345 RNG_CALIBRATION .seed_calibration / FRLG_FRAME_DURATION ,
359346 RNG_CALIBRATION .csf_calibration ,
360347 RNG_CALIBRATION .advances_calibration
361348 };
362- env.log (" Initial Seed calibration (frames): " + std::to_string (calibrations.seed_offset ));
363- env.log (" Initial CSF calibration (frames): " + std::to_string (calibrations.csf_offset ));
364- env.log (" Initial In-game calibration (frames x2): " + std::to_string (calibrations.ingame_offset ));
349+ log_calibrations (env.console , calibrations, true );
365350
366351 Milliseconds launch_delay = INITIAL_LAUNCH_DELAY ;
367352
@@ -373,7 +358,7 @@ void GiftRng::program(SingleSwitchProgramEnvironment& env, ProControllerContext&
373358 while (true ){
374359 if (calibration_history.results .size () > 0 ){
375360 env.log (" Checking for nonshiny target hit..." );
376- if (have_hit_target (env, TARGET_SEED , calibration_history.results .back ())){
361+ if (have_hit_target (TARGET_SEED , ADVANCES , calibration_history.results .back ())){
377362 env.log (" Target Hit!" );
378363 stats.nonshiny ++;
379364 break ;
@@ -410,8 +395,7 @@ void GiftRng::program(SingleSwitchProgramEnvironment& env, ProControllerContext&
410395 }
411396
412397 // if previous resets had uncertain advances, slightly modify the seed delay to try to hit a different target
413- double seed_bump = SEED_BUMPS [uncertain_history.results .size () % 5 ];
414- calibrations.seed_offset += seed_bump;
398+ apply_seed_bump (calibrations, uncertain_history);
415399
416400 uint64_t ingame_advances = ADVANCES - CONTINUE_SCREEN_FRAMES ;
417401
@@ -459,60 +443,23 @@ void GiftRng::program(SingleSwitchProgramEnvironment& env, ProControllerContext&
459443 AdvRngFilters filters = observation_to_filters (pokemon, BASE_STATS );
460444 RNG_FILTERS .set (filters);
461445
462- std::vector<AdvRngState> search_hits = get_search_results (env.console , searcher, filters, SEED_VALUES , ADVANCES , advances_radius, GENDER_THRESHOLD );
463- RNG_CALIBRATION .set_hits (search_hits);
464- bool force_finish = (
465- (MAX_RARE_CANDIES == 0 )
466- || all_equal (search_hits)
467- || all_indistinguishable (search_hits, searcher, GENDER_THRESHOLD )
468- );
469- bool finished = update_history (
470- env.console , uncertain_history, calibration_history, MAX_HISTORY_LENGTH ,
471- calibrations, search_hits, 1 , 2 , force_finish
472- );
473-
474- for (uint64_t i=0 ; i<MAX_RARE_CANDIES ; i++){
475- if (finished){
476- break ;
477- }
478-
479- bool failed = use_rare_candy (env.console , context, LANGUAGE , pokemon, filters, BASE_STATS , AdvRngMethod::Method1, false , i == 0 );
480- if (failed){
481- update_history (
482- env.console , uncertain_history, calibration_history,
483- MAX_HISTORY_LENGTH , calibrations, search_hits, 1 , 2 , true
484- );
485- stats.errors ++;
486- send_program_recoverable_error_notification (
487- env, NOTIFICATION_ERROR_RECOVERABLE ,
488- " Failed to use Rare Candy."
489- );
446+ std::vector<AdvRngState> search_hits = refine_calibration_with_rare_candy (
447+ env, context, LANGUAGE , pokemon, filters, BASE_STATS ,
448+ uncertain_history, calibration_history, calibrations,
449+ MAX_HISTORY_LENGTH , MAX_RARE_CANDIES , AdvRngMethod::Method1, false ,
450+ stats.errors , NOTIFICATION_ERROR_RECOVERABLE ,
451+ [&](AdvRngFilters& f){
452+ return get_search_results (env.console , searcher, f, SEED_VALUES , ADVANCES , advances_radius, GENDER_THRESHOLD );
453+ },
454+ [&](const std::vector<AdvRngState>& h){ RNG_CALIBRATION .set_hits (h); },
455+ [&](const AdvRngFilters& f){ RNG_FILTERS .set (f); },
456+ [&](const std::vector<AdvRngState>& h){
457+ return all_equal (h) || all_indistinguishable (h, searcher, GENDER_THRESHOLD );
490458 }
491- RNG_FILTERS .set (filters);
492-
493- search_hits = get_search_results (env.console , searcher, filters, SEED_VALUES , ADVANCES , advances_radius, GENDER_THRESHOLD );
494- RNG_CALIBRATION .set_hits (search_hits);
495-
496- force_finish = (
497- failed
498- || (i == (MAX_RARE_CANDIES - 1 ))
499- || all_equal (search_hits)
500- || all_indistinguishable (search_hits, searcher, GENDER_THRESHOLD )
501- );
502- finished = update_history (
503- env.console , uncertain_history,
504- calibration_history, MAX_HISTORY_LENGTH ,
505- calibrations, search_hits,
506- 1 , 2 , force_finish
507- );
508- }
459+ );
509460
510461 env.log (" RNG search finished." );
511- if (search_hits.size () == 0 ){
512- failed_searches++;
513- }else {
514- failed_searches = 0 ;
515- }
462+ update_failed_searches (failed_searches, search_hits);
516463
517464 }
518465
0 commit comments