Source:NetHack 5.0.0/src/shknam.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to shknam.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/shknam.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	shknam.c	$NHDT-Date: 1764109114 2025/11/25 22:18:34 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.86 $ */
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. /* shknam.c -- initialize a shop */
7. 
8. #include "hack.h"
9. 
10. staticfn boolean stock_room_goodpos(struct mkroom *, int, int, int, int);
11. staticfn boolean veggy_item(struct obj * obj, int);
12. staticfn int shkveg(void);
13. staticfn void mkveggy_at(int, int);
14. staticfn void mkshobj_at(const struct shclass *, int, int, boolean);
15. staticfn void nameshk(struct monst *, const char *const *);
16. staticfn int good_shopdoor(struct mkroom *, coordxy *, coordxy *);
17. staticfn int shkinit(const struct shclass *, struct mkroom *);
18. 
19. #define VEGETARIAN_CLASS (MAXOCLASSES + 1)
20. 
21. /*
22.  *  Name prefix codes:
23.  *      dash          -  female, personal name
24.  *      underscore    _  female, general name
25.  *      plus          +  male, personal name
26.  *      vertical bar  |  male, general name (implied for most of shktools)
27.  *      equals        =  gender not specified, personal name
28.  *
29.  *  Personal names do not receive the honorific prefix "Mr." or "Ms.".
30.  */
31. 
32. static const char *const shkliquors[] = {
33.     /* Ukraine */
34.     "Njezjin", "Tsjernigof", "Ossipewsk", "Gorlowka",
35.     /* Belarus */
36.     "Gomel",
37.     /* N. Russia */
38.     "Konosja", "Weliki Oestjoeg", "Syktywkar", "Sablja", "Narodnaja", "Kyzyl",
39.     /* Silezie */
40.     "Walbrzych", "Swidnica", "Klodzko", "Raciborz", "Gliwice", "Brzeg",
41.     "Krnov", "Hradec Kralove",
42.     /* Schweiz */
43.     "Leuk", "Brig", "Brienz", "Thun", "Sarnen", "Burglen", "Elm", "Flims",
44.     "Vals", "Schuls", "Zum Loch", 0
45. };
46. 
47. static const char *const shkbooks[] = {
48.     /* Eire */
49.     "Skibbereen",  "Kanturk",   "Rath Luirc",     "Ennistymon",
50.     "Lahinch",     "Kinnegad",  "Lugnaquillia",   "Enniscorthy",
51.     "Gweebarra",   "Kittamagh", "Nenagh",         "Sneem",
52.     "Ballingeary", "Kilgarvan", "Cahersiveen",    "Glenbeigh",
53.     "Kilmihil",    "Kiltamagh", "Droichead Atha", "Inniscrone",
54.     "Clonegal",    "Lisnaskea", "Culdaff",        "Dunfanaghy",
55.     "Inishbofin",  "Kesh",      0
56. };
57. 
58. static const char *const shkarmors[] = {
59.     /* Turquie */
60.     "Demirci",    "Kalecik",    "Boyabai",    "Yildizeli", "Gaziantep",
61.     "Siirt",      "Akhalataki", "Tirebolu",   "Aksaray",   "Ermenak",
62.     "Iskenderun", "Kadirli",    "Siverek",    "Pervari",   "Malasgirt",
63.     "Bayburt",    "Ayancik",    "Zonguldak",  "Balya",     "Tefenni",
64.     "Artvin",     "Kars",       "Makharadze", "Malazgirt", "Midyat",
65.     "Birecik",    "Kirikkale",  "Alaca",      "Polatli",   "Nallihan",
66.     0
67. };
68. 
69. static const char *const shkwands[] = {
70.     /* Wales */
71.     "Yr Wyddgrug", "Trallwng", "Mallwyd", "Pontarfynach", "Rhaeader",
72.     "Llandrindod", "Llanfair-ym-muallt", "Y-Fenni", "Maesteg", "Rhydaman",
73.     "Beddgelert", "Curig", "Llanrwst", "Llanerchymedd", "Caergybi",
74.     /* Scotland */
75.     "Nairn", "Turriff", "Inverurie", "Braemar", "Lochnagar", "Kerloch",
76.     "Beinn a Ghlo", "Drumnadrochit", "Morven", "Uist", "Storr",
77.     "Sgurr na Ciche", "Cannich", "Gairloch", "Kyleakin", "Dunvegan", 0
78. };
79. 
80. static const char *const shkrings[] = {
81.     /* Hollandse familienamen */
82.     "Feyfer",     "Flugi",         "Gheel",      "Havic",   "Haynin",
83.     "Hoboken",    "Imbyze",        "Juyn",       "Kinsky",  "Massis",
84.     "Matray",     "Moy",           "Olycan",     "Sadelin", "Svaving",
85.     "Tapper",     "Terwen",        "Wirix",      "Ypey",
86.     /* Skandinaviske navne */
87.     "Rastegaisa", "Varjag Njarga", "Kautekeino", "Abisko",  "Enontekis",
88.     "Rovaniemi",  "Avasaksa",      "Haparanda",  "Lulea",   "Gellivare",
89.     "Oeloe",      "Kajaani",       "Fauske",     0
90. };
91. 
92. static const char *const shkfoods[] = {
93.     /* Indonesia */
94.     "Djasinga",    "Tjibarusa",   "Tjiwidej",      "Pengalengan",
95.     "Bandjar",     "Parbalingga", "Bojolali",      "Sarangan",
96.     "Ngebel",      "Djombang",    "Ardjawinangun", "Berbek",
97.     "Papar",       "Baliga",      "Tjisolok",      "Siboga",
98.     "Banjoewangi", "Trenggalek",  "Karangkobar",   "Njalindoeng",
99.     "Pasawahan",   "Pameunpeuk",  "Patjitan",      "Kediri",
100.     "Pemboeang",   "Tringanoe",   "Makin",         "Tipor",
101.     "Semai",       "Berhala",     "Tegal",         "Samoe",
102.     0
103. };
104. 
105. static const char *const shkweapons[] = {
106.     /* Perigord */
107.     "Voulgezac",   "Rouffiac",   "Lerignac",   "Touverac",  "Guizengeard",
108.     "Melac",       "Neuvicq",    "Vanzac",     "Picq",      "Urignac",
109.     "Corignac",    "Fleac",      "Lonzac",     "Vergt",     "Queyssac",
110.     "Liorac",      "Echourgnac", "Cazelon",    "Eypau",     "Carignan",
111.     "Monbazillac", "Jonzac",     "Pons",       "Jumilhac",  "Fenouilledes",
112.     "Laguiolet",   "Saujon",     "Eymoutiers", "Eygurande", "Eauze",
113.     "Labouheyre",  0
114. };
115. 
116. static const char *const shktools[] = {
117.     /* Spmi */
118.     "Ymla", "Eed-morra", "Elan Lapinski", "Cubask", "Nieb", "Bnowr Falr",
119.     "Sperc", "Noskcirdneh", "Yawolloh", "Hyeghu", "Niskal", "Trahnil",
120.     "Htargcm", "Enrobwem", "Kachzi Rellim", "Regien", "Donmyar", "Yelpur",
121.     "Nosnehpets", "Stewe", "Renrut", "Senna Hut", "-Zlaw", "Nosalnef",
122.     "Rewuorb", "Rellenk", "Yad", "Cire Htims", "Y-crad", "Nenilukah",
123.     "Corsh", "Aned", "Dark Eery", "Niknar", "Lapu", "Lechaim",
124.     "Rebrol-nek", "AlliWar Wickson", "Oguhmk", "Telloc Cyaj",
125. #ifdef OVERLAY
126.     "Erreip", "Nehpets", "Mron", "Snivek", "Kahztiy",
127. #endif
128. #ifdef WIN32
129.     "Lexa", "Niod",
130. #endif
131. #ifdef MACOS9
132.     "Nhoj-lee", "Evad\'kh", "Ettaw-noj", "Tsew-mot", "Ydna-s", "Yao-hang",
133.     "Tonbar", "Kivenhoug", "Llardom",
134. #endif
135. #ifdef AMIGA
136.     "Falo", "Nosid-da\'r", "Ekim-p", "Noslo", "Yl-rednow", "Mured-oog",
137.     "Ivrajimsal",
138. #endif
139. #ifdef TOS
140.     "Nivram",
141. #endif
142. #ifdef OS2
143.     "Nedraawi-nav",
144. #endif
145. #ifdef VMS
146.     "Lez-tneg", "Ytnu-haled",
147. #endif
148.     0
149. };
150. 
151. static const char *const shklight[] = {
152.     /* Romania */
153.     "Zarnesti", "Slanic", "Nehoiasu", "Ludus", "Sighisoara", "Nisipitu",
154.     "Razboieni", "Bicaz", "Dorohoi", "Vaslui", "Fetesti", "Tirgu Neamt",
155.     "Babadag", "Zimnicea", "Zlatna", "Jiu", "Eforie", "Mamaia",
156.     /* Bulgaria */
157.     "Silistra", "Tulovo", "Panagyuritshte", "Smolyan", "Kirklareli", "Pernik",
158.     "Lom", "Haskovo", "Dobrinishte", "Varvara", "Oryahovo", "Troyan",
159.     "Lovech", "Sliven", 0
160. };
161. 
162. static const char *const shkgeneral[] = {
163.     /* Suriname */
164.     "Hebiwerie",    "Possogroenoe", "Asidonhopo",   "Manlobbi",
165.     "Adjama",       "Pakka Pakka",  "Kabalebo",     "Wonotobo",
166.     "Akalapi",      "Sipaliwini",
167.     /* Greenland */
168.     "Annootok",     "Upernavik",    "Angmagssalik",
169.     /* N. Canada */
170.     "Aklavik",      "Inuvik",       "Tuktoyaktuk",  "Chicoutimi",
171.     "Ouiatchouane", "Chibougamau",  "Matagami",     "Kipawa",
172.     "Kinojevis",    "Abitibi",      "Maganasipi",
173.     /* Iceland */
174.     "Akureyri",     "Kopasker",     "Budereyri",    "Akranes",
175.     "Bordeyri",     "Holmavik",     0
176. };
177. 
178. static const char *const shkhealthfoods[] = {
179.     /* Tibet */
180.     "Ga'er",    "Zhangmu",   "Rikaze",   "Jiangji",     "Changdu",
181.     "Linzhi",   "Shigatse",  "Gyantse",  "Ganden",      "Tsurphu",
182.     "Lhasa",    "Tsedong",   "Drepung",
183.     /* Hippie names */
184.     "=Azura",   "=Blaze",    "=Breanna", "=Breezy",     "=Dharma",
185.     "=Feather", "=Jasmine",  "=Luna",    "=Melody",     "=Moonjava",
186.     "=Petal",   "=Rhiannon", "=Starla",  "=Tranquilla", "=Windsong",
187.     "=Zennia",  "=Zoe",      "=Zora",    0
188. };
189. 
190. /*
191.  * To add new shop types, all that is necessary is to edit the shtypes[]
192.  * array.  See mkroom.h for the structure definition.  Typically, you'll
193.  * have to lower some or all of the probability fields in old entries to
194.  * free up some percentage for the new type.
195.  *
196.  * The placement type field is not yet used but might be someday.
197.  *
198.  * The iprobs array in each entry defines the probabilities for various kinds
199.  * of objects to be present in the given shop type.  You can associate with
200.  * each percentage either a generic object type (represented by one of the
201.  * *_CLASS enum value) or a specific object enum value.
202.  * In the latter case, prepend it with a unary minus so the code can know
203.  * (by testing the sign) whether to use mkobj() or mksobj().
204.  * shtypes[] is externally referenced from mkroom.c, mon.c and shk.c.
205.  *
206.  * The second, usually shorter, store type name is used in automatically
207.  * generated annotations for #overview.  If Null, the first name gets used.
208.  */
209. const struct shclass shtypes[] = {
210.     { "general store", NULL,
211.       RANDOM_CLASS,
212.       42,
213.       D_SHOP,
214.       { { 100, RANDOM_CLASS },
215.         { 0, 0 },
216.         { 0, 0 },
217.         { 0, 0 },
218.         { 0, 0 },
219.         { 0, 0 } },
220.       shkgeneral },
221.     { "used armor dealership", "armor shop",
222.       ARMOR_CLASS,
223.       14,
224.       D_SHOP,
225.       { { 90, ARMOR_CLASS },
226.         { 10, WEAPON_CLASS },
227.         { 0, 0 },
228.         { 0, 0 },
229.         { 0, 0 },
230.         { 0, 0 } },
231.       shkarmors },
232.     { "second-hand bookstore", "scroll shop",
233.       SCROLL_CLASS,
234.       10,
235.       D_SHOP,
236.       { { 90, SCROLL_CLASS },
237.         { 10, SPBOOK_CLASS },
238.         { 0, 0 },
239.         { 0, 0 },
240.         { 0, 0 },
241.         { 0, 0 } },
242.       shkbooks },
243.     { "liquor emporium", "potion shop",
244.       POTION_CLASS,
245.       10,
246.       D_SHOP,
247.       { { 100, POTION_CLASS },
248.         { 0, 0 },
249.         { 0, 0 },
250.         { 0, 0 },
251.         { 0, 0 },
252.         { 0, 0 } },
253.       shkliquors },
254.     { "antique weapons outlet", "weapon shop",
255.       WEAPON_CLASS,
256.       5,
257.       D_SHOP,
258.       { { 90, WEAPON_CLASS },
259.         { 10, ARMOR_CLASS },
260.         { 0, 0 },
261.         { 0, 0 },
262.         { 0, 0 },
263.         { 0, 0 } },
264.       shkweapons },
265.     { "delicatessen", "food shop",
266.       FOOD_CLASS,
267.       5,
268.       D_SHOP,
269.       { { 83, FOOD_CLASS },
270.         { 5, -POT_FRUIT_JUICE },
271.         { 4, -POT_BOOZE },
272.         { 5, -POT_WATER },
273.         { 3, -ICE_BOX },
274.         { 0, 0 } },
275.       shkfoods },
276.     { "jewelers", "ring shop",
277.       RING_CLASS,
278.       3,
279.       D_SHOP,
280.       { { 85, RING_CLASS },
281.         { 10, GEM_CLASS },
282.         { 5, AMULET_CLASS },
283.         { 0, 0 },
284.         { 0, 0 },
285.         { 0, 0 } },
286.       shkrings },
287.     { "quality apparel and accessories", "wand shop",
288.       WAND_CLASS,
289.       3,
290.       D_SHOP,
291.       { { 90, WAND_CLASS },
292.         { 5, -LEATHER_GLOVES },
293.         { 5, -ELVEN_CLOAK },
294.         { 0, 0 } },
295.       shkwands },
296.     { "hardware store", "tool shop",
297.       TOOL_CLASS,
298.       3,
299.       D_SHOP,
300.       { { 100, TOOL_CLASS },
301.         { 0, 0 },
302.         { 0, 0 },
303.         { 0, 0 },
304.         { 0, 0 },
305.         { 0, 0 } },
306.       shktools },
307.     { "rare books", "bookstore",
308.       SPBOOK_CLASS,
309.       3,
310.       D_SHOP,
311.       { { 90, SPBOOK_CLASS },
312.         { 10, SCROLL_CLASS },
313.         { 0, 0 },
314.         { 0, 0 },
315.         { 0, 0 },
316.         { 0, 0 } },
317.       shkbooks },
318.     { "health food store", "vegetarian food shop",
319.       FOOD_CLASS,
320.       2,
321.       D_SHOP,
322.       { { 70, VEGETARIAN_CLASS },
323.         { 20, -POT_FRUIT_JUICE },
324.         { 4, -POT_HEALING },
325.         { 3, -POT_FULL_HEALING },
326.         { 2, -SCR_FOOD_DETECTION },
327.         { 1, -LUMP_OF_ROYAL_JELLY } },
328.       shkhealthfoods },
329.     /* Shops below this point are "unique".  That is they must all have a
330.      * probability of zero.  They are only created via the special level
331.      * loader.
332.      */
333.     { "lighting store", "lighting shop",
334.       TOOL_CLASS,
335.       0,
336.       D_SHOP,
337.       { { 30, -WAX_CANDLE },
338.         { 44, -TALLOW_CANDLE },
339.         { 5, -BRASS_LANTERN },
340.         { 9, -OIL_LAMP },
341.         { 3, -MAGIC_LAMP },
342.         { 5, -POT_OIL },
343.         { 2, -WAN_LIGHT },
344.         { 1, -SCR_LIGHT },
345.         { 1, -SPE_LIGHT } },
346.       shklight },
347.     /* sentinel */
348.     { (char *) 0, NULL,
349.       0,
350.       0,
351.       0,
352.       { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
353.       0 }
354. };
355. 
356. #if 0
357. /* validate shop probabilities; otherwise incorrect local changes could
358.    end up provoking infinite loops or wild subscripts fetching garbage */
359. void
360. init_shop_selection()
361. {
362.     int i, j, item_prob, shop_prob;
363. 
364.     for (shop_prob = 0, i = 0; i < SIZE(shtypes); i++) {
365.         shop_prob += shtypes[i].prob;
366.         for (item_prob = 0, j = 0; j < SIZE(shtypes[0].iprobs); j++)
367.             item_prob += shtypes[i].iprobs[j].iprob;
368.         if (item_prob != 100)
369.             panic("item probabilities total to %d for %s shops!",
370.                   item_prob, shtypes[i].name);
371.     }
372.     if (shop_prob != 100)
373.         panic("shop probabilities total to %d!", shop_prob);
374. }
375. #endif /*0*/
376. 
377. /* decide whether an object or object type is considered vegetarian;
378.    for types, items which might go either way are assumed to be veggy */
379. staticfn boolean
380. veggy_item(struct obj* obj, int otyp /* used iff obj is null */)
381. {
382.     int corpsenm;
383.     char oclass;
384. 
385.     if (obj) {
386.         /* actual object; will check tin content and corpse species */
387.         otyp = (int) obj->otyp;
388.         oclass = obj->oclass;
389.         corpsenm = obj->corpsenm;
390.     } else {
391.         /* just a type; caller will have to handle tins and corpses */
392.         oclass = objects[otyp].oc_class;
393.         corpsenm = PM_LICHEN; /* veggy standin */
394.     }
395. 
396.     if (oclass == FOOD_CLASS) {
397.         if (objects[otyp].oc_material == VEGGY || otyp == EGG)
398.             return TRUE;
399.         if (otyp == TIN && corpsenm == NON_PM) /* implies obj is non-null */
400.             return (boolean) (obj->spe == 1); /* 0 = empty, 1 = spinach */
401.         if (otyp == TIN || otyp == CORPSE)
402.             return (boolean) (ismnum(corpsenm) && vegetarian(&mons[corpsenm]));
403.     }
404.     return FALSE;
405. }
406. 
407. staticfn int
408. shkveg(void)
409. {
410.     int i, j, maxprob, prob;
411.     char oclass = FOOD_CLASS;
412.     int ok[NUM_OBJECTS];
413. 
414.     (void) memset((genericptr_t) ok, 0, sizeof ok); /* lint suppression */
415.     j = maxprob = 0;
416.     for (i = svb.bases[(int) oclass]; i < NUM_OBJECTS; ++i) {
417.         if (objects[i].oc_class != oclass)
418.             break;
419. 
420.         if (veggy_item((struct obj *) 0, i)) {
421.             ok[j++] = i;
422.             maxprob += objects[i].oc_prob;
423.         }
424.     }
425.     if (maxprob < 1)
426.         panic("shkveg no veggy objects");
427.     prob = rnd(maxprob);
428. 
429.     j = 0;
430.     i = ok[0];
431.     while ((prob -= objects[i].oc_prob) > 0) {
432.         j++;
433.         i = ok[j];
434.     }
435. 
436.     if (objects[i].oc_class != oclass || !OBJ_NAME(objects[i]))
437.         panic("shkveg probtype error, oclass=%d i=%d", (int) oclass, i);
438.     return i;
439. }
440. 
441. /* make a random item for health food store */
442. staticfn void
443. mkveggy_at(int sx, int sy)
444. {
445.     struct obj *obj = mksobj_at(shkveg(), sx, sy, TRUE, TRUE);
446. 
447.     if (obj && obj->otyp == TIN)
448.         set_tin_variety(obj, HEALTHY_TIN);
449.     return;
450. }
451. 
452. /* make an object of the appropriate type for a shop square */
453. staticfn void
454. mkshobj_at(const struct shclass *shp, int sx, int sy, boolean mkspecl)
455. {
456.     struct monst *mtmp;
457.     struct permonst *ptr;
458.     int atype;
459. 
460.     /* 3.6 tribute */
461.     if (mkspecl && (!strcmp(shp->name, "rare books")
462.                     || !strcmp(shp->name, "second-hand bookstore"))) {
463.         struct obj *novel = mksobj_at(SPE_NOVEL, sx, sy, FALSE, FALSE);
464. 
465.         if (novel)
466.             svc.context.tribute.bookstock = TRUE;
467.         return;
468.     }
469. 
470.     if (rn2(100) < depth(&u.uz) && !MON_AT(sx, sy)
471.         && (ptr = mkclass(S_MIMIC, 0)) != 0
472.         && (mtmp = makemon(ptr, sx, sy, NO_MM_FLAGS)) != 0) {
473.         /* nothing */
474.     } else {
475.         atype = get_shop_item((int) (shp - shtypes));
476.         if (atype == VEGETARIAN_CLASS)
477.             mkveggy_at(sx, sy);
478.         else if (atype < 0)
479.             (void) mksobj_at(-atype, sx, sy, TRUE, TRUE);
480.         else
481.             (void) mkobj_at(atype, sx, sy, TRUE);
482.     }
483. }
484. 
485. /* extract a shopkeeper name for the given shop type */
486. staticfn void
487. nameshk(struct monst *shk, const char *const *nlp)
488. {
489.     int i, trycnt, names_avail;
490.     const char *shname = 0;
491.     struct monst *mtmp;
492.     int name_wanted = shk->m_id;
493.     s_level *sptr;
494. 
495.     if (nlp == shklight && In_mines(&u.uz)
496.         && (sptr = Is_special(&u.uz)) != 0 && sptr->flags.town) {
497.         /* special-case minetown lighting shk */
498.         shname = "+Izchak";
499.         shk->female = FALSE;
500.     } else {
501.         /* We want variation from game to game, without needing the save
502.            and restore support which would be necessary for randomization;
503.            try not to make too many assumptions about time_t's internals;
504.            use ledger_no rather than depth to keep minetown distinct. */
505.         int nseed = (int) ((long) ubirthday / 257L);
506. 
507.         name_wanted += ledger_no(&u.uz) + (nseed % 13) - (nseed % 5);
508.         if (name_wanted < 0)
509.             name_wanted += (13 + 5);
510.         shk->female = name_wanted & 1;
511. 
512.         for (names_avail = 0; nlp[names_avail]; names_avail++)
513.             continue;
514.         assert(names_avail > 0);
515.         name_wanted = name_wanted % names_avail;
516. 
517.         for (trycnt = 0; trycnt < 50; trycnt++) {
518.             if (nlp == shktools) {
519.                 shname = shktools[rn2(names_avail)];
520.                 shk->female = 0; /* reversed below for '_' prefix */
521.             } else if (name_wanted < names_avail) {
522.                 shname = nlp[name_wanted];
523.             } else if ((i = rn2(names_avail)) != 0) {
524.                 shname = nlp[i - 1];
525.             } else if (nlp != shkgeneral) {
526.                 nlp = shkgeneral; /* try general names */
527.                 for (names_avail = 0; nlp[names_avail]; names_avail++)
528.                     continue;
529.                 continue; /* next `trycnt' iteration */
530.             } else {
531.                 shname = shk->female ? "-Lucrezia" : "+Dirk";
532.             }
533.             if (*shname == '_' || *shname == '-')
534.                 shk->female = 1;
535.             else if (*shname == '|' || *shname == '+')
536.                 shk->female = 0;
537. 
538.             /* is name already in use on this level? */
539.             for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
540.                 if (DEADMONSTER(mtmp) || (mtmp == shk) || !mtmp->isshk)
541.                     continue;
542.                 assert(has_eshk(mtmp));
543.                 if (strcmp(ESHK(mtmp)->shknam, shname))
544.                     continue;
545.                 name_wanted = names_avail; /* try a random name */
546.                 break;
547.             }
548.             if (!mtmp)
549.                 break; /* new name */
550.         }
551.     }
552.     (void) strncpy(ESHK(shk)->shknam, shname, PL_NSIZ);
553.     ESHK(shk)->shknam[PL_NSIZ - 1] = 0;
554. }
555. 
556. void
557. neweshk(struct monst *mtmp)
558. {
559.     if (!mtmp->mextra)
560.         mtmp->mextra = newmextra();
561.     if (!ESHK(mtmp))
562.         ESHK(mtmp) = (struct eshk *) alloc(sizeof(struct eshk));
563.     (void) memset((genericptr_t) ESHK(mtmp), 0, sizeof(struct eshk));
564.     ESHK(mtmp)->parentmid = mtmp->m_id;
565.     ESHK(mtmp)->bill_p = (struct bill_x *) 0;
566. }
567. 
568. void
569. free_eshk(struct monst *mtmp)
570. {
571.     if (mtmp->mextra && ESHK(mtmp)) {
572.         free((genericptr_t) ESHK(mtmp));
573.         ESHK(mtmp) = (struct eshk *) 0;
574.     }
575.     mtmp->isshk = 0;
576. }
577. 
578. /* find a door in room sroom which is good for shop entrance.
579.    returns -1 if no good door found, or the svd.doors index
580.    and the door coordinates in sx, sy */
581. staticfn int
582. good_shopdoor(struct mkroom *sroom, coordxy *sx, coordxy *sy)
583. {
584.     int i;
585. 
586.     for (i = 0; i < sroom->doorct; i++) {
587.         int di = sroom->fdoor + i;
588. 
589.         *sx = svd.doors[di].x;
590.         *sy = svd.doors[di].y;
591. 
592.         /* check that the shopkeeper placement is sane */
593.         if (sroom->irregular) {
594.             int rmno = (int) ((sroom - svr.rooms) + ROOMOFFSET);
595. 
596.             if (isok(*sx - 1, *sy) && !levl[*sx - 1][*sy].edge
597.                 && (int) levl[*sx - 1][*sy].roomno == rmno)
598.                 (*sx)--;
599.             else if (isok(*sx + 1, *sy) && !levl[*sx + 1][*sy].edge
600.                      && (int) levl[*sx + 1][*sy].roomno == rmno)
601.                 (*sx)++;
602.             else if (isok(*sx, *sy - 1) && !levl[*sx][*sy - 1].edge
603.                      && (int) levl[*sx][*sy - 1].roomno == rmno)
604.                 (*sy)--;
605.             else if (isok(*sx, *sy + 1) && !levl[*sx][*sy + 1].edge
606.                      && (int) levl[*sx][*sy + 1].roomno == rmno)
607.                 (*sy)++;
608.             else
609.                 continue;
610.         } else if (*sx == sroom->lx - 1) {
611.             (*sx)++;
612.         } else if (*sx == sroom->hx + 1) {
613.             (*sx)--;
614.         } else if (*sy == sroom->ly - 1) {
615.             (*sy)++;
616.         } else if (*sy == sroom->hy + 1) {
617.             (*sy)--;
618.         } else {
619.             continue;
620.         }
621.         return di;
622.     }
623.     return -1;
624. }
625. 
626. /* create a new shopkeeper in the given room */
627. staticfn int
628. shkinit(const struct shclass *shp, struct mkroom *sroom)
629. {
630.     int sh;
631.     coordxy sx, sy;
632.     struct monst *shk;
633.     struct eshk *eshkp;
634. 
635.     /* place the shopkeeper in the given room */
636.     sh = good_shopdoor(sroom, &sx, &sy);
637.     if (sh < 0) {
638. #ifdef DEBUG
639.         /* Said to happen sometimes, but I have never seen it. */
640.         /* Supposedly fixed by fdoor change in mklev.c */
641.         if (wizard) {
642.             int j = sroom->doorct;
643. 
644.             impossible("Where is shopdoor?");
645.             pline("Room at (%d,%d),(%d,%d).", sroom->lx, sroom->ly, sroom->hx,
646.                   sroom->hy);
647.             pline("doormax=%d doorct=%d fdoor=%d", gd.doorindex, sroom->doorct,
648.                   sh);
649.             while (j--) {
650.                 pline("door [%d,%d]", svd.doors[sh].x, svd.doors[sh].y);
651.                 sh++;
652.             }
653.             display_nhwindow(WIN_MESSAGE, FALSE);
654.         }
655. #endif
656.         return -1;
657.     }
658. 
659.     if (MON_AT(sx, sy))
660.         (void) rloc(m_at(sx, sy), RLOC_NOMSG); /* insurance */
661. 
662.     /* now initialize the shopkeeper monster structure */
663.     if (!(shk = makemon(&mons[PM_SHOPKEEPER], sx, sy, MM_ESHK)))
664.         return -1;
665.     eshkp = ESHK(shk); /* makemon(...,MM_ESHK) allocates this */
666.     shk->isshk = shk->mpeaceful = 1;
667.     set_malign(shk);
668.     shk->msleeping = 0;
669.     mon_learns_traps(shk, ALL_TRAPS); /* we know all the traps already */
670.     eshkp->shoproom = (schar) ((sroom - svr.rooms) + ROOMOFFSET);
671.     sroom->resident = shk;
672.     eshkp->shoptype = sroom->rtype;
673.     assign_level(&eshkp->shoplevel, &u.uz);
674.     eshkp->shd = svd.doors[sh];
675.     eshkp->shk.x = sx;
676.     eshkp->shk.y = sy;
677.     eshkp->robbed = eshkp->credit = eshkp->debit = eshkp->loan = 0L;
678.     eshkp->following = eshkp->surcharge = eshkp->dismiss_kops = FALSE;
679.     eshkp->billct = eshkp->visitct = 0;
680.     eshkp->bill_p = (struct bill_x *) 0;
681.     eshkp->customer[0] = '\0';
682.     mkmonmoney(shk, 1000L + 30L * (long) rnd(100)); /* initial capital */
683.     if (shp->shknms == shkrings)
684.         (void) mongets(shk, TOUCHSTONE);
685.     if (shp->shknms == shktools || shp->shknms == shkwands ||
686.         (shp->shknms == shkrings && rn2(2)) ||
687.         (shp->shknms == shkgeneral && rn2(5)))
688.         (void) mongets(shk, SCR_CHARGING);
689.     nameshk(shk, shp->shknms);
690. 
691.     return sh;
692. }
693. 
694. staticfn boolean
695. stock_room_goodpos(struct mkroom *sroom, int rmno, int sh, int sx, int sy)
696. {
697.     if (sroom->irregular) {
698.         if (levl[sx][sy].edge
699.             || (int) levl[sx][sy].roomno != rmno
700.             || distmin(sx, sy, svd.doors[sh].x, svd.doors[sh].y) <= 1)
701.             return FALSE;
702.     } else if ((sx == sroom->lx && svd.doors[sh].x == sx - 1)
703.                || (sx == sroom->hx && svd.doors[sh].x == sx + 1)
704.                || (sy == sroom->ly && svd.doors[sh].y == sy - 1)
705.                || (sy == sroom->hy && svd.doors[sh].y == sy + 1))
706.         return FALSE;
707. 
708.     /* only generate items on solid floor squares */
709.     if (!IS_ROOM(levl[sx][sy].typ)) {
710.         return FALSE;
711.     }
712. 
713.     return TRUE;
714. }
715. 
716. /* stock a newly-created room with objects */
717. void
718. stock_room(int shp_indx, struct mkroom *sroom)
719. {
720.     /*
721.      * Someday soon we'll dispatch on the shdist field of shclass to do
722.      * different placements in this routine. Currently it only supports
723.      * shop-style placement (all squares except a row nearest the first
724.      * door get objects).
725.      */
726.     int sx, sy, sh;
727.     int stockcount = 0, specialspot = 0;
728.     char buf[BUFSZ];
729.     int rmno = (int) ((sroom - svr.rooms) + ROOMOFFSET);
730.     const struct shclass *shp = &shtypes[shp_indx];
731. 
732.     /* first, try to place a shopkeeper in the room */
733.     if ((sh = shkinit(shp, sroom)) < 0)
734.         return;
735. 
736.     /* make sure no doorways without doors, and no trapped doors, in shops */
737.     sx = svd.doors[sroom->fdoor].x;
738.     sy = svd.doors[sroom->fdoor].y;
739.     if (levl[sx][sy].doormask == D_NODOOR) {
740.         levl[sx][sy].doormask = D_ISOPEN;
741.         newsym(sx, sy);
742.     }
743.     if (levl[sx][sy].typ == SDOOR) {
744.         cvt_sdoor_to_door(&levl[sx][sy]); /* .typ = DOOR */
745.         newsym(sx, sy);
746.     }
747.     if (levl[sx][sy].doormask & D_TRAPPED)
748.         levl[sx][sy].doormask = D_LOCKED;
749. 
750.     if (levl[sx][sy].doormask == D_LOCKED) {
751.         int m = sx, n = sy;
752. 
753.         if (inside_shop(sx + 1, sy))
754.             m--;
755.         else if (inside_shop(sx - 1, sy))
756.             m++;
757.         if (inside_shop(sx, sy + 1))
758.             n--;
759.         else if (inside_shop(sx, sy - 1))
760.             n++;
761.         Sprintf(buf, "Closed for inventory");
762.         make_engr_at(m, n, buf, NULL, 0L, DUST);
763.         if (levl[m][n].typ != CORR && levl[m][n].typ != ROOM)
764.             levl[m][n].typ = (Is_special(&u.uz)
765.                               || *in_rooms(m, n, 0)) ? ROOM : CORR;
766.     }
767. 
768.     if (svc.context.tribute.enabled && !svc.context.tribute.bookstock) {
769.         /*
770.          * Out of the number of spots where we're actually
771.          * going to put stuff, randomly single out one in particular.
772.          */
773.         for (sx = sroom->lx; sx <= sroom->hx; sx++)
774.             for (sy = sroom->ly; sy <= sroom->hy; sy++)
775.                 if (stock_room_goodpos(sroom, rmno, sh, sx,sy))
776.                     stockcount++;
777.         specialspot = rnd(stockcount);
778.         stockcount = 0;
779.     }
780. 
781.     for (sx = sroom->lx; sx <= sroom->hx; sx++)
782.         for (sy = sroom->ly; sy <= sroom->hy; sy++)
783.             if (stock_room_goodpos(sroom, rmno, sh, sx,sy)) {
784.                 stockcount++;
785.                 mkshobj_at(shp, sx, sy,
786.                            ((stockcount) && (stockcount == specialspot)));
787.             }
788. 
789.     /*
790.      * Special monster placements (if any) should go here: that way,
791.      * monsters will sit on top of objects and not the other way around.
792.      */
793. 
794.     /* Hack for Orcus's level: it's a ghost town, get rid of shopkeepers */
795.     if (on_level(&u.uz, &orcus_level)) {
796.         struct monst *mtmp = shop_keeper(rmno);
797.         mongone(mtmp);
798.     }
799. 
800.     svl.level.flags.has_shop = TRUE;
801. }
802. 
803. /* does shkp's shop stock this item type? */
804. boolean
805. saleable(struct monst *shkp, struct obj *obj)
806. {
807.     int i, shp_indx = ESHK(shkp)->shoptype - SHOPBASE;
808.     const struct shclass *shp = &shtypes[shp_indx];
809. 
810.     if (shp->symb == RANDOM_CLASS)
811.         return TRUE;
812.     for (i = 0; i < SIZE(shtypes[0].iprobs) && shp->iprobs[i].iprob; i++) {
813.         /* pseudo-class needs special handling */
814.         if (shp->iprobs[i].itype == VEGETARIAN_CLASS) {
815.             if (veggy_item(obj, 0))
816.                 return TRUE;
817.         } else if ((shp->iprobs[i].itype < 0)
818.                        ? shp->iprobs[i].itype == -obj->otyp
819.                        : shp->iprobs[i].itype == obj->oclass)
820.             return TRUE;
821.     }
822.     /* not found */
823.     return FALSE;
824. }
825. 
826. /* positive value: class; negative value: specific object type.
827.    can also return non-existing object class (eg. VEGETARIAN_CLASS) */
828. int
829. get_shop_item(int type)
830. {
831.     const struct shclass *shp = shtypes + type;
832.     int i, j;
833. 
834.     /* select an appropriate object type at random */
835.     for (j = rnd(100), i = 0; (j -= shp->iprobs[i].iprob) > 0; i++)
836.         continue;
837. 
838.     return shp->iprobs[i].itype;
839. }
840. 
841. /* version of shkname() for beginning of sentence */
842. char *
843. Shknam(struct monst *mtmp)
844. {
845.     char *nam = shkname(mtmp);
846. 
847.     /* 'nam[]' is almost certainly already capitalized, but be sure */
848.     nam[0] = highc(nam[0]);
849.     return nam;
850. }
851. 
852. /* shopkeeper's name, without any visibility constraint; if hallucinating,
853.    will yield some other shopkeeper's name (not necessarily one residing
854.    in the current game's dungeon, or who keeps same type of shop) */
855. char *
856. shkname(struct monst *mtmp)
857. {
858.     char *nam;
859.     unsigned save_isshk = mtmp->isshk;
860. 
861.     mtmp->isshk = 0; /* don't want mon_nam() calling shkname() */
862.     /* get a modifiable name buffer along with fallback result */
863.     nam = noit_mon_nam(mtmp);
864.     mtmp->isshk = save_isshk;
865. 
866.     if (!mtmp->isshk) {
867.         impossible("shkname: \"%s\" is not a shopkeeper.", nam);
868.     } else if (!has_eshk(mtmp)) {
869.         panic("shkname: shopkeeper \"%s\" lacks 'eshk' data.", nam);
870.     } else {
871.         const char *shknm = ESHK(mtmp)->shknam;
872. 
873.         if (Hallucination && !program_state.gameover) {
874.             const char *const *nlp;
875.             int num;
876. 
877.             /* count the number of non-unique shop types;
878.                pick one randomly, ignoring shop generation probabilities;
879.                pick a name at random from that shop type's list */
880.             for (num = 0; num < SIZE(shtypes); num++)
881.                 if (shtypes[num].prob == 0)
882.                     break;
883.             if (num > 0) {
884.                 nlp = shtypes[rn2(num)].shknms;
885.                 for (num = 0; nlp[num]; num++)
886.                     continue;
887.                 if (num > 0)
888.                     shknm = nlp[rn2(num)];
889.             }
890.         }
891.         /* strip prefix if present */
892.         if (!letter(*shknm))
893.             ++shknm;
894.         Strcpy(nam, shknm);
895.     }
896.     return nam;
897. }
898. 
899. boolean
900. shkname_is_pname(struct monst *mtmp)
901. {
902.     const char *shknm = ESHK(mtmp)->shknam;
903. 
904.     return (boolean) (*shknm == '-' || *shknm == '+' || *shknm == '=');
905. }
906. 
907. boolean
908. is_izchak(struct monst *shkp, boolean override_hallucination)
909. {
910.     const char *shknm;
911. 
912.     if (Hallucination && !override_hallucination)
913.         return FALSE;
914.     if (!shkp->isshk)
915.         return FALSE;
916.     /* outside of town, Izchak becomes just an ordinary shopkeeper */
917.     if (!in_town(shkp->mx, shkp->my))
918.         return FALSE;
919.     shknm = ESHK(shkp)->shknam;
920.     /* skip "+" prefix */
921.     if (!letter(*shknm))
922.         ++shknm;
923.     return (boolean) !strcmp(shknm, "Izchak");
924. }
925. 
926. #undef VEGETARIAN_CLASS
927. 
928. /*shknam.c*/