From 2d824e869e12e1eccc27932e40a49528acea24ca Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 16 Jan 2025 12:08:46 -0600 Subject: [PATCH] Encode startup buttons as string in replays, fix #533 --- src/game/boe.consts.hpp | 12 ++++-------- src/game/boe.global.hpp | 5 +++++ src/game/boe.graphics.cpp | 9 +++------ src/game/boe.main.cpp | 37 ++++++++++++++++++++++++++++++++++++- src/game/boe.startup.cpp | 2 +- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/game/boe.consts.hpp b/src/game/boe.consts.hpp index 22ec70b1..a4f3b315 100644 --- a/src/game/boe.consts.hpp +++ b/src/game/boe.consts.hpp @@ -10,6 +10,7 @@ #define BOE_GAME_CONSTS_H #include +#include /* overall mode; some seem to be missing */ enum eGameMode { @@ -84,14 +85,9 @@ enum eTrapType { // Startup buttons enum eStartButton { - // Left Column - STARTBTN_TUTORIAL = 0, - STARTBTN_LOAD = 1, - STARTBTN_PREFS = 2, - // Right Column - STARTBTN_NEW = 3, - STARTBTN_JOIN = 4, - STARTBTN_SCROLL = 5, + STARTBTN_TUTORIAL = 0, STARTBTN_NEW = 3, + STARTBTN_LOAD = 1, STARTBTN_JOIN = 4, + STARTBTN_PREFS = 2, STARTBTN_SCROLL = 5, // Keep last: MAX_eStartButton = 6 }; diff --git a/src/game/boe.global.hpp b/src/game/boe.global.hpp index cd109572..3377f364 100644 --- a/src/game/boe.global.hpp +++ b/src/game/boe.global.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "boe.consts.hpp" #define ASB add_string_to_buf @@ -26,4 +27,8 @@ struct effect_pat_type { unsigned short pattern[9][9]; }; +extern std::map startup_button_indices; +extern std::map startup_button_names; +extern std::map startup_button_names_v1; + #endif diff --git a/src/game/boe.graphics.cpp b/src/game/boe.graphics.cpp index 56b9648f..17c7f121 100644 --- a/src/game/boe.graphics.cpp +++ b/src/game/boe.graphics.cpp @@ -414,12 +414,9 @@ void draw_startup_stats() { void draw_start_button(eStartButton which_position,short which_button) { rectangle from_rect,to_rect; const char *button_labels[MAX_eStartButton]; - button_labels[STARTBTN_TUTORIAL] = "Tutorial"; - button_labels[STARTBTN_LOAD] = "Load Game"; - button_labels[STARTBTN_PREFS] = "Preferences"; - button_labels[STARTBTN_NEW] = "Make New Party"; - button_labels[STARTBTN_JOIN] = "Start Scenario"; - button_labels[STARTBTN_SCROLL] = ""; + for(int i = 0; i < MAX_eStartButton; ++i){ + button_labels[i] = startup_button_names[i].c_str(); + } // The 0..65535 version of the blue component was 14472; the commented version was 43144431 sf::Color base_color = {0,0,57}; diff --git a/src/game/boe.main.cpp b/src/game/boe.main.cpp index 24c7a9a3..5e5f5043 100644 --- a/src/game/boe.main.cpp +++ b/src/game/boe.main.cpp @@ -310,6 +310,29 @@ static void process_args(int argc, char* argv[]) { } } +std::map startup_button_indices = { + // Button layout since 11/30/24 + {"Tutorial", 0}, {"Make New Party", 3}, + {"Load Game", 1}, {"Start Scenario", 4}, + {"Preferences", 2}, + + // Buttons that don't exist anymore + {"Custom Scenario", -1}, +}; + +std::map startup_button_names = { + {0, "Tutorial"}, {3, "Make New Party"}, + {1, "Load Game"}, {4, "Start Scenario"}, + {2, "Preferences"}, {5, ""}, +}; + +// Map legacy int indices onto new string-mapped layout +std::map startup_button_names_v1 = { + {0, "Load Game"}, {3, "Start Scenario"}, + {1, "Make New Party"}, {4, "Custom Scenario"}, + {2, "Preferences"}, +}; + void replay_action(Element& action) { bool did_something = false, need_redraw = false, need_reprint = false; @@ -320,7 +343,19 @@ void replay_action(Element& action) { // NOTE: Action replay blocks need to return early unless the action advances time if(overall_mode == MODE_STARTUP && t == "startup_button_click"){ auto info = info_from_action(action); - eStartButton btn = static_cast(std::stoi(info["btn"])); + int btn_idx = -1; + try{ + // Legacy replays use ints to encode startup buttons + btn_idx = std::stoi(info["btn"]); + }catch(std::invalid_argument& err){ + // Newer replays use strings to encode startup buttons + btn_idx = startup_button_indices[info["btn"]]; + } + // No-op button + if(btn_idx == -1){ + return; + } + eStartButton btn = static_cast(btn_idx); eKeyMod mods = static_cast(std::stoi(info["mods"])); handle_startup_button_click(btn, mods); return; diff --git a/src/game/boe.startup.cpp b/src/game/boe.startup.cpp index 894cc559..8e554c4c 100644 --- a/src/game/boe.startup.cpp +++ b/src/game/boe.startup.cpp @@ -42,7 +42,7 @@ enum_map(eStartButton, rectangle) startup_button; void handle_startup_button_click(eStartButton btn, eKeyMod mods) { if(recording){ std::map info; - info["btn"] = boost::lexical_cast(btn); + info["btn"] = startup_button_names[btn]; info["mods"] = boost::lexical_cast(mods); record_action("startup_button_click", info); }