Source:NetHack 5.0.0/src/o init.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to o_init.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/o_init.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	o_init.c	$NHDT-Date: 1771216675 2026/02/15 20:37:55 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.101 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. #ifndef SFCTOOL
9. staticfn void setgemprobs(d_level *);
10. staticfn void randomize_gem_colors(void);
11. staticfn void shuffle(int, int, boolean);
12. staticfn void shuffle_all(void);
13. staticfn int QSORTCALLBACK discovered_cmp(const genericptr, const genericptr);
14. staticfn char *sortloot_descr(int, char *);
15. staticfn char *disco_typename(int);
16. staticfn void disco_append_typename(char *, int);
17. staticfn void disco_fmt_uniq(int, char *outbuf) NONNULLARG2;
18. staticfn void disco_output_sorted(winid, char **, int, boolean);
19. staticfn char *oclass_to_name(char, char *);
20. 
21. #ifdef TILES_IN_GLYPHMAP
22. extern glyph_map glyphmap[MAX_GLYPH];
23. staticfn void shuffle_tiles(void);
24. 
25. /* Shuffle tile assignments to match descriptions, so a red potion isn't
26.  * displayed with a blue tile and so on.
27.  *
28.  * Tile assignments are not saved, and shouldn't be so that a game can
29.  * be resumed on an otherwise identical non-tile-using binary, so we have
30.  * to reshuffle the assignments from oc_descr_idx information when a game
31.  * is restored.  So might as well do that the first time instead of writing
32.  * another routine.
33.  */
34. staticfn void
35. shuffle_tiles(void)
36. {
37.     int i;
38.     short tmp_tilemap[2][NUM_OBJECTS];
39. 
40.     for (i = 0; i < NUM_OBJECTS; i++) {
41.         tmp_tilemap[0][i] = glyphmap[objects[i].oc_descr_idx
42.                                      + GLYPH_OBJ_OFF].tileidx;
43.         tmp_tilemap[1][i] = glyphmap[objects[i].oc_descr_idx
44.                                      + GLYPH_OBJ_PILETOP_OFF].tileidx;
45.     }
46.     for (i = 0; i < NUM_OBJECTS; i++) {
47.         glyphmap[i + GLYPH_OBJ_OFF].tileidx = tmp_tilemap[0][i];
48.         glyphmap[i + GLYPH_OBJ_PILETOP_OFF].tileidx = tmp_tilemap[1][i];
49.     }
50. }
51. #endif /* TILES_IN_GLYPHMAP */
52. 
53. staticfn void
54. setgemprobs(d_level *dlev)
55. {
56.     int j, first, lev, sum = 0;
57. 
58.     if (dlev)
59.         lev = (ledger_no(dlev) > maxledgerno()) ? maxledgerno()
60.                                                 : ledger_no(dlev);
61.     else
62.         lev = 0;
63.     first = svb.bases[GEM_CLASS];
64. 
65.     for (j = 0; j < 9 - lev / 3; j++)
66.         objects[first + j].oc_prob = 0;
67.     first += j;
68.     if (first > LAST_REAL_GEM || objects[first].oc_class != GEM_CLASS
69.         || OBJ_NAME(objects[first]) == (char *) 0) {
70.         raw_printf("Not enough gems? - first=%d j=%d LAST_GEM=%d", first, j,
71.                    LAST_REAL_GEM);
72.         wait_synch();
73.     }
74.     for (j = first; j <= LAST_REAL_GEM; j++)
75.         objects[j].oc_prob = (171 + j - first) / (LAST_REAL_GEM + 1 - first);
76. 
77.     /* recompute GEM_CLASS total oc_prob - including rocks/stones */
78.     for (j = svb.bases[GEM_CLASS]; j < svb.bases[GEM_CLASS + 1]; j++)
79.         sum += objects[j].oc_prob;
80.     go.oclass_prob_totals[GEM_CLASS] = sum;
81. }
82. 
83. /* some gems can have different colors */
84. staticfn void
85. randomize_gem_colors(void)
86. {
87. #define COPY_OBJ_DESCR(o_dst, o_src) \
88.     o_dst.oc_descr_idx = o_src.oc_descr_idx, o_dst.oc_color = o_src.oc_color
89.     if (rn2(2)) { /* change turquoise from green to blue? */
90.         COPY_OBJ_DESCR(objects[TURQUOISE], objects[SAPPHIRE]);
91.     }
92.     if (rn2(2)) { /* change aquamarine from green to blue? */
93.         COPY_OBJ_DESCR(objects[AQUAMARINE], objects[SAPPHIRE]);
94.     }
95.     switch (rn2(4)) { /* change fluorite from violet? */
96.     case 0:
97.         break;
98.     case 1: /* blue */
99.         COPY_OBJ_DESCR(objects[FLUORITE], objects[SAPPHIRE]);
100.         break;
101.     case 2: /* white */
102.         COPY_OBJ_DESCR(objects[FLUORITE], objects[DIAMOND]);
103.         break;
104.     case 3: /* green */
105.         COPY_OBJ_DESCR(objects[FLUORITE], objects[EMERALD]);
106.         break;
107.     }
108. #undef COPY_OBJ_DESCR
109. }
110. 
111. /* shuffle descriptions on objects o_low to o_high */
112. staticfn void
113. shuffle(int o_low, int o_high, boolean domaterial)
114. {
115.     int i, j, num_to_shuffle;
116.     short sw;
117.     int color;
118. 
119.     for (num_to_shuffle = 0, j = o_low; j <= o_high; j++)
120.         if (!objects[j].oc_name_known)
121.             num_to_shuffle++;
122.     if (num_to_shuffle < 2)
123.         return;
124. 
125.     for (j = o_low; j <= o_high; j++) {
126.         if (objects[j].oc_name_known)
127.             continue;
128.         do
129.             i = j + rn2(o_high - j + 1);
130.         while (objects[i].oc_name_known);
131.         sw = objects[j].oc_descr_idx;
132.         objects[j].oc_descr_idx = objects[i].oc_descr_idx;
133.         objects[i].oc_descr_idx = sw;
134.         sw = objects[j].oc_tough;
135.         objects[j].oc_tough = objects[i].oc_tough;
136.         objects[i].oc_tough = sw;
137.         color = objects[j].oc_color;
138.         objects[j].oc_color = objects[i].oc_color;
139.         objects[i].oc_color = color;
140. 
141.         /* shuffle material */
142.         if (domaterial) {
143.             sw = objects[j].oc_material;
144.             objects[j].oc_material = objects[i].oc_material;
145.             objects[i].oc_material = sw;
146.         }
147.     }
148. }
149. 
150. void
151. init_objects(void)
152. {
153.     int i, first, last, prevoclass;
154.     char oclass;
155. 
156.     for (i = 0; i <= MAXOCLASSES; i++) {
157.         svb.bases[i] = 0;
158.         if (i > 0 && i < MAXOCLASSES && objects[i].oc_class != i)
159.             panic(
160.               "init_objects: class for generic object #%d doesn't match (%d)",
161.                   i, objects[i].oc_class);
162.     }
163.     /* initialize object descriptions */
164.     for (i = 0; i < NUM_OBJECTS; i++)
165.         objects[i].oc_name_idx = objects[i].oc_descr_idx = i;
166.     /* init base; if probs given check that they add up to 1000,
167.        otherwise compute probs */
168.     first = MAXOCLASSES;
169.     prevoclass = -1;
170.     while (first < NUM_OBJECTS) {
171.         oclass = objects[first].oc_class;
172.         /*
173.          * objects[] sanity check:  must be in ascending oc_class order to
174.          * be able to use bases[class+1]-1 for the end of a class's range.
175.          * Also catches a non-contiguous class because reverting to any
176.          * earlier class would involve switching back to a lower class
177.          * number after having moved on to one or more other classes.
178.          */
179.         if ((int) oclass < prevoclass)
180.             panic("objects[%d] class #%d not in order!", first, oclass);
181. 
182.         last = first + 1;
183.         while (last < NUM_OBJECTS && objects[last].oc_class == oclass)
184.             last++;
185.         svb.bases[(int) oclass] = first;
186. 
187.         if (oclass == GEM_CLASS) {
188.             setgemprobs((d_level *) 0);
189.             randomize_gem_colors();
190.         }
191.         first = last;
192.         prevoclass = (int) oclass;
193.     }
194.     /* extra entry allows deriving the range of a class via
195.        bases[class] through bases[class+1]-1 for all classes
196.        (except for ILLOBJ_CLASS which is separated from WEAPON_CLASS
197.        by generic objects); second extra entry is to prevent an
198.        unexplained crash in doclassdisco(), where the code ended up
199.        attempting to process non-existent class MAXOCLASSES; the
200.        [MAXOCLASSES+1] element gives that non-class 0 objects
201.        when traversing objects[] from bases[X] through bases[X+1]-1 */
202.     svb.bases[MAXOCLASSES] = svb.bases[MAXOCLASSES + 1] = NUM_OBJECTS;
203.     /* hypothetically someone might remove all objects of some class,
204.        or be adding a new class and not populated it yet, leaving gaps
205.        in bases[]; guarantee that there are no such gaps */
206.     for (last = MAXOCLASSES - 1; last >= 0; --last)
207.         if (!svb.bases[last])
208.             svb.bases[last] = svb.bases[last + 1];
209. 
210.     /* check objects[].oc_name_known */
211.     for (i = MAXOCLASSES; i < NUM_OBJECTS; ++i) {
212.         int nmkn = objects[i].oc_name_known != 0;
213. 
214.         if (!OBJ_DESCR(objects[i]) ^ nmkn) {
215.             if (iflags.sanity_check) {
216.                 impossible(
217.                     "obj #%d (%s) name is %s despite%s alternate description",
218.                            i, OBJ_NAME(objects[i]),
219.                            nmkn ? "pre-known" : "not known",
220.                            nmkn ? "" : " no");
221.             }
222.             /* repair the mistake and keep going */
223.             objects[i].oc_name_known = nmkn ? 0 : 1;
224.         }
225.     }
226.     /* compute oclass_prob_totals */
227.     init_oclass_probs();
228. 
229.     /* shuffle descriptions */
230.     shuffle_all();
231. #ifdef TILES_IN_GLYPHMAP
232.     shuffle_tiles();
233. #endif
234.     objects[WAN_NOTHING].oc_dir = rn2(2) ? NODIR : IMMEDIATE;
235. }
236. 
237. /* Compute the total probability of each object class.
238.  * Assumes svb.bases[] has already been set. */
239. void
240. init_oclass_probs(void)
241. {
242.     int i;
243.     short sum;
244.     int oclass;
245.     for (oclass = 0; oclass < MAXOCLASSES; ++oclass) {
246.         sum = 0;
247.         /* note: for ILLOBJ_CLASS, bases[oclass+1]-1 isn't the last item
248.            in the class; but all the generic items have probability 0 so
249.            adding them to 'sum' has no impact */
250.         for (i = svb.bases[oclass]; i < svb.bases[oclass + 1]; ++i) {
251.             sum += objects[i].oc_prob;
252.         }
253.         if (sum <= 0 && oclass != ILLOBJ_CLASS
254.             && svb.bases[oclass] != svb.bases[oclass + 1]) {
255.             impossible("%s (%d) probability total for oclass %d",
256.                        !sum ? "zero" : "negative", sum, oclass);
257.             /* gracefully fail by setting all members of this class to 1 */
258.             for (i = svb.bases[oclass]; i < svb.bases[oclass + 1]; ++i) {
259.                 objects[i].oc_prob = 1;
260.                 sum++;
261.             }
262.         }
263.         go.oclass_prob_totals[oclass] = sum;
264.     }
265. }
266. 
267. /* retrieve the range of objects that otyp shares descriptions with */
268. void
269. obj_shuffle_range(
270.     int otyp,         /* input: representative item */
271.     int *lo_p, int *hi_p) /* output: range that item belongs among */
272. {
273.     int i, ocls = objects[otyp].oc_class;
274. 
275.     /* default is just the object itself */
276.     *lo_p = *hi_p = otyp;
277. 
278.     switch (ocls) {
279.     case ARMOR_CLASS:
280.         if (otyp >= HELMET && otyp <= HELM_OF_TELEPATHY)
281.             *lo_p = HELMET, *hi_p = HELM_OF_TELEPATHY;
282.         else if (otyp >= LEATHER_GLOVES && otyp <= GAUNTLETS_OF_DEXTERITY)
283.             *lo_p = LEATHER_GLOVES, *hi_p = GAUNTLETS_OF_DEXTERITY;
284.         else if (otyp >= CLOAK_OF_PROTECTION && otyp <= CLOAK_OF_DISPLACEMENT)
285.             *lo_p = CLOAK_OF_PROTECTION, *hi_p = CLOAK_OF_DISPLACEMENT;
286.         else if (otyp >= SPEED_BOOTS && otyp <= LEVITATION_BOOTS)
287.             *lo_p = SPEED_BOOTS, *hi_p = LEVITATION_BOOTS;
288.         break;
289.     case POTION_CLASS:
290.         /* potion of water has the only fixed description */
291.         *lo_p = svb.bases[POTION_CLASS];
292.         *hi_p = POT_WATER - 1;
293.         break;
294.     case AMULET_CLASS:
295.     case SCROLL_CLASS:
296.     case SPBOOK_CLASS:
297.         /* exclude non-magic types and also unique ones */
298.         *lo_p = svb.bases[ocls];
299.         for (i = *lo_p; objects[i].oc_class == ocls; i++)
300.             if (objects[i].oc_unique || !objects[i].oc_magic)
301.                 break;
302.         *hi_p = i - 1;
303.         break;
304.     case RING_CLASS:
305.     case WAND_CLASS:
306.     case VENOM_CLASS:
307.         /* entire class */
308.         *lo_p = svb.bases[ocls];
309.         *hi_p = svb.bases[ocls + 1] - 1;
310.         break;
311.     }
312. 
313.     /* artifact checking might ask about item which isn't part of any range
314.        but fell within the classes that do have ranges specified above */
315.     if (otyp < *lo_p || otyp > *hi_p)
316.         *lo_p = *hi_p = otyp;
317.     return;
318. }
319. 
320. /* randomize object descriptions */
321. staticfn void
322. shuffle_all(void)
323. {
324.     /* entire classes; obj_shuffle_range() handles their exceptions */
325.     static char shuffle_classes[] = {
326.         AMULET_CLASS, POTION_CLASS, RING_CLASS,  SCROLL_CLASS,
327.         SPBOOK_CLASS, WAND_CLASS,   VENOM_CLASS,
328.     };
329.     /* sub-class type ranges (one item from each group) */
330.     static short shuffle_types[] = {
331.         HELMET, LEATHER_GLOVES, CLOAK_OF_PROTECTION, SPEED_BOOTS,
332.     };
333.     int first, last, idx;
334. 
335.     /* do whole classes (amulets, &c) */
336.     for (idx = 0; idx < SIZE(shuffle_classes); idx++) {
337.         obj_shuffle_range(svb.bases[(int) shuffle_classes[idx]],
338.                           &first, &last);
339.         shuffle(first, last, TRUE);
340.     }
341.     /* do type ranges (helms, &c) */
342.     for (idx = 0; idx < SIZE(shuffle_types); idx++) {
343.         obj_shuffle_range(shuffle_types[idx], &first, &last);
344.         shuffle(first, last, FALSE);
345.     }
346.     return;
347. }
348. 
349. /* Return TRUE if the provided string matches the unidentified description of
350.  * the provided object. */
351. boolean
352. objdescr_is(struct obj *obj, const char *descr)
353. {
354.     const char *objdescr;
355. 
356.     if (!obj) {
357.         impossible("objdescr_is: null obj");
358.         return FALSE;
359.     }
360. 
361.     objdescr = OBJ_DESCR(objects[obj->otyp]);
362.     if (!objdescr)
363.         return FALSE; /* no obj description, no match */
364.     return !strcmp(objdescr, descr);
365. }
366. 
367. /* level dependent initialization */
368. void
369. oinit(void)
370. {
371.     setgemprobs(&u.uz);
372. }
373. 
374. void
375. savenames(NHFILE *nhfp)
376. {
377.     int i;
378.     unsigned int len;
379. 
380.     if (update_file(nhfp)) {
381.         for (i = 0; i < (MAXOCLASSES + 2); ++i) {
382.             Sfo_int(nhfp, &svb.bases[i], "names-bases");
383.         }
384.         for (i = 0; i < NUM_OBJECTS; ++i) {
385.             Sfo_short(nhfp, &svd.disco[i], "names-disco");
386.         }
387.         for (i = 0; i < NUM_OBJECTS; ++i) {
388.             Sfo_objclass(nhfp, &objects[i], "names-objclass");
389.         }
390.     }
391.     /* as long as we use only one version of Hack we
392.        need not save oc_name and oc_descr, but we must save
393.        oc_uname for all objects */
394.     for (i = 0; i < NUM_OBJECTS; i++)
395.         if (objects[i].oc_uname) {
396.             if (update_file(nhfp)) {
397.                 len = Strlen(objects[i].oc_uname) + 1;
398.                 Sfo_unsigned(nhfp, &len, "names-len");
399.                 Sfo_char(nhfp, objects[i].oc_uname, "names-oc_uname",
400.                              (int) len);
401.             }
402.             if (release_data(nhfp)) {
403.                 free((genericptr_t) objects[i].oc_uname);
404.                 objects[i].oc_uname = 0;
405.             }
406.         }
407. }
408. #endif /* !SFCTOOL */
409. 
410. void
411. restnames(NHFILE *nhfp)
412. {
413.     int i;
414.     unsigned int len = 0;
415. 
416.     for (i = 0; i < (MAXOCLASSES + 2); ++i) {
417.         Sfi_int(nhfp, &svb.bases[i], "names-bases");
418.     }
419.     for (i = 0; i < NUM_OBJECTS; ++i) {
420.         Sfi_short(nhfp, &svd.disco[i], "names-disco");
421.     }
422.     for (i = 0; i < NUM_OBJECTS; ++i) {
423.         Sfi_objclass(nhfp, &objects[i], "names-objclass");
424.     }
425.     for (i = 0; i < NUM_OBJECTS; i++) {
426.         if (objects[i].oc_uname) {
427.             Sfi_unsigned(nhfp, &len, "names-len");
428.             objects[i].oc_uname = (char *) alloc(len);
429.             Sfi_char(nhfp, objects[i].oc_uname, "names-oc_uname", (int) len);
430.         }
431.     }
432. #ifndef SFCTOOL
433. #ifdef TILES_IN_GLYPHMAP
434.     shuffle_tiles();
435. #endif
436. #endif
437. }
438. 
439. #ifndef SFCTOOL
440. /* make the object dknown and mark it as encountered */
441. void
442. observe_object(struct obj *obj)
443. {
444.     int oindx = obj->otyp;
445. 
446.     /* skip for generic objects and for STRANGE_OBJECT */
447.     if (oindx >= FIRST_OBJECT && !Hallucination) {
448.         obj->dknown = 1;
449.         discover_object(oindx, FALSE, TRUE, FALSE);
450.     }
451. }
452. 
453. void
454. discover_object(
455.     int oindx,                   /* type of object */
456.     boolean mark_as_known,       /* discover the type */
457.     boolean mark_as_encountered, /* mark the type as having been seen/felt */
458.     boolean credit_hero)         /* exercise wisdom */
459. {
460.     if (oindx < FIRST_OBJECT) /* don't discover generic objects */
461.         return;
462. 
463.     if ((!objects[oindx].oc_name_known && mark_as_known)
464.         || (!objects[oindx].oc_encountered && mark_as_encountered)
465.         || (Role_if(PM_SAMURAI)
466.             && Japanese_item_name(oindx, (const char *) 0))) {
467.         int dindx, acls = objects[oindx].oc_class;
468. 
469.         /* Loop thru disco[] 'til we find the target (which may have been
470.            uname'd) or the next open slot; one or the other will be found
471.            before we reach the next class... */
472.         for (dindx = svb.bases[acls]; svd.disco[dindx] != 0; dindx++)
473.             if (svd.disco[dindx] == oindx)
474.                 break;
475.         svd.disco[dindx] = oindx;
476. 
477.         if (mark_as_encountered)
478.             objects[oindx].oc_encountered = 1;
479. 
480.         if (!objects[oindx].oc_name_known && mark_as_known) {
481.             objects[oindx].oc_name_known = 1;
482.             if (credit_hero)
483.                 exercise(A_WIS, TRUE);
484. 
485.             /* !in_moveloop => initial inventory,
486.                gameover => final disclosure */
487.             if (program_state.in_moveloop && !program_state.gameover) {
488.                 if (objects[oindx].oc_class == GEM_CLASS)
489.                     gem_learned(oindx); /* could affect price of unpaid gems */
490.                 update_inventory();
491.             }
492.         }
493.     }
494. }
495. 
496. /* if a class name has been cleared, we may need to purge it from disco[] */
497. void
498. undiscover_object(int oindx)
499. {
500.     if (!objects[oindx].oc_name_known && !objects[oindx].oc_encountered) {
501.         int dindx, acls = objects[oindx].oc_class;
502.         boolean found = FALSE;
503. 
504.         /* find the object; shift those behind it forward one slot */
505.         for (dindx = svb.bases[acls];
506.              dindx < NUM_OBJECTS && svd.disco[dindx] != 0
507.                  && objects[dindx].oc_class == acls;
508.              dindx++)
509.             if (found)
510.                 svd.disco[dindx - 1] = svd.disco[dindx];
511.             else if (svd.disco[dindx] == oindx)
512.                 found = TRUE;
513. 
514.         /* clear last slot */
515.         if (found)
516.             svd.disco[dindx - 1] = 0;
517.         else
518.             impossible("named object not in disco");
519. 
520.         if (objects[oindx].oc_class == GEM_CLASS)
521.             gem_learned(oindx); /* ok, it's actually been unlearned */
522.     }
523. }
524. 
525. boolean
526. interesting_to_discover(int i)
527. {
528.     /* most players who don't speak Japanese manage to figure out what
529.        gunyoki, osaku, and so forth mean, but treat them as pre-discovered
530.        to be disclosed by '\' */
531.     if (Role_if(PM_SAMURAI) && Japanese_item_name(i, (const char *) 0))
532.         return TRUE;
533. 
534.     /* Objects that were discovered without encountering them are now printed
535.        with a '*' */
536.     return (boolean) (objects[i].oc_uname != (char *) 0
537.                       || ((objects[i].oc_name_known
538.                            || objects[i].oc_encountered)
539.                           && OBJ_DESCR(objects[i]) != (char *) 0));
540. }
541. 
542. /* items that should stand out once they're known */
543. static const short uniq_objs[] = {
544.     AMULET_OF_YENDOR,
545.     /* same order as major oracularity; alphabetical when fully IDed */
546.     BELL_OF_OPENING,
547.     SPE_BOOK_OF_THE_DEAD,
548.     CANDELABRUM_OF_INVOCATION,
549. };
550. 
551. /* discoveries qsort comparison function */
552. staticfn int QSORTCALLBACK
553. discovered_cmp(const genericptr v1, const genericptr v2)
554. {
555.     const char *s1 = *(const char **) v1;
556.     const char *s2 = *(const char **) v2;
557.     /* each element starts with "* " or "  " but we don't sort by those */
558.     int res = strcmpi(s1 + 2, s2 + 2);
559. 
560.     if (res == 0) {
561.         ; /* no tie-breaker needed */
562.     }
563.     return res;
564. }
565. 
566. staticfn char *
567. sortloot_descr(int otyp, char *outbuf)
568. {
569.     Loot sl_cookie;
570.     struct obj o;
571. 
572.     o = cg.zeroobj;
573.     o.otyp = otyp;
574.     o.oclass = objects[otyp].oc_class;
575.     o.dknown = 1; /* not observe_object, this isn't a real object */
576.     o.known = (objects[otyp].oc_name_known || !objects[otyp].oc_uses_known)
577.               ? 1 : 0;
578.     o.corpsenm = NON_PM; /* suppress statue and figurine details */
579.     /* but suppressing fruit details leads to "bad fruit #0" */
580.     if (otyp == SLIME_MOLD)
581.         o.spe = svc.context.current_fruit;
582. 
583.     (void) memset((genericptr_t) &sl_cookie, 0, sizeof sl_cookie);
584.     sl_cookie.obj = (struct obj *) 0;
585.     sl_cookie.str = (char *) 0;
586. 
587.     loot_classify(&sl_cookie, &o);
588.     Sprintf(outbuf, "%02d%02d%1d ",
589.             sl_cookie.orderclass, sl_cookie.subclass, sl_cookie.disco);
590.     return outbuf;
591. }
592. #endif /* !SFCTOOL */
593. 
594. #define DISCO_BYCLASS      0 /* by discovery order within each class */
595. #define DISCO_SORTLOOT     1 /* by discovery order within each subclass */
596. #define DISCO_ALPHABYCLASS 2 /* alphabetized within each class */
597. #define DISCO_ALPHABETIZED 3 /* alphabetized across all classes */
598. /* also used in options.c (optfn_sortdiscoveries) */
599. static const char disco_order_let[] = "osca";
600. static const char *const disco_orders_descr[] = {
601.     "by order of discovery within each class",
602.     "sortloot order (by class with some sub-class groupings)",
603.     "alphabetical within each class",
604.     "alphabetical across all classes",
605.     (char *) 0
606. };
607. 
608. #ifndef SFCTOOL
609. 
610. int
611. choose_disco_sort(
612.     int mode) /* 0 => 'O' cmd, 1 => full discoveries; 2 => class disco */
613. {
614.     winid tmpwin;
615.     menu_item *selected;
616.     anything any;
617.     int i, n, choice;
618.     int clr = NO_COLOR;
619. 
620.     tmpwin = create_nhwindow(NHW_MENU);
621.     start_menu(tmpwin, MENU_BEHAVE_STANDARD);
622.     any = cg.zeroany; /* zero out all bits */
623.     for (i = 0; disco_orders_descr[i]; ++i) {
624.         any.a_int = disco_order_let[i];
625.         add_menu(tmpwin, &nul_glyphinfo, &any, (char) any.a_int,
626.                  0, ATR_NONE, clr,
627.                  disco_orders_descr[i],
628.                  (disco_order_let[i] == flags.discosort)
629.                     ? MENU_ITEMFLAGS_SELECTED
630.                     : MENU_ITEMFLAGS_NONE);
631.     }
632.     if (mode == 2) {
633.         /* called via 'm `' where full alphabetize doesn't make sense
634.            (only showing one class so can't span all classes) but the
635.            chosen sort will stick and also apply to '\' usage */
636.         add_menu_str(tmpwin, "");
637.         add_menu_str(tmpwin,
638.                      "Note: full alphabetical and alphabetical within class");
639.         add_menu_str(tmpwin,
640.                      "      are equivalent for single class discovery, but");
641.         add_menu_str(tmpwin,
642.                     "      will matter for future use of total discoveries.");
643.     }
644.     end_menu(tmpwin, "Ordering of discoveries");
645. 
646.     n = select_menu(tmpwin, PICK_ONE, &selected);
647.     destroy_nhwindow(tmpwin);
648.     if (n > 0) {
649.         choice = selected[0].item.a_int;
650.         /* skip preselected entry if we have more than one item chosen */
651.         if (n > 1 && choice == (int) flags.discosort)
652.             choice = selected[1].item.a_int;
653.         free((genericptr_t) selected);
654.         flags.discosort = choice;
655.     }
656.     return n;
657. }
658. 
659. /* augment obj_typename() with explanation of Japanese item names */
660. staticfn char *
661. disco_typename(int otyp)
662. {
663.     char *result = obj_typename(otyp);
664. 
665.     if (Role_if(PM_SAMURAI) && Japanese_item_name(otyp, (const char *) 0)) {
666.         char buf[BUFSZ];
667.         const char *actualn = (((otyp != MAGIC_HARP && otyp != WOODEN_HARP)
668.                                 || objects[otyp].oc_name_known)
669.                                ? OBJ_NAME(objects[otyp])
670.                                /* undiscovered harp (since wooden harp is
671.                                   non-magic so pre-discovered, only applies
672.                                   to magic harp and will only be seen if
673.                                   magic harp has been 'called' something) */
674.                                : "harp");
675. 
676.         if (!actualn) { /* won't happen; used to pacify static analyzer */
677.             ;
678.         } else if (strstri(result, " called")) {
679.             Sprintf(buf, " [%s] called", actualn);
680.             (void) strsubst(result, " called", buf);
681.         } else if (strstri(result, " (")) {
682.             Sprintf(buf, " [%s] (", actualn);
683.             (void) strsubst(result, " (", buf);
684.         } else {
685.             Sprintf(eos(result), " [%s]", actualn);
686.         }
687.     }
688.     return result;
689. }
690. 
691. /* append typename(dis) to buf[], possibly truncating in the process;
692.    also append price quote information if it fits */
693. staticfn void
694. disco_append_typename(char *buf, int dis)
695. {
696.     size_t len = strlen(buf);
697.     char *p, *typnm = disco_typename(dis);
698.     size_t typnm_len = strlen(typnm);
699.     char *eos;
700. 
701.     if (len + typnm_len < BUFSZ) {
702.         /* ordinary */
703.         Strcat(buf, typnm);
704.         eos = buf + len + typnm_len;
705.     } else if ((p = strrchr(typnm, '(')) != 0
706.                && p > typnm && p[-1] == ' ' && strchr(p, ')') != 0) {
707.         /* typename() returned "really long user-applied name (actual type)"
708.            and we want to truncate from "really long user-applied name" while
709.            keeping " (actual type)" intact */
710.         --p; /* back up to space in front of open paren */
711.         (void) strncat(buf, typnm, BUFSZ - 1 - (len + (unsigned) strlen(p)));
712.         Strcat(buf, p);
713.         eos = buf + strlen(buf);
714.     } else {
715.         /* unexpected; just truncate from end of typename */
716.         (void) strncat(buf, typnm, BUFSZ - 1 - len);
717.         eos = buf + strlen(buf);
718.     }
719. 
720.     append_price_quote(buf, &eos, dis);
721. }
722. 
723. /* minor fixup for Book of the Dead needed in more than one place */
724. staticfn void
725. disco_fmt_uniq(int uidx, char *outbuf)
726. {
727.     Sprintf(outbuf, "  %s", objects[uidx].oc_name_known
728.                               ? OBJ_NAME(objects[uidx])
729.                               : OBJ_DESCR(objects[uidx]));
730.     /* in the spellbooks section of main discoveries list, encountered
731.        but not fully discovered Book of the Dead is shown as
732.        "spellbook (papyrus)" like other encountered but not discovered books;
733.        in the unique/relics section we want "papyrus spellbook" instead */
734.     if (!objects[uidx].oc_name_known
735.         && objects[uidx].oc_class == SPBOOK_CLASS)
736.         Strcat(outbuf, " spellbook");
737. }
738. 
739. /* sort and output sorted_lines to window and free the lines */
740. staticfn void
741. disco_output_sorted(
742.     winid tmpwin,
743.     char **sorted_lines, int sorted_ct,
744.     boolean lootsort)
745. {
746.     char *p;
747.     int j;
748. 
749.     qsort(sorted_lines, sorted_ct, sizeof (char *), discovered_cmp);
750.     for (j = 0; j < sorted_ct; ++j) {
751.         p = sorted_lines[j];
752.         assert(p != NULL); /* pacify static analyzer */
753.         if (lootsort) {
754.             p[6] = p[0]; /* '*' or ' ' */
755.             p += 6;
756.         }
757.         putstr(tmpwin, 0, p);
758.         free(sorted_lines[j]), sorted_lines[j] = 0;
759.     }
760. }
761. 
762. /* the #known command - show discovered object types */
763. int
764. dodiscovered(void) /* free after Robert Viduya */
765. {
766.     winid tmpwin;
767.     char *s, oclass, prev_class,
768.          classes[MAXOCLASSES], buf[BUFSZ],
769.          *sorted_lines[NUM_OBJECTS]; /* overkill */
770.     const char *p;
771.     int i, dis, ct, uniq_ct, arti_ct, sorted_ct, uidx;
772.     long sortindx;  // should be ptrdiff_t, but we don't require that exists
773.     boolean alphabetized, alphabyclass, lootsort;
774. 
775.     if (!flags.discosort || !(p = strchr(disco_order_let, flags.discosort)))
776.         flags.discosort = 'o';
777. 
778.     if (iflags.menu_requested) {
779.         if (choose_disco_sort(1) < 0)
780.             return ECMD_OK;
781.     }
782.     alphabyclass = (flags.discosort == 'c');
783.     alphabetized = (flags.discosort == 'a' || alphabyclass);
784.     lootsort = (flags.discosort == 's');
785.     sortindx = strchr(disco_order_let, flags.discosort) - disco_order_let;
786. 
787.     tmpwin = create_nhwindow(NHW_TEXT);
788.     Sprintf(buf, "Discoveries, %s", disco_orders_descr[sortindx]);
789.     putstr(tmpwin, 0, buf);
790.     putstr(tmpwin, 0, "");
791. 
792.     /*
793.      * FIXME?
794.      *  relics and artifacts don't obey player's sort order even though
795.      *  the header line states that they're shown in such-and-such order.
796.      */
797. 
798.     /* gather "unique objects", also called "relics", into a pseudo-class;
799.        they'll also be displayed individually within their regular class */
800.     uniq_ct = 0;
801.     for (i = dis = 0; i < SIZE(uniq_objs); i++) {
802.         uidx = uniq_objs[i];
803.         if (objects[uidx].oc_name_known
804.             || (objects[uidx].oc_encountered && uidx != AMULET_OF_YENDOR)) {
805.             if (!dis++)
806.                 putstr(tmpwin, iflags.menu_headings.attr,
807.                        "Unique items or Relics");
808.             ++uniq_ct;
809.             disco_fmt_uniq(uidx, buf);
810.             putstr(tmpwin, 0, buf);
811.         }
812.     }
813.     /* display any known artifacts as another pseudo-class */
814.     arti_ct = disp_artifact_discoveries(tmpwin);
815. 
816.     /* several classes are omitted from packorder; one is of interest here */
817.     Strcpy(classes, flags.inv_order);
818.     if (!strchr(classes, VENOM_CLASS))
819.         (void) strkitten(classes, VENOM_CLASS); /* append char to string */
820. 
821.     ct = uniq_ct + arti_ct;
822.     sorted_ct = 0;
823.     for (s = classes; *s; s++) {
824.         oclass = *s;
825.         prev_class = oclass + 1; /* forced different from oclass */
826.         for (i = svb.bases[(int) oclass];
827.              i < NUM_OBJECTS && objects[i].oc_class == oclass; i++) {
828.             if ((dis = svd.disco[i]) != 0 && interesting_to_discover(dis)) {
829.                 ct++;
830.                 if (oclass != prev_class) {
831.                     if ((alphabyclass || lootsort) && sorted_ct) {
832.                         /* output previous class */
833.                         disco_output_sorted(tmpwin, sorted_lines, sorted_ct,
834.                                             lootsort);
835.                         sorted_ct = 0;
836.                     }
837.                     if (!alphabetized || alphabyclass) {
838.                         /* header for new class */
839.                         putstr(tmpwin, iflags.menu_headings.attr,
840.                                let_to_name(oclass, FALSE, FALSE));
841.                         prev_class = oclass;
842.                     }
843.                 }
844.                 Strcpy(buf, objects[dis].oc_encountered ? "  " : "* ");
845.                 if (lootsort)
846.                     (void) sortloot_descr(dis, &buf[2]);
847.                 disco_append_typename(buf, dis);
848. 
849.                 if (!alphabetized && !lootsort)
850.                     putstr(tmpwin, 0, buf);
851.                 else
852.                     sorted_lines[sorted_ct++] = dupstr(buf);
853.             }
854.         }
855.     }
856.     if (ct == 0) {
857.         You("haven't discovered anything yet...");
858.     } else {
859.         if (sorted_ct) {
860.             /* if we're alphabetizing by class, we've already shown the
861.                relevant header above; if we're alphabetizing across all
862.                classes, we normally don't need a header; but it we showed
863.                any unique items or any artifacts then we do need one */
864.             if ((uniq_ct || arti_ct) && alphabetized && !alphabyclass)
865.                 putstr(tmpwin, iflags.menu_headings.attr, "Discovered items");
866.             disco_output_sorted(tmpwin, sorted_lines, sorted_ct, lootsort);
867.         }
868.         display_nhwindow(tmpwin, TRUE);
869.     }
870.     destroy_nhwindow(tmpwin);
871. 
872.     return ECMD_OK;
873. }
874. 
875. /* lower case let_to_name() output, which differs from def_oc_syms[].name */
876. staticfn char *
877. oclass_to_name(char oclass, char *buf)
878. {
879.     char *s;
880. 
881.     Strcpy(buf, let_to_name(oclass, FALSE, FALSE));
882.     for (s = buf; *s; ++s)
883.         *s = lowc(*s);
884.     return buf;
885. }
886. 
887. /* the #knownclass command - show discovered object types for one class;
888.    in addition to actual object classes, supports pseudo-class 'a' for
889.    discovered artifacts and 'u' (or 'r', for "relics") for unique items */
890. int
891. doclassdisco(void)
892. {
893.     static NEARDATA const char
894.         prompt[] = "View discoveries for which sort of objects?",
895.         havent_discovered_any[] = "haven't discovered any %s yet.",
896.         unique_items[] = "unique items or relics",
897.         artifact_items[] = "artifacts";
898.     winid tmpwin = WIN_ERR;
899.     menu_item *pick_list = 0;
900.     anything any;
901.     char *s, c, oclass, menulet, allclasses[MAXOCLASSES],
902.          discosyms[3 + MAXOCLASSES + 1], buf[BUFSZ],
903.          *sorted_lines[NUM_OBJECTS]; /* overkill */
904.     const char *p;
905.     int i, ct, dis, xtras, sorted_ct, uidx;
906.     boolean traditional, alphabetized, lootsort;
907.     int clr = NO_COLOR;
908. 
909.     if (!flags.discosort || !(p = strchr(disco_order_let, flags.discosort)))
910.         flags.discosort = 'o';
911. 
912.     if (iflags.menu_requested) {
913.         if (choose_disco_sort(2) < 0)
914.             return ECMD_OK;
915.     }
916.     alphabetized = (flags.discosort == 'a' || flags.discosort == 'c');
917.     lootsort = (flags.discosort == 's');
918. 
919.     discosyms[0] = '\0';
920.     traditional = (flags.menu_style == MENU_TRADITIONAL
921.                    || flags.menu_style == MENU_COMBINATION);
922.     if (!traditional) {
923.         tmpwin = create_nhwindow(NHW_MENU);
924.         start_menu(tmpwin, MENU_BEHAVE_STANDARD);
925.     }
926.     any = cg.zeroany;
927.     menulet = 'a';
928. 
929.     /*
930.      * FIXME?
931.      *  relics and artifacts don't obey player's sort order even though
932.      *  the header line states that they're shown in such-and-such order.
933.      */
934. 
935.     /* check whether we've discovered any unique objects (primarily the
936.        invocation items; the Guidebook calls unique items "relics" but the
937.        Amulet of Yendor is unique too so we haven't made a blanket change
938.        from 'u' to 'r') */
939.     for (i = 0; i < SIZE(uniq_objs); i++) {
940.         uidx = uniq_objs[i];
941.         if (objects[uidx].oc_name_known
942.             || (objects[uidx].oc_encountered && uidx != AMULET_OF_YENDOR)) {
943.             Strcat(discosyms, "u");
944.             if (!traditional) {
945.                 any.a_int = 'u';
946.                 /* FIXME: having 'r' as an accelerator to provide an unseen
947.                    synonym works but doesn't make much sense since the main
948.                    selector is 'a' (implicit lootabc) rather than 'u' */
949.                 add_menu(tmpwin, &nul_glyphinfo, &any, menulet++, 'r',
950.                          ATR_NONE, clr, unique_items, MENU_ITEMFLAGS_NONE);
951.             }
952.             break;
953.         }
954.     }
955. 
956.     /* check whether we've discovered any artifacts */
957.     if (disp_artifact_discoveries(WIN_ERR) > 0) {
958.         Strcat(discosyms, "a");
959.         if (!traditional) {
960.             any.a_int = 'a';
961.             add_menu(tmpwin, &nul_glyphinfo, &any, menulet++, 0,
962.                      ATR_NONE, clr, artifact_items, MENU_ITEMFLAGS_NONE);
963.         }
964.     }
965. 
966.     /* collect classes with discoveries, in packorder ordering; several
967.        classes are omitted from packorder and one is of interest here */
968.     Strcpy(allclasses, flags.inv_order);
969.     if (!strchr(allclasses, VENOM_CLASS))
970.         (void) strkitten(allclasses, VENOM_CLASS); /* append char to string */
971.     /* construct discosyms[] */
972.     for (s = allclasses; *s; ++s) {
973.         oclass = *s;
974.         c = def_oc_syms[(int) oclass].sym;
975.         for (i = svb.bases[(int) oclass];
976.              i < NUM_OBJECTS && objects[i].oc_class == oclass; ++i)
977.             if ((dis = svd.disco[i]) != 0 && interesting_to_discover(dis)) {
978.                 if (!strchr(discosyms, c)) {
979.                     (void) strkitten(discosyms, c);
980.                     if (!traditional) {
981.                         any.a_int = c;
982.                         add_menu(tmpwin, &nul_glyphinfo, &any,
983.                                  menulet++, c, ATR_NONE, clr,
984.                                  oclass_to_name(oclass, buf),
985.                                  MENU_ITEMFLAGS_NONE);
986.                     }
987.                 }
988.             }
989.     }
990. 
991.     /* there might not be anything for us to do... */
992.     if (!discosyms[0]) {
993.         You(havent_discovered_any, "items");
994.         if (tmpwin != WIN_ERR)
995.             destroy_nhwindow(tmpwin);
996.         return ECMD_OK;
997.     }
998. 
999.     /* have player choose a class */
1000.     c = '\0'; /* class not chosen yet */
1001.     if (traditional) {
1002.         char allclasses_plustwo[sizeof allclasses + 3];
1003. 
1004.         /* we'll prompt even if there's only one viable class; we add all
1005.            nonviable classes as unseen acceptable choices so player can ask
1006.            for discoveries of any class whether it has discoveries or not */
1007.         Sprintf(allclasses_plustwo, "%s%c%c%c", allclasses, 'a', 'u', 'r');
1008.         for (s = allclasses_plustwo, xtras = 0; *s; ++s) {
1009.             c = strchr("aur", *s) ? *s : def_oc_syms[(int) *s].sym;
1010.             if (!strchr(discosyms, c)) {
1011.                 if (!xtras++)
1012.                     (void) strkitten(discosyms, '\033');
1013.                 (void) strkitten(discosyms, c);
1014.             }
1015.         }
1016.         /* get the class (via its symbol character) */
1017.         c = yn_function(prompt, discosyms, '\0', TRUE);
1018.         if (!c)
1019.             clear_nhwindow(WIN_MESSAGE);
1020.     } else {
1021.         /* menustyle:full or menustyle:partial */
1022.         if (!discosyms[1] && flags.menu_style == MENU_PARTIAL) {
1023.             /* only one class; menustyle:partial normally jumps past class
1024.                filtering straight to final menu so skip class filter here */
1025.             c = discosyms[0];
1026.         } else {
1027.             /* more than one choice, or menustyle:full which normally has
1028.                an intermediate class selection menu before the final menu */
1029.             end_menu(tmpwin, prompt);
1030.             i = select_menu(tmpwin, PICK_ONE, &pick_list);
1031.             if (i > 0) {
1032.                 c = pick_list[0].item.a_int;
1033.                 free((genericptr_t) pick_list);
1034.             } /* else c stays 0 */
1035.         }
1036.         destroy_nhwindow(tmpwin);
1037.     }
1038.     if (!c)
1039.         return ECMD_OK; /* player declined to make a selection */
1040. 
1041.     /*
1042.      * show discoveries for object class c
1043.      */
1044.     tmpwin = create_nhwindow(NHW_TEXT);
1045.     ct = 0;
1046.     switch (c) {
1047.     case 'u':
1048.     case 'r':
1049.         putstr(tmpwin, iflags.menu_headings.attr,
1050.                upstart(strcpy(buf, unique_items)));
1051.         for (i = 0; i < SIZE(uniq_objs); i++) {
1052.             uidx = uniq_objs[i];
1053.             if (objects[uidx].oc_name_known
1054.                 || (objects[uidx].oc_encountered
1055.                     && uidx != AMULET_OF_YENDOR)) {
1056.                 ++ct;
1057.                 disco_fmt_uniq(uidx, buf);
1058.                 putstr(tmpwin, 0, buf);
1059.             }
1060.         }
1061.         if (!ct)
1062.             You(havent_discovered_any, unique_items);
1063.         break;
1064.     case 'a':
1065.         /* note: this will work all the time for menustyle traditional
1066.            but requires at least one artifact discovery for other styles
1067.            [could fix that by forcing the 'a' choice into the pick-class
1068.            menu when running in wizard mode] */
1069.         if (wizard && y_n("Dump information about all artifacts?") == 'y') {
1070.             dump_artifact_info(tmpwin);
1071.             ct = NROFARTIFACTS; /* non-zero vs zero is what matters below */
1072.             break;
1073.         }
1074.         /* disp_artifact_discoveries() includes a header */
1075.         ct = disp_artifact_discoveries(tmpwin);
1076.         if (!ct)
1077.             You(havent_discovered_any, artifact_items);
1078.         break;
1079.     default:
1080.         oclass = def_char_to_objclass(c);
1081.         /* this should never happen but has been observed via the fuzzer */
1082.         if (oclass == MAXOCLASSES)
1083.             impossible("doclassdisco: invalid object class '%s'", visctrl(c));
1084.         Sprintf(buf, "Discovered %s in %s", let_to_name(oclass, FALSE, FALSE),
1085.                 (flags.discosort == 'o') ? "order of discovery"
1086.                 : (flags.discosort == 's') ? "'sortloot' order"
1087.                   : "alphabetical order");
1088.         putstr(tmpwin, 0, buf); /* skip iflags.menu_headings */
1089.         sorted_ct = 0;
1090.         for (i = svb.bases[(int) oclass]; i <= svb.bases[oclass + 1] - 1;
1091.              ++i) {
1092.             if ((dis = svd.disco[i]) != 0 && interesting_to_discover(dis)) {
1093.                 ++ct;
1094.                 Strcpy(buf, objects[dis].oc_encountered ? "  " : "* ");
1095.                 if (lootsort)
1096.                     (void) sortloot_descr(dis, &buf[2]);
1097.                 disco_append_typename(buf, dis);
1098. 
1099.                 if (!alphabetized && !lootsort)
1100.                     putstr(tmpwin, 0, buf);
1101.                 else
1102.                     sorted_lines[sorted_ct++] = dupstr(buf);
1103.             }
1104.         }
1105.         if (!ct) {
1106.             You(havent_discovered_any, oclass_to_name(oclass, buf));
1107.         } else if (sorted_ct) {
1108.             qsort(sorted_lines, sorted_ct, sizeof (char *), discovered_cmp);
1109.             for (i = 0; i < sorted_ct; ++i) {
1110.                 char *sl;
1111. 
1112.                 sl = sorted_lines[i];
1113.                 if (lootsort) {
1114.                     sl[6] = sl[0]; /* '*' or ' ' */
1115.                     sl += 6;
1116.                 }
1117.                 putstr(tmpwin, 0, sl);
1118.                 free(sorted_lines[i]), sorted_lines[i] = 0;
1119.             }
1120.         }
1121.         break;
1122.     }
1123.     if (ct)
1124.         display_nhwindow(tmpwin, TRUE);
1125.     destroy_nhwindow(tmpwin);
1126.     return ECMD_OK;
1127. }
1128. 
1129. /* put up nameable subset of discoveries list as a menu */
1130. void
1131. rename_disco(void)
1132. {
1133.     int i, dis;
1134.     int ct = 0, mn = 0, sl;
1135.     char *s, oclass, prev_class;
1136.     winid tmpwin;
1137.     anything any;
1138.     menu_item *selected = 0;
1139.     int clr = NO_COLOR;
1140.     char buf[BUFSZ];
1141. 
1142.     any = cg.zeroany;
1143.     tmpwin = create_nhwindow(NHW_MENU);
1144.     start_menu(tmpwin, MENU_BEHAVE_STANDARD);
1145. 
1146.     /*
1147.      * Skip the "unique objects" section (each will appear within its
1148.      * regular class if it is nameable) and the artifacts section.
1149.      * We assume that classes omitted from packorder aren't nameable
1150.      * so we skip venom too.
1151.      */
1152. 
1153.     /* for each class, show discoveries in that class */
1154.     for (s = flags.inv_order; *s; s++) {
1155.         oclass = *s;
1156.         prev_class = oclass + 1; /* forced different from oclass */
1157.         for (i = svb.bases[(int) oclass];
1158.              i < NUM_OBJECTS && objects[i].oc_class == oclass; i++) {
1159.             dis = svd.disco[i];
1160.             if (!dis || !interesting_to_discover(dis))
1161.                 continue;
1162.             ct++;
1163.             if (!objtyp_is_callable(dis))
1164.                 continue;
1165.             mn++;
1166. 
1167.             if (oclass != prev_class) {
1168.                 any.a_int = 0;
1169.                 add_menu_heading(tmpwin,
1170.                                  let_to_name(oclass, FALSE, FALSE));
1171.                 prev_class = oclass;
1172.             }
1173.             any.a_int = dis;
1174.             *buf = '\0';
1175.             disco_append_typename(buf, dis);
1176.             add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0,
1177.                      ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE);
1178.         }
1179.     }
1180.     if (ct == 0) {
1181.         You("haven't discovered anything yet...");
1182.     } else if (mn == 0) {
1183.         pline("None of your discoveries can be assigned names...");
1184.     } else {
1185.         end_menu(tmpwin, "Pick an object type to name");
1186.         dis = STRANGE_OBJECT;
1187.         sl = select_menu(tmpwin, PICK_ONE, &selected);
1188.         if (sl > 0) {
1189.             dis = selected[0].item.a_int;
1190.             free((genericptr_t) selected);
1191.         }
1192.         if (dis != STRANGE_OBJECT) {
1193.             struct obj odummy;
1194. 
1195.             odummy = cg.zeroobj;
1196.             odummy.otyp = dis;
1197.             odummy.oclass = objects[dis].oc_class;
1198.             odummy.quan = 1L;
1199.             odummy.known = !objects[dis].oc_uses_known;
1200.             odummy.dknown = 1; /* not observe_object: it isn't real */
1201.             docall(&odummy);
1202.         }
1203.     }
1204.     destroy_nhwindow(tmpwin);
1205.     return;
1206. }
1207. #endif /* !SFCTOOL */
1208. 
1209. void
1210. get_sortdisco(char *opts, boolean cnf)
1211. {
1212.     const char *p = strchr(disco_order_let, flags.discosort);
1213. 
1214.     if (!p)
1215.         flags.discosort = 'o', p = disco_order_let;
1216.     if (cnf)
1217.         Sprintf(opts, "%c", flags.discosort);
1218.     else
1219.         Strcpy(opts, disco_orders_descr[p - disco_order_let]);
1220. }
1221. /*o_init.c*/