1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.invent.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.invent.c,v 1.4 1999/11/16 10:26:36 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.invent.c,v 1.2 2003/06/17 04:25:24 dillon Exp $ */
8 extern struct obj *splitobj();
9 extern struct obj zeroobj;
11 extern char quitchars[];
12 static char *xprname();
16 extern struct wseg *wsegs[32];
21 static int lastinvnr = 51; /* 0 ... 51 */
30 for(i = 0; i < 52; i++) inuse[i] = FALSE;
31 for(obj = invent; obj; obj = obj->nobj) if(obj != otmp) {
33 if('a' <= i && i <= 'z') inuse[i - 'a'] = TRUE; else
34 if('A' <= i && i <= 'Z') inuse[i - 'A' + 26] = TRUE;
35 if(i == otmp->invlet) otmp->invlet = 0;
37 if((i = otmp->invlet) &&
38 (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
40 for(i = lastinvnr+1; i != lastinvnr; i++) {
41 if(i == 52) { i = -1; continue; }
44 otmp->invlet = (inuse[i] ? NOINVSYM :
45 (i < 26) ? ('a'+i) : ('A'+i-26));
55 /* merge or attach to end of chain */
60 for(otmp = invent; /* otmp */; otmp = otmp->nobj) {
61 if(merged(otmp, obj, 0))
70 if(flags.invlet_constant) {
73 * The ordering of the chain is nowhere significant
74 * so in case you prefer some other order than the
75 * historical one, change the code below.
77 if(otmp) { /* find proper place in chain */
79 if((invent->invlet ^ 040) > (obj->invlet ^ 040)) {
83 for(otmp = invent; ; otmp = otmp->nobj) {
85 (otmp->nobj->invlet ^ 040) > (obj->invlet ^ 040)){
86 obj->nobj = otmp->nobj;
102 obj->owt = weight(obj);
106 obfree(obj, (struct obj *) 0);
116 invent = invent->nobj;
118 for(otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
119 if(!otmp->nobj) panic("freeinv");
120 otmp->nobj = obj->nobj;
124 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
125 delobj(obj) struct obj *obj; {
128 obfree(obj, (struct obj *) 0);
131 /* unlink obj from chain starting with fobj */
132 freeobj(obj) struct obj *obj; {
135 if(obj == fobj) fobj = fobj->nobj;
137 for(otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
138 if(!otmp) panic("error in freeobj");
139 otmp->nobj = obj->nobj;
143 /* Note: freegold throws away its argument! */
144 freegold(gold) struct gold *gold; {
147 if(gold == fgold) fgold = gold->ngold;
149 for(gtmp = fgold; gtmp->ngold != gold; gtmp = gtmp->ngold)
150 if(!gtmp) panic("error in freegold");
151 gtmp->ngold = gold->ngold;
162 ftrap = ftrap->ntrap;
164 for(ttmp = ftrap; ttmp->ntrap != trap; ttmp = ttmp->ntrap) ;
165 ttmp->ntrap = trap->ntrap;
170 struct wseg *m_atseg;
182 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon){
183 if(mtmp->mx == x && mtmp->my == y)
187 for(wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
188 if(wtmp->wx == x && wtmp->wy == y){
204 for(otmp = fobj; otmp; otmp = otmp->nobj)
205 if(otmp->ox == x && otmp->oy == y) return(otmp);
215 for(otmp = fobj; otmp; otmp = otmp->nobj)
216 if(otmp->ox == x && otmp->oy == y && otmp->otyp == n)
221 carried(obj) struct obj *obj; {
223 for(otmp = invent; otmp; otmp = otmp->nobj)
224 if(otmp == obj) return(1);
233 for(otmp = invent; otmp; otmp = otmp->nobj)
234 if(otmp->otyp == type)
240 o_on(id, objchn) unsigned int id; struct obj *objchn; {
242 if(objchn->o_id == id) return(objchn);
243 objchn = objchn->nobj;
245 return((struct obj *) 0);
252 struct trap *trap = ftrap;
254 if(trap->tx == x && trap->ty == y) return(trap);
264 struct gold *gold = fgold;
266 if(gold->gx == x && gold->gy == y) return(gold);
272 /* make dummy object structure containing gold - for temporary use only */
280 /* should set o_id etc. but otmp will be freed soon */
290 * struct obj *xxx: object to do something with.
291 * (struct obj *) 0 error return: no object.
292 * &zeroobj explicitly no object (as in w-).
299 char ilet,ilet1,ilet2;
304 xchar allowcnt = 0; /* 0, 1 or 2 */
305 boolean allowgold = FALSE;
306 boolean allowall = FALSE;
307 boolean allownone = FALSE;
311 if(*let == '0') let++, allowcnt = 1;
312 if(*let == '$') let++, allowgold = TRUE;
313 if(*let == '#') let++, allowall = TRUE;
314 if(*let == '-') let++, allownone = TRUE;
315 if(allownone) *bp++ = '-';
316 if(allowgold) *bp++ = '$';
317 if(bp > buf && bp[-1] == '-') *bp++ = ' ';
320 for(otmp = invent; otmp; otmp = otmp->nobj){
321 if(!*let || index(let, otmp->olet)) {
322 bp[foo++] = flags.invlet_constant ? otmp->invlet : ilet;
324 /* ugly check: remove inappropriate things */
325 if((!strcmp(word, "take off") &&
326 !(otmp->owornmask & (W_ARMOR - W_ARM2)))
327 || (!strcmp(word, "wear") &&
328 (otmp->owornmask & (W_ARMOR | W_RING)))
329 || (!strcmp(word, "wield") &&
330 (otmp->owornmask & W_WEP))) {
335 if(ilet == 'z') ilet = 'A'; else ilet++;
338 if(foo == 0 && bp > buf && bp[-1] == ' ') *--bp = 0;
339 (void) strcpy(lets, bp); /* necessary since we destroy buf */
340 if(foo > 5) { /* compactify string */
344 while(ilet = bp[++foo2] = bp[++foo]){
347 bp[foo2 - 1] = ilet1 = '-';
348 else if(ilet2 == '-') {
349 bp[--foo2] = ++ilet1;
357 if(!foo && !allowall && !allowgold && !allownone) {
358 pline("You don't have anything %sto %s.",
359 foox ? "else " : "", word);
364 pline("What do you want to %s [*]? ", word);
366 pline("What do you want to %s [%s or ?*]? ",
371 while(digit(ilet) && allowcnt) {
373 cnt = 10*cnt + (ilet - '0');
376 allowcnt = 2; /* signal presence of cnt */
380 pline("No count allowed with this command.");
383 if(index(quitchars,ilet))
384 return((struct obj *)0);
386 return(allownone ? &zeroobj : (struct obj *) 0);
390 pline("You cannot %s gold.", word);
393 if(!(allowcnt == 2 && cnt < u.ugold))
395 return(mkgoldobj(cnt));
399 if(!(ilet = morc)) continue;
400 /* he typed a letter (not a space) to more() */
401 } else if(ilet == '*') {
403 if(!(ilet = morc)) continue;
406 if(flags.invlet_constant) {
407 for(otmp = invent; otmp; otmp = otmp->nobj)
408 if(otmp->invlet == ilet) break;
410 if(ilet >= 'A' && ilet <= 'Z') ilet += 'z'-'A'+1;
412 for(otmp = invent; otmp && ilet;
413 ilet--, otmp = otmp->nobj) ;
416 pline("You don't have that object.");
419 if(cnt < 0 || otmp->quan < cnt) {
420 pline("You don't have that many! [You have %u]"
426 if(!allowall && let && !index(let,otmp->olet)) {
427 pline("That is a silly thing to %s.",word);
430 if(allowcnt == 2) { /* cnt given */
431 if(cnt == 0) return(0);
432 if(cnt != otmp->quan) {
434 obj = splitobj(otmp, (int) cnt);
435 if(otmp == uwep) setuwep(obj);
441 ckunpaid(otmp) struct obj *otmp; {
442 return( otmp->unpaid );
445 /* interactive version of getobj - used for Drop and Identify */
446 /* return the number of times fn was called successfully */
447 ggetobj(word, fn, max)
454 int oletct = 0, iletct = 0;
455 boolean allflag = FALSE;
456 char olets[20], ilets[20];
457 int (*ckfn)() = (int (*)()) 0;
458 xchar allowgold = (u.ugold && !strcmp(word, "drop")) ? 1 : 0; /* BAH */
459 if(!invent && !allowgold){
460 pline("You have nothing to %s.", word);
463 struct obj *otmp = invent;
466 if(allowgold) ilets[iletct++] = '$';
469 if(!index(ilets, otmp->olet)){
470 ilets[iletct++] = otmp->olet;
473 if(otmp->unpaid) uflg = 1;
476 ilets[iletct++] = ' ';
477 if(uflg) ilets[iletct++] = 'u';
478 if(invent) ilets[iletct++] = 'a';
481 pline("What kinds of thing do you want to %s? [%s] ",
484 if(buf[0] == '\033') {
491 if(sym == ' ') continue;
494 (*fn)(mkgoldobj(u.ugold));
496 pline("You have no gold.");
499 if(sym == 'a' || sym == 'A') allflag = TRUE; else
500 if(sym == 'u' || sym == 'U') ckfn = ckunpaid; else
501 if(index("!%?[()=*/\"0", sym)){
502 if(!index(olets, sym)){
503 olets[oletct++] = sym;
507 else pline("You don't have any %c's.", sym);
509 if(allowgold == 2 && !oletct)
510 return(1); /* he dropped gold (or at least tried to) */
512 return(askchain(invent, olets, allflag, fn, ckfn, max));
516 * Walk through the chain starting at objchn and ask for all objects
517 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
518 * whether the action in question (i.e., fn) has to be performed.
519 * If allflag then no questions are asked. Max gives the max nr of
520 * objects to be treated. Return the number of objects treated.
522 askchain(objchn, olets, allflag, fn, ckfn, max)
526 int (*fn)(), (*ckfn)();
529 struct obj *otmp, *otmp2;
533 for(otmp = objchn; otmp; otmp = otmp2){
534 if(ilet == 'z') ilet = 'A'; else ilet++;
536 if(olets && *olets && !index(olets, otmp->olet)) continue;
537 if(ckfn && !(*ckfn)(otmp)) continue;
539 pline(xprname(otmp, ilet));
540 addtopl(" [nyaq]? ");
550 if(--max == 0) goto ret;
558 pline(cnt ? "That was all." : "No applicable objects.");
563 obj_to_let(obj) /* should of course only be called for things in invent */
569 if(flags.invlet_constant)
572 for(otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
573 if(++ilet > 'z') ilet = 'A';
574 return(otmp ? ilet : NOINVSYM);
580 pline(xprname(obj, obj_to_let(obj)));
588 static char li[BUFSZ];
590 (void) sprintf(li, "%c - %s.",
591 flags.invlet_constant ? obj->invlet : let,
602 /* called with 0 or "": all objects in inventory */
603 /* otherwise: all objects with (serial) letter in lets */
612 morc = 0; /* just to be sure */
615 pline("Not carrying anything.");
619 cornline(0, (char *) 0);
621 for(otmp = invent; otmp; otmp = otmp->nobj) {
622 if(flags.invlet_constant) ilet = otmp->invlet;
623 if(!lets || !*lets || index(lets, ilet)) {
624 cornline(1, xprname(otmp, ilet));
627 if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
633 dotypeinv () /* free after Robert Viduya */
634 /* Changed to one type only, so he doesnt have to type cr */
640 boolean billx = inshop() && doinvbill(0);
641 boolean unpd = FALSE;
643 if (!invent && !u.ugold && !billx) {
644 pline ("You aren't carrying anything.");
649 if(u.ugold) stuff[stct++] = '$';
651 for(otmp = invent; otmp; otmp = otmp->nobj) {
652 if (!index (stuff, otmp->olet)) {
653 stuff[stct++] = otmp->olet;
659 if(unpd) stuff[stct++] = 'u';
660 if(billx) stuff[stct++] = 'x';
664 pline ("What type of object [%s] do you want an inventory of? ",
667 if(index(quitchars,c)) return(0);
674 if(c == 'x' || c == 'X') {
678 pline("No used-up objects on the shopping bill.");
682 if((c == 'u' || c == 'U') && !unpd) {
683 pline("You are not carrying any unpaid objects.");
689 for (otmp = invent; otmp; otmp = otmp -> nobj) {
690 if(flags.invlet_constant) ilet = otmp->invlet;
691 if (c == otmp -> olet || (c == 'u' && otmp -> unpaid))
692 stuff[stct++] = ilet;
693 if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
697 pline("You have no such objects.");
704 /* look at what is here */
706 struct obj *otmp, *otmp0;
708 char *verb = Blind ? "feel" : "see";
713 pline("You try to feel what is lying here on the floor.");
714 if(Levitation) { /* ab@unido */
715 pline("You cannot reach the floor!");
719 otmp0 = o_at(u.ux, u.uy);
720 gold = g_at(u.ux, u.uy);
723 if(u.uswallow || (!otmp0 && !gold)) {
724 pline("You %s no objects here.", verb);
728 cornline(0, "Things that are here:");
729 for(otmp = otmp0; otmp; otmp = otmp->nobj) {
730 if(otmp->ox == u.ux && otmp->oy == u.uy) {
732 cornline(1, doname(otmp));
733 if(Blind && otmp->otyp == DEAD_COCKATRICE && !uarmg) {
734 pline("Touching the dead cockatrice is a fatal mistake ...");
735 pline("You die ...");
736 killer = "dead cockatrice";
745 (void) sprintf(gbuf, "%ld gold piece%s",
746 gold->amount, plur(gold->amount));
748 pline("You %s here %s.", verb, gbuf);
753 if(ct == 1 && !gold) {
754 pline("You %s here %s.", verb, doname(otmp0));
755 cornline(3, (char *) 0);
758 cornline(2, (char *) 0);
762 stackobj(obj) struct obj *obj; {
763 struct obj *otmp = fobj;
764 for(otmp = fobj; otmp; otmp = otmp->nobj) if(otmp != obj)
765 if(otmp->ox == obj->ox && otmp->oy == obj->oy &&
770 /* merge obj with otmp and delete obj if types agree */
771 merged(otmp,obj,lose) struct obj *otmp, *obj; {
772 if(obj->otyp == otmp->otyp &&
773 obj->unpaid == otmp->unpaid &&
774 obj->spe == otmp->spe &&
775 obj->dknown == otmp->dknown &&
776 obj->cursed == otmp->cursed &&
777 (index("%*?!", obj->olet) ||
778 (obj->known == otmp->known &&
779 (obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)))) {
780 otmp->quan += obj->quan;
781 otmp->owt += obj->owt;
782 if(lose) freeobj(obj);
783 obfree(obj,otmp); /* free(obj), bill->otmp */
789 * Gold is no longer displayed; in fact, when you have a lot of money,
790 * it may take a while before you have counted it all.
791 * [Bug: d$ and pickup still tell you how much it was.]
793 extern int (*occupation)();
795 static long goldcounted;
798 if((goldcounted += 100*(u.ulevel + 1)) >= u.ugold) {
800 if(!rn2(2)) eps = rnd((int) (u.ugold/100 + 1));
801 pline("You probably have about %ld gold pieces.",
803 return(0); /* done */
805 return(1); /* continue */
810 pline("You do not carry any gold.");
811 else if(u.ugold <= 500)
812 pline("You are carrying %ld gold pieces.", u.ugold);
814 pline("You sit down in order to count your gold pieces.");
816 occupation = countgold;
817 occtxt = "counting your gold";
822 /* --- end of gold counting section --- */
825 if(!uwep) pline("You are empty handed.");
831 if(!uarm && !uarmg && !uarms && !uarmh)
832 pline("You are not wearing any armor.");
837 if(uarm) lets[ct++] = obj_to_let(uarm);
838 if(uarm2) lets[ct++] = obj_to_let(uarm2);
839 if(uarmh) lets[ct++] = obj_to_let(uarmh);
840 if(uarms) lets[ct++] = obj_to_let(uarms);
841 if(uarmg) lets[ct++] = obj_to_let(uarmg);
849 if(!uleft && !uright)
850 pline("You are not wearing any rings.");
855 if(uleft) lets[ct++] = obj_to_let(uleft);
856 if(uright) lets[ct++] = obj_to_let(uright);
864 return(c >= '0' && c <= '9');