NetBSD/games/hack/hack.invent.c

986 lines
20 KiB
C
Raw Normal View History

/* $NetBSD: hack.invent.c,v 1.7 2001/03/25 20:44:00 jsm Exp $ */
1997-10-19 20:56:41 +04:00
1993-08-02 21:16:36 +04:00
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
1993-08-02 21:16:36 +04:00
#ifndef lint
__RCSID("$NetBSD: hack.invent.c,v 1.7 2001/03/25 20:44:00 jsm Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
#include <stdlib.h>
#include "hack.h"
#include "extern.h"
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
#include "def.wseg.h"
1997-10-19 20:56:41 +04:00
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
#define NOINVSYM '#'
1997-10-19 20:56:41 +04:00
static int lastinvnr = 51; /* 0 ... 51 */
static void assigninvlet __P((struct obj *));
static char *xprname __P((struct obj *, char));
static void
1993-03-21 12:45:37 +03:00
assigninvlet(otmp)
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
boolean inuse[52];
int i;
struct obj *obj;
for (i = 0; i < 52; i++)
inuse[i] = FALSE;
for (obj = invent; obj; obj = obj->nobj)
if (obj != otmp) {
i = obj->invlet;
if ('a' <= i && i <= 'z')
inuse[i - 'a'] = TRUE;
else if ('A' <= i && i <= 'Z')
inuse[i - 'A' + 26] = TRUE;
if (i == otmp->invlet)
otmp->invlet = 0;
}
if ((i = otmp->invlet) &&
1993-03-21 12:45:37 +03:00
(('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
return;
1997-10-19 20:56:41 +04:00
for (i = lastinvnr + 1; i != lastinvnr; i++) {
if (i == 52) {
i = -1;
continue;
}
if (!inuse[i])
break;
1993-03-21 12:45:37 +03:00
}
otmp->invlet = (inuse[i] ? NOINVSYM :
1997-10-19 20:56:41 +04:00
(i < 26) ? ('a' + i) : ('A' + i - 26));
1993-03-21 12:45:37 +03:00
lastinvnr = i;
}
1997-10-19 20:56:41 +04:00
struct obj *
1993-03-21 12:45:37 +03:00
addinv(obj)
1997-10-19 20:56:41 +04:00
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
/* merge or attach to end of chain */
1997-10-19 20:56:41 +04:00
if (!invent) {
1993-03-21 12:45:37 +03:00
invent = obj;
otmp = 0;
} else
1997-10-19 20:56:41 +04:00
for (otmp = invent; /* otmp */ ; otmp = otmp->nobj) {
if (merged(otmp, obj, 0))
return (otmp);
if (!otmp->nobj) {
otmp->nobj = obj;
break;
}
1993-03-21 12:45:37 +03:00
}
obj->nobj = 0;
1997-10-19 20:56:41 +04:00
if (flags.invlet_constant) {
1993-03-21 12:45:37 +03:00
assigninvlet(obj);
/*
* The ordering of the chain is nowhere significant
* so in case you prefer some other order than the
* historical one, change the code below.
*/
1997-10-19 20:56:41 +04:00
if (otmp) { /* find proper place in chain */
1993-03-21 12:45:37 +03:00
otmp->nobj = 0;
1997-10-19 20:56:41 +04:00
if ((invent->invlet ^ 040) > (obj->invlet ^ 040)) {
1993-03-21 12:45:37 +03:00
obj->nobj = invent;
invent = obj;
} else
1997-10-19 20:56:41 +04:00
for (otmp = invent;; otmp = otmp->nobj) {
if (!otmp->nobj ||
(otmp->nobj->invlet ^ 040) > (obj->invlet ^ 040)) {
obj->nobj = otmp->nobj;
otmp->nobj = obj;
break;
}
}
1993-03-21 12:45:37 +03:00
}
}
1997-10-19 20:56:41 +04:00
return (obj);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
useup(obj)
1997-10-19 20:56:41 +04:00
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (obj->quan > 1) {
1993-03-21 12:45:37 +03:00
obj->quan--;
obj->owt = weight(obj);
} else {
setnotworn(obj);
freeinv(obj);
obfree(obj, (struct obj *) 0);
}
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
freeinv(obj)
1997-10-19 20:56:41 +04:00
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (obj == invent)
1993-03-21 12:45:37 +03:00
invent = invent->nobj;
else {
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
if (!otmp->nobj)
panic("freeinv");
1993-03-21 12:45:37 +03:00
otmp->nobj = obj->nobj;
}
}
/* destroy object in fobj chain (if unpaid, it remains on the bill) */
1997-10-19 20:56:41 +04:00
void
delobj(obj)
struct obj *obj;
{
1993-03-21 12:45:37 +03:00
freeobj(obj);
unpobj(obj);
obfree(obj, (struct obj *) 0);
}
/* unlink obj from chain starting with fobj */
1997-10-19 20:56:41 +04:00
void
freeobj(obj)
struct obj *obj;
{
struct obj *otmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (obj == fobj)
fobj = fobj->nobj;
1993-03-21 12:45:37 +03:00
else {
1997-10-19 20:56:41 +04:00
for (otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
if (!otmp)
panic("error in freeobj");
1993-03-21 12:45:37 +03:00
otmp->nobj = obj->nobj;
}
}
/* Note: freegold throws away its argument! */
1997-10-19 20:56:41 +04:00
void
freegold(gold)
struct gold *gold;
{
struct gold *gtmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (gold == fgold)
fgold = gold->ngold;
1993-03-21 12:45:37 +03:00
else {
1997-10-19 20:56:41 +04:00
for (gtmp = fgold; gtmp->ngold != gold; gtmp = gtmp->ngold)
if (!gtmp)
panic("error in freegold");
1993-03-21 12:45:37 +03:00
gtmp->ngold = gold->ngold;
}
free((char *) gold);
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
deltrap(trap)
1997-10-19 20:56:41 +04:00
struct trap *trap;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct trap *ttmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (trap == ftrap)
1993-03-21 12:45:37 +03:00
ftrap = ftrap->ntrap;
else {
1997-10-19 20:56:41 +04:00
for (ttmp = ftrap; ttmp->ntrap != trap; ttmp = ttmp->ntrap);
1993-03-21 12:45:37 +03:00
ttmp->ntrap = trap->ntrap;
}
free((char *) trap);
}
1997-10-19 20:56:41 +04:00
struct wseg *m_atseg;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
struct monst *
m_at(x, y)
int x, y;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct monst *mtmp;
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
struct wseg *wtmp;
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
m_atseg = 0;
1997-10-19 20:56:41 +04:00
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
if (mtmp->mx == x && mtmp->my == y)
return (mtmp);
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
if (mtmp->wormno) {
for (wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
if (wtmp->wx == x && wtmp->wy == y) {
m_atseg = wtmp;
return (mtmp);
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct obj *
o_at(x, y)
int x, y;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
for (otmp = fobj; otmp; otmp = otmp->nobj)
if (otmp->ox == x && otmp->oy == y)
return (otmp);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct obj *
sobj_at(n, x, y)
int n, x, y;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
for (otmp = fobj; otmp; otmp = otmp->nobj)
if (otmp->ox == x && otmp->oy == y && otmp->otyp == n)
return (otmp);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
carried(obj)
struct obj *obj;
{
struct obj *otmp;
for (otmp = invent; otmp; otmp = otmp->nobj)
if (otmp == obj)
return (1);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
carrying(type)
1997-10-19 20:56:41 +04:00
int type;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp; otmp = otmp->nobj)
if (otmp->otyp == type)
return (TRUE);
return (FALSE);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct obj *
o_on(id, objchn)
unsigned int id;
struct obj *objchn;
{
while (objchn) {
if (objchn->o_id == id)
return (objchn);
1993-03-21 12:45:37 +03:00
objchn = objchn->nobj;
}
1997-10-19 20:56:41 +04:00
return ((struct obj *) 0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct trap *
t_at(x, y)
int x, y;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct trap *trap = ftrap;
while (trap) {
if (trap->tx == x && trap->ty == y)
return (trap);
1993-03-21 12:45:37 +03:00
trap = trap->ntrap;
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct gold *
g_at(x, y)
int x, y;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct gold *gold = fgold;
while (gold) {
if (gold->gx == x && gold->gy == y)
return (gold);
1993-03-21 12:45:37 +03:00
gold = gold->ngold;
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* make dummy object structure containing gold - for temporary use only */
1997-10-19 20:56:41 +04:00
struct obj *
1993-03-21 12:45:37 +03:00
mkgoldobj(q)
1997-10-19 20:56:41 +04:00
long q;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
1993-03-21 12:45:37 +03:00
otmp = newobj(0);
/* should set o_id etc. but otmp will be freed soon */
otmp->olet = '$';
u.ugold -= q;
OGOLD(otmp) = q;
flags.botl = 1;
1997-10-19 20:56:41 +04:00
return (otmp);
1993-03-21 12:45:37 +03:00
}
/*
* getobj returns:
* struct obj *xxx: object to do something with.
* (struct obj *) 0 error return: no object.
* &zeroobj explicitly no object (as in w-).
*/
1997-10-19 20:56:41 +04:00
struct obj *
getobj(let, word)
const char *let, *word;
1997-10-19 20:56:41 +04:00
{
struct obj *otmp;
char ilet, ilet1, ilet2;
char buf[BUFSZ];
char lets[BUFSZ];
int foo = 0, foo2;
char *bp = buf;
xchar allowcnt = 0; /* 0, 1 or 2 */
boolean allowgold = FALSE;
boolean allowall = FALSE;
boolean allownone = FALSE;
xchar foox = 0;
long cnt;
if (*let == '0')
let++, allowcnt = 1;
if (*let == '$')
let++, allowgold = TRUE;
if (*let == '#')
let++, allowall = TRUE;
if (*let == '-')
let++, allownone = TRUE;
if (allownone)
*bp++ = '-';
if (allowgold)
*bp++ = '$';
if (bp > buf && bp[-1] == '-')
*bp++ = ' ';
1993-03-21 12:45:37 +03:00
ilet = 'a';
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp; otmp = otmp->nobj) {
if (!*let || strchr(let, otmp->olet)) {
bp[foo++] = flags.invlet_constant ? otmp->invlet : ilet;
/* ugly check: remove inappropriate things */
if ((!strcmp(word, "take off") &&
!(otmp->owornmask & (W_ARMOR - W_ARM2)))
|| (!strcmp(word, "wear") &&
(otmp->owornmask & (W_ARMOR | W_RING)))
|| (!strcmp(word, "wield") &&
(otmp->owornmask & W_WEP))) {
foo--;
foox++;
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (ilet == 'z')
ilet = 'A';
else
ilet++;
1993-03-21 12:45:37 +03:00
}
bp[foo] = 0;
1997-10-19 20:56:41 +04:00
if (foo == 0 && bp > buf && bp[-1] == ' ')
*--bp = 0;
(void) strcpy(lets, bp);/* necessary since we destroy buf */
if (foo > 5) { /* compactify string */
1993-03-21 12:45:37 +03:00
foo = foo2 = 1;
ilet2 = bp[0];
ilet1 = bp[1];
1997-10-19 20:56:41 +04:00
while ((ilet = bp[++foo2] = bp[++foo]) != '\0') {
if (ilet == ilet1 + 1) {
if (ilet1 == ilet2 + 1)
1993-03-21 12:45:37 +03:00
bp[foo2 - 1] = ilet1 = '-';
1997-10-19 20:56:41 +04:00
else if (ilet2 == '-') {
1993-03-21 12:45:37 +03:00
bp[--foo2] = ++ilet1;
continue;
}
}
ilet2 = ilet1;
ilet1 = ilet;
}
}
1997-10-19 20:56:41 +04:00
if (!foo && !allowall && !allowgold && !allownone) {
1993-03-21 12:45:37 +03:00
pline("You don't have anything %sto %s.",
1997-10-19 20:56:41 +04:00
foox ? "else " : "", word);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
for (;;) {
if (!buf[0])
1993-03-21 12:45:37 +03:00
pline("What do you want to %s [*]? ", word);
else
pline("What do you want to %s [%s or ?*]? ",
1997-10-19 20:56:41 +04:00
word, buf);
1993-03-21 12:45:37 +03:00
cnt = 0;
ilet = readchar();
1997-10-19 20:56:41 +04:00
while (digit(ilet) && allowcnt) {
1993-03-21 12:45:37 +03:00
if (cnt < 100000000)
1997-10-19 20:56:41 +04:00
cnt = 10 * cnt + (ilet - '0');
1993-03-21 12:45:37 +03:00
else
1997-10-19 20:56:41 +04:00
cnt = 999999999;
1993-03-21 12:45:37 +03:00
allowcnt = 2; /* signal presence of cnt */
ilet = readchar();
}
1997-10-19 20:56:41 +04:00
if (digit(ilet)) {
1993-03-21 12:45:37 +03:00
pline("No count allowed with this command.");
continue;
}
1997-10-19 20:56:41 +04:00
if (strchr(quitchars, ilet))
return ((struct obj *) 0);
if (ilet == '-') {
return (allownone ? &zeroobj : (struct obj *) 0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (ilet == '$') {
if (!allowgold) {
1993-03-21 12:45:37 +03:00
pline("You cannot %s gold.", word);
continue;
}
1997-10-19 20:56:41 +04:00
if (!(allowcnt == 2 && cnt < u.ugold))
1993-03-21 12:45:37 +03:00
cnt = u.ugold;
1997-10-19 20:56:41 +04:00
return (mkgoldobj(cnt));
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (ilet == '?') {
1993-03-21 12:45:37 +03:00
doinv(lets);
1997-10-19 20:56:41 +04:00
if (!(ilet = morc))
continue;
1993-03-21 12:45:37 +03:00
/* he typed a letter (not a space) to more() */
1997-10-19 20:56:41 +04:00
} else if (ilet == '*') {
1993-03-21 12:45:37 +03:00
doinv((char *) 0);
1997-10-19 20:56:41 +04:00
if (!(ilet = morc))
continue;
1993-03-21 12:45:37 +03:00
/* ... */
}
1997-10-19 20:56:41 +04:00
if (flags.invlet_constant) {
for (otmp = invent; otmp; otmp = otmp->nobj)
if (otmp->invlet == ilet)
break;
1993-03-21 12:45:37 +03:00
} else {
1997-10-19 20:56:41 +04:00
if (ilet >= 'A' && ilet <= 'Z')
ilet += 'z' - 'A' + 1;
1993-03-21 12:45:37 +03:00
ilet -= 'a';
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp && ilet;
ilet--, otmp = otmp->nobj);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (!otmp) {
1993-03-21 12:45:37 +03:00
pline("You don't have that object.");
continue;
}
1997-10-19 20:56:41 +04:00
if (cnt < 0 || otmp->quan < cnt) {
1993-03-21 12:45:37 +03:00
pline("You don't have that many! [You have %u]"
1997-10-19 20:56:41 +04:00
,otmp->quan);
1993-03-21 12:45:37 +03:00
continue;
}
break;
}
1997-10-19 20:56:41 +04:00
if (!allowall && let && !strchr(let, otmp->olet)) {
pline("That is a silly thing to %s.", word);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (allowcnt == 2) { /* cnt given */
if (cnt == 0)
return (0);
if (cnt != otmp->quan) {
struct obj *obj;
1993-03-21 12:45:37 +03:00
obj = splitobj(otmp, (int) cnt);
1997-10-19 20:56:41 +04:00
if (otmp == uwep)
setuwep(obj);
1993-03-21 12:45:37 +03:00
}
}
1997-10-19 20:56:41 +04:00
return (otmp);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
ckunpaid(otmp)
struct obj *otmp;
{
return (otmp->unpaid);
1993-03-21 12:45:37 +03:00
}
/* interactive version of getobj - used for Drop and Identify */
/* return the number of times fn was called successfully */
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
ggetobj(word, fn, max)
const char *word;
1997-10-19 20:56:41 +04:00
int (*fn) __P((struct obj *));
int max;
{
char buf[BUFSZ];
char *ip;
char sym;
int oletct = 0, iletct = 0;
boolean allflag = FALSE;
char olets[20], ilets[20];
int (*ckfn) __P((struct obj *)) =
(int (*) __P((struct obj *))) 0;
xchar allowgold = (u.ugold && !strcmp(word, "drop")) ? 1 : 0; /* BAH */
if (!invent && !allowgold) {
1993-03-21 12:45:37 +03:00
pline("You have nothing to %s.", word);
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
} else {
1997-10-19 20:56:41 +04:00
struct obj *otmp = invent;
int uflg = 0;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (allowgold)
ilets[iletct++] = '$';
1993-03-21 12:45:37 +03:00
ilets[iletct] = 0;
1997-10-19 20:56:41 +04:00
while (otmp) {
if (!strchr(ilets, otmp->olet)) {
1993-03-21 12:45:37 +03:00
ilets[iletct++] = otmp->olet;
ilets[iletct] = 0;
}
1997-10-19 20:56:41 +04:00
if (otmp->unpaid)
uflg = 1;
1993-03-21 12:45:37 +03:00
otmp = otmp->nobj;
}
ilets[iletct++] = ' ';
1997-10-19 20:56:41 +04:00
if (uflg)
ilets[iletct++] = 'u';
if (invent)
ilets[iletct++] = 'a';
1993-03-21 12:45:37 +03:00
ilets[iletct] = 0;
}
pline("What kinds of thing do you want to %s? [%s] ",
1997-10-19 20:56:41 +04:00
word, ilets);
1993-03-21 12:45:37 +03:00
getlin(buf);
1997-10-19 20:56:41 +04:00
if (buf[0] == '\033') {
1993-03-21 12:45:37 +03:00
clrlin();
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
ip = buf;
olets[0] = 0;
1997-10-19 20:56:41 +04:00
while ((sym = *ip++) != '\0') {
if (sym == ' ')
continue;
if (sym == '$') {
if (allowgold == 1)
(*fn) (mkgoldobj(u.ugold));
else if (!u.ugold)
1993-03-21 12:45:37 +03:00
pline("You have no gold.");
allowgold = 2;
1997-10-19 20:56:41 +04:00
} else if (sym == 'a' || sym == 'A')
allflag = TRUE;
else if (sym == 'u' || sym == 'U')
ckfn = ckunpaid;
else if (strchr("!%?[()=*/\"0", sym)) {
if (!strchr(olets, sym)) {
1993-03-21 12:45:37 +03:00
olets[oletct++] = sym;
olets[oletct] = 0;
}
1997-10-19 20:56:41 +04:00
} else
pline("You don't have any %c's.", sym);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (allowgold == 2 && !oletct)
return (1); /* he dropped gold (or at least tried to) */
1993-03-21 12:45:37 +03:00
else
1997-10-19 20:56:41 +04:00
return (askchain(invent, olets, allflag, fn, ckfn, max));
1993-03-21 12:45:37 +03:00
}
/*
* Walk through the chain starting at objchn and ask for all objects
* with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
* whether the action in question (i.e., fn) has to be performed.
* If allflag then no questions are asked. Max gives the max nr of
* objects to be treated. Return the number of objects treated.
*/
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
askchain(objchn, olets, allflag, fn, ckfn, max)
1997-10-19 20:56:41 +04:00
struct obj *objchn;
char *olets;
int allflag;
int (*fn) __P((struct obj *));
int (*ckfn) __P((struct obj *));
int max;
{
struct obj *otmp, *otmp2;
char sym, ilet;
int cnt = 0;
ilet = 'a' - 1;
for (otmp = objchn; otmp; otmp = otmp2) {
if (ilet == 'z')
ilet = 'A';
else
ilet++;
1993-03-21 12:45:37 +03:00
otmp2 = otmp->nobj;
1997-10-19 20:56:41 +04:00
if (olets && *olets && !strchr(olets, otmp->olet))
continue;
if (ckfn && !(*ckfn) (otmp))
continue;
if (!allflag) {
1993-03-21 12:45:37 +03:00
pline(xprname(otmp, ilet));
addtopl(" [nyaq]? ");
sym = readchar();
1997-10-19 20:56:41 +04:00
} else
sym = 'y';
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
switch (sym) {
1993-03-21 12:45:37 +03:00
case 'a':
allflag = 1;
case 'y':
1997-10-19 20:56:41 +04:00
cnt += (*fn) (otmp);
if (--max == 0)
goto ret;
1993-03-21 12:45:37 +03:00
case 'n':
default:
break;
case 'q':
goto ret;
}
}
pline(cnt ? "That was all." : "No applicable objects.");
ret:
1997-10-19 20:56:41 +04:00
return (cnt);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
char
obj_to_let(obj) /* should of course only be called for things
* in invent */
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
char ilet;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (flags.invlet_constant)
return (obj->invlet);
1993-03-21 12:45:37 +03:00
ilet = 'a';
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
if (++ilet > 'z')
ilet = 'A';
return (otmp ? ilet : NOINVSYM);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
prinv(obj)
1997-10-19 20:56:41 +04:00
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
pline(xprname(obj, obj_to_let(obj)));
}
1997-10-19 20:56:41 +04:00
static char *
xprname(obj, let)
struct obj *obj;
char let;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
static char li[BUFSZ];
1993-03-21 12:45:37 +03:00
(void) sprintf(li, "%c - %s.",
1997-10-19 20:56:41 +04:00
flags.invlet_constant ? obj->invlet : let,
doname(obj));
return (li);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
ddoinv()
{
doinv((char *) 0);
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* called with 0 or "": all objects in inventory */
/* otherwise: all objects with (serial) letter in lets */
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
doinv(lets)
1997-10-19 20:56:41 +04:00
char *lets;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp;
char ilet;
int ct = 0;
char any[BUFSZ];
1993-03-21 12:45:37 +03:00
morc = 0; /* just to be sure */
1997-10-19 20:56:41 +04:00
if (!invent) {
1993-03-21 12:45:37 +03:00
pline("Not carrying anything.");
return;
}
cornline(0, (char *) 0);
ilet = 'a';
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp; otmp = otmp->nobj) {
if (flags.invlet_constant)
ilet = otmp->invlet;
if (!lets || !*lets || strchr(lets, ilet)) {
cornline(1, xprname(otmp, ilet));
any[ct++] = ilet;
}
if (!flags.invlet_constant)
if (++ilet > 'z')
ilet = 'A';
1993-03-21 12:45:37 +03:00
}
any[ct] = 0;
cornline(2, any);
}
1997-10-19 20:56:41 +04:00
int
dotypeinv()
{ /* free after Robert Viduya */
/* Changed to one type only, so he doesnt have to type cr */
char c, ilet;
char stuff[BUFSZ];
int stct;
struct obj *otmp;
boolean billx = inshop() && doinvbill(0);
boolean unpd = FALSE;
1993-03-21 12:45:37 +03:00
if (!invent && !u.ugold && !billx) {
1997-10-19 20:56:41 +04:00
pline("You aren't carrying anything.");
return (0);
1993-03-21 12:45:37 +03:00
}
stct = 0;
1997-10-19 20:56:41 +04:00
if (u.ugold)
stuff[stct++] = '$';
1993-03-21 12:45:37 +03:00
stuff[stct] = 0;
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp; otmp = otmp->nobj) {
if (!strchr(stuff, otmp->olet)) {
stuff[stct++] = otmp->olet;
stuff[stct] = 0;
}
if (otmp->unpaid)
unpd = TRUE;
}
if (unpd)
stuff[stct++] = 'u';
if (billx)
stuff[stct++] = 'x';
1993-03-21 12:45:37 +03:00
stuff[stct] = 0;
1997-10-19 20:56:41 +04:00
if (stct > 1) {
pline("What type of object [%s] do you want an inventory of? ",
stuff);
c = readchar();
if (strchr(quitchars, c))
return (0);
1993-03-21 12:45:37 +03:00
} else
1997-10-19 20:56:41 +04:00
c = stuff[0];
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (c == '$')
return (doprgold());
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (c == 'x' || c == 'X') {
if (billx)
(void) doinvbill(1);
else
pline("No used-up objects on the shopping bill.");
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if ((c == 'u' || c == 'U') && !unpd) {
1993-03-21 12:45:37 +03:00
pline("You are not carrying any unpaid objects.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
stct = 0;
ilet = 'a';
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp; otmp = otmp->nobj) {
if (flags.invlet_constant)
ilet = otmp->invlet;
if (c == otmp->olet || (c == 'u' && otmp->unpaid))
stuff[stct++] = ilet;
if (!flags.invlet_constant)
if (++ilet > 'z')
ilet = 'A';
1993-03-21 12:45:37 +03:00
}
stuff[stct] = '\0';
1997-10-19 20:56:41 +04:00
if (stct == 0)
1993-03-21 12:45:37 +03:00
pline("You have no such objects.");
else
1997-10-19 20:56:41 +04:00
doinv(stuff);
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* look at what is here */
1997-10-19 20:56:41 +04:00
int
dolook()
{
struct obj *otmp = NULL, *otmp0 = NULL;
struct gold *gold = NULL;
const char *verb = Blind ? "feel" : "see";
1997-10-19 20:56:41 +04:00
int ct = 0;
if (!u.uswallow) {
if (Blind) {
pline("You try to feel what is lying here on the floor.");
if (Levitation) { /* ab@unido */
pline("You cannot reach the floor!");
return (1);
}
}
otmp0 = o_at(u.ux, u.uy);
gold = g_at(u.ux, u.uy);
}
if (u.uswallow || (!otmp0 && !gold)) {
pline("You %s no objects here.", verb);
return (!!Blind);
}
cornline(0, "Things that are here:");
for (otmp = otmp0; otmp; otmp = otmp->nobj) {
if (otmp->ox == u.ux && otmp->oy == u.uy) {
ct++;
cornline(1, doname(otmp));
if (Blind && otmp->otyp == DEAD_COCKATRICE && !uarmg) {
pline("Touching the dead cockatrice is a fatal mistake ...");
pline("You die ...");
killer = "dead cockatrice";
done("died");
}
}
}
if (gold) {
char gbuf[30];
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
(void) sprintf(gbuf, "%ld gold piece%s",
gold->amount, plur(gold->amount));
if (!ct++)
pline("You %s here %s.", verb, gbuf);
else
cornline(1, gbuf);
}
if (ct == 1 && !gold) {
pline("You %s here %s.", verb, doname(otmp0));
cornline(3, (char *) 0);
}
if (ct > 1)
cornline(2, (char *) 0);
return (!!Blind);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
stackobj(obj)
struct obj *obj;
{
struct obj *otmp = fobj;
for (otmp = fobj; otmp; otmp = otmp->nobj)
if (otmp != obj)
if (otmp->ox == obj->ox && otmp->oy == obj->oy &&
merged(obj, otmp, 1))
return;
1993-03-21 12:45:37 +03:00
}
/* merge obj with otmp and delete obj if types agree */
1997-10-19 20:56:41 +04:00
int
merged(otmp, obj, lose)
struct obj *otmp, *obj;
int lose;
1997-10-19 20:56:41 +04:00
{
if (obj->otyp == otmp->otyp &&
obj->unpaid == otmp->unpaid &&
obj->spe == otmp->spe &&
obj->dknown == otmp->dknown &&
obj->cursed == otmp->cursed &&
(strchr("%*?!", obj->olet) ||
(obj->known == otmp->known &&
(obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)))) {
1993-03-21 12:45:37 +03:00
otmp->quan += obj->quan;
otmp->owt += obj->owt;
1997-10-19 20:56:41 +04:00
if (lose)
freeobj(obj);
obfree(obj, otmp); /* free(obj), bill->otmp */
return (1);
} else
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
static long goldcounted;
1993-03-21 12:45:37 +03:00
/*
* Gold is no longer displayed; in fact, when you have a lot of money,
* it may take a while before you have counted it all.
* [Bug: d$ and pickup still tell you how much it was.]
*/
1997-10-19 20:56:41 +04:00
int
countgold()
{
if ((goldcounted += 100 * (u.ulevel + 1)) >= u.ugold) {
long eps = 0;
if (!rn2(2))
eps = rnd((int) (u.ugold / 100 + 1));
1993-03-21 12:45:37 +03:00
pline("You probably have about %ld gold pieces.",
1997-10-19 20:56:41 +04:00
u.ugold + eps);
return (0); /* done */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
return (1); /* continue */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
doprgold()
{
if (!u.ugold)
1993-03-21 12:45:37 +03:00
pline("You do not carry any gold.");
1997-10-19 20:56:41 +04:00
else if (u.ugold <= 500)
1993-03-21 12:45:37 +03:00
pline("You are carrying %ld gold pieces.", u.ugold);
else {
pline("You sit down in order to count your gold pieces.");
goldcounted = 500;
occupation = countgold;
occtxt = "counting your gold";
}
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
/* --- end of gold counting section --- */
1997-10-19 20:56:41 +04:00
int
doprwep()
{
if (!uwep)
pline("You are empty handed.");
else
prinv(uwep);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
doprarm()
{
if (!uarm && !uarmg && !uarms && !uarmh)
1993-03-21 12:45:37 +03:00
pline("You are not wearing any armor.");
else {
1997-10-19 20:56:41 +04:00
char lets[6];
int ct = 0;
if (uarm)
lets[ct++] = obj_to_let(uarm);
if (uarm2)
lets[ct++] = obj_to_let(uarm2);
if (uarmh)
lets[ct++] = obj_to_let(uarmh);
if (uarms)
lets[ct++] = obj_to_let(uarms);
if (uarmg)
lets[ct++] = obj_to_let(uarmg);
1993-03-21 12:45:37 +03:00
lets[ct] = 0;
doinv(lets);
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
doprring()
{
if (!uleft && !uright)
1993-03-21 12:45:37 +03:00
pline("You are not wearing any rings.");
else {
1997-10-19 20:56:41 +04:00
char lets[3];
int ct = 0;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (uleft)
lets[ct++] = obj_to_let(uleft);
if (uright)
lets[ct++] = obj_to_let(uright);
1993-03-21 12:45:37 +03:00
lets[ct] = 0;
doinv(lets);
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
digit(c)
char c;
{
return (c >= '0' && c <= '9');
1993-03-21 12:45:37 +03:00
}