Skip to content

Commit e305a86

Browse files
authored
Stall buyer example (#788)
1 parent a3bda14 commit e305a86

File tree

4 files changed

+76
-60
lines changed

4 files changed

+76
-60
lines changed

SerialPrograms/Source/PokemonLZA/PokemonLZA_Panels.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// General
1414
#include "Programs/PokemonLZA_ClothingBuyer.h"
15-
#include "Programs/PokemonLZA_BerryBuyer.h"
15+
#include "Programs/PokemonLZA_StallBuyer.h"
1616

1717
// Trading
1818
#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h"
@@ -52,7 +52,7 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
5252

5353
ret.emplace_back("---- General ----");
5454
ret.emplace_back(make_single_switch_program<ClothingBuyer_Descriptor, ClothingBuyer>());
55-
ret.emplace_back(make_single_switch_program<BerryBuyer_Descriptor, BerryBuyer>());
55+
ret.emplace_back(make_single_switch_program<StallBuyer_Descriptor, StallBuyer>());
5656
if (IS_BETA_VERSION){
5757
ret.emplace_back(make_multi_switch_program<SelfBoxTrade_Descriptor, SelfBoxTrade>());
5858
}

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BerryBuyer.cpp renamed to SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_StallBuyer.cpp

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Berry Buyer
1+
/* Stall Buyer
22
*
33
* From: https://github.com/PokemonAutomation/
44
*
@@ -14,27 +14,27 @@
1414
#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h"
1515
#include "PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h"
1616
#include "PokemonLZA/Inference/PokemonLZA_DialogDetector.h"
17-
#include "PokemonLZA_BerryBuyer.h"
17+
#include "PokemonLZA_StallBuyer.h"
1818

1919
namespace PokemonAutomation{
2020
namespace NintendoSwitch{
2121
namespace PokemonLZA{
2222

2323
using namespace Pokemon;
2424

25-
BerryBuyer_Descriptor::BerryBuyer_Descriptor()
25+
StallBuyer_Descriptor::StallBuyer_Descriptor()
2626
: SingleSwitchProgramDescriptor(
27-
"PokemonLZA:BerryBuyer",
28-
STRING_POKEMON + " LZA", "Berry Buyer",
29-
"Programs/PokemonLZA/BerryBuyer.html",
30-
"Buy EV reducing berries from stall.",
27+
"PokemonLZA:StallBuyer",
28+
STRING_POKEMON + " LZA", "Stall Buyer",
29+
"Programs/PokemonLZA/StallBuyer.html",
30+
"Buy berries or mints from stall.",
3131
ProgramControllerClass::StandardController_NoRestrictions,
3232
FeedbackType::REQUIRED,
3333
AllowCommandsWhenRunning::DISABLE_COMMANDS
3434
)
3535
{}
3636

37-
class BerryBuyer_Descriptor::Stats : public StatsTracker{
37+
class StallBuyer_Descriptor::Stats : public StatsTracker{
3838
public:
3939
Stats()
4040
: purchases(m_stats["Purchases"])
@@ -47,27 +47,33 @@ class BerryBuyer_Descriptor::Stats : public StatsTracker{
4747
std::atomic<uint64_t>& purchases;
4848
std::atomic<uint64_t>& errors;
4949
};
50-
std::unique_ptr<StatsTracker> BerryBuyer_Descriptor::make_stats() const{
50+
std::unique_ptr<StatsTracker> StallBuyer_Descriptor::make_stats() const{
5151
return std::unique_ptr<StatsTracker>(new Stats());
5252
}
5353

5454

55-
BerryBuyer::BerryBuyer()
56-
: BERRY_TYPE(
57-
"<b>Berry to Purchase:</b>",
55+
StallBuyer::StallBuyer()
56+
: ITEM_POSITION(
57+
"<b>Item Position to Purchase:</b>",
5858
{
59-
{BerryType::POMEG, "pomeg", "Pomeg (HP)"},
60-
{BerryType::KELPSY, "kelpsy", "Kelpsy (Attack)"},
61-
{BerryType::QUALOT, "qualot", "Qualot (Defence)"},
62-
{BerryType::HONDEW, "hondew", "Hondew (Special Attack)"},
63-
{BerryType::GREPA, "grepa", "Grepa (Special Defence)"},
64-
{BerryType::TAMATO, "tamato", "Tamato (Speed)"},
59+
{ItemPosition::FirstItem, "FirstItem", "First Item" },
60+
{ItemPosition::SecondItem, "SecondItem", "Second Item" },
61+
{ItemPosition::ThirdItem, "ThirdItem", "Third Item" },
62+
{ItemPosition::FourthItem, "FourthItem", "Fourth Item" },
63+
{ItemPosition::FifthItem, "FifthItem", "Fifth Item" },
64+
{ItemPosition::SixthItem, "SixthItem", "Sixth Item" },
65+
{ItemPosition::SeventhItem, "SeventhItem", "Seventh Item" },
6566
},
6667
LockMode::LOCK_WHILE_RUNNING,
67-
BerryType::POMEG
68+
ItemPosition::FirstItem
69+
)
70+
, NUM_ITEM(
71+
"<b>Number of available items in the stall:</b><br>Number of available items in the stall.",
72+
LockMode::LOCK_WHILE_RUNNING,
73+
6, 2, 7
6874
)
6975
, NUM_PURCHASE(
70-
"<b>Number to Purchase:</b><br>The number of berries you want to purchase.",
76+
"<b>Number to Purchase:</b><br>The number of items you want to purchase.",
7177
LockMode::LOCK_WHILE_RUNNING,
7278
100, 1, 999
7379
)
@@ -79,15 +85,35 @@ BerryBuyer::BerryBuyer()
7985
&NOTIFICATION_ERROR_FATAL,
8086
})
8187
{
82-
PA_ADD_OPTION(BERRY_TYPE);
88+
PA_ADD_OPTION(ITEM_POSITION);
89+
PA_ADD_OPTION(NUM_ITEM);
8390
PA_ADD_OPTION(NUM_PURCHASE);
8491
PA_ADD_OPTION(GO_HOME_WHEN_DONE);
8592
PA_ADD_OPTION(NOTIFICATIONS);
8693
}
8794

88-
void BerryBuyer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
89-
BerryBuyer_Descriptor::Stats& stats = env.current_stats<BerryBuyer_Descriptor::Stats>();
95+
std::pair<DpadPosition, int> compute_needed_inputs(int item_position, int num_item){
96+
int down_presses = item_position;
97+
int up_presses = num_item - item_position + 1;
98+
99+
if (down_presses <= up_presses){
100+
return { DPAD_DOWN, down_presses };
101+
}else{
102+
return { DPAD_UP, up_presses };
103+
}
104+
}
105+
106+
void StallBuyer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
107+
StallBuyer_Descriptor::Stats& stats = env.current_stats<StallBuyer_Descriptor::Stats>();
90108
assert_16_9_720p_min(env.logger(), env.console);
109+
int item_position = static_cast<int>(ITEM_POSITION.get());
110+
if (item_position >= NUM_ITEM){
111+
throw UserSetupError(
112+
env.logger(),
113+
"Item position to purchase must be less than or equal to number of available items in the stall."
114+
);
115+
}
116+
91117
while (true) {
92118
context.wait_for_all_requests();
93119

@@ -100,12 +126,12 @@ void BerryBuyer::program(SingleSwitchProgramEnvironment& env, ProControllerConte
100126
SelectionArrowWatcher select(
101127
COLOR_YELLOW, &env.console.overlay(),
102128
SelectionArrowType::RIGHT,
103-
{0.715, 0.235, 0.045, 0.080}
129+
{0.715, 0.165, 0.045, 0.440}
104130
);
105131
SelectionArrowWatcher confirm(
106132
COLOR_YELLOW, &env.console.overlay(),
107133
SelectionArrowType::RIGHT,
108-
{0.715, 0.600, 0.045, 0.080}
134+
{0.665, 0.600, 0.145, 0.080}
109135
);
110136
FlatWhiteDialogWatcher dialog(COLOR_RED, &env.console.overlay());
111137

@@ -121,29 +147,17 @@ void BerryBuyer::program(SingleSwitchProgramEnvironment& env, ProControllerConte
121147
);
122148
context.wait_for(100ms);
123149

150+
auto [direction, presses] = compute_needed_inputs(item_position, NUM_ITEM);
124151
switch (ret){
125152
case 0:
126153
env.log("Detected A button.");
127154
pbf_press_button(context, BUTTON_A, 160ms, 80ms);
128155
continue;
129156

130157
case 1:
131-
env.log("Detected berry selection screen.");
132-
switch(BERRY_TYPE){
133-
case BerryType::HONDEW:
134-
pbf_press_dpad(context, DPAD_DOWN, 160ms, 80ms);
135-
case BerryType::QUALOT:
136-
pbf_press_dpad(context, DPAD_DOWN, 160ms, 80ms);
137-
case BerryType::KELPSY:
138-
pbf_press_dpad(context, DPAD_DOWN, 160ms, 80ms);
139-
case BerryType::POMEG:
140-
break;
141-
case BerryType::GREPA:
142-
pbf_press_dpad(context, DPAD_UP, 160ms, 80ms);
143-
case BerryType::TAMATO:
144-
pbf_press_dpad(context, DPAD_UP, 160ms, 80ms);
145-
pbf_press_dpad(context, DPAD_UP, 160ms, 80ms);
146-
break;
158+
env.log("Detected item selection screen.");
159+
for (int i = 0; i < presses; i++){
160+
pbf_press_dpad(context, direction, 160ms, 80ms);
147161
}
148162
pbf_press_button(context, BUTTON_A, 160ms, 80ms);
149163
continue;

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BerryBuyer.h renamed to SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_StallBuyer.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/* Berry Buyer
1+
/* Stall Buyer
22
*
33
* From: https://github.com/PokemonAutomation/
44
*
55
*/
66

7-
#ifndef PokemonAutomation_PokemonLZA_BerryBuyer_H
8-
#define PokemonAutomation_PokemonLZA_BerryBuyer_H
7+
#ifndef PokemonAutomation_PokemonLZA_StallBuyer_H
8+
#define PokemonAutomation_PokemonLZA_StallBuyer_H
99

1010
#include "Common/Cpp/Options/SimpleIntegerOption.h"
1111
#include "CommonFramework/Notifications/EventNotificationsTable.h"
@@ -18,32 +18,34 @@ namespace PokemonLZA{
1818

1919

2020

21-
class BerryBuyer_Descriptor : public SingleSwitchProgramDescriptor{
21+
class StallBuyer_Descriptor : public SingleSwitchProgramDescriptor{
2222
public:
23-
BerryBuyer_Descriptor();
23+
StallBuyer_Descriptor();
2424

2525
class Stats;
2626
virtual std::unique_ptr<StatsTracker> make_stats() const override;
2727
};
2828

2929

3030

31-
class BerryBuyer : public SingleSwitchProgramInstance{
31+
class StallBuyer : public SingleSwitchProgramInstance{
3232
public:
33-
BerryBuyer();
33+
StallBuyer();
3434

3535
virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override;
3636

3737
private:
38-
enum class BerryType{
39-
POMEG,
40-
KELPSY,
41-
QUALOT,
42-
HONDEW,
43-
GREPA,
44-
TAMATO,
38+
enum class ItemPosition{
39+
FirstItem,
40+
SecondItem,
41+
ThirdItem,
42+
FourthItem,
43+
FifthItem,
44+
SixthItem,
45+
SeventhItem
4546
};
46-
EnumDropdownOption<BerryType> BERRY_TYPE;
47+
EnumDropdownOption<ItemPosition> ITEM_POSITION;
48+
SimpleIntegerOption<uint8_t> NUM_ITEM;
4749
SimpleIntegerOption<uint16_t> NUM_PURCHASE;
4850
GoHomeWhenDoneOption GO_HOME_WHEN_DONE;
4951
EventNotificationOption NOTIFICATION_STATUS_UPDATE;

SerialPrograms/SourceFiles.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,8 +1575,6 @@ file(GLOB LIBRARY_SOURCES
15751575
Source/PokemonLZA/Programs/Farming/PokemonLZA_RestaurantFarmer.h
15761576
Source/PokemonLZA/Programs/PokemonLZA_BasicNavigation.cpp
15771577
Source/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h
1578-
Source/PokemonLZA/Programs/PokemonLZA_BerryBuyer.cpp
1579-
Source/PokemonLZA/Programs/PokemonLZA_BerryBuyer.h
15801578
Source/PokemonLZA/Programs/PokemonLZA_ClothingBuyer.cpp
15811579
Source/PokemonLZA/Programs/PokemonLZA_ClothingBuyer.h
15821580
Source/PokemonLZA/Programs/PokemonLZA_GameEntry.cpp
@@ -1585,6 +1583,8 @@ file(GLOB LIBRARY_SOURCES
15851583
Source/PokemonLZA/Programs/PokemonLZA_MegaShardFarmer.h
15861584
Source/PokemonLZA/Programs/PokemonLZA_MenuNavigation.cpp
15871585
Source/PokemonLZA/Programs/PokemonLZA_MenuNavigation.h
1586+
Source/PokemonLZA/Programs/PokemonLZA_StallBuyer.cpp
1587+
Source/PokemonLZA/Programs/PokemonLZA_StallBuyer.h
15881588
Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp
15891589
Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h
15901590
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.cpp

0 commit comments

Comments
 (0)