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.5 2006/08/21 19:45:32 pavalos Exp $ */
7 extern struct obj zeroobj;
8 extern char quitchars[];
10 static void assigninvlet(struct obj *);
11 static struct obj *mkgoldobj(long);
12 static bool ckunpaid(struct obj *);
13 static char obj_to_let(struct obj *);
14 static char *xprname(struct obj *, char);
15 static void doinv(char *);
16 static bool merged(struct obj *, struct obj *, bool);
17 static bool countgold(void);
20 extern struct wseg *wsegs[32];
25 static int lastinvnr = 51; /* 0 ... 51 */
27 assigninvlet(struct obj *otmp)
33 for(i = 0; i < 52; i++) inuse[i] = FALSE;
34 for(obj = invent; obj; obj = obj->nobj) if(obj != otmp) {
36 if('a' <= i && i <= 'z') inuse[i - 'a'] = TRUE; else
37 if('A' <= i && i <= 'Z') inuse[i - 'A' + 26] = TRUE;
38 if(i == otmp->invlet) otmp->invlet = 0;
40 if((i = otmp->invlet) &&
41 (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
43 for(i = lastinvnr+1; i != lastinvnr; i++) {
44 if(i == 52) { i = -1; continue; }
47 otmp->invlet = (inuse[i] ? NOINVSYM :
48 (i < 26) ? ('a'+i) : ('A'+i-26));
53 addinv(struct obj *obj)
57 /* merge or attach to end of chain */
62 for(otmp = invent; /* otmp */; otmp = otmp->nobj) {
63 if(merged(otmp, obj, 0))
72 if(flags.invlet_constant) {
75 * The ordering of the chain is nowhere significant
76 * so in case you prefer some other order than the
77 * historical one, change the code below.
79 if(otmp) { /* find proper place in chain */
81 if((invent->invlet ^ 040) > (obj->invlet ^ 040)) {
85 for(otmp = invent; ; otmp = otmp->nobj) {
87 (otmp->nobj->invlet ^ 040) > (obj->invlet ^ 040)){
88 obj->nobj = otmp->nobj;
100 useup(struct obj *obj)
104 obj->owt = weight(obj);
108 obfree(obj, (struct obj *) 0);
113 freeinv(struct obj *obj)
118 invent = invent->nobj;
120 for(otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
121 if(!otmp->nobj) panic("freeinv");
122 otmp->nobj = obj->nobj;
126 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
128 delobj(struct obj *obj)
132 obfree(obj, (struct obj *) 0);
135 /* unlink obj from chain starting with fobj */
137 freeobj(struct obj *obj)
141 if(obj == fobj) fobj = fobj->nobj;
143 for(otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
144 if(!otmp) panic("error in freeobj");
145 otmp->nobj = obj->nobj;
149 /* Note: freegold throws away its argument! */
151 freegold(struct gold *gold)
155 if(gold == fgold) fgold = gold->ngold;
157 for(gtmp = fgold; gtmp->ngold != gold; gtmp = gtmp->ngold)
158 if(!gtmp) panic("error in freegold");
159 gtmp->ngold = gold->ngold;
165 deltrap(struct trap *trap)
170 ftrap = ftrap->ntrap;
172 for(ttmp = ftrap; ttmp->ntrap != trap; ttmp = ttmp->ntrap) ;
173 ttmp->ntrap = trap->ntrap;
178 struct wseg *m_atseg;
189 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon){
190 if(mtmp->mx == x && mtmp->my == y)
194 for(wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
195 if(wtmp->wx == x && wtmp->wy == y){
210 for(otmp = fobj; otmp; otmp = otmp->nobj)
211 if(otmp->ox == x && otmp->oy == y) return(otmp);
216 sobj_at(int n, int x, int y)
220 for(otmp = fobj; otmp; otmp = otmp->nobj)
221 if(otmp->ox == x && otmp->oy == y && otmp->otyp == n)
227 carried(struct obj *obj)
230 for(otmp = invent; otmp; otmp = otmp->nobj)
231 if(otmp == obj) return(1);
240 for(otmp = invent; otmp; otmp = otmp->nobj)
241 if(otmp->otyp == type)
247 o_on(unsigned int id, struct obj *objchn)
250 if(objchn->o_id == id) return(objchn);
251 objchn = objchn->nobj;
253 return((struct obj *) 0);
259 struct trap *trap = ftrap;
261 if(trap->tx == x && trap->ty == y) return(trap);
270 struct gold *gold = fgold;
272 if(gold->gx == x && gold->gy == y) return(gold);
278 /* make dummy object structure containing gold - for temporary use only */
285 /* should set o_id etc. but otmp will be freed soon */
295 * struct obj *xxx: object to do something with.
296 * (struct obj *) 0 error return: no object.
297 * &zeroobj explicitly no object (as in w-).
300 getobj(const char *let, const char *word)
303 char ilet,ilet1,ilet2;
308 xchar allowcnt = 0; /* 0, 1 or 2 */
309 boolean allowgold = FALSE;
310 boolean allowall = FALSE;
311 boolean allownone = FALSE;
315 if(*let == '0') let++, allowcnt = 1;
316 if(*let == '$') let++, allowgold = TRUE;
317 if(*let == '#') let++, allowall = TRUE;
318 if(*let == '-') let++, allownone = TRUE;
319 if(allownone) *bp++ = '-';
320 if(allowgold) *bp++ = '$';
321 if(bp > buf && bp[-1] == '-') *bp++ = ' ';
324 for(otmp = invent; otmp; otmp = otmp->nobj){
325 if(!*let || index(let, otmp->olet)) {
326 bp[foo++] = flags.invlet_constant ? otmp->invlet : ilet;
328 /* ugly check: remove inappropriate things */
329 if((!strcmp(word, "take off") &&
330 !(otmp->owornmask & (W_ARMOR - W_ARM2)))
331 || (!strcmp(word, "wear") &&
332 (otmp->owornmask & (W_ARMOR | W_RING)))
333 || (!strcmp(word, "wield") &&
334 (otmp->owornmask & W_WEP))) {
339 if(ilet == 'z') ilet = 'A'; else ilet++;
342 if(foo == 0 && bp > buf && bp[-1] == ' ') *--bp = 0;
343 strcpy(lets, bp); /* necessary since we destroy buf */
344 if(foo > 5) { /* compactify string */
348 while((ilet = bp[++foo2] = bp[++foo])){
351 bp[foo2 - 1] = ilet1 = '-';
352 else if(ilet2 == '-') {
353 bp[--foo2] = ++ilet1;
361 if(!foo && !allowall && !allowgold && !allownone) {
362 pline("You don't have anything %sto %s.",
363 foox ? "else " : "", word);
368 pline("What do you want to %s [*]? ", word);
370 pline("What do you want to %s [%s or ?*]? ",
375 while(digit(ilet) && allowcnt) {
377 cnt = 10*cnt + (ilet - '0');
380 allowcnt = 2; /* signal presence of cnt */
384 pline("No count allowed with this command.");
387 if(index(quitchars,ilet))
388 return((struct obj *)0);
390 return(allownone ? &zeroobj : (struct obj *) 0);
394 pline("You cannot %s gold.", word);
397 if(!(allowcnt == 2 && cnt < u.ugold))
399 return(mkgoldobj(cnt));
403 if(!(ilet = morc)) continue;
404 /* he typed a letter (not a space) to more() */
405 } else if(ilet == '*') {
407 if(!(ilet = morc)) continue;
410 if(flags.invlet_constant) {
411 for(otmp = invent; otmp; otmp = otmp->nobj)
412 if(otmp->invlet == ilet) break;
414 if(ilet >= 'A' && ilet <= 'Z') ilet += 'z'-'A'+1;
416 for(otmp = invent; otmp && ilet;
417 ilet--, otmp = otmp->nobj) ;
420 pline("You don't have that object.");
423 if(cnt < 0 || otmp->quan < cnt) {
424 pline("You don't have that many! [You have %u]"
430 if(!allowall && let && !index(let,otmp->olet)) {
431 pline("That is a silly thing to %s.",word);
434 if(allowcnt == 2) { /* cnt given */
435 if(cnt == 0) return(0);
436 if(cnt != otmp->quan) {
438 obj = splitobj(otmp, (int) cnt);
439 if(otmp == uwep) setuwep(obj);
446 ckunpaid(struct obj *otmp)
448 return( otmp->unpaid );
451 /* interactive version of getobj - used for Drop and Identify */
452 /* return the number of times fn was called successfully */
454 ggetobj(const char *word, int (*fn)(struct obj *), int max)
459 int oletct = 0, iletct = 0;
460 boolean allflag = FALSE;
461 char olets[20], ilets[20];
462 bool (*ckfn)(struct obj *) = (bool (*)()) 0;
463 xchar allowgold = (u.ugold && !strcmp(word, "drop")) ? 1 : 0; /* BAH */
464 if(!invent && !allowgold){
465 pline("You have nothing to %s.", word);
468 struct obj *otmp = invent;
471 if(allowgold) ilets[iletct++] = '$';
474 if(!index(ilets, otmp->olet)){
475 ilets[iletct++] = otmp->olet;
478 if(otmp->unpaid) uflg = 1;
481 ilets[iletct++] = ' ';
482 if(uflg) ilets[iletct++] = 'u';
483 if(invent) ilets[iletct++] = 'a';
486 pline("What kinds of thing do you want to %s? [%s] ",
489 if(buf[0] == '\033') {
495 while((sym = *ip++)){
496 if(sym == ' ') continue;
499 (*fn)(mkgoldobj(u.ugold));
501 pline("You have no gold.");
504 if(sym == 'a' || sym == 'A') allflag = TRUE; else
505 if(sym == 'u' || sym == 'U') ckfn = ckunpaid; else
506 if(index("!%?[()=*/\"0", sym)){
507 if(!index(olets, sym)){
508 olets[oletct++] = sym;
512 else pline("You don't have any %c's.", sym);
514 if(allowgold == 2 && !oletct)
515 return(1); /* he dropped gold (or at least tried to) */
517 return(askchain(invent, olets, allflag, fn, ckfn, max));
521 * Walk through the chain starting at objchn and ask for all objects
522 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
523 * whether the action in question (i.e., fn) has to be performed.
524 * If allflag then no questions are asked. Max gives the max nr of
525 * objects to be treated. Return the number of objects treated.
528 askchain(struct obj *objchn, char *olets, int allflag,
529 int (*fn)(struct obj *), bool (*ckfn)(struct obj *), int max)
531 struct obj *otmp, *otmp2;
535 for(otmp = objchn; otmp; otmp = otmp2){
536 if(ilet == 'z') ilet = 'A'; else ilet++;
538 if(olets && *olets && !index(olets, otmp->olet)) continue;
539 if(ckfn && !(*ckfn)(otmp)) continue;
541 pline(xprname(otmp, ilet));
542 addtopl(" [nyaq]? ");
552 if(--max == 0) goto ret;
560 pline(cnt ? "That was all." : "No applicable objects.");
565 /* should of course only be called for things in invent */
567 obj_to_let(struct obj *obj)
572 if(flags.invlet_constant)
575 for(otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
576 if(++ilet > 'z') ilet = 'A';
577 return(otmp ? ilet : NOINVSYM);
581 prinv(struct obj *obj)
583 pline(xprname(obj, obj_to_let(obj)));
587 xprname(struct obj *obj, char let)
589 static char li[BUFSZ];
591 sprintf(li, "%c - %s.",
592 flags.invlet_constant ? obj->invlet : let,
604 /* called with 0 or "": all objects in inventory */
605 /* otherwise: all objects with (serial) letter in lets */
614 morc = 0; /* just to be sure */
617 pline("Not carrying anything.");
621 cornline(0, (char *) 0);
623 for(otmp = invent; otmp; otmp = otmp->nobj) {
624 if(flags.invlet_constant) ilet = otmp->invlet;
625 if(!lets || !*lets || index(lets, ilet)) {
626 cornline(1, xprname(otmp, ilet));
629 if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
636 dotypeinv(void) /* free after Robert Viduya */
637 /* Changed to one type only, so he doesnt have to type cr */
643 boolean billx = inshop() && doinvbill(0);
644 boolean unpd = FALSE;
646 if (!invent && !u.ugold && !billx) {
647 pline ("You aren't carrying anything.");
652 if(u.ugold) stuff[stct++] = '$';
654 for(otmp = invent; otmp; otmp = otmp->nobj) {
655 if (!index (stuff, otmp->olet)) {
656 stuff[stct++] = otmp->olet;
662 if(unpd) stuff[stct++] = 'u';
663 if(billx) stuff[stct++] = 'x';
667 pline ("What type of object [%s] do you want an inventory of? ",
670 if(index(quitchars,c)) return(0);
677 if(c == 'x' || c == 'X') {
681 pline("No used-up objects on the shopping bill.");
685 if((c == 'u' || c == 'U') && !unpd) {
686 pline("You are not carrying any unpaid objects.");
692 for (otmp = invent; otmp; otmp = otmp -> nobj) {
693 if(flags.invlet_constant) ilet = otmp->invlet;
694 if (c == otmp -> olet || (c == 'u' && otmp -> unpaid))
695 stuff[stct++] = ilet;
696 if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
700 pline("You have no such objects.");
707 /* look at what is here */
711 struct obj *otmp, *otmp0 = NULL;
712 struct gold *gold = NULL;
713 const char *verb = Blind ? "feel" : "see";
718 pline("You try to feel what is lying here on the floor.");
719 if(Levitation) { /* ab@unido */
720 pline("You cannot reach the floor!");
724 otmp0 = o_at(u.ux, u.uy);
725 gold = g_at(u.ux, u.uy);
728 if(u.uswallow || (!otmp0 && !gold)) {
729 pline("You %s no objects here.", verb);
733 cornline(0, "Things that are here:");
734 for(otmp = otmp0; otmp; otmp = otmp->nobj) {
735 if(otmp->ox == u.ux && otmp->oy == u.uy) {
737 cornline(1, doname(otmp));
738 if(Blind && otmp->otyp == DEAD_COCKATRICE && !uarmg) {
739 pline("Touching the dead cockatrice is a fatal mistake ...");
740 pline("You die ...");
741 killer = "dead cockatrice";
750 sprintf(gbuf, "%ld gold piece%s",
751 gold->amount, plur(gold->amount));
753 pline("You %s here %s.", verb, gbuf);
758 if(ct == 1 && !gold) {
759 pline("You %s here %s.", verb, doname(otmp0));
760 cornline(3, (char *) 0);
763 cornline(2, (char *) 0);
768 stackobj(struct obj *obj)
770 struct obj *otmp = fobj;
771 for(otmp = fobj; otmp; otmp = otmp->nobj) if(otmp != obj)
772 if(otmp->ox == obj->ox && otmp->oy == obj->oy &&
777 /* merge obj with otmp and delete obj if types agree */
779 merged(struct obj *otmp, struct obj *obj, bool lose)
781 if(obj->otyp == otmp->otyp &&
782 obj->unpaid == otmp->unpaid &&
783 obj->spe == otmp->spe &&
784 obj->dknown == otmp->dknown &&
785 obj->cursed == otmp->cursed &&
786 (index("%*?!", obj->olet) ||
787 (obj->known == otmp->known &&
788 (obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)))) {
789 otmp->quan += obj->quan;
790 otmp->owt += obj->owt;
791 if(lose) freeobj(obj);
792 obfree(obj,otmp); /* free(obj), bill->otmp */
798 * Gold is no longer displayed; in fact, when you have a lot of money,
799 * it may take a while before you have counted it all.
800 * [Bug: d$ and pickup still tell you how much it was.]
802 static long goldcounted;
807 if((goldcounted += 100*(u.ulevel + 1)) >= u.ugold) {
809 if(!rn2(2)) eps = rnd((int) (u.ugold/100 + 1));
810 pline("You probably have about %ld gold pieces.",
812 return(0); /* done */
814 return(1); /* continue */
821 pline("You do not carry any gold.");
822 else if(u.ugold <= 500)
823 pline("You are carrying %ld gold pieces.", u.ugold);
825 pline("You sit down in order to count your gold pieces.");
827 occupation = countgold;
828 occtxt = "counting your gold";
833 /* --- end of gold counting section --- */
838 if(!uwep) pline("You are empty handed.");
846 if(!uarm && !uarmg && !uarms && !uarmh)
847 pline("You are not wearing any armor.");
852 if(uarm) lets[ct++] = obj_to_let(uarm);
853 if(uarm2) lets[ct++] = obj_to_let(uarm2);
854 if(uarmh) lets[ct++] = obj_to_let(uarmh);
855 if(uarms) lets[ct++] = obj_to_let(uarms);
856 if(uarmg) lets[ct++] = obj_to_let(uarmg);
866 if(!uleft && !uright)
867 pline("You are not wearing any rings.");
872 if(uleft) lets[ct++] = obj_to_let(uleft);
873 if(uright) lets[ct++] = obj_to_let(uright);
883 return(c >= '0' && c <= '9');