MCPcopy Create free account
hub / github.com/NetHack/NetHack / reorder_fruit

Function reorder_fruit

src/objnam.c:522–554  ·  view source on GitHub ↗

sort the named-fruit linked list by fruit index number */

Source from the content-addressed store, hash-verified

520
521/* sort the named-fruit linked list by fruit index number */
522void
523reorder_fruit(boolean forward)
524{
525 struct fruit *f, *allfr[1 + 127];
526 int i, j, k = SIZE(allfr);
527
528 for (i = 0; i < k; ++i)
529 allfr[i] = (struct fruit *) 0;
530 for (f = gf.ffruit; f; f = f->nextf) {
531 /* without sanity checking, this would reduce to 'allfr[f->fid]=f' */
532 j = f->fid;
533 if (j < 1 || j >= k) {
534 impossible("reorder_fruit: fruit index (%d) out of range", j);
535 return; /* don't sort after all; should never happen... */
536 } else if (allfr[j]) {
537 impossible("reorder_fruit: duplicate fruit index (%d)", j);
538 return;
539 }
540 allfr[j] = f;
541 }
542 gf.ffruit = 0; /* reset linked list; we're rebuilding it from scratch */
543 /* slot [0] will always be empty; must start 'i' at 1 to avoid
544 [k - i] being out of bounds during first iteration */
545 for (i = 1; i < k; ++i) {
546 /* for forward ordering, go through indices from high to low;
547 for backward ordering, go from low to high */
548 j = forward ? (k - i) : i;
549 if (allfr[j]) {
550 allfr[j]->nextf = gf.ffruit;
551 gf.ffruit = allfr[j];
552 }
553 }
554}
555
556/* add "<pfx> called <sfx>" to end of buf, truncating if necessary */
557staticfn void

Callers 1

attributes_enlightenmentFunction · 0.85

Calls 1

impossibleFunction · 0.70

Tested by

no test coverage detected