cmp_init_mongen_order() qsort comparison function tweaks

The array was ending up ordered the same on different qsort
implementations.

This incorporates the mlet value into the sort value for comparison.
That guarantees that everything stays ordered by mlet, followed by
difficulty (and would even if something ever got misplaced in
monsters.h). It means the "they are equal" zero return for differing
mlet values is not required, and has been removed.

This removes the "they are equal" zero returns for G_NOGEN | G_UNIQ
monsters, so they will still get sorted rather than left at the
whatever array element they happened to be at (which I don't think
should be an issue?).
This commit is contained in:
nhmall
2025-02-14 19:30:34 -05:00
parent f1be2eaffa
commit 8672807a5e

View File

@@ -1752,16 +1752,21 @@ static boolean mongen_order_init = FALSE;
staticfn int QSORTCALLBACK
cmp_init_mongen_order(const void *p1, const void *p2)
{
const int *pi1 = p1;
const int *pi2 = p2;
int i1 = *pi1, i2 = *pi2;
int i1 = *((int *) p1), i2 = *((int *) p2);
#if 0
/* This will cause these to be moved last in the mlet sort order */
int offset1 = ((mons[i1].geno & (G_NOGEN | G_UNIQ)) != 0) ? 99 : 0,
offset2 = ((mons[i2].geno & (G_NOGEN | G_UNIQ)) != 0) ? 99 : 0;
#else
int offset1 = 0, offset2 = 0;
#endif
if (((mons[i1].geno & (G_NOGEN|G_UNIQ)) != 0)
|| ((mons[i2].geno & (G_NOGEN|G_UNIQ)) != 0))
return 0;
if (mons[i1].mlet != mons[i2].mlet)
return 0;
return (int)(mons[i1].difficulty - mons[i2].difficulty);
/* incorporate the mlet into the sort values for comparison */
int difficulty1 =
((int) mons[i1].difficulty + offset1 | ((int) mons[i1].mlet << 8)),
difficulty2 =
((int) mons[i2].difficulty + offset2 | ((int) mons[i2].mlet << 8));
return difficulty1 - difficulty2;
}
/* check that monsters are in correct difficulty order for mkclass() */
@@ -1797,13 +1802,13 @@ init_mongen_order(void)
return;
mongen_order_init = TRUE;
for (i = LOW_PM; i <= SPECIAL_PM; i++)
for (i = LOW_PM; i <= NUMMONS; i++)
mongen_order[i] = i;
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
check_mongen_order();
#endif
qsort((genericptr_t) mongen_order, NUMMONS, sizeof(int), cmp_init_mongen_order);
qsort((genericptr_t) mongen_order, SPECIAL_PM, sizeof(int), cmp_init_mongen_order);
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
check_mongen_order();
#endif