Skip to content
Merged
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
16 changes: 15 additions & 1 deletion SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include <utility>
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "CommonFramework/GlobalAutoPaths.h"
Expand All @@ -15,7 +16,10 @@ namespace PokemonAutomation{



SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path)
SpriteDatabase::SpriteDatabase(
const char* sprite_path, const char* json_path,
const std::function<bool(const std::string& slug)>& mirror_horizontally
)
: m_backing_image(RESOURCE_PATH() + sprite_path)
{
std::string path = RESOURCE_PATH() + json_path;
Expand All @@ -37,6 +41,16 @@ SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path)
JsonObject& obj = item.second.to_object_throw(path);
int y = (int)obj.get_integer_throw("top", path);
int x = (int)obj.get_integer_throw("left", path);
if (mirror_horizontally && mirror_horizontally(slug)){
if (x < 0 || y < 0 || (size_t)(x + width) > m_backing_image.width() || (size_t)(y + height) > m_backing_image.height()){
throw FileException(nullptr, PA_CURRENT_FUNCTION, "Sprite out of bounds: " + slug, path);
}
for (int r = y; r < y + (int)height; r++){
for (int c0 = x, c1 = x + (int)width - 1; c0 < c1; c0++, c1--){
std::swap(m_backing_image.pixel(c0, r), m_backing_image.pixel(c1, r));
}
}
}

ImageViewRGB32 sprite = extract_box_reference(m_backing_image, ImagePixelBox(x, y, x + width, y + height));
m_database.emplace(
Expand Down
10 changes: 9 additions & 1 deletion SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H
#define PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H

#include <functional>
#include <map>
#include "CommonFramework/ImageTypes/ImageRGB32.h"

Expand All @@ -32,7 +33,14 @@ class SpriteDatabase{
// (next pokemon) ...
// }
// }
SpriteDatabase(const char* sprite_path, const char* json_path);
//
// If "mirror_horizontally" is set, each sprite for which it returns true
// is flipped left-to-right in place. This avoids storing a separate
// flipped copy of the image.
SpriteDatabase(
const char* sprite_path, const char* json_path,
const std::function<bool(const std::string& slug)>& mirror_horizontally = nullptr
);

public:
struct Sprite{
Expand Down
14 changes: 13 additions & 1 deletion SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ std::string base_species_slug(const std::string& species){
return species;
}

bool species_matches(const std::string& observed, const std::string& candidate){
const std::string base = base_species_slug(observed);
if (base != base_species_slug(candidate)){
return false;
}
// Accept matches with the base slug in case form couldn't be read
if (observed == base || candidate == base){
return true;
}
return observed == candidate;
}

int slot_to_unownform(AdvEncounterSlot slot){
if (slot.species.find("unown") == std::string::npos){
return -1;
Expand Down Expand Up @@ -521,7 +533,7 @@ bool check_for_match(AdvPokemonResult res, AdvRngFilters target, int16_t gender_
}

bool check_for_match(AdvWildPokemonResult res, AdvRngFilters target, int16_t gender_threshold, uint16_t tid_xor_sid){
return (base_species_slug(target.species) == base_species_slug(res.species))
return species_matches(target.species, res.species)
&& (target.level == res.level)
&& (target.nature == AdvNature::Any || (res.nature == target.nature))
&& (target.ability == AdvAbility::Any || (res.ability == target.ability))
Expand Down
9 changes: 6 additions & 3 deletions SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,14 @@ Pokemon::NatureAdjustments nature_to_adjustment(AdvNature nature);
Pokemon::AdvNature string_to_nature(const std::string& nature_string);
std::string nature_to_string(const AdvNature& nature);

// Forms that cannot be told apart from an observation (e.g. Unown letters) share a base slug.
// Encounter slots carry the form-specific slug, while an observed species only ever carries
// the base slug, so both sides must be folded before they are compared.
// Forms of the same species (e.g. Unown letters) share a base slug.
// Encounter slots carry the form-specific slug (e.g. "unown-b").
std::string base_species_slug(const std::string& species);

// Return true if an observed species is consistent with a candidate species.
// Species with forms match by base slug, unless both sides name a specific form
bool species_matches(const std::string& observed, const std::string& candidate);

std::string gender_to_string(const AdvGender& gender);

AdvGender gender_from_gender_value(uint8_t gender_value, int16_t threshold);
Expand Down
Loading
Loading