const-correctness in universe classes

This commit is contained in:
2023-01-22 17:53:18 -05:00
parent 346be973a6
commit 47bf06c15b
9 changed files with 104 additions and 48 deletions

View File

@@ -2211,7 +2211,7 @@ void general_spec(const runtime_state& ctx) {
break;
case eSpecType::BUY_ITEMS_OF_TYPE:
for(short i = 0; i < 144; i++)
if(univ.party.check_class(spec.ex1a,true))
if(univ.party.take_class(spec.ex1a))
store_val++;
if(store_val == 0) {
if(spec.ex1b >= 0)
@@ -3341,7 +3341,9 @@ void ifthen_spec(const runtime_state& ctx) {
}
break;
case eSpecType::IF_HAVE_ITEM_CLASS:
if(univ.party.check_class(spec.ex1a,spec.ex2a > 0))
if(spec.ex2a > 0 && univ.party.take_class(spec.ex1a))
ctx.next_spec = spec.ex1b;
else if(spec.ex2a == 0 && univ.party.has_class(spec.ex1a))
ctx.next_spec = spec.ex1b;
break;
case eSpecType::IF_EQUIP_ITEM_CLASS:

View File

@@ -382,7 +382,7 @@ void cCreature::readFrom(const cTagFile_Page& page) {
page["DIRECTION"] >> direction;
}
void cCreature::print_attacks(iLiving* target) {
void cCreature::print_attacks(iLiving* target) const {
if(!print_result) return;
std::string msg = m_name;
msg += " attacks ";
@@ -394,7 +394,7 @@ void cCreature::print_attacks(iLiving* target) {
print_result(msg);
}
void cCreature::spell_note(int which_mess) {
void cCreature::spell_note(int which_mess) const {
if(!print_result) return;
std::string msg = m_name;
switch(which_mess) {
@@ -590,18 +590,18 @@ void cCreature::spell_note(int which_mess) {
print_result((char *) msg.c_str());
}
void cCreature::cast_spell_note(eSpell spell) {
void cCreature::cast_spell_note(eSpell spell) const {
if(!print_result) return;
print_result(m_name + " casts:");
print_result(" " + (*spell).name());
}
void cCreature::breathe_note() {
void cCreature::breathe_note() const {
if(!print_result) return;
print_result(m_name + " breathes.");
}
void cCreature::damaged_msg(int how_much,int how_much_spec) {
void cCreature::damaged_msg(int how_much,int how_much_spec) const {
if(!print_result) return;
std::ostringstream sout;
sout << " " << m_name << " takes " << how_much;
@@ -610,7 +610,7 @@ void cCreature::damaged_msg(int how_much,int how_much_spec) {
print_result(sout.str());
}
void cCreature::killed_msg() {
void cCreature::killed_msg() const {
if(!print_result) return;
print_result(" " + m_name + " dies.");
}

View File

@@ -62,12 +62,12 @@ public:
int magic_adjust(int base);
void spell_note(int which);
void cast_spell_note(eSpell spell);
void print_attacks(iLiving* target);
void breathe_note();
void damaged_msg(int how_much, int extra);
void killed_msg();
void spell_note(int which) const;
void cast_spell_note(eSpell spell) const;
void print_attacks(iLiving* target) const;
void breathe_note() const;
void damaged_msg(int how_much, int extra) const;
void killed_msg() const;
bool on_space(location loc) const;
void import_legacy(legacy::creature_data_type old);

View File

@@ -365,14 +365,14 @@ void cParty::replace_pc(size_t spot, std::unique_ptr<cPlayer> with) {
}
}
size_t cParty::free_space() {
size_t cParty::free_space() const {
for(int i = 0; i < 6; i++)
if(adven[i]->main_status == eMainStatus::ABSENT)
return i;
return 6;
}
size_t cParty::count(eMainStatus type) {
size_t cParty::count(eMainStatus type) const {
size_t sz = 0;
for(int i = 0; i < 6; i++)
if(adven[i]->main_status == type)
@@ -621,7 +621,7 @@ bool cParty::forced_give(cItem item,eItemAbil abil,short dat) {
return false;
}
bool cParty::has_abil(eItemAbil abil, short dat) {
bool cParty::has_abil(eItemAbil abil, short dat) const {
for(int i = 0; i < 6; i++)
if(adven[i]->main_status == eMainStatus::ALIVE)
if(adven[i]->has_abil(abil,dat))
@@ -641,17 +641,26 @@ bool cParty::take_abil(eItemAbil abil, short dat) {
return false;
}
bool cParty::check_class(unsigned int item_class,bool take) {
bool cParty::has_class(unsigned int item_class) {
if(item_class == 0)
return false;
for(auto& pc : *this)
if(pc.main_status == eMainStatus::ALIVE)
if(cInvenSlot item = pc.has_class(item_class)) {
if(take) {
if(item->charges > 1)
item->charges--;
else pc.take_item(item.slot);
}
return true;
}
return false;
}
bool cParty::take_class(unsigned int item_class) const {
if(item_class == 0)
return false;
for(auto& pc : *this)
if(pc.main_status == eMainStatus::ALIVE)
if(cInvenSlot item = pc.has_class(item_class)) {
if(item->charges > 1)
item->charges--;
else pc.take_item(item.slot);
return true;
}
return false;
@@ -1107,7 +1116,7 @@ void cParty::force_ptr(unsigned short p, unsigned short val){
magic_ptrs[p-10] = val;
}
unsigned char cParty::get_ptr(unsigned short p){
unsigned char cParty::get_ptr(unsigned short p) const {
if(p < 10 || p >= 200)
throw std::range_error("Attempted to access a nonexistent pointer (10..199)");
if(p < 100)
@@ -1173,7 +1182,7 @@ iLiving& cParty::pc_present() const {
}
// stuff done legit, i.e. flags are within proper ranges for stuff done flag
bool cParty::sd_legit(short a, short b) {
bool cParty::sd_legit(short a, short b) const {
if((minmax(0,sdx_max,a) == a) && (minmax(0,sdy_max,b) == b))
return true;
return false;

View File

@@ -144,7 +144,7 @@ public:
void set_ptr(unsigned short p, unsigned short sdfx, unsigned short sdfy);
void force_ptr(unsigned short p, unsigned short val);
void clear_ptr(unsigned short p);
unsigned char get_ptr(unsigned short p);
unsigned char get_ptr(unsigned short p) const;
void import_legacy(legacy::party_record_type& old, cUniverse& univ);
void import_legacy(legacy::big_tr_type& old);
@@ -185,9 +185,8 @@ public:
std::unique_ptr<cPlayer> remove_pc(size_t spot);
void new_pc(size_t spot);
void replace_pc(size_t spot, std::unique_ptr<cPlayer> with);
size_t free_space();
size_t count(eMainStatus type = eMainStatus::ALIVE);
void void_pcs();
size_t free_space() const;
size_t count(eMainStatus type = eMainStatus::ALIVE) const;
bool save_talk(const std::string& who, const std::string& where, const std::string& str1, const std::string& str2);
bool add_to_journal(const std::string& event, short day);
bool record(eEncNoteType type, const std::string& what, const std::string& where);
@@ -199,9 +198,10 @@ public:
bool give_item(cItem item,int flags);
bool forced_give(cItem item,eItemAbil abil,short dat = -1);
bool has_abil(eItemAbil abil, short dat = -1);
bool has_abil(eItemAbil abil, short dat = -1) const;
bool take_abil(eItemAbil abil, short dat = -1);
bool check_class(unsigned int item_class,bool take);
bool has_class(unsigned int item_class);
bool take_class(unsigned int item_class) const;
bool start_split(short x, short y, snd_num_t noise, short who);
bool end_split(snd_num_t noise);
@@ -210,7 +210,7 @@ public:
iLiving& pc_present() const; // If only one pc is present, returns that pc. Otherwise returns party.
void swap_pcs(size_t a, size_t b);
bool sd_legit(short a, short b);
bool sd_legit(short a, short b) const;
auto begin() -> boost::indirect_iterator<decltype(adven)::iterator> {
return boost::make_indirect_iterator(adven.begin());
@@ -220,6 +220,14 @@ public:
return boost::make_indirect_iterator(adven.end());
}
auto begin() const -> boost::indirect_iterator<decltype(adven)::const_iterator> {
return boost::make_indirect_iterator(adven.begin());
}
auto end() const -> boost::indirect_iterator<decltype(adven)::const_iterator> {
return boost::make_indirect_iterator(adven.end());
}
typedef std::vector<cEncNote>::iterator encIter;
typedef std::vector<cJournal>::iterator journalIter;
typedef std::vector<cConvers>::iterator talkIter;

View File

@@ -62,7 +62,7 @@ void cPlayer::import_legacy(legacy::pc_record_type old){
direction = eDirection(old.direction);
}
short cPlayer::get_tnl(){
short cPlayer::get_tnl() const {
// Omitting a race from this list gives it a value of 0, thanks to the defaulting implementation of operator[]
static std::map<const eRace, const int> rp = {{eRace::NEPHIL,12},{eRace::SLITH,20},{eRace::VAHNATAI,18}};
static std::map<const eTrait, const short> ap = {
@@ -619,6 +619,10 @@ bool cPlayer::unequip_item(int which_item, bool do_print) {
return true;
}
const std::pair<cInvenSlot, cInvenSlot> cPlayer::get_weapons() const {
return const_cast<cPlayer*>(this)->get_weapons();
}
std::pair<cInvenSlot, cInvenSlot> cPlayer::get_weapons() {
cInvenSlot weap1 = has_type_equip(eItemType::ONE_HANDED);
if(weap1) {

View File

@@ -144,6 +144,7 @@ public:
bool equip_item(int which_item, bool do_print);
bool unequip_item(int which_item, bool do_print);
std::pair<cInvenSlot, cInvenSlot> get_weapons();
const std::pair<cInvenSlot, cInvenSlot> get_weapons() const;
void take_item(int which_item);
void remove_charge(int which_item);
const cInvenSlot has_space() const;
@@ -180,7 +181,7 @@ public:
cPlayer(cParty& party,ePartyPreset key,short slot);
cPlayer(no_party_t, const cPlayer& other);
cPlayer(cParty& party, const cPlayer& other);
short get_tnl();
short get_tnl() const;
void writeTo(cTagFile& file) const;
void readFrom(const cTagFile& file);
virtual ~cPlayer() = default;

View File

@@ -107,6 +107,14 @@ cTown& cCurTown::operator * (){
return *record();
}
const cTown* cCurTown::operator -> () const {
return record();
}
const cTown& cCurTown::operator * () const {
return *record();
}
void cCurTown::place_preset_fields() {
// Initialize barriers, etc. Note non-sfx gets forgotten if this is a town recently visited.
fields.resize(record()->max_dim, record()->max_dim);
@@ -196,7 +204,12 @@ void cCurTown::save_setup(vector2d<unsigned short>& setup) const {
}
cSpeech& cCurTown::cur_talk() {
// Make sure we actually have a valid speech stored
// TODO: Make sure we actually have a valid speech stored
return univ.scenario.towns[cur_talk_loaded]->talking;
}
const cSpeech& cCurTown::cur_talk() const {
// TODO: Make sure we actually have a valid speech stored
return univ.scenario.towns[cur_talk_loaded]->talking;
}
@@ -790,7 +803,7 @@ bool cCurTown::set_force_cage(short x, short y, bool b){
}
// TODO: This seems to be wrong; impassable implies "blocks movement", which two other blockages also do
bool cCurTown::is_impassable(short i,short j) {
bool cCurTown::is_impassable(short i,short j) const {
if(!is_on_map(i, j)) return false;
ter_num_t ter;
@@ -811,10 +824,18 @@ auto cCurOut::operator [] (size_t i) -> arr_96& {
return out[i];
}
auto cCurOut::operator [] (size_t i) const -> const arr_96& {
return out[i];
}
ter_num_t& cCurOut::operator [] (location loc) {
return out[loc.x][loc.y];
}
const ter_num_t& cCurOut::operator [] (location loc) const {
return out[loc.x][loc.y];
}
void cCurOut::writeTo(std::ostream& file) const {
writeArray(file, out, max_dim, max_dim);
writeArray(file, out_e, max_dim, max_dim);
@@ -914,7 +935,11 @@ cOutdoors* cCurOut::operator->() {
return univ.scenario.outdoors[x][y];
}
bool cCurOut::is_spot(int x, int y) {
const cOutdoors* cCurOut::operator->() const {
return const_cast<cCurOut*>(this)->operator->();
}
bool cCurOut::is_spot(int x, int y) const {
int sector_x = 0, sector_y = 0;
if(x >= 48) sector_x++, x -= 48;
if(y >= 48) sector_y++, y -= 48;
@@ -926,7 +951,7 @@ bool cCurOut::is_spot(int x, int y) {
return univ.scenario.outdoors[sector_x][sector_y]->special_spot[x][y];
}
bool cCurOut::is_road(int x, int y) {
bool cCurOut::is_road(int x, int y) const {
int sector_x = 0, sector_y = 0;
if(x >= 48) sector_x++, x -= 48;
if(y >= 48) sector_y++, y -= 48;
@@ -938,7 +963,7 @@ bool cCurOut::is_road(int x, int y) {
return univ.scenario.outdoors[sector_x][sector_y]->roads[x][y];
}
bool cCurOut::is_on_map(int x, int y) {
bool cCurOut::is_on_map(int x, int y) const {
if(x < 0 || y < 0) return false;
if(x >= max_dim) return false;
if(y >= max_dim) return false;
@@ -1344,7 +1369,7 @@ void cUniverse::clear_stored_pcs() {
stored_pcs.clear();
}
short cCurTown::countMonsters(){
short cCurTown::countMonsters() const {
short to_ret = 0;
for(short i = 0; i < monst.size(); i++)
if(monst[i].active > 0)

View File

@@ -54,9 +54,13 @@ public:
cTown* operator -> ();
cTown& operator * ();
const cTown* operator -> () const;
const cTown& operator * () const;
explicit cCurTown(cUniverse& univ);
short countMonsters();
cSpeech& cur_talk(); // Get the currently loaded speech
short countMonsters() const;
// Get the currently loaded speech
cSpeech& cur_talk();
const cSpeech& cur_talk() const;
bool prep_talk(short which); // Prepare for loading specified speech, returning true if already loaded
void prep_arena(); // Set up for a combat arena
void place_preset_fields();
@@ -72,7 +76,7 @@ public:
bool is_ice_wall(short x, short y) const;
bool is_blade_wall(short x, short y) const;
bool is_sleep_cloud(short x, short y) const;
bool is_block(short x, short y) const; // currently unused
bool is_block(short x, short y) const; // pushable block
bool is_spot(short x, short y) const;
bool is_special(short x, short y) const;
bool is_web(short x, short y) const;
@@ -99,7 +103,7 @@ public:
bool set_ice_wall(short x, short y, bool b);
bool set_blade_wall(short x, short y, bool b);
bool set_sleep_cloud(short x, short y, bool b);
bool set_block(short x, short y, bool b); // currently unused
bool set_block(short x, short y, bool b); // pushable block
bool set_spot(short x, short y, bool b);
bool set_web(short x, short y, bool b);
bool set_crate(short x, short y, bool b);
@@ -117,7 +121,7 @@ public:
bool set_rubble(short x, short y, bool b);
bool set_force_cage(short x, short y, bool b);
bool set_road(short x, short y, bool b);
bool is_impassable(short x, short y);
bool is_impassable(short x, short y) const;
bool is_on_map(short x, short y) const;
void writeTo(cTagFile& file) const;
void readFrom(const cTagFile& file);
@@ -144,18 +148,21 @@ public:
unsigned char out_e[max_dim][max_dim];
// These take global coords (ie 0..95)
bool is_spot(int x, int y);
bool is_road(int x, int y);
bool is_on_map(int x, int y);
bool is_spot(int x, int y) const;
bool is_road(int x, int y) const;
bool is_on_map(int x, int y) const;
void import_legacy(legacy::out_info_type& old);
typedef ter_num_t arr_96[max_dim];
arr_96& operator [] (size_t i);
const arr_96& operator [] (size_t i) const;
ter_num_t& operator [] (location loc);
const ter_num_t& operator [] (location loc) const;
void writeTo(std::ostream& file) const;
void readFrom(std::istream& file);
cOutdoors* operator->();
const cOutdoors* operator->() const;
explicit cCurOut(cUniverse& univ);
// It's not directly copyable due to the cUniverse reference, which must always point to the cUniverse that contains it.
// The cUniverse copy constructor is thus responsible for performing the copy.