From 21536fa1fa54dcfa5f9cbc6283eedf4f2f86cac0 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 9 May 2025 08:37:13 -0500 Subject: [PATCH] extract editor state vars in cScenario to a new struct --- src/fileio/fileio_scen.cpp | 8 ++++---- src/scenario/scenario.cpp | 12 +++++------- src/scenario/scenario.hpp | 13 ++++++++++--- src/scenedit/scen.actions.cpp | 4 ++-- src/scenedit/scen.main.cpp | 4 +--- src/scenedit/scen.townout.cpp | 4 ++-- test/scen_legacy.cpp | 4 ++-- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/fileio/fileio_scen.cpp b/src/fileio/fileio_scen.cpp index 3ad54e49..e0c794fa 100644 --- a/src/fileio/fileio_scen.cpp +++ b/src/fileio/fileio_scen.cpp @@ -944,9 +944,9 @@ void readScenarioFromXml(ticpp::Document&& data, cScenario& scenario) { } // Old scenario files may have last-out-section and last-town in scenario.xml else if(type == "last-out-section") { - scenario.last_out_edited = readLocFromXml(*edit); + scenario.editor_state.last_out_edited = readLocFromXml(*edit); } else if(type == "last-town") { - edit->GetText(&scenario.last_town_edited); + edit->GetText(&scenario.editor_state.last_town_edited); } else if(type == "sound") { int sndnum = 0; @@ -1080,9 +1080,9 @@ void readEditorStateFromXml(ticpp::Document&& data, cScenario& scenario) { for(elem = elem.begin(data.FirstChildElement()); elem != elem.end(); elem++) { elem->GetValue(&type); if(type == "last-out-section") { - scenario.last_out_edited = readLocFromXml(*elem); + scenario.editor_state.last_out_edited = readLocFromXml(*elem); } else if(type == "last-town") { - elem->GetText(&scenario.last_town_edited); + elem->GetText(&scenario.editor_state.last_town_edited); } } } diff --git a/src/scenario/scenario.cpp b/src/scenario/scenario.cpp index 78c4b66b..ee7f609c 100644 --- a/src/scenario/scenario.cpp +++ b/src/scenario/scenario.cpp @@ -110,8 +110,7 @@ cScenario::cScenario(const cScenario& other) , scenario_timers(other.scenario_timers) , scen_specials(other.scen_specials) , storage_shortcuts(other.storage_shortcuts) - , last_out_edited(other.last_out_edited) - , last_town_edited(other.last_town_edited) + , editor_state(other.editor_state) , format(other.format) , campaign_id(other.campaign_id) , scen_items(other.scen_items) @@ -178,8 +177,7 @@ void swap(cScenario& lhs, cScenario& rhs) { swap(lhs.scenario_timers, rhs.scenario_timers); swap(lhs.scen_specials, rhs.scen_specials); swap(lhs.storage_shortcuts, rhs.storage_shortcuts); - swap(lhs.last_out_edited, rhs.last_out_edited); - swap(lhs.last_town_edited, rhs.last_town_edited); + swap(lhs.editor_state, rhs.editor_state); swap(lhs.format, rhs.format); swap(lhs.campaign_id, rhs.campaign_id); swap(lhs.scen_items, rhs.scen_items); @@ -263,9 +261,9 @@ void cScenario::import_legacy(legacy::scenario_data_type& old){ scenario_timers[i].time = old.scenario_timer_times[i]; scenario_timers[i].node = old.scenario_timer_specs[i]; } - last_out_edited.x = old.last_out_edited.x; - last_out_edited.y = old.last_out_edited.y; - last_town_edited = old.last_town_edited; + editor_state.last_out_edited.x = old.last_out_edited.x; + editor_state.last_out_edited.y = old.last_out_edited.y; + editor_state.last_town_edited = old.last_town_edited; adjust_diff = true; } diff --git a/src/scenario/scenario.hpp b/src/scenario/scenario.hpp index 22d03bea..99f4344b 100644 --- a/src/scenario/scenario.hpp +++ b/src/scenario/scenario.hpp @@ -46,6 +46,15 @@ struct town_entrance_t { int town; }; +// This is completely unnecessary outside of the scenario editor, but harmless to load anyway, +// and much easier to store in cScenario so readScenarioFromXML() doesn't need conditionally compiled +// access to scenedit-specific global variables (which won't work unless we want to compile the common +// sources 3 times), or globals redeclared for no reason in boe.main.cpp and pc.main.cpp +struct editor_state_t { + short last_town_edited; + location last_out_edited; +}; + class cScenario { public: class cItemStorage { @@ -84,9 +93,7 @@ public: std::array scenario_timers; std::vector scen_specials; std::array storage_shortcuts; - // These two \/ are populated at load time and then never used again - location last_out_edited; - short last_town_edited; + editor_state_t editor_state; scenario_header_flags format; std::string campaign_id; // A hopefully unique identifier to specify the campaign this scenario is a part of. std::vector scen_items; diff --git a/src/scenedit/scen.actions.cpp b/src/scenedit/scen.actions.cpp index 839e95a5..2b2c1e0e 100644 --- a/src/scenedit/scen.actions.cpp +++ b/src/scenedit/scen.actions.cpp @@ -2882,6 +2882,6 @@ bool monst_on_space(location loc,short m_num) { } void restore_editor_state() { - set_current_town(scenario.last_town_edited); - set_current_out(scenario.last_out_edited); + set_current_town(scenario.editor_state.last_town_edited); + set_current_out(scenario.editor_state.last_out_edited); } \ No newline at end of file diff --git a/src/scenedit/scen.main.cpp b/src/scenedit/scen.main.cpp index d8964a97..6bfcd8c5 100644 --- a/src/scenedit/scen.main.cpp +++ b/src/scenedit/scen.main.cpp @@ -448,10 +448,8 @@ void handle_menu_choice(eMenu item_hit) { break; file_to_load = item_hit == eMenu::FILE_OPEN ? nav_get_scenario() : scenario.scen_file; if(!file_to_load.empty() && load_scenario(file_to_load, scenario)) { - cur_town = scenario.last_town_edited; - town = scenario.towns[cur_town]; + restore_editor_state(); change_made = false; - set_current_out(scenario.last_out_edited); } else if(!file_to_load.empty()) set_up_start_screen(); // Failed to load file, dump to start undo_list.clear(); diff --git a/src/scenedit/scen.townout.cpp b/src/scenedit/scen.townout.cpp index 1b6013f4..211c4824 100644 --- a/src/scenedit/scen.townout.cpp +++ b/src/scenedit/scen.townout.cpp @@ -1529,12 +1529,12 @@ void set_current_town(int to) { if(to < 0 || to >= scenario.towns.size()) return; cur_town = to; town = scenario.towns[cur_town]; - scenario.last_town_edited = cur_town; + scenario.editor_state.last_town_edited = cur_town; } void set_current_out(location out_sec) { cur_out = out_sec; - scenario.last_out_edited = cur_out; + scenario.editor_state.last_out_edited = cur_out; current_terrain = scenario.outdoors[cur_out.x][cur_out.y]; set_up_main_screen(); } diff --git a/test/scen_legacy.cpp b/test/scen_legacy.cpp index 7e721ed5..a73119d4 100644 --- a/test/scen_legacy.cpp +++ b/test/scen_legacy.cpp @@ -72,8 +72,8 @@ TEST_CASE("Converting legacy scenario data") { CHECK(scen.intro_pic == 27); CHECK(scen.is_legacy); CHECK(scen.journal_strs.empty()); - CHECK(scen.last_out_edited == loc(1,1)); - CHECK(scen.last_town_edited == 2); + CHECK(scen.editor_state.last_out_edited == loc(1,1)); + CHECK(scen.editor_state.last_town_edited == 2); CHECK(scen.out_sec_start == loc(4,4)); CHECK(scen.out_start == loc(1,1)); CHECK(scen.rating == eContentRating::R);