From 785943b9be166ee4221428b19fa60d85cafba759 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 15 Aug 2016 12:31:45 -0400 Subject: [PATCH] More usage of standard containers instead of bare arrays --- src/boe.actions.cpp | 14 ++++++------- src/boe.combat.cpp | 11 +++++----- src/boe.dlgutil.cpp | 2 +- src/boe.fileio.cpp | 8 +++---- src/boe.infodlg.cpp | 2 +- src/boe.monster.cpp | 6 +++--- src/boe.party.cpp | 34 ++++++++++++++---------------- src/boe.specials.cpp | 6 +++--- src/boe.town.cpp | 38 ++++++++++++++++++---------------- src/classes/monster.cpp | 7 ++++--- src/classes/monster.hpp | 3 ++- src/classes/outdoors.cpp | 2 +- src/classes/party.cpp | 22 ++++++++++++++------ src/classes/party.hpp | 10 ++++----- src/classes/pc.cpp | 14 ++++++------- src/classes/pc.hpp | 4 ++-- src/classes/scenario.hpp | 2 +- src/classes/talking.hpp | 2 +- src/classes/town.cpp | 20 +++++++----------- src/classes/town.hpp | 7 +++---- src/classes/universe.cpp | 22 ++++++++++---------- src/pcedit/pc.main.cpp | 4 ++-- src/scenedit/scen.fileio.cpp | 20 +++++++++--------- src/scenedit/scen.graphics.cpp | 10 ++++----- src/scenedit/scen.keydlgs.cpp | 4 ++-- src/scenedit/scen.townout.cpp | 16 +++++++------- src/tools/fileio_scen.cpp | 8 +++---- 27 files changed, 151 insertions(+), 147 deletions(-) diff --git a/src/boe.actions.cpp b/src/boe.actions.cpp index 31ef9b14..8e3586a3 100644 --- a/src/boe.actions.cpp +++ b/src/boe.actions.cpp @@ -1817,11 +1817,11 @@ bool handle_keystroke(sf::Event& event){ univ.party[i].cur_sp = 100; } award_party_xp(25); - for(i = 0; i < 6; i++) - for(j = 0; j < 62; j++) { - univ.party[i].priest_spells[j] = 1; - univ.party[i].mage_spells[j] = 1; - } + for(i = 0; i < 6; i++) { + auto& who = univ.party[i]; + who.priest_spells.set(); + who.mage_spells.set(); + } refresh_store_items(); add_string_to_buf("Debug: Add stuff and heal."); print_buf(); @@ -2028,8 +2028,8 @@ bool handle_keystroke(sf::Event& event){ ASB("DEBUG: Towns have short memory."); ASB("Your deeds have been forgotten."); print_buf(); - for(i = 0; i < 4; i++) - univ.party.creature_save[i].which_town = 200; + for(auto& pop : univ.party.creature_save) + pop.which_town = 200; break; case '!': if(!univ.debug_mode) break; diff --git a/src/boe.combat.cpp b/src/boe.combat.cpp index 577facf9..27efcdc1 100644 --- a/src/boe.combat.cpp +++ b/src/boe.combat.cpp @@ -2806,10 +2806,11 @@ void monster_attack(short who_att,iLiving* target) { frame_space(target->get_loc(),1,1,1); } - - - if(attacker->a[0].dice != 0 || attacker->a[1].dice != 0 || attacker->a[2].dice != 0) - attacker->print_attacks(target); + for(const auto& att : attacker->a) + if(att.dice != 0) { + attacker->print_attacks(target); + break; + } // Some things depend on whether it's a player or a monster. cCreature* m_target = dynamic_cast(target); @@ -2824,7 +2825,7 @@ void monster_attack(short who_att,iLiving* target) { return; } - for(i = 0; i < 3; i++) { + for(i = 0; i < attacker->a.size(); i++) { if(attacker->a[i].dice > 0 && target->is_alive()) { // sprintf ((char *) create_line, " Attacks %s.",(char *) univ.party[target].name); // add_string_to_buf((char *) create_line); diff --git a/src/boe.dlgutil.cpp b/src/boe.dlgutil.cpp index e6dd22ba..bd8f2187 100644 --- a/src/boe.dlgutil.cpp +++ b/src/boe.dlgutil.cpp @@ -1007,7 +1007,7 @@ void handle_talk_event(location p) { else { univ.party.gold -= a; put_pc_screen(); - univ.party.can_find_town[b] = 1; + univ.party.can_find_town[b] = true; } save_talk_str2 = ""; break; diff --git a/src/boe.fileio.cpp b/src/boe.fileio.cpp index 202815b1..2af5a6ce 100644 --- a/src/boe.fileio.cpp +++ b/src/boe.fileio.cpp @@ -77,10 +77,10 @@ void finish_load_party(){ // Saved creatures may not have had their monster attributes saved // Make sure that they know what they are! // Cast to cMonster base class and assign, to avoid clobbering other attributes - for(int i = 0; i < 4; i++) { - for(size_t j = 0; j < univ.party.creature_save[i].size(); j++) { - int number = univ.party.creature_save[i][j].number; - cMonster& monst = univ.party.creature_save[i][j]; + for(auto& pop : univ.party.creature_save) { + for(size_t i = 0; i < pop.size(); i++) { + int number = pop[i].number; + cMonster& monst = pop[i]; monst = univ.scenario.scen_monsters[number]; } } diff --git a/src/boe.infodlg.cpp b/src/boe.infodlg.cpp index 8f1a9109..5663fb0f 100644 --- a/src/boe.infodlg.cpp +++ b/src/boe.infodlg.cpp @@ -366,7 +366,7 @@ static void put_monst_info(cDialog& me, const cCreature& store_m) { i++; } - for(i = 0; i < 3; i++) { + for(i = 0; i < store_m.a.size(); i++) { if(store_m.a[i].dice > 0) { if(store_m.a[i].sides == 0) continue; std::ostringstream sout(std::ios_base::ate); diff --git a/src/boe.monster.cpp b/src/boe.monster.cpp index bd87f026..42602d10 100644 --- a/src/boe.monster.cpp +++ b/src/boe.monster.cpp @@ -47,7 +47,7 @@ void create_wand_monst() { r1 = get_ran(1,0,univ.out->wandering.size() - 1); if(overall_mode == MODE_OUTDOORS) { if(!univ.out->wandering[r1].isNull()) { - r2 = get_ran(1,0,3); + r2 = get_ran(1,0,univ.out->wandering_locs.size() - 1); while(point_onscreen(univ.out->wandering_locs[r2], global_to_local(univ.party.out_loc)) && num_tries++ < 100) r2 = get_ran(1,0,3); if(!is_blocked(univ.out->wandering_locs[r2])) @@ -593,7 +593,7 @@ bool rand_move(mon_num_t i) { if(univ.town.monst[i].targ_loc.x == 0) { // maybe pick a wand loc, else juist pick a loc - j = get_ran(1,0,3); + j = get_ran(1,0,univ.town->wandering_locs.size() - 1); store_loc = univ.town->wandering_locs[j]; if(!loc_off_act_area(store_loc) && (get_ran(1,0,1) == 1)) @@ -1080,7 +1080,7 @@ void record_monst(cCreature* which_m, bool forced) { } else { which_m->spell_note(24); - r1 = get_ran(1,0,3); + r1 = get_ran(1,0,univ.party.imprisoned_monst.size() - 1); if(univ.party.imprisoned_monst[r1] == 0) univ.party.imprisoned_monst[r1] = which_m->number; else { diff --git a/src/boe.party.cpp b/src/boe.party.cpp index 16d71dad..912fcdb6 100644 --- a/src/boe.party.cpp +++ b/src/boe.party.cpp @@ -147,8 +147,8 @@ static void init_party_scen_data() { } univ.party.in_boat = -1; univ.party.in_horse = -1; - for(i = 0; i < 4; i++) - univ.party.creature_save[i].which_town = 200; + for(auto& pop : univ.party.creature_save) + pop.which_town = 200; for(i = 0; i < 10; i++) univ.party.out_c[i].exists = false; for(i = 0; i < 5; i++) @@ -167,6 +167,7 @@ static void init_party_scen_data() { univ.party.direction = DIR_N; univ.party.at_which_save_slot = 0; + univ.party.can_find_town.resize(univ.scenario.towns.size()); for(i = 0; i < univ.scenario.towns.size(); i++) univ.party.can_find_town[i] = !univ.scenario.towns[i]->is_hidden; for(i = 0; i < 20; i++) @@ -184,8 +185,8 @@ static void init_party_scen_data() { } } - for(i = 0; i < 200; i++) - univ.party.m_killed[i] = 0; + univ.party.m_killed.clear(); + univ.party.m_killed.resize(univ.scenario.towns.size()); for(i = 0; i < 200; i++) for(j = 0; j < 8; j++) @@ -233,8 +234,6 @@ static void init_party_scen_data() { // party record already contains scen name void put_party_in_scen(std::string scen_name) { short i,j; - std::array strs; - std::array buttons = {-1,-1,-1}; bool item_took = false; // Drop debug mode @@ -276,8 +275,8 @@ void put_party_in_scen(std::string scen_name) { if(item_took) cChoiceDlog("removed-special-items").show(); univ.party.age = 0; - for(i = 0; i < 200; i++) - univ.party.m_killed[i] = 0; + univ.party.m_killed.clear(); + univ.party.m_killed.resize(univ.scenario.towns.size()); univ.party.party_event_timers.clear(); fs::path path = locate_scenario(scen_name); @@ -314,12 +313,10 @@ void put_party_in_scen(std::string scen_name) { adjust_monst_menu(); // Throw up intro dialog - buttons[0] = 1; - for(j = 0; j < 6; j++) + for(j = 0; j < univ.scenario.intro_strs.size(); j++) if(!univ.scenario.intro_strs[j].empty()) { - for(i = 0; i < 6; i++) - strs[i] = univ.scenario.intro_strs[i]; - custom_choice_dialog(strs,univ.scenario.intro_mess_pic,PIC_SCEN,buttons) ; + std::array buttons = {0,-1,-1}; + custom_choice_dialog(univ.scenario.intro_strs, univ.scenario.intro_mess_pic, PIC_SCEN, buttons); j = 6; } short k; @@ -1618,12 +1615,12 @@ bool pc_can_cast_spell(short pc_num,eSkill type) { // If they can't cast the most basic level 1 spell, let's just make sure they can't cast any spells. // Find a spell they definitely know, and see if they can cast that. - if(type == eSkill::MAGE_SPELLS) { + if(type == eSkill::MAGE_SPELLS && univ.party[pc_num].mage_spells.any()) { for(int i = 0; i < 62; i++) if(univ.party[pc_num].mage_spells[i]) return pc_can_cast_spell(pc_num, eSpell(i)); } - if(type == eSkill::PRIEST_SPELLS) { + if(type == eSkill::PRIEST_SPELLS && univ.party[pc_num].priest_spells.any()) { for(int i = 0; i < 62; i++) if(univ.party[pc_num].priest_spells[i]) return pc_can_cast_spell(pc_num, eSpell(i + 100)); @@ -2273,7 +2270,7 @@ eAlchemy alch_choice(short pc_num) { std::string n = boost::lexical_cast(i + 1); chooseAlchemy["label" + n].setText(get_str("magic-names", i + 200)); chooseAlchemy["potion" + n].attachClickHandler(alch_choice_event_filter); - if(univ.party[pc_num].skill(eSkill::ALCHEMY) < difficulty[i] || univ.party.alchemy[i] == 0) + if(univ.party[pc_num].skill(eSkill::ALCHEMY) < difficulty[i] || !univ.party.alchemy[i]) chooseAlchemy["potion" + n].hide(); } std::ostringstream sout; @@ -2345,13 +2342,12 @@ mon_num_t pick_trapped_monst() { cChoiceDlog soulCrystal("soul-crystal",{"cancel","pick1","pick2","pick3","pick4"}); - for(i = 0; i < 4; i++) { + for(mon_num_t which : univ.party.imprisoned_monst) { std::string n = boost::lexical_cast(i + 1); - if(univ.party.imprisoned_monst[i] == 0) { + if(which == 0) { soulCrystal->getControl("pick" + n).hide(); } else { - mon_num_t which = univ.party.imprisoned_monst[i]; sp = get_m_name(which); soulCrystal->getControl("slot" + n).setText(sp); get_monst = which >= 10000 ? univ.party.summons[which - 10000] : univ.scenario.scen_monsters[which]; diff --git a/src/boe.specials.cpp b/src/boe.specials.cpp index b2f66d72..8d5ba0ad 100644 --- a/src/boe.specials.cpp +++ b/src/boe.specials.cpp @@ -3166,9 +3166,9 @@ void affect_spec(eSpecCtx which_mode,cSpecial cur_node,short cur_spec_type, if(pc_num < 100) break; if(spec.ex1a == 0) record_monst(dynamic_cast(pc), spec.ex1b); - else for(i = 0; i < 4; i++) { - if(univ.party.imprisoned_monst[i] == dynamic_cast(pc)->number) - univ.party.imprisoned_monst[i] = 0; + else for(mon_num_t& monst : univ.party.imprisoned_monst) { + if(monst == dynamic_cast(pc)->number) + monst = 0; } break; case eSpecType::AFFECT_PARTY_STATUS: diff --git a/src/boe.town.cpp b/src/boe.town.cpp index 260718bf..b6e360b5 100644 --- a/src/boe.town.cpp +++ b/src/boe.town.cpp @@ -144,9 +144,9 @@ void start_town_mode(short which_town, short entry_dir) { at_which_save_slot = univ.party.at_which_save_slot; - for(i = 0; i < 4; i++) - if(town_number == univ.party.creature_save[i].which_town) { - univ.town.monst = univ.party.creature_save[i]; + for(auto& pop : univ.party.creature_save) + if(town_number == pop.which_town) { + univ.town.monst = pop; monsters_loaded = true; for(j = 0; j < univ.town.monst.size(); j++) { @@ -536,9 +536,9 @@ location end_town_mode(short switching_level,location destination) { // returns } if(overall_mode == MODE_TOWN) { - for(i = 0; i < 4; i++) - if(univ.party.creature_save[i].which_town == univ.party.town_num) { - univ.party.creature_save[i] = univ.town.monst; + for(auto& pop : univ.party.creature_save) + if(pop.which_town == univ.party.town_num) { + pop = univ.town.monst; for(j = 0; j < univ.town->max_dim(); j++) for(k = 0; k < univ.town->max_dim(); k++) univ.party.setup[i][j][k] = (univ.town.fields[j][k] & 0xff00) >> 8; @@ -589,32 +589,32 @@ location end_town_mode(short switching_level,location destination) { // returns if(is_town()) { if(destination.x <= univ.town->in_town_rect.left) { - if(univ.town->exit_locs[1].x > 0) - to_return = local_to_global(univ.town->exit_locs[1]); + if(univ.town->exits[1].x > 0) + to_return = local_to_global(univ.town->exits[1]); else to_return.x--; univ.party.out_loc = to_return; univ.party.out_loc.x++; - handle_leave_town_specials(univ.party.town_num, univ.town->exit_specs[1],destination) ; + handle_leave_town_specials(univ.party.town_num, univ.town->exits[1].spec,destination) ; } else if(destination.x >= univ.town->in_town_rect.right) { - if(univ.town->exit_locs[3].x > 0) - to_return = local_to_global(univ.town->exit_locs[3]); + if(univ.town->exits[3].x > 0) + to_return = local_to_global(univ.town->exits[3]); else to_return.x++; univ.party.out_loc = to_return; univ.party.out_loc.x--; - handle_leave_town_specials(univ.party.town_num, univ.town->exit_specs[3],destination) ; + handle_leave_town_specials(univ.party.town_num, univ.town->exits[3].spec,destination) ; } else if(destination.y <= univ.town->in_town_rect.top) { - if(univ.town->exit_locs[0].x > 0) - to_return = local_to_global(univ.town->exit_locs[0]); + if(univ.town->exits[0].x > 0) + to_return = local_to_global(univ.town->exits[0]); else to_return.y--; univ.party.out_loc = to_return; univ.party.out_loc.y++; - handle_leave_town_specials(univ.party.town_num, univ.town->exit_specs[0],destination) ; + handle_leave_town_specials(univ.party.town_num, univ.town->exits[0].spec,destination) ; } else if(destination.y >= univ.town->in_town_rect.bottom) { - if(univ.town->exit_locs[2].x > 0) - to_return = local_to_global(univ.town->exit_locs[2]); + if(univ.town->exits[2].x > 0) + to_return = local_to_global(univ.town->exits[2]); else to_return.y++; univ.party.out_loc = to_return; univ.party.out_loc.y--; - handle_leave_town_specials(univ.party.town_num, univ.town->exit_specs[2],destination) ; + handle_leave_town_specials(univ.party.town_num, univ.town->exits[2].spec,destination) ; } } @@ -1273,6 +1273,8 @@ void erase_out_specials() { univ.scenario.ter_types[sector.terrain[sector.city_locs[k].x][sector.city_locs[k].y]].special == eTerSpec::TOWN_ENTRANCE && (sector.city_locs[k].x == minmax(0,47,sector.city_locs[k].x)) && (sector.city_locs[k].y == minmax(0,47,sector.city_locs[k].y))) { + if(sector.city_locs[k].spec < 0 || sector.city_locs[k].spec >= univ.scenario.towns.size()) + continue; if(!univ.party.can_find_town[sector.city_locs[k].spec]) { univ.out[48 * i + sector.city_locs[k].x][48 * j + sector.city_locs[k].y] = univ.scenario.ter_types[sector.terrain[sector.city_locs[k].x][sector.city_locs[k].y]].flag1; diff --git a/src/classes/monster.cpp b/src/classes/monster.cpp index 39b260bc..571f6172 100644 --- a/src/classes/monster.cpp +++ b/src/classes/monster.cpp @@ -140,9 +140,9 @@ void cMonster::append(legacy::monster_record_type& old){ int cMonster::addAttack(unsigned short dice, unsigned short sides, eMonstMelee type) { int which = 0; - while(which < 3 && a[which].dice > 0 && a[which].sides > 0) + while(which < a.size() && a[which].dice > 0 && a[which].sides > 0) which++; - if(which >= 3) return -1; + if(which >= a.size()) return -1; a[which].dice = dice; a[which].sides = sides; a[which].type = type; @@ -779,7 +779,7 @@ void cMonster::writeTo(std::ostream& file) const { file << "LEVEL " << int(level) << '\n'; file << "ARMOR " << int(armor) << '\n'; file << "SKILL " << int(skill) << '\n'; - for(int i = 0; i < 3; i++) + for(int i = 0; i < a.size(); i++) file << "ATTACK " << i + 1 << ' ' << a[i].dice << ' ' << a[i].sides << ' ' << a[i].type << '\n'; file << "HEALTH " << int(m_health) << '\n'; file << "SPEED " << int(speed) << '\n'; @@ -859,6 +859,7 @@ void cMonster::readFrom(std::istream& file) { } else if(cur == "ATTACK") { int which; line >> which; + if(which < 0 || which >= a.size()) continue; line >> a[which].dice >> a[which].sides >> a[which].type; which--; } else if(cur == "SIZE") { diff --git a/src/classes/monster.hpp b/src/classes/monster.hpp index 10b1e215..928d45f1 100644 --- a/src/classes/monster.hpp +++ b/src/classes/monster.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include "soundtool.hpp" @@ -99,7 +100,7 @@ public: short m_health; unsigned int armor; unsigned int skill; - cAttack a[3]; + std::array a; eRace m_type; unsigned int speed; unsigned int mu; diff --git a/src/classes/outdoors.cpp b/src/classes/outdoors.cpp index e54fdda9..7224e6b3 100644 --- a/src/classes/outdoors.cpp +++ b/src/classes/outdoors.cpp @@ -121,7 +121,7 @@ cOutdoors::cOutdoors(cScenario& scenario) : scenario(&scenario) { roads[i][j] = false; } - for(i = 0; i < 4; i++) { + for(i = 0; i < wandering_locs.size(); i++) { wandering_locs[i] = locs[i]; } out_name = "Area name"; diff --git a/src/classes/party.cpp b/src/classes/party.cpp index 4923dfde..37ca4026 100644 --- a/src/classes/party.cpp +++ b/src/classes/party.cpp @@ -136,6 +136,8 @@ void cParty::append(legacy::party_record_type& old, const cScenario& scen){ at_which_save_slot = old.at_which_save_slot; for(i = 0; i < 20 ; i++) alchemy[i] = old.alchemy[i]; + can_find_town.resize(200); + m_killed.resize(200); for(i = 0; i < 200; i++){ can_find_town[i] = old.can_find_town[i]; m_killed[i] = old.m_killed[i]; @@ -587,22 +589,22 @@ void cParty::writeTo(std::ostream& file) const { file << "SOULCRYSTAL " << i << ' ' << imprisoned_monst[i] << '\n'; file << "DIRECTION " << direction << '\n'; file << "WHICHSLOT " << at_which_save_slot << '\n'; - for(int i = 0; i < 4; i++) { + for(int i = 0; i < creature_save.size(); i++) { file << "TOWNSAVE " << i << ' ' << creature_save[i].which_town; if(creature_save[i].hostile) file << " HOSTILE"; file << '\n'; } - for(int i = 0; i < 20; i++) + for(int i = 0; i < alchemy.size(); i++) if(alchemy[i]) file << "ALCHEMY " << i << '\n'; - for(int i = 0; i < 200; i++) + for(int i = 0; i < can_find_town.size(); i++) if(can_find_town[i]) file << "TOWNVISIBLE " << i << '\n'; for(auto key : key_times) file << "EVENT " << key.first << ' ' << key.second << '\n'; for(int i : spec_items) file << "ITEM " << i << '\n'; - for(int i = 0; i < 200; i++) + for(int i = 0; i < m_killed.size(); i++) if(m_killed[i] > 0) file << "TOWNSLAUGHTER " << i << ' ' << m_killed[i] << '\n'; file << "KILLS " << total_m_killed << '\n'; @@ -678,7 +680,7 @@ void cParty::writeTo(std::ostream& file) const { file << "TIMER " << ' ' << party_event_timers[i].time << ' ' << party_event_timers[i].node_type << ' ' << party_event_timers[i].node << '\f'; file << '\f'; - for(int i = 0; i < 4; i++) + for(int i = 0; i < creature_save.size(); i++) for(int j = 0; j < creature_save[i].size(); j++) { if(creature_save[i][j].active > 0) { file << "CREATURE " << i << ' ' << j << '\n'; @@ -820,6 +822,7 @@ void cParty::readFrom(std::istream& file){ }else if(cur == "SOULCRYSTAL"){ int i; sin >> i; + if(i < 0 || i >= imprisoned_monst.size()) continue; sin >> imprisoned_monst[i]; }else if(cur == "DIRECTION") sin >> direction; @@ -828,16 +831,20 @@ void cParty::readFrom(std::istream& file){ else if(cur == "ALCHEMY"){ int i; sin >> i; - alchemy[i] = true; + if(i >= 0 && i < alchemy.size()) + alchemy[i] = true; } else if(cur == "TOWNSAVE") { int i; std::string str; sin >> i; + if(i < 0 || i >= creature_save.size()) continue; sin >> creature_save[i].which_town >> str; creature_save[i].hostile = str == "HOSTILE"; } else if(cur == "TOWNVISIBLE") { int i; sin >> i; + if(i >= can_find_town.size()) + can_find_town.resize(i + 1); can_find_town[i] = true; }else if(cur == "EVENT"){ int i; @@ -850,6 +857,8 @@ void cParty::readFrom(std::istream& file){ }else if(cur == "TOWNSLAUGHTER"){ int i; sin >> i; + if(i >= m_killed.size()) + m_killed.resize(i + 1); sin >> m_killed[i]; } else if(cur == "QUEST") { int i; @@ -929,6 +938,7 @@ void cParty::readFrom(std::istream& file){ } else if(cur == "CREATURE") { int i, j; bin >> i >> j; + if(i < 0 || i >= creature_save.size()) continue; creature_save[i].init(j); creature_save[i][j].readFrom(bin); } else if(cur == "STORED") { diff --git a/src/classes/party.hpp b/src/classes/party.hpp index 947bbbb8..0897d823 100644 --- a/src/classes/party.hpp +++ b/src/classes/party.hpp @@ -87,14 +87,14 @@ public: short town_num; std::array boats; std::array horses; - cPopulation creature_save[4]; + std::array creature_save; short in_boat; short in_horse; std::array out_c; std::map> magic_store_items; std::map> store_limited_stock; std::vector job_banks; - mon_num_t imprisoned_monst[4]; // Soul Crystal + std::array imprisoned_monst; // Soul Crystal std::set m_noted; // has the monster been scried? std::set m_seen; // has the monster ever been seen? (this used to have the above meaning) std::vector journal; @@ -109,12 +109,12 @@ public: size_t left_in; eDirection direction; short at_which_save_slot; - bool alchemy[20]; - bool can_find_town[200]; + std::bitset<20> alchemy; + std::vector can_find_town; std::map key_times; std::vector party_event_timers; std::set spec_items; - long m_killed[200]; // monsters killed per town + std::vector m_killed; long long total_m_killed, total_dam_done, total_xp_gained, total_dam_taken; std::string scen_name; private: diff --git a/src/classes/pc.cpp b/src/classes/pc.cpp index 95d4a7f7..45308427 100644 --- a/src/classes/pc.cpp +++ b/src/classes/pc.cpp @@ -850,8 +850,8 @@ cPlayer::cPlayer(cParty& party) : party(&party) { equip[i] = false; for(i = 0; i < 62; i++) { - priest_spells[i] = (i < 30) ? true : false; - mage_spells[i] = (i < 30) ? true : false; + priest_spells[i] = i < 30; + mage_spells[i] = i < 30; } which_graphic = 0; weap_poisoned = 24; @@ -908,10 +908,8 @@ cPlayer::cPlayer(cParty& party,long key,short slot) : cPlayer(party) { for(i = 0; i < 24; i++) equip[i] = false; - for(i = 0; i < 62; i++) { - priest_spells[i] = true; - mage_spells[i] = true; - } + priest_spells.set(); + mage_spells.set(); which_graphic = slot * 3 + 1; // 1, 4, 7, 10, 13, 16 if(slot == 2) which_graphic++; weap_poisoned = 24; // was 16, as an E2 relic @@ -1004,8 +1002,8 @@ cPlayer::cPlayer(cParty& party,long key,short slot) : cPlayer(party) { cur_sp = pc_sp[slot]; max_sp = pc_sp[slot]; for(i = 0; i < 62; i++) { - priest_spells[i] = (i < 30) ? true : false; - mage_spells[i] = (i < 30) ? true : false; + priest_spells[i] = i < 30; + mage_spells[i] = i < 30; } for(i = 0; i < 15; i++) { eTrait trait = eTrait(i); diff --git a/src/classes/pc.hpp b/src/classes/pc.hpp index 0f97d41d..15ed303b 100644 --- a/src/classes/pc.hpp +++ b/src/classes/pc.hpp @@ -51,8 +51,8 @@ public: short level; std::array items; std::array equip; - bool priest_spells[62]; - bool mage_spells[62]; + std::bitset<62> priest_spells; + std::bitset<62> mage_spells; pic_num_t which_graphic; short weap_poisoned; // HACK: This is only really marked mutable so that I can use operator[] from const methods diff --git a/src/classes/scenario.hpp b/src/classes/scenario.hpp index bbe926e4..3fe79907 100644 --- a/src/classes/scenario.hpp +++ b/src/classes/scenario.hpp @@ -91,7 +91,7 @@ public: std::string scen_name; std::string who_wrote[2]; std::string contact_info[2]; - std::string intro_strs[6]; + std::array intro_strs; std::vector journal_strs; std::vector spec_strs; std::vector snd_names; diff --git a/src/classes/talking.hpp b/src/classes/talking.hpp index 10a5b4d1..ae65cdc2 100644 --- a/src/classes/talking.hpp +++ b/src/classes/talking.hpp @@ -47,7 +47,7 @@ public: std::fill(link2, link2 + 4, ' '); } }; - cPersonality people[10]; + std::array people; std::vector talk_nodes; void append(legacy::talking_record_type& old, std::vector& shops); diff --git a/src/classes/town.cpp b/src/classes/town.cpp index 296b5d9b..2ea72eef 100644 --- a/src/classes/town.cpp +++ b/src/classes/town.cpp @@ -28,9 +28,9 @@ void cTown::append(legacy::town_record_type& old){ for(i = 0; i < 4; i++){ start_locs[i].x = old.start_locs[i].x; start_locs[i].y = old.start_locs[i].y; - exit_locs[i].x = old.exit_locs[i].x; - exit_locs[i].y = old.exit_locs[i].y; - exit_specs[i] = old.exit_specs[i]; + exits[i].x = old.exit_locs[i].x; + exits[i].y = old.exit_locs[i].y; + exits[i].spec = old.exit_specs[i]; wandering[i].append(old.wandering[i]); } preset_fields.clear(); @@ -88,9 +88,9 @@ cTown::cTown(cScenario& scenario) : scenario(&scenario) { lighting_type = LIGHT_NORMAL; for(i = 0; i < 4; i++) { start_locs[i].x = 100; - exit_specs[i] = -1; - exit_locs[i].x = -1; - exit_locs[i].y = -1; + exits[i].spec = -1; + exits[i].x = -1; + exits[i].y = -1; } max_num_monst = 30000; spec_on_entry = -1; @@ -109,12 +109,8 @@ cTown::cTown(cScenario& scenario) : scenario(&scenario) { comment[1] = "Comment 2"; comment[2] = "Comment 3"; - for(i = 0; i < 10; i++) { - talking.people[i].title = "Unused"; - talking.people[i].look = ""; - talking.people[i].name = ""; - talking.people[i].job = ""; - talking.people[i].dunno = ""; + for(auto& who : talking.people) { + who.title = "Unused"; } } diff --git a/src/classes/town.hpp b/src/classes/town.hpp index 9b725b1d..adc6e47e 100644 --- a/src/classes/town.hpp +++ b/src/classes/town.hpp @@ -66,13 +66,12 @@ public: short town_chop_time,town_chop_key; int bg_town, bg_fight; std::array wandering; - location wandering_locs[4]; + std::array wandering_locs; std::vector special_locs; std::vector sign_locs; eLighting lighting_type; - location start_locs[4]; - location exit_locs[4]; - short exit_specs[4]; + std::array start_locs; + std::array exits; rectangle in_town_rect; std::vector preset_items; std::vector creatures; diff --git a/src/classes/universe.cpp b/src/classes/universe.cpp index ec9d6458..c8fd5f34 100644 --- a/src/classes/universe.cpp +++ b/src/classes/universe.cpp @@ -1074,9 +1074,9 @@ void cUniverse::exportGraphics() { check_item(party.stored_items[i][j]); } } - for(int i = 0; i < 4; i++) { - if(party.imprisoned_monst[i] != 0) - check_monst(scenario.scen_monsters[party.imprisoned_monst[i]]); + for(mon_num_t monst : party.imprisoned_monst) { + if(monst != 0) + check_monst(scenario.scen_monsters[monst]); } // And then, just add all the graphics, and update references to them for(auto pic : update_pcs) { @@ -1147,11 +1147,11 @@ void cUniverse::exportSummons() { } } } - for(int i = 0; i < 4; i++) { - if(party.imprisoned_monst[i] == 0) continue; - if(party.imprisoned_monst[i] >= 10000) - used_monsters.insert(party.imprisoned_monst[i] - 10000); - else need_monsters.insert(party.imprisoned_monst[i]); + for(mon_num_t monst : party.imprisoned_monst) { + if(monst == 0) continue; + if(monst >= 10000) + used_monsters.insert(monst - 10000); + else need_monsters.insert(monst); } std::stack last_check; for(mon_num_t m : need_monsters) last_check.push(m); @@ -1185,9 +1185,9 @@ void cUniverse::exportSummons() { else party.summons.push_back(scenario.scen_monsters[monst]); for(auto& item : update_items[monst]) item->abil_data[1] = 10000 + dest; - for(int i = 0; i < 4; i++) - if(party.imprisoned_monst[i] == monst) - party.imprisoned_monst[i] = dest + 10000; + for(mon_num_t& sc : party.imprisoned_monst) + if(sc == monst) + sc = dest + 10000; } } diff --git a/src/pcedit/pc.main.cpp b/src/pcedit/pc.main.cpp index 404efd60..5a062538 100644 --- a/src/pcedit/pc.main.cpp +++ b/src/pcedit/pc.main.cpp @@ -239,8 +239,8 @@ void handle_menu_choice(eMenu item_hit) { case eMenu::RESET_TOWNS: display_strings(20,7); - for(i = 0; i < 4; i++) - univ.party.creature_save[i].which_town = 200; + for(auto& pop : univ.party.creature_save) + pop.which_town = 200; break; case eMenu::HEAL_DAMAGE: display_strings(1,15); diff --git a/src/scenedit/scen.fileio.cpp b/src/scenedit/scen.fileio.cpp index ad24874c..962832e9 100644 --- a/src/scenedit/scen.fileio.cpp +++ b/src/scenedit/scen.fileio.cpp @@ -138,7 +138,7 @@ void writeScenarioToXml(ticpp::Printer&& data, cScenario& scenario) { data.PushElement("icon", scenario.intro_mess_pic); { int last = -1; - for(int i = 0; i < 6; i++) { + for(int i = 0; i < scenario.intro_strs.size(); i++) { if(!scenario.intro_strs[i].empty()) last = i; } @@ -679,20 +679,20 @@ void writeTownToXml(ticpp::Printer&& data, cTown& town) { data.PushText(town.spec_on_entry_if_dead); data.CloseElement("onenter"); } - for(int i = 0; i < 4; i++) { - if(town.exit_locs[i].x >= 0 && town.exit_locs[i].y >= 0) { + for(int i = 0; i < town.exits.size(); i++) { + if(town.exits[i].x >= 0 && town.exits[i].y >= 0) { data.OpenElement("exit"); data.PushAttribute("dir", directions[i]); - data.PushAttribute("x", town.exit_locs[i].x); - data.PushAttribute("y", town.exit_locs[i].y); + data.PushAttribute("x", town.exits[i].x); + data.PushAttribute("y", town.exits[i].y); data.CloseElement("exit"); } } for(int i = 0; i < 4; i++) { - if(town.exit_specs[i] >= 0) { + if(town.exits[i].spec >= 0) { data.OpenElement("onexit"); data.PushAttribute("dir", directions[i]); - data.PushText(town.exit_specs[i]); + data.PushText(town.exits[i].spec); data.CloseElement("onexit"); } } @@ -869,7 +869,7 @@ map_data buildOutMapData(location which, cScenario& scenario) { if(!sector.sign_locs[i].text.empty()) terrain.addFeature(sector.sign_locs[i].x, sector.sign_locs[i].y, eMapFeature::SIGN, i); } - for(size_t i = 0; i < 4; i++) { + for(size_t i = 0; i < sector.wandering_locs.size(); i++) { terrain.addFeature(sector.wandering_locs[i].x, sector.wandering_locs[i].y, eMapFeature::WANDERING, i); } for(size_t i = 0; i < scenario.boats.size(); i++) { @@ -905,7 +905,7 @@ map_data buildTownMapData(size_t which, cScenario& scenario) { if(!town.sign_locs[i].text.empty()) terrain.addFeature(town.sign_locs[i].x, town.sign_locs[i].y, eMapFeature::SIGN, i); } - for(size_t i = 0; i < 4; i++) { + for(size_t i = 0; i < town.wandering_locs.size(); i++) { terrain.addFeature(town.wandering_locs[i].x, town.wandering_locs[i].y, eMapFeature::WANDERING, i); } for(size_t i = 0; i < town.preset_items.size(); i++) { @@ -1163,7 +1163,7 @@ void scen_text_dump(){ fout << "Who Wrote 1: " << scenario.who_wrote[0] << endl; fout << "Who Wrote 2: " << scenario.who_wrote[1] << endl; fout << "Contact Info: " << scenario.contact_info[0] << " " << scenario.contact_info[1] << endl; - for(short i = 0; i < 6; i++) + for(short i = 0; i < scenario.intro_strs.size(); i++) if(scenario.intro_strs[i][0] != '*') fout << " Intro Message " << i << ": " << scenario.intro_strs[i] << endl; for(short i = 0; i < scenario.journal_strs.size(); i++) diff --git a/src/scenedit/scen.graphics.cpp b/src/scenedit/scen.graphics.cpp index 8341d193..b6a9e3c5 100644 --- a/src/scenedit/scen.graphics.cpp +++ b/src/scenedit/scen.graphics.cpp @@ -287,11 +287,11 @@ static std::vector get_small_icons(location at, ter_num_t t_to_draw) { if(editing_town) { if(scenario.ter_types[t_to_draw].light_radius > 0) icons.push_back(83); - for(size_t i = 0; i < 4; i++) + for(size_t i = 0; i < town->start_locs.size(); i++) if(at == town->start_locs[i]) { icons.push_back(70 + i); } - for(size_t i = 0; i < 4; i++) + for(size_t i = 0; i < town->wandering_locs.size(); i++) if(at == town->wandering_locs[i]) { icons.push_back(86); } @@ -302,7 +302,7 @@ static std::vector get_small_icons(location at, ter_num_t t_to_draw) { icons.push_back(34); } } else { - for(size_t i = 0; i < 4; i++) + for(size_t i = 0; i < current_terrain->wandering_locs.size(); i++) if(at == current_terrain->wandering_locs[i]) { icons.push_back(86); } @@ -1244,13 +1244,13 @@ void draw_frames() { draw_rect.left = 23 + q * 28; draw_rect.right = 50 + q * 28; draw_rect.offset(TER_RECT_UL_X,TER_RECT_UL_Y); - for(i = 0; i < 4; i++) + for(i = 0; i < town->wandering_locs.size(); i++) if((which_pt.x == town->wandering_locs[i].x) && (which_pt.y == town->wandering_locs[i].y)) { frame_rect(mainPtr, draw_rect, sf::Color::Red); } - for(i = 0; i < 4; i++) + for(i = 0; i < town->start_locs.size(); i++) if((which_pt.x == town->start_locs[i].x) && (which_pt.y == town->start_locs[i].y)) { frame_rect(mainPtr, draw_rect, sf::Color::Magenta); diff --git a/src/scenedit/scen.keydlgs.cpp b/src/scenedit/scen.keydlgs.cpp index 3e441c8e..5eb22320 100644 --- a/src/scenedit/scen.keydlgs.cpp +++ b/src/scenedit/scen.keydlgs.cpp @@ -1172,7 +1172,7 @@ static bool edit_scen_intro_event_filter(cDialog& me, std::string item_hit, eKey showError("Intro picture number is out of range.","",&me); return true; } - for(i = 0; i < 6; i++) { + for(i = 0; i < scenario.intro_strs.size(); i++) { std::string id = "str" + std::to_string(i + 1); scenario.intro_strs[i] = me[id].getText(); } @@ -1197,7 +1197,7 @@ void edit_scen_intro() { edit.attachClickHandlers(edit_scen_intro_event_filter, {"okay", "cancel", "choose"}); edit["picnum"].setTextToNum(scenario.intro_pic); - for(i = 0; i < 6; i++) { + for(i = 0; i < scenario.intro_strs.size(); i++) { std::string id = "str" + std::to_string(i + 1); edit[id].setText(scenario.intro_strs[i]); } diff --git a/src/scenedit/scen.townout.cpp b/src/scenedit/scen.townout.cpp index 7760faa0..9d6a291d 100644 --- a/src/scenedit/scen.townout.cpp +++ b/src/scenedit/scen.townout.cpp @@ -729,11 +729,11 @@ void edit_town_events() { static bool save_advanced_town(cDialog& me, std::string, eKeyMod) { if(!me.toast(true)) return true; - for(int i = 0; i < 4; i++) { + for(int i = 0; i < town->exits.size(); i++) { std::string id = std::to_string(i + 1); - town->exit_specs[i] = me["onexit" + id].getTextAsNum(); - town->exit_locs[i].x = me["exit" + id + "-x"].getTextAsNum(); - town->exit_locs[i].y = me["exit" + id + "-y"].getTextAsNum(); + town->exits[i].spec = me["onexit" + id].getTextAsNum(); + town->exits[i].x = me["exit" + id + "-x"].getTextAsNum(); + town->exits[i].y = me["exit" + id + "-y"].getTextAsNum(); } town->spec_on_entry = me["onenter"].getTextAsNum(); town->spec_on_entry_if_dead = me["onenterdead"].getTextAsNum(); @@ -750,11 +750,11 @@ static bool save_advanced_town(cDialog& me, std::string, eKeyMod) { static void put_advanced_town_in_dlog(cDialog& me) { short i; - for(i = 0; i < 4; i++) { + for(i = 0; i < town->exits.size(); i++) { std::string id = std::to_string(i + 1); - me["onexit" + id].setTextToNum(town->exit_specs[i]); - me["exit" + id + "-x"].setTextToNum(town->exit_locs[i].x); - me["exit" + id + "-y"].setTextToNum(town->exit_locs[i].y); + me["onexit" + id].setTextToNum(town->exits[i].spec); + me["exit" + id + "-x"].setTextToNum(town->exits[i].x); + me["exit" + id + "-y"].setTextToNum(town->exits[i].y); } me["onenter"].setTextToNum(town->spec_on_entry); me["onenterdead"].setTextToNum(town->spec_on_entry_if_dead); diff --git a/src/tools/fileio_scen.cpp b/src/tools/fileio_scen.cpp index 10843b0e..93d8fefe 100644 --- a/src/tools/fileio_scen.cpp +++ b/src/tools/fileio_scen.cpp @@ -699,7 +699,7 @@ void readScenarioFromXml(ticpp::Document&& data, cScenario& scenario) { } else if(type == "icon") { info->GetText(&scenario.intro_mess_pic); } else if(type == "intro-msg") { - if(found_intro >= 6) + if(found_intro >= scenario.intro_strs.size()) throw xBadNode(type,info->Row(),info->Column(),fname); info->GetText(&scenario.intro_strs[found_intro], false); found_intro++; @@ -1385,7 +1385,7 @@ void readMonstersFromXml(ticpp::Document&& data, cScenario& scenario) { Iterator atk; for(atk = atk.begin(monst.Get()); atk != atk.end(); atk++) { atk->GetValue(&type); - if(num_attacks >= 3 || type != "attack") + if(num_attacks >= the_mon.a.size() || type != "attack") throw xBadNode(type, atk->Row(), atk->Column(), fname); cMonster::cAttack& the_atk = the_mon.a[num_attacks]; atk->GetText(&val); @@ -1629,13 +1629,13 @@ void readTownFromXml(ticpp::Document&& data, cTown*& town, cScenario& scen) { size_t which = dirs.find_first_of(val); if(which == std::string::npos) throw xBadVal(type, "dir", val, elem->Row(), elem->Column(), fname); - town->exit_locs[which] = loc; + town->exits[which] = loc; } else if(type == "onexit") { elem->GetAttribute("dir", &val); size_t which = dirs.find_first_of(val); if(which == std::string::npos) throw xBadVal(type, "dir", val, elem->Row(), elem->Column(), fname); - elem->GetText(&town->exit_specs[which]); + elem->GetText(&town->exits[which].spec); } else if(type == "onoffend") { elem->GetText(&town->spec_on_hostile); } else if(type == "timer") {