1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.invent.c - version 1.0.3 */
7 extern struct obj *splitobj();
8 extern struct obj zeroobj;
10 extern char quitchars[];
11 static char *xprname();
15 extern struct wseg *wsegs[32];
20 static int lastinvnr = 51; /* 0 ... 51 */
29 for(i = 0; i < 52; i++) inuse[i] = FALSE;
30 for(obj = invent; obj; obj = obj->nobj) if(obj != otmp) {
32 if('a' <= i && i <= 'z') inuse[i - 'a'] = TRUE; else
33 if('A' <= i && i <= 'Z') inuse[i - 'A' + 26] = TRUE;
34 if(i == otmp->invlet) otmp->invlet = 0;
36 if((i = otmp->invlet) &&
37 (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
39 for(i = lastinvnr+1; i != lastinvnr; i++) {
40 if(i == 52) { i = -1; continue; }
43 otmp->invlet = (inuse[i] ? NOINVSYM :
44 (i < 26) ? ('a'+i) : ('A'+i-26));
54 /* merge or attach to end of chain */
59 for(otmp = invent; /* otmp */; otmp = otmp->nobj) {
60 if(merged(otmp, obj, 0))
69 if(flags.invlet_constant) {
72 * The ordering of the chain is nowhere significant
73 * so in case you prefer some other order than the
74 * historical one, change the code below.
76 if(otmp) { /* find proper place in chain */
78 if((invent->invlet ^ 040) > (obj->invlet ^ 040)) {
82 for(otmp = invent; ; otmp = otmp->nobj) {
84 (otmp->nobj->invlet ^ 040) > (obj->invlet ^ 040)){
85 obj->nobj = otmp->nobj;
101 obj->owt = weight(obj);
105 obfree(obj, (struct obj *) 0);
115 invent = invent->nobj;
117 for(otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
118 if(!otmp->nobj) panic("freeinv");
119 otmp->nobj = obj->nobj;
123 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
124 delobj(obj) struct obj *obj; {
127 obfree(obj, (struct obj *) 0);
130 /* unlink obj from chain starting with fobj */
131 freeobj(obj) struct obj *obj; {
134 if(obj == fobj) fobj = fobj->nobj;
136 for(otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
137 if(!otmp) panic("error in freeobj");
138 otmp->nobj = obj->nobj;
142 /* Note: freegold throws away its argument! */
143 freegold(gold) struct gold *gold; {
146 if(gold == fgold) fgold = gold->ngold;
148 for(gtmp = fgold; gtmp->ngold != gold; gtmp = gtmp->ngold)
149 if(!gtmp) panic("error in freegold");
150 gtmp->ngold = gold->ngold;
161 ftrap = ftrap->ntrap;
163 for(ttmp = ftrap; ttmp->ntrap != trap; ttmp = ttmp->ntrap) ;
164 ttmp->ntrap = trap->ntrap;
169 struct wseg *m_atseg;
181 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon){
182 if(mtmp->mx == x && mtmp->my == y)
186 for(wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
187 if(wtmp->wx == x && wtmp->wy == y){
203 for(otmp = fobj; otmp; otmp = otmp->nobj)
204 if(otmp->ox == x && otmp->oy == y) return(otmp);
214 for(otmp = fobj; otmp; otmp = otmp->nobj)
215 if(otmp->ox == x && otmp->oy == y && otmp->otyp == n)
220 carried(obj) struct obj *obj; {
222 for(otmp = invent; otmp; otmp = otmp->nobj)
223 if(otmp == obj) return(1);
232 for(otmp = invent; otmp; otmp = otmp->nobj)
233 if(otmp->otyp == type)
239 o_on(id, objchn) unsigned int id; struct obj *objchn; {
241 if(objchn->o_id == id) return(objchn);
242 objchn = objchn->nobj;
244 return((struct obj *) 0);
251 struct trap *trap = ftrap;
253 if(trap->tx == x && trap->ty == y) return(trap);
263 struct gold *gold = fgold;
265 if(gold->gx == x && gold->gy == y) return(gold);
271 /* make dummy object structure containing gold - for temporary use only */
279 /* should set o_id etc. but otmp will be freed soon */
289 * struct obj *xxx: object to do something with.
290 * (struct obj *) 0 error return: no object.
291 * &zeroobj explicitly no object (as in w-).
298 char ilet,ilet1,ilet2;
303 xchar allowcnt = 0; /* 0, 1 or 2 */
304 boolean allowgold = FALSE;
305 boolean allowall = FALSE;
306 boolean allownone = FALSE;
310 if(*let == '0') let++, allowcnt = 1;
311 if(*let == '$') let++, allowgold = TRUE;
312 if(*let == '#') let++, allowall = TRUE;
313 if(*let == '-') let++, allownone = TRUE;
314 if(allownone) *bp++ = '-';
315 if(allowgold) *bp++ = '$';
316 if(bp > buf && bp[-1] == '-') *bp++ = ' ';
319 for(otmp = invent; otmp; otmp = otmp->nobj){
320 if(!*let || index(let, otmp->olet)) {
321 bp[foo++] = flags.invlet_constant ? otmp->invlet : ilet;
323 /* ugly check: remove inappropriate things */
324 if((!strcmp(word, "take off") &&
325 !(otmp->owornmask & (W_ARMOR - W_ARM2)))
326 || (!strcmp(word, "wear") &&
327 (otmp->owornmask & (W_ARMOR | W_RING)))
328 || (!strcmp(word, "wield") &&
329 (otmp->owornmask & W_WEP))) {
334 if(ilet == 'z') ilet = 'A'; else ilet++;
337 if(foo == 0 && bp > buf && bp[-1] == ' ') *--bp = 0;
338 (void) strcpy(lets, bp); /* necessary since we destroy buf */
339 if(foo > 5) { /* compactify string */
343 while(ilet = bp[++foo2] = bp[++foo]){
346 bp[foo2 - 1] = ilet1 = '-';
347 else if(ilet2 == '-') {
348 bp[--foo2] = ++ilet1;
356 if(!foo && !allowall && !allowgold && !allownone) {
357 pline("You don't have anything %sto %s.",
358 foox ? "else " : "", word);
363 pline("What do you want to %s [*]? ", word);
365 pline("What do you want to %s [%s or ?*]? ",
370 while(digit(ilet) && allowcnt) {
372 cnt = 10*cnt + (ilet - '0');
375 allowcnt = 2; /* signal presence of cnt */
379 pline("No count allowed with this command.");
382 if(index(quitchars,ilet))
383 return((struct obj *)0);
385 return(allownone ? &zeroobj : (struct obj *) 0);
389 pline("You cannot %s gold.", word);
392 if(!(allowcnt == 2 && cnt < u.ugold))
394 return(mkgoldobj(cnt));
398 if(!(ilet = morc)) continue;
399 /* he typed a letter (not a space) to more() */
400 } else if(ilet == '*') {
402 if(!(ilet = morc)) continue;
405 if(flags.invlet_constant) {
406 for(otmp = invent; otmp; otmp = otmp->nobj)
407 if(otmp->invlet == ilet) break;
409 if(ilet >= 'A' && ilet <= 'Z') ilet += 'z'-'A'+1;
411 for(otmp = invent; otmp && ilet;
412 ilet--, otmp = otmp->nobj) ;
415 pline("You don't have that object.");
418 if(cnt < 0 || otmp->quan < cnt) {
419 pline("You don't have that many! [You have %u]"
425 if(!allowall && let && !index(let,otmp->olet)) {
426 pline("That is a silly thing to %s.",word);
429 if(allowcnt == 2) { /* cnt given */
430 if(cnt == 0) return(0);
431 if(cnt != otmp->quan) {
433 obj = splitobj(otmp, (int) cnt);
434 if(otmp == uwep) setuwep(obj);
440 ckunpaid(otmp) struct obj *otmp; {
441 return( otmp->unpaid );
444 /* interactive version of getobj - used for Drop and Identify */
445 /* return the number of times fn was called successfully */
446 ggetobj(word, fn, max)
453 int oletct = 0, iletct = 0;
454 boolean allflag = FALSE;
455 char olets[20], ilets[20];
456 int (*ckfn)() = (int (*)()) 0;
457 xchar allowgold = (u.ugold && !strcmp(word, "drop")) ? 1 : 0; /* BAH */
458 if(!invent && !allowgold){
459 pline("You have nothing to %s.", word);
462 struct obj *otmp = invent;
465 if(allowgold) ilets[iletct++] = '$';
468 if(!index(ilets, otmp->olet)){
469 ilets[iletct++] = otmp->olet;
472 if(otmp->unpaid) uflg = 1;
475 ilets[iletct++] = ' ';
476 if(uflg) ilets[iletct++] = 'u';
477 if(invent) ilets[iletct++] = 'a';
480 pline("What kinds of thing do you want to %s? [%s] ",
483 if(buf[0] == '\033') {
490 if(sym == ' ') continue;
493 (*fn)(mkgoldobj(u.ugold));
495 pline("You have no gold.");
498 if(sym == 'a' || sym == 'A') allflag = TRUE; else
499 if(sym == 'u' || sym == 'U') ckfn = ckunpaid; else
500 if(index("!%?[()=*/\"0", sym)){
501 if(!index(olets, sym)){
502 olets[oletct++] = sym;
506 else pline("You don't have any %c's.", sym);
508 if(allowgold == 2 && !oletct)
509 return(1); /* he dropped gold (or at least tried to) */
511 return(askchain(invent, olets, allflag, fn, ckfn, max));
515 * Walk through the chain starting at objchn and ask for all objects
516 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
517 * whether the action in question (i.e., fn) has to be performed.
518 * If allflag then no questions are asked. Max gives the max nr of
519 * objects to be treated. Return the number of objects treated.
521 askchain(objchn, olets, allflag, fn, ckfn, max)
525 int (*fn)(), (*ckfn)();
528 struct obj *otmp, *otmp2;
532 for(otmp = objchn; otmp; otmp = otmp2){
533 if(ilet == 'z') ilet = 'A'; else ilet++;
535 if(olets && *olets && !index(olets, otmp->olet)) continue;
536 if(ckfn && !(*ckfn)(otmp)) continue;
538 pline(xprname(otmp, ilet));
539 addtopl(" [nyaq]? ");
549 if(--max == 0) goto ret;
557 pline(cnt ? "That was all." : "No applicable objects.");
562 obj_to_let(obj) /* should of course only be called for things in invent */
568 if(flags.invlet_constant)
571 for(otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
572 if(++ilet > 'z') ilet = 'A';
573 return(otmp ? ilet : NOINVSYM);
579 pline(xprname(obj, obj_to_let(obj)));
587 static char li[BUFSZ];
589 (void) sprintf(li, "%c - %s.",
590 flags.invlet_constant ? obj->invlet : let,
601 /* called with 0 or "": all objects in inventory */
602 /* otherwise: all objects with (serial) letter in lets */
611 morc = 0; /* just to be sure */
614 pline("Not carrying anything.");
618 cornline(0, (char *) 0);
620 for(otmp = invent; otmp; otmp = otmp->nobj) {
621 if(flags.invlet_constant) ilet = otmp->invlet;
622 if(!lets || !*lets || index(lets, ilet)) {
623 cornline(1, xprname(otmp, ilet));
626 if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
632 dotypeinv () /* free after Robert Viduya */
633 /* Changed to one type only, so he doesnt have to type cr */
639 boolean billx = inshop() && doinvbill(0);
640 boolean unpd = FALSE;
642 if (!invent && !u.ugold && !billx) {
643 pline ("You aren't carrying anything.");
648 if(u.ugold) stuff[stct++] = '$';
650 for(otmp = invent; otmp; otmp = otmp->nobj) {
651 if (!index (stuff, otmp->olet)) {
652 stuff[stct++] = otmp->olet;
658 if(unpd) stuff[stct++] = 'u';
659 if(billx) stuff[stct++] = 'x';
663 pline ("What type of object [%s] do you want an inventory of? ",
666 if(index(quitchars,c)) return(0);
673 if(c == 'x' || c == 'X') {
677 pline("No used-up objects on the shopping bill.");
681 if((c == 'u' || c == 'U') && !unpd) {
682 pline("You are not carrying any unpaid objects.");
688 for (otmp = invent; otmp; otmp = otmp -> nobj) {
689 if(flags.invlet_constant) ilet = otmp->invlet;
690 if (c == otmp -> olet || (c == 'u' && otmp -> unpaid))
691 stuff[stct++] = ilet;
692 if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
696 pline("You have no such objects.");
703 /* look at what is here */
705 struct obj *otmp, *otmp0;
707 char *verb = Blind ? "feel" : "see";
712 pline("You try to feel what is lying here on the floor.");
713 if(Levitation) { /* ab@unido */
714 pline("You cannot reach the floor!");
718 otmp0 = o_at(u.ux, u.uy);
719 gold = g_at(u.ux, u.uy);
722 if(u.uswallow || (!otmp0 && !gold)) {
723 pline("You %s no objects here.", verb);
727 cornline(0, "Things that are here:");
728 for(otmp = otmp0; otmp; otmp = otmp->nobj) {
729 if(otmp->ox == u.ux && otmp->oy == u.uy) {
731 cornline(1, doname(otmp));
732 if(Blind && otmp->otyp == DEAD_COCKATRICE && !uarmg) {
733 pline("Touching the dead cockatrice is a fatal mistake ...");
734 pline("You die ...");
735 killer = "dead cockatrice";
744 (void) sprintf(gbuf, "%ld gold piece%s",
745 gold->amount, plur(gold->amount));
747 pline("You %s here %s.", verb, gbuf);
752 if(ct == 1 && !gold) {
753 pline("You %s here %s.", verb, doname(otmp0));
754 cornline(3, (char *) 0);
757 cornline(2, (char *) 0);
761 stackobj(obj) struct obj *obj; {
762 struct obj *otmp = fobj;
763 for(otmp = fobj; otmp; otmp = otmp->nobj) if(otmp != obj)
764 if(otmp->ox == obj->ox && otmp->oy == obj->oy &&
769 /* merge obj with otmp and delete obj if types agree */
770 merged(otmp,obj,lose) struct obj *otmp, *obj; {
771 if(obj->otyp == otmp->otyp &&
772 obj->unpaid == otmp->unpaid &&
773 obj->spe == otmp->spe &&
774 obj->dknown == otmp->dknown &&
775 obj->cursed == otmp->cursed &&
776 (index("%*?!", obj->olet) ||
777 (obj->known == otmp->known &&
778 (obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)))) {
779 otmp->quan += obj->quan;
780 otmp->owt += obj->owt;
781 if(lose) freeobj(obj);
782 obfree(obj,otmp); /* free(obj), bill->otmp */
788 * Gold is no longer displayed; in fact, when you have a lot of money,
789 * it may take a while before you have counted it all.
790 * [Bug: d$ and pickup still tell you how much it was.]
792 extern int (*occupation)();
794 static long goldcounted;
797 if((goldcounted += 100*(u.ulevel + 1)) >= u.ugold) {
799 if(!rn2(2)) eps = rnd((int) (u.ugold/100 + 1));
800 pline("You probably have about %ld gold pieces.",
802 return(0); /* done */
804 return(1); /* continue */
809 pline("You do not carry any gold.");
810 else if(u.ugold <= 500)
811 pline("You are carrying %ld gold pieces.", u.ugold);
813 pline("You sit down in order to count your gold pieces.");
815 occupation = countgold;
816 occtxt = "counting your gold";
821 /* --- end of gold counting section --- */
824 if(!uwep) pline("You are empty handed.");
830 if(!uarm && !uarmg && !uarms && !uarmh)
831 pline("You are not wearing any armor.");
836 if(uarm) lets[ct++] = obj_to_let(uarm);
837 if(uarm2) lets[ct++] = obj_to_let(uarm2);
838 if(uarmh) lets[ct++] = obj_to_let(uarmh);
839 if(uarms) lets[ct++] = obj_to_let(uarms);
840 if(uarmg) lets[ct++] = obj_to_let(uarmg);
848 if(!uleft && !uright)
849 pline("You are not wearing any rings.");
854 if(uleft) lets[ct++] = obj_to_let(uleft);
855 if(uright) lets[ct++] = obj_to_let(uright);
863 return(c >= '0' && c <= '9');