Skip to content

Commit

Permalink
fix: infinite loop if monster attribute was missing during monster sp…
Browse files Browse the repository at this point in the history
…awning
  • Loading branch information
lollek committed Feb 2, 2025
1 parent 3a72633 commit cb60ceb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ January 2025
* Changes to how rivers are created, since they caused infinite loops and segfaults. Still, unsure if the code is correct.
* Capitalize light sources in equipment list
* Make throwing more viable by making thrown objects fall to the ground even when hitting.
* Fix infinite loop if we wanted a monster attribute which was not applied to any monster at that level

## 5-alpha521

Expand Down
11 changes: 3 additions & 8 deletions src/generate_monster/monster.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void place_monster(const long y, const long x, const long template,
*/
static bool summon_monster(int64_t *y, int64_t *x, const bool is_asleep,
obj_set const fval_set,
monster_attribute const **monster_attributes) {
monster_attribute const *const *monster_attributes) {
long const max_monster_level =
MIN(dun_level + MON_SUMMON_ADJ, MAX_MONS_LEVEL);

Expand Down Expand Up @@ -175,7 +175,6 @@ static bool summon_monster(int64_t *y, int64_t *x, const bool is_asleep,
}

for (int counter = 0; counter < 11; counter++) {
continue_spawn_loop:;
// Town level has separate creatures from the rest of the levels
long monster_i = dun_level == 0
? randint(m_level[0])
Expand All @@ -190,12 +189,8 @@ static bool summon_monster(int64_t *y, int64_t *x, const bool is_asleep,
continue;
}

for (monster_attribute const **attribute = monster_attributes;
*attribute != NULL; (*attribute)++) {
if (!monster_template_has_attribute(template, **attribute)) {
counter++;
goto continue_spawn_loop; // continue the outer loop
}
if (!monster_template_has_attributes(template, monster_attributes)) {
continue;
}

place_monster(monster_y, monster_x, monster_i, is_asleep);
Expand Down
14 changes: 14 additions & 0 deletions src/generate_monster/monster_template.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "monster_template.h"

#include <stddef.h>

long const m_level[MAX_MONS_LEVEL + 1] = {
18, 14, 33, 51, 63, 77, 87, 97, 104, 114, 129, 135, 152, 162, 170,
186, 196, 201, 208, 215, 229, 234, 239, 247, 252, 262, 269, 274, 282, 288,
Expand Down Expand Up @@ -937,3 +939,15 @@ bool monster_template_has_attribute(monster_template const *template,
}
return false;
}

bool monster_template_has_attributes(
monster_template const *template,
monster_attribute const *const *monster_attributes) {
for (monster_attribute const *attribute = *monster_attributes;
attribute != NULL; attribute++) {
if (!monster_template_has_attribute(template, *attribute)) {
return false;
}
}
return true;
}
3 changes: 3 additions & 0 deletions src/generate_monster/monster_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ extern long const m_level[MAX_MONS_LEVEL + 1];

bool monster_template_has_attribute(monster_template const *template,
monster_attribute attribute);
bool monster_template_has_attributes(
monster_template const *template,
monster_attribute const *const *monster_attributes);

0 comments on commit cb60ceb

Please sign in to comment.