objects[] infrastructure

The bases[] array allows finding the index of the first object in
a particular class.  Extend it so that bases[class + 1] - 1 is a
reliable way to find the last object in any class.  The array had
to be extended by one so that the last class has a [class+1] entry
available, and object initialization now makes sure that classes
within objects[] are in ascending order so that [class+1] always
holds a higher index than [class].
This commit is contained in:
PatR
2020-04-19 04:18:22 -07:00
parent 8c744de8cd
commit 37ef5a2561
3 changed files with 42 additions and 15 deletions

View File

@@ -2897,13 +2897,19 @@ int xtra_prob; /* to force 0% random generation items to also be considered */
int i, n = 0;
short validobjs[NUM_OBJECTS];
register const char *zn;
int prob, maxprob = 0;
int lo, hi, prob, maxprob = 0;
if (!name || !*name)
return STRANGE_OBJECT;
memset((genericptr_t) validobjs, 0, sizeof validobjs);
(void) memset((genericptr_t) validobjs, 0, sizeof validobjs);
if (oclass) {
lo = g.bases[(uchar) oclass];
hi = g.bases[(uchar) oclass + 1] - 1;
} else {
lo = STRANGE_OBJECT + 1;
hi = NUM_OBJECTS - 1;
}
/* FIXME:
* When this spans classes (the !oclass case), the item
* probabilities are not very useful because they don't take
@@ -2912,9 +2918,7 @@ int xtra_prob; /* to force 0% random generation items to also be considered */
* "blank" would have 10/11 chance to yield a blook even though
* scrolls are supposed to be much more common than books.]
*/
for (i = oclass ? g.bases[(int) oclass] : STRANGE_OBJECT + 1;
i < NUM_OBJECTS && (!oclass || objects[i].oc_class == oclass);
++i) {
for (i = lo; i <= hi; ++i) {
/* don't match extra descriptions (w/o real name) */
if ((zn = OBJ_NAME(objects[i])) == 0)
continue;
@@ -3840,9 +3844,12 @@ struct obj *no_wish;
}
if (((typ = rnd_otyp_by_namedesc(actualn, oclass, 1)) != STRANGE_OBJECT)
|| ((typ = rnd_otyp_by_namedesc(dn, oclass, 1)) != STRANGE_OBJECT)
|| (dn != actualn
&& (typ = rnd_otyp_by_namedesc(dn, oclass, 1)) != STRANGE_OBJECT)
|| ((typ = rnd_otyp_by_namedesc(un, oclass, 1)) != STRANGE_OBJECT)
|| ((typ = rnd_otyp_by_namedesc(origbp, oclass, 1)) != STRANGE_OBJECT))
|| (origbp != actualn
&& ((typ = rnd_otyp_by_namedesc(origbp, oclass, 1))
!= STRANGE_OBJECT)))
goto typfnd;
typ = 0;