Source:NetHack 3.6.4/src/music.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to music.c from the source code of NetHack 3.6.4. To link to a particular line, write [[Source:NetHack 3.6.4/src/music.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 3.6	music.c	$NHDT-Date: 1573063606 2019/11/06 18:06:46 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.60 $ */
2. /*      Copyright (c) 1989 by Jean-Christophe Collet */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. /*
6.  * This file contains the different functions designed to manipulate the
7.  * musical instruments and their various effects.
8.  *
9.  * The list of instruments / effects is :
10.  *
11.  * (wooden) flute       may calm snakes if player has enough dexterity
12.  * magic flute          may put monsters to sleep:  area of effect depends
13.  *                      on player level.
14.  * (tooled) horn        Will awaken monsters:  area of effect depends on
15.  *                      player level.  May also scare monsters.
16.  * fire horn            Acts like a wand of fire.
17.  * frost horn           Acts like a wand of cold.
18.  * bugle                Will awaken soldiers (if any):  area of effect depends
19.  *                      on player level.
20.  * (wooden) harp        May calm nymph if player has enough dexterity.
21.  * magic harp           Charm monsters:  area of effect depends on player
22.  *                      level.
23.  * (leather) drum       Will awaken monsters like the horn.
24.  * drum of earthquake   Will initiate an earthquake whose intensity depends
25.  *                      on player level.  That is, it creates random pits
26.  *                      called here chasms.
27.  */
28. 
29. #include "hack.h"
30. 
31. STATIC_DCL void FDECL(awaken_monsters, (int));
32. STATIC_DCL void FDECL(put_monsters_to_sleep, (int));
33. STATIC_DCL void FDECL(charm_snakes, (int));
34. STATIC_DCL void FDECL(calm_nymphs, (int));
35. STATIC_DCL void FDECL(charm_monsters, (int));
36. STATIC_DCL void FDECL(do_earthquake, (int));
37. STATIC_DCL int FDECL(do_improvisation, (struct obj *));
38. 
39. #ifdef UNIX386MUSIC
40. STATIC_DCL int NDECL(atconsole);
41. STATIC_DCL void FDECL(speaker, (struct obj *, char *));
42. #endif
43. #ifdef VPIX_MUSIC
44. extern int sco_flag_console; /* will need changing if not _M_UNIX */
45. STATIC_DCL void NDECL(playinit);
46. STATIC_DCL void FDECL(playstring, (char *, size_t));
47. STATIC_DCL void FDECL(speaker, (struct obj *, char *));
48. #endif
49. #ifdef PCMUSIC
50. void FDECL(pc_speaker, (struct obj *, char *));
51. #endif
52. #ifdef AMIGA
53. void FDECL(amii_speaker, (struct obj *, char *, int));
54. #endif
55. 
56. /*
57.  * Wake every monster in range...
58.  */
59. 
60. STATIC_OVL void
61. awaken_monsters(distance)
62. int distance;
63. {
64.     register struct monst *mtmp;
65.     register int distm;
66. 
67.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
68.         if (DEADMONSTER(mtmp))
69.             continue;
70.         if ((distm = distu(mtmp->mx, mtmp->my)) < distance) {
71.             mtmp->msleeping = 0;
72.             mtmp->mcanmove = 1;
73.             mtmp->mfrozen = 0;
74.             /* may scare some monsters -- waiting monsters excluded */
75.             if (!unique_corpstat(mtmp->data)
76.                 && (mtmp->mstrategy & STRAT_WAITMASK) != 0)
77.                 mtmp->mstrategy &= ~STRAT_WAITMASK;
78.             else if (distm < distance / 3
79.                      && !resist(mtmp, TOOL_CLASS, 0, NOTELL)
80.                      /* some monsters are immune */
81.                      && onscary(0, 0, mtmp))
82.                 monflee(mtmp, 0, FALSE, TRUE);
83.         }
84.     }
85. }
86. 
87. /*
88.  * Make monsters fall asleep.  Note that they may resist the spell.
89.  */
90. 
91. STATIC_OVL void
92. put_monsters_to_sleep(distance)
93. int distance;
94. {
95.     register struct monst *mtmp;
96. 
97.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
98.         if (DEADMONSTER(mtmp))
99.             continue;
100.         if (distu(mtmp->mx, mtmp->my) < distance
101.             && sleep_monst(mtmp, d(10, 10), TOOL_CLASS)) {
102.             mtmp->msleeping = 1; /* 10d10 turns + wake_nearby to rouse */
103.             slept_monst(mtmp);
104.         }
105.     }
106. }
107. 
108. /*
109.  * Charm snakes in range.  Note that the snakes are NOT tamed.
110.  */
111. 
112. STATIC_OVL void
113. charm_snakes(distance)
114. int distance;
115. {
116.     register struct monst *mtmp;
117.     int could_see_mon, was_peaceful;
118. 
119.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
120.         if (DEADMONSTER(mtmp))
121.             continue;
122.         if (mtmp->data->mlet == S_SNAKE && mtmp->mcanmove
123.             && distu(mtmp->mx, mtmp->my) < distance) {
124.             was_peaceful = mtmp->mpeaceful;
125.             mtmp->mpeaceful = 1;
126.             mtmp->mavenge = 0;
127.             mtmp->mstrategy &= ~STRAT_WAITMASK;
128.             could_see_mon = canseemon(mtmp);
129.             mtmp->mundetected = 0;
130.             newsym(mtmp->mx, mtmp->my);
131.             if (canseemon(mtmp)) {
132.                 if (!could_see_mon)
133.                     You("notice %s, swaying with the music.", a_monnam(mtmp));
134.                 else
135.                     pline("%s freezes, then sways with the music%s.",
136.                           Monnam(mtmp),
137.                           was_peaceful ? "" : ", and now seems quieter");
138.             }
139.         }
140.     }
141. }
142. 
143. /*
144.  * Calm nymphs in range.
145.  */
146. 
147. STATIC_OVL void
148. calm_nymphs(distance)
149. int distance;
150. {
151.     register struct monst *mtmp;
152. 
153.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
154.         if (DEADMONSTER(mtmp))
155.             continue;
156.         if (mtmp->data->mlet == S_NYMPH && mtmp->mcanmove
157.             && distu(mtmp->mx, mtmp->my) < distance) {
158.             mtmp->msleeping = 0;
159.             mtmp->mpeaceful = 1;
160.             mtmp->mavenge = 0;
161.             mtmp->mstrategy &= ~STRAT_WAITMASK;
162.             if (canseemon(mtmp))
163.                 pline(
164.                     "%s listens cheerfully to the music, then seems quieter.",
165.                       Monnam(mtmp));
166.         }
167.     }
168. }
169. 
170. /* Awake soldiers anywhere the level (and any nearby monster). */
171. void
172. awaken_soldiers(bugler)
173. struct monst *bugler; /* monster that played instrument */
174. {
175.     register struct monst *mtmp;
176.     int distance, distm;
177. 
178.     /* distance of affected non-soldier monsters to bugler */
179.     distance = ((bugler == &youmonst) ? u.ulevel : bugler->data->mlevel) * 30;
180. 
181.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
182.         if (DEADMONSTER(mtmp))
183.             continue;
184.         if (is_mercenary(mtmp->data) && mtmp->data != &mons[PM_GUARD]) {
185.             mtmp->mpeaceful = mtmp->msleeping = mtmp->mfrozen = 0;
186.             mtmp->mcanmove = 1;
187.             mtmp->mstrategy &= ~STRAT_WAITMASK;
188.             if (canseemon(mtmp))
189.                 pline("%s is now ready for battle!", Monnam(mtmp));
190.             else if (!Deaf)
191.                 Norep("%s the rattle of battle gear being readied.",
192.                       "You hear");  /* Deaf-aware */
193.         } else if ((distm = ((bugler == &youmonst)
194.                                  ? distu(mtmp->mx, mtmp->my)
195.                                  : dist2(bugler->mx, bugler->my, mtmp->mx,
196.                                          mtmp->my))) < distance) {
197.             mtmp->msleeping = 0;
198.             mtmp->mcanmove = 1;
199.             mtmp->mfrozen = 0;
200.             /* may scare some monsters -- waiting monsters excluded */
201.             if (!unique_corpstat(mtmp->data)
202.                 && (mtmp->mstrategy & STRAT_WAITMASK) != 0)
203.                 mtmp->mstrategy &= ~STRAT_WAITMASK;
204.             else if (distm < distance / 3
205.                      && !resist(mtmp, TOOL_CLASS, 0, NOTELL))
206.                 monflee(mtmp, 0, FALSE, TRUE);
207.         }
208.     }
209. }
210. 
211. /* Charm monsters in range.  Note that they may resist the spell.
212.  * If swallowed, range is reduced to 0.
213.  */
214. STATIC_OVL void
215. charm_monsters(distance)
216. int distance;
217. {
218.     struct monst *mtmp, *mtmp2;
219. 
220.     if (u.uswallow) {
221.         if (!resist(u.ustuck, TOOL_CLASS, 0, NOTELL))
222.             (void) tamedog(u.ustuck, (struct obj *) 0);
223.     } else {
224.         for (mtmp = fmon; mtmp; mtmp = mtmp2) {
225.             mtmp2 = mtmp->nmon;
226.             if (DEADMONSTER(mtmp))
227.                 continue;
228. 
229.             if (distu(mtmp->mx, mtmp->my) <= distance) {
230.                 if (!resist(mtmp, TOOL_CLASS, 0, NOTELL))
231.                     (void) tamedog(mtmp, (struct obj *) 0);
232.             }
233.         }
234.     }
235. }
236. 
237. /* Generate earthquake :-) of desired force.
238.  * That is:  create random chasms (pits).
239.  */
240. STATIC_OVL void
241. do_earthquake(force)
242. int force;
243. {
244.     register int x, y;
245.     struct monst *mtmp;
246.     struct obj *otmp;
247.     struct trap *chasm, *trap_at_u = t_at(u.ux, u.uy);
248.     int start_x, start_y, end_x, end_y;
249.     schar filltype;
250.     unsigned tu_pit = 0;
251. 
252.     if (trap_at_u)
253.         tu_pit = is_pit(trap_at_u->ttyp);
254.     start_x = u.ux - (force * 2);
255.     start_y = u.uy - (force * 2);
256.     end_x = u.ux + (force * 2);
257.     end_y = u.uy + (force * 2);
258.     start_x = max(start_x, 1);
259.     start_y = max(start_y, 0);
260.     end_x = min(end_x, COLNO - 1);
261.     end_y = min(end_y, ROWNO - 1);
262.     for (x = start_x; x <= end_x; x++)
263.         for (y = start_y; y <= end_y; y++) {
264.             if ((mtmp = m_at(x, y)) != 0) {
265.                 wakeup(mtmp, TRUE); /* peaceful monster will become hostile */
266.                 if (mtmp->mundetected && is_hider(mtmp->data)) {
267.                     mtmp->mundetected = 0;
268.                     if (cansee(x, y))
269.                         pline("%s is shaken loose from the ceiling!",
270.                               Amonnam(mtmp));
271.                     else
272.                         You_hear("a thumping sound.");
273.                     if (x == u.ux && y == u.uy)
274.                         You("easily dodge the falling %s.", mon_nam(mtmp));
275.                     newsym(x, y);
276.                 }
277.             }
278.             if (!rn2(14 - force))
279.                 switch (levl[x][y].typ) {
280.                 case FOUNTAIN: /* Make the fountain disappear */
281.                     if (cansee(x, y))
282.                         pline_The("fountain falls into a chasm.");
283.                     goto do_pit;
284.                 case SINK:
285.                     if (cansee(x, y))
286.                         pline_The("kitchen sink falls into a chasm.");
287.                     goto do_pit;
288.                 case ALTAR:
289.                     if (Is_astralevel(&u.uz) || Is_sanctum(&u.uz))
290.                         break;
291. 
292.                     if (cansee(x, y))
293.                         pline_The("altar falls into a chasm.");
294.                     goto do_pit;
295.                 case GRAVE:
296.                     if (cansee(x, y))
297.                         pline_The("headstone topples into a chasm.");
298.                     goto do_pit;
299.                 case THRONE:
300.                     if (cansee(x, y))
301.                         pline_The("throne falls into a chasm.");
302.                     /*FALLTHRU*/
303.                 case ROOM:
304.                 case CORR: /* Try to make a pit */
305.  do_pit:
306.                     chasm = maketrap(x, y, PIT);
307.                     if (!chasm)
308.                         break; /* no pit if portal at that location */
309.                     chasm->tseen = 1;
310. 
311.                     /* TODO:
312.                      * This ought to be split into a separate routine to
313.                      * reduce indentation and the consequent line-wraps.
314.                      */
315. 
316.                     levl[x][y].doormask = 0;
317.                     /*
318.                      * Let liquid flow into the newly created chasm.
319.                      * Adjust corresponding code in apply.c for
320.                      * exploding wand of digging if you alter this sequence.
321.                      */
322.                     filltype = fillholetyp(x, y, FALSE);
323.                     if (filltype != ROOM) {
324.                         levl[x][y].typ = filltype; /* flags set via doormask */
325.                         liquid_flow(x, y, filltype, chasm, (char *) 0);
326.                     }
327. 
328.                     mtmp = m_at(x, y);
329. 
330.                     if ((otmp = sobj_at(BOULDER, x, y)) != 0) {
331.                         if (cansee(x, y))
332.                             pline("KADOOM!  The boulder falls into a chasm%s!",
333.                                   (x == u.ux && y == u.uy) ? " below you"
334.                                                            : "");
335.                         if (mtmp)
336.                             mtmp->mtrapped = 0;
337.                         obj_extract_self(otmp);
338.                         (void) flooreffects(otmp, x, y, "");
339.                         break;
340.                     }
341. 
342.                     /* We have to check whether monsters or player
343.                        falls in a chasm... */
344.                     if (mtmp) {
345.                         if (!is_flyer(mtmp->data)
346.                             && !is_clinger(mtmp->data)) {
347.                             boolean m_already_trapped = mtmp->mtrapped;
348. 
349.                             mtmp->mtrapped = 1;
350.                             if (!m_already_trapped) { /* suppress messages */
351.                                 if (cansee(x, y))
352.                                     pline("%s falls into a chasm!",
353.                                           Monnam(mtmp));
354.                                 else if (humanoid(mtmp->data))
355.                                     You_hear("a scream!");
356.                             }
357.                             /* Falling is okay for falling down
358.                                 within a pit from jostling too */
359.                             mselftouch(mtmp, "Falling, ", TRUE);
360.                             if (!DEADMONSTER(mtmp)) {
361.                                 mtmp->mhp -= rnd(m_already_trapped ? 4 : 6);
362.                                 if (DEADMONSTER(mtmp)) {
363.                                     if (!cansee(x, y)) {
364.                                         pline("It is destroyed!");
365.                                     } else {
366.                                         You("destroy %s!",
367.                                             mtmp->mtame
368.                                               ? x_monnam(mtmp, ARTICLE_THE,
369.                                                          "poor",
370.                                                          has_mname(mtmp)
371.                                                            ? SUPPRESS_SADDLE
372.                                                            : 0,
373.                                                          FALSE)
374.                                               : mon_nam(mtmp));
375.                                     }
376.                                     xkilled(mtmp, XKILL_NOMSG);
377.                                 }
378.                             }
379.                         }
380.                     } else if (x == u.ux && y == u.uy) {
381.                         if (u.utrap && u.utraptype == TT_BURIEDBALL) {
382.                             /* Note:  the chain should break if a pit gets
383.                                created at the buried ball's location, which
384.                                is not necessarily here.  But if we don't do
385.                                things this way, entering the new pit below
386.                                will override current trap anyway, but too
387.                                late to get Lev and Fly handling. */
388.                             Your("chain breaks!");
389.                             reset_utrap(TRUE);
390.                         }
391.                         if (Levitation || Flying
392.                             || is_clinger(youmonst.data)) {
393.                             if (!tu_pit) { /* no pit here previously */
394.                                 pline("A chasm opens up under you!");
395.                                 You("don't fall in!");
396.                             }
397.                         } else if (!tu_pit || !u.utrap
398.                                    || (u.utrap && u.utraptype != TT_PIT)) {
399.                             /* no pit here previously, or you were
400.                                not in it even if there was */
401.                             You("fall into a chasm!");
402.                             set_utrap(rn1(6, 2), TT_PIT);
403.                             losehp(Maybe_Half_Phys(rnd(6)),
404.                                    "fell into a chasm", NO_KILLER_PREFIX);
405.                             selftouch("Falling, you");
406.                         } else if (u.utrap && u.utraptype == TT_PIT) {
407.                             boolean keepfooting =
408.                                 ((Fumbling && !rn2(5))
409.                                  || (!rnl(Role_if(PM_ARCHEOLOGIST) ? 3 : 9))
410.                                  || ((ACURR(A_DEX) > 7) && rn2(5)));
411. 
412.                             You("are jostled around violently!");
413.                             set_utrap(rn1(6, 2), TT_PIT);
414.                             losehp(Maybe_Half_Phys(rnd(keepfooting ? 2 : 4)),
415.                                    "hurt in a chasm", NO_KILLER_PREFIX);
416.                             if (keepfooting)
417.                                 exercise(A_DEX, TRUE);
418.                             else
419.                                 selftouch(
420.                                     (Upolyd && (slithy(youmonst.data)
421.                                                 || nolimbs(youmonst.data)))
422.                                         ? "Shaken, you"
423.                                         : "Falling down, you");
424.                         }
425.                     } else
426.                         newsym(x, y);
427.                     break;
428.                 case DOOR: /* Make the door collapse */
429.                     if (levl[x][y].doormask == D_NODOOR)
430.                         goto do_pit;
431.                     if (cansee(x, y))
432.                         pline_The("door collapses.");
433.                     if (*in_rooms(x, y, SHOPBASE))
434.                         add_damage(x, y, 0L);
435.                     levl[x][y].doormask = D_NODOOR;
436.                     unblock_point(x, y);
437.                     newsym(x, y);
438.                     break;
439.                 }
440.         }
441. }
442. 
443. const char *
444. generic_lvl_desc()
445. {
446.     if (Is_astralevel(&u.uz))
447.         return "astral plane";
448.     else if (In_endgame(&u.uz))
449.         return "plane";
450.     else if (Is_sanctum(&u.uz))
451.         return "sanctum";
452.     else if (In_sokoban(&u.uz))
453.         return "puzzle";
454.     else if (In_V_tower(&u.uz))
455.         return "tower";
456.     else
457.         return "dungeon";
458. }
459. 
460. const char *beats[] = {
461.     "stepper", "one drop", "slow two", "triple stroke roll",
462.     "double shuffle", "half-time shuffle", "second line", "train"
463. };
464. 
465. /*
466.  * The player is trying to extract something from his/her instrument.
467.  */
468. STATIC_OVL int
469. do_improvisation(instr)
470. struct obj *instr;
471. {
472.     int damage, mode, do_spec = !(Stunned || Confusion);
473.     struct obj itmp;
474.     boolean mundane = FALSE;
475. 
476.     itmp = *instr;
477.     itmp.oextra = (struct oextra *) 0; /* ok on this copy as instr maintains
478.                                           the ptr to free at some point if
479.                                           there is one */
480. 
481.     /* if won't yield special effect, make sound of mundane counterpart */
482.     if (!do_spec || instr->spe <= 0)
483.         while (objects[itmp.otyp].oc_magic) {
484.             itmp.otyp -= 1;
485.             mundane = TRUE;
486.         }
487. 
488. #ifdef MAC
489.     mac_speaker(&itmp, "C");
490. #endif
491. #ifdef AMIGA
492.     amii_speaker(&itmp, "Cw", AMII_OKAY_VOLUME);
493. #endif
494. #ifdef VPIX_MUSIC
495.     if (sco_flag_console)
496.         speaker(&itmp, "C");
497. #endif
498. #ifdef PCMUSIC
499.     pc_speaker(&itmp, "C");
500. #endif
501. 
502. #define PLAY_NORMAL   0x00
503. #define PLAY_STUNNED  0x01
504. #define PLAY_CONFUSED 0x02
505. #define PLAY_HALLU    0x04
506.     mode = PLAY_NORMAL;
507.     if (Stunned)
508.         mode |= PLAY_STUNNED;
509.     if (Confusion)
510.         mode |= PLAY_CONFUSED;
511.     if (Hallucination)
512.         mode |= PLAY_HALLU;
513. 
514.     if (!rn2(2)) {
515.         /*
516.          * TEMPORARY?  for multiple impairments, don't always
517.          * give the generic "it's far from music" message.
518.          */
519.         /* remove if STUNNED+CONFUSED ever gets its own message below */
520.         if (mode == (PLAY_STUNNED | PLAY_CONFUSED))
521.             mode = !rn2(2) ? PLAY_STUNNED : PLAY_CONFUSED;
522.         /* likewise for stunned and/or confused combined with hallucination */
523.         if (mode & PLAY_HALLU)
524.             mode = PLAY_HALLU;
525.     }
526. 
527.     /* 3.6.3: most of these gave "You produce <blah>" and then many of
528.        the instrument-specific messages below which immediately follow
529.        also gave "You produce <something>."  That looked strange so we
530.        now use a different verb here */
531.     switch (mode) {
532.     case PLAY_NORMAL:
533.         You("start playing %s.", yname(instr));
534.         break;
535.     case PLAY_STUNNED:
536.         if (!Deaf)
537.             You("radiate an obnoxious droning sound.");
538.         else
539.             You_feel("a monotonous vibration.");
540.         break;
541.     case PLAY_CONFUSED:
542.         if (!Deaf)
543.             You("generate a raucous noise.");
544.         else
545.             You_feel("a jarring vibration.");
546.         break;
547.     case PLAY_HALLU:
548.         You("disseminate a kaleidoscopic display of floating butterflies.");
549.         break;
550.     /* TODO? give some or all of these combinations their own feedback;
551.        hallucination ones should reference senses other than hearing... */
552.     case PLAY_STUNNED | PLAY_CONFUSED:
553.     case PLAY_STUNNED | PLAY_HALLU:
554.     case PLAY_CONFUSED | PLAY_HALLU:
555.     case PLAY_STUNNED | PLAY_CONFUSED | PLAY_HALLU:
556.     default:
557.         pline("What you perform is quite far from music...");
558.         break;
559.     }
560. #undef PLAY_NORMAL
561. #undef PLAY_STUNNED
562. #undef PLAY_CONFUSED
563. #undef PLAY_HALLU
564. 
565.     switch (itmp.otyp) { /* note: itmp.otyp might differ from instr->otyp */
566.     case MAGIC_FLUTE: /* Make monster fall asleep */
567.         consume_obj_charge(instr, TRUE);
568. 
569.         You("%sproduce %s music.", !Deaf ? "" : "seem to ",
570.             Hallucination ? "piped" : "soft");
571.         put_monsters_to_sleep(u.ulevel * 5);
572.         exercise(A_DEX, TRUE);
573.         break;
574.     case WOODEN_FLUTE: /* May charm snakes */
575.         do_spec &= (rn2(ACURR(A_DEX)) + u.ulevel > 25);
576.         if (!Deaf)
577.             pline("%s.", Tobjnam(instr, do_spec ? "trill" : "toot"));
578.         else
579.             You_feel("%s %s.", yname(instr), do_spec ? "trill" : "toot");
580.         if (do_spec)
581.             charm_snakes(u.ulevel * 3);
582.         exercise(A_DEX, TRUE);
583.         break;
584.     case FIRE_HORN:  /* Idem wand of fire */
585.     case FROST_HORN: /* Idem wand of cold */
586.         consume_obj_charge(instr, TRUE);
587. 
588.         if (!getdir((char *) 0)) {
589.             pline("%s.", Tobjnam(instr, "vibrate"));
590.             break;
591.         } else if (!u.dx && !u.dy && !u.dz) {
592.             if ((damage = zapyourself(instr, TRUE)) != 0) {
593.                 char buf[BUFSZ];
594. 
595.                 Sprintf(buf, "using a magical horn on %sself", uhim());
596.                 losehp(damage, buf, KILLED_BY); /* fire or frost damage */
597.             }
598.         } else {
599.             buzz((instr->otyp == FROST_HORN) ? AD_COLD - 1 : AD_FIRE - 1,
600.                  rn1(6, 6), u.ux, u.uy, u.dx, u.dy);
601.         }
602.         makeknown(instr->otyp);
603.         break;
604.     case TOOLED_HORN: /* Awaken or scare monsters */
605.         if (!Deaf)
606.             You("produce a frightful, grave sound.");
607.         else
608.             You("blow into the horn.");
609.         awaken_monsters(u.ulevel * 30);
610.         exercise(A_WIS, FALSE);
611.         break;
612.     case BUGLE: /* Awaken & attract soldiers */
613.         if (!Deaf)
614.             You("extract a loud noise from %s.", yname(instr));
615.         else
616.             You("blow into the bugle.");
617.         awaken_soldiers(&youmonst);
618.         exercise(A_WIS, FALSE);
619.         break;
620.     case MAGIC_HARP: /* Charm monsters */
621.         consume_obj_charge(instr, TRUE);
622. 
623.         if (!Deaf)
624.             pline("%s very attractive music.", Tobjnam(instr, "produce"));
625.         else
626.             You_feel("very soothing vibrations.");
627.         charm_monsters((u.ulevel - 1) / 3 + 1);
628.         exercise(A_DEX, TRUE);
629.         break;
630.     case WOODEN_HARP: /* May calm Nymph */
631.         do_spec &= (rn2(ACURR(A_DEX)) + u.ulevel > 25);
632.         if (!Deaf)
633.             pline("%s %s.", Yname2(instr),
634.                   do_spec ? "produces a lilting melody" : "twangs");
635.         else
636.             You_feel("soothing vibrations.");
637.         if (do_spec)
638.             calm_nymphs(u.ulevel * 3);
639.         exercise(A_DEX, TRUE);
640.         break;
641.     case DRUM_OF_EARTHQUAKE: /* create several pits */
642.         /* a drum of earthquake does not cause deafness
643.            while still magically functional, nor afterwards
644.            when it invokes the LEATHER_DRUM case instead and
645.            mundane is flagged */
646.         consume_obj_charge(instr, TRUE);
647. 
648.         You("produce a heavy, thunderous rolling!");
649.         pline_The("entire %s is shaking around you!", generic_lvl_desc());
650.         do_earthquake((u.ulevel - 1) / 3 + 1);
651.         /* shake up monsters in a much larger radius... */
652.         awaken_monsters(ROWNO * COLNO);
653.         makeknown(DRUM_OF_EARTHQUAKE);
654.         break;
655.     case LEATHER_DRUM: /* Awaken monsters */
656.         if (!mundane) {
657.             if (!Deaf) {
658.                 You("beat a deafening row!");
659.                 incr_itimeout(&HDeaf, rn1(20, 30));
660.             } else {
661.                 You("pound on the drum.");
662.             }
663.             exercise(A_WIS, FALSE);
664.         } else
665.             You("%s %s.",
666.                 rn2(2) ? "butcher" : rn2(2) ? "manage" : "pull off",
667.                 an(beats[rn2(SIZE(beats))]));
668.         awaken_monsters(u.ulevel * (mundane ? 5 : 40));
669.         context.botl = TRUE;
670.         break;
671.     default:
672.         impossible("What a weird instrument (%d)!", instr->otyp);
673.         return 0;
674.     }
675.     return 2; /* That takes time */
676. }
677. 
678. /*
679.  * So you want music...
680.  */
681. int
682. do_play_instrument(instr)
683. struct obj *instr;
684. {
685.     char buf[BUFSZ] = DUMMY, c = 'y';
686.     char *s;
687.     int x, y;
688.     boolean ok;
689. 
690.     if (Underwater) {
691.         You_cant("play music underwater!");
692.         return 0;
693.     } else if ((instr->otyp == WOODEN_FLUTE || instr->otyp == MAGIC_FLUTE
694.                 || instr->otyp == TOOLED_HORN || instr->otyp == FROST_HORN
695.                 || instr->otyp == FIRE_HORN || instr->otyp == BUGLE)
696.                && !can_blow(&youmonst)) {
697.         You("are incapable of playing %s.", the(distant_name(instr, xname)));
698.         return 0;
699.     }
700.     if (instr->otyp != LEATHER_DRUM && instr->otyp != DRUM_OF_EARTHQUAKE
701.         && !(Stunned || Confusion || Hallucination)) {
702.         c = ynq("Improvise?");
703.         if (c == 'q')
704.             goto nevermind;
705.     }
706. 
707.     if (c == 'n') {
708.         if (u.uevent.uheard_tune == 2)
709.             c = ynq("Play the passtune?");
710.         if (c == 'q') {
711.             goto nevermind;
712.         } else if (c == 'y') {
713.             Strcpy(buf, tune);
714.         } else {
715.             getlin("What tune are you playing? [5 notes, A-G]", buf);
716.             (void) mungspaces(buf);
717.             if (*buf == '\033')
718.                 goto nevermind;
719. 
720.             /* convert to uppercase and change any "H" to the expected "B" */
721.             for (s = buf; *s; s++) {
722. #ifndef AMIGA
723.                 *s = highc(*s);
724. #else
725.                 /* The AMIGA supports two octaves of notes */
726.                 if (*s == 'h')
727.                     *s = 'b';
728. #endif
729.                 if (*s == 'H')
730.                     *s = 'B';
731.             }
732.         }
733. 
734.         You(!Deaf ? "extract a strange sound from %s!"
735.                   : "can feel %s emitting vibrations.", the(xname(instr)));
736. 
737. #ifdef UNIX386MUSIC
738.         /* if user is at the console, play through the console speaker */
739.         if (atconsole())
740.             speaker(instr, buf);
741. #endif
742. #ifdef VPIX_MUSIC
743.         if (sco_flag_console)
744.             speaker(instr, buf);
745. #endif
746. #ifdef MAC
747.         mac_speaker(instr, buf);
748. #endif
749. #ifdef PCMUSIC
750.         pc_speaker(instr, buf);
751. #endif
752. #ifdef AMIGA
753.         {
754.             char nbuf[20];
755.             int i;
756. 
757.             for (i = 0; buf[i] && i < 5; ++i) {
758.                 nbuf[i * 2] = buf[i];
759.                 nbuf[(i * 2) + 1] = 'h';
760.             }
761.             nbuf[i * 2] = 0;
762.             amii_speaker(instr, nbuf, AMII_OKAY_VOLUME);
763.         }
764. #endif
765.         /* Check if there was the Stronghold drawbridge near
766.          * and if the tune conforms to what we're waiting for.
767.          */
768.         if (Is_stronghold(&u.uz)) {
769.             exercise(A_WIS, TRUE); /* just for trying */
770.             if (!strcmp(buf, tune)) {
771.                 /* Search for the drawbridge */
772.                 for (y = u.uy - 1; y <= u.uy + 1; y++)
773.                     for (x = u.ux - 1; x <= u.ux + 1; x++)
774.                         if (isok(x, y))
775.                             if (find_drawbridge(&x, &y)) {
776.                                 /* tune now fully known */
777.                                 u.uevent.uheard_tune = 2;
778.                                 if (levl[x][y].typ == DRAWBRIDGE_DOWN)
779.                                     close_drawbridge(x, y);
780.                                 else
781.                                     open_drawbridge(x, y);
782.                                 return 1;
783.                             }
784.             } else if (!Deaf) {
785.                 if (u.uevent.uheard_tune < 1)
786.                     u.uevent.uheard_tune = 1;
787.                 /* Okay, it wasn't the right tune, but perhaps
788.                  * we can give the player some hints like in the
789.                  * Mastermind game */
790.                 ok = FALSE;
791.                 for (y = u.uy - 1; y <= u.uy + 1 && !ok; y++)
792.                     for (x = u.ux - 1; x <= u.ux + 1 && !ok; x++)
793.                         if (isok(x, y))
794.                             if (IS_DRAWBRIDGE(levl[x][y].typ)
795.                                 || is_drawbridge_wall(x, y) >= 0)
796.                                 ok = TRUE;
797.                 if (ok) { /* There is a drawbridge near */
798.                     int tumblers, gears;
799.                     boolean matched[5];
800. 
801.                     tumblers = gears = 0;
802.                     for (x = 0; x < 5; x++)
803.                         matched[x] = FALSE;
804. 
805.                     for (x = 0; x < (int) strlen(buf); x++)
806.                         if (x < 5) {
807.                             if (buf[x] == tune[x]) {
808.                                 gears++;
809.                                 matched[x] = TRUE;
810.                             } else {
811.                                 for (y = 0; y < 5; y++)
812.                                     if (!matched[y] && buf[x] == tune[y]
813.                                         && buf[y] != tune[y]) {
814.                                         tumblers++;
815.                                         matched[y] = TRUE;
816.                                         break;
817.                                     }
818.                             }
819.                         }
820.                     if (tumblers) {
821.                         if (gears)
822.                             You_hear("%d tumbler%s click and %d gear%s turn.",
823.                                      tumblers, plur(tumblers), gears,
824.                                      plur(gears));
825.                         else
826.                             You_hear("%d tumbler%s click.", tumblers,
827.                                      plur(tumblers));
828.                     } else if (gears) {
829.                         You_hear("%d gear%s turn.", gears, plur(gears));
830.                         /* could only get `gears == 5' by playing five
831.                            correct notes followed by excess; otherwise,
832.                            tune would have matched above */
833.                         if (gears == 5)
834.                             u.uevent.uheard_tune = 2;
835.                     }
836.                 }
837.             }
838.         }
839.         return 1;
840.     } else
841.         return do_improvisation(instr);
842. 
843.  nevermind:
844.     pline1(Never_mind);
845.     return 0;
846. }
847. 
848. #ifdef UNIX386MUSIC
849. /*
850.  * Play audible music on the machine's speaker if appropriate.
851.  */
852. 
853. STATIC_OVL int
854. atconsole()
855. {
856.     /*
857.      * Kluge alert: This code assumes that your [34]86 has no X terminals
858.      * attached and that the console tty type is AT386 (this is always true
859.      * under AT&T UNIX for these boxen). The theory here is that your remote
860.      * ttys will have terminal type `ansi' or something else other than
861.      * `AT386' or `xterm'. We'd like to do better than this, but testing
862.      * to see if we're running on the console physical terminal is quite
863.      * difficult given the presence of virtual consoles and other modern
864.      * UNIX impedimenta...
865.      */
866.     char *termtype = nh_getenv("TERM");
867. 
868.     return (!strcmp(termtype, "AT386") || !strcmp(termtype, "xterm"));
869. }
870. 
871. STATIC_OVL void
872. speaker(instr, buf)
873. struct obj *instr;
874. char *buf;
875. {
876.     /*
877.      * For this to work, you need to have installed the PD speaker-control
878.      * driver for PC-compatible UNIX boxes that I (esr@snark.thyrsus.com)
879.      * posted to comp.sources.unix in Feb 1990.  A copy should be included
880.      * with your nethack distribution.
881.      */
882.     int fd;
883. 
884.     if ((fd = open("/dev/speaker", 1)) != -1) {
885.         /* send a prefix to modify instrumental `timbre' */
886.         switch (instr->otyp) {
887.         case WOODEN_FLUTE:
888.         case MAGIC_FLUTE:
889.             (void) write(fd, ">ol", 1); /* up one octave & lock */
890.             break;
891.         case TOOLED_HORN:
892.         case FROST_HORN:
893.         case FIRE_HORN:
894.             (void) write(fd, "<<ol", 2); /* drop two octaves & lock */
895.             break;
896.         case BUGLE:
897.             (void) write(fd, "ol", 2); /* octave lock */
898.             break;
899.         case WOODEN_HARP:
900.         case MAGIC_HARP:
901.             (void) write(fd, "l8mlol", 4); /* fast, legato, octave lock */
902.             break;
903.         }
904.         (void) write(fd, buf, strlen(buf));
905.         (void) nhclose(fd);
906.     }
907. }
908. #endif /* UNIX386MUSIC */
909. 
910. #ifdef VPIX_MUSIC
911. 
912. #if 0
913. #include <sys/types.h>
914. #include <sys/console.h>
915. #include <sys/vtkd.h>
916. #else
917. #define KIOC ('K' << 8)
918. #define KDMKTONE (KIOC | 8)
919. #endif
920. 
921. #define noDEBUG
922. 
923. /* emit tone of frequency hz for given number of ticks */
924. STATIC_OVL void
925. tone(hz, ticks)
926. unsigned int hz, ticks;
927. {
928.     ioctl(0, KDMKTONE, hz | ((ticks * 10) << 16));
929. #ifdef DEBUG
930.     printf("TONE: %6d %6d\n", hz, ticks * 10);
931. #endif
932.     nap(ticks * 10);
933. }
934. 
935. /* rest for given number of ticks */
936. STATIC_OVL void
937. rest(ticks)
938. int ticks;
939. {
940.     nap(ticks * 10);
941. #ifdef DEBUG
942.     printf("REST:        %6d\n", ticks * 10);
943. #endif
944. }
945. 
946. #include "interp.c" /* from snd86unx.shr */
947. 
948. STATIC_OVL void
949. speaker(instr, buf)
950. struct obj *instr;
951. char *buf;
952. {
953.     /* emit a prefix to modify instrumental `timbre' */
954.     playinit();
955.     switch (instr->otyp) {
956.     case WOODEN_FLUTE:
957.     case MAGIC_FLUTE:
958.         playstring(">ol", 1); /* up one octave & lock */
959.         break;
960.     case TOOLED_HORN:
961.     case FROST_HORN:
962.     case FIRE_HORN:
963.         playstring("<<ol", 2); /* drop two octaves & lock */
964.         break;
965.     case BUGLE:
966.         playstring("ol", 2); /* octave lock */
967.         break;
968.     case WOODEN_HARP:
969.     case MAGIC_HARP:
970.         playstring("l8mlol", 4); /* fast, legato, octave lock */
971.         break;
972.     }
973.     playstring(buf, strlen(buf));
974. }
975. 
976. #ifdef VPIX_DEBUG
977. main(argc, argv)
978. int argc;
979. char *argv[];
980. {
981.     if (argc == 2) {
982.         playinit();
983.         playstring(argv[1], strlen(argv[1]));
984.     }
985. }
986. #endif
987. #endif /* VPIX_MUSIC */
988. 
989. /*music.c*/