From b422cdf429a82a2e8f5f915e21923fd8e773d2cc Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Wed, 18 Jan 2023 20:08:56 -0500 Subject: [PATCH] Add some terrain bounds checking and some named constants --- src/game/boe.actions.cpp | 8 ++-- src/game/boe.fileio.cpp | 78 +++++++++++++++++++-------------------- src/game/boe.locutils.cpp | 30 ++++++++++----- src/universe/universe.cpp | 14 +++---- src/universe/universe.hpp | 10 +++-- 5 files changed, 77 insertions(+), 63 deletions(-) diff --git a/src/game/boe.actions.cpp b/src/game/boe.actions.cpp index 80b8f824..a37be2e6 100644 --- a/src/game/boe.actions.cpp +++ b/src/game/boe.actions.cpp @@ -1913,12 +1913,12 @@ bool handle_keystroke(const sf::Event& event){ case 'Q': if(!univ.debug_mode) break; if(overall_mode == MODE_OUTDOORS) { - for(short i = 0; i < 96; i++) - for(short j = 0; j < 96; j++) + for(short i = 0; i < univ.out.max_dim; i++) + for(short j = 0; j < univ.out.max_dim; j++) make_explored(i,j); } else { - for(short i = 0; i < 64; i++) - for(short j = 0; j < 64; j++) + for(short i = 0; i < univ.town->max_dim; i++) + for(short j = 0; j < univ.town->max_dim; j++) make_explored(i,j); } clear_map(); diff --git a/src/game/boe.fileio.cpp b/src/game/boe.fileio.cpp index afe8a38a..9c319055 100644 --- a/src/game/boe.fileio.cpp +++ b/src/game/boe.fileio.cpp @@ -132,21 +132,21 @@ void shift_universe_left() { save_outdoor_maps(); univ.party.outdoor_corner.x--; univ.party.i_w_c.x++; - univ.party.out_loc.x += 48; + univ.party.out_loc.x += univ.out.half_dim; - for(short i = 48; i < 96; i++) - for(short j = 0; j < 96; j++) - univ.out.out_e[i][j] = univ.out.out_e[i - 48][j]; + for(short i = univ.out.half_dim; i < univ.out.max_dim; i++) + for(short j = 0; j < univ.out.max_dim; j++) + univ.out.out_e[i][j] = univ.out.out_e[i - univ.out.half_dim][j]; - for(short i = 0; i < 48; i++) - for(short j = 0; j < 96; j++) + for(short i = 0; i < univ.out.half_dim; i++) + for(short j = 0; j < univ.out.max_dim; j++) univ.out.out_e[i][j] = 0; - for(short i = 0; i < 10; i++) { - if(univ.party.out_c[i].m_loc.x > 48) + for(short i = 0; i < univ.party.out_c.size(); i++) { + if(univ.party.out_c[i].m_loc.x > univ.out.half_dim) univ.party.out_c[i].exists = false; if(univ.party.out_c[i].exists) - univ.party.out_c[i].m_loc.x += 48; + univ.party.out_c[i].m_loc.x += univ.out.half_dim; } build_outdoors(); @@ -159,20 +159,20 @@ void shift_universe_right() { save_outdoor_maps(); univ.party.outdoor_corner.x++; univ.party.i_w_c.x--; - univ.party.out_loc.x -= 48; - for(short i = 0; i < 48; i++) - for(short j = 0; j < 96; j++) - univ.out.out_e[i][j] = univ.out.out_e[i + 48][j]; - for(short i = 48; i < 96; i++) - for(short j = 0; j < 96; j++) + univ.party.out_loc.x -= univ.out.half_dim; + for(short i = 0; i < univ.out.half_dim; i++) + for(short j = 0; j < univ.out.max_dim; j++) + univ.out.out_e[i][j] = univ.out.out_e[i + univ.out.half_dim][j]; + for(short i = univ.out.half_dim; i < univ.out.max_dim; i++) + for(short j = 0; j < univ.out.max_dim; j++) univ.out.out_e[i][j] = 0; - for(short i = 0; i < 10; i++) { - if(univ.party.out_c[i].m_loc.x < 48) + for(short i = 0; i < univ.party.out_c.size(); i++) { + if(univ.party.out_c[i].m_loc.x < univ.out.half_dim) univ.party.out_c[i].exists = false; if(univ.party.out_c[i].exists) - univ.party.out_c[i].m_loc.x -= 48; + univ.party.out_c[i].m_loc.x -= univ.out.half_dim; } build_outdoors(); restore_cursor(); @@ -184,20 +184,20 @@ void shift_universe_up() { save_outdoor_maps(); univ.party.outdoor_corner.y--; univ.party.i_w_c.y++; - univ.party.out_loc.y += 48; + univ.party.out_loc.y += univ.out.half_dim; - for(short i = 0; i < 96; i++) - for(short j = 48; j < 96; j++) - univ.out.out_e[i][j] = univ.out.out_e[i][j - 48]; - for(short i = 0; i < 96; i++) - for(short j = 0; j < 48; j++) + for(short i = 0; i < univ.out.max_dim; i++) + for(short j = univ.out.half_dim; j < univ.out.max_dim; j++) + univ.out.out_e[i][j] = univ.out.out_e[i][j - univ.out.half_dim]; + for(short i = 0; i < univ.out.max_dim; i++) + for(short j = 0; j < univ.out.half_dim; j++) univ.out.out_e[i][j] = 0; - for(short i = 0; i < 10; i++) { - if(univ.party.out_c[i].m_loc.y > 48) + for(short i = 0; i < univ.party.out_c.size(); i++) { + if(univ.party.out_c[i].m_loc.y > univ.out.half_dim) univ.party.out_c[i].exists = false; if(univ.party.out_c[i].exists) - univ.party.out_c[i].m_loc.y += 48; + univ.party.out_c[i].m_loc.y += univ.out.half_dim; } build_outdoors(); @@ -211,20 +211,20 @@ void shift_universe_down() { save_outdoor_maps(); univ.party.outdoor_corner.y++; univ.party.i_w_c.y--; - univ.party.out_loc.y = univ.party.out_loc.y - 48; + univ.party.out_loc.y = univ.party.out_loc.y - univ.out.half_dim; - for(short i = 0; i < 96; i++) - for(short j = 0; j < 48; j++) - univ.out.out_e[i][j] = univ.out.out_e[i][j + 48]; - for(short i = 0; i < 96; i++) - for(short j = 48; j < 96; j++) + for(short i = 0; i < univ.out.max_dim; i++) + for(short j = 0; j < univ.out.half_dim; j++) + univ.out.out_e[i][j] = univ.out.out_e[i][j + univ.out.half_dim]; + for(short i = 0; i < univ.out.max_dim; i++) + for(short j = univ.out.half_dim; j < univ.out.max_dim; j++) univ.out.out_e[i][j] = 0; - for(short i = 0; i < 10; i++) { - if(univ.party.out_c[i].m_loc.y < 48) + for(short i = 0; i < univ.party.out_c.size(); i++) { + if(univ.party.out_c[i].m_loc.y < univ.out.half_dim) univ.party.out_c[i].exists = false; if(univ.party.out_c[i].exists) - univ.party.out_c[i].m_loc.y = univ.party.out_c[i].m_loc.y - 48; + univ.party.out_c[i].m_loc.y = univ.party.out_c[i].m_loc.y - univ.out.half_dim; } build_outdoors(); @@ -251,10 +251,10 @@ void position_party(short out_x,short out_y,short pc_pos_x,short pc_pos_y) { } univ.party.i_w_c.x = (univ.party.out_loc.x > 47) ? 1 : 0; univ.party.i_w_c.y = (univ.party.out_loc.y > 47) ? 1 : 0; - for(short i = 0; i < 10; i++) + for(short i = 0; i < univ.party.out_c.size(); i++) univ.party.out_c[i].exists = false; - for(short i = 0; i < 96; i++) - for(short j = 0; j < 96; j++) + for(short i = 0; i < univ.out.max_dim; i++) + for(short j = 0; j < univ.out.max_dim; j++) univ.out.out_e[i][j] = 0; build_outdoors(); } diff --git a/src/game/boe.locutils.cpp b/src/game/boe.locutils.cpp index b005c376..7e102fe4 100644 --- a/src/game/boe.locutils.cpp +++ b/src/game/boe.locutils.cpp @@ -215,16 +215,25 @@ short combat_obscurity(short x, short y) { } ter_num_t coord_to_ter(short x,short y) { - return is_out() ? univ.out[x][y] : univ.town->terrain(x,y); + if(x < 0 || y < 0) return 0; + if(is_out()) { + if(x >= univ.out.max_dim || y >= univ.out.max_dim) { + return 0; + } + return univ.out[x][y]; + } + if(x >= univ.town->max_dim || y >= univ.town->max_dim) { + return 0; + } + return univ.town->terrain(x,y); } //// bool is_container(location loc) { - ter_num_t ter; - + if(loc.x < 0 || loc.y < 0) return false; if((univ.town.is_barrel(loc.x,loc.y)) || (univ.town.is_crate(loc.x,loc.y))) return true; - ter = coord_to_ter(loc.x,loc.y); + ter_num_t ter = coord_to_ter(loc.x,loc.y); if(univ.scenario.ter_types[ter].special == eTerSpec::IS_A_CONTAINER) return true; return false; @@ -266,10 +275,10 @@ void update_explored(location dest) { // All purpose function to check is spot is free for travel into. bool is_blocked(location to_check) { - short gr; - ter_num_t ter; - + if(to_check.x < 0 || to_check.y < 0) return true; if(is_out()) { + if(to_check.x >= univ.out.max_dim || to_check.y >= univ.out.max_dim) + return true; if(impassable(univ.out[to_check.x][to_check.y])) { return true; } @@ -283,8 +292,9 @@ bool is_blocked(location to_check) { } if((is_town()) || (is_combat())) { - ter = univ.town->terrain(to_check.x,to_check.y); - gr = univ.scenario.ter_types[ter].picture; + if(to_check.x >= univ.town->max_dim || to_check.y >= univ.town->max_dim) + return true; + ter_num_t ter = univ.town->terrain(to_check.x,to_check.y); // Terrain blocking? if(impassable(ter)) { @@ -398,6 +408,8 @@ bool can_see_monst(location l,short m_num) { bool outd_is_blocked(location to_check) { if(overall_mode == MODE_OUTDOORS) { + if(to_check.x < 0 || to_check.y < 0 || to_check.x >= univ.out.max_dim || to_check.y >= univ.out.max_dim) + return true; if(impassable(univ.out[to_check.x][to_check.y])) { return true; } diff --git a/src/universe/universe.cpp b/src/universe/universe.cpp index dab9681d..a1feab5f 100644 --- a/src/universe/universe.cpp +++ b/src/universe/universe.cpp @@ -767,7 +767,7 @@ bool cCurTown::is_impassable(short i,short j) { else return false; } -ter_num_t(& cCurOut::operator [] (size_t i))[96]{ +auto cCurOut::operator [] (size_t i) -> arr_96& { return out[i]; } @@ -776,9 +776,9 @@ ter_num_t& cCurOut::operator [] (location loc) { } void cCurOut::writeTo(std::ostream& file) const { - writeArray(file, expl, 96, 96); - writeArray(file, out, 96, 96); - writeArray(file, out_e, 96, 96); + writeArray(file, expl, max_dim, max_dim); + writeArray(file, out, max_dim, max_dim); + writeArray(file, out_e, max_dim, max_dim); // file << "OUTDOORS 0 0" << std::endl; // outdoors[0][0].writeTo(file); // file << "OUTDOORS 0 1" << std::endl; @@ -791,9 +791,9 @@ void cCurOut::writeTo(std::ostream& file) const { } void cCurOut::readFrom(std::istream& file) { - readArray(file, expl, 96, 96); - readArray(file, out, 96, 96); - readArray(file, out_e, 96, 96); + readArray(file, expl, max_dim, max_dim); + readArray(file, out, max_dim, max_dim); + readArray(file, out_e, max_dim, max_dim); } void cCurTown::writeTo(cTagFile& file) const { diff --git a/src/universe/universe.hpp b/src/universe/universe.hpp index 77bdbb18..64c218d0 100644 --- a/src/universe/universe.hpp +++ b/src/universe/universe.hpp @@ -136,9 +136,11 @@ public: class cCurOut { cUniverse& univ; public: - char expl[96][96]; // formerly out_info_type - ter_num_t out[96][96]; - unsigned char out_e[96][96]; + static const int max_dim = 96; + static const int half_dim = max_dim / 2; + char expl[max_dim][max_dim]; // formerly out_info_type + ter_num_t out[max_dim][max_dim]; + unsigned char out_e[max_dim][max_dim]; // These take global coords (ie 0..95) bool is_spot(int x, int y); @@ -146,7 +148,7 @@ public: void import_legacy(legacy::out_info_type& old); - typedef ter_num_t arr_96[96]; + typedef ter_num_t arr_96[max_dim]; arr_96& operator [] (size_t i); ter_num_t& operator [] (location loc); void writeTo(std::ostream& file) const;