Replace C-style 2D arrays with nested std::array

This commit is contained in:
2023-01-24 20:26:57 -05:00
parent 1a0da3c4d7
commit 64c7e4a5a0
6 changed files with 36 additions and 22 deletions

View File

@@ -37,7 +37,7 @@ cParty::cParty(ePartyPreset party_preset) {
out_loc.y = 84;
in_boat = -1;
in_horse = -1;
memset(stuff_done, 0, sizeof(stuff_done));
wipe_sdfs();
std::fill(magic_ptrs.begin(), magic_ptrs.end(), 0);
for(int i = 0; i < 10; i++)
out_c[i].exists = false;
@@ -53,6 +53,7 @@ cParty::cParty(const cParty& other)
, age(other.age)
, gold(other.gold)
, food(other.food)
, stuff_done(other.stuff_done)
, hostiles_present(other.hostiles_present)
, easy_mode(other.easy_mode)
, less_wm(other.less_wm)
@@ -101,7 +102,6 @@ cParty::cParty(const cParty& other)
, campaign_flags(other.campaign_flags)
, pointers(other.pointers)
{
memcpy(stuff_done, other.stuff_done, sizeof(stuff_done));
for(int i = 0; i < 6; i++) {
adven[i].reset(new cPlayer(*this, *other.adven[i]));
}
@@ -121,6 +121,7 @@ void cParty::swap(cParty& other) {
std::swap(age, other.age);
std::swap(gold, other.gold);
std::swap(food, other.food);
std::swap(stuff_done, other.stuff_done);
std::swap(hostiles_present, other.hostiles_present);
std::swap(easy_mode, other.easy_mode);
std::swap(less_wm, other.less_wm);
@@ -170,11 +171,6 @@ void cParty::swap(cParty& other) {
std::swap(scen_played, other.scen_played);
std::swap(campaign_flags, other.campaign_flags);
std::swap(pointers, other.pointers);
unsigned char temp_sdf[350][50];
memcpy(temp_sdf, stuff_done, sizeof(stuff_done));
memcpy(stuff_done, other.stuff_done, sizeof(stuff_done));
memcpy(other.stuff_done, temp_sdf, sizeof(stuff_done));
unsigned short temp_setup[4][64][64];
for(size_t i = 0; i < adven.size(); i++) {
std::swap(adven[i], other.adven[i]);
}
@@ -908,7 +904,7 @@ void cParty::readFrom(const cTagFile& file) {
left_in = -1;
}
memset(stuff_done, 0, sizeof(stuff_done));
wipe_sdfs();
for(size_t i = 0; i < page["SDF"].size(); i++) {
size_t x, y, val;
page["SDF"] >> x >> y >> val;
@@ -1188,6 +1184,12 @@ bool cParty::sd_legit(short a, short b) const {
return false;
}
void cParty::wipe_sdfs() {
for(auto& col : stuff_done) {
col.fill(0);
}
}
bool operator==(const cParty::cConvers& one, const cParty::cConvers& two) {
if(one.who_said != two.who_said) return false;
if(one.in_town != two.in_town) return false;

View File

@@ -36,13 +36,16 @@ namespace legacy {
struct setup_save_type;
};
template<typename T, size_t x, size_t y>
using array2d = std::array<std::array<T, y>, x>;
struct campaign_flag_type{
unsigned char idx[25][25];
array2d<unsigned char, 25, 25> idx{};
private:
using idx_array = decltype(idx);
public:
static const int x_max = std::extent<idx_array, 0>::value - 1;
static const int y_max = std::extent<idx_array, 1>::value - 1;
static const int x_max = std::tuple_size<idx_array>::value;
static const int y_max = std::tuple_size<idx_array::value_type>::value;
};
struct job_bank_t {
@@ -87,7 +90,7 @@ public:
unsigned long age;
unsigned short gold;
unsigned short food;
unsigned char stuff_done[350][50];
array2d<unsigned char, 350, 50> stuff_done;
// These used to be stored as magic SDFs
unsigned char hostiles_present;
bool easy_mode = false, less_wm = false;
@@ -138,8 +141,8 @@ private:
std::map<unsigned short,std::pair<unsigned short,unsigned char>> pointers;
using sd_array = decltype(stuff_done);
public:
static const int sdx_max = std::extent<sd_array, 0>::value - 1;
static const int sdy_max = std::extent<sd_array, 1>::value - 1;
static const int sdx_max = std::tuple_size<sd_array>::value;
static const int sdy_max = std::tuple_size<sd_array::value_type>::value;
void set_ptr(unsigned short p, unsigned short sdfx, unsigned short sdfy);
void force_ptr(unsigned short p, unsigned short val);
@@ -211,6 +214,7 @@ public:
void swap_pcs(size_t a, size_t b);
bool sd_legit(short a, short b) const;
void wipe_sdfs();
auto begin() -> boost::indirect_iterator<decltype(adven)::iterator> {
return boost::make_indirect_iterator(adven.begin());

View File

@@ -1018,8 +1018,8 @@ void cUniverse::swap(cUniverse& other) {
}
void cCurOut::copy(const cCurOut& other) {
memcpy(out, other.out, sizeof(out));
memcpy(out_e, other.out_e, sizeof(out_e));
out = other.out;
out_e = other.out_e;
}
void cCurOut::swap(cCurOut& other) {
@@ -1381,7 +1381,7 @@ void cUniverse::enter_scenario(const std::string& name) {
using namespace std::placeholders;
party.age = 0;
memset(party.stuff_done, 0, sizeof(party.stuff_done));
party.wipe_sdfs();
party.light_level = 0;
party.outdoor_corner = scenario.out_sec_start;
party.i_w_c = {0, 0};

View File

@@ -144,8 +144,8 @@ class cCurOut {
public:
static const int max_dim = 96;
static const int half_dim = max_dim / 2;
ter_num_t out[max_dim][max_dim];
unsigned char out_e[max_dim][max_dim];
array2d<ter_num_t, max_dim, max_dim> out;
array2d<unsigned char, max_dim, max_dim> out_e;
// These take global coords (ie 0..95)
bool is_spot(int x, int y) const;
@@ -154,7 +154,7 @@ public:
void import_legacy(legacy::out_info_type& old);
typedef ter_num_t arr_96[max_dim];
using arr_96 = decltype(out)::value_type;
arr_96& operator [] (size_t i);
const arr_96& operator [] (size_t i) const;
ter_num_t& operator [] (location loc);