Skip to content

Commit

Permalink
use nested map for SDF names to save stack space. Fix calref#674
Browse files Browse the repository at this point in the history
This also fixes a bug where the wrong dimension was checked
  • Loading branch information
NQNStudios committed Mar 3, 2025
1 parent 3d48cb1 commit 8fc0d09
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/fileio/fileio_scen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,10 @@ void readScenarioFromXml(ticpp::Document&& data, cScenario& scenario) {
} else if(type == "sdf") {
int row, col;
edit->GetAttribute("row", &row);
if(row < 0 || row >= scenario.sdf_names.size())
if(row < 0 || row >= SDF_ROWS)
throw xBadVal(type, "row", std::to_string(row), edit->Row(), edit->Column(), fname);
edit->GetAttribute("col", &col);
if(col < 0 || col >= scenario.sdf_names[0].size())
if(col < 0 || col >= SDF_COLUMNS)
throw xBadVal(type, "col", std::to_string(col), edit->Row(), edit->Column(), fname);
edit->GetText(&scenario.sdf_names[row][col]);
} else if(type == "graphics") {
Expand Down
3 changes: 3 additions & 0 deletions src/global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace bp = boost::process;
const int MAX_GOLD = 30000;
const int MAX_FOOD = 25000;

const int SDF_COLUMNS = 350;
const int SDF_ROWS = 50;

inline bool str_to_bool(std::string str) {
return str == "true";
}
Expand Down
6 changes: 1 addition & 5 deletions src/scenario/scenario.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ struct scenario_header_flags {

enum eContentRating {G, PG, R, NC17};

// TODO: Duplicated in party.hpp
template<typename T, size_t x, size_t y>
using array2d = std::array<std::array<T, y>, x>;

// Used for finding town entrances in the outdoors
struct town_entrance_t {
location out_sec;
Expand Down Expand Up @@ -107,7 +103,7 @@ class cScenario {
std::vector<std::string> evt_names;
std::vector<std::string> ic_names;
std::vector<std::string> itf_names;
array2d<std::string, 350, 50> sdf_names;
std::map<int, std::map<int, std::string>> sdf_names;
bool adjust_diff;
bool is_legacy;
fs::path scen_file; // transient
Expand Down
4 changes: 2 additions & 2 deletions src/scenedit/scen.fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ void writeScenarioToXml(ticpp::Printer&& data, cScenario& scenario) {
data.PushText(scenario.itf_names[i]);
data.CloseElement("item-typeflag");
}
for(int x = 0; x < scenario.sdf_names.size(); x++) {
for(int y = 0; y < scenario.sdf_names[x].size(); y++) {
for(int x = 0; x < SDF_COLUMNS; x++) {
for(int y = 0; y < SDF_ROWS; y++) {
if(scenario.sdf_names[x][y].empty()) continue;
data.OpenElement("sdf");
data.PushAttribute("row", x);
Expand Down
12 changes: 6 additions & 6 deletions src/scenedit/scen.sdfpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ bool cStuffDonePicker::handle_scroll(std::string item_hit) {
if(item_hit == "up") {
if(viewport.y > 0) viewport.y -= rows;
} else if(item_hit == "down") {
if(viewport.y < scenario.sdf_names[0].size() - rows) viewport.y += rows;
if(viewport.y < SDF_ROWS - rows) viewport.y += rows;
} else if(item_hit == "left") {
if(viewport.x > 0) viewport.x -= cols;
} else if(item_hit == "right") {
if(viewport.x < scenario.sdf_names.size() - cols) viewport.x += cols;
if(viewport.x < SDF_COLUMNS - cols) viewport.x += cols;
}
if(viewport.x == 0) dlog["left"].hide();
else dlog["left"].show();
if(viewport.y == 0) dlog["up"].hide();
else dlog["up"].show();
if(viewport.x >= scenario.sdf_names.size() - cols) dlog["right"].hide();
if(viewport.x >= SDF_COLUMNS - cols) dlog["right"].hide();
else dlog["right"].show();
if(viewport.y >= scenario.sdf_names[0].size() - rows) dlog["down"].hide();
if(viewport.y >= SDF_ROWS - rows) dlog["down"].hide();
else dlog["down"].show();
fill_names();
if(!item_hit.empty()) select_active();
Expand Down Expand Up @@ -106,8 +106,8 @@ location cStuffDonePicker::run() {
}

void cStuffDonePicker::clamp_sdf() {
chosen_sdf.x = minmax(0, scenario.sdf_names.size() - 1, chosen_sdf.x);
chosen_sdf.y = minmax(0, scenario.sdf_names[0].size() - 1, chosen_sdf.y);
chosen_sdf.x = minmax(0, SDF_COLUMNS - 1, chosen_sdf.x);
chosen_sdf.y = minmax(0, SDF_ROWS - 1, chosen_sdf.y);
viewport.x = cols * floor(chosen_sdf.x / float(cols));
viewport.y = rows * floor(chosen_sdf.y / float(rows));
}
Expand Down
2 changes: 1 addition & 1 deletion src/universe/party.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class cParty : public iLiving {
unsigned long age;
unsigned short gold;
unsigned short food;
array2d<unsigned char, 350, 50> stuff_done;
array2d<unsigned char, SDF_COLUMNS, SDF_ROWS> stuff_done;
// These used to be stored as magic SDFs
unsigned char hostiles_present;
bool easy_mode = false, less_wm = false;
Expand Down

0 comments on commit 8fc0d09

Please sign in to comment.