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