Source:NetHack 5.0.0/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 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/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 5.0	music.c	$NHDT-Date: 1736530208 2025/01/10 09:30:08 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.120 $ */
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. staticfn void awaken_scare(struct monst *, boolean);
32. staticfn void awaken_monsters(int);
33. staticfn void put_monsters_to_sleep(int);
34. staticfn void charm_snakes(int);
35. staticfn void calm_nymphs(int);
36. staticfn void charm_monsters(int);
37. staticfn void do_pit(coordxy, coordxy, unsigned);
38. staticfn void do_earthquake(int);
39. staticfn const char *generic_lvl_desc(void);
40. staticfn int do_improvisation(struct obj *);
41. staticfn char *improvised_notes(boolean *);
42. 
43. /* wake up monster, possibly scare it */
44. staticfn void
45. awaken_scare(struct monst *mtmp, boolean scary)
46. {
47.     mtmp->msleeping = 0;
48.     mtmp->mcanmove = 1;
49.     mtmp->mfrozen = 0;
50.     /* may scare some monsters -- waiting monsters excluded */
51.     if (!unique_corpstat(mtmp->data)
52.         && (mtmp->mstrategy & STRAT_WAITMASK) != 0)
53.         mtmp->mstrategy &= ~STRAT_WAITMASK;
54.     else if (scary
55.              && !mindless(mtmp->data)
56.              && !resist(mtmp, TOOL_CLASS, 0, NOTELL)
57.              /* some monsters are immune */
58.              && onscary(0, 0, mtmp))
59.         monflee(mtmp, 0, FALSE, TRUE);
60. }
61. 
62. /*
63.  * Wake every monster in range...
64.  */
65. 
66. staticfn void
67. awaken_monsters(int distance)
68. {
69.     struct monst *mtmp;
70.     int distm;
71. 
72.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
73.         if (DEADMONSTER(mtmp))
74.             continue;
75.         if ((distm = mdistu(mtmp)) < distance)
76.             awaken_scare(mtmp, (distm < distance / 3));
77.     }
78. }
79. 
80. /*
81.  * Make monsters fall asleep.  Note that they may resist the spell.
82.  */
83. 
84. staticfn void
85. put_monsters_to_sleep(int distance)
86. {
87.     struct monst *mtmp;
88. 
89.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
90.         if (DEADMONSTER(mtmp))
91.             continue;
92.         if (mdistu(mtmp) < distance
93.             && sleep_monst(mtmp, d(10, 10), TOOL_CLASS)) {
94.             mtmp->msleeping = 1; /* 10d10 turns + wake_nearby to rouse */
95.             slept_monst(mtmp);
96.         }
97.     }
98. }
99. 
100. /*
101.  * Charm snakes in range.  Note that the snakes are NOT tamed.
102.  */
103. 
104. staticfn void
105. charm_snakes(int distance)
106. {
107.     struct monst *mtmp;
108.     int could_see_mon, was_peaceful;
109. 
110.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
111.         if (DEADMONSTER(mtmp))
112.             continue;
113.         if (mtmp->data->mlet == S_SNAKE && mtmp->mcanmove
114.             && mdistu(mtmp) < distance) {
115.             was_peaceful = mtmp->mpeaceful;
116.             mtmp->mpeaceful = 1;
117.             mtmp->mavenge = 0;
118.             mtmp->mstrategy &= ~STRAT_WAITMASK;
119.             could_see_mon = canseemon(mtmp);
120.             mtmp->mundetected = 0;
121.             newsym(mtmp->mx, mtmp->my);
122.             if (canseemon(mtmp)) {
123.                 if (!could_see_mon)
124.                     You("notice %s, swaying with the music.", a_monnam(mtmp));
125.                 else
126.                     pline("%s freezes, then sways with the music%s.",
127.                           Monnam(mtmp),
128.                           was_peaceful ? "" : ", and now seems quieter");
129.             }
130.         }
131.     }
132. }
133. 
134. /*
135.  * Calm nymphs in range.
136.  */
137. 
138. staticfn void
139. calm_nymphs(int distance)
140. {
141.     struct monst *mtmp;
142. 
143.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
144.         if (DEADMONSTER(mtmp))
145.             continue;
146.         if (mtmp->data->mlet == S_NYMPH && mtmp->mcanmove
147.             && mdistu(mtmp) < distance) {
148.             mtmp->msleeping = 0;
149.             mtmp->mpeaceful = 1;
150.             mtmp->mavenge = 0;
151.             mtmp->mstrategy &= ~STRAT_WAITMASK;
152.             if (canseemon(mtmp))
153.                 pline(
154.                     "%s listens cheerfully to the music, then seems quieter.",
155.                       Monnam(mtmp));
156.         }
157.     }
158. }
159. 
160. /* Awake soldiers anywhere the level (and any nearby monster). */
161. void
162. awaken_soldiers(struct monst *bugler  /* monster that played instrument */)
163. {
164.     struct monst *mtmp;
165.     int distance, distm;
166. 
167.     /* distance of affected non-soldier monsters to bugler */
168.     distance = ((bugler == &gy.youmonst) ? u.ulevel
169.                                          : bugler->data->mlevel) * 30;
170. 
171.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
172.         if (DEADMONSTER(mtmp))
173.             continue;
174.         if (is_mercenary(mtmp->data) && mtmp->data != &mons[PM_GUARD]) {
175.             if (!mtmp->mtame)
176.                 mtmp->mpeaceful = 0;
177.             mtmp->msleeping = mtmp->mfrozen = 0;
178.             mtmp->mcanmove = 1;
179.             mtmp->mstrategy &= ~STRAT_WAITMASK;
180.             if (canseemon(mtmp))
181.                 pline("%s is now ready for battle!", Monnam(mtmp));
182.             else if (!Deaf)
183.                 Norep("%s the rattle of battle gear being readied.",
184.                       "You hear");  /* Deaf-aware */
185.         } else if ((distm = ((bugler == &gy.youmonst)
186.                                  ? mdistu(mtmp)
187.                                  : dist2(bugler->mx, bugler->my, mtmp->mx,
188.                                          mtmp->my))) < distance) {
189.             awaken_scare(mtmp, (distm < distance / 3));
190.         }
191.     }
192. }
193. 
194. /* Charm monsters in range.  Note that they may resist the spell. */
195. staticfn void
196. charm_monsters(int distance)
197. {
198.     struct monst *mtmp, *mtmp2;
199. 
200.     if (u.uswallow)
201.         distance = 0; /* only u.ustuck will be affected (u.usteed is Null
202.                        * since hero gets forcibly dismounted when engulfed) */
203. 
204.     for (mtmp = fmon; mtmp; mtmp = mtmp2) {
205.         mtmp2 = mtmp->nmon;
206.         if (DEADMONSTER(mtmp))
207.             continue;
208. 
209.         if (mdistu(mtmp) <= distance) {
210.             /* a shopkeeper can't be tamed but tamedog() pacifies an angry
211.                one; do that even if mtmp resists in order to behave the same
212.                as a non-cursed scroll of taming or spell of charm monster */
213.             if (!resist(mtmp, TOOL_CLASS, 0, NOTELL) || mtmp->isshk)
214.                 (void) tamedog(mtmp, (struct obj *) 0, TRUE);
215.         }
216.     }
217. }
218. 
219. /* Try to make a pit. */
220. staticfn void
221. do_pit(coordxy x, coordxy y, unsigned tu_pit)
222. {
223.     struct monst *mtmp;
224.     struct obj *otmp;
225.     struct trap *chasm;
226.     schar filltype;
227. 
228.     chasm = maketrap(x, y, PIT);
229.     if (!chasm)
230.         return; /* no pit if portal at that location */
231.     chasm->tseen = 1;
232. 
233.     mtmp = m_at(x, y); /* (redundant?) */
234.     if ((otmp = sobj_at(BOULDER, x, y)) != 0) {
235.         if (cansee(x, y))
236.             pline("KADOOM!  The boulder falls into a chasm%s!",
237.                   u_at(x, y) ? " below you" : "");
238.         if (mtmp)
239.             mtmp->mtrapped = 0;
240.         obj_extract_self(otmp);
241.         (void) flooreffects(otmp, x, y, "");
242.         return;
243.     }
244. 
245.     /* Let liquid flow into the newly created chasm.
246.        Adjust corresponding code in apply.c for exploding
247.        wand of digging if you alter this sequence. */
248.     filltype = fillholetyp(x, y, FALSE);
249.     if (filltype != ROOM) {
250.         set_levltyp(x, y, filltype); /* levl[x][y] = filltype; */
251.         liquid_flow(x, y, filltype, chasm, (char *) 0);
252.         /* liquid_flow() deletes trap, might kill mtmp */
253.         if ((chasm = t_at(x, y)) == NULL)
254.             return;
255.     }
256. 
257.     /* We have to check whether monsters or hero falls into a
258.        new pit....  Note: if we get here, chasm is non-Null. */
259.     if (mtmp) {
260.         if (!is_flyer(mtmp->data) && !is_clinger(mtmp->data)) {
261.             boolean m_already_trapped = mtmp->mtrapped;
262. 
263.             mtmp->mtrapped = 1;
264.             if (!m_already_trapped) { /* suppress messages */
265.                 if (cansee(x, y)) {
266.                     pline("%s falls into a chasm!", Monnam(mtmp));
267.                 } else if (humanoid(mtmp->data)) {
268.                     Soundeffect(se_scream, 50);
269.                     You_hear("a scream!");
270.                 }
271.             }
272.             /* Falling is okay for falling down
273.                within a pit from jostling too */
274.             mselftouch(mtmp, "Falling, ", TRUE);
275.             if (!DEADMONSTER(mtmp)) {
276.                 mtmp->mhp -= rnd(m_already_trapped ? 4 : 6);
277.                 if (DEADMONSTER(mtmp)) {
278.                     if (!cansee(x, y)) {
279.                         pline("It is destroyed!");
280.                     } else {
281.                         You("destroy %s!",
282.                             mtmp->mtame
283.                              ? x_monnam(mtmp, ARTICLE_THE, "poor",
284.                                         has_mgivenname(mtmp)
285.                                          ? SUPPRESS_SADDLE : 0,
286.                                         FALSE)
287.                              : mon_nam(mtmp));
288.                     }
289.                     xkilled(mtmp, XKILL_NOMSG);
290.                 }
291.             }
292.         }
293.     } else if (u_at(x, y)) {
294.         if (u.utrap && u.utraptype == TT_BURIEDBALL) {
295.             /* Note:  the chain should break if a pit gets
296.                created at the buried ball's location, which
297.                is not necessarily here.  But if we don't do
298.                things this way, entering the new pit below
299.                will override current trap anyway, but too
300.                late to get Lev and Fly handling. */
301.             Your("chain breaks!");
302.             reset_utrap(TRUE);
303.         }
304.         if (Levitation || Flying || is_clinger(gy.youmonst.data)) {
305.             if (!tu_pit) { /* no pit here previously */
306.                 pline("A chasm opens up under you!");
307.                 You("don't fall in!");
308.             }
309.         } else if (!tu_pit || !u.utrap || u.utraptype != TT_PIT) {
310.             /* no pit here previously, or you were
311.                not in it even if there was */
312.             You("fall into a chasm!");
313.             set_utrap(rn1(6, 2), TT_PIT);
314.             losehp(Maybe_Half_Phys(rnd(6)),
315.                    "fell into a chasm", NO_KILLER_PREFIX);
316.             selftouch("Falling, you");
317.         } else if (u.utrap && u.utraptype == TT_PIT) {
318.             boolean keepfooting =
319.                     (!(Fumbling && rn2(5))
320.                      && (!(rnl(Role_if(PM_ARCHEOLOGIST) ? 3 : 9))
321.                          || ((ACURR(A_DEX) > 7) && rn2(5))));
322. 
323.             You("are jostled around violently!");
324.             set_utrap(rn1(6, 2), TT_PIT);
325.             losehp(Maybe_Half_Phys(rnd(keepfooting ? 2 : 4)),
326.                    "hurt in a chasm", NO_KILLER_PREFIX);
327.             if (keepfooting)
328.                 exercise(A_DEX, TRUE);
329.             else
330.                 selftouch((Upolyd && (slithy(gy.youmonst.data)
331.                                     || nolimbs(gy.youmonst.data)))
332.                           ? "Shaken, you"
333.                           : "Falling down, you");
334.         }
335.     } else {
336.         newsym(x, y);
337.     }
338. }
339. 
340. /* Generate earthquake :-) of desired force.
341.  * That is:  create random chasms (pits).
342.  */
343. staticfn void
344. do_earthquake(int force)
345. {
346.     static const char into_a_chasm[] = " into a chasm";
347.     coordxy x, y;
348.     struct monst *mtmp;
349.     struct trap *trap_at_u = t_at(u.ux, u.uy);
350.     int start_x, start_y, end_x, end_y, amsk;
351.     aligntyp algn;
352.     unsigned tu_pit = 0;
353. 
354.     if (trap_at_u)
355.         tu_pit = is_pit(trap_at_u->ttyp);
356.     if (force > 13) /* sanity precaution; maximum used is actually 10 */
357.         force = 13;
358.     start_x = u.ux - (force * 2);
359.     start_y = u.uy - (force * 2);
360.     end_x = u.ux + (force * 2);
361.     end_y = u.uy + (force * 2);
362.     start_x = max(start_x, 1);
363.     start_y = max(start_y, 0);
364.     end_x = min(end_x, COLNO - 1);
365.     end_y = min(end_y, ROWNO - 1);
366.     for (x = start_x; x <= end_x; x++)
367.         for (y = start_y; y <= end_y; y++) {
368.             if ((mtmp = m_at(x, y)) != 0) {
369.                 wakeup(mtmp, TRUE); /* peaceful monster will become hostile */
370.                 if (mtmp->mundetected) {
371.                     mtmp->mundetected = 0;
372.                     newsym(x, y);
373.                     if (ceiling_hider(mtmp->data)) {
374.                         if (cansee(x, y)) {
375.                             pline("%s is shaken loose from the ceiling!",
376.                                   Amonnam(mtmp));
377.                         } else if (!is_flyer(mtmp->data)) {
378.                             Soundeffect(se_thump, 50);
379.                             You_hear("a thump.");
380.                         }
381.                     }
382.                 }
383.                 if (M_AP_TYPE(mtmp) != M_AP_NOTHING
384.                     && M_AP_TYPE(mtmp) != M_AP_MONSTER)
385.                     seemimic(mtmp);
386.             }
387.             if (rn2(14 - force))
388.                 continue;
389. 
390.        /*
391.         * Possible extensions:
392.         *  When a door is trapped, explode it instead of silently
393.         *   turning it into an empty doorway.
394.         *  Trigger divine wrath when an altar is dumped into a chasm.
395.         *  Sometimes replace sink with fountain or fountain with pool
396.         *   instead of always producing a pit.
397.         *  Sometimes release monster and/or treasure from a grave or
398.         *   a throne instead of just dumping them into the chasm.
399.         *  Chance to destroy wall segments?  Trees too?
400.         *  Honor non-diggable for locked doors, walls, and trees.
401.         *   Treat non-passwall as if it was non-diggable?
402.         *  Conjoin some of the umpteen pits when they're adjacent?
403.         *
404.         *  Replace 'goto do_pit;' with 'do_pit = TRUE; break;' and
405.         *   move the pit code to after the switch.
406.         */
407. 
408.             switch (levl[x][y].typ) {
409.             case FOUNTAIN: /* make the fountain disappear */
410.                 if (cansee(x, y))
411.                     pline_The("fountain falls%s.", into_a_chasm);
412.                 do_pit(x, y, tu_pit);
413.                 break;
414.             case SINK:
415.                 if (cansee(x, y))
416.                     pline_The("kitchen sink falls%s.", into_a_chasm);
417.                 do_pit(x, y, tu_pit);
418.                 break;
419.             case ALTAR:
420.                 amsk = altarmask_at(x, y);
421.                 /* always preserve the high altars */
422.                 if ((amsk & AM_SANCTUM) != 0)
423.                     break;
424.                 algn = Amask2align(amsk & AM_MASK);
425.                 if (cansee(x, y))
426.                     pline_The("%s altar falls%s.",
427.                               align_str(algn), into_a_chasm);
428.                 desecrate_altar(FALSE, algn);
429.                 do_pit(x, y, tu_pit);
430.                 break;
431.             case GRAVE:
432.                 if (cansee(x, y))
433.                     pline_The("headstone topples%s.", into_a_chasm);
434.                 do_pit(x, y, tu_pit);
435.                 break;
436.             case THRONE:
437.                 if (cansee(x, y))
438.                     pline_The("throne falls%s.", into_a_chasm);
439.                 do_pit(x, y, tu_pit);
440.                 break;
441.             case SCORR:
442.                 levl[x][y].typ = CORR;
443.                 unblock_point(x, y);
444.                 if (cansee(x, y))
445.                     pline("A secret corridor is revealed.");
446.                 FALLTHROUGH;
447.                 /*FALLTHRU*/
448.             case CORR:
449.             case ROOM:
450.                 do_pit(x, y, tu_pit);
451.                 break;
452.             case SDOOR:
453.                 cvt_sdoor_to_door(&levl[x][y]); /* .typ = DOOR */
454.                 if (cansee(x, y))
455.                     pline("A secret door is revealed.");
456.                 FALLTHROUGH;
457.                 /*FALLTHRU*/
458.             case DOOR: /* make the door collapse */
459.                 /* if already doorless, treat like room or corridor */
460.                 if (levl[x][y].doormask == D_NODOOR) {
461.                     do_pit(x, y, tu_pit);
462.                     break;
463.                 }
464.                 /* wasn't doorless, now it will be */
465.                 levl[x][y].doormask = D_NODOOR;
466.                 recalc_block_point(x, y);
467.                 newsym(x, y); /* before pline */
468.                 if (cansee(x, y))
469.                     pline_The("door collapses.");
470.                 if (*in_rooms(x, y, SHOPBASE))
471.                     add_damage(x, y, 0L);
472.                 break;
473.             }
474.         }
475. }
476. 
477. staticfn const char *
478. generic_lvl_desc(void)
479. {
480.     if (Is_astralevel(&u.uz))
481.         return "astral plane";
482.     else if (In_endgame(&u.uz))
483.         return "plane";
484.     else if (Is_sanctum(&u.uz))
485.         return "sanctum";
486.     else if (In_sokoban(&u.uz))
487.         return "puzzle";
488.     else if (In_V_tower(&u.uz))
489.         return "tower";
490.     else
491.         return "dungeon";
492. }
493. 
494. static const char *beats[] = {
495.     "stepper", "one drop", "slow two", "triple stroke roll",
496.     "double shuffle", "half-time shuffle", "second line", "train"
497. };
498. 
499. /*
500.  * The player is trying to extract something from his/her instrument.
501.  */
502. staticfn int
503. do_improvisation(struct obj *instr)
504. {
505.     int damage, mode, do_spec = !(Stunned || Confusion);
506.     struct obj itmp;
507.     boolean mundane = FALSE, same_old_song = FALSE;
508.     static char my_goto_song[] = {'C', '\0'},
509.                 *improvisation = my_goto_song;
510. 
511.     itmp = *instr;
512.     itmp.oextra = (struct oextra *) 0; /* ok on this copy as instr maintains
513.                                         * the ptr to free at some point if
514.                                         * there is one */
515. 
516.     /* if won't yield special effect, make sound of mundane counterpart */
517.     if (!do_spec || instr->spe <= 0)
518.         while (objects[itmp.otyp].oc_magic) {
519.             itmp.otyp -= 1;
520.             mundane = TRUE;
521.         }
522. 
523. #define PLAY_NORMAL   0x00
524. #define PLAY_STUNNED  0x01
525. #define PLAY_CONFUSED 0x02
526. #define PLAY_HALLU    0x04
527.     mode = PLAY_NORMAL;
528.     if (Stunned)
529.         mode |= PLAY_STUNNED;
530.     if (Confusion)
531.         mode |= PLAY_CONFUSED;
532.     if (Hallucination)
533.         mode |= PLAY_HALLU;
534. 
535.     if (!rn2(2)) {
536.         /*
537.          * TEMPORARY?  for multiple impairments, don't always
538.          * give the generic "it's far from music" message.
539.          */
540.         /* remove if STUNNED+CONFUSED ever gets its own message below */
541.         if (mode == (PLAY_STUNNED | PLAY_CONFUSED))
542.             mode = !rn2(2) ? PLAY_STUNNED : PLAY_CONFUSED;
543.         /* likewise for stunned and/or confused combined with hallucination */
544.         if (mode & PLAY_HALLU)
545.             mode = PLAY_HALLU;
546.     }
547. 
548.     /* 3.6.3: most of these gave "You produce <blah>" and then many of
549.        the instrument-specific messages below which immediately follow
550.        also gave "You produce <something>."  That looked strange so we
551.        now use a different verb here */
552.     switch (mode) {
553.     case PLAY_NORMAL:
554.         You("start playing %s.", yname(instr));
555.         break;
556.     case PLAY_STUNNED:
557.         if (!Deaf)
558.             You("radiate an obnoxious droning sound.");
559.         else
560.             You_feel("a monotonous vibration.");
561.         break;
562.     case PLAY_CONFUSED:
563.         if (!Deaf)
564.             You("generate a raucous noise.");
565.         else
566.             You_feel("a jarring vibration.");
567.         break;
568.     case PLAY_HALLU:
569.         You("disseminate a kaleidoscopic display of floating butterflies.");
570.         break;
571.     /* TODO? give some or all of these combinations their own feedback;
572.        hallucination ones should reference senses other than hearing... */
573.     case PLAY_STUNNED | PLAY_CONFUSED:
574.     case PLAY_STUNNED | PLAY_HALLU:
575.     case PLAY_CONFUSED | PLAY_HALLU:
576.     case PLAY_STUNNED | PLAY_CONFUSED | PLAY_HALLU:
577.     default:
578.         pline("What you perform is quite far from music...");
579.         break;
580.     }
581. #undef PLAY_NORMAL
582. #undef PLAY_STUNNED
583. #undef PLAY_CONFUSED
584. #undef PLAY_HALLU
585. 
586.     improvisation = improvised_notes(&same_old_song);
587. 
588.     switch (itmp.otyp) { /* note: itmp.otyp might differ from instr->otyp */
589.     case MAGIC_FLUTE: /* Make monster fall asleep */
590.         consume_obj_charge(instr, TRUE);
591. 
592.         You("%sproduce %s%s music.", !Deaf ? "" : "seem to ",
593.             Hallucination ? "piped" : "soft",
594.             same_old_song ? ", familiar" : "");
595.         Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
596.         put_monsters_to_sleep(u.ulevel * 5);
597.         exercise(A_DEX, TRUE);
598.         break;
599.     case WOODEN_FLUTE: /* May charm snakes */
600.         do_spec &= (rn2(ACURR(A_DEX)) + u.ulevel > 25);
601.         if (!Deaf)
602.             pline("%s%s.", Tobjnam(instr, do_spec ? "trill" : "toot"),
603.                   same_old_song ? " a familiar tune" : "");
604.         else
605.             You_feel("%s %s.", yname(instr), do_spec ? "trill" : "toot");
606.         Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
607.         if (do_spec)
608.             charm_snakes(u.ulevel * 3);
609.         exercise(A_DEX, TRUE);
610.         break;
611.     case FIRE_HORN:  /* Idem wand of fire */
612.     case FROST_HORN: /* Idem wand of cold */
613.         consume_obj_charge(instr, TRUE);
614. 
615.         if (!getdir((char *) 0)) {
616.             pline("%s.", Tobjnam(instr, "vibrate"));
617.             break;
618.         } else if (!u.dx && !u.dy && !u.dz) {
619.             if ((damage = zapyourself(instr, TRUE)) != 0) {
620.                 char buf[BUFSZ];
621. 
622.                 Sprintf(buf, "using a magical horn on %sself", uhim());
623.                 Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
624.                 losehp(damage, buf, KILLED_BY); /* fire or frost damage */
625.             }
626.         } else {
627.             int type = BZ_OFS_AD((instr->otyp == FROST_HORN) ? AD_COLD
628.                                                              : AD_FIRE);
629. 
630.             if (!Blind)
631.                 pline("A %s blasts out of the horn!", flash_str(type, FALSE));
632.             Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
633.             gc.current_wand = instr;
634.             ubuzz(BZ_U_WAND(type), rn1(6, 6));
635.             gc.current_wand = 0;
636.         }
637.         makeknown(instr->otyp);
638.         break;
639.     case TOOLED_HORN: /* Awaken or scare monsters */
640.         if (!Deaf)
641.             You("produce a frightful, grave%s sound.",
642.                 same_old_song ? ", yet familiar," : "");
643.         else
644.             You("blow into the horn.");
645.         Hero_playnotes(obj_to_instr(&itmp), improvisation, 80);
646.         awaken_monsters(u.ulevel * 30);
647.         exercise(A_WIS, FALSE);
648.         break;
649.     case BUGLE: /* Awaken & attract soldiers */
650.         if (!Deaf)
651.             You("extract a loud%s noise from %s.",
652.                 same_old_song ? ", familiar" : "", yname(instr));
653.         else
654.             You("blow into the bugle.");
655.         Hero_playnotes(obj_to_instr(&itmp), improvisation, 80);
656.         awaken_soldiers(&gy.youmonst);
657.         exercise(A_WIS, FALSE);
658.         break;
659.     case MAGIC_HARP: /* Charm monsters */
660.         consume_obj_charge(instr, TRUE);
661. 
662.         if (!Deaf)
663.             pline("%s very attractive%s music.",
664.                   Tobjnam(instr, "produce"),
665.                   same_old_song ? " and familiar" : "");
666.         else
667.             You_feel("very soothing vibrations.");
668.         Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
669.         charm_monsters((u.ulevel - 1) / 3 + 1);
670.         exercise(A_DEX, TRUE);
671.         break;
672.     case WOODEN_HARP: /* May calm Nymph */
673.         do_spec &= (rn2(ACURR(A_DEX)) + u.ulevel > 25);
674.         if (!Deaf)
675.             pline("%s %s.", Yname2(instr),
676.                   (do_spec && same_old_song)
677.                   ? "produces a familiar, lilting melody"
678.                   : (do_spec) ? "produces a lilting melody"
679.                     : (same_old_song) ? "twangs a familiar tune"
680.                       : "twangs");
681.         else
682.             You_feel("soothing vibrations.");
683.         Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
684.         if (do_spec)
685.             calm_nymphs(u.ulevel * 3);
686.         exercise(A_DEX, TRUE);
687.         break;
688.     case DRUM_OF_EARTHQUAKE: /* create several pits */
689.         /* a drum of earthquake does not cause deafness
690.            while still magically functional, nor afterwards
691.            when it invokes the LEATHER_DRUM case instead and
692.            mundane is flagged */
693.         consume_obj_charge(instr, TRUE);
694. 
695.         You("produce a heavy, thunderous rolling!");
696.         Hero_playnotes(obj_to_instr(&itmp), "C", 100);
697.         pline_The("entire %s is shaking around you!", generic_lvl_desc());
698.         do_earthquake((u.ulevel - 1) / 3 + 1);
699.         /* shake up monsters in a much larger radius... */
700.         awaken_monsters(ROWNO * COLNO);
701.         makeknown(DRUM_OF_EARTHQUAKE);
702.         break;
703.     case LEATHER_DRUM: /* Awaken monsters */
704.         if (!mundane) {
705.             if (!Deaf) {
706.                 You("beat a %sdeafening row!",
707.                     same_old_song ? "familiar " : "");
708.                 Hero_playnotes(obj_to_instr(&itmp), "CCC", 100);
709.                 incr_itimeout(&HDeaf, rn1(20, 30));
710.             } else {
711.                 You("pound on the drum.");
712.             }
713.             exercise(A_WIS, FALSE);
714.         } else {
715.             /* TODO maybe: sound effects for these riffs */
716.             You("%s %s.",
717.                 rn2(2) ? "butcher" : rn2(2) ? "manage" : "pull off",
718.                 an(ROLL_FROM(beats)));
719.             Hero_playnotes(obj_to_instr(&itmp), improvisation, 50);
720.         }
721.         awaken_monsters(u.ulevel * (mundane ? 5 : 40));
722.         disp.botl = TRUE;
723.         break;
724.     default:
725.         impossible("What a weird instrument (%d)!", instr->otyp);
726.         return 0;
727.     }
728.     nhUse(improvisation);
729.     return 2; /* That takes time */
730. }
731. 
732. staticfn char *
733. improvised_notes(boolean *same_as_last_time)
734. {
735.     static const char notes[7] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
736.     /* target buffer has to be in svc.context, otherwise saving game
737.      * between improvised recitals would not be able to maintain
738.      * the same_as_last_time context. */
739. 
740.     /* You can change your tune, usually */
741.     if (!(Unchanging && svc.context.jingle[0] != '\0')) {
742.         int i, notecount = rnd(SIZE(svc.context.jingle) - 1); /* 1 - 5 */
743. 
744.         for (i = 0; i < notecount; ++i) {
745.             svc.context.jingle[i] = ROLL_FROM(notes);
746.         }
747.         svc.context.jingle[notecount] = '\0';
748.         *same_as_last_time = FALSE;
749.     } else {
750.         *same_as_last_time = TRUE;
751.     }
752.     return svc.context.jingle;
753. }
754. 
755. /*
756.  * So you want music...
757.  */
758. int
759. do_play_instrument(struct obj *instr)
760. {
761.     char buf[BUFSZ] = DUMMY, c = 'y';
762.     char *s;
763.     coordxy x, y;
764.     boolean ok;
765. 
766.     if (Underwater) {
767.         You_cant("play music underwater!");
768.         return ECMD_OK;
769.     } else if ((instr->otyp == WOODEN_FLUTE || instr->otyp == MAGIC_FLUTE
770.                 || instr->otyp == TOOLED_HORN || instr->otyp == FROST_HORN
771.                 || instr->otyp == FIRE_HORN || instr->otyp == BUGLE)
772.                && !can_blow(&gy.youmonst)) {
773.         You("are incapable of playing %s.", thesimpleoname(instr));
774.         return ECMD_OK;
775.     }
776.     if (instr->otyp != LEATHER_DRUM && instr->otyp != DRUM_OF_EARTHQUAKE
777.         && !(Stunned || Confusion || Hallucination)) {
778.         c = ynq("Improvise?");
779.         if (c == 'q')
780.             goto nevermind;
781.     }
782. 
783.     if (c != 'n')
784.         return do_improvisation(instr) ? ECMD_TIME : ECMD_OK;
785. 
786.     if (u.uevent.uheard_tune == 2)
787.         c = ynq("Play the passtune?");
788.     if (c == 'q') {
789.         goto nevermind;
790.     } else if (c == 'y') {
791.         Strcpy(buf, svt.tune);
792.     } else {
793.         getlin("What tune are you playing? [5 notes, A-G]", buf);
794.         (void) mungspaces(buf);
795.         if (*buf == '\033')
796.             goto nevermind;
797. 
798.         /* convert to uppercase and change any "H" to the expected "B" */
799.         for (s = buf; *s; s++) {
800.             *s = highc(*s);
801.             if (*s == 'H')
802.                 *s = 'B';
803.         }
804.     }
805. 
806.     You(!Deaf ? "extract a strange sound from %s!"
807.               : "can feel %s emitting vibrations.", the(xname(instr)));
808.     Hero_playnotes(obj_to_instr(instr), buf, 50);
809. 
810. 
811.     /* Check if there was the Stronghold drawbridge near
812.      * and if the tune conforms to what we're waiting for.
813.      */
814.     if (Is_stronghold(&u.uz)) {
815.         exercise(A_WIS, TRUE); /* just for trying */
816.         if (!strcmp(buf, svt.tune)) {
817.             /* Search for the drawbridge */
818.             for (y = u.uy - 1; y <= u.uy + 1; y++)
819.                 for (x = u.ux - 1; x <= u.ux + 1; x++) {
820.                     if (!isok(x, y))
821.                         continue;
822.                     if (find_drawbridge(&x, &y)) {
823.                         /* tune now fully known */
824.                         u.uevent.uheard_tune = 2;
825.                         record_achievement(ACH_TUNE);
826.                         if (levl[x][y].typ == DRAWBRIDGE_DOWN)
827.                             close_drawbridge(x, y);
828.                         else
829.                             open_drawbridge(x, y);
830.                         return ECMD_TIME;
831.                     }
832.                 }
833.         } else if (!Deaf) {
834.             if (u.uevent.uheard_tune < 1)
835.                 u.uevent.uheard_tune = 1;
836.             /* Okay, it wasn't the right tune, but perhaps
837.              * we can give the player some hints like in the
838.              * Mastermind game */
839.             ok = FALSE;
840.             for (y = u.uy - 1; y <= u.uy + 1 && !ok; y++)
841.                 for (x = u.ux - 1; x <= u.ux + 1 && !ok; x++)
842.                     if (isok(x, y))
843.                         if (IS_DRAWBRIDGE(levl[x][y].typ)
844.                             || is_drawbridge_wall(x, y) >= 0)
845.                             ok = TRUE;
846.             if (ok) { /* There is a drawbridge near */
847.                 int tumblers, gears;
848.                 boolean matched[5];
849. 
850.                 tumblers = gears = 0;
851.                 for (x = 0; x < 5; x++)
852.                     matched[x] = FALSE;
853. 
854.                 for (x = 0; x < (int) strlen(buf); x++)
855.                     if (x < 5) {
856.                         if (buf[x] == svt.tune[x]) {
857.                             gears++;
858.                             matched[x] = TRUE;
859.                         } else {
860.                             for (y = 0; y < 5; y++)
861.                                 if (!matched[y] && buf[x] == svt.tune[y]
862.                                     && buf[y] != svt.tune[y]) {
863.                                     tumblers++;
864.                                     matched[y] = TRUE;
865.                                     break;
866.                                 }
867.                         }
868.                     }
869.                 if (tumblers) {
870.                     if (gears) {
871.                         Soundeffect(se_tumbler_click, 50);
872.                         Soundeffect(se_gear_turn, 50);
873.                         You_hear("%d tumbler%s click and %d gear%s turn.",
874.                                  tumblers, plur(tumblers), gears,
875.                                  plur(gears));
876.                     } else {
877.                         Soundeffect(se_tumbler_click, 50);
878.                         You_hear("%d tumbler%s click.", tumblers,
879.                                  plur(tumblers));
880.                     }
881.                 } else if (gears) {
882.                     You_hear("%d gear%s turn.", gears, plur(gears));
883.                     /* could only get `gears == 5' by playing five
884.                        correct notes followed by excess; otherwise,
885.                        tune would have matched above */
886.                     if (gears == 5) {
887.                         u.uevent.uheard_tune = 2;
888.                         record_achievement(ACH_TUNE);
889.                     }
890.                 }
891.             }
892.         }
893.     }
894.     return ECMD_TIME;
895. 
896.  nevermind:
897.     pline1(Never_mind);
898.     return ECMD_OK;
899. }
900. 
901. enum instruments
902. obj_to_instr(struct obj *obj SOUNDLIBONLY) {
903.     enum instruments ret_instr = ins_no_instrument;
904. 
905. #if defined(SND_LIB_INTEGRATED)
906.     switch(obj->otyp) {
907.         case WOODEN_FLUTE:
908.             ret_instr = ins_flute;
909.             break;
910.         case MAGIC_FLUTE:
911.             ret_instr = ins_pan_flute;
912.             break;
913.         case TOOLED_HORN:
914.             ret_instr = ins_english_horn;
915.             break;
916.         case FROST_HORN:
917.             ret_instr = ins_french_horn;
918.             break;
919.         case FIRE_HORN:
920.             ret_instr = ins_baritone_sax;
921.             break;
922.         case BUGLE:
923.             ret_instr = ins_trumpet;
924.             break;
925.         case WOODEN_HARP:
926.             ret_instr = ins_orchestral_harp;
927.             break;
928.         case MAGIC_HARP:
929.             ret_instr = ins_cello;
930.             break;
931.         case BELL:
932.         case BELL_OF_OPENING:
933.             ret_instr = ins_tinkle_bell;
934.             break;
935.         case DRUM_OF_EARTHQUAKE:
936.             ret_instr = ins_taiko_drum;
937.             break;
938.         case LEATHER_DRUM:
939.             ret_instr = ins_melodic_tom;
940.             break;
941.     }
942. #endif
943.     return ret_instr;
944. }
945. /*music.c*/