Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1x 1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // music.js — Port of music.c
// Musical instruments and their various effects.
import { game } from './gstate.js';
import { rn2, rnd, rn1, c_d } from './rng.js';
import { pline, You, Your, You_feel, You_hear, You_cant, pline_The, verbalize } from './pline.js';
import {
Blind,
Confusion,
Deaf,
Flying,
Fumbling,
Hallucination,
Levitation,
Maybe_Half_Phys,
Role_if,
Stunned,
Unchanging,
Underwater,
Upolyd
} from './macros.js';
import { levl, m_at, t_at } from './map_access.js';
import { ACURR } from './attrib.js';
import { canseemon, newsym, seemimic, setBotl } from './display.js';
import { makemon } from './makemon.js';
import {
mons, PM_GUARD, PM_ARCHEOLOGIST,
S_SNAKE, S_NYMPH, S_MIMIC,
AD_COLD, AD_FIRE,
} from './monsters.js';
import { losehp } from './hack.js';
import { ACH_TUNE, ALTAR, AM_MASK, AM_SANCTUM, ARTICLE_THE, A_DEX, A_WIS, Amask2align, COLNO, CORR, DEAF, DOOR, DRAWBRIDGE_DOWN, D_NODOOR, ECMD_OK, ECMD_TIME, FOUNTAIN, GRAVE, IS_DRAWBRIDGE, KILLED_BY, M_AP_MONSTER, M_AP_NOTHING, M_AP_TYPE, NOTELL, NO_KILLER_PREFIX, Never_mind, PIT, ROOM, ROWNO, SCORR, SDOOR, SHOPBASE, SINK, STRAT_WAITMASK, SUPPRESS_SADDLE, THRONE, TT_BURIEDBALL, TT_PIT, XKILL_NOMSG, has_mgivenname, is_pit, isok, u_at } from './const.js';
import { TOOL_CLASS, WOODEN_FLUTE, MAGIC_FLUTE, TOOLED_HORN, FROST_HORN, FIRE_HORN, BUGLE, WOODEN_HARP, MAGIC_HARP, LEATHER_DRUM, DRUM_OF_EARTHQUAKE, BOULDER } from './objects.js';
import { dist2 } from './hacklib.js';
import { highc, mungspaces, plur, ROLL_FROM, set_levltyp } from './hacklib.js';
import { Tobjnam, Yname2, an, the, thesimpleoname, xname, yname } from './objnam.js';
import { cansee } from './vision.js';
import { ynFunction, getlin } from './input.js';
import { rnl } from './rng.js';
import { consume_obj_charge, makeknown } from './invent.js';
import { Amonnam, DEADMONSTER, Monnam, a_monnam, mon_nam, monflee, uhim, wakeup, x_monnam } from './mon.js';
import { add_damage, in_rooms } from './shk.js';
import { obj_extract_self, sobj_at } from './mkobj.js';
import { selftouch } from './mhitu.js';
import { resist } from './zap.js';
import { flooreffects, Norep } from './do.js';
import { tamedog } from './dog.js';
import { exercise } from './attrib.js';
import { unblock_point, recalc_block_point } from './display.js';
import { can_blow, humanoid, is_clinger, is_flyer, is_hider, is_mercenary, is_mindless as mindless, mdistu, nolimbs, slithy, unique_corpstat } from './mondata.js';
import { desecrate_altar, altarmask_at } from './pray.js';
import { fillholetyp, liquid_flow } from './dig.js';
import { align_str, record_achievement } from './insight.js';
import { onscary } from './monmove.js';
import { Soundeffect } from './sounds.js';
import { maketrap, mselftouch, reset_utrap, set_utrap } from './trap.js';
import { close_drawbridge, find_drawbridge, is_drawbridge_wall, open_drawbridge } from './dbridge.js';
import { sleep_monst, slept_monst } from './mhitm.js';
import { xkilled } from './uhitm.js';
import { getdir } from './cmd.js';
import { cvt_sdoor_to_door } from './detect.js';
import { incr_itimeout } from './timeout.js';
// ── Non-RNG stubs for functions not yet ported ──
// unique_corpstat: imported from mondata.js
// mindless: imported from mondata.js as is_mindless
// resist imported from zap.js
// onscary imported from monmove.js
// monflee imported from mon.js
// canseemon imported from display.js
// is_mercenary imported from mondata.js
// wakeup imported from mon.js
// C ref: mondata.h — ceiling_hider: hider that clings (non-mimic) or flies
function ceiling_hider(ptr) {
return is_hider(ptr) && ((is_clinger(ptr) && ptr.mlet !== S_MIMIC) || is_flyer(ptr));
}
// humanoid imported from mondata.js
// losehp imported from hack.js
function Hero_playnotes(_instr, _notes, _vol) { /* no-op: sound system */ }
function obj_to_instr(_obj) { return 0; /* ins_no_instrument */ }
// can_blow imported from mondata.js
// an imported from objnam.js
// uhim: imported from mon.js
// fillholetyp imported from dig.js
// set_levltyp imported from hacklib.js
// liquid_flow imported from dig.js
// is_flyer imported from mondata.js
// is_clinger imported from mondata.js
// reset_utrap: imported from trap.js
// Maybe_Half_Phys imported from macros.js
// slithy imported from mondata.js
// nolimbs imported from mondata.js
// altarmask_at imported from pray.js
// align_str: imported from insight.js
// desecrate_altar imported from pray.js
// record_achievement imported from insight.js
// TODO: port direction input
// mdistu imported from display.js
// ── Helper constants ──
// ACH_TUNE imported from const.js
// ────────────────────────────────────────────────────────
// Ported functions from music.c
// ────────────────────────────────────────────────────────
// C ref: music.c:44 awaken_scare()
async function awaken_scare(mtmp, scary) {
mtmp.msleeping = 0;
mtmp.mcanmove = 1;
mtmp.mfrozen = 0;
/* may scare some monsters -- waiting monsters excluded */
if (!unique_corpstat(mtmp.data)
&& (mtmp.mstrategy & STRAT_WAITMASK) !== 0) {
mtmp.mstrategy &= ~STRAT_WAITMASK;
} else if (scary
&& !mindless(mtmp.data)
&& !resist(mtmp, TOOL_CLASS, 0, NOTELL)
/* some monsters are immune */
&& onscary(0, 0, mtmp)) {
await monflee(mtmp, 0, false, true);
}
}
// C ref: music.c:66 awaken_monsters()
async function awaken_monsters(distance) {
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp))
continue;
const distm = mdistu(mtmp);
if (distm < distance)
await awaken_scare(mtmp, (distm < Math.trunc(distance / 3)));
}
}
// C ref: music.c:84 put_monsters_to_sleep()
async function put_monsters_to_sleep(distance) {
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp))
continue;
if (mdistu(mtmp) < distance) {
const sleepamt = c_d(10, 10);
if (sleep_monst(mtmp, sleepamt, TOOL_CLASS)) {
mtmp.msleeping = 1; /* 10d10 turns + wake_nearby to rouse */
await slept_monst(mtmp);
}
}
}
}
// C ref: music.c:104 charm_snakes()
async function charm_snakes(distance) {
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp))
continue;
if (mtmp.data.mlet === S_SNAKE && mtmp.mcanmove
&& mdistu(mtmp) < distance) {
const was_peaceful = mtmp.mpeaceful;
mtmp.mpeaceful = 1;
mtmp.mavenge = 0;
mtmp.mstrategy &= ~STRAT_WAITMASK;
const could_see_mon = canseemon(mtmp);
mtmp.mundetected = 0;
newsym(mtmp.mx, mtmp.my);
if (canseemon(mtmp)) {
if (!could_see_mon)
await You(`notice ${a_monnam(mtmp)}, swaying with the music.`);
else
await pline(`${Monnam(mtmp)} freezes, then sways with the music${was_peaceful ? '' : ', and now seems quieter'}.`);
}
}
}
}
// C ref: music.c:138 calm_nymphs()
async function calm_nymphs(distance) {
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp))
continue;
if (mtmp.data.mlet === S_NYMPH && mtmp.mcanmove
&& mdistu(mtmp) < distance) {
mtmp.msleeping = 0;
mtmp.mpeaceful = 1;
mtmp.mavenge = 0;
mtmp.mstrategy &= ~STRAT_WAITMASK;
if (canseemon(mtmp))
await pline(`${Monnam(mtmp)} listens cheerfully to the music, then seems quieter.`);
}
}
}
// C ref: music.c:161 awaken_soldiers()
export async function awaken_soldiers(bugler) {
const is_player = (bugler === game.player);
/* distance of affected non-soldier monsters to bugler */
const distance = (is_player ? game.u.ulevel : bugler.data.mlevel) * 30;
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp))
continue;
if (is_mercenary(mtmp.data) && mtmp.data !== mons[PM_GUARD]) {
if (!mtmp.mtame)
mtmp.mpeaceful = 0;
mtmp.msleeping = 0;
mtmp.mfrozen = 0;
mtmp.mcanmove = 1;
mtmp.mstrategy &= ~STRAT_WAITMASK;
if (canseemon(mtmp))
await pline(`${Monnam(mtmp)} is now ready for battle!`);
else if (!Deaf())
await Norep(`${'You hear'} the rattle of battle gear being readied.`);
} else {
const distm = is_player
? mdistu(mtmp)
: dist2(bugler.mx, bugler.my, mtmp.mx, mtmp.my);
if (distm < distance) {
await awaken_scare(mtmp, (distm < Math.trunc(distance / 3)));
}
}
}
}
// C ref: music.c:195 charm_monsters()
async function charm_monsters(distance) {
if (game.u.uswallow)
distance = 0; /* only u.ustuck will be affected */
let mtmp = game.fmon;
while (mtmp) {
const mtmp2 = mtmp.nmon;
if (!DEADMONSTER(mtmp)) {
if (mdistu(mtmp) <= distance) {
if (!resist(mtmp, TOOL_CLASS, 0, NOTELL) || mtmp.isshk)
await tamedog(mtmp, null, true);
}
}
mtmp = mtmp2;
}
}
// C ref: music.c:220 do_pit()
async function do_pit(x, y, tu_pit) {
const chasm = await maketrap(x, y, PIT);
if (!chasm)
return; /* no pit if portal at that location */
chasm.tseen = 1;
const mtmp = m_at(x, y); /* (redundant?) */
const otmp = sobj_at(BOULDER, x, y);
if (otmp != null) {
if (cansee(x, y))
await pline(`KADOOM! The boulder falls into a chasm${u_at(x, y) ? ' below you' : ''}!`);
if (mtmp)
mtmp.mtrapped = 0;
obj_extract_self(otmp);
await flooreffects(otmp, x, y, '');
return;
}
/* Let liquid flow into the newly created chasm. */
const filltype = fillholetyp(x, y, false);
if (filltype !== ROOM) {
set_levltyp(x, y, filltype);
await liquid_flow(x, y, filltype, chasm, null);
/* liquid_flow() deletes trap, might kill mtmp */
if (t_at(x, y) == null)
return;
}
/* We have to check whether monsters or hero falls into a new pit. */
if (mtmp) {
if (!is_flyer(mtmp.data) && !is_clinger(mtmp.data)) {
const m_already_trapped = mtmp.mtrapped;
mtmp.mtrapped = 1;
if (!m_already_trapped) { /* suppress messages */
if (cansee(x, y)) {
await pline(`${Monnam(mtmp)} falls into a chasm!`);
} else if (humanoid(mtmp.data)) {
Soundeffect('se_scream', 50);
await You_hear('a scream!');
}
}
/* Falling is okay for falling down within a pit from jostling too */
mselftouch(mtmp, 'Falling, ', true);
if (!DEADMONSTER(mtmp)) {
const dmg = rnd(m_already_trapped ? 4 : 6);
mtmp.mhp -= dmg;
if (DEADMONSTER(mtmp)) {
if (!cansee(x, y)) {
await pline('It is destroyed!');
} else {
await You(`destroy ${mtmp.mtame
? x_monnam(mtmp, ARTICLE_THE, 'poor',
has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0,
false)
: mon_nam(mtmp)}!`);
}
await xkilled(mtmp, XKILL_NOMSG);
}
}
}
} else if (u_at(x, y)) {
if (game.u.utrap && game.u.utraptype === TT_BURIEDBALL) {
await Your('chain breaks!');
await reset_utrap(true);
}
if (Levitation() || Flying() || is_clinger(game.player?.data)) {
if (!tu_pit) { /* no pit here previously */
await pline('A chasm opens up under you!');
await You("don't fall in!");
}
} else if (!tu_pit || !game.u.utrap || game.u.utraptype !== TT_PIT) {
await You('fall into a chasm!');
const trap_dur = rn1(6, 2);
set_utrap(trap_dur, TT_PIT);
const phys_dmg = Maybe_Half_Phys(rnd(6));
await losehp(phys_dmg, 'fell into a chasm', NO_KILLER_PREFIX);
await selftouch('Falling, you');
} else if (game.u.utrap && game.u.utraptype === TT_PIT) {
/* C: keepfooting = (!(Fumbling && rn2(5))
* && (!(rnl(...)) || ((ACURR(A_DEX) > 7) && rn2(5))))
* Short-circuit evaluation: rn2(5) only called if Fumbling is true;
* second rn2(5) only called if rnl is truthy AND DEX > 7.
*/
const keepfooting =
(!(Fumbling() && rn2(5))
&& (!rnl(Role_if(PM_ARCHEOLOGIST) ? 3 : 9)
|| ((ACURR(A_DEX) > 7) && rn2(5))));
await You('are jostled around violently!');
const trap_dur2 = rn1(6, 2);
set_utrap(trap_dur2, TT_PIT);
const phys_dmg2 = Maybe_Half_Phys(rnd(keepfooting ? 2 : 4));
await losehp(phys_dmg2, 'hurt in a chasm', NO_KILLER_PREFIX);
if (keepfooting)
exercise(A_DEX, true);
else
await selftouch((Upolyd() && (slithy(game.player?.data)
|| nolimbs(game.player?.data)))
? 'Shaken, you'
: 'Falling down, you');
}
} else {
newsym(x, y);
}
}
// C ref: music.c:343 do_earthquake()
async function do_earthquake(force) {
const into_a_chasm = ' into a chasm';
const trap_at_u = t_at(game.u.ux, game.u.uy);
let tu_pit = 0;
if (trap_at_u)
tu_pit = is_pit(trap_at_u.ttyp) ? 1 : 0;
if (force > 13) /* sanity precaution; maximum used is actually 10 */
force = 13;
let start_x = game.u.ux - (force * 2);
let start_y = game.u.uy - (force * 2);
let end_x = game.u.ux + (force * 2);
let end_y = game.u.uy + (force * 2);
start_x = Math.max(start_x, 1);
start_y = Math.max(start_y, 0);
end_x = Math.min(end_x, COLNO - 1);
end_y = Math.min(end_y, ROWNO - 1);
for (let x = start_x; x <= end_x; x++) {
for (let y = start_y; y <= end_y; y++) {
const mtmp = m_at(x, y);
if (mtmp != null) {
wakeup(mtmp, true); /* peaceful monster will become hostile */
if (mtmp.mundetected) {
mtmp.mundetected = 0;
newsym(x, y);
if (ceiling_hider(mtmp.data)) {
if (cansee(x, y)) {
await pline(`${Amonnam(mtmp)} is shaken loose from the ceiling!`);
} else if (!is_flyer(mtmp.data)) {
Soundeffect('se_thump', 50);
await You_hear('a thump.');
}
}
}
if (M_AP_TYPE(mtmp) !== M_AP_NOTHING
&& M_AP_TYPE(mtmp) !== M_AP_MONSTER)
seemimic(mtmp);
}
if (rn2(14 - force))
continue;
const loc = levl(x, y);
if (!loc) continue;
switch (loc.typ) {
case FOUNTAIN: /* make the fountain disappear */
if (cansee(x, y))
await pline_The(`fountain falls${into_a_chasm}.`);
await do_pit(x, y, tu_pit);
break;
case SINK:
if (cansee(x, y))
await pline_The(`kitchen sink falls${into_a_chasm}.`);
await do_pit(x, y, tu_pit);
break;
case ALTAR: {
const amsk = altarmask_at(x, y);
/* always preserve the high altars */
if ((amsk & AM_SANCTUM) !== 0)
break;
const algn = Amask2align(amsk & AM_MASK);
if (cansee(x, y))
await pline_The(`${align_str(algn)} altar falls${into_a_chasm}.`);
await desecrate_altar(false, algn);
await do_pit(x, y, tu_pit);
break;
}
case GRAVE:
if (cansee(x, y))
await pline_The(`headstone topples${into_a_chasm}.`);
await do_pit(x, y, tu_pit);
break;
case THRONE:
if (cansee(x, y))
await pline_The(`throne falls${into_a_chasm}.`);
await do_pit(x, y, tu_pit);
break;
case SCORR:
loc.typ = CORR;
unblock_point(x, y);
if (cansee(x, y))
await pline('A secret corridor is revealed.');
/* FALLTHROUGH */
await do_pit(x, y, tu_pit);
break;
case CORR:
case ROOM:
await do_pit(x, y, tu_pit);
break;
case SDOOR:
cvt_sdoor_to_door(loc); /* .typ = DOOR */
if (cansee(x, y))
await pline('A secret door is revealed.');
/* FALLTHROUGH */
// fall through
case DOOR: /* make the door collapse */
/* if already doorless, treat like room or corridor */
if (loc.doormask === D_NODOOR) {
await do_pit(x, y, tu_pit);
break;
}
/* wasn't doorless, now it will be */
loc.doormask = D_NODOOR;
recalc_block_point(x, y);
newsym(x, y); /* before pline */
if (cansee(x, y))
await pline_The('door collapses.');
if (in_rooms(x, y, SHOPBASE).length > 0)
add_damage(x, y, 0);
break;
}
}
}
}
// C ref: music.c:477 generic_lvl_desc()
function generic_lvl_desc() {
// TODO: port Is_astralevel, In_endgame, Is_sanctum, In_sokoban, In_V_tower
return 'dungeon';
}
const beats = [
'stepper', 'one drop', 'slow two', 'triple stroke roll',
'double shuffle', 'half-time shuffle', 'second line', 'train',
];
// C ref: music.c:502 do_improvisation()
async function do_improvisation(instr) {
let do_spec = !(Stunned() || Confusion());
let mundane = false;
let same_old_song = false;
// itmp = shallow copy of instr
const itmp = Object.assign({}, instr);
itmp.oextra = null;
/* if won't yield special effect, make sound of mundane counterpart */
if (!do_spec || instr.spe <= 0) {
while (game.objects[itmp.otyp]?.oc_magic) {
itmp.otyp -= 1;
mundane = true;
}
}
const PLAY_NORMAL = 0x00;
const PLAY_STUNNED = 0x01;
const PLAY_CONFUSED = 0x02;
const PLAY_HALLU = 0x04;
let mode = PLAY_NORMAL;
if (Stunned())
mode |= PLAY_STUNNED;
if (Confusion())
mode |= PLAY_CONFUSED;
if (Hallucination())
mode |= PLAY_HALLU;
if (!rn2(2)) {
/* remove if STUNNED+CONFUSED ever gets its own message below */
if (mode === (PLAY_STUNNED | PLAY_CONFUSED))
mode = !rn2(2) ? PLAY_STUNNED : PLAY_CONFUSED;
/* likewise for stunned and/or confused combined with hallucination */
if (mode & PLAY_HALLU)
mode = PLAY_HALLU;
}
switch (mode) {
case PLAY_NORMAL:
await You(`start playing ${yname(instr)}.`);
break;
case PLAY_STUNNED:
if (!Deaf())
await You('radiate an obnoxious droning sound.');
else
await You_feel('a monotonous vibration.');
break;
case PLAY_CONFUSED:
if (!Deaf())
await You('generate a raucous noise.');
else
await You_feel('a jarring vibration.');
break;
case PLAY_HALLU:
await You('disseminate a kaleidoscopic display of floating butterflies.');
break;
case PLAY_STUNNED | PLAY_CONFUSED:
case PLAY_STUNNED | PLAY_HALLU:
case PLAY_CONFUSED | PLAY_HALLU:
case PLAY_STUNNED | PLAY_CONFUSED | PLAY_HALLU:
default:
await pline('What you perform is quite far from music...');
break;
}
const improvResult = improvised_notes();
const improvisation = improvResult.notes;
same_old_song = improvResult.same_old_song;
switch (itmp.otyp) { /* note: itmp.otyp might differ from instr.otyp */
case MAGIC_FLUTE: /* Make monster fall asleep */
consume_obj_charge(instr, true);
await You(`${!Deaf() ? '' : 'seem to '}produce ${Hallucination() ? 'piped' : 'soft'}${same_old_song ? ', familiar' : ''} music.`);
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
await put_monsters_to_sleep(game.u.ulevel * 5);
exercise(A_DEX, true);
break;
case WOODEN_FLUTE: /* May charm snakes */ {
const dex_check = rn2(ACURR(A_DEX));
do_spec &= (dex_check + game.u.ulevel > 25) ? 1 : 0;
if (!Deaf())
await pline(`${Tobjnam(instr, do_spec ? 'trill' : 'toot')}${same_old_song ? ' a familiar tune' : ''}.`);
else
await You_feel(`${yname(instr)} ${do_spec ? 'trill' : 'toot'}.`);
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
if (do_spec)
await charm_snakes(game.u.ulevel * 3);
exercise(A_DEX, true);
break;
}
case FIRE_HORN: /* Idem wand of fire */
case FROST_HORN: { /* Idem wand of cold */
consume_obj_charge(instr, true);
if (!await getdir(null)) {
await pline(`${Tobjnam(instr, 'vibrate')}.`);
break;
} else if (!game.u.dx && !game.u.dy && !game.u.dz) {
// zapyourself not ported; stub
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
// TODO: zapyourself(instr, TRUE) + losehp
} else {
// TODO: ubuzz not ported
if (!Blind())
await pline('A blast shoots out of the horn!');
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
// TODO: ubuzz(type, rn1(6, 6))
}
makeknown(instr.otyp);
break;
}
case TOOLED_HORN: /* Awaken or scare monsters */
if (!Deaf())
await You(`produce a frightful, grave${same_old_song ? ', yet familiar,' : ''} sound.`);
else
await You('blow into the horn.');
Hero_playnotes(obj_to_instr(itmp), improvisation, 80);
await awaken_monsters(game.u.ulevel * 30);
exercise(A_WIS, false);
break;
case BUGLE: /* Awaken & attract soldiers */
if (!Deaf())
await You(`extract a loud${same_old_song ? ', familiar' : ''} noise from ${yname(instr)}.`);
else
await You('blow into the bugle.');
Hero_playnotes(obj_to_instr(itmp), improvisation, 80);
await awaken_soldiers(game.player);
exercise(A_WIS, false);
break;
case MAGIC_HARP: /* Charm monsters */
consume_obj_charge(instr, true);
if (!Deaf())
await pline(`${Tobjnam(instr, 'produce')} very attractive${same_old_song ? ' and familiar' : ''} music.`);
else
await You_feel('very soothing vibrations.');
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
await charm_monsters(Math.trunc((game.u.ulevel - 1) / 3) + 1);
exercise(A_DEX, true);
break;
case WOODEN_HARP: { /* May calm Nymph */
const dex_check2 = rn2(ACURR(A_DEX));
do_spec &= (dex_check2 + game.u.ulevel > 25) ? 1 : 0;
if (!Deaf())
await pline(`${Yname2(instr)} ${(do_spec && same_old_song)
? 'produces a familiar, lilting melody'
: (do_spec) ? 'produces a lilting melody'
: (same_old_song) ? 'twangs a familiar tune'
: 'twangs'}.`);
else
await You_feel('soothing vibrations.');
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
if (do_spec)
await calm_nymphs(game.u.ulevel * 3);
exercise(A_DEX, true);
break;
}
case DRUM_OF_EARTHQUAKE: /* create several pits */
consume_obj_charge(instr, true);
await You('produce a heavy, thunderous rolling!');
Hero_playnotes(obj_to_instr(itmp), 'C', 100);
await pline_The(`entire ${generic_lvl_desc()} is shaking around you!`);
await do_earthquake(Math.trunc((game.u.ulevel - 1) / 3) + 1);
/* shake up monsters in a much larger radius... */
await awaken_monsters(ROWNO * COLNO);
makeknown(DRUM_OF_EARTHQUAKE);
break;
case LEATHER_DRUM: /* Awaken monsters */
if (!mundane) {
if (!Deaf()) {
await You(`beat a ${same_old_song ? 'familiar ' : ''}deafening row!`);
Hero_playnotes(obj_to_instr(itmp), 'CCC', 100);
incr_itimeout(DEAF, rn1(20, 30));
} else {
await You('pound on the drum.');
}
exercise(A_WIS, false);
} else {
/* Pre-evaluate RNG for the three-way choice: rn2(2), then maybe rn2(2) */
const choice1 = rn2(2);
const choice2 = rn2(2);
const verb = choice1 ? 'butcher' : choice2 ? 'manage' : 'pull off';
await You(`${verb} ${an(ROLL_FROM(beats))}.`);
Hero_playnotes(obj_to_instr(itmp), improvisation, 50);
}
await awaken_monsters(game.u.ulevel * (mundane ? 5 : 40));
setBotl('do_improvisation');
break;
default:
await pline(`What a weird instrument (${instr.otyp})!`);
return 0;
}
return 2; /* That takes time */
}
// C ref: music.c:732 improvised_notes()
function improvised_notes() {
const notes = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
// context.jingle is stored in game.context
if (!game.context) game.context = {};
if (!game.context.jingle) game.context.jingle = '';
let same_old_song = false;
if (!(Unchanging() && game.context.jingle.length > 0)) {
const notecount = rnd(5); /* 1 - 5 */
let jingle = '';
for (let i = 0; i < notecount; ++i) {
jingle += ROLL_FROM(notes);
}
game.context.jingle = jingle;
same_old_song = false;
} else {
same_old_song = true;
}
return { notes: game.context.jingle, same_old_song };
}
// C ref: music.c:758 do_play_instrument()
export async function do_play_instrument(instr) {
let buf = '';
let c = 'y';
if (Underwater()) {
await You_cant('play music underwater!');
return ECMD_OK;
} else if ((instr.otyp === WOODEN_FLUTE || instr.otyp === MAGIC_FLUTE
|| instr.otyp === TOOLED_HORN || instr.otyp === FROST_HORN
|| instr.otyp === FIRE_HORN || instr.otyp === BUGLE)
&& !can_blow(game.player)) {
await You(`are incapable of playing ${thesimpleoname(instr)}.`);
return ECMD_OK;
}
if (instr.otyp !== LEATHER_DRUM && instr.otyp !== DRUM_OF_EARTHQUAKE
&& !(Stunned() || Confusion() || Hallucination())) {
c = await ynFunction('Improvise?', 'ynq', 'y');
if (c === 'q') {
await pline(Never_mind);
return ECMD_OK;
}
}
if (c !== 'n') {
const result = await do_improvisation(instr);
return result ? ECMD_TIME : ECMD_OK;
}
if (game.u.uevent && game.u.uevent.uheard_tune === 2)
c = await ynFunction('Play the passtune?', 'ynq', 'y');
if (c === 'q') {
await pline(Never_mind);
return ECMD_OK;
} else if (c === 'y') {
// C: Strcpy(buf, svt.tune)
buf = game.tune || 'AAAAA'; // TODO: svt.tune
} else {
buf = await getlin('What tune are you playing? [5 notes, A-G]');
buf = mungspaces(buf);
if (buf.charAt(0) === '\x1b') { // ESC
await pline(Never_mind);
return ECMD_OK;
}
/* convert to uppercase and change any "H" to the expected "B" */
buf = buf.split('').map(ch => {
let c2 = highc(ch);
if (c2 === 'H') c2 = 'B';
return c2;
}).join('');
}
await You(!Deaf() ? `extract a strange sound from ${the(xname(instr))}!`
: `can feel ${the(xname(instr))} emitting vibrations.`);
Hero_playnotes(obj_to_instr(instr), buf, 50);
/* Check if there was the Stronghold drawbridge near
* and if the tune conforms to what we're waiting for. */
// TODO: Is_stronghold not ported
const is_stronghold = false; // Is_stronghold(&u.uz)
if (is_stronghold) {
exercise(A_WIS, true); /* just for trying */
const tune = game.tune || 'AAAAA';
if (buf === tune) {
/* Search for the drawbridge */
for (let dy = game.u.uy - 1; dy <= game.u.uy + 1; dy++) {
for (let dx = game.u.ux - 1; dx <= game.u.ux + 1; dx++) {
if (!isok(dx, dy))
continue;
// find_drawbridge modifies x,y by reference in C;
// simplified here
if (find_drawbridge(dx, dy)) {
if (game.u.uevent) game.u.uevent.uheard_tune = 2;
record_achievement(ACH_TUNE);
const loc = levl(dx, dy);
if (loc && loc.typ === DRAWBRIDGE_DOWN)
await close_drawbridge(dx, dy);
else
await open_drawbridge(dx, dy);
return ECMD_TIME;
}
}
}
} else if (!Deaf()) {
if (game.u.uevent && game.u.uevent.uheard_tune < 1)
game.u.uevent.uheard_tune = 1;
/* Mastermind-like hints */
let ok = false;
for (let dy = game.u.uy - 1; dy <= game.u.uy + 1 && !ok; dy++) {
for (let dx = game.u.ux - 1; dx <= game.u.ux + 1 && !ok; dx++) {
if (isok(dx, dy)) {
const loc = levl(dx, dy);
if (loc && (IS_DRAWBRIDGE(loc.typ)
|| is_drawbridge_wall(dx, dy) >= 0))
ok = true;
}
}
}
if (ok) { /* There is a drawbridge near */
let tumblers = 0, gears = 0;
const matched = [false, false, false, false, false];
for (let i = 0; i < buf.length; i++) {
if (i < 5) {
if (buf[i] === tune[i]) {
gears++;
matched[i] = true;
} else {
for (let j = 0; j < 5; j++) {
if (!matched[j] && buf[i] === tune[j]
&& buf[j] !== tune[j]) {
tumblers++;
matched[j] = true;
break;
}
}
}
}
}
if (tumblers) {
if (gears) {
Soundeffect('se_tumbler_click', 50);
Soundeffect('se_gear_turn', 50);
await You_hear(`${tumblers} tumbler${plur(tumblers)} click and ${gears} gear${plur(gears)} turn.`);
} else {
Soundeffect('se_tumbler_click', 50);
await You_hear(`${tumblers} tumbler${plur(tumblers)} click.`);
}
} else if (gears) {
await You_hear(`${gears} gear${plur(gears)} turn.`);
if (gears === 5) {
if (game.u.uevent) game.u.uevent.uheard_tune = 2;
record_achievement(ACH_TUNE);
}
}
}
}
}
return ECMD_TIME;
}
|