Source:NetHack 5.0.0/src/role.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to role.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/role.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	role.c	$NHDT-Date: 1737607158 2025/01/22 20:39:18 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.107 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985-1999. */
3. /*-Copyright (c) Robert Patrick Rankin, 2012. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. /*** Table of all roles ***/
9. /* According to AD&D, HD for some classes (ex. Wizard) should be smaller
10.  * (4-sided for wizards).  But this is not AD&D, and using the AD&D
11.  * rule here produces an unplayable character.  Thus I have used a minimum
12.  * of an 10-sided hit die for everything.  Another AD&D change: wizards get
13.  * a minimum strength of 4 since without one you can't teleport or cast
14.  * spells. --KAA
15.  *
16.  * As the wizard has been updated (wizard patch 5 jun '96) their HD can be
17.  * brought closer into line with AD&D. This forces wizards to use magic more
18.  * and distance themselves from their attackers. --LSZ
19.  *
20.  * With the introduction of races, some hit points and energy
21.  * has been reallocated for each race.  The values assigned
22.  * to the roles has been reduced by the amount allocated to
23.  * humans.  --KMH
24.  *
25.  * God names use a leading underscore to flag goddesses.
26.  */
27. 
28. /* NUM_ROLES is defined in hack.h */
29. 
30. const struct Role roles[NUM_ROLES+1] = {
31.     { { "Archeologist", 0 },
32.       { { "Digger", 0 },
33.         { "Field Worker", 0 },
34.         { "Investigator", 0 },
35.         { "Exhumer", 0 },
36.         { "Excavator", 0 },
37.         { "Spelunker", 0 },
38.         { "Speleologist", 0 },
39.         { "Collector", 0 },
40.         { "Curator", 0 } },
41.       "Quetzalcoatl", "Camaxtli", "Huhetotl", /* Central American */
42.       "Arc",
43.       "the College of Archeology",
44.       "the Tomb of the Toltec Kings",
45.       PM_ARCHEOLOGIST,
46.       NON_PM,
47.       PM_LORD_CARNARVON,
48.       PM_STUDENT,
49.       PM_MINION_OF_HUHETOTL,
50.       NON_PM,
51.       PM_HUMAN_MUMMY,
52.       S_SNAKE,
53.       S_MUMMY,
54.       ART_ORB_OF_DETECTION,
55.       MH_HUMAN | MH_DWARF | MH_GNOME | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL
56.           | ROLE_NEUTRAL,
57.       /* Str Int Wis Dex Con Cha */
58.       { 7, 10, 10, 7, 7, 7 },
59.       { 20, 20, 20, 10, 20, 10 },
60.       /* Init   Lower  Higher */
61.       { 11, 0, 0, 8, 1, 0 }, /* Hit points */
62.       { 1, 0, 0, 1, 0, 1 },
63.       14, /* Energy */
64.       10,
65.       5,
66.       0,
67.       2,
68.       10,
69.       A_INT,
70.       SPE_MAGIC_MAPPING,
71.       -4 },
72.     { { "Barbarian", 0 },
73.       { { "Plunderer", "Plunderess" },
74.         { "Pillager", 0 },
75.         { "Bandit", 0 },
76.         { "Brigand", 0 },
77.         { "Raider", 0 },
78.         { "Reaver", 0 },
79.         { "Slayer", 0 },
80.         { "Chieftain", "Chieftainess" },
81.         { "Conqueror", "Conqueress" } },
82.       "Mitra", "Crom", "Set", /* Hyborian */
83.       "Bar",
84.       "the Camp of the Duali Tribe",
85.       "the Duali Oasis",
86.       PM_BARBARIAN,
87.       NON_PM,
88.       PM_PELIAS,
89.       PM_CHIEFTAIN,
90.       PM_THOTH_AMON,
91.       PM_OGRE,
92.       PM_TROLL,
93.       S_OGRE,
94.       S_TROLL,
95.       ART_HEART_OF_AHRIMAN,
96.       MH_HUMAN | MH_ORC | ROLE_MALE | ROLE_FEMALE | ROLE_NEUTRAL
97.           | ROLE_CHAOTIC,
98.       /* Str Int Wis Dex Con Cha */
99.       { 16, 7, 7, 15, 16, 6 },
100.       { 30, 6, 7, 20, 30, 7 },
101.       /* Init   Lower  Higher */
102.       { 14, 0, 0, 10, 2, 0 }, /* Hit points */
103.       { 1, 0, 0, 1, 0, 1 },
104.       10, /* Energy */
105.       10,
106.       14,
107.       0,
108.       0,
109.       8,
110.       A_INT,
111.       SPE_HASTE_SELF,
112.       -4 },
113.     { { "Caveman", "Cavewoman" },
114.       { { "Troglodyte", 0 },
115.         { "Aborigine", 0 },
116.         { "Wanderer", 0 },
117.         { "Vagrant", 0 },
118.         { "Wayfarer", 0 },
119.         { "Roamer", 0 },
120.         { "Nomad", 0 },
121.         { "Rover", 0 },
122.         { "Pioneer", 0 } },
123.       "Anu", "_Ishtar", "Anshar", /* Babylonian */
124.       "Cav",
125.       "the Caves of the Ancestors",
126.       "the Dragon's Lair",
127.       PM_CAVE_DWELLER,
128.       PM_LITTLE_DOG,
129.       PM_SHAMAN_KARNOV,
130.       PM_NEANDERTHAL,
131.       PM_CHROMATIC_DRAGON,
132.       PM_BUGBEAR,
133.       PM_HILL_GIANT,
134.       S_HUMANOID,
135.       S_GIANT,
136.       ART_SCEPTRE_OF_MIGHT,
137.       MH_HUMAN | MH_DWARF | MH_GNOME | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL
138.           | ROLE_NEUTRAL,
139.       /* Str Int Wis Dex Con Cha */
140.       { 10, 7, 7, 7, 8, 6 },
141.       { 30, 6, 7, 20, 30, 7 },
142.       /* Init   Lower  Higher */
143.       { 14, 0, 0, 8, 2, 0 }, /* Hit points */
144.       { 1, 0, 0, 1, 0, 1 },
145.       10, /* Energy */
146.       0,
147.       12,
148.       0,
149.       1,
150.       8,
151.       A_INT,
152.       SPE_DIG,
153.       -4 },
154.     { { "Healer", 0 },
155.       { { "Rhizotomist", 0 },
156.         { "Empiric", 0 },
157.         { "Embalmer", 0 },
158.         { "Dresser", 0 },
159.         { "Medicus ossium", "Medica ossium" },
160.         { "Herbalist", 0 },
161.         { "Magister", "Magistra" },
162.         { "Physician", 0 },
163.         { "Chirurgeon", 0 } },
164.       "_Athena", "Hermes", "Poseidon", /* Greek */
165.       "Hea",
166.       "the Temple of Epidaurus",
167.       "the Temple of Coeus",
168.       PM_HEALER,
169.       NON_PM,
170.       PM_HIPPOCRATES,
171.       PM_ATTENDANT,
172.       PM_CYCLOPS,
173.       PM_GIANT_RAT,
174.       PM_SNAKE,
175.       S_RODENT,
176.       S_YETI,
177.       ART_STAFF_OF_AESCULAPIUS,
178.       MH_HUMAN | MH_GNOME | ROLE_MALE | ROLE_FEMALE | ROLE_NEUTRAL,
179.       /* Str Int Wis Dex Con Cha */
180.       { 7, 7, 13, 7, 11, 16 },
181.       { 15, 20, 20, 15, 25, 5 },
182.       /* Init   Lower  Higher */
183.       { 11, 0, 0, 8, 1, 0 }, /* Hit points */
184.       { 1, 4, 0, 1, 0, 2 },
185.       20, /* Energy */
186.       10,
187.       3,
188.       -3,
189.       2,
190.       10,
191.       A_WIS,
192.       SPE_CURE_SICKNESS,
193.       -4 },
194.     { { "Knight", 0 },
195.       { { "Gallant", 0 },
196.         { "Esquire", 0 },
197.         { "Bachelor", 0 },
198.         { "Sergeant", 0 },
199.         { "Knight", 0 },
200.         { "Banneret", 0 },
201.         { "Chevalier", "Chevaliere" },
202.         { "Seignieur", "Dame" },
203.         { "Paladin", 0 } },
204.       "Lugh", "_Brigit", "Manannan Mac Lir", /* Celtic */
205.       "Kni",
206.       "Camelot Castle",
207.       "the Isle of Glass",
208.       PM_KNIGHT,
209.       PM_PONY,
210.       PM_KING_ARTHUR,
211.       PM_PAGE,
212.       PM_IXOTH,
213.       PM_QUASIT,
214.       PM_OCHRE_JELLY,
215.       S_IMP,
216.       S_JELLY,
217.       ART_MAGIC_MIRROR_OF_MERLIN,
218.       MH_HUMAN | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL,
219.       /* Str Int Wis Dex Con Cha */
220.       { 13, 7, 14, 8, 10, 17 },
221.       { 30, 15, 15, 10, 20, 10 },
222.       /* Init   Lower  Higher */
223.       { 14, 0, 0, 8, 2, 0 }, /* Hit points */
224.       { 1, 4, 0, 1, 0, 2 },
225.       10, /* Energy */
226.       10,
227.       8,
228.       -2,
229.       0,
230.       9,
231.       A_WIS,
232.       SPE_TURN_UNDEAD,
233.       -4 },
234.     { { "Monk", 0 },
235.       { { "Candidate", 0 },
236.         { "Novice", 0 },
237.         { "Initiate", 0 },
238.         { "Student of Stones", 0 },
239.         { "Student of Waters", 0 },
240.         { "Student of Metals", 0 },
241.         { "Student of Winds", 0 },
242.         { "Student of Fire", 0 },
243.         { "Master", 0 } },
244.       "Shan Lai Ching", "Chih Sung-tzu", "Huan Ti", /* Chinese */
245.       "Mon",
246.       "the Monastery of Chan-Sune",
247.       "the Monastery of the Earth-Lord",
248.       PM_MONK,
249.       NON_PM,
250.       PM_GRAND_MASTER,
251.       PM_ABBOT,
252.       PM_MASTER_KAEN,
253.       PM_EARTH_ELEMENTAL,
254.       PM_XORN,
255.       S_ELEMENTAL,
256.       S_XORN,
257.       ART_EYES_OF_THE_OVERWORLD,
258.       MH_HUMAN | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL | ROLE_NEUTRAL
259.           | ROLE_CHAOTIC,
260.       /* Str Int Wis Dex Con Cha */
261.       { 10, 7, 8, 8, 7, 7 },
262.       { 25, 10, 20, 20, 15, 10 },
263.       /* Init   Lower  Higher */
264.       { 12, 0, 0, 8, 1, 0 }, /* Hit points */
265.       { 2, 2, 0, 2, 0, 2 },
266.       10, /* Energy */
267.       10,
268.       8,
269.       -2,
270.       2,
271.       20,
272.       A_WIS,
273.       SPE_RESTORE_ABILITY,
274.       -4 },
275.     { { "Priest", "Priestess" },
276.       { { "Aspirant", 0 },
277.         { "Acolyte", 0 },
278.         { "Adept", 0 },
279.         { "Priest", "Priestess" },
280.         { "Curate", 0 },
281.         { "Canon", "Canoness" },
282.         { "Lama", 0 },
283.         { "Patriarch", "Matriarch" },
284.         { "High Priest", "High Priestess" } },
285.       0, 0, 0, /* deities from a randomly chosen other role will be used */
286.       "Pri",
287.       "the Great Temple",
288.       "the Temple of Nalzok",
289.       PM_CLERIC,
290.       NON_PM,
291.       PM_ARCH_PRIEST,
292.       PM_ACOLYTE,
293.       PM_NALZOK,
294.       PM_HUMAN_ZOMBIE,
295.       PM_WRAITH,
296.       S_ZOMBIE,
297.       S_WRAITH,
298.       ART_MITRE_OF_HOLINESS,
299.       MH_HUMAN | MH_ELF | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL | ROLE_NEUTRAL
300.           | ROLE_CHAOTIC,
301.       /* Str Int Wis Dex Con Cha */
302.       { 7, 7, 10, 7, 7, 7 },
303.       { 15, 10, 30, 15, 20, 10 },
304.       /* Init   Lower  Higher */
305.       { 12, 0, 0, 8, 1, 0 }, /* Hit points */
306.       { 4, 3, 0, 2, 0, 2 },
307.       10, /* Energy */
308.       0,
309.       3,
310.       -2,
311.       2,
312.       10,
313.       A_WIS,
314.       SPE_REMOVE_CURSE,
315.       -4 },
316.     /* Note:  Rogue precedes Ranger so that use of `-R' on the command line
317.        retains its traditional meaning. */
318.     { { "Rogue", 0 },
319.       { { "Footpad", 0 },
320.         { "Cutpurse", 0 },
321.         { "Rogue", 0 },
322.         { "Pilferer", 0 },
323.         { "Robber", 0 },
324.         { "Burglar", 0 },
325.         { "Filcher", 0 },
326.         { "Magsman", "Magswoman" },
327.         { "Thief", 0 } },
328.       "Issek", "Mog", "Kos", /* Nehwon */
329.       "Rog",
330.       "the Thieves' Guild Hall",
331.       "the Assassins' Guild Hall",
332.       PM_ROGUE,
333.       NON_PM,
334.       PM_MASTER_OF_THIEVES,
335.       PM_THUG,
336.       PM_MASTER_ASSASSIN,
337.       PM_LEPRECHAUN,
338.       PM_GUARDIAN_NAGA,
339.       S_NYMPH,
340.       S_NAGA,
341.       ART_MASTER_KEY_OF_THIEVERY,
342.       MH_HUMAN | MH_ORC | ROLE_MALE | ROLE_FEMALE | ROLE_CHAOTIC,
343.       /* Str Int Wis Dex Con Cha */
344.       { 7, 7, 7, 10, 7, 6 },
345.       { 20, 10, 10, 30, 20, 10 },
346.       /* Init   Lower  Higher */
347.       { 10, 0, 0, 8, 1, 0 }, /* Hit points */
348.       { 1, 0, 0, 1, 0, 1 },
349.       11, /* Energy */
350.       10,
351.       8,
352.       0,
353.       1,
354.       9,
355.       A_INT,
356.       SPE_DETECT_TREASURE,
357.       -4 },
358.     { { "Ranger", 0 },
359.       {
360. #if 0 /* OBSOLETE */
361.         {"Edhel",   "Elleth"},
362.         {"Edhel",   "Elleth"},         /* elf-maid */
363.         {"Ohtar",   "Ohtie"},          /* warrior */
364.         {"Kano",    "Kanie"},          /* commander (Q.) ['a] educated guess,
365.                                           until further research- SAC */
366.         {"Arandur"," Aranduriel"}, /* king's servant, minister (Q.) - guess */
367.         {"Hir",         "Hiril"},      /* lord, lady (S.) ['ir] */
368.         {"Aredhel",     "Arwen"},      /* noble elf, maiden (S.) */
369.         {"Ernil",       "Elentariel"}, /* prince (S.), elf-maiden (Q.) */
370.         {"Elentar",     "Elentari"},   /* Star-king, -queen (Q.) */
371.         "Solonor Thelandira", "Aerdrie Faenya", "Lolth", /* Elven */
372. #endif
373.         { "Tenderfoot", 0 },
374.         { "Lookout", 0 },
375.         { "Trailblazer", 0 },
376.         { "Reconnoiterer", "Reconnoiteress" },
377.         { "Scout", 0 },
378.         { "Arbalester", 0 }, /* One skilled at crossbows */
379.         { "Archer", 0 },
380.         { "Sharpshooter", 0 },
381.         { "Marksman", "Markswoman" } },
382.       "Mercury", "_Venus", "Mars", /* Roman/planets */
383.       "Ran",
384.       "Orion's camp",
385.       "the cave of the wumpus",
386.       PM_RANGER,
387.       PM_LITTLE_DOG /* Orion & canis major */,
388.       PM_ORION,
389.       PM_HUNTER,
390.       PM_SCORPIUS,
391.       PM_FOREST_CENTAUR,
392.       PM_SCORPION,
393.       S_CENTAUR,
394.       S_SPIDER,
395.       ART_LONGBOW_OF_DIANA,
396.       MH_HUMAN | MH_ELF | MH_GNOME | MH_ORC | ROLE_MALE | ROLE_FEMALE
397.           | ROLE_NEUTRAL | ROLE_CHAOTIC,
398.       /* Str Int Wis Dex Con Cha */
399.       { 13, 13, 13, 9, 13, 7 },
400.       { 30, 10, 10, 20, 20, 10 },
401.       /* Init   Lower  Higher */
402.       { 13, 0, 0, 6, 1, 0 }, /* Hit points */
403.       { 1, 0, 0, 1, 0, 1 },
404.       12, /* Energy */
405.       10,
406.       9,
407.       2,
408.       1,
409.       10,
410.       A_INT,
411.       SPE_INVISIBILITY,
412.       -4 },
413.     { { "Samurai", 0 },
414.       { { "Hatamoto", 0 },       /* Banner Knight */
415.         { "Ronin", 0 },          /* no allegiance */
416.         { "Ninja", "Kunoichi" }, /* secret society */
417.         { "Joshu", 0 },          /* heads a castle */
418.         { "Ryoshu", 0 },         /* has a territory */
419.         { "Kokushu", 0 },        /* heads a province */
420.         { "Daimyo", 0 },         /* a samurai lord */
421.         { "Kuge", 0 },           /* Noble of the Court */
422.         { "Shogun", 0 } },       /* supreme commander, warlord */
423.       "_Amaterasu Omikami", "Raijin", "Susanowo", /* Japanese */
424.       "Sam",
425.       "the Castle of the Taro Clan",
426.       "the Shogun's Castle",
427.       PM_SAMURAI,
428.       PM_LITTLE_DOG,
429.       PM_LORD_SATO,
430.       PM_ROSHI,
431.       PM_ASHIKAGA_TAKAUJI,
432.       PM_WOLF,
433.       PM_STALKER,
434.       S_DOG,
435.       S_ELEMENTAL,
436.       ART_TSURUGI_OF_MURAMASA,
437.       MH_HUMAN | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL,
438.       /* Str Int Wis Dex Con Cha */
439.       { 10, 8, 7, 10, 17, 6 },
440.       { 30, 10, 8, 30, 14, 8 },
441.       /* Init   Lower  Higher */
442.       { 13, 0, 0, 8, 1, 0 }, /* Hit points */
443.       { 1, 0, 0, 1, 0, 1 },
444.       11, /* Energy */
445.       10,
446.       10,
447.       0,
448.       0,
449.       8,
450.       A_INT,
451.       SPE_CLAIRVOYANCE,
452.       -4 },
453.     { { "Tourist", 0 },
454.       { { "Rambler", 0 },
455.         { "Sightseer", 0 },
456.         { "Excursionist", 0 },
457.         { "Peregrinator", "Peregrinatrix" },
458.         { "Traveler", 0 },
459.         { "Journeyer", 0 },
460.         { "Voyager", 0 },
461.         { "Explorer", 0 },
462.         { "Adventurer", 0 } },
463.       "Blind Io", "_The Lady", "Offler", /* Discworld */
464.       "Tou",
465.       "Ankh-Morpork",
466.       "the Thieves' Guild Hall",
467.       PM_TOURIST,
468.       NON_PM,
469.       PM_TWOFLOWER,
470.       PM_GUIDE,
471.       PM_MASTER_OF_THIEVES,
472.       PM_GIANT_SPIDER,
473.       PM_FOREST_CENTAUR,
474.       S_SPIDER,
475.       S_CENTAUR,
476.       ART_YENDORIAN_EXPRESS_CARD,
477.       MH_HUMAN | ROLE_MALE | ROLE_FEMALE | ROLE_NEUTRAL,
478.       /* Str Int Wis Dex Con Cha */
479.       { 7, 10, 6, 7, 7, 10 },
480.       { 15, 10, 10, 15, 30, 20 },
481.       /* Init   Lower  Higher */
482.       { 8, 0, 0, 8, 0, 0 }, /* Hit points */
483.       { 1, 0, 0, 1, 0, 1 },
484.       14, /* Energy */
485.       0,
486.       5,
487.       1,
488.       2,
489.       10,
490.       A_INT,
491.       SPE_CHARM_MONSTER,
492.       -4 },
493.     { { "Valkyrie", 0 },
494.       { { "Stripling", 0 },
495.         { "Skirmisher", 0 },
496.         { "Fighter", 0 },
497.         { "Man-at-arms", "Woman-at-arms" },
498.         { "Warrior", 0 },
499.         { "Swashbuckler", 0 },
500.         { "Hero", "Heroine" },
501.         { "Champion", 0 },
502.         { "Lord", "Lady" } },
503.       "Tyr", "Odin", "Loki", /* Norse */
504.       "Val",
505.       "the Shrine of Destiny",
506.       "the cave of Surtur",
507.       PM_VALKYRIE,
508.       NON_PM /*PM_WINTER_WOLF_CUB*/,
509.       PM_NORN,
510.       PM_WARRIOR,
511.       PM_LORD_SURTUR,
512.       PM_FIRE_ANT,
513.       PM_FIRE_GIANT,
514.       S_ANT,
515.       S_GIANT,
516.       ART_ORB_OF_FATE,
517.       MH_HUMAN | MH_DWARF | ROLE_FEMALE | ROLE_LAWFUL | ROLE_NEUTRAL,
518.       /* Str Int Wis Dex Con Cha */
519.       { 10, 7, 7, 7, 10, 7 },
520.       { 30, 6, 7, 20, 30, 7 },
521.       /* Init   Lower  Higher */
522.       { 14, 0, 0, 8, 2, 0 }, /* Hit points */
523.       { 1, 0, 0, 1, 0, 1 },
524.       10, /* Energy */
525.       0,
526.       10,
527.       -2,
528.       0,
529.       9,
530.       A_WIS,
531.       SPE_CONE_OF_COLD,
532.       -4 },
533.     { { "Wizard", 0 },
534.       { { "Evoker", 0 },
535.         { "Conjurer", 0 },
536.         { "Thaumaturge", 0 },
537.         { "Magician", 0 },
538.         { "Enchanter", "Enchantress" },
539.         { "Sorcerer", "Sorceress" },
540.         { "Necromancer", 0 },
541.         { "Wizard", 0 },
542.         { "Mage", 0 } },
543.       "Ptah", "Thoth", "Anhur", /* Egyptian */
544.       "Wiz",
545.       "the Lonely Tower",
546.       "the Tower of Darkness",
547.       PM_WIZARD,
548.       PM_KITTEN,
549.       PM_NEFERET_THE_GREEN,
550.       PM_APPRENTICE,
551.       PM_DARK_ONE,
552.       PM_VAMPIRE_BAT,
553.       PM_XORN,
554.       S_BAT,
555.       S_WRAITH,
556.       ART_EYE_OF_THE_AETHIOPICA,
557.       MH_HUMAN | MH_ELF | MH_GNOME | MH_ORC | ROLE_MALE | ROLE_FEMALE
558.           | ROLE_NEUTRAL | ROLE_CHAOTIC,
559.       /* Str Int Wis Dex Con Cha */
560.       { 7, 10, 7, 7, 7, 7 },
561.       { 10, 30, 10, 20, 20, 10 },
562.       /* Init   Lower  Higher */
563.       { 10, 0, 0, 8, 1, 0 }, /* Hit points */
564.       { 4, 3, 0, 2, 0, 3 },
565.       12, /* Energy */
566.       0,
567.       1,
568.       0,
569.       3,
570.       10,
571.       A_INT,
572.       SPE_MAGIC_MISSILE,
573.       -4 },
574.     /* Array terminator */
575.     UNDEFINED_ROLE,
576. };
577. 
578. /* Table of all races */
579. 
580. /* NUM_RACES is defined in hack.h */
581. const struct Race races[NUM_RACES + 1] = {
582.     {
583.         "human",
584.         "human",
585.         "humanity",
586.         "Hum",
587.         { "man", "woman" },
588.         PM_HUMAN,
589.         PM_HUMAN_MUMMY,
590.         PM_HUMAN_ZOMBIE,
591.         MH_HUMAN | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL | ROLE_NEUTRAL
592.             | ROLE_CHAOTIC,
593.         MH_HUMAN,
594.         0,
595.         MH_GNOME | MH_ORC,
596.         /*    Str     Int Wis Dex Con Cha */
597.         { 3, 3, 3, 3, 3, 3 },
598.         { STR18(100), 18, 18, 18, 18, 18 },
599.         /* Init   Lower  Higher */
600.         { 2, 0, 0, 2, 1, 0 }, /* Hit points */
601.         { 1, 0, 2, 0, 2, 0 }  /* Energy */
602.     },
603.     {
604.         "elf",
605.         "elven",
606.         "elvenkind",
607.         "Elf",
608.         { 0, 0 },
609.         PM_ELF,
610.         PM_ELF_MUMMY,
611.         PM_ELF_ZOMBIE,
612.         MH_ELF | ROLE_MALE | ROLE_FEMALE | ROLE_CHAOTIC,
613.         MH_ELF,
614.         MH_ELF,
615.         MH_ORC,
616.         /*  Str    Int Wis Dex Con Cha */
617.         { 3, 3, 3, 3, 3, 3 },
618.         { 18, 20, 20, 18, 16, 18 },
619.         /* Init   Lower  Higher */
620.         { 1, 0, 0, 1, 1, 0 }, /* Hit points */
621.         { 2, 0, 3, 0, 3, 0 }  /* Energy */
622.     },
623.     {
624.         "dwarf",
625.         "dwarven",
626.         "dwarvenkind",
627.         "Dwa",
628.         { 0, 0 },
629.         PM_DWARF,
630.         PM_DWARF_MUMMY,
631.         PM_DWARF_ZOMBIE,
632.         MH_DWARF | ROLE_MALE | ROLE_FEMALE | ROLE_LAWFUL,
633.         MH_DWARF,
634.         MH_DWARF | MH_GNOME,
635.         MH_ORC,
636.         /*    Str     Int Wis Dex Con Cha */
637.         { 3, 3, 3, 3, 3, 3 },
638.         { STR18(100), 16, 16, 20, 20, 16 },
639.         /* Init   Lower  Higher */
640.         { 4, 0, 0, 3, 2, 0 }, /* Hit points */
641.         { 0, 0, 0, 0, 0, 0 }  /* Energy */
642.     },
643.     {
644.         "gnome",
645.         "gnomish",
646.         "gnomehood",
647.         "Gno",
648.         { 0, 0 },
649.         PM_GNOME,
650.         PM_GNOME_MUMMY,
651.         PM_GNOME_ZOMBIE,
652.         MH_GNOME | ROLE_MALE | ROLE_FEMALE | ROLE_NEUTRAL,
653.         MH_GNOME,
654.         MH_DWARF | MH_GNOME,
655.         MH_HUMAN,
656.         /*  Str    Int Wis Dex Con Cha */
657.         { 3, 3, 3, 3, 3, 3 },
658.         { STR18(50), 19, 18, 18, 18, 18 },
659.         /* Init   Lower  Higher */
660.         { 1, 0, 0, 1, 0, 0 }, /* Hit points */
661.         { 2, 0, 2, 0, 2, 0 }  /* Energy */
662.     },
663.     {
664.         "orc",
665.         "orcish",
666.         "orcdom",
667.         "Orc",
668.         { 0, 0 },
669.         PM_ORC,
670.         PM_ORC_MUMMY,
671.         PM_ORC_ZOMBIE,
672.         MH_ORC | ROLE_MALE | ROLE_FEMALE | ROLE_CHAOTIC,
673.         MH_ORC,
674.         0,
675.         MH_HUMAN | MH_ELF | MH_DWARF,
676.         /*  Str    Int Wis Dex Con Cha */
677.         { 3, 3, 3, 3, 3, 3 },
678.         { STR18(50), 16, 16, 18, 18, 16 },
679.         /* Init   Lower  Higher */
680.         { 1, 0, 0, 1, 0, 0 }, /* Hit points */
681.         { 1, 0, 1, 0, 1, 0 }  /* Energy */
682.     },
683.     /* Array terminator */
684.     UNDEFINED_RACE,
685. };
686. 
687. /* Table of all genders */
688. const struct Gender genders[] = {
689.     { "male", "he", "him", "his", "Mal", ROLE_MALE },
690.     { "female", "she", "her", "her", "Fem", ROLE_FEMALE },
691.     { "neuter", "it", "it", "its", "Ntr", ROLE_NEUTER },
692.     /* used by pronoun_gender() when hallucinating */
693.     { "group", "they", "them", "their", "Grp", 0 },
694. };
695. 
696. /* Table of all alignments */
697. const struct Align aligns[] = {
698.     { "law", "lawful", "Law", ROLE_LAWFUL, A_LAWFUL },
699.     { "balance", "neutral", "Neu", ROLE_NEUTRAL, A_NEUTRAL },
700.     { "chaos", "chaotic", "Cha", ROLE_CHAOTIC, A_CHAOTIC },
701.     { "evil", "unaligned", "Una", 0, A_NONE }
702. };
703. 
704. staticfn int randrole_filtered(void);
705. staticfn char *promptsep(char *, int);
706. staticfn int role_gendercount(int);
707. staticfn int race_alignmentcount(int);
708. 
709. /* used by str2XXX() */
710. static char NEARDATA randomstr[] = "random";
711. 
712. boolean
713. validrole(int rolenum)
714. {
715.     return (boolean) (IndexOkT(rolenum, roles));
716. }
717. 
718. int
719. randrole(boolean for_display)
720. {
721.     int res = SIZE(roles) - 1;
722. 
723.     if (for_display)
724.         res = rn2_on_display_rng(res);
725.     else
726.         res = rn2(res);
727.     return res;
728. }
729. 
730. staticfn int
731. randrole_filtered(void)
732. {
733.     int i, n = 0, set[SIZE(roles)];
734. 
735.     /* this doesn't rule out impossible combinations but attempts to
736.        honor all the filter masks */
737.     for (i = 0; i < SIZE(roles) - 1; ++i) /* -1: avoid terminating element */
738.         if (ok_role(i, ROLE_NONE, ROLE_NONE, ROLE_NONE)
739.             && ok_race(i, ROLE_RANDOM, ROLE_NONE, ROLE_NONE)
740.             && ok_gend(i, ROLE_NONE, ROLE_RANDOM, ROLE_NONE)
741.             && ok_align(i, ROLE_NONE, ROLE_NONE, ROLE_RANDOM))
742.             set[n++] = i;
743.     return n ? set[rn2(n)] : randrole(FALSE);
744. }
745. 
746. int
747. str2role(const char *str)
748. {
749.     int i, len;
750. 
751.     /* Is str valid? */
752.     if (!str || !str[0])
753.         return ROLE_NONE;
754. 
755.     /* Match as much of str as is provided */
756.     len = Strlen(str);
757.     for (i = 0; roles[i].name.m; i++) {
758.         /* Does it match the male name? */
759.         if (!strncmpi(str, roles[i].name.m, len))
760.             return i;
761.         /* Or the female name? */
762.         if (roles[i].name.f && !strncmpi(str, roles[i].name.f, len))
763.             return i;
764.         /* Or the filecode? */
765.         if (!strcmpi(str, roles[i].filecode))
766.             return i;
767.     }
768. 
769.     if ((len == 1 && (*str == '*' || *str == '@'))
770.         || !strncmpi(str, randomstr, len))
771.         return ROLE_RANDOM;
772. 
773.     /* Couldn't find anything appropriate */
774.     return ROLE_NONE;
775. }
776. 
777. boolean
778. validrace(int rolenum, int racenum)
779. {
780.     /* Assumes validrole */
781.     return (boolean) (IndexOkT(racenum, races)
782.                       && (roles[rolenum].allow & races[racenum].allow
783.                           & ROLE_RACEMASK));
784. }
785. 
786. int
787. randrace(int rolenum)
788. {
789.     int i, n = 0;
790. 
791.     /* Count the number of valid races */
792.     for (i = 0; races[i].noun; i++)
793.         if (roles[rolenum].allow & races[i].allow & ROLE_RACEMASK)
794.             n++;
795. 
796.     /* Pick a random race */
797.     /* Use a factor of 100 in case of bad random number generators */
798.     if (n)
799.         n = rn2(n * 100) / 100;
800.     for (i = 0; races[i].noun; i++)
801.         if (roles[rolenum].allow & races[i].allow & ROLE_RACEMASK) {
802.             if (n)
803.                 n--;
804.             else
805.                 return i;
806.         }
807. 
808.     /* This role has no permitted races? */
809.     return rn2(SIZE(races) - 1);
810. }
811. 
812. int
813. str2race(const char *str)
814. {
815.     int i, len;
816. 
817.     /* Is str valid? */
818.     if (!str || !str[0])
819.         return ROLE_NONE;
820. 
821.     /* Match as much of str as is provided */
822.     len = Strlen(str);
823.     for (i = 0; races[i].noun; i++) {
824.         /* Does it match the noun? */
825.         if (!strncmpi(str, races[i].noun, len))
826.             return i;
827.         /* check adjective too */
828.         if (races[i].adj && !strncmpi(str, races[i].adj, len))
829.             return i;
830.         /* Or the filecode? */
831.         if (!strcmpi(str, races[i].filecode))
832.             return i;
833.     }
834. 
835.     if ((len == 1 && (*str == '*' || *str == '@'))
836.         || !strncmpi(str, randomstr, len))
837.         return ROLE_RANDOM;
838. 
839.     /* Couldn't find anything appropriate */
840.     return ROLE_NONE;
841. }
842. 
843. boolean
844. validgend(int rolenum, int racenum, int gendnum)
845. {
846.     /* Assumes validrole and validrace */
847.     return (boolean) (gendnum >= 0 && gendnum < ROLE_GENDERS
848.                       && (roles[rolenum].allow & races[racenum].allow
849.                           & genders[gendnum].allow & ROLE_GENDMASK));
850. }
851. 
852. int
853. randgend(int rolenum, int racenum)
854. {
855.     int i, n = 0;
856. 
857.     /* Count the number of valid genders */
858.     for (i = 0; i < ROLE_GENDERS; i++)
859.         if (roles[rolenum].allow & races[racenum].allow & genders[i].allow
860.             & ROLE_GENDMASK)
861.             n++;
862. 
863.     /* Pick a random gender */
864.     if (n)
865.         n = rn2(n);
866.     for (i = 0; i < ROLE_GENDERS; i++)
867.         if (roles[rolenum].allow & races[racenum].allow & genders[i].allow
868.             & ROLE_GENDMASK) {
869.             if (n)
870.                 n--;
871.             else
872.                 return i;
873.         }
874. 
875.     /* This role/race has no permitted genders? */
876.     return rn2(ROLE_GENDERS);
877. }
878. 
879. int
880. str2gend(const char *str)
881. {
882.     int i, len;
883. 
884.     /* Is str valid? */
885.     if (!str || !str[0])
886.         return ROLE_NONE;
887. 
888.     /* Match as much of str as is provided */
889.     len = Strlen(str);
890.     for (i = 0; i < ROLE_GENDERS; i++) {
891.         /* Does it match the adjective? */
892.         if (!strncmpi(str, genders[i].adj, len))
893.             return i;
894.         /* Or the filecode? */
895.         if (!strcmpi(str, genders[i].filecode))
896.             return i;
897.     }
898.     if ((len == 1 && (*str == '*' || *str == '@'))
899.         || !strncmpi(str, randomstr, len))
900.         return ROLE_RANDOM;
901. 
902.     /* Couldn't find anything appropriate */
903.     return ROLE_NONE;
904. }
905. 
906. boolean
907. validalign(int rolenum, int racenum, int alignnum)
908. {
909.     /* Assumes validrole and validrace */
910.     return (boolean) (alignnum >= 0 && alignnum < ROLE_ALIGNS
911.                       && (roles[rolenum].allow & races[racenum].allow
912.                           & aligns[alignnum].allow & ROLE_ALIGNMASK));
913. }
914. 
915. int
916. randalign(int rolenum, int racenum)
917. {
918.     int i, n = 0;
919. 
920.     /* Count the number of valid alignments */
921.     for (i = 0; i < ROLE_ALIGNS; i++)
922.         if (roles[rolenum].allow & races[racenum].allow & aligns[i].allow
923.             & ROLE_ALIGNMASK)
924.             n++;
925. 
926.     /* Pick a random alignment */
927.     if (n)
928.         n = rn2(n);
929.     for (i = 0; i < ROLE_ALIGNS; i++)
930.         if (roles[rolenum].allow & races[racenum].allow & aligns[i].allow
931.             & ROLE_ALIGNMASK) {
932.             if (n)
933.                 n--;
934.             else
935.                 return i;
936.         }
937. 
938.     /* This role/race has no permitted alignments? */
939.     return rn2(ROLE_ALIGNS);
940. }
941. 
942. int
943. str2align(const char *str)
944. {
945.     int i, len;
946. 
947.     /* Is str valid? */
948.     if (!str || !str[0])
949.         return ROLE_NONE;
950. 
951.     /* Match as much of str as is provided */
952.     len = Strlen(str);
953.     for (i = 0; i < ROLE_ALIGNS; i++) {
954.         /* Does it match the adjective? */
955.         if (!strncmpi(str, aligns[i].adj, len))
956.             return i;
957.         /* Or the filecode? */
958.         if (!strcmpi(str, aligns[i].filecode))
959.             return i;
960.     }
961.     if ((len == 1 && (*str == '*' || *str == '@'))
962.         || !strncmpi(str, randomstr, len))
963.         return ROLE_RANDOM;
964. 
965.     /* Couldn't find anything appropriate */
966.     return ROLE_NONE;
967. }
968. 
969. /* is rolenum compatible with any racenum/gendnum/alignnum constraints? */
970. boolean
971. ok_role(int rolenum, int racenum, int gendnum, int alignnum)
972. {
973.     int i;
974.     short allow;
975. 
976.     if (IndexOkT(rolenum, roles)) {
977.         if (gr.rfilter.roles[rolenum])
978.             return FALSE;
979.         allow = roles[rolenum].allow;
980.         if (IndexOkT(racenum, races)
981.             && !(allow & races[racenum].allow & ROLE_RACEMASK))
982.             return FALSE;
983.         if (gendnum >= 0 && gendnum < ROLE_GENDERS
984.             && !(allow & genders[gendnum].allow & ROLE_GENDMASK))
985.             return FALSE;
986.         if (alignnum >= 0 && alignnum < ROLE_ALIGNS
987.             && !(allow & aligns[alignnum].allow & ROLE_ALIGNMASK))
988.             return FALSE;
989.         return TRUE;
990.     } else {
991.         /* random; check whether any selection is possible */
992.         for (i = 0; i < SIZE(roles) - 1; i++) {
993.             if (gr.rfilter.roles[i])
994.                 continue;
995.             allow = roles[i].allow;
996.             if (IndexOkT(racenum, races)
997.                 && !(allow & races[racenum].allow & ROLE_RACEMASK))
998.                 continue;
999.             if (gendnum >= 0 && gendnum < ROLE_GENDERS
1000.                 && !(allow & genders[gendnum].allow & ROLE_GENDMASK))
1001.                 continue;
1002.             if (alignnum >= 0 && alignnum < ROLE_ALIGNS
1003.                 && !(allow & aligns[alignnum].allow & ROLE_ALIGNMASK))
1004.                 continue;
1005.             return TRUE;
1006.         }
1007.         return FALSE;
1008.     }
1009. }
1010. 
1011. /* pick a random role subject to any racenum/gendnum/alignnum constraints */
1012. /* If pickhow == PICK_RIGID a role is returned only if there is  */
1013. /* a single possibility */
1014. int
1015. pick_role(int racenum, int gendnum, int alignnum, int pickhow)
1016. {
1017.     int i;
1018.     int roles_ok = 0, set[SIZE(roles)];
1019. 
1020.     for (i = 0; i < SIZE(roles) - 1; i++) {
1021.         if (ok_role(i, racenum, gendnum, alignnum)
1022.             && ok_race(i, (racenum >= 0) ? racenum : ROLE_RANDOM,
1023.                        gendnum, alignnum)
1024.             && ok_gend(i, racenum,
1025.                        (gendnum >= 0) ? gendnum : ROLE_RANDOM, alignnum)
1026.             && ok_align(i, racenum,
1027.                         gendnum, (alignnum >= 0) ? alignnum : ROLE_RANDOM))
1028.             set[roles_ok++] = i;
1029.     }
1030.     if (roles_ok == 0 || (roles_ok > 1 && pickhow == PICK_RIGID))
1031.         return ROLE_NONE;
1032.     return set[rn2(roles_ok)];
1033. }
1034. 
1035. /* is racenum compatible with any rolenum/gendnum/alignnum constraints? */
1036. boolean
1037. ok_race(int rolenum, int racenum, int gendnum, int alignnum)
1038. {
1039.     int i;
1040.     short allow;
1041. 
1042.     if (IndexOkT(racenum, races)) {
1043.         if (gr.rfilter.mask & races[racenum].selfmask)
1044.             return FALSE;
1045.         allow = races[racenum].allow;
1046.         if (IndexOkT(rolenum, roles)
1047.             && !(allow & roles[rolenum].allow & ROLE_RACEMASK))
1048.             return FALSE;
1049.         if (gendnum >= 0 && gendnum < ROLE_GENDERS
1050.             && !(allow & genders[gendnum].allow & ROLE_GENDMASK))
1051.             return FALSE;
1052.         if (alignnum >= 0 && alignnum < ROLE_ALIGNS
1053.             && !(allow & aligns[alignnum].allow & ROLE_ALIGNMASK))
1054.             return FALSE;
1055.         return TRUE;
1056.     } else {
1057.         /* random; check whether any selection is possible */
1058.         for (i = 0; i < SIZE(races) - 1; i++) {
1059.             if (gr.rfilter.mask & races[i].selfmask)
1060.                 continue;
1061.             allow = races[i].allow;
1062.             if (IndexOkT(rolenum, roles)
1063.                 && !(allow & roles[rolenum].allow & ROLE_RACEMASK))
1064.                 continue;
1065.             if (gendnum >= 0 && gendnum < ROLE_GENDERS
1066.                 && !(allow & genders[gendnum].allow & ROLE_GENDMASK))
1067.                 continue;
1068.             if (alignnum >= 0 && alignnum < ROLE_ALIGNS
1069.                 && !(allow & aligns[alignnum].allow & ROLE_ALIGNMASK))
1070.                 continue;
1071.             return TRUE;
1072.         }
1073.         return FALSE;
1074.     }
1075. }
1076. 
1077. /* Pick a random race subject to any rolenum/gendnum/alignnum constraints.
1078.    If pickhow == PICK_RIGID a race is returned only if there is
1079.    a single possibility. */
1080. int
1081. pick_race(int rolenum, int gendnum, int alignnum, int pickhow)
1082. {
1083.     int i;
1084.     int races_ok = 0;
1085. 
1086.     for (i = 0; i < SIZE(races) - 1; i++) {
1087.         if (ok_race(rolenum, i, gendnum, alignnum))
1088.             races_ok++;
1089.     }
1090.     if (races_ok == 0 || (races_ok > 1 && pickhow == PICK_RIGID))
1091.         return ROLE_NONE;
1092.     races_ok = rn2(races_ok);
1093.     for (i = 0; i < SIZE(races) - 1; i++) {
1094.         if (ok_race(rolenum, i, gendnum, alignnum)) {
1095.             if (races_ok == 0)
1096.                 return i;
1097.             else
1098.                 races_ok--;
1099.         }
1100.     }
1101.     return ROLE_NONE;
1102. }
1103. 
1104. /* is gendnum compatible with any rolenum/racenum/alignnum constraints? */
1105. /* gender and alignment are not comparable (and also not constrainable) */
1106. boolean
1107. ok_gend(int rolenum, int racenum, int gendnum, int alignnum UNUSED)
1108. {
1109.     int i;
1110.     short allow;
1111. 
1112.     if (gendnum >= 0 && gendnum < ROLE_GENDERS) {
1113.         if (gr.rfilter.mask & genders[gendnum].allow)
1114.             return FALSE;
1115.         allow = genders[gendnum].allow;
1116.         if (IndexOkT(rolenum, roles)
1117.             && !(allow & roles[rolenum].allow & ROLE_GENDMASK))
1118.             return FALSE;
1119.         if (IndexOkT(racenum, races)
1120.             && !(allow & races[racenum].allow & ROLE_GENDMASK))
1121.             return FALSE;
1122.         return TRUE;
1123.     } else {
1124.         /* random; check whether any selection is possible */
1125.         for (i = 0; i < ROLE_GENDERS; i++) {
1126.             if (gr.rfilter.mask & genders[i].allow)
1127.                 continue;
1128.             allow = genders[i].allow;
1129.             if (IndexOkT(rolenum, roles)
1130.                 && !(allow & roles[rolenum].allow & ROLE_GENDMASK))
1131.                 continue;
1132.             if (IndexOkT(racenum, races)
1133.                 && !(allow & races[racenum].allow & ROLE_GENDMASK))
1134.                 continue;
1135.             return TRUE;
1136.         }
1137.         return FALSE;
1138.     }
1139. }
1140. 
1141. /* pick a random gender subject to any rolenum/racenum/alignnum constraints */
1142. /* gender and alignment are not comparable (and also not constrainable) */
1143. /* If pickhow == PICK_RIGID a gender is returned only if there is  */
1144. /* a single possibility */
1145. int
1146. pick_gend(int rolenum, int racenum, int alignnum, int pickhow)
1147. {
1148.     int i;
1149.     int gends_ok = 0;
1150. 
1151.     for (i = 0; i < ROLE_GENDERS; i++) {
1152.         if (ok_gend(rolenum, racenum, i, alignnum))
1153.             gends_ok++;
1154.     }
1155.     if (gends_ok == 0 || (gends_ok > 1 && pickhow == PICK_RIGID))
1156.         return ROLE_NONE;
1157.     gends_ok = rn2(gends_ok);
1158.     for (i = 0; i < ROLE_GENDERS; i++) {
1159.         if (ok_gend(rolenum, racenum, i, alignnum)) {
1160.             if (gends_ok == 0)
1161.                 return i;
1162.             else
1163.                 gends_ok--;
1164.         }
1165.     }
1166.     return ROLE_NONE;
1167. }
1168. 
1169. /* is alignnum compatible with any rolenum/racenum/gendnum constraints? */
1170. /* alignment and gender are not comparable (and also not constrainable) */
1171. boolean
1172. ok_align(int rolenum, int racenum, int gendnum UNUSED, int alignnum)
1173. {
1174.     int i;
1175.     short allow;
1176. 
1177.     if (alignnum >= 0 && alignnum < ROLE_ALIGNS) {
1178.         if (gr.rfilter.mask & aligns[alignnum].allow)
1179.             return FALSE;
1180.         allow = aligns[alignnum].allow;
1181.         if (IndexOkT(rolenum, roles)
1182.             && !(allow & roles[rolenum].allow & ROLE_ALIGNMASK))
1183.             return FALSE;
1184.         if (IndexOkT(racenum, races)
1185.             && !(allow & races[racenum].allow & ROLE_ALIGNMASK))
1186.             return FALSE;
1187.         return TRUE;
1188.     } else {
1189.         /* random; check whether any selection is possible */
1190.         for (i = 0; i < ROLE_ALIGNS; i++) {
1191.             if (gr.rfilter.mask & aligns[i].allow)
1192.                 continue;
1193.             allow = aligns[i].allow;
1194.             if (IndexOkT(rolenum, roles)
1195.                 && !(allow & roles[rolenum].allow & ROLE_ALIGNMASK))
1196.                 continue;
1197.             if (IndexOkT(racenum, races)
1198.                 && !(allow & races[racenum].allow & ROLE_ALIGNMASK))
1199.                 continue;
1200.             return TRUE;
1201.         }
1202.         return FALSE;
1203.     }
1204. }
1205. 
1206. /* Pick a random alignment subject to any rolenum/racenum/gendnum constraints;
1207.    alignment and gender are not comparable (and also not constrainable).
1208.    If pickhow == PICK_RIGID an alignment is returned only if there is
1209.    a single possibility. */
1210. int
1211. pick_align(int rolenum, int racenum, int gendnum, int pickhow)
1212. {
1213.     int i;
1214.     int aligns_ok = 0;
1215. 
1216.     for (i = 0; i < ROLE_ALIGNS; i++) {
1217.         if (ok_align(rolenum, racenum, gendnum, i))
1218.             aligns_ok++;
1219.     }
1220.     if (aligns_ok == 0 || (aligns_ok > 1 && pickhow == PICK_RIGID))
1221.         return ROLE_NONE;
1222.     aligns_ok = rn2(aligns_ok);
1223.     for (i = 0; i < ROLE_ALIGNS; i++) {
1224.         if (ok_align(rolenum, racenum, gendnum, i)) {
1225.             if (aligns_ok == 0)
1226.                 return i;
1227.             else
1228.                 aligns_ok--;
1229.         }
1230.     }
1231.     return ROLE_NONE;
1232. }
1233. 
1234. void
1235. rigid_role_checks(void)
1236. {
1237.     int tmp;
1238. 
1239.     /* Some roles are limited to a single race, alignment, or gender and
1240.      * calling this routine prior to XXX_player_selection() will help
1241.      * prevent an extraneous prompt that actually doesn't allow
1242.      * you to choose anything further. Note the use of PICK_RIGID which
1243.      * causes the pick_XX() routine to return a value only if there is one
1244.      * single possible selection, otherwise it returns ROLE_NONE.
1245.      *
1246.      */
1247.     if (flags.initrole == ROLE_RANDOM) {
1248.         /* If the role was explicitly specified as ROLE_RANDOM
1249.          * via -uXXXX-@ or OPTIONS=role:random then choose the role
1250.          * in here to narrow down later choices.
1251.          */
1252.         flags.initrole = pick_role(flags.initrace, flags.initgend,
1253.                                    flags.initalign, PICK_RANDOM);
1254.         if (flags.initrole < 0)
1255.             flags.initrole = randrole_filtered();
1256.     }
1257.     if (flags.initrace == ROLE_RANDOM
1258.         && (tmp = pick_race(flags.initrole, flags.initgend,
1259.                             flags.initalign, PICK_RANDOM)) != ROLE_NONE)
1260.         flags.initrace = tmp;
1261.     if (flags.initalign == ROLE_RANDOM
1262.         && (tmp = pick_align(flags.initrole, flags.initrace,
1263.                              flags.initgend, PICK_RANDOM)) != ROLE_NONE)
1264.         flags.initalign = tmp;
1265.     if (flags.initgend == ROLE_RANDOM
1266.         && (tmp = pick_gend(flags.initrole, flags.initrace,
1267.                             flags.initalign, PICK_RANDOM)) != ROLE_NONE)
1268.         flags.initgend = tmp;
1269. 
1270.     if (flags.initrole != ROLE_NONE) {
1271.         if (flags.initrace == ROLE_NONE)
1272.             flags.initrace = pick_race(flags.initrole, flags.initgend,
1273.                                        flags.initalign, PICK_RIGID);
1274.         if (flags.initalign == ROLE_NONE)
1275.             flags.initalign = pick_align(flags.initrole, flags.initrace,
1276.                                          flags.initgend, PICK_RIGID);
1277.         if (flags.initgend == ROLE_NONE)
1278.             flags.initgend = pick_gend(flags.initrole, flags.initrace,
1279.                                        flags.initalign, PICK_RIGID);
1280.     }
1281. }
1282. 
1283. boolean
1284. setrolefilter(const char *bufp)
1285. {
1286.     int i;
1287.     boolean reslt = TRUE;
1288. 
1289.     if ((i = str2role(bufp)) != ROLE_NONE && i != ROLE_RANDOM)
1290.         gr.rfilter.roles[i] = TRUE;
1291.     else if ((i = str2race(bufp)) != ROLE_NONE && i != ROLE_RANDOM)
1292.         gr.rfilter.mask |= races[i].selfmask;
1293.     else if ((i = str2gend(bufp)) != ROLE_NONE && i != ROLE_RANDOM)
1294.         gr.rfilter.mask |= genders[i].allow;
1295.     else if ((i = str2align(bufp)) != ROLE_NONE && i != ROLE_RANDOM)
1296.         gr.rfilter.mask |= aligns[i].allow;
1297.     else
1298.         reslt = FALSE;
1299.     return reslt;
1300. }
1301. 
1302. boolean
1303. gotrolefilter(void)
1304. {
1305.     int i;
1306. 
1307.     if (gr.rfilter.mask)
1308.         return TRUE;
1309.     for (i = 0; i < SIZE(roles) - 1; ++i)
1310.         if (gr.rfilter.roles[i])
1311.             return TRUE;
1312.     return FALSE;
1313. }
1314. 
1315. /* create a string like " !Bar !Kni" or " !chaotic" that can be
1316.    put back into an RC file by #saveoptions */
1317. char *
1318. rolefilterstring(char *outbuf, int which)
1319. {
1320.     int i;
1321. 
1322.     outbuf[0] = outbuf[1] = '\0';
1323.     switch (which) {
1324.     case RS_ROLE:
1325.         for (i = 0; i < SIZE(roles) - 1; ++i) {
1326.             if (gr.rfilter.roles[i])
1327.                 Sprintf(eos(outbuf), " !%.3s", roles[i].name.m);
1328.         }
1329.         break;
1330.     case RS_RACE:
1331.         for (i = 0; i < SIZE(races) - 1; ++i) {
1332.             if ((gr.rfilter.mask & races[i].selfmask) != 0)
1333.                 Sprintf(eos(outbuf), " !%s", races[i].noun);
1334.         }
1335.         break;
1336.     case RS_GENDER:
1337.         for (i = 0; i < SIZE(genders) - 1; ++i) {
1338.             if ((gr.rfilter.mask & genders[i].allow) != 0)
1339.                 Sprintf(eos(outbuf), " !%s", genders[i].adj);
1340.         }
1341.         break;
1342.     case RS_ALGNMNT:
1343.         for (i = 0; i < SIZE(aligns) - 1; ++i) {
1344.             if ((gr.rfilter.mask & aligns[i].allow) != 0)
1345.                 Sprintf(eos(outbuf), " !%s", aligns[i].adj);
1346.         }
1347.         break;
1348.     default:
1349.         impossible("rolefilterstring: bad role aspect (%d)", which);
1350.         Strcpy(outbuf, " ?");
1351.         break;
1352.     }
1353.     /* constructed with a leading space; drop it */
1354.     return &outbuf[1];
1355. }
1356. 
1357. void
1358. clearrolefilter(int which)
1359. {
1360.     int i;
1361. 
1362.     switch (which) {
1363.     case RS_filter:
1364.         gr.rfilter.mask = 0; /* clear race, gender, and alignment filters */
1365.         FALLTHROUGH;
1366.         /*FALLTHRU*/
1367.     case RS_ROLE:
1368.         for (i = 0; i < SIZE(roles) - 1; ++i)
1369.             gr.rfilter.roles[i] = FALSE;
1370.         break;
1371.     case RS_RACE:
1372.         gr.rfilter.mask &= ~ROLE_RACEMASK;
1373.         break;
1374.     case RS_GENDER:
1375.         gr.rfilter.mask &= ~ROLE_GENDMASK;
1376.         break;
1377.     case RS_ALGNMNT:
1378.         gr.rfilter.mask &= ~ROLE_ALIGNMASK;
1379.         break;
1380.     }
1381. }
1382. 
1383. staticfn char *
1384. promptsep(char *buf, int num_post_attribs)
1385. {
1386.     const char *conjuct = "and ";
1387. 
1388.     if (num_post_attribs > 1 && gr.role_post_attribs < num_post_attribs
1389.         && gr.role_post_attribs > 1)
1390.         Strcat(buf, ",");
1391.     Strcat(buf, " ");
1392.     --gr.role_post_attribs;
1393.     if (!gr.role_post_attribs && num_post_attribs > 1)
1394.         Strcat(buf, conjuct);
1395.     return buf;
1396. }
1397. 
1398. staticfn int
1399. role_gendercount(int rolenum)
1400. {
1401.     int gendcount = 0;
1402. 
1403.     if (validrole(rolenum)) {
1404.         if (roles[rolenum].allow & ROLE_MALE)
1405.             ++gendcount;
1406.         if (roles[rolenum].allow & ROLE_FEMALE)
1407.             ++gendcount;
1408.         if (roles[rolenum].allow & ROLE_NEUTER)
1409.             ++gendcount;
1410.     }
1411.     return gendcount;
1412. }
1413. 
1414. staticfn int
1415. race_alignmentcount(int racenum)
1416. {
1417.     int aligncount = 0;
1418. 
1419.     if (racenum != ROLE_NONE && racenum != ROLE_RANDOM) {
1420.         if (races[racenum].allow & ROLE_CHAOTIC)
1421.             ++aligncount;
1422.         if (races[racenum].allow & ROLE_LAWFUL)
1423.             ++aligncount;
1424.         if (races[racenum].allow & ROLE_NEUTRAL)
1425.             ++aligncount;
1426.     }
1427.     return aligncount;
1428. }
1429. 
1430. char *
1431. root_plselection_prompt(
1432.     char *suppliedbuf, int buflen,
1433.     int rolenum, int racenum, int gendnum, int alignnum)
1434. {
1435.     int k, gendercount = 0, aligncount = 0;
1436.     char buf[BUFSZ];
1437.     static char err_ret[] = " character's";
1438.     boolean donefirst = FALSE;
1439. 
1440.     if (!suppliedbuf || buflen < 1)
1441.         return err_ret;
1442. 
1443.     /* initialize these static variables each time this is called */
1444.     gr.role_post_attribs = 0;
1445.     for (k = 0; k < NUM_BP; ++k)
1446.         gr.role_pa[k] = 0;
1447.     buf[0] = '\0';
1448.     *suppliedbuf = '\0';
1449. 
1450.     /* How many alignments are allowed for the desired race? */
1451.     if (racenum != ROLE_NONE && racenum != ROLE_RANDOM)
1452.         aligncount = race_alignmentcount(racenum);
1453. 
1454.     if (alignnum != ROLE_NONE && alignnum != ROLE_RANDOM
1455.         && ok_align(rolenum, racenum, gendnum, alignnum)) {
1456. #if 0   /* 'if' and 'else' had duplicate code here; probably a copy+parse
1457.          * oversight; if a problem with filtering of random role selection
1458.          * crops up, this is probably the place to start looking */
1459. 
1460.         /* if race specified, and multiple choice of alignments for it */
1461.         if ((racenum >= 0) && (aligncount > 1)) {
1462.         } else {
1463.         }
1464. #endif  /* the four lines of code below were in both 'if' and 'else' above */
1465.         if (donefirst)
1466.             Strcat(buf, " ");
1467.         Strcat(buf, aligns[alignnum].adj);
1468.         donefirst = TRUE;
1469.     } else {
1470.         /* in case we got here by failing the ok_align() test */
1471.         if (alignnum != ROLE_RANDOM)
1472.             alignnum = ROLE_NONE;
1473.         /* if alignment not specified, but race is specified
1474.            and only one choice of alignment for that race then
1475.            don't include it in the later list */
1476.         if ((((racenum != ROLE_NONE && racenum != ROLE_RANDOM)
1477.               && ok_race(rolenum, racenum, gendnum, alignnum))
1478.              && (aligncount > 1))
1479.             || (racenum == ROLE_NONE || racenum == ROLE_RANDOM)) {
1480.             gr.role_pa[BP_ALIGN] = 1;
1481.             gr.role_post_attribs++;
1482.         }
1483.     }
1484.     /* <your lawful> */
1485. 
1486.     /* How many genders are allowed for the desired role? */
1487.     if (validrole(rolenum))
1488.         gendercount = role_gendercount(rolenum);
1489. 
1490.     if (gendnum != ROLE_NONE && gendnum != ROLE_RANDOM) {
1491.         if (validrole(rolenum)) {
1492.             /* if role specified, and multiple choice of genders for it,
1493.                and name of role itself does not distinguish gender */
1494.             if ((rolenum != ROLE_NONE) && (gendercount > 1)
1495.                 && !roles[rolenum].name.f) {
1496.                 if (donefirst)
1497.                     Strcat(buf, " ");
1498.                 Strcat(buf, genders[gendnum].adj);
1499.                 donefirst = TRUE;
1500.             }
1501.         } else {
1502.             if (donefirst)
1503.                 Strcat(buf, " ");
1504.             Strcat(buf, genders[gendnum].adj);
1505.             donefirst = TRUE;
1506.         }
1507.     } else {
1508.         /* if gender not specified, but role is specified
1509.                 and only one choice of gender then
1510.                 don't include it in the later list */
1511.         if ((validrole(rolenum) && (gendercount > 1))
1512.             || !validrole(rolenum)) {
1513.             gr.role_pa[BP_GEND] = 1;
1514.             gr.role_post_attribs++;
1515.         }
1516.     }
1517.     /* <your lawful female> */
1518. 
1519.     if (racenum != ROLE_NONE && racenum != ROLE_RANDOM) {
1520.         if (validrole(rolenum)
1521.             && ok_race(rolenum, racenum, gendnum, alignnum)) {
1522.             if (donefirst)
1523.                 Strcat(buf, " ");
1524.             Strcat(buf, (rolenum == ROLE_NONE) ? races[racenum].noun
1525.                                                : races[racenum].adj);
1526.             donefirst = TRUE;
1527.         } else if (!validrole(rolenum)) {
1528.             if (donefirst)
1529.                 Strcat(buf, " ");
1530.             Strcat(buf, races[racenum].noun);
1531.             donefirst = TRUE;
1532.         } else {
1533.             gr.role_pa[BP_RACE] = 1;
1534.             gr.role_post_attribs++;
1535.         }
1536.     } else {
1537.         gr.role_pa[BP_RACE] = 1;
1538.         gr.role_post_attribs++;
1539.     }
1540.     /* <your lawful female gnomish> || <your lawful female gnome> */
1541. 
1542.     if (validrole(rolenum)) {
1543.         assert(IndexOkT(rolenum, roles));
1544.         if (donefirst)
1545.             Strcat(buf, " ");
1546.         if (gendnum != ROLE_NONE) {
1547.             if (gendnum == 1 && roles[rolenum].name.f)
1548.                 Strcat(buf, roles[rolenum].name.f);
1549.             else
1550.                 Strcat(buf, roles[rolenum].name.m);
1551.         } else {
1552.             if (roles[rolenum].name.f) {
1553.                 Strcat(buf, roles[rolenum].name.m);
1554.                 Strcat(buf, "/");
1555.                 Strcat(buf, roles[rolenum].name.f);
1556.             } else
1557.                 Strcat(buf, roles[rolenum].name.m);
1558.         }
1559.         donefirst = TRUE;
1560.     } else if (rolenum == ROLE_NONE) {
1561.         gr.role_pa[BP_ROLE] = 1;
1562.         gr.role_post_attribs++;
1563.     }
1564. 
1565.     if ((racenum == ROLE_NONE || racenum == ROLE_RANDOM)
1566.         && !validrole(rolenum)) {
1567.         if (donefirst)
1568.             Strcat(buf, " ");
1569.         Strcat(buf, "character");
1570.         /*donefirst = TRUE;*/
1571.     }
1572.     /* <your lawful female gnomish cavewoman> || <your lawful female gnome>
1573.      *    || <your lawful female character>
1574.      */
1575.     if (buflen > (int) (strlen(buf) + 1)) {
1576.         Strcpy(suppliedbuf, buf);
1577.         return suppliedbuf;
1578.     } else
1579.         return err_ret;
1580. }
1581. 
1582. char *
1583. build_plselection_prompt(
1584.     char *buf, int buflen,
1585.     int rolenum, int racenum, int gendnum, int alignnum)
1586. {
1587.     const char *defprompt = "Shall I pick a character for you? [ynaq] ";
1588.     int num_post_attribs = 0;
1589.     char tmpbuf[BUFSZ], *p;
1590. 
1591.     if (buflen < QBUFSZ)
1592.         return (char *) defprompt;
1593. 
1594.     Strcpy(tmpbuf, "Shall I pick ");
1595.     if (racenum != ROLE_NONE || validrole(rolenum))
1596.         Strcat(tmpbuf, "your ");
1597.     else
1598.         Strcat(tmpbuf, "a ");
1599.     /* <your> */
1600. 
1601.     (void) root_plselection_prompt(eos(tmpbuf), buflen - Strlen(tmpbuf),
1602.                                    rolenum, racenum, gendnum, alignnum);
1603.     /* "Shall I pick a character's role, race, gender, and alignment for you?"
1604.        plus " [ynaq] (y)" is a little too long for a conventional 80 columns;
1605.        also, "pick a character's <anything>" sounds a bit stilted */
1606.     strsubst(tmpbuf, "pick a character", "pick character");
1607.     Sprintf(buf, "%s", s_suffix(tmpbuf));
1608.     /* don't bother splitting caveman/cavewoman or priest/priestess
1609.        in order to apply possessive suffix to both halves, but do
1610.        change "priest/priestess'" to "priest/priestess's" */
1611.     if ((p = strstri(buf, "priest/priestess'")) != 0
1612.         && p[sizeof "priest/priestess'" - sizeof ""] == '\0')
1613.         strkitten(buf, 's');
1614. 
1615.     /* buf should now be:
1616.      *    <your lawful female gnomish cavewoman's>
1617.      * || <your lawful female gnome's>
1618.      * || <your lawful female character's>
1619.      *
1620.      * Now append the post attributes to it
1621.      */
1622.     num_post_attribs = gr.role_post_attribs;
1623.     if (!num_post_attribs) {
1624.         /* some constraints might have been mutually exclusive, in which case
1625.            some prompting that would have been omitted is needed after all */
1626.         if (flags.initrole == ROLE_NONE && !gr.role_pa[BP_ROLE])
1627.             gr.role_pa[BP_ROLE] = ++gr.role_post_attribs;
1628.         if (flags.initrace == ROLE_NONE && !gr.role_pa[BP_RACE])
1629.             gr.role_pa[BP_RACE] = ++gr.role_post_attribs;
1630.         if (flags.initalign == ROLE_NONE && !gr.role_pa[BP_ALIGN])
1631.             gr.role_pa[BP_ALIGN] = ++gr.role_post_attribs;
1632.         if (flags.initgend == ROLE_NONE && !gr.role_pa[BP_GEND])
1633.             gr.role_pa[BP_GEND] = ++gr.role_post_attribs;
1634.         num_post_attribs = gr.role_post_attribs;
1635.     }
1636.     if (num_post_attribs) {
1637.         if (gr.role_pa[BP_RACE]) {
1638.             (void) promptsep(eos(buf), num_post_attribs);
1639.             Strcat(buf, "race");
1640.         }
1641.         if (gr.role_pa[BP_ROLE]) {
1642.             (void) promptsep(eos(buf), num_post_attribs);
1643.             Strcat(buf, "role");
1644.         }
1645.         if (gr.role_pa[BP_GEND]) {
1646.             (void) promptsep(eos(buf), num_post_attribs);
1647.             Strcat(buf, "gender");
1648.         }
1649.         if (gr.role_pa[BP_ALIGN]) {
1650.             (void) promptsep(eos(buf), num_post_attribs);
1651.             Strcat(buf, "alignment");
1652.         }
1653.     }
1654.     Strcat(buf, " for you? [ynaq] ");
1655.     return buf;
1656. }
1657. 
1658. #undef BP_ALIGN
1659. #undef BP_GEND
1660. #undef BP_RACE
1661. #undef BP_ROLE
1662. #undef NUM_BP
1663. 
1664. void
1665. plnamesuffix(void)
1666. {
1667.     char *sptr, *eptr;
1668.     int i;
1669. 
1670.     /* some generic user names will be ignored in favor of prompting */
1671.     if (sysopt.genericusers) {
1672.         if (*sysopt.genericusers == '*') {
1673.             svp.plname[0] = '\0';
1674.         } else {
1675.             /* need to ignore appended '-role-race-gender-alignment';
1676.                'plnamelen' is non-zero when dealing with plname[] value that
1677.                contains a username with dash(es) in it and is usually 0 */
1678.             i = ((eptr = strchr(svp.plname + gp.plnamelen, '-')) != 0)
1679.                 ? (int) (eptr - svp.plname)
1680.                 : (int) Strlen(svp.plname);
1681.             /* look for plname[] in the 'genericusers' space-separated list */
1682.             if (findword(sysopt.genericusers, svp.plname, i, FALSE))
1683.                 /* it's generic; remove it so that askname() will be called */
1684.                 svp.plname[0] = '\0';
1685.         }
1686.         if (!svp.plname[0])
1687.             gp.plnamelen = 0;
1688.     }
1689. 
1690.     do {
1691.         if (!svp.plname[0]) {
1692.             askname(); /* fill svp.plname[] if necessary, or set
1693.                         * defer_plname */
1694.             gp.plnamelen = 0; /* plname[] might have -role-race-&c attached */
1695.         }
1696. 
1697.         /* Look for tokens delimited by '-' */
1698.         sptr = svp.plname + gp.plnamelen;
1699.         if ((eptr = strchr(sptr, '-')) != (char *) 0)
1700.             *eptr++ = '\0';
1701.         while (eptr) {
1702.             /* Isolate the next token */
1703.             sptr = eptr;
1704.             if ((eptr = strchr(sptr, '-')) != (char *) 0)
1705.                 *eptr++ = '\0';
1706. 
1707.             /* Try to match it to something */
1708.             if ((i = str2role(sptr)) != ROLE_NONE)
1709.                 flags.initrole = i;
1710.             else if ((i = str2race(sptr)) != ROLE_NONE)
1711.                 flags.initrace = i;
1712.             else if ((i = str2gend(sptr)) != ROLE_NONE)
1713.                 flags.initgend = i;
1714.             else if ((i = str2align(sptr)) != ROLE_NONE)
1715.                 flags.initalign = i;
1716.         }
1717.     } while (!svp.plname[0] && !iflags.defer_plname);
1718. 
1719.     /* commas in the svp.plname confuse the record file, convert to spaces */
1720.     (void) strNsubst(svp.plname, ",", " ", 0);
1721. }
1722. 
1723. /* show current settings for name, role, race, gender, and alignment
1724.    in the specified window */
1725. void
1726. role_selection_prolog(int which, winid where)
1727. {
1728.     static const char NEARDATA choosing[] = " choosing now",
1729.                                not_yet[] = " not yet specified",
1730.                                rand_choice[] = " random";
1731.     char buf[BUFSZ];
1732.     int r, c, gend, a, allowmask;
1733. 
1734.     r = flags.initrole;
1735.     c = flags.initrace;
1736.     gend = flags.initgend;
1737.     a = flags.initalign;
1738.     if (r >= 0) {
1739.         assert(IndexOkT(r, roles));
1740.         allowmask = roles[r].allow;
1741.         if ((allowmask & ROLE_RACEMASK) == MH_HUMAN)
1742.             c = 0; /* races[human] */
1743.         else if (IndexOkT(c, races)
1744.                  && !(allowmask & ROLE_RACEMASK & races[c].allow))
1745.             c = ROLE_RANDOM;
1746.         if ((allowmask & ROLE_GENDMASK) == ROLE_MALE)
1747.             gend = 0; /* role forces male (hypothetical) */
1748.         else if ((allowmask & ROLE_GENDMASK) == ROLE_FEMALE)
1749.             gend = 1; /* role forces female (valkyrie) */
1750.         if ((allowmask & ROLE_ALIGNMASK) == AM_LAWFUL)
1751.             a = 0; /* aligns[lawful] */
1752.         else if ((allowmask & ROLE_ALIGNMASK) == AM_NEUTRAL)
1753.             a = 1; /* aligns[neutral] */
1754.         else if ((allowmask & ROLE_ALIGNMASK) == AM_CHAOTIC)
1755.             a = 2; /* aligns[chaotic] */
1756.     }
1757.     if (c >= 0) {
1758.         assert(IndexOkT(c, races));
1759.         allowmask = races[c].allow;
1760.         if ((allowmask & ROLE_ALIGNMASK) == AM_LAWFUL)
1761.             a = 0; /* aligns[lawful] */
1762.         else if ((allowmask & ROLE_ALIGNMASK) == AM_NEUTRAL)
1763.             a = 1; /* aligns[neutral] */
1764.         else if ((allowmask & ROLE_ALIGNMASK) == AM_CHAOTIC)
1765.             a = 2; /* aligns[chaotic] */
1766.         /* [c never forces gender] */
1767.     }
1768.     /* [g and a don't constrain anything sufficiently
1769.        to narrow something done to a single choice] */
1770. 
1771.     Sprintf(buf, "%12s ", "name:");
1772.     Strcat(buf, (which == RS_NAME) ? choosing
1773.                 : !*svp.plname ? not_yet : svp.plname);
1774.     putstr(where, 0, buf);
1775.     Sprintf(buf, "%12s ", "role:");
1776.     assert(which == RS_ROLE || r == ROLE_NONE || r == ROLE_RANDOM
1777.            || IndexOkT(r, roles));
1778.     Strcat(buf, (which == RS_ROLE) ? choosing
1779.                 : (r == ROLE_NONE) ? not_yet
1780.                   : (r == ROLE_RANDOM) ? rand_choice
1781.                     : roles[r].name.m);
1782.     if (r >= 0 && roles[r].name.f) {
1783.         /* distinct female name [caveman/cavewoman, priest/priestess] */
1784.         if (gend == 1)
1785.             /* female specified; replace male role name with female one */
1786.             Sprintf(strchr(buf, ':'), ": %s", roles[r].name.f);
1787.         else if (gend < 0)
1788.             /* gender unspecified; append slash and female role name */
1789.             Sprintf(eos(buf), "/%s", roles[r].name.f);
1790.     }
1791.     putstr(where, 0, buf);
1792.     Sprintf(buf, "%12s ", "race:");
1793.     assert(which == RS_RACE || c == ROLE_NONE || c == ROLE_RANDOM
1794.            || IndexOkT(c, races));
1795.     Strcat(buf, (which == RS_RACE) ? choosing
1796.                 : (c == ROLE_NONE) ? not_yet
1797.                   : (c == ROLE_RANDOM) ? rand_choice
1798.                     : races[c].noun);
1799.     putstr(where, 0, buf);
1800.     Sprintf(buf, "%12s ", "gender:");
1801.     Strcat(buf, (which == RS_GENDER) ? choosing
1802.                 : (gend == ROLE_NONE) ? not_yet
1803.                   : (gend == ROLE_RANDOM) ? rand_choice
1804.                     : genders[gend].adj);
1805.     putstr(where, 0, buf);
1806.     Sprintf(buf, "%12s ", "alignment:");
1807.     Strcat(buf, (which == RS_ALGNMNT) ? choosing
1808.                 : (a == ROLE_NONE) ? not_yet
1809.                   : (a == ROLE_RANDOM) ? rand_choice
1810.                     : aligns[a].adj);
1811.     putstr(where, 0, buf);
1812. }
1813. 
1814. /* add a "pick alignment first"-type entry to the specified menu */
1815. void
1816. role_menu_extra(int which, winid where, boolean preselect)
1817. {
1818.     static NEARDATA const char RS_menu_let[] = {
1819.         '=',  /* name */
1820.         '?',  /* role */
1821.         '/',  /* race */
1822.         '\"', /* gender */
1823.         '[',  /* alignment */
1824.     };
1825.     anything any;
1826.     char buf[BUFSZ];
1827.     const char *what = 0, *constrainer = 0, *forcedvalue = 0;
1828.     int f = 0, r, c, gend, a, i, allowmask;
1829.     int clr = NO_COLOR;
1830. 
1831.     r = flags.initrole;
1832.     c = flags.initrace;
1833.     switch (which) {
1834.     case RS_NAME:
1835.         what = "name";
1836.         break;
1837.     case RS_ROLE:
1838.         what = "role";
1839.         f = r;
1840.         for (i = 0; i < SIZE(roles) - 1; ++i)
1841.             if (i != f && !gr.rfilter.roles[i])
1842.                 break;
1843.         if (i == SIZE(roles) - 1) {
1844.             constrainer = "filter";
1845.             forcedvalue = "role";
1846.         }
1847.         break;
1848.     case RS_RACE:
1849.         what = "race";
1850.         f = flags.initrace;
1851.         c = ROLE_NONE; /* override player's setting */
1852.         if (r >= 0) {
1853.             allowmask = roles[r].allow & ROLE_RACEMASK;
1854.             if (allowmask == MH_HUMAN)
1855.                 c = 0; /* races[human] */
1856.             if (c >= 0) {
1857.                 constrainer = "role";
1858.                 forcedvalue = races[c].noun;
1859.             } else if (f >= 0 && ((allowmask & ~gr.rfilter.mask)
1860.                                   == races[f].selfmask)) {
1861.                 /* if there is only one race choice available due to user
1862.                    options disallowing others, race menu entry is disabled */
1863.                 constrainer = "filter";
1864.                 forcedvalue = "race";
1865.             }
1866.         }
1867.         break;
1868.     case RS_GENDER:
1869.         what = "gender";
1870.         f = flags.initgend;
1871.         gend = ROLE_NONE;
1872.         if (r >= 0) {
1873.             allowmask = roles[r].allow & ROLE_GENDMASK;
1874.             if (allowmask == ROLE_MALE)
1875.                 gend = 0; /* genders[male] */
1876.             else if (allowmask == ROLE_FEMALE)
1877.                 gend = 1; /* genders[female] */
1878.             if (gend >= 0) {
1879.                 constrainer = "role";
1880.                 forcedvalue = genders[gend].adj;
1881.             } else if (f >= 0 && ((allowmask & ~gr.rfilter.mask)
1882.                                   == genders[f].allow)) {
1883.                 /* if there is only one gender choice available due to user
1884.                    options disallowing other, gender menu entry is disabled */
1885.                 constrainer = "filter";
1886.                 forcedvalue = "gender";
1887.             }
1888.         }
1889.         break;
1890.     case RS_ALGNMNT:
1891.         what = "alignment";
1892.         f = flags.initalign;
1893.         a = ROLE_NONE;
1894.         if (r >= 0) {
1895.             allowmask = roles[r].allow & ROLE_ALIGNMASK;
1896.             if (allowmask == AM_LAWFUL)
1897.                 a = 0; /* aligns[lawful] */
1898.             else if (allowmask == AM_NEUTRAL)
1899.                 a = 1; /* aligns[neutral] */
1900.             else if (allowmask == AM_CHAOTIC)
1901.                 a = 2; /* aligns[chaotic] */
1902.             if (a >= 0)
1903.                 constrainer = "role";
1904.         }
1905.         if (c >= 0 && !constrainer) {
1906.             allowmask = races[c].allow & ROLE_ALIGNMASK;
1907.             if (allowmask == AM_LAWFUL)
1908.                 a = 0; /* aligns[lawful] */
1909.             else if (allowmask == AM_NEUTRAL)
1910.                 a = 1; /* aligns[neutral] */
1911.             else if (allowmask == AM_CHAOTIC)
1912.                 a = 2; /* aligns[chaotic] */
1913.             if (a >= 0)
1914.                 constrainer = "race";
1915.         }
1916.         if (f >= 0 && !constrainer
1917.             && (ROLE_ALIGNMASK & ~gr.rfilter.mask) == aligns[f].allow) {
1918.             /* if there is only one alignment choice available due to user
1919.                options disallowing others, algn menu entry is disabled */
1920.             constrainer = "filter";
1921.             forcedvalue = "alignment";
1922.         }
1923.         if (a >= 0)
1924.             forcedvalue = aligns[a].adj;
1925.         break;
1926.     }
1927. 
1928.     any = cg.zeroany; /* zero out all bits */
1929.     if (constrainer) {
1930.         any.a_int = 0;
1931.         /* use four spaces of padding to fake a grayed out menu choice */
1932.         Sprintf(buf, "%4s%s forces %s", "", constrainer, forcedvalue);
1933.         add_menu_str(where, buf);
1934.     } else if (what) {
1935.         any.a_int = RS_menu_arg(which);
1936.         Sprintf(buf, "Pick%s %s first", (f >= 0) ? " another" : "", what);
1937.         add_menu(where, &nul_glyphinfo, &any, RS_menu_let[which], 0,
1938.                  ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE);
1939.     } else if (which == RS_filter) {
1940.         char setfiltering[40];
1941. 
1942.         any.a_int = RS_menu_arg(RS_filter);
1943.         Sprintf(setfiltering, "%s role/race/&c filtering",
1944.                 gotrolefilter() ? "Reset" : "Set");
1945.         add_menu(where, &nul_glyphinfo, &any, '~', 0, ATR_NONE,
1946.                  clr, setfiltering, MENU_ITEMFLAGS_NONE);
1947.     } else if (which == ROLE_RANDOM) {
1948.         any.a_int = ROLE_RANDOM;
1949.         add_menu(where, &nul_glyphinfo, &any, '*', 0,
1950.                  ATR_NONE, clr, "Random",
1951.                  preselect ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
1952.     } else if (which == ROLE_NONE) {
1953.         any.a_int = ROLE_NONE;
1954.         add_menu(where, &nul_glyphinfo, &any, 'q', 0,
1955.                  ATR_NONE, clr, "Quit",
1956.                  preselect ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
1957.     } else {
1958.         impossible("role_menu_extra: bad arg (%d)", which);
1959.     }
1960. }
1961. 
1962. /*
1963.  *      Special setup modifications here:
1964.  *
1965.  *      Unfortunately, this is going to have to be done
1966.  *      on each newgame or restore, because you lose the permonst mods
1967.  *      across a save/restore.  :-)
1968.  *
1969.  *      1 - The Rogue Leader is the Tourist Nemesis.
1970.  *      2 - Priests start with a random alignment - convert the leader and
1971.  *          guardians here.
1972.  *      3 - Priests also get their set of deities from a randomly chosen role.
1973.  *      4 - [obsolete] Elves can have one of two different leaders,
1974.  *          but can't work it out here because it requires hacking the
1975.  *          level file data (see sp_lev.c).
1976.  *
1977.  * This code also replaces quest_init().
1978.  */
1979. void
1980. role_init(void)
1981. {
1982.     int alignmnt;
1983.     struct permonst *pm;
1984. 
1985.     /* Strip the role letter out of the player name.
1986.      * This is included for backwards compatibility.
1987.      */
1988.     plnamesuffix();
1989. 
1990.     /* Check for a valid role.  Try flags.initrole first. */
1991.     if (!validrole(flags.initrole)) {
1992.         /* Try the player letter second */
1993.         if ((flags.initrole = str2role(svp.pl_character)) < 0)
1994.             /* None specified; pick a random role */
1995.             flags.initrole = randrole_filtered();
1996.     }
1997. 
1998.     /* We now have a valid role index.  Copy the role name back. */
1999.     /* This should become OBSOLETE */
2000.     Strcpy(svp.pl_character, roles[flags.initrole].name.m);
2001.     svp.pl_character[PL_CSIZ - 1] = '\0';
2002. 
2003.     /* Check for a valid race */
2004.     if (!validrace(flags.initrole, flags.initrace))
2005.         flags.initrace = randrace(flags.initrole);
2006. 
2007.     /* Check for a valid gender.  If new game, check both initgend
2008.      * and female.  On restore, assume flags.female is correct. */
2009.     if (flags.pantheon == -1) { /* new game */
2010.         if (!validgend(flags.initrole, flags.initrace, flags.female))
2011.             flags.female = !flags.female;
2012.     }
2013.     if (!validgend(flags.initrole, flags.initrace, flags.initgend))
2014.         /* Note that there is no way to check for an unspecified gender. */
2015.         flags.initgend = flags.female;
2016. 
2017.     /* Check for a valid alignment */
2018.     if (!validalign(flags.initrole, flags.initrace, flags.initalign))
2019.         /* Pick a random alignment */
2020.         flags.initalign = randalign(flags.initrole, flags.initrace);
2021.     alignmnt = aligns[flags.initalign].value;
2022. 
2023.     /* Initialize gu.urole and gu.urace */
2024.     gu.urole = roles[flags.initrole];
2025.     gu.urace = races[flags.initrace];
2026. 
2027.     /* Fix up the quest leader */
2028.     if (gu.urole.ldrnum != NON_PM) {
2029.         pm = &mons[gu.urole.ldrnum];
2030.         pm->msound = MS_LEADER;
2031.         pm->mflags2 |= (M2_PEACEFUL);
2032.         pm->mflags3 |= M3_CLOSE;
2033.         pm->maligntyp = alignmnt * 3;
2034.         /* if gender is random, we choose it now instead of waiting
2035.            until the leader monster is created */
2036.         svq.quest_status.ldrgend =
2037.             is_neuter(pm) ? 2 : is_female(pm) ? 1 : is_male(pm)
2038.                                                         ? 0
2039.                                                         : (rn2(100) < 50);
2040.     }
2041. 
2042.     /* Fix up the quest guardians */
2043.     if (gu.urole.guardnum != NON_PM) {
2044.         pm = &mons[gu.urole.guardnum];
2045.         pm->mflags2 |= (M2_PEACEFUL);
2046.         pm->maligntyp = alignmnt * 3;
2047.     }
2048. 
2049.     /* Fix up the quest nemesis */
2050.     if (gu.urole.neminum != NON_PM) {
2051.         pm = &mons[gu.urole.neminum];
2052.         pm->msound = MS_NEMESIS;
2053.         pm->mflags2 &= ~(M2_PEACEFUL);
2054.         pm->mflags2 |= (M2_NASTY | M2_STALK | M2_HOSTILE);
2055.         pm->mflags3 &= ~(M3_CLOSE);
2056.         pm->mflags3 |= M3_WANTSARTI | M3_WAITFORU;
2057.         /* if gender is random, we choose it now instead of waiting
2058.            until the nemesis monster is created */
2059.         svq.quest_status.nemgend = is_neuter(pm) ? 2 : is_female(pm) ? 1
2060.                                    : is_male(pm) ? 0 : (rn2(100) < 50);
2061.     }
2062. 
2063.     /* Fix up the god names */
2064.     if (flags.pantheon == -1) {             /* new game */
2065.         int trycnt = 0;
2066.         flags.pantheon = flags.initrole;    /* use own gods */
2067.         /* unless they're missing */
2068.         while (!roles[flags.pantheon].lgod && ++trycnt < 100)
2069.             flags.pantheon = randrole(FALSE);
2070.         if (!roles[flags.pantheon].lgod) {
2071.             int i;
2072.             for (i = 0; i < SIZE(roles) - 1; i++)
2073.                 if (roles[i].lgod) {
2074.                     flags.pantheon = i;
2075.                     break;
2076.                 }
2077.         }
2078.     }
2079.     if (!gu.urole.lgod) {
2080.         gu.urole.lgod = roles[flags.pantheon].lgod;
2081.         gu.urole.ngod = roles[flags.pantheon].ngod;
2082.         gu.urole.cgod = roles[flags.pantheon].cgod;
2083.     }
2084.     /* 0 or 1; no gods are neuter, nor is gender randomized */
2085.     svq.quest_status.godgend = !strcmpi(align_gtitle(alignmnt), "goddess");
2086. 
2087.     if (Role_if(PM_CLERIC))
2088.         objects[SPE_LIGHT].oc_skill = P_CLERIC_SPELL;
2089. 
2090. #if 0
2091. /*
2092.  * Disable this fixup so that mons[] can be const.  The only
2093.  * place where it actually matters for the hero is in set_uasmon()
2094.  * and that can use mons[race] rather than mons[role] for this
2095.  * particular property.  Despite the comment, it is checked--where
2096.  * needed--via intrinsic 'Infravision' which set_uasmon() manages.
2097.  */
2098.     /* Fix up infravision */
2099.     if (mons[gu.urace.mnum].mflags3 & M3_INFRAVISION) {
2100.         /* although an infravision intrinsic is possible, infravision
2101.          * is purely a property of the physical race.  This means that we
2102.          * must put the infravision flag in the player's current race
2103.          * (either that or have separate permonst entries for
2104.          * elven/non-elven members of each class).  The side effect is that
2105.          * all NPCs of that class will have (probably bogus) infravision,
2106.          * but since infravision has no effect for NPCs anyway we can
2107.          * ignore this.
2108.          */
2109.         mons[gu.urole.mnum].mflags3 |= M3_INFRAVISION;
2110.     }
2111. #endif /*0*/
2112. 
2113.     /* Artifacts are fixed in hack_artifacts() */
2114. 
2115.     /* Success! */
2116.     return;
2117. }
2118. 
2119. const char *
2120. Hello(struct monst *mtmp)
2121. {
2122.     switch (Role_switch) {
2123.     case PM_KNIGHT:
2124.         return "Salutations"; /* Olde English */
2125.     case PM_SAMURAI:
2126.         return (mtmp && mtmp->data == &mons[PM_SHOPKEEPER])
2127.                     ? "Irasshaimase"
2128.                     : "Konnichi wa"; /* Japanese */
2129.     case PM_TOURIST:
2130.         return "Aloha"; /* Hawaiian */
2131.     case PM_VALKYRIE:
2132.         return
2133. #ifdef MAIL_STRUCTURES
2134.                (mtmp && mtmp->data == &mons[PM_MAIL_DAEMON]) ? "Hallo" :
2135. #endif
2136.                "Velkommen"; /* Norse */
2137.     default:
2138.         return "Hello";
2139.     }
2140. }
2141. 
2142. const char *
2143. Goodbye(void)
2144. {
2145.     switch (Role_switch) {
2146.     case PM_KNIGHT:
2147.         return "Fare thee well"; /* Olde English */
2148.     case PM_SAMURAI:
2149.         return "Sayonara"; /* Japanese */
2150.     case PM_TOURIST:
2151.         return "Aloha"; /* Hawaiian */
2152.     case PM_VALKYRIE:
2153.         return "Farvel"; /* Norse */
2154.     default:
2155.         return "Goodbye";
2156.     }
2157. }
2158. 
2159. /* if pmindex is any player race (not necessarily the hero's),
2160.    return a pointer to the races[] entry for it; if pmindex is for some
2161.    other type of monster which isn't a player race, return Null */
2162. const struct Race *
2163. character_race(short pmindex)
2164. {
2165.     const struct Race *r;
2166. 
2167.     for (r = races; r->noun != NULL; ++r)
2168.         if (r->mnum == pmindex)
2169.             return r;
2170.     return (const struct Race *) NULL;
2171. }
2172. 
2173. /*--------------------------------------------------------------------------*/
2174. 
2175. /* potential interface routine */
2176. void
2177. genl_player_selection(void)
2178. {
2179.     if (genl_player_setup(0))
2180.         return;
2181. 
2182.     /* player cancelled role/race/&c selection, so quit */
2183.     nh_terminate(EXIT_SUCCESS);
2184.     /*NOTREACHED*/
2185. }
2186. 
2187. #if defined(TTY_GRAPHICS) || defined(CURSES_GRAPHICS) || defined(SHIM_GRAPHICS)
2188. /* ['#else' far below] */
2189. 
2190. staticfn boolean reset_role_filtering(void);
2191. staticfn winid plsel_startmenu(int, int);
2192. staticfn int maybe_skip_seps(int, int);
2193. staticfn void setup_rolemenu(winid, boolean, int, int, int);
2194. staticfn void setup_racemenu(winid, boolean, int, int, int);
2195. staticfn void setup_gendmenu(winid, boolean, int, int, int);
2196. staticfn void setup_algnmenu(winid, boolean, int, int, int);
2197. 
2198. /* try to reduce clutter in the code below... */
2199. #define ROLE flags.initrole
2200. #define RACE flags.initrace
2201. #define GEND flags.initgend
2202. #define ALGN flags.initalign
2203. 
2204. /* guts of tty's player_selection() */
2205. int
2206. genl_player_setup(int screenheight)
2207. {
2208.     char pbuf[QBUFSZ];
2209.     anything any;
2210.     int i, k, n, choice, nextpick;
2211.     boolean getconfirmation, picksomething;
2212.     winid win = WIN_ERR;
2213.     menu_item *selected = 0;
2214.     int clr = NO_COLOR;
2215.     char pick4u = 'n';
2216.     int result = 0; /* assume failure (player chooses to 'quit') */
2217. 
2218.     program_state.in_role_selection++; /* affects tty menu cleanup */
2219.     /* Used to avoid "Is this ok?" if player has already specified all
2220.      * four facets of role.
2221.      * Note that rigid_role_checks might force any unspecified facets to
2222.      * have a specific value, but that will still require confirmation;
2223.      * player can specify the forced ones if avoiding that is demanded.
2224.      */
2225.     picksomething = (ROLE == ROLE_NONE || RACE == ROLE_NONE
2226.                      || GEND == ROLE_NONE || ALGN == ROLE_NONE);
2227.     /* Used for '-@';
2228.      * choose randomly without asking for all unspecified facets.
2229.      */
2230.     if (flags.randomall && picksomething) {
2231.         if (ROLE == ROLE_NONE)
2232.             ROLE = ROLE_RANDOM;
2233.         if (RACE == ROLE_NONE)
2234.             RACE = ROLE_RANDOM;
2235.         if (GEND == ROLE_NONE)
2236.             GEND = ROLE_RANDOM;
2237.         if (ALGN == ROLE_NONE)
2238.             ALGN = ROLE_RANDOM;
2239.     }
2240. 
2241.     /* prevent unnecessary prompting if role forces race (samurai) or gender
2242.        (valkyrie) or alignment (rogue), or race forces alignment (orc), &c */
2243.     rigid_role_checks();
2244. 
2245.     if (ROLE == ROLE_NONE || RACE == ROLE_NONE
2246.         || GEND == ROLE_NONE || ALGN == ROLE_NONE) {
2247.         char *prompt = build_plselection_prompt(pbuf, QBUFSZ,
2248.                                                 ROLE, RACE, GEND, ALGN);
2249.         /* prompt[] contains "Shall I pick ... for you? [ynaq] "
2250.            y - game picks role,&c then asks player to confirm;
2251.            n - player manually chooses via menu selections;
2252.            a - like 'y', but skips confirmation and starts game;
2253.            q - quit
2254.          */
2255. #if 1
2256.         trimspaces(prompt); /* 'prompt' is constructed with trailing space */
2257.         /* accept any character and do validation ourselves so that we can
2258.            shorten prompt; it will be "Shall I pick ... for you? [ynaq] "
2259.            with final space appended by yn_function() [for tty at least] */
2260.         do {
2261.             pick4u = yn_function(prompt, (char *) 0, '\0', FALSE);
2262.             pick4u = lowc(pick4u);
2263.             if (pick4u == '\033' || pick4u == 'q') /* handle [q] */
2264.                 goto setup_done;
2265.             if (pick4u == ' ' || pick4u == '\n' || pick4u == '\r')
2266.                 pick4u = 'y'; /* default */
2267.             else if (pick4u == '@' || pick4u == '*')
2268.                 pick4u = 'a'; /* similar to '-@' on command line */
2269.             /* TODO? handle response of '?' */
2270.         } while (pick4u != 'y' && pick4u != 'n' && pick4u != 'a'); /* [yna] */
2271. 
2272. #else /* slightly simpler but more likely to end up being wrapped */
2273. 
2274.         char *p;
2275.         /* strip choices off prompt string; yn_function() will show them */
2276.         if ((p = strchr(prompt, '[')) != 0)
2277.             *p = '\0';
2278.         trimspaces(prompt); /* remove trailing space */
2279.         /* prompt becomes "Shall I pick ... for you? [ynaq] (y) "
2280.            with " [ynaq] (y) " appended by yn_function() which also changes
2281.            user's <space> and <return> to 'y', <escape> to 'q' */
2282.         pick4u = yn_function(prompt, ynaqchars, 'y', FALSE);
2283.         if (pick4u != 'y' && pick4u != 'a' && pick4u != 'n')
2284.             goto setup_done; /* bail */
2285. #endif
2286.     }
2287. 
2288.  makepicks:
2289.     nextpick = RS_ROLE;
2290.     do {
2291.         if (nextpick == RS_ROLE) {
2292.             nextpick = RS_RACE;
2293.             /* Select a role, if necessary;
2294.                we'll try to be compatible with pre-selected
2295.                race/gender/alignment, but may not succeed. */
2296.             if (ROLE < 0) {
2297.                 /* process the choice */
2298.                 if (pick4u == 'y' || pick4u == 'a' || ROLE == ROLE_RANDOM) {
2299.                     /* pick a random role */
2300.                     k = pick_role(RACE, GEND, ALGN, PICK_RANDOM);
2301.                     if (k < 0) {
2302.                         pline("Incompatible role!");
2303.                         k = randrole(FALSE);
2304.                     }
2305.                 } else {
2306.                     /* 'excess' is used to try to avoid tty pagination */
2307.                     int excess = maybe_skip_seps(screenheight, RS_ROLE);
2308. 
2309.                     /* prompt for a role */
2310.                     win = plsel_startmenu(screenheight, RS_ROLE);
2311.                     /* populate the menu with role choices */
2312.                     setup_rolemenu(win, TRUE, RACE, GEND, ALGN);
2313.                     /* add miscellaneous menu entries */
2314.                     role_menu_extra(ROLE_RANDOM, win, TRUE);
2315.                     any = cg.zeroany; /* separator, not a choice */
2316.                     if (excess < 1 || excess > 2)
2317.                         add_menu_str(win, "");
2318.                     role_menu_extra(RS_RACE, win, FALSE);
2319.                     role_menu_extra(RS_GENDER, win, FALSE);
2320.                     role_menu_extra(RS_ALGNMNT, win, FALSE);
2321.                     role_menu_extra(RS_filter, win, FALSE);
2322.                     role_menu_extra(ROLE_NONE, win, FALSE); /* quit */
2323.                     Strcpy(pbuf, "Pick a role or profession");
2324.                     end_menu(win, pbuf);
2325.                     n = select_menu(win, PICK_ONE, &selected);
2326.                     /*
2327.                      * PICK_ONE with preselected choice behaves strangely:
2328.                      *  n == -1 -- <escape>, so use quit choice;
2329.                      *  n ==  0 -- explicitly chose preselected entry,
2330.                      *             toggling it off, so use it;
2331.                      *  n ==  1 -- implicitly chose preselected entry
2332.                      *             with <space> or <return>;
2333.                      *  n ==  2 -- explicitly chose a different entry, so
2334.                      *             both it and preselected one are in list.
2335.                      */
2336.                     if (n > 0) {
2337.                         choice = selected[0].item.a_int;
2338.                         if (n > 1 && choice == ROLE_RANDOM)
2339.                             choice = selected[1].item.a_int;
2340.                     } else
2341.                         choice = (n == 0) ? ROLE_RANDOM : ROLE_NONE;
2342.                     if (selected)
2343.                         free((genericptr_t) selected), selected = 0;
2344.                     destroy_nhwindow(win), win = WIN_ERR;
2345. 
2346.                     if (choice == ROLE_NONE) {
2347.                         goto setup_done; /* selected quit */
2348.                     } else if (choice == RS_menu_arg(RS_ALGNMNT)) {
2349.                         ALGN = k = ROLE_NONE;
2350.                         nextpick = RS_ALGNMNT;
2351.                     } else if (choice == RS_menu_arg(RS_GENDER)) {
2352.                         GEND = k = ROLE_NONE;
2353.                         nextpick = RS_GENDER;
2354.                     } else if (choice == RS_menu_arg(RS_RACE)) {
2355.                         RACE = k = ROLE_NONE;
2356.                         nextpick = RS_RACE;
2357.                     } else if (choice == RS_menu_arg(RS_filter)) {
2358.                         ROLE = k = ROLE_NONE;
2359.                         (void) reset_role_filtering();
2360.                         nextpick = RS_ROLE;
2361.                     } else if (choice == ROLE_RANDOM) {
2362.                         k = pick_role(RACE, GEND, ALGN, PICK_RANDOM);
2363.                         if (k < 0)
2364.                             k = randrole(FALSE);
2365.                     } else {
2366.                         k = choice - 1;
2367.                     }
2368.                 }
2369.                 ROLE = k;
2370.             } /* needed role */
2371.         }     /* picking role */
2372. 
2373.         if (nextpick == RS_RACE) {
2374.             nextpick = (ROLE < 0) ? RS_ROLE : RS_GENDER;
2375.             /* Select a race, if necessary;
2376.                force compatibility with role, try for compatibility
2377.                with pre-selected gender/alignment. */
2378.             if (RACE < 0 || !validrace(ROLE, RACE)) {
2379.                 /* no race yet, or pre-selected race not valid */
2380.                 if (pick4u == 'y' || pick4u == 'a' || RACE == ROLE_RANDOM) {
2381.                     k = pick_race(ROLE, GEND, ALGN, PICK_RANDOM);
2382.                     if (k < 0) {
2383.                         pline("Incompatible race!");
2384.                         k = randrace(ROLE);
2385.                     }
2386.                 } else { /* pick4u == 'n' */
2387.                     /* Count the number of valid races */
2388.                     n = 0; /* number valid */
2389.                     k = 0; /* valid race */
2390.                     for (i = 0; races[i].noun; i++)
2391.                         if (ok_race(ROLE, i, GEND, ALGN)) {
2392.                             n++;
2393.                             k = i;
2394.                         }
2395.                     if (n == 0) {
2396.                         for (i = 0; races[i].noun; i++)
2397.                             if (validrace(ROLE, i)) {
2398.                                 n++;
2399.                                 k = i;
2400.                             }
2401.                     }
2402.                     /* Permit the user to pick, if there is more than one */
2403.                     if (n > 1) {
2404.                         win = plsel_startmenu(screenheight, RS_RACE);
2405.                         any = cg.zeroany; /* zero out all bits */
2406.                         /* populate the menu with role choices */
2407.                         setup_racemenu(win, TRUE, ROLE, GEND, ALGN);
2408.                         /* add miscellaneous menu entries */
2409.                         role_menu_extra(ROLE_RANDOM, win, TRUE);
2410.                         any.a_int = 0; /* separator, not a choice */
2411.                         add_menu_str(win, "");
2412.                         role_menu_extra(RS_ROLE, win, FALSE);
2413.                         role_menu_extra(RS_GENDER, win, FALSE);
2414.                         role_menu_extra(RS_ALGNMNT, win, FALSE);
2415.                         role_menu_extra(RS_filter, win, FALSE);
2416.                         role_menu_extra(ROLE_NONE, win, FALSE); /* quit */
2417.                         Strcpy(pbuf, "Pick a race or species");
2418.                         end_menu(win, pbuf);
2419.                         n = select_menu(win, PICK_ONE, &selected);
2420.                         if (n > 0) {
2421.                             choice = selected[0].item.a_int;
2422.                             if (n > 1 && choice == ROLE_RANDOM)
2423.                                 choice = selected[1].item.a_int;
2424.                         } else
2425.                             choice = (n == 0) ? ROLE_RANDOM : ROLE_NONE;
2426.                         if (selected)
2427.                             free((genericptr_t) selected), selected = 0;
2428.                         destroy_nhwindow(win), win = WIN_ERR;
2429. 
2430.                         if (choice == ROLE_NONE) {
2431.                             goto setup_done; /* selected quit */
2432.                         } else if (choice == RS_menu_arg(RS_ALGNMNT)) {
2433.                             ALGN = k = ROLE_NONE;
2434.                             nextpick = RS_ALGNMNT;
2435.                         } else if (choice == RS_menu_arg(RS_GENDER)) {
2436.                             GEND = k = ROLE_NONE;
2437.                             nextpick = RS_GENDER;
2438.                         } else if (choice == RS_menu_arg(RS_ROLE)) {
2439.                             ROLE = k = ROLE_NONE;
2440.                             nextpick = RS_ROLE;
2441.                         } else if (choice == RS_menu_arg(RS_filter)) {
2442.                             RACE = k = ROLE_NONE;
2443.                             if (reset_role_filtering())
2444.                                 nextpick = RS_ROLE;
2445.                             else
2446.                                 nextpick = RS_RACE;
2447.                         } else if (choice == ROLE_RANDOM) {
2448.                             k = pick_race(ROLE, GEND, ALGN, PICK_RANDOM);
2449.                             if (k < 0)
2450.                                 k = randrace(ROLE);
2451.                         } else {
2452.                             k = choice - 1;
2453.                         }
2454.                     }
2455.                 }
2456.                 RACE = k;
2457.             } /* needed race */
2458.         }     /* picking race */
2459. 
2460.         if (nextpick == RS_GENDER) {
2461.             nextpick = (ROLE < 0) ? RS_ROLE : (RACE < 0) ? RS_RACE
2462.                        : RS_ALGNMNT;
2463.             /* Select a gender, if necessary;
2464.                force compatibility with role/race, try for compatibility
2465.                with pre-selected alignment. */
2466.             if (GEND < 0 || !validgend(ROLE, RACE, GEND)) {
2467.                 /* no gender yet, or pre-selected gender not valid */
2468.                 if (pick4u == 'y' || pick4u == 'a' || GEND == ROLE_RANDOM) {
2469.                     k = pick_gend(ROLE, RACE, ALGN, PICK_RANDOM);
2470.                     if (k < 0) {
2471.                         pline("Incompatible gender!");
2472.                         k = randgend(ROLE, RACE);
2473.                     }
2474.                 } else { /* pick4u == 'n' */
2475.                     /* Count the number of valid genders */
2476.                     n = 0; /* number valid */
2477.                     k = 0; /* valid gender */
2478.                     for (i = 0; i < ROLE_GENDERS; i++)
2479.                         if (ok_gend(ROLE, RACE, i, ALGN)) {
2480.                             n++;
2481.                             k = i;
2482.                         }
2483.                     if (n == 0) {
2484.                         for (i = 0; i < ROLE_GENDERS; i++)
2485.                             if (validgend(ROLE, RACE, i)) {
2486.                                 n++;
2487.                                 k = i;
2488.                             }
2489.                     }
2490.                     /* Permit the user to pick, if there is more than one */
2491.                     if (n > 1) {
2492.                         win = plsel_startmenu(screenheight, RS_GENDER);
2493.                         any = cg.zeroany; /* zero out all bits */
2494.                         /* populate the menu with gender choices */
2495.                         setup_gendmenu(win, TRUE, ROLE, RACE, ALGN);
2496.                         /* add miscellaneous menu entries */
2497.                         role_menu_extra(ROLE_RANDOM, win, TRUE);
2498.                         any.a_int = 0; /* separator, not a choice */
2499.                         add_menu_str(win, "");
2500.                         role_menu_extra(RS_ROLE, win, FALSE);
2501.                         role_menu_extra(RS_RACE, win, FALSE);
2502.                         role_menu_extra(RS_ALGNMNT, win, FALSE);
2503.                         role_menu_extra(RS_filter, win, FALSE);
2504.                         role_menu_extra(ROLE_NONE, win, FALSE); /* quit */
2505.                         Strcpy(pbuf, "Pick a gender or sex");
2506.                         end_menu(win, pbuf);
2507.                         n = select_menu(win, PICK_ONE, &selected);
2508.                         if (n > 0) {
2509.                             choice = selected[0].item.a_int;
2510.                             if (n > 1 && choice == ROLE_RANDOM)
2511.                                 choice = selected[1].item.a_int;
2512.                         } else
2513.                             choice = (n == 0) ? ROLE_RANDOM : ROLE_NONE;
2514.                         if (selected)
2515.                             free((genericptr_t) selected), selected = 0;
2516.                         destroy_nhwindow(win), win = WIN_ERR;
2517. 
2518.                         if (choice == ROLE_NONE) {
2519.                             goto setup_done; /* selected quit */
2520.                         } else if (choice == RS_menu_arg(RS_ALGNMNT)) {
2521.                             ALGN = k = ROLE_NONE;
2522.                             nextpick = RS_ALGNMNT;
2523.                         } else if (choice == RS_menu_arg(RS_RACE)) {
2524.                             RACE = k = ROLE_NONE;
2525.                             nextpick = RS_RACE;
2526.                         } else if (choice == RS_menu_arg(RS_ROLE)) {
2527.                             ROLE = k = ROLE_NONE;
2528.                             nextpick = RS_ROLE;
2529.                         } else if (choice == RS_menu_arg(RS_filter)) {
2530.                             GEND = k = ROLE_NONE;
2531.                             if (reset_role_filtering())
2532.                                 nextpick = RS_ROLE;
2533.                             else
2534.                                 nextpick = RS_GENDER;
2535.                         } else if (choice == ROLE_RANDOM) {
2536.                             k = pick_gend(ROLE, RACE, ALGN, PICK_RANDOM);
2537.                             if (k < 0)
2538.                                 k = randgend(ROLE, RACE);
2539.                         } else {
2540.                             k = choice - 1;
2541.                         }
2542.                     }
2543.                 }
2544.                 GEND = k;
2545.             } /* needed gender */
2546.         }     /* picking gender */
2547. 
2548.         if (nextpick == RS_ALGNMNT) {
2549.             nextpick = (ROLE < 0) ? RS_ROLE
2550.                        : (RACE < 0) ? RS_RACE
2551.                          : RS_GENDER;
2552.             /* Select an alignment, if necessary;
2553.                force compatibility with role/race/gender. */
2554.             if (ALGN < 0 || !validalign(ROLE, RACE, ALGN)) {
2555.                 /* no alignment yet, or pre-selected alignment not valid */
2556.                 if (pick4u == 'y' || pick4u == 'a' || ALGN == ROLE_RANDOM) {
2557.                     k = pick_align(ROLE, RACE, GEND, PICK_RANDOM);
2558.                     if (k < 0) {
2559.                         pline("Incompatible alignment!");
2560.                         k = randalign(ROLE, RACE);
2561.                     }
2562.                 } else { /* pick4u == 'n' */
2563.                     /* Count the number of valid alignments */
2564.                     n = 0; /* number valid */
2565.                     k = 0; /* valid alignment */
2566.                     for (i = 0; i < ROLE_ALIGNS; i++)
2567.                         if (ok_align(ROLE, RACE, GEND, i)) {
2568.                             n++;
2569.                             k = i;
2570.                         }
2571.                     if (n == 0) {
2572.                         for (i = 0; i < ROLE_ALIGNS; i++)
2573.                             if (validalign(ROLE, RACE, i)) {
2574.                                 n++;
2575.                                 k = i;
2576.                             }
2577.                     }
2578.                     /* Permit the user to pick, if there is more than one */
2579.                     if (n > 1) {
2580.                         win = plsel_startmenu(screenheight, RS_ALGNMNT);
2581.                         any = cg.zeroany; /* zero out all bits */
2582.                         setup_algnmenu(win, TRUE, ROLE, RACE, GEND);
2583.                         role_menu_extra(ROLE_RANDOM, win, TRUE);
2584.                         any.a_int = 0; /* separator, not a choice */
2585.                         add_menu_str(win, "");
2586.                         role_menu_extra(RS_ROLE, win, FALSE);
2587.                         role_menu_extra(RS_RACE, win, FALSE);
2588.                         role_menu_extra(RS_GENDER, win, FALSE);
2589.                         role_menu_extra(RS_filter, win, FALSE);
2590.                         role_menu_extra(ROLE_NONE, win, FALSE); /* quit */
2591.                         Strcpy(pbuf, "Pick an alignment or creed");
2592.                         end_menu(win, pbuf);
2593.                         n = select_menu(win, PICK_ONE, &selected);
2594.                         if (n > 0) {
2595.                             choice = selected[0].item.a_int;
2596.                             if (n > 1 && choice == ROLE_RANDOM)
2597.                                 choice = selected[1].item.a_int;
2598.                         } else
2599.                             choice = (n == 0) ? ROLE_RANDOM : ROLE_NONE;
2600.                         if (selected)
2601.                             free((genericptr_t) selected), selected = 0;
2602.                         destroy_nhwindow(win), win = WIN_ERR;
2603. 
2604.                         if (choice == ROLE_NONE) {
2605.                             goto setup_done; /* selected quit */
2606.                         } else if (choice == RS_menu_arg(RS_GENDER)) {
2607.                             GEND = k = ROLE_NONE;
2608.                             nextpick = RS_GENDER;
2609.                         } else if (choice == RS_menu_arg(RS_RACE)) {
2610.                             RACE = k = ROLE_NONE;
2611.                             nextpick = RS_RACE;
2612.                         } else if (choice == RS_menu_arg(RS_ROLE)) {
2613.                             ROLE = k = ROLE_NONE;
2614.                             nextpick = RS_ROLE;
2615.                         } else if (choice == RS_menu_arg(RS_filter)) {
2616.                             ALGN = k = ROLE_NONE;
2617.                             if (reset_role_filtering())
2618.                                 nextpick = RS_ROLE;
2619.                             else
2620.                                 nextpick = RS_ALGNMNT;
2621.                         } else if (choice == ROLE_RANDOM) {
2622.                             k = pick_align(ROLE, RACE, GEND, PICK_RANDOM);
2623.                             if (k < 0)
2624.                                 k = randalign(ROLE, RACE);
2625.                         } else {
2626.                             k = choice - 1;
2627.                         }
2628.                     }
2629.                 }
2630.                 ALGN = k;
2631.             } /* needed alignment */
2632.         }     /* picking alignment */
2633. 
2634.     } while (ROLE < 0 || RACE < 0 || GEND < 0 || ALGN < 0);
2635. 
2636.     /*
2637.      *  Role, race, &c have now been determined;
2638.      *  ask for confirmation and maybe go back to choose all over again.
2639.      *
2640.      *  Uses ynaq for familiarity, although 'a' is usually a
2641.      *  superset of 'y' but here is an alternate form of 'n'.
2642.      *  Menu layout:
2643.      *   title:  Is this ok? [ynaq]
2644.      *   blank:
2645.      *    text:  $name, $alignment $gender $race $role
2646.      *   blank:
2647.      *    menu:  y + yes; play
2648.      *           n - no; pick again
2649.      *   maybe:  a - no; rename hero
2650.      *           q - quit
2651.      *           (end)
2652.      */
2653.     getconfirmation = (picksomething && pick4u != 'a' && !flags.randomall);
2654.     while (getconfirmation) {
2655.         win = plsel_startmenu(screenheight, RS_filter); /* filter: not ROLE */
2656.         any = cg.zeroany; /* zero out all bits */
2657.         /* [ynaq] menu choices */
2658.         any.a_int = 1;
2659.         add_menu(win, &nul_glyphinfo, &any, 'y', 0,
2660.                  ATR_NONE, clr, "Yes; start game", MENU_ITEMFLAGS_SELECTED);
2661.         any.a_int = 2;
2662.         add_menu(win, &nul_glyphinfo, &any, 'n', 0,
2663.                  ATR_NONE, clr, "No; choose role again", MENU_ITEMFLAGS_NONE);
2664.         if (iflags.renameallowed) {
2665.             any.a_int = 3;
2666.             add_menu(win, &nul_glyphinfo, &any, 'a', 0, ATR_NONE,
2667.                      clr, "Not yet; choose another name",
2668.                      MENU_ITEMFLAGS_NONE);
2669.         }
2670.         any.a_int = -1;
2671.         add_menu(win, &nul_glyphinfo, &any, 'q', 0,
2672.                  ATR_NONE, clr, "Quit", MENU_ITEMFLAGS_NONE);
2673.         Sprintf(pbuf, "Is this ok? [yn%sq]", iflags.renameallowed ? "a" : "");
2674.         end_menu(win, pbuf);
2675.         n = select_menu(win, PICK_ONE, &selected);
2676.         /* [pick-one menus with a preselected entry behave oddly...] */
2677.         choice = (n > 0) ? selected[n - 1].item.a_int : (n == 0) ? 1 : -1;
2678.         if (selected)
2679.             free((genericptr_t) selected), selected = 0;
2680.         destroy_nhwindow(win);
2681. 
2682.         switch (choice) {
2683.         default: /* 'q' or ESC */
2684.             goto setup_done; /* quit */
2685.             break;
2686.         case 3: { /* 'a' */
2687.             /*
2688.              * TODO: what, if anything, should be done if the name is
2689.              * changed to or from "wizard" after port-specific startup
2690.              * code has set flags.debug based on the original name?
2691.              */
2692.             int saveROLE, saveRACE, saveGEND, saveALGN;
2693. 
2694.             iflags.renameinprogress = TRUE; /* affects main() in unixmain.c */
2695.             /* plnamesuffix() can change any or all of ROLE, RACE,
2696.                GEND, ALGN; we'll override that and honor only the name */
2697.             saveROLE = ROLE, saveRACE = RACE,
2698.             saveGEND = GEND, saveALGN = ALGN;
2699.             svp.plname[0] = '\0';
2700.             plnamesuffix(); /* calls askname() when svp.plname[] is empty */
2701.             ROLE = saveROLE, RACE = saveRACE,
2702.             GEND = saveGEND, ALGN = saveALGN;
2703.             break; /* getconfirmation is still True */
2704.         }
2705.         case 2: /* 'n' */
2706.             /* start fresh, but bypass "shall I pick everything for you?"
2707.                step; any partial role selection via config file, command
2708.                line, or name suffix is discarded this time */
2709.             pick4u = 'n';
2710.             ROLE = RACE = GEND = ALGN = ROLE_NONE;
2711.             goto makepicks;
2712.             break;
2713.         case 1: /* 'y' or Space or Return/Enter */
2714.             /* success; drop out through end of function */
2715.             getconfirmation = FALSE;
2716.             break;
2717.         }
2718.     } /* while 'getconfirmation' */
2719.     /* Success! */
2720.     result = 1;
2721. 
2722.  setup_done:
2723.     program_state.in_role_selection--;
2724.     return result;
2725. }
2726. 
2727. staticfn boolean
2728. reset_role_filtering(void)
2729. {
2730.     winid win;
2731.     int i, n;
2732.     char filterprompt[QBUFSZ];
2733.     menu_item *selected = 0;
2734. 
2735.     win = create_nhwindow(NHW_MENU);
2736.     start_menu(win, MENU_BEHAVE_STANDARD);
2737. 
2738.     /* no extra blank line preceding this entry; end_menu supplies one */
2739.     add_menu_str(win, "Unacceptable roles");
2740.     setup_rolemenu(win, FALSE, ROLE_NONE, ROLE_NONE, ROLE_NONE);
2741. 
2742.     add_menu_str(win, "");
2743.     add_menu_str(win, "Unacceptable races");
2744.     setup_racemenu(win, FALSE, ROLE_NONE, ROLE_NONE, ROLE_NONE);
2745. 
2746.     add_menu_str(win, "");
2747.     add_menu_str(win, "Unacceptable genders");
2748.     setup_gendmenu(win, FALSE, ROLE_NONE, ROLE_NONE, ROLE_NONE);
2749. 
2750.     add_menu_str(win, "");
2751.     add_menu_str(win, "Unacceptable alignments");
2752.     setup_algnmenu(win, FALSE, ROLE_NONE, ROLE_NONE, ROLE_NONE);
2753. 
2754.     Sprintf(filterprompt, "Pick all that apply%s",
2755.             gotrolefilter() ? " and/or unpick any that no longer apply" : "");
2756.     end_menu(win, filterprompt);
2757.     n = select_menu(win, PICK_ANY, &selected);
2758. 
2759.     if (n >= 0) { /* n==0: clear current filters and don't set new ones */
2760.         clearrolefilter(RS_filter);
2761.         for (i = 0; i < n; i++)
2762.             setrolefilter(selected[i].item.a_string);
2763. 
2764.         ROLE = RACE = GEND = ALGN = ROLE_NONE;
2765.     }
2766.     if (selected)
2767.         free((genericptr_t) selected), selected = 0;
2768.     destroy_nhwindow(win);
2769.     return (n > 0) ? TRUE : FALSE;
2770. }
2771. 
2772. /* the change in format when this extended role selection was converted from
2773.    tty-only to tty+curses+? made the role selection menu require two pages
2774.    on a traditional 24-line tty; that wasn't fair to tty, so squeeze out
2775.    some blank separator lines from the menu if that will make it fit on one */
2776. staticfn int
2777. maybe_skip_seps(int rows, int aspect)
2778. {
2779.     int i, n = 0;
2780. 
2781.     /* not much point to generalizing this to other aspects */
2782.     if (aspect != RS_ROLE)
2783.         return 0;
2784.     /*
2785.      * If there are one or two excess lines, setup_rolemenu() will omit
2786.      * the separator between 'random' and 'pick race first'.  If there are
2787.      * two, plsel_startmenu() will omit the one between role info so far
2788.      * ("<role> <race> ...") and the set of role entries.
2789.      */
2790. 
2791.     n += 4; /* title and ensuing separator, role info so far and separator */
2792.     for (i = 0; roles[i].name.m; ++i)
2793.         if (ok_role(i, RACE, GEND, ALGN) && ok_race(i, RACE, GEND, ALGN)
2794.             && ok_gend(i, RACE, GEND, ALGN) && ok_align(i, RACE, GEND, ALGN))
2795.             ++n;
2796.     n += 2; /* 'random' and separator */
2797.     n += 5; /* race 1st, gender 1st, alignment 1st, reset filter, quit */
2798.     n += 1; /* footer/prompt */
2799.     if (rows > 0 && n > rows)
2800.         return n - rows;
2801.     return 0;
2802. }
2803. 
2804. /* start a menu; show role aspects specified so far as a header line */
2805. staticfn winid
2806. plsel_startmenu(int ttyrows, int aspect)
2807. {
2808.     char qbuf[QBUFSZ];
2809.     winid win;
2810.     const char *rolename;
2811. 
2812.     /* whatever aspect was just chosen might force others (Orc => chaotic,
2813.        Samurai => Human+lawful, Valkyrie => female) */
2814.     rigid_role_checks();
2815. 
2816.     rolename = (ROLE < 0) ? "<role>"
2817.                : (GEND == 1 && roles[ROLE].name.f) ? roles[ROLE].name.f
2818.                  : roles[ROLE].name.m;
2819.     if (!svp.plname[0] || ROLE < 0 || RACE < 0 || GEND < 0 || ALGN < 0) {
2820.         /* "<role> <race.noun> <gender> <alignment>" */
2821.         Sprintf(qbuf, "%.20s %.20s %.20s %.20s",
2822.                 rolename,
2823.                 (RACE < 0) ? "<race>" : races[RACE].noun,
2824.                 (GEND < 0) ? "<gender>" : genders[GEND].adj,
2825.                 (ALGN < 0) ? "<alignment>" : aligns[ALGN].adj);
2826.     } else {
2827.         /* "<name> the <alignment> <gender> <race.adjective> <role>" */
2828.         Sprintf(qbuf, "%.20s the %.20s %.20s %.20s %.20s",
2829.                 svp.plname,
2830.                 aligns[ALGN].adj,
2831.                 genders[GEND].adj,
2832.                 races[RACE].adj,
2833.                 rolename);
2834.     }
2835. 
2836.     win = create_nhwindow(NHW_MENU);
2837.     if (win == WIN_ERR)
2838.         panic("could not create role selection window");
2839.     start_menu(win, MENU_BEHAVE_STANDARD);
2840. 
2841.     add_menu_str(win, qbuf);
2842.     if (maybe_skip_seps(ttyrows, aspect) != 2)
2843.         add_menu_str(win, "");
2844.     return win;
2845. }
2846. 
2847. #undef ROLE
2848. #undef RACE
2849. #undef GEND
2850. #undef ALGN
2851. 
2852. /* add entries a-Archeologist, b-Barbarian, &c to menu being built in 'win' */
2853. staticfn void
2854. setup_rolemenu(
2855.     winid win,
2856.     boolean filtering, /* True => exclude filtered roles;
2857.                         * False => filter reset */
2858.     int race, int gend, int algn) /* all ROLE_NONE for !filtering case */
2859. {
2860.     anything any;
2861.     int i;
2862.     boolean role_ok;
2863.     char thisch, lastch = '\0', rolenamebuf[50];
2864.     int clr = NO_COLOR;
2865. 
2866.     any = cg.zeroany; /* zero out all bits */
2867.     for (i = 0; roles[i].name.m; i++) {
2868.         /* role can be constrained by any of race, gender, or alignment */
2869.         role_ok = (ok_role(i, race, gend, algn)
2870.                    && ok_race(i, race, gend, algn)
2871.                    && ok_gend(i, race, gend, algn)
2872.                    && ok_align(i, race, gend, algn));
2873.         if (filtering && !role_ok)
2874.             continue;
2875.         if (filtering)
2876.             any.a_int = i + 1;
2877.         else
2878.             any.a_string = roles[i].name.m;
2879.         thisch = lowc(*roles[i].name.m);
2880.         if (thisch == lastch)
2881.             thisch = highc(thisch);
2882.         Strcpy(rolenamebuf, roles[i].name.m);
2883.         if (roles[i].name.f) {
2884.             /* role has distinct name for female (C,P) */
2885.             if (gend == 1) {
2886.                 /* female already chosen; replace male name */
2887.                 Strcpy(rolenamebuf, roles[i].name.f);
2888.             } else if (gend < 0) {
2889.                 /* not chosen yet; append slash+female name */
2890.                 Strcat(rolenamebuf, "/");
2891.                 Strcat(rolenamebuf, roles[i].name.f);
2892.             }
2893.         }
2894.         /* !filtering implies reset_role_filtering() where we want to
2895.            mark this role as preselected if current filter excludes it */
2896.         add_menu(win, &nul_glyphinfo, &any, thisch, 0,
2897.                  ATR_NONE, clr, an(rolenamebuf),
2898.                  (!filtering && !role_ok)
2899.                     ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
2900.         lastch = thisch;
2901.     }
2902. }
2903. 
2904. staticfn void
2905. setup_racemenu(
2906.     winid win,
2907.     boolean filtering,
2908.     int role, int gend, int algn)
2909. {
2910.     anything any;
2911.     boolean race_ok;
2912.     int i;
2913.     char this_ch;
2914.     int clr = NO_COLOR;
2915. 
2916.     any = cg.zeroany;
2917.     for (i = 0; races[i].noun; i++) {
2918.         /* no ok_gend(); race isn't constrained by gender */
2919.         race_ok = (ok_race(role, i, gend, algn)
2920.                    && ok_role(role, i, gend, algn)
2921.                    && ok_align(role, i, gend, algn));
2922.         if (filtering && !race_ok)
2923.             continue;
2924.         if (filtering)
2925.             any.a_int = i + 1;
2926.         else
2927.             any.a_string = races[i].noun;
2928.         this_ch = *races[i].noun;
2929.         /* filtering: picking race, so choose by first letter, with
2930.            capital letter as unseen accelerator;
2931.            !filtering: resetting filter rather than picking, choose by
2932.            capital letter since lowercase role letters will be present */
2933.         add_menu(win, &nul_glyphinfo, &any,
2934.                  filtering ? this_ch : highc(this_ch),
2935.                  filtering ? highc(this_ch) : 0,
2936.                  ATR_NONE, clr, races[i].noun,
2937.                  (!filtering && !race_ok)
2938.                     ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
2939.     }
2940. }
2941. 
2942. staticfn void
2943. setup_gendmenu(
2944.     winid win,
2945.     boolean filtering,
2946.     int role, int race, int algn)
2947. {
2948.     anything any;
2949.     boolean gend_ok;
2950.     int i;
2951.     char this_ch;
2952.     int clr = NO_COLOR;
2953. 
2954.     any = cg.zeroany;
2955.     for (i = 0; i < ROLE_GENDERS; i++) {
2956.         /* no ok_align(); gender isn't constrained by alignment */
2957.         gend_ok = (ok_gend(role, race, i, algn)
2958.                    && ok_role(role, race, i, algn)
2959.                    && ok_race(role, race, i, algn));
2960.         if (filtering && !gend_ok)
2961.             continue;
2962.         if (filtering)
2963.             any.a_int = i + 1;
2964.         else
2965.             any.a_string = genders[i].adj;
2966.         this_ch = *genders[i].adj;
2967.         /* (see setup_racemenu for explanation of selector letters
2968.            and setup_rolemenu for preselection) */
2969.         add_menu(win, &nul_glyphinfo, &any,
2970.                  filtering ? this_ch : highc(this_ch),
2971.                  filtering ? highc(this_ch) : 0,
2972.                  ATR_NONE, clr, genders[i].adj,
2973.                  (!filtering && !gend_ok)
2974.                     ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
2975.     }
2976. }
2977. 
2978. staticfn void
2979. setup_algnmenu(
2980.     winid win,
2981.     boolean filtering,
2982.     int role, int race, int gend)
2983. {
2984.     anything any;
2985.     boolean algn_ok;
2986.     int i;
2987.     char this_ch;
2988.     int clr = NO_COLOR;
2989. 
2990.     any = cg.zeroany;
2991.     for (i = 0; i < ROLE_ALIGNS; i++) {
2992.         /* no ok_gend(); alignment isn't constrained by gender */
2993.         algn_ok = (ok_align(role, race, gend, i)
2994.                    && ok_role(role, race, gend, i)
2995.                    && ok_race(role, race, gend, i));
2996.         if (filtering && !algn_ok)
2997.             continue;
2998.         if (filtering)
2999.             any.a_int = i + 1;
3000.         else
3001.             any.a_string = aligns[i].adj;
3002.         this_ch = *aligns[i].adj;
3003.         /* (see setup_racemenu for explanation of selector letters
3004.            and setup_rolemenu for preselection) */
3005.         add_menu(win, &nul_glyphinfo, &any,
3006.                  filtering ? this_ch : highc(this_ch),
3007.                  filtering ? highc(this_ch) : 0,
3008.                  ATR_NONE, clr, aligns[i].adj,
3009.                  (!filtering && !algn_ok)
3010.                     ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
3011.     }
3012. }
3013. 
3014. #else /* !TTY_GRAPHICS */
3015. 
3016. int
3017. genl_player_setup(int screenheight UNUSED)
3018. {
3019.     return 0;
3020. }
3021. 
3022. #endif /* ?TTY_GRAPHICS */
3023. 
3024. /* role.c */