Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include <algorithm>
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Inference/PokemonSV_WhiteButtonDetector.h"
#include "Inference/Boxes/PokemonSV_BoxDetection.h"
Expand All @@ -25,15 +26,236 @@
#include "Inference/Overworld/PokemonSV_LetsGoKillDetector.h"
#include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h"
#include "Inference/Picnics/PokemonSV_SandwichHandDetector.h"
#include "Programs/Farming/PokemonSV_BlueberryQuests.h"
#include "Programs/Farming/PokemonSV_BBQSoloFarmer.h"
#include "PokemonSV_Tests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{

namespace{

class NullLogger : public Logger{
public:
virtual void log(const std::string&, Color) override{}
};

// A singleplayer quest panel that behaves like the game's: 3 blue quests,
// and a completed quest is replaced by a new one. process_quest_list()
// follows the two rules of the real one that the round loop depends on:
// quests in not_possible_quests are skipped, and if nothing is doable all
// quests are rerolled and nothing is returned. Red quests and the
// exclusions table are not modeled. Records the game actions taken.
class SimulatedBBQGameActions : public BBQGameActions{
public:
std::vector<BBQuests> panel;
// Each attempt returns the next outcome; once exhausted, attempts succeed.
std::vector<bool> outcomes = {};
// Completing the first quest also completes the second, e.g. walking
// 500 meters while navigating to a photo target.
std::vector<std::pair<BBQuests, BBQuests>> also_completes = {};

std::vector<std::string> events;

virtual std::vector<BBQuests> read_quests() override{
events.push_back("read");
if (++m_reads > 1000){
throw std::runtime_error("Too many quest panel reads.");
}
return panel;
}
virtual std::vector<BBQuests> process_quest_list(
const std::vector<BBQuests>& quests, uint8_t
) override{
events.push_back("process");
std::vector<BBQuests> doable;
for (BBQuests quest : quests){
if (!not_possible_quests.contains(quest)){
doable.push_back(quest);
}
}
if (doable.empty()){
for (BBQuests& slot : panel){
slot = next_new_quest();
}
}
return doable;
}
virtual bool do_quest(BBQuests quest, uint8_t&) override{
events.push_back("do");
const bool success = m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true;
if (success){
complete(quest);
for (const auto& [done, extra] : also_completes){
if (done == quest){
complete(extra);
}
}
}
return success;
}
virtual void quest_completed() override{
events.push_back("completed");
}
virtual void end_round(bool save) override{
events.push_back(save ? "end(save)" : "end");
}

private:
void complete(BBQuests quest){
auto iter = std::find(panel.begin(), panel.end(), quest);
if (iter != panel.end()){
*iter = next_new_quest();
}
}

// New quests come from a fixed rotation of doable blue quests, skipping
// any that are already on the panel.
BBQuests next_new_quest(){
static const BBQuests ROTATION[] = {
BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar,
BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal,
BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass,
};
while (true){
BBQuests quest = ROTATION[m_next_new_quest++ % std::size(ROTATION)];
if (std::find(panel.begin(), panel.end(), quest) == panel.end()){
return quest;
}
}
}

size_t m_reads = 0;
size_t m_next_outcome = 0;
size_t m_next_new_quest = 0;
};

std::string join_events(const std::vector<std::string>& events){
std::string ret;
for (const std::string& event : events){
if (!ret.empty()) ret += ' ';
ret += event;
}
return ret;
}

// One simulated farmer run: option values, the panel it starts with and how
// quests turn out, and the game actions it must take.
struct BBQRoundsScenario{
std::string name;
uint64_t num_quests;
uint64_t save_num_quests = 0;
bool stop_when_out_of_eggs = false;
std::vector<BBQuests> panel = {BBQuests::make_tm, BBQuests::auto_10, BBQuests::sneak_up};
std::vector<bool> outcomes = {};
std::vector<std::pair<BBQuests, BBQuests>> also_completes = {};
uint64_t expected_completed = 0;
std::string expected;
};

class Test_BBQFarmerRounds : public UnitTest{
public:
Test_BBQFarmerRounds(BBQRoundsScenario scenario)
: UnitTest("PokemonSV::BBQFarmerRounds - " + scenario.name)
, m_scenario(std::move(scenario))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
BBQOption options;
options.NUM_QUESTS.set(m_scenario.num_quests);
options.SAVE_NUM_QUESTS.set(m_scenario.save_num_quests);
if (m_scenario.stop_when_out_of_eggs){
options.NUM_EGGS.set(0);
options.OUT_OF_EGGS.set(BBQOption::OOEggs::Stop);
}

SimulatedBBQGameActions game;
game.panel = m_scenario.panel;
game.outcomes = m_scenario.outcomes;
game.also_completes = m_scenario.also_completes;

NullLogger logger;
run_bbq_rounds(logger, options, game);
const uint64_t completed = (uint64_t)std::count(game.events.begin(), game.events.end(), "completed");
if (completed != m_scenario.expected_completed){
return "Expected " + std::to_string(m_scenario.expected_completed)
+ " completed quests, counted " + std::to_string(completed);
}
const std::string actual = join_events(game.events);
if (actual != m_scenario.expected){
return "Expected: " + m_scenario.expected + "\nActual: " + actual;
}
return true;
}

private:
const BBQRoundsScenario m_scenario;
};

void add_tests_BBQFarmerRounds(UnitTestDatabase& database){
const std::string round_of_3 = "read process read do completed read do completed read do completed";
const BBQRoundsScenario scenarios[] = {
{
// The goal (4) is reached in the middle of round 2. The loop only
// checks the goal between rounds, so round 2 finishes and 6 quests
// complete.
.name = "finishes the round that reaches the quest goal",
.num_quests = 4,
.expected_completed = 6,
.expected = round_of_3 + " end " + round_of_3 + " end",
},
{
// Doing photo_swim also completes travel_500, so travel_500 is
// gone when the loop reads the panel before attempting it.
.name = "quest already gone counts as completed",
.num_quests = 3,
.panel = {BBQuests::photo_swim, BBQuests::travel_500, BBQuests::make_tm},
.also_completes = {{BBQuests::photo_swim, BBQuests::travel_500}},
.expected_completed = 3,
.expected = "read process read do completed read completed read do completed end",
},
{
// The first attempt fails: 3 attempts, 2 completions.
.name = "failed quest does not count",
.num_quests = 2,
.outcomes = {false},
.expected_completed = 2,
.expected = "read process read do read do completed read do completed end",
},
{
// Nothing on the panel is doable, so process_quest_list() rerolls
// all of it and returns nothing; the loop reads the panel again.
.name = "panel with nothing doable is rerolled",
.num_quests = 3,
.panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect},
.expected_completed = 3,
.expected = "read process " + round_of_3 + " end",
},
{
.name = "saves at exact multiples",
.num_quests = 6,
.save_num_quests = 3,
.expected_completed = 6,
.expected = round_of_3 + " end(save) " + round_of_3 + " end(save)",
},
{
.name = "stops when out of eggs",
.num_quests = 5,
.stop_when_out_of_eggs = true,
.expected_completed = 0,
.expected = "",
},
};
for (const BBQRoundsScenario& scenario : scenarios){
database.add<Test_BBQFarmerRounds>(scenario);
}
}

}

void add_tests(UnitTestDatabase& database){
add_tests_BBQFarmerRounds(database);
add_tests_WhiteButtonDetector(database);
add_tests_BoxInfoDetector(database);
add_tests_BoxEggDetector(database);
Expand Down
Loading
Loading