Files
isaac-port/src/ui/menu/special_seeds.cpp
T

431 lines
18 KiB
C++

#include "ui/menu/special_seeds.h"
#include "engine/data.h"
#include "engine/fx_layers.h"
#include "engine/log.h"
#include "game/manager.h"
#include "ui/menu/manager.h"
#include <algorithm>
#include <cstdlib>
constexpr const char* SEEDS_MENU_ANM2 = "gfx/ui/main menu/SeedsMenu.anm2";
constexpr const char* SEED_MENU_XML = "seedmenu.xml";
constexpr const char* SEED_MENU_CATEGORY = "SeedMenu";
constexpr std::string_view NUM_SEEDS_TAG = "{numseeds}";
// seedmenu.xml: the entry dialog's texts and the list's lines.
constexpr std::string_view DIALOG_TAG = "entryDialog";
constexpr std::string_view LIST_TAG = "seedmenu";
constexpr std::string_view RESET_TAG = "reset";
constexpr std::string_view CONFIRM_TAG = "confirm";
constexpr std::string_view SEED_TAG = "seed";
constexpr std::string_view SPACER_TAG = "spacer";
constexpr std::string_view IMPLEMENTED_ATTR = "implemented";
constexpr std::string_view IMPLEMENTED_TRUE[] = {"true", "True"};
// The paper's parts and their nulls. Its middle is torn pieces, picked by a fixed seed so they stay put.
constexpr const char* PAPER_TOP = "PaperTop";
constexpr const char* PAPER_MIDDLE = "PaperMiddle";
constexpr const char* PAPER_BOTTOM = "PaperBottom";
constexpr const char* NEXT_SECTION_NULL = "NextSection";
constexpr const char* LIST_END_NULL = "ListEnd";
constexpr int PAPER_LAST_PIECE = 4;
constexpr uint32_t PAPER_SEED = 0xb00b135;
constexpr uint32_t CHECKMARK_SEED = 0x512578;
constexpr uint32_t CHECKMARK_FRAMES = 3;
// Where a line's parts sit: its text from the line's place, the checkmark, cursor and no achievement icon from the
// text, and how far down each kind of line pushes the next.
constexpr const char* ANIMATION_CHECKMARK = "Checkmark";
constexpr const char* ANIMATION_CURSOR = "Cursor";
constexpr const char* ANIMATION_NO_ACHIEVEMENT = "NoAchievement";
constexpr float TEXT_OFFSET_X = 150;
constexpr float TEXT_OFFSET_Y = 66;
constexpr float CHECKMARK_OFFSET_X = -12;
constexpr float CHECKMARK_OFFSET_Y = 4;
constexpr float CURSOR_OFFSET_X = -29;
constexpr float CURSOR_OFFSET_Y = 7;
constexpr float CURSOR_UNCHECKED_X = 16;
constexpr float NO_ACHIEVEMENT_OFFSET_X = 13;
constexpr float NO_ACHIEVEMENT_OFFSET_Y = 5;
constexpr float BUTTON_HEIGHT = 19;
constexpr float SEED_HEIGHT = 16;
constexpr float SPACER_HEIGHT = 6;
// The text colors: buttons and effects that stay on, effects the selected line would turn off, effects off, and
// effects off that cannot join the ones on.
constexpr uint32_t TEXT_DROPPED_ARGB = 0xff962c2c;
constexpr uint32_t TEXT_OFF_ARGB = 0xff8f8988;
constexpr uint32_t TEXT_BLOCKED_ARGB = 0xffbdb5b3;
// The view follows the cursor below this height. Held directions repeat after a delay, and dyslexia scrambles the
// names for a while after it is turned on.
constexpr float VIEW_FOLLOW_Y = 135;
constexpr int KEY_REPEAT_DELAY = 29;
constexpr int KEY_REPEAT_INTERVAL = 5;
constexpr int DYSLEXIA_TIME = 30;
// What string_scramble_seeded does at each character, by a draw out of 15: swap with the next or the one before,
// drop it, or put a random letter or a space before it. Any other draw keeps it.
constexpr uint32_t FNV_OFFSET = 0x811c9dc5;
constexpr uint32_t FNV_PRIME = 0x1000193;
constexpr uint32_t SCRAMBLE_CHOICES = 15;
constexpr uint32_t SCRAMBLE_SWAP_NEXT = 0;
constexpr uint32_t SCRAMBLE_SWAP_PREVIOUS = 1;
constexpr uint32_t SCRAMBLE_DROP_END = 4;
constexpr uint32_t SCRAMBLE_LETTER = 4;
constexpr uint32_t SCRAMBLE_SPACE = 5;
constexpr uint32_t ALPHABET_SIZE = 26;
// 00682b90 Scrambles a text the same way every time, by a hash of it: letters swapped, dropped and added.
std::string string_scramble_seeded(std::string text)
{
uint32_t hash = FNV_OFFSET;
RNG rng{};
for (char character : text) hash = ((uint8_t)character ^ hash) * FNV_PRIME;
rng.SetSeed(hash, DEFAULT_SHIFT_INDEX);
for (int i = 0; i < (int)text.size(); i++)
{
uint32_t choice = rng.Next() % SCRAMBLE_CHOICES;
if (choice == SCRAMBLE_SWAP_NEXT && i + 1 < (int)text.size()) std::swap(text[i], text[i + 1]);
else if (choice == SCRAMBLE_SWAP_PREVIOUS && i > 0) std::swap(text[i], text[i - 1]);
else if (choice > SCRAMBLE_SWAP_PREVIOUS && choice < SCRAMBLE_DROP_END) text.erase(i--, 1);
else if (choice == SCRAMBLE_LETTER) text.insert(i++, 1, (char)('a' + rng.Next() % ALPHABET_SIZE));
else if (choice == SCRAMBLE_SPACE) text.insert(i++, 1, ' ');
}
return text;
}
// 008f0040 The line's name in the current language when it is a #key of the SeedMenu strings, else as it is.
std::string SpecialSeed::get_display_name() const
{
StringTable& strings = g_manager->stringTable;
bool isMissing = false;
if (name.empty() || name[0] != STRING_KEY_PREFIX) return name;
std::string_view text = strings.get_string(SEED_MENU_CATEGORY, strings.language, name.substr(1), &isMissing);
return isMissing ? name : std::string(text);
}
// Loads the menu at its place on the menu sheet, then resets it (inlined in MenuManager::Init).
void Menu_SpecialSeeds::Init(Vector menuPosition)
{
specialSeedsSprite.Load(SEEDS_MENU_ANM2, GRAPHICS_LOAD);
position = menuPosition;
Reset();
}
// Loads the menu again (inlined in MenuManager::LoadGraphics).
void Menu_SpecialSeeds::LoadGraphics()
{
specialSeedsSprite.Load(SEEDS_MENU_ANM2, GRAPHICS_LOAD);
}
// The first child of a node with a tag, or the node itself when there is none.
static DataNode node_child_find(DataNode node, std::string_view tag)
{
for (DataNode child : node_children(node))
if (node_tag(child) == tag) return child;
return node;
}
// Reads seedmenu.xml: the entry dialog's texts and the list, keeping the effects the slot has found and that the game
// implements. The slot forgets the ones it does not implement. Spacers never stack or end the list.
// The original reads before the list for a spacer heading it. Such a spacer is dropped.
void Menu_SpecialSeeds::entries_load()
{
PersistentGameData& data = g_manager->persistentGameData;
Data file;
data_load(file, SEED_MENU_XML);
if (!file.isLoaded)
{
log_printf(LOG_INFO, "ERROR Could not read seed menu layout\n");
return;
}
entries.clear();
DataNode dialog = node_child_find(file.root, DIALOG_TAG);
unlockText = node_attr(dialog, "unlock");
enableText = node_attr(dialog, "enable");
for (DataNode node : node_children(node_child_find(file.root, LIST_TAG)))
{
std::string_view tag = node_tag(node);
std::string name(node_attr(node, "name"));
if (tag == RESET_TAG) entries.push_back({name, {}, SPECIAL_SEED_RESET});
else if (tag == CONFIRM_TAG) entries.push_back({name, {}, SPECIAL_SEED_CONFIRM});
else if (tag == SEED_TAG)
{
std::string implemented(node_attr(node, IMPLEMENTED_ATTR));
int id = std::atoi(std::string(node_attr(node, "id")).c_str());
bool isKnown = id >= 0 && id < SPECIAL_SEED_COUNT;
bool isImplemented = !node_attr_find(node, IMPLEMENTED_ATTR) ||
std::ranges::count(IMPLEMENTED_TRUE, implemented) || std::atoi(implemented.c_str());
if (isImplemented && isKnown && data.specialSeeds[id])
entries.push_back({name, {}, SPECIAL_SEED_EFFECT, id});
else if (!isImplemented && isKnown && !data.isReadOnly)
{
data.specialSeeds[id] = false;
data.isChangesMade = true;
}
}
else if (tag == SPACER_TAG && !entries.empty() && entries.back().type != SPECIAL_SEED_SPACER)
entries.push_back({});
}
if (!entries.empty() && entries.back().type == SPECIAL_SEED_SPACER) entries.pop_back();
Vector layout = vector_make(0, 0);
for (SpecialSeed& entry : entries)
{
entry.position = layout;
layout.y += entry.type == SPECIAL_SEED_SPACER ? SPACER_HEIGHT
: entry.type == SPECIAL_SEED_EFFECT ? SEED_HEIGHT
: BUTTON_HEIGHT;
}
}
// 008f19f0 Puts the cursor back on the first line and reads the list again.
// The original scrolls the challenges menu back to the top here, not this one. Kept.
void Menu_SpecialSeeds::Reset()
{
MenuManager& menus = *g_menuManager;
selectedElement = 0;
holdInputTimer = 0;
menus.viewPositionSet[MENU_CHALLENGE].y = CHALLENGE_VIEW_Y;
menus.isViewMoving = true;
entries_load();
}
// Moves the cursor a step to the next line that is not a spacer, wrapping around.
void Menu_SpecialSeeds::selection_move(int step)
{
int count = (int)entries.size();
for (selectedElement += step; selectedElement >= 0 && selectedElement < count; selectedElement += step)
if (entries[selectedElement].type != SPECIAL_SEED_SPACER) return;
selectedElement = step < 0 ? count - 1 : 0;
}
// Clears every effect on reset, leaves on confirm, and turns the selected effect off, or on without the ones it is
// banned with.
void Menu_SpecialSeeds::entry_confirm()
{
Seeds& seeds = g_menuManager->seedsObject;
SpecialSeed entry = entries[selectedElement];
if (entry.type == SPECIAL_SEED_RESET) seeds.ClearSeedEffects();
else if (entry.type == SPECIAL_SEED_CONFIRM) g_menuManager->pop_menu();
else if (entry.type == SPECIAL_SEED_EFFECT && seeds.HasSeedEffect(entry.id)) seeds.RemoveSeedEffect(entry.id);
else if (entry.type == SPECIAL_SEED_EFFECT)
{
seeds.RemoveBlockingSeedEffects(entry.id);
seeds.AddSeedEffect(entry.id);
}
}
// Makes the slot forget the selected effect and turns it off, then reads the list again with the cursor on the next
// line that is not a spacer, or the last one.
void Menu_SpecialSeeds::seed_forget()
{
PersistentGameData& data = g_manager->persistentGameData;
SpecialSeed entry = entries[selectedElement];
int selected = selectedElement;
if (entry.type != SPECIAL_SEED_EFFECT) return;
if (!data.isReadOnly)
{
data.specialSeeds[entry.id] = false;
data.isChangesMade = true;
}
g_menuManager->seedsObject.RemoveSeedEffect(entry.id);
Reset();
while (selected < (int)entries.size() && entries[selected].type == SPECIAL_SEED_SPACER) selected++;
selectedElement = std::min(selected, (int)entries.size() - 1);
}
// 008f01b0 Steps the menu: confirm picks the selected line, LB, RB and the tab together forget its effect, up and down
// move the cursor (repeating while held) and back leaves. The cursor and the view then follow the selected line.
// The original reads an empty list. Without seedmenu.xml the menu only animates and leaves.
void Menu_SpecialSeeds::Update()
{
MenuManager& menus = *g_menuManager;
Seeds& seeds = menus.seedsObject;
bool isForget = is_action(ACTION_MENU_LB, ACTION_PRESSED) && is_action(ACTION_MENU_RB, ACTION_PRESSED) &&
is_action(ACTION_MENU_TAB, ACTION_TRIGGERED);
bool isRepeat =
holdInputTimer == 0 || (holdInputTimer > KEY_REPEAT_DELAY && holdInputTimer % KEY_REPEAT_INTERVAL == 0);
if ((g_manager->framecount & 1) == 0) specialSeedsSprite.Update();
if (entries.empty())
{
if (is_action(ACTION_MENU_BACK, ACTION_TRIGGERED)) menus.pop_menu();
return;
}
if (is_action(ACTION_MENU_CONFIRM, ACTION_TRIGGERED)) entry_confirm();
else if (isForget) seed_forget();
else if (is_action(ACTION_MENU_UP, ACTION_PRESSED))
{
if (isRepeat) selection_move(-1);
}
else if (is_action(ACTION_MENU_DOWN, ACTION_PRESSED))
{
if (isRepeat) selection_move(1);
}
else if (is_action(ACTION_MENU_BACK, ACTION_TRIGGERED)) menus.pop_menu();
dyslexiaTimer = seeds.HasSeedEffect(SEED_DYSLEXIA) ? std::min(dyslexiaTimer + 1, DYSLEXIA_TIME) : 0;
bool isHeld = is_action(ACTION_MENU_DOWN, ACTION_PRESSED) || is_action(ACTION_MENU_UP, ACTION_PRESSED);
holdInputTimer = isHeld ? holdInputTimer + 1 : 0;
if (entries.empty()) return;
Vector line = entries[selectedElement].position;
cursorPosition = vector_make(TEXT_OFFSET_X + line.x, TEXT_OFFSET_Y + line.y + SEED_HEIGHT);
Vector& view = menus.viewPositionSet[MENU_SPECIAL_SEEDS];
view = menus.specialSeedsViewPosition;
menus.isViewMoving = true;
if (cursorPosition.y > VIEW_FOLLOW_Y) view.y -= cursorPosition.y - VIEW_FOLLOW_Y;
}
// The keyframe position of a null at a frame of an animation, or the origin when there is none.
static Vector null_position_get(const ANM2& sprite, std::string_view animation, std::string_view null, int frame)
{
const NullFrame* nullFrame = sprite.GetNullFrame(animation, null, frame);
return nullFrame ? nullFrame->pos : vector_make(0, 0);
}
// Draws the paper: its top, torn middle pieces down to past the last line (the last piece where the next would reach
// further), and its bottom.
void Menu_SpecialSeeds::paper_render(Vector origin)
{
ANM2& sprite = specialSeedsSprite;
Vector top = null_position_get(sprite, PAPER_TOP, NEXT_SECTION_NULL, 0);
const AnimationData* middle = sprite.GetAnimationData(PAPER_MIDDLE);
uint32_t pieceCount = middle ? (uint32_t)middle->length : 0;
float paperEnd = entries.empty() ? SEED_HEIGHT : entries.back().position.y + SEED_HEIGHT;
float reach = top.y + null_position_get(sprite, PAPER_BOTTOM, LIST_END_NULL, 0).y - TEXT_OFFSET_Y;
Vector at = vector_add(origin, top);
RNG pieceRng{};
sprite.SetFrame(PAPER_TOP, 0);
sprite.Render({.position = origin});
pieceRng.SetSeed(PAPER_SEED, DEFAULT_SHIFT_INDEX);
while (reach < paperEnd)
{
int piece = (int)pieceRng.RandomInt(pieceCount);
Vector next = null_position_get(sprite, PAPER_MIDDLE, NEXT_SECTION_NULL, piece);
if (paperEnd < next.y + reach)
{
piece = PAPER_LAST_PIECE;
next = null_position_get(sprite, PAPER_MIDDLE, NEXT_SECTION_NULL, piece);
}
sprite.SetFrame(PAPER_MIDDLE, piece);
sprite.Render({.position = at});
reach += next.y;
at = vector_add(at, next);
}
sprite.SetFrame(PAPER_BOTTOM, 0);
sprite.Render({.position = at});
}
// The color of a line's text: dark for buttons and effects on, red for effects on that the selected line would turn
// off, grey for effects off and pale for effects off that cannot join the ones on.
static KColor entry_color_get(const SpecialSeed& entry, const SpecialSeed& selected)
{
Seeds& seeds = g_menuManager->seedsObject;
if (entry.type != SPECIAL_SEED_EFFECT) return kcolor_argb(TEXT_ARGB);
if (!seeds.HasSeedEffect(entry.id))
return kcolor_argb(seeds.CanAddSeedEffect(entry.id) ? TEXT_OFF_ARGB : TEXT_BLOCKED_ARGB);
bool isDropped = selected.type == SPECIAL_SEED_RESET ||
(selected.type == SPECIAL_SEED_EFFECT && Seeds::IsSeedComboBanned(entry.id, selected.id));
return kcolor_argb(isDropped ? TEXT_DROPPED_ARGB : TEXT_ARGB);
}
// Whether an effect alone keeps a run from unlocking achievements.
static bool is_achievement_blocking(int seedEffect)
{
Seeds probe;
probe.AddSeedEffect(seedEffect);
return probe.AchievementUnlocksDisallowed();
}
// Draws a line that is not a spacer: the checkmark of an effect on, its name in its color ({numseeds} counting the
// effects on, scrambled for a while under dyslexia in English), and the no achievement icon after the confirm button
// or an effect that blocks achievements (lit when on). Says whether it drew the checkmark.
bool Menu_SpecialSeeds::entry_render(const SpecialSeed& entry, Vector textPosition, RNG& checkmarkRng)
{
Manager& manager = *g_manager;
Seeds& seeds = g_menuManager->seedsObject;
ANM2& sprite = specialSeedsSprite;
KColor color = entry_color_get(entry, entries[selectedElement]);
bool isOn = entry.type == SPECIAL_SEED_EFFECT && seeds.HasSeedEffect(entry.id);
bool isButton = entry.type == SPECIAL_SEED_RESET || entry.type == SPECIAL_SEED_CONFIRM;
int language = manager.stringTable.language;
Vector checkmark = vector_make(textPosition.x + CHECKMARK_OFFSET_X, textPosition.y + CHECKMARK_OFFSET_Y);
sprite.SetFrame(ANIMATION_CHECKMARK, (int)checkmarkRng.RandomInt(CHECKMARK_FRAMES));
if (isOn) sprite.Render({.position = checkmark});
std::string name = entry.get_display_name();
std::string count = std::to_string(seeds.CountSeedEffects());
for (size_t at = name.find(NUM_SEEDS_TAG); at != std::string::npos; at = name.find(NUM_SEEDS_TAG, at))
{
name.replace(at, NUM_SEEDS_TAG.size(), count);
at += count.size();
}
bool isScrambled = seeds.HasSeedEffect(SEED_DYSLEXIA) && dyslexiaTimer > 0 && dyslexiaTimer < DYSLEXIA_TIME;
if (isScrambled && language == LANGUAGE_ENGLISH) name = string_scramble_seeded(name);
bool isLargeFont = isButton || language == LANGUAGE_JAPANESE || language == LANGUAGE_KOREAN;
Font& font = isLargeFont ? manager.font2_TeamMeatEx12 : manager.font1_TeamMeatEx10;
FontSettings settings;
font.DrawString(name, textPosition, vector_make(1, 1), color, settings);
bool isConfirmBlocked = entry.type == SPECIAL_SEED_CONFIRM && seeds.AchievementUnlocksDisallowed();
bool isEffectBlocking = entry.type == SPECIAL_SEED_EFFECT && is_achievement_blocking(entry.id);
if (isConfirmBlocked || isEffectBlocking)
{
float width = (float)font.GetStringWidth(name);
sprite.SetFrame(ANIMATION_NO_ACHIEVEMENT, isEffectBlocking && !isOn);
sprite.Render({.position = vector_make(textPosition.x + NO_ACHIEVEMENT_OFFSET_X + width,
textPosition.y + NO_ACHIEVEMENT_OFFSET_Y)});
}
return isOn;
}
// 008f06e0 Draws the paper, then each line and the cursor by the selected one, further right when it has no
// checkmark. Checkmarks are drawn from a fixed seed so they keep their look.
void Menu_SpecialSeeds::Render()
{
MenuManager& menus = *g_menuManager;
Vector origin = vector_add(menus.viewPosition, position);
RNG checkmarkRng{};
paper_render(origin);
checkmarkRng.SetSeed(CHECKMARK_SEED, DEFAULT_SHIFT_INDEX);
for (int i = 0; i < (int)entries.size(); i++)
{
const SpecialSeed& entry = entries[i];
Vector text =
vector_make(origin.x + entry.position.x + TEXT_OFFSET_X, origin.y + entry.position.y + TEXT_OFFSET_Y);
bool isChecked = entry.type != SPECIAL_SEED_SPACER && entry_render(entry, text, checkmarkRng);
if (i != selectedElement) continue;
float x = text.x + CURSOR_OFFSET_X + (isChecked ? 0 : CURSOR_UNCHECKED_X);
specialSeedsSprite.SetFrame(ANIMATION_CURSOR, 0);
specialSeedsSprite.Render({.position = vector_make(x, text.y + CURSOR_OFFSET_Y)});
}
}