|
| 1 | +/* Battle Dialog Detectors |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CommonTools/Images/SolidColorTest.h" |
| 8 | +#include "CommonTools/Images/ImageFilter.h" |
| 9 | +#include "PokemonFRLG/PokemonFRLG_Settings.h" |
| 10 | +#include "PokemonFRLG_BattleDialogs.h" |
| 11 | + |
| 12 | +namespace PokemonAutomation{ |
| 13 | +namespace NintendoSwitch{ |
| 14 | +namespace PokemonFRLG{ |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +BattleDialogDetector::BattleDialogDetector(Color color) |
| 19 | + : m_dialog_top_box(0.0372308, 0.750154, 0.925538, 0.00934615) |
| 20 | + , m_dialog_right_box(0.956615, 0.7595, 0.00615385, 0.185885) |
| 21 | + , m_dialog_top_jpn_box(0.043890, 0.749227, 0.910248, 0.008023) //jpn positions might work for other languages |
| 22 | + , m_dialog_right_jpn_box(0.950583, 0.751901, 0.003556, 0.199230) //dialog might get in the way though |
| 23 | +{} |
| 24 | +void BattleDialogDetector::make_overlays(VideoOverlaySet& items) const{ |
| 25 | + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; |
| 26 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_top_box)); |
| 27 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_right_box)); |
| 28 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_top_jpn_box)); |
| 29 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_right_jpn_box)); |
| 30 | +} |
| 31 | +bool BattleDialogDetector::detect(const ImageViewRGB32& screen){ |
| 32 | + ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); |
| 33 | + |
| 34 | + ImageViewRGB32 dialog_top_image = extract_box_reference(game_screen, m_dialog_top_box); |
| 35 | + ImageViewRGB32 dialog_right_image = extract_box_reference(game_screen, m_dialog_right_box); |
| 36 | + ImageViewRGB32 dialog_top_jpn_image = extract_box_reference(game_screen, m_dialog_top_jpn_box); |
| 37 | + ImageViewRGB32 dialog_right_jpn_image = extract_box_reference(game_screen, m_dialog_right_jpn_box); |
| 38 | + |
| 39 | + if ((is_solid(dialog_top_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 40 | + && is_solid(dialog_right_image, { 0.176, 0.357, 0.467 }, 0.25, 20)) |
| 41 | + || |
| 42 | + (is_solid(dialog_top_jpn_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 43 | + && is_solid(dialog_right_jpn_image, { 0.176, 0.357, 0.467 }, 0.25, 20)) |
| 44 | + ){ |
| 45 | + return true; |
| 46 | + } |
| 47 | + return false; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +BattleMenuDetector::BattleMenuDetector(Color color) |
| 52 | + : m_menu_top_box(0.528308, 0.742885, 0.439385, 0.00830769) //top of the white dialog box |
| 53 | + , m_menu_right_box(0.961538, 0.752231, 0.00615385, 0.200423) |
| 54 | + , m_dialog_top_box(0.036, 0.749115, 0.459077, 0.0135) |
| 55 | + , m_dialog_right_box(0.490154, 0.762615, 0.00615385, 0.178615) //right side, closest to the menu |
| 56 | + , m_menu_top_jpn_box(0.593239, 0.743878, 0.373344, 0.009360) //very different positions! |
| 57 | + , m_menu_right_jpn_box(0.961538, 0.752231, 0.00615385, 0.200423) |
| 58 | + , m_dialog_top_jpn_box(0.043001, 0.750564, 0.520015, 0.002674) |
| 59 | + , m_dialog_right_jpn_box(0.559460, 0.750564, 0.003556, 0.196556) |
| 60 | +{} |
| 61 | +void BattleMenuDetector::make_overlays(VideoOverlaySet& items) const{ |
| 62 | + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; |
| 63 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_menu_top_box)); |
| 64 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_menu_right_box)); |
| 65 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_top_box)); |
| 66 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_right_box)); |
| 67 | + |
| 68 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_menu_top_jpn_box)); |
| 69 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_menu_right_jpn_box)); |
| 70 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_top_jpn_box)); |
| 71 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_right_jpn_box)); |
| 72 | +} |
| 73 | +bool BattleMenuDetector::detect(const ImageViewRGB32& screen){ |
| 74 | + ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); |
| 75 | + |
| 76 | + //Menu is white |
| 77 | + ImageViewRGB32 menu_top_image = extract_box_reference(game_screen, m_menu_top_box); |
| 78 | + ImageViewRGB32 menu_right_image = extract_box_reference(game_screen, m_menu_right_box); |
| 79 | + ImageViewRGB32 menu_top_jpn_image = extract_box_reference(game_screen, m_menu_top_jpn_box); |
| 80 | + ImageViewRGB32 menu_right_jpn_image = extract_box_reference(game_screen, m_menu_right_jpn_box); |
| 81 | + |
| 82 | + //Background dialog is teal |
| 83 | + ImageViewRGB32 dialog_top_image = extract_box_reference(game_screen, m_dialog_top_box); |
| 84 | + ImageViewRGB32 dialog_right_image = extract_box_reference(game_screen, m_dialog_right_box); |
| 85 | + ImageViewRGB32 dialog_top_jpn_image = extract_box_reference(game_screen, m_dialog_top_jpn_box); |
| 86 | + ImageViewRGB32 dialog_right_jpn_image = extract_box_reference(game_screen, m_dialog_right_jpn_box); |
| 87 | + |
| 88 | + if ((is_white(menu_top_image) //All languages except japanese |
| 89 | + && is_white(menu_right_image) |
| 90 | + && is_solid(dialog_top_image, { 0.176, 0.357, 0.467 }, 0.25, 20) //40, 81, 106 teal |
| 91 | + && is_solid(dialog_right_image, { 0.176, 0.357, 0.467 }, 0.25, 20)) |
| 92 | + || |
| 93 | + (is_white(menu_top_jpn_image) //japanese |
| 94 | + && is_white(menu_right_jpn_image) |
| 95 | + && is_solid(dialog_top_jpn_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 96 | + && is_solid(dialog_right_jpn_image, { 0.176, 0.357, 0.467 }, 0.25, 20)) |
| 97 | + ){ |
| 98 | + return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +AdvanceBattleDialogDetector::AdvanceBattleDialogDetector(Color color) |
| 105 | + : m_dialog_box(0.036, 0.748077, 0.926769, 0.204577) |
| 106 | + , m_dialog_top_box(0.0372308, 0.750154, 0.925538, 0.00934615) |
| 107 | + , m_dialog_right_box(0.956615, 0.7595, 0.00615385, 0.185885) |
| 108 | + , m_dialog_jpn_box(0.043890, 0.749227, 0.911137, 0.203242) //this position is very different! |
| 109 | + , m_dialog_top_jpn_box(0.043890, 0.749227, 0.910248, 0.008023) |
| 110 | + , m_dialog_right_jpn_box(0.950583, 0.751901, 0.003556, 0.199230) |
| 111 | +{} |
| 112 | +void AdvanceBattleDialogDetector::make_overlays(VideoOverlaySet& items) const{ |
| 113 | + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; |
| 114 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_box)); |
| 115 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_top_box)); |
| 116 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_right_box)); |
| 117 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_jpn_box)); |
| 118 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_top_jpn_box)); |
| 119 | + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_dialog_right_jpn_box)); |
| 120 | +} |
| 121 | +bool AdvanceBattleDialogDetector::detect(const ImageViewRGB32& screen){ |
| 122 | + ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); |
| 123 | + |
| 124 | + const bool replace_color_within_range = false; |
| 125 | + |
| 126 | + //Filter out background |
| 127 | + ImageRGB32 filtered_region = filter_rgb32_range( |
| 128 | + extract_box_reference(game_screen, m_dialog_box), |
| 129 | + combine_rgb(164, 0, 0), combine_rgb(255, 114, 87), Color(0), replace_color_within_range |
| 130 | + ); |
| 131 | + ImageStats stats = image_stats(filtered_region); |
| 132 | + |
| 133 | + /* |
| 134 | + filtered_region.save("./filtered_only.png"); |
| 135 | + cout << stats.average.r << endl; |
| 136 | + cout << stats.average.g << endl; |
| 137 | + cout << stats.average.b << endl; |
| 138 | + */ |
| 139 | + |
| 140 | + //japanese |
| 141 | + ImageRGB32 filtered_region_jpn = filter_rgb32_range( |
| 142 | + extract_box_reference(game_screen, m_dialog_jpn_box), |
| 143 | + combine_rgb(164, 0, 0), combine_rgb(255, 114, 87), Color(0), replace_color_within_range |
| 144 | + ); |
| 145 | + ImageStats stats2 = image_stats(filtered_region_jpn); |
| 146 | + |
| 147 | + ImageViewRGB32 dialog_top_image = extract_box_reference(game_screen, m_dialog_top_box); |
| 148 | + ImageViewRGB32 dialog_right_image = extract_box_reference(game_screen, m_dialog_right_box); |
| 149 | + ImageViewRGB32 dialog_top_jpn_image = extract_box_reference(game_screen, m_dialog_top_jpn_box); |
| 150 | + ImageViewRGB32 dialog_right_jpn_image = extract_box_reference(game_screen, m_dialog_right_jpn_box); |
| 151 | + |
| 152 | + if ((is_solid(dialog_top_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 153 | + && is_solid(dialog_right_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 154 | + && (stats.average.r > stats.average.b + 180) |
| 155 | + && (stats.average.r > stats.average.g + 180)) |
| 156 | + || |
| 157 | + (is_solid(dialog_top_jpn_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 158 | + && is_solid(dialog_right_jpn_image, { 0.176, 0.357, 0.467 }, 0.25, 20) |
| 159 | + && (stats2.average.r > stats2.average.b + 180) |
| 160 | + && (stats2.average.r > stats2.average.g + 180)) |
| 161 | + ){ |
| 162 | + return true; |
| 163 | + } |
| 164 | + return false; |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +} |
| 172 | +} |
| 173 | +} |
0 commit comments