hack.invent.c revision 1.6 1 1.6 fair /* $NetBSD: hack.invent.c,v 1.6 1997/10/23 07:05:55 fair Exp $ */
2 1.5 christos
3 1.3 mycroft /*
4 1.3 mycroft * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5 1.3 mycroft */
6 1.3 mycroft
7 1.5 christos #include <sys/cdefs.h>
8 1.3 mycroft #ifndef lint
9 1.6 fair __RCSID("$NetBSD: hack.invent.c,v 1.6 1997/10/23 07:05:55 fair Exp $");
10 1.5 christos #endif /* not lint */
11 1.1 cgd
12 1.5 christos #include <stdlib.h>
13 1.5 christos #include "hack.h"
14 1.5 christos #include "extern.h"
15 1.1 cgd
16 1.1 cgd #ifndef NOWORM
17 1.1 cgd #include "def.wseg.h"
18 1.5 christos #endif /* NOWORM */
19 1.1 cgd
20 1.1 cgd #define NOINVSYM '#'
21 1.1 cgd
22 1.5 christos static int lastinvnr = 51; /* 0 ... 51 */
23 1.5 christos
24 1.5 christos static void assigninvlet __P((struct obj *));
25 1.5 christos static char *xprname __P((struct obj *, char));
26 1.5 christos
27 1.5 christos static void
28 1.1 cgd assigninvlet(otmp)
29 1.5 christos struct obj *otmp;
30 1.1 cgd {
31 1.5 christos boolean inuse[52];
32 1.5 christos int i;
33 1.5 christos struct obj *obj;
34 1.5 christos
35 1.5 christos for (i = 0; i < 52; i++)
36 1.5 christos inuse[i] = FALSE;
37 1.5 christos for (obj = invent; obj; obj = obj->nobj)
38 1.5 christos if (obj != otmp) {
39 1.5 christos i = obj->invlet;
40 1.5 christos if ('a' <= i && i <= 'z')
41 1.5 christos inuse[i - 'a'] = TRUE;
42 1.5 christos else if ('A' <= i && i <= 'Z')
43 1.5 christos inuse[i - 'A' + 26] = TRUE;
44 1.5 christos if (i == otmp->invlet)
45 1.5 christos otmp->invlet = 0;
46 1.5 christos }
47 1.5 christos if ((i = otmp->invlet) &&
48 1.1 cgd (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
49 1.1 cgd return;
50 1.5 christos for (i = lastinvnr + 1; i != lastinvnr; i++) {
51 1.5 christos if (i == 52) {
52 1.5 christos i = -1;
53 1.5 christos continue;
54 1.5 christos }
55 1.5 christos if (!inuse[i])
56 1.5 christos break;
57 1.1 cgd }
58 1.1 cgd otmp->invlet = (inuse[i] ? NOINVSYM :
59 1.5 christos (i < 26) ? ('a' + i) : ('A' + i - 26));
60 1.1 cgd lastinvnr = i;
61 1.1 cgd }
62 1.1 cgd
63 1.5 christos struct obj *
64 1.1 cgd addinv(obj)
65 1.5 christos struct obj *obj;
66 1.1 cgd {
67 1.5 christos struct obj *otmp;
68 1.1 cgd
69 1.1 cgd /* merge or attach to end of chain */
70 1.5 christos if (!invent) {
71 1.1 cgd invent = obj;
72 1.1 cgd otmp = 0;
73 1.1 cgd } else
74 1.5 christos for (otmp = invent; /* otmp */ ; otmp = otmp->nobj) {
75 1.5 christos if (merged(otmp, obj, 0))
76 1.5 christos return (otmp);
77 1.5 christos if (!otmp->nobj) {
78 1.5 christos otmp->nobj = obj;
79 1.5 christos break;
80 1.5 christos }
81 1.1 cgd }
82 1.1 cgd obj->nobj = 0;
83 1.1 cgd
84 1.5 christos if (flags.invlet_constant) {
85 1.1 cgd assigninvlet(obj);
86 1.1 cgd /*
87 1.1 cgd * The ordering of the chain is nowhere significant
88 1.1 cgd * so in case you prefer some other order than the
89 1.1 cgd * historical one, change the code below.
90 1.1 cgd */
91 1.5 christos if (otmp) { /* find proper place in chain */
92 1.1 cgd otmp->nobj = 0;
93 1.5 christos if ((invent->invlet ^ 040) > (obj->invlet ^ 040)) {
94 1.1 cgd obj->nobj = invent;
95 1.1 cgd invent = obj;
96 1.1 cgd } else
97 1.5 christos for (otmp = invent;; otmp = otmp->nobj) {
98 1.5 christos if (!otmp->nobj ||
99 1.5 christos (otmp->nobj->invlet ^ 040) > (obj->invlet ^ 040)) {
100 1.5 christos obj->nobj = otmp->nobj;
101 1.5 christos otmp->nobj = obj;
102 1.5 christos break;
103 1.5 christos }
104 1.5 christos }
105 1.1 cgd }
106 1.1 cgd }
107 1.5 christos return (obj);
108 1.1 cgd }
109 1.1 cgd
110 1.5 christos void
111 1.1 cgd useup(obj)
112 1.5 christos struct obj *obj;
113 1.1 cgd {
114 1.5 christos if (obj->quan > 1) {
115 1.1 cgd obj->quan--;
116 1.1 cgd obj->owt = weight(obj);
117 1.1 cgd } else {
118 1.1 cgd setnotworn(obj);
119 1.1 cgd freeinv(obj);
120 1.1 cgd obfree(obj, (struct obj *) 0);
121 1.1 cgd }
122 1.1 cgd }
123 1.1 cgd
124 1.5 christos void
125 1.1 cgd freeinv(obj)
126 1.5 christos struct obj *obj;
127 1.1 cgd {
128 1.5 christos struct obj *otmp;
129 1.1 cgd
130 1.5 christos if (obj == invent)
131 1.1 cgd invent = invent->nobj;
132 1.1 cgd else {
133 1.5 christos for (otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
134 1.5 christos if (!otmp->nobj)
135 1.5 christos panic("freeinv");
136 1.1 cgd otmp->nobj = obj->nobj;
137 1.1 cgd }
138 1.1 cgd }
139 1.1 cgd
140 1.1 cgd /* destroy object in fobj chain (if unpaid, it remains on the bill) */
141 1.5 christos void
142 1.5 christos delobj(obj)
143 1.5 christos struct obj *obj;
144 1.5 christos {
145 1.1 cgd freeobj(obj);
146 1.1 cgd unpobj(obj);
147 1.1 cgd obfree(obj, (struct obj *) 0);
148 1.1 cgd }
149 1.1 cgd
150 1.1 cgd /* unlink obj from chain starting with fobj */
151 1.5 christos void
152 1.5 christos freeobj(obj)
153 1.5 christos struct obj *obj;
154 1.5 christos {
155 1.5 christos struct obj *otmp;
156 1.1 cgd
157 1.5 christos if (obj == fobj)
158 1.5 christos fobj = fobj->nobj;
159 1.1 cgd else {
160 1.5 christos for (otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
161 1.5 christos if (!otmp)
162 1.5 christos panic("error in freeobj");
163 1.1 cgd otmp->nobj = obj->nobj;
164 1.1 cgd }
165 1.1 cgd }
166 1.1 cgd
167 1.1 cgd /* Note: freegold throws away its argument! */
168 1.5 christos void
169 1.5 christos freegold(gold)
170 1.5 christos struct gold *gold;
171 1.5 christos {
172 1.5 christos struct gold *gtmp;
173 1.1 cgd
174 1.5 christos if (gold == fgold)
175 1.5 christos fgold = gold->ngold;
176 1.1 cgd else {
177 1.5 christos for (gtmp = fgold; gtmp->ngold != gold; gtmp = gtmp->ngold)
178 1.5 christos if (!gtmp)
179 1.5 christos panic("error in freegold");
180 1.1 cgd gtmp->ngold = gold->ngold;
181 1.1 cgd }
182 1.1 cgd free((char *) gold);
183 1.1 cgd }
184 1.1 cgd
185 1.5 christos void
186 1.1 cgd deltrap(trap)
187 1.5 christos struct trap *trap;
188 1.1 cgd {
189 1.5 christos struct trap *ttmp;
190 1.1 cgd
191 1.5 christos if (trap == ftrap)
192 1.1 cgd ftrap = ftrap->ntrap;
193 1.1 cgd else {
194 1.5 christos for (ttmp = ftrap; ttmp->ntrap != trap; ttmp = ttmp->ntrap);
195 1.1 cgd ttmp->ntrap = trap->ntrap;
196 1.1 cgd }
197 1.1 cgd free((char *) trap);
198 1.1 cgd }
199 1.1 cgd
200 1.5 christos struct wseg *m_atseg;
201 1.1 cgd
202 1.5 christos struct monst *
203 1.5 christos m_at(x, y)
204 1.5 christos int x, y;
205 1.1 cgd {
206 1.5 christos struct monst *mtmp;
207 1.1 cgd #ifndef NOWORM
208 1.5 christos struct wseg *wtmp;
209 1.5 christos #endif /* NOWORM */
210 1.1 cgd
211 1.1 cgd m_atseg = 0;
212 1.5 christos for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
213 1.5 christos if (mtmp->mx == x && mtmp->my == y)
214 1.5 christos return (mtmp);
215 1.1 cgd #ifndef NOWORM
216 1.5 christos if (mtmp->wormno) {
217 1.5 christos for (wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
218 1.5 christos if (wtmp->wx == x && wtmp->wy == y) {
219 1.5 christos m_atseg = wtmp;
220 1.5 christos return (mtmp);
221 1.5 christos }
222 1.1 cgd }
223 1.5 christos #endif /* NOWORM */
224 1.1 cgd }
225 1.5 christos return (0);
226 1.1 cgd }
227 1.1 cgd
228 1.5 christos struct obj *
229 1.5 christos o_at(x, y)
230 1.5 christos int x, y;
231 1.1 cgd {
232 1.5 christos struct obj *otmp;
233 1.1 cgd
234 1.5 christos for (otmp = fobj; otmp; otmp = otmp->nobj)
235 1.5 christos if (otmp->ox == x && otmp->oy == y)
236 1.5 christos return (otmp);
237 1.5 christos return (0);
238 1.1 cgd }
239 1.1 cgd
240 1.5 christos struct obj *
241 1.5 christos sobj_at(n, x, y)
242 1.5 christos int n, x, y;
243 1.1 cgd {
244 1.5 christos struct obj *otmp;
245 1.1 cgd
246 1.5 christos for (otmp = fobj; otmp; otmp = otmp->nobj)
247 1.5 christos if (otmp->ox == x && otmp->oy == y && otmp->otyp == n)
248 1.5 christos return (otmp);
249 1.5 christos return (0);
250 1.1 cgd }
251 1.1 cgd
252 1.5 christos int
253 1.5 christos carried(obj)
254 1.5 christos struct obj *obj;
255 1.5 christos {
256 1.5 christos struct obj *otmp;
257 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj)
258 1.5 christos if (otmp == obj)
259 1.5 christos return (1);
260 1.5 christos return (0);
261 1.1 cgd }
262 1.1 cgd
263 1.5 christos int
264 1.1 cgd carrying(type)
265 1.5 christos int type;
266 1.1 cgd {
267 1.5 christos struct obj *otmp;
268 1.1 cgd
269 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj)
270 1.5 christos if (otmp->otyp == type)
271 1.5 christos return (TRUE);
272 1.5 christos return (FALSE);
273 1.1 cgd }
274 1.1 cgd
275 1.5 christos struct obj *
276 1.5 christos o_on(id, objchn)
277 1.5 christos unsigned int id;
278 1.5 christos struct obj *objchn;
279 1.5 christos {
280 1.5 christos while (objchn) {
281 1.5 christos if (objchn->o_id == id)
282 1.5 christos return (objchn);
283 1.1 cgd objchn = objchn->nobj;
284 1.1 cgd }
285 1.5 christos return ((struct obj *) 0);
286 1.1 cgd }
287 1.1 cgd
288 1.5 christos struct trap *
289 1.5 christos t_at(x, y)
290 1.5 christos int x, y;
291 1.1 cgd {
292 1.5 christos struct trap *trap = ftrap;
293 1.5 christos while (trap) {
294 1.5 christos if (trap->tx == x && trap->ty == y)
295 1.5 christos return (trap);
296 1.1 cgd trap = trap->ntrap;
297 1.1 cgd }
298 1.5 christos return (0);
299 1.1 cgd }
300 1.1 cgd
301 1.5 christos struct gold *
302 1.5 christos g_at(x, y)
303 1.5 christos int x, y;
304 1.1 cgd {
305 1.5 christos struct gold *gold = fgold;
306 1.5 christos while (gold) {
307 1.5 christos if (gold->gx == x && gold->gy == y)
308 1.5 christos return (gold);
309 1.1 cgd gold = gold->ngold;
310 1.1 cgd }
311 1.5 christos return (0);
312 1.1 cgd }
313 1.1 cgd
314 1.1 cgd /* make dummy object structure containing gold - for temporary use only */
315 1.5 christos struct obj *
316 1.1 cgd mkgoldobj(q)
317 1.5 christos long q;
318 1.1 cgd {
319 1.5 christos struct obj *otmp;
320 1.1 cgd
321 1.1 cgd otmp = newobj(0);
322 1.1 cgd /* should set o_id etc. but otmp will be freed soon */
323 1.1 cgd otmp->olet = '$';
324 1.1 cgd u.ugold -= q;
325 1.1 cgd OGOLD(otmp) = q;
326 1.1 cgd flags.botl = 1;
327 1.5 christos return (otmp);
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * getobj returns:
332 1.1 cgd * struct obj *xxx: object to do something with.
333 1.1 cgd * (struct obj *) 0 error return: no object.
334 1.1 cgd * &zeroobj explicitly no object (as in w-).
335 1.1 cgd */
336 1.5 christos struct obj *
337 1.5 christos getobj(let, word)
338 1.5 christos char *let, *word;
339 1.5 christos {
340 1.5 christos struct obj *otmp;
341 1.5 christos char ilet, ilet1, ilet2;
342 1.5 christos char buf[BUFSZ];
343 1.5 christos char lets[BUFSZ];
344 1.5 christos int foo = 0, foo2;
345 1.5 christos char *bp = buf;
346 1.5 christos xchar allowcnt = 0; /* 0, 1 or 2 */
347 1.5 christos boolean allowgold = FALSE;
348 1.5 christos boolean allowall = FALSE;
349 1.5 christos boolean allownone = FALSE;
350 1.5 christos xchar foox = 0;
351 1.5 christos long cnt;
352 1.5 christos
353 1.5 christos if (*let == '0')
354 1.5 christos let++, allowcnt = 1;
355 1.5 christos if (*let == '$')
356 1.5 christos let++, allowgold = TRUE;
357 1.5 christos if (*let == '#')
358 1.5 christos let++, allowall = TRUE;
359 1.5 christos if (*let == '-')
360 1.5 christos let++, allownone = TRUE;
361 1.5 christos if (allownone)
362 1.5 christos *bp++ = '-';
363 1.5 christos if (allowgold)
364 1.5 christos *bp++ = '$';
365 1.5 christos if (bp > buf && bp[-1] == '-')
366 1.5 christos *bp++ = ' ';
367 1.1 cgd
368 1.1 cgd ilet = 'a';
369 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj) {
370 1.5 christos if (!*let || strchr(let, otmp->olet)) {
371 1.5 christos bp[foo++] = flags.invlet_constant ? otmp->invlet : ilet;
372 1.5 christos
373 1.5 christos /* ugly check: remove inappropriate things */
374 1.5 christos if ((!strcmp(word, "take off") &&
375 1.5 christos !(otmp->owornmask & (W_ARMOR - W_ARM2)))
376 1.5 christos || (!strcmp(word, "wear") &&
377 1.5 christos (otmp->owornmask & (W_ARMOR | W_RING)))
378 1.5 christos || (!strcmp(word, "wield") &&
379 1.5 christos (otmp->owornmask & W_WEP))) {
380 1.5 christos foo--;
381 1.5 christos foox++;
382 1.5 christos }
383 1.1 cgd }
384 1.5 christos if (ilet == 'z')
385 1.5 christos ilet = 'A';
386 1.5 christos else
387 1.5 christos ilet++;
388 1.1 cgd }
389 1.1 cgd bp[foo] = 0;
390 1.5 christos if (foo == 0 && bp > buf && bp[-1] == ' ')
391 1.5 christos *--bp = 0;
392 1.5 christos (void) strcpy(lets, bp);/* necessary since we destroy buf */
393 1.5 christos if (foo > 5) { /* compactify string */
394 1.1 cgd foo = foo2 = 1;
395 1.1 cgd ilet2 = bp[0];
396 1.1 cgd ilet1 = bp[1];
397 1.5 christos while ((ilet = bp[++foo2] = bp[++foo]) != '\0') {
398 1.5 christos if (ilet == ilet1 + 1) {
399 1.5 christos if (ilet1 == ilet2 + 1)
400 1.1 cgd bp[foo2 - 1] = ilet1 = '-';
401 1.5 christos else if (ilet2 == '-') {
402 1.1 cgd bp[--foo2] = ++ilet1;
403 1.1 cgd continue;
404 1.1 cgd }
405 1.1 cgd }
406 1.1 cgd ilet2 = ilet1;
407 1.1 cgd ilet1 = ilet;
408 1.1 cgd }
409 1.1 cgd }
410 1.5 christos if (!foo && !allowall && !allowgold && !allownone) {
411 1.1 cgd pline("You don't have anything %sto %s.",
412 1.5 christos foox ? "else " : "", word);
413 1.5 christos return (0);
414 1.1 cgd }
415 1.5 christos for (;;) {
416 1.5 christos if (!buf[0])
417 1.1 cgd pline("What do you want to %s [*]? ", word);
418 1.1 cgd else
419 1.1 cgd pline("What do you want to %s [%s or ?*]? ",
420 1.5 christos word, buf);
421 1.1 cgd
422 1.1 cgd cnt = 0;
423 1.1 cgd ilet = readchar();
424 1.5 christos while (digit(ilet) && allowcnt) {
425 1.1 cgd if (cnt < 100000000)
426 1.5 christos cnt = 10 * cnt + (ilet - '0');
427 1.1 cgd else
428 1.5 christos cnt = 999999999;
429 1.1 cgd allowcnt = 2; /* signal presence of cnt */
430 1.1 cgd ilet = readchar();
431 1.1 cgd }
432 1.5 christos if (digit(ilet)) {
433 1.1 cgd pline("No count allowed with this command.");
434 1.1 cgd continue;
435 1.1 cgd }
436 1.5 christos if (strchr(quitchars, ilet))
437 1.5 christos return ((struct obj *) 0);
438 1.5 christos if (ilet == '-') {
439 1.5 christos return (allownone ? &zeroobj : (struct obj *) 0);
440 1.1 cgd }
441 1.5 christos if (ilet == '$') {
442 1.5 christos if (!allowgold) {
443 1.1 cgd pline("You cannot %s gold.", word);
444 1.1 cgd continue;
445 1.1 cgd }
446 1.5 christos if (!(allowcnt == 2 && cnt < u.ugold))
447 1.1 cgd cnt = u.ugold;
448 1.5 christos return (mkgoldobj(cnt));
449 1.1 cgd }
450 1.5 christos if (ilet == '?') {
451 1.1 cgd doinv(lets);
452 1.5 christos if (!(ilet = morc))
453 1.5 christos continue;
454 1.1 cgd /* he typed a letter (not a space) to more() */
455 1.5 christos } else if (ilet == '*') {
456 1.1 cgd doinv((char *) 0);
457 1.5 christos if (!(ilet = morc))
458 1.5 christos continue;
459 1.1 cgd /* ... */
460 1.1 cgd }
461 1.5 christos if (flags.invlet_constant) {
462 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj)
463 1.5 christos if (otmp->invlet == ilet)
464 1.5 christos break;
465 1.1 cgd } else {
466 1.5 christos if (ilet >= 'A' && ilet <= 'Z')
467 1.5 christos ilet += 'z' - 'A' + 1;
468 1.1 cgd ilet -= 'a';
469 1.5 christos for (otmp = invent; otmp && ilet;
470 1.5 christos ilet--, otmp = otmp->nobj);
471 1.1 cgd }
472 1.5 christos if (!otmp) {
473 1.1 cgd pline("You don't have that object.");
474 1.1 cgd continue;
475 1.1 cgd }
476 1.5 christos if (cnt < 0 || otmp->quan < cnt) {
477 1.1 cgd pline("You don't have that many! [You have %u]"
478 1.5 christos ,otmp->quan);
479 1.1 cgd continue;
480 1.1 cgd }
481 1.1 cgd break;
482 1.1 cgd }
483 1.5 christos if (!allowall && let && !strchr(let, otmp->olet)) {
484 1.5 christos pline("That is a silly thing to %s.", word);
485 1.5 christos return (0);
486 1.5 christos }
487 1.5 christos if (allowcnt == 2) { /* cnt given */
488 1.5 christos if (cnt == 0)
489 1.5 christos return (0);
490 1.5 christos if (cnt != otmp->quan) {
491 1.5 christos struct obj *obj;
492 1.1 cgd obj = splitobj(otmp, (int) cnt);
493 1.5 christos if (otmp == uwep)
494 1.5 christos setuwep(obj);
495 1.1 cgd }
496 1.1 cgd }
497 1.5 christos return (otmp);
498 1.1 cgd }
499 1.1 cgd
500 1.5 christos int
501 1.5 christos ckunpaid(otmp)
502 1.5 christos struct obj *otmp;
503 1.5 christos {
504 1.5 christos return (otmp->unpaid);
505 1.1 cgd }
506 1.1 cgd
507 1.1 cgd /* interactive version of getobj - used for Drop and Identify */
508 1.1 cgd /* return the number of times fn was called successfully */
509 1.5 christos int
510 1.1 cgd ggetobj(word, fn, max)
511 1.5 christos char *word;
512 1.5 christos int (*fn) __P((struct obj *));
513 1.5 christos int max;
514 1.5 christos {
515 1.5 christos char buf[BUFSZ];
516 1.5 christos char *ip;
517 1.5 christos char sym;
518 1.5 christos int oletct = 0, iletct = 0;
519 1.5 christos boolean allflag = FALSE;
520 1.5 christos char olets[20], ilets[20];
521 1.5 christos int (*ckfn) __P((struct obj *)) =
522 1.5 christos (int (*) __P((struct obj *))) 0;
523 1.5 christos xchar allowgold = (u.ugold && !strcmp(word, "drop")) ? 1 : 0; /* BAH */
524 1.5 christos if (!invent && !allowgold) {
525 1.1 cgd pline("You have nothing to %s.", word);
526 1.5 christos return (0);
527 1.1 cgd } else {
528 1.5 christos struct obj *otmp = invent;
529 1.5 christos int uflg = 0;
530 1.1 cgd
531 1.5 christos if (allowgold)
532 1.5 christos ilets[iletct++] = '$';
533 1.1 cgd ilets[iletct] = 0;
534 1.5 christos while (otmp) {
535 1.5 christos if (!strchr(ilets, otmp->olet)) {
536 1.1 cgd ilets[iletct++] = otmp->olet;
537 1.1 cgd ilets[iletct] = 0;
538 1.1 cgd }
539 1.5 christos if (otmp->unpaid)
540 1.5 christos uflg = 1;
541 1.1 cgd otmp = otmp->nobj;
542 1.1 cgd }
543 1.1 cgd ilets[iletct++] = ' ';
544 1.5 christos if (uflg)
545 1.5 christos ilets[iletct++] = 'u';
546 1.5 christos if (invent)
547 1.5 christos ilets[iletct++] = 'a';
548 1.1 cgd ilets[iletct] = 0;
549 1.1 cgd }
550 1.1 cgd pline("What kinds of thing do you want to %s? [%s] ",
551 1.5 christos word, ilets);
552 1.1 cgd getlin(buf);
553 1.5 christos if (buf[0] == '\033') {
554 1.1 cgd clrlin();
555 1.5 christos return (0);
556 1.1 cgd }
557 1.1 cgd ip = buf;
558 1.1 cgd olets[0] = 0;
559 1.5 christos while ((sym = *ip++) != '\0') {
560 1.5 christos if (sym == ' ')
561 1.5 christos continue;
562 1.5 christos if (sym == '$') {
563 1.5 christos if (allowgold == 1)
564 1.5 christos (*fn) (mkgoldobj(u.ugold));
565 1.5 christos else if (!u.ugold)
566 1.1 cgd pline("You have no gold.");
567 1.1 cgd allowgold = 2;
568 1.5 christos } else if (sym == 'a' || sym == 'A')
569 1.5 christos allflag = TRUE;
570 1.5 christos else if (sym == 'u' || sym == 'U')
571 1.5 christos ckfn = ckunpaid;
572 1.5 christos else if (strchr("!%?[()=*/\"0", sym)) {
573 1.5 christos if (!strchr(olets, sym)) {
574 1.1 cgd olets[oletct++] = sym;
575 1.1 cgd olets[oletct] = 0;
576 1.1 cgd }
577 1.5 christos } else
578 1.5 christos pline("You don't have any %c's.", sym);
579 1.1 cgd }
580 1.5 christos if (allowgold == 2 && !oletct)
581 1.5 christos return (1); /* he dropped gold (or at least tried to) */
582 1.1 cgd else
583 1.5 christos return (askchain(invent, olets, allflag, fn, ckfn, max));
584 1.1 cgd }
585 1.1 cgd
586 1.1 cgd /*
587 1.1 cgd * Walk through the chain starting at objchn and ask for all objects
588 1.1 cgd * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
589 1.1 cgd * whether the action in question (i.e., fn) has to be performed.
590 1.1 cgd * If allflag then no questions are asked. Max gives the max nr of
591 1.1 cgd * objects to be treated. Return the number of objects treated.
592 1.1 cgd */
593 1.5 christos int
594 1.1 cgd askchain(objchn, olets, allflag, fn, ckfn, max)
595 1.5 christos struct obj *objchn;
596 1.5 christos char *olets;
597 1.5 christos int allflag;
598 1.5 christos int (*fn) __P((struct obj *));
599 1.5 christos int (*ckfn) __P((struct obj *));
600 1.5 christos int max;
601 1.5 christos {
602 1.5 christos struct obj *otmp, *otmp2;
603 1.5 christos char sym, ilet;
604 1.5 christos int cnt = 0;
605 1.5 christos ilet = 'a' - 1;
606 1.5 christos for (otmp = objchn; otmp; otmp = otmp2) {
607 1.5 christos if (ilet == 'z')
608 1.5 christos ilet = 'A';
609 1.5 christos else
610 1.5 christos ilet++;
611 1.1 cgd otmp2 = otmp->nobj;
612 1.5 christos if (olets && *olets && !strchr(olets, otmp->olet))
613 1.5 christos continue;
614 1.5 christos if (ckfn && !(*ckfn) (otmp))
615 1.5 christos continue;
616 1.5 christos if (!allflag) {
617 1.1 cgd pline(xprname(otmp, ilet));
618 1.1 cgd addtopl(" [nyaq]? ");
619 1.1 cgd sym = readchar();
620 1.5 christos } else
621 1.5 christos sym = 'y';
622 1.1 cgd
623 1.5 christos switch (sym) {
624 1.1 cgd case 'a':
625 1.1 cgd allflag = 1;
626 1.1 cgd case 'y':
627 1.5 christos cnt += (*fn) (otmp);
628 1.5 christos if (--max == 0)
629 1.5 christos goto ret;
630 1.1 cgd case 'n':
631 1.1 cgd default:
632 1.1 cgd break;
633 1.1 cgd case 'q':
634 1.1 cgd goto ret;
635 1.1 cgd }
636 1.1 cgd }
637 1.1 cgd pline(cnt ? "That was all." : "No applicable objects.");
638 1.1 cgd ret:
639 1.5 christos return (cnt);
640 1.1 cgd }
641 1.1 cgd
642 1.5 christos char
643 1.5 christos obj_to_let(obj) /* should of course only be called for things
644 1.5 christos * in invent */
645 1.5 christos struct obj *obj;
646 1.1 cgd {
647 1.5 christos struct obj *otmp;
648 1.5 christos char ilet;
649 1.1 cgd
650 1.5 christos if (flags.invlet_constant)
651 1.5 christos return (obj->invlet);
652 1.1 cgd ilet = 'a';
653 1.5 christos for (otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
654 1.5 christos if (++ilet > 'z')
655 1.5 christos ilet = 'A';
656 1.5 christos return (otmp ? ilet : NOINVSYM);
657 1.1 cgd }
658 1.1 cgd
659 1.5 christos void
660 1.1 cgd prinv(obj)
661 1.5 christos struct obj *obj;
662 1.1 cgd {
663 1.1 cgd pline(xprname(obj, obj_to_let(obj)));
664 1.1 cgd }
665 1.1 cgd
666 1.5 christos static char *
667 1.5 christos xprname(obj, let)
668 1.5 christos struct obj *obj;
669 1.5 christos char let;
670 1.1 cgd {
671 1.5 christos static char li[BUFSZ];
672 1.1 cgd
673 1.1 cgd (void) sprintf(li, "%c - %s.",
674 1.5 christos flags.invlet_constant ? obj->invlet : let,
675 1.5 christos doname(obj));
676 1.5 christos return (li);
677 1.1 cgd }
678 1.1 cgd
679 1.5 christos int
680 1.1 cgd ddoinv()
681 1.1 cgd {
682 1.1 cgd doinv((char *) 0);
683 1.5 christos return (0);
684 1.1 cgd }
685 1.1 cgd
686 1.1 cgd /* called with 0 or "": all objects in inventory */
687 1.1 cgd /* otherwise: all objects with (serial) letter in lets */
688 1.5 christos void
689 1.1 cgd doinv(lets)
690 1.5 christos char *lets;
691 1.1 cgd {
692 1.5 christos struct obj *otmp;
693 1.5 christos char ilet;
694 1.5 christos int ct = 0;
695 1.5 christos char any[BUFSZ];
696 1.1 cgd
697 1.1 cgd morc = 0; /* just to be sure */
698 1.1 cgd
699 1.5 christos if (!invent) {
700 1.1 cgd pline("Not carrying anything.");
701 1.1 cgd return;
702 1.1 cgd }
703 1.1 cgd cornline(0, (char *) 0);
704 1.1 cgd ilet = 'a';
705 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj) {
706 1.5 christos if (flags.invlet_constant)
707 1.5 christos ilet = otmp->invlet;
708 1.5 christos if (!lets || !*lets || strchr(lets, ilet)) {
709 1.5 christos cornline(1, xprname(otmp, ilet));
710 1.5 christos any[ct++] = ilet;
711 1.5 christos }
712 1.5 christos if (!flags.invlet_constant)
713 1.5 christos if (++ilet > 'z')
714 1.5 christos ilet = 'A';
715 1.1 cgd }
716 1.1 cgd any[ct] = 0;
717 1.1 cgd cornline(2, any);
718 1.1 cgd }
719 1.1 cgd
720 1.5 christos int
721 1.5 christos dotypeinv()
722 1.5 christos { /* free after Robert Viduya */
723 1.5 christos /* Changed to one type only, so he doesnt have to type cr */
724 1.5 christos char c, ilet;
725 1.5 christos char stuff[BUFSZ];
726 1.5 christos int stct;
727 1.5 christos struct obj *otmp;
728 1.5 christos boolean billx = inshop() && doinvbill(0);
729 1.5 christos boolean unpd = FALSE;
730 1.1 cgd
731 1.1 cgd if (!invent && !u.ugold && !billx) {
732 1.5 christos pline("You aren't carrying anything.");
733 1.5 christos return (0);
734 1.1 cgd }
735 1.1 cgd stct = 0;
736 1.5 christos if (u.ugold)
737 1.5 christos stuff[stct++] = '$';
738 1.1 cgd stuff[stct] = 0;
739 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj) {
740 1.5 christos if (!strchr(stuff, otmp->olet)) {
741 1.5 christos stuff[stct++] = otmp->olet;
742 1.5 christos stuff[stct] = 0;
743 1.5 christos }
744 1.5 christos if (otmp->unpaid)
745 1.5 christos unpd = TRUE;
746 1.5 christos }
747 1.5 christos if (unpd)
748 1.5 christos stuff[stct++] = 'u';
749 1.5 christos if (billx)
750 1.5 christos stuff[stct++] = 'x';
751 1.1 cgd stuff[stct] = 0;
752 1.1 cgd
753 1.5 christos if (stct > 1) {
754 1.5 christos pline("What type of object [%s] do you want an inventory of? ",
755 1.5 christos stuff);
756 1.5 christos c = readchar();
757 1.5 christos if (strchr(quitchars, c))
758 1.5 christos return (0);
759 1.1 cgd } else
760 1.5 christos c = stuff[0];
761 1.1 cgd
762 1.5 christos if (c == '$')
763 1.5 christos return (doprgold());
764 1.1 cgd
765 1.5 christos if (c == 'x' || c == 'X') {
766 1.5 christos if (billx)
767 1.5 christos (void) doinvbill(1);
768 1.5 christos else
769 1.5 christos pline("No used-up objects on the shopping bill.");
770 1.5 christos return (0);
771 1.1 cgd }
772 1.5 christos if ((c == 'u' || c == 'U') && !unpd) {
773 1.1 cgd pline("You are not carrying any unpaid objects.");
774 1.5 christos return (0);
775 1.1 cgd }
776 1.1 cgd stct = 0;
777 1.1 cgd ilet = 'a';
778 1.5 christos for (otmp = invent; otmp; otmp = otmp->nobj) {
779 1.5 christos if (flags.invlet_constant)
780 1.5 christos ilet = otmp->invlet;
781 1.5 christos if (c == otmp->olet || (c == 'u' && otmp->unpaid))
782 1.5 christos stuff[stct++] = ilet;
783 1.5 christos if (!flags.invlet_constant)
784 1.5 christos if (++ilet > 'z')
785 1.5 christos ilet = 'A';
786 1.1 cgd }
787 1.1 cgd stuff[stct] = '\0';
788 1.5 christos if (stct == 0)
789 1.1 cgd pline("You have no such objects.");
790 1.1 cgd else
791 1.5 christos doinv(stuff);
792 1.1 cgd
793 1.5 christos return (0);
794 1.1 cgd }
795 1.1 cgd
796 1.1 cgd /* look at what is here */
797 1.5 christos int
798 1.5 christos dolook()
799 1.5 christos {
800 1.6 fair struct obj *otmp = NULL, *otmp0 = NULL;
801 1.6 fair struct gold *gold = NULL;
802 1.5 christos char *verb = Blind ? "feel" : "see";
803 1.5 christos int ct = 0;
804 1.5 christos
805 1.5 christos if (!u.uswallow) {
806 1.5 christos if (Blind) {
807 1.5 christos pline("You try to feel what is lying here on the floor.");
808 1.5 christos if (Levitation) { /* ab@unido */
809 1.5 christos pline("You cannot reach the floor!");
810 1.5 christos return (1);
811 1.5 christos }
812 1.5 christos }
813 1.5 christos otmp0 = o_at(u.ux, u.uy);
814 1.5 christos gold = g_at(u.ux, u.uy);
815 1.5 christos }
816 1.5 christos if (u.uswallow || (!otmp0 && !gold)) {
817 1.5 christos pline("You %s no objects here.", verb);
818 1.5 christos return (!!Blind);
819 1.5 christos }
820 1.5 christos cornline(0, "Things that are here:");
821 1.5 christos for (otmp = otmp0; otmp; otmp = otmp->nobj) {
822 1.5 christos if (otmp->ox == u.ux && otmp->oy == u.uy) {
823 1.5 christos ct++;
824 1.5 christos cornline(1, doname(otmp));
825 1.5 christos if (Blind && otmp->otyp == DEAD_COCKATRICE && !uarmg) {
826 1.5 christos pline("Touching the dead cockatrice is a fatal mistake ...");
827 1.5 christos pline("You die ...");
828 1.5 christos killer = "dead cockatrice";
829 1.5 christos done("died");
830 1.5 christos }
831 1.5 christos }
832 1.5 christos }
833 1.5 christos
834 1.5 christos if (gold) {
835 1.5 christos char gbuf[30];
836 1.1 cgd
837 1.5 christos (void) sprintf(gbuf, "%ld gold piece%s",
838 1.5 christos gold->amount, plur(gold->amount));
839 1.5 christos if (!ct++)
840 1.5 christos pline("You %s here %s.", verb, gbuf);
841 1.5 christos else
842 1.5 christos cornline(1, gbuf);
843 1.5 christos }
844 1.5 christos if (ct == 1 && !gold) {
845 1.5 christos pline("You %s here %s.", verb, doname(otmp0));
846 1.5 christos cornline(3, (char *) 0);
847 1.5 christos }
848 1.5 christos if (ct > 1)
849 1.5 christos cornline(2, (char *) 0);
850 1.5 christos return (!!Blind);
851 1.5 christos }
852 1.5 christos
853 1.5 christos void
854 1.5 christos stackobj(obj)
855 1.5 christos struct obj *obj;
856 1.5 christos {
857 1.5 christos struct obj *otmp = fobj;
858 1.5 christos for (otmp = fobj; otmp; otmp = otmp->nobj)
859 1.5 christos if (otmp != obj)
860 1.5 christos if (otmp->ox == obj->ox && otmp->oy == obj->oy &&
861 1.5 christos merged(obj, otmp, 1))
862 1.5 christos return;
863 1.1 cgd }
864 1.1 cgd
865 1.1 cgd /* merge obj with otmp and delete obj if types agree */
866 1.5 christos int
867 1.5 christos merged(otmp, obj, lose)
868 1.5 christos struct obj *otmp, *obj;
869 1.5 christos {
870 1.5 christos if (obj->otyp == otmp->otyp &&
871 1.5 christos obj->unpaid == otmp->unpaid &&
872 1.5 christos obj->spe == otmp->spe &&
873 1.5 christos obj->dknown == otmp->dknown &&
874 1.5 christos obj->cursed == otmp->cursed &&
875 1.5 christos (strchr("%*?!", obj->olet) ||
876 1.5 christos (obj->known == otmp->known &&
877 1.5 christos (obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)))) {
878 1.1 cgd otmp->quan += obj->quan;
879 1.1 cgd otmp->owt += obj->owt;
880 1.5 christos if (lose)
881 1.5 christos freeobj(obj);
882 1.5 christos obfree(obj, otmp); /* free(obj), bill->otmp */
883 1.5 christos return (1);
884 1.5 christos } else
885 1.5 christos return (0);
886 1.1 cgd }
887 1.1 cgd
888 1.5 christos static long goldcounted;
889 1.1 cgd /*
890 1.1 cgd * Gold is no longer displayed; in fact, when you have a lot of money,
891 1.1 cgd * it may take a while before you have counted it all.
892 1.1 cgd * [Bug: d$ and pickup still tell you how much it was.]
893 1.1 cgd */
894 1.5 christos int
895 1.5 christos countgold()
896 1.5 christos {
897 1.5 christos if ((goldcounted += 100 * (u.ulevel + 1)) >= u.ugold) {
898 1.5 christos long eps = 0;
899 1.5 christos if (!rn2(2))
900 1.5 christos eps = rnd((int) (u.ugold / 100 + 1));
901 1.1 cgd pline("You probably have about %ld gold pieces.",
902 1.5 christos u.ugold + eps);
903 1.5 christos return (0); /* done */
904 1.1 cgd }
905 1.5 christos return (1); /* continue */
906 1.1 cgd }
907 1.1 cgd
908 1.5 christos int
909 1.5 christos doprgold()
910 1.5 christos {
911 1.5 christos if (!u.ugold)
912 1.1 cgd pline("You do not carry any gold.");
913 1.5 christos else if (u.ugold <= 500)
914 1.1 cgd pline("You are carrying %ld gold pieces.", u.ugold);
915 1.1 cgd else {
916 1.1 cgd pline("You sit down in order to count your gold pieces.");
917 1.1 cgd goldcounted = 500;
918 1.1 cgd occupation = countgold;
919 1.1 cgd occtxt = "counting your gold";
920 1.1 cgd }
921 1.5 christos return (1);
922 1.1 cgd }
923 1.1 cgd
924 1.1 cgd /* --- end of gold counting section --- */
925 1.5 christos int
926 1.5 christos doprwep()
927 1.5 christos {
928 1.5 christos if (!uwep)
929 1.5 christos pline("You are empty handed.");
930 1.5 christos else
931 1.5 christos prinv(uwep);
932 1.5 christos return (0);
933 1.1 cgd }
934 1.1 cgd
935 1.5 christos int
936 1.5 christos doprarm()
937 1.5 christos {
938 1.5 christos if (!uarm && !uarmg && !uarms && !uarmh)
939 1.1 cgd pline("You are not wearing any armor.");
940 1.1 cgd else {
941 1.5 christos char lets[6];
942 1.5 christos int ct = 0;
943 1.1 cgd
944 1.5 christos if (uarm)
945 1.5 christos lets[ct++] = obj_to_let(uarm);
946 1.5 christos if (uarm2)
947 1.5 christos lets[ct++] = obj_to_let(uarm2);
948 1.5 christos if (uarmh)
949 1.5 christos lets[ct++] = obj_to_let(uarmh);
950 1.5 christos if (uarms)
951 1.5 christos lets[ct++] = obj_to_let(uarms);
952 1.5 christos if (uarmg)
953 1.5 christos lets[ct++] = obj_to_let(uarmg);
954 1.1 cgd lets[ct] = 0;
955 1.1 cgd doinv(lets);
956 1.1 cgd }
957 1.5 christos return (0);
958 1.1 cgd }
959 1.1 cgd
960 1.5 christos int
961 1.5 christos doprring()
962 1.5 christos {
963 1.5 christos if (!uleft && !uright)
964 1.1 cgd pline("You are not wearing any rings.");
965 1.1 cgd else {
966 1.5 christos char lets[3];
967 1.5 christos int ct = 0;
968 1.1 cgd
969 1.5 christos if (uleft)
970 1.5 christos lets[ct++] = obj_to_let(uleft);
971 1.5 christos if (uright)
972 1.5 christos lets[ct++] = obj_to_let(uright);
973 1.1 cgd lets[ct] = 0;
974 1.1 cgd doinv(lets);
975 1.1 cgd }
976 1.5 christos return (0);
977 1.1 cgd }
978 1.1 cgd
979 1.5 christos int
980 1.5 christos digit(c)
981 1.5 christos char c;
982 1.5 christos {
983 1.5 christos return (c >= '0' && c <= '9');
984 1.1 cgd }
985