extract editor state vars in cScenario to a new struct

This commit is contained in:
2025-05-09 08:37:13 -05:00
parent ae40151726
commit 21536fa1fa
7 changed files with 26 additions and 23 deletions

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -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<cTimer,20> scenario_timers;
std::vector<cSpecial> scen_specials;
std::array<cItemStorage,10> 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<cItem> scen_items;

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -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();
}

View File

@@ -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);