[net.games.hack] Sorting one's inventory

andrew@orca.UUCP (Andrew Klossner) (06/08/85)

I like my inventory (the response to the 'i' command) to be sorted, so
that all weapons appear together, all armor appears together, etc.
This mod does that.  All weapons appear first, followed by all armor,
followed by food and corpses, then gems, then scrolls, then potions,
then wands, then rings, then apply-able items.  This order can be
revised by changing the string SORTOBJS.

This code contains many more comments than the original.  I find that I
can't hack a hack routine unless I first comment it so I can see what
I'm doing.

  -=- Andrew Klossner   (decvax!tektronix!orca!andrew)       [UUCP]
                        (orca!andrew.tektronix@csnet-relay)  [ARPA]

========================================================================
/* In hack.invent.c, replace the addinv() procedure with the following: */

/* Collating sequence for sorting objects in an inventory.
   Just rearrange the characters in this string to change the sequence. */
static char SORTOBJS[] = "$)[%*?!/=(`";

static int classLess(newLet, establishedLet)

/* Return true if the new object letter is less than the established
   object letter, using a non-ASCII collating sequence. */

{
	/* I'm told that, under system V, "index" is spelled "strchr". */
	return index(SORTOBJS,newLet)<index(SORTOBJS,establishedLet);
	}

struct obj *
addinv(obj) register struct obj *obj; {
	register struct obj *otmp;

	/* Loop through the inventory.  For each item, if we can merge
	   this with it, do so, otherwise append the item to the end
	   of the list of items of its class. */
	for(otmp = (struct obj *)&invent; otmp; otmp = otmp->nobj) {

		/* Try to merge.  If it works, just return. */
		if(merged(otmp, obj, 0)) return(otmp);

		/* Couldn't merge.
		   See if we've come to the end of this class of items;
		   if so, link this item in here and return. */
		if(!otmp->nobj || classLess(obj->olet, otmp->nobj->olet)) {
			obj->nobj = otmp->nobj;
			otmp->nobj = obj;
			return(obj);
		}
	}

	/* Here if there were no items in the list to start with.
	   This item becomes the first item in the list. */
	invent = obj;
	obj->nobj = 0;
	return(obj);
}