fixed segmentation fault when sorting enemies
This commit is contained in:
@ -36,9 +36,9 @@ void character::apply_effect(potion *effect) {
|
|||||||
void character::insert_effect(potion *effect) {
|
void character::insert_effect(potion *effect) {
|
||||||
effects.push_back(effect);
|
effects.push_back(effect);
|
||||||
|
|
||||||
for (int i = effects.size() - 1; i > 0; --i)
|
for (int i = effects.size() - 1; i > 0 &&
|
||||||
if (effect->get_priority() < effects[i - 1]->get_priority())
|
effect->get_priority() < effects[i - 1]->get_priority(); --i)
|
||||||
std::swap(effects[i], effects[i - 1]);
|
std::swap(effects[i], effects[i - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
result character::calc_effects() {
|
result character::calc_effects() {
|
||||||
|
@ -33,7 +33,7 @@ long_result halfling::get_hit(character *ch, const int tATK,
|
|||||||
" is slain by PC. "};
|
" is slain by PC. "};
|
||||||
|
|
||||||
return {result::hit, "PC deals " +
|
return {result::hit, "PC deals " +
|
||||||
std::to_string(tmp) + " damage to " + abbrev};
|
std::to_string(tmp) + " damage to " + abbrev + ". "};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {miss, "PC tried to hit " + abbrev +
|
return {miss, "PC tried to hit " + abbrev +
|
||||||
|
@ -77,7 +77,7 @@ long_result enemy_base::get_hit(character *ch, const int tATK,
|
|||||||
" is slain by PC. "};
|
" is slain by PC. "};
|
||||||
|
|
||||||
return {result::hit, "PC deals " +
|
return {result::hit, "PC deals " +
|
||||||
std::to_string(tmp) + " damage to " + abbrev};
|
std::to_string(tmp) + " damage to " + abbrev + ". "};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {miss, "PC tried to hit " + abbrev + " but missed. "};
|
return {miss, "PC tried to hit " + abbrev + " but missed. "};
|
||||||
|
19
src/game.cc
19
src/game.cc
@ -41,17 +41,26 @@ void game::new_level() {
|
|||||||
rng, enabled_features));
|
rng, enabled_features));
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <algorithm>
|
enemy_list game::sort_enemies(const enemy_list &elist) {
|
||||||
bool compare_characters(const character *a, const character *b) {
|
enemy_list result;
|
||||||
return a->get_pos() < b->get_pos();
|
result.reserve(elist.size());
|
||||||
|
|
||||||
|
for (auto enemy : elist) {
|
||||||
|
result.push_back(enemy);
|
||||||
|
|
||||||
|
for (int i = result.size() - 1; i > 0 &&
|
||||||
|
enemy->get_pos() < result[i - 1]->get_pos(); --i)
|
||||||
|
std::swap(result[i], result[i - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
character *game::move_enemies() {
|
character *game::move_enemies() {
|
||||||
if (the_world)
|
if (the_world)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto enemies = levels[curr_level]->get_elist();
|
auto enemies = sort_enemies(levels[curr_level]->get_elist());
|
||||||
std::sort(enemies.begin(), enemies.end(), ::compare_characters);
|
|
||||||
|
|
||||||
for (auto ch : enemies) {
|
for (auto ch : enemies) {
|
||||||
bool hostile = ch->get_race() == rmerchant ?
|
bool hostile = ch->get_race() == rmerchant ?
|
||||||
|
@ -52,6 +52,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void new_level();
|
void new_level();
|
||||||
character *move_enemies();
|
character *move_enemies();
|
||||||
|
enemy_list sort_enemies(const enemy_list &elist);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,7 +31,7 @@ game_command console_input::get_command() {
|
|||||||
|
|
||||||
return (game_command)((tmp = get_direction(cmd)) ==
|
return (game_command)((tmp = get_direction(cmd)) ==
|
||||||
game_command_panic
|
game_command_panic
|
||||||
? tmp : tmp - move_north +
|
? tmp : tmp +
|
||||||
(use ? apply_north : attack_north));
|
(use ? apply_north : attack_north));
|
||||||
} else if (cmd == "yes") {
|
} else if (cmd == "yes") {
|
||||||
return game_command::enter;
|
return game_command::enter;
|
||||||
|
Reference in New Issue
Block a user