Skip to content

Commit

Permalink
lev comp: Simplify mon gen save/restore code
Browse files Browse the repository at this point in the history
Also, the monster generation marker is now 8 bits instead of 32.

This goes with commit a6d9021 (unhardcode quest mon gen).
  • Loading branch information
tung committed Jun 7, 2012
1 parent afd84d2 commit 1501d6f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
30 changes: 13 additions & 17 deletions libnitrohack/src/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,35 +159,31 @@ static void restdamage(struct memfile *mf, struct level *lev, boolean ghostly)

static struct mon_gen_override *rest_mongen_override(struct memfile *mf)
{
int marker;
struct mon_gen_override *or;
struct mon_gen_tuple *mt;
boolean has_gen_chances, next;

or = NULL;

mfmagic_check(mf, MONGEN_MAGIC);

marker = mread32(mf);
if (marker) {
/* Check for mon gen marker. */
if (mread8(mf)) {
or = malloc(sizeof(*or));
or->override_chance = mread32(mf);
or->total_mon_freq = mread32(mf);
has_gen_chances = mread8(mf) ? TRUE : FALSE;
or->gen_chances = NULL;

if (has_gen_chances) {
do {
mfmagic_check(mf, MONGENTUPLE_MAGIC);
mt = malloc(sizeof(*mt));
mt->freq = mread32(mf);
mt->is_sym = mread8(mf) ? TRUE : FALSE;
mt->monid = mread32(mf);
next = mread8(mf) ? TRUE : FALSE;

mt->next = or->gen_chances;
or->gen_chances = mt;
} while (next);
/* Check for next tuple marker. */
while (mread8(mf)) {
mfmagic_check(mf, MONGENTUPLE_MAGIC);

mt = malloc(sizeof(*mt));
mt->freq = mread32(mf);
mt->is_sym = mread8(mf) ? TRUE : FALSE;
mt->monid = mread32(mf);

mt->next = or->gen_chances;
or->gen_chances = mt;
}
}

Expand Down
22 changes: 12 additions & 10 deletions libnitrohack/src/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,25 +449,27 @@ static void save_mongen_override(struct memfile *mf, struct mon_gen_override *or
mfmagic_set(mf, MONGEN_MAGIC);

if (!or) {
/* marker == 0 means no monster generation override */
mwrite32(mf, 0);
/* mon gen marker == 0: override doesn't exist */
mwrite8(mf, 0);
} else {
/* marker == 1 means monster generation override is present */
mwrite32(mf, 1);
/* mon gen marker == 1: override exists */
mwrite8(mf, 1);
mwrite32(mf, or->override_chance);
mwrite32(mf, or->total_mon_freq);
mwrite8(mf, or->gen_chances ? 1 : 0);

mt = or->gen_chances;
while (mt) {
for (mt = or->gen_chances; mt; mt = mt->next) {
/* Write tuple marker. */
mwrite8(mf, 1);

mfmagic_set(mf, MONGENTUPLE_MAGIC);

mwrite32(mf, mt->freq);
mwrite8(mf, mt->is_sym ? 1 : 0);
mwrite32(mf, mt->monid);
mwrite8(mf, mt->next ? 1 : 0);

mt = mt->next;
}

/* End-of-tuples marker. */
mwrite8(mf, 0);
}
}

Expand Down

0 comments on commit 1501d6f

Please sign in to comment.