All files / js engrave.js

77.86% Statements 517/664
63.08% Branches 94/149
54.16% Functions 13/24
77.86% Lines 517/664

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 66573x 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 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x                     73x 73x 73x 73x 73x 73x 73x 83x 83x 83x 83x 68x 355x 355x 355x 355x 355x         355x 355x 279x 279x 355x 29x 29x 29x 250x 250x 355x 53x 352x 197x 7531x 100x 100x 100x 100x 100x 100x       100x 100x 100x 7531x 197x 250x 250x 353x 355x 68x 83x 83x 83x 5x 5x 83x 83x 73x 73x 73x 73x 73x 73x 73x 73x 22x 22x 22x 22x 8x 8x 22x 22x 22x 73x 73x 73x 73x 73x 142407x 142407x 279027x 279027x 141403x 142407x 73x 73x 73x 73x 73x 90400x 90400x 495x 495x 495x 495x     495x 90394x 90400x 73x 73x 73x 73x 73x 275x 275x 275x 73x 73x 73x 73x 73x 15902x 15902x 15902x 15902x 61x 61x 61x 17x 17x 61x 61x 61x 61x 61x 61x     61x 61x 15902x 73x 73x 73x 73x 73x                         73x 73x 73x 73x 73x 4382x 4382x 67x 67x 67x 67x 67x 2358x 6x 6x 6x 6x 6x 4382x 4382x 57x 57x 57x 57x 57x 4382x 4x 4x 4x 4x 4x 4382x           4382x           4382x       4382x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 58x 58x 67x 67x 67x 67x 67x 67x 67x 4382x 73x 73x 73x 73x 73x 73x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 399x 8x 8x 1x 1x 8x 399x 399x 399x 399x 399x 399x 399x 73x 73x 73x 73x 73x 73x               73x 73x 73x 73x 73x 257x 257x 257x 73x 73x 73x 73x 73x 83x 83x 83x 83x 83x 68x 68x 83x 73x 73x 73x 73x 73x 73x                         73x 73x 73x 73x 73x 245x 245x 245x 245x 244x 244x 244x 244x 244x 244x 244x 245x 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 2x 2x 2x 2x 2x 2x 2x 2x         2x 2x 2x       2x 2x       2x 2x       2x 2x 2x 24x 24x 24x 24x 15x 24x 24x 14x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x   1x         1x             1x             1x 2x 2x 2x 2x                     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 21x 21x 21x 21x 1x 1x 21x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x  
// engrave.js — Port of engrave.c
// Engraving functions: make_engr_at, engr_at, del_engr, del_engr_at,
// sengr_at, read_engr_at, u_wipe_engr, wipe_engr_at, wipeout_text,
// random_engraving, can_reach_floor, cant_reach_floor, freehand,
// make_grave, doengrave, save_engravings, rest_engravings, rloc_engr,
// disturb_grave, sanitize_engravings, forget_engravings, blengr,
// engr_can_be_felt, see_engraving, feel_engraving
 
import { game } from './gstate.js';
import { rn2, rnd, rn1, pushRngLogEntry } from './rng.js';
import { PM_GHOUL } from './monsters.js';
import {
    DUST, ENGRAVE, BURN, MARK, HEADSTONE, ENGR_BLOOD, N_ENGRAVE,
    GRAVE, ICE, ROOM, BUFSZ, COLNO, ROWNO,
    ECMD_OK, ECMD_FAIL, ECMD_CANCEL,
    A_WIS,
    IS_GRAVE, IS_ALTAR, IS_FOUNTAIN, IS_AIR, ACCESSIBLE,
    Never_mind,
    CLOUD, DRAWBRIDGE_DOWN,
    GETOBJ_ALLOWCNT, GETOBJ_PROMPT, GETOBJ_SUGGEST, GETOBJ_DOWNPLAY,
    u_at, FINGER, FINGERTIP, P_PICK_AXE,
} from './const.js';
import { newsym, setBotl } from './display.js';
import { surface } from './hacklib.js';
import { is_ice, is_lava, is_pool } from './dbridge.js';
import { t_at } from './map_access.js';
import { cansee } from './vision.js';
import { Blind, Deaf, Levitation, Flying, Confusion, Stunned, Hallucination, getLuck } from './macros.js';
import { getlin, ynFunction } from './input.js';
import { getobj } from './invent.js';
import { nomul, set_occupation } from './cmd.js';
import { exercise } from './attrib.js';
import { makemon } from './makemon.js';
import { goodpos } from './makemon.js';
import { EPITAPHS, EPITAPH_LINE_BYTES, EPITAPH_CHUNKSIZE } from './epitaphs.js';
import { get_rnd_text, getrumor } from './rumors.js';
import { parseEncryptedDataFile, xcrypt } from './hacklib.js';
import { ENGRAVE_FILE_TEXT } from './engrave_data.js';
import { is_ammo, DAGGER, KNIFE, AXE, PICK_AXE, DWARVISH_MATTOCK, WAN_DIGGING, WAN_FIRE, WAN_LIGHTNING, SPE_DIG, SPE_FIREBALL, SPE_CONE_OF_COLD, SCR_FIRE, FLESH, TOWEL, MAGIC_MARKER, TOOL_CLASS, WEAPON_CLASS, WAND_CLASS, GEM_CLASS, RING_CLASS } from './objects.js';
import { P_SKILL, use_skill } from './weapon.js';
import { is_blade, is_weptool } from './mondata.js';
import { body_part, mbodypart } from './polyself.js';
 
// Parse encrypted engrave data at module init
const _engraveData = parseEncryptedDataFile(ENGRAVE_FILE_TEXT);
const ENGRAVINGS = _engraveData.texts;
const ENGRAVE_LINE_BYTES = _engraveData.lineBytes;
const ENGRAVE_CHUNKSIZE = _engraveData.chunksize;
import { can_reach_floor } from './hack.js';
import { You, You_cant, You_feel, You_hear, You_see, pline, pline_The } from './pline.js';
import { doname } from './objnam.js';
 
const MD_PAD_RUMORS = 60;
 
// ============================================================
// rubouts table — C ref: engrave.c:66-116
// ============================================================
const rubouts = [
    ['A', '^'],     ['B', 'Pb['],   ['C', '('],     ['D', '|)['],
    ['E', '|FL[_'], ['F', '|-'],    ['G', 'C('],    ['H', '|-'],
    ['I', '|'],     ['K', '|<'],    ['L', '|_'],    ['M', '|'],
    ['N', '|\\'],   ['O', 'C('],    ['P', 'F'],     ['Q', 'C('],
    ['R', 'PF'],    ['T', '|'],     ['U', 'J'],     ['V', '/\\'],
    ['W', 'V/\\'],  ['Z', '/'],     ['b', '|'],     ['d', 'c|'],
    ['e', 'c'],     ['g', 'c'],     ['h', 'n'],     ['j', 'i'],
    ['k', '|'],     ['l', '|'],     ['m', 'nr'],    ['n', 'r'],
    ['o', 'c'],     ['q', 'c'],     ['w', 'v'],     ['y', 'v'],
    [':', '.'],     [';', ',:'],    [',', '.'],     ['=', '-'],
    ['+', '-|'],    ['*', '+'],     ['@', '0'],     ['0', 'C('],
    ['1', '|'],     ['6', 'o'],     ['7', '/'],     ['8', '3o'],
];
 
// ============================================================
// blind_writing table — C ref: engrave.c:1746-1765
// ============================================================
const blind_writing = [
    [0x44,0x66,0x6d,0x69,0x62,0x65,0x22,0x45,0x7b,0x71,0x65,0x6d,0x72,0x00],
    [0x51,0x67,0x60,0x7a,0x7f,0x21,0x40,0x71,0x6b,0x71,0x6f,0x67,0x63,0x00],
    [0x49,0x6d,0x73,0x69,0x62,0x65,0x22,0x4c,0x61,0x7c,0x6d,0x67,0x24,0x42,0x7f,0x69,0x6c,0x77,0x67,0x7e,0x00],
    [0x4b,0x6d,0x6c,0x66,0x30,0x4c,0x6b,0x68,0x7c,0x7f,0x6f,0x00],
    [0x51,0x67,0x70,0x7a,0x7f,0x6f,0x67,0x68,0x64,0x71,0x21,0x4f,0x6b,0x6d,0x7e,0x72,0x00],
    [0x4c,0x63,0x76,0x61,0x71,0x21,0x48,0x6b,0x7b,0x75,0x67,0x63,0x24,0x45,0x65,0x6b,0x6b,0x65,0x00],
    [0x4c,0x67,0x68,0x6b,0x78,0x68,0x6d,0x76,0x7a,0x75,0x21,0x4f,0x71,0x7a,0x75,0x6f,0x77,0x00],
    [0x44,0x66,0x6d,0x7c,0x78,0x21,0x50,0x65,0x66,0x65,0x6c,0x00],
    [0x44,0x66,0x73,0x69,0x62,0x65,0x22,0x56,0x7d,0x63,0x69,0x76,0x6b,0x66,0x00],
];
 
// C ref: ROLL_FROM macro — pick random element
function blengr() {
    const idx = rn2(blind_writing.length);
    const bytes = blind_writing[idx];
    // Convert byte array to string (stop at 0x00)
    let s = '';
    for (let i = 0; i < bytes.length && bytes[i] !== 0x00; i++) {
        s += String.fromCharCode(bytes[i]);
    }
    return xcrypt(s);
}
 
// ============================================================
// wipeout_text — C ref: engrave.c:119-183
// Degrade characters in a string. Returns modified string.
// In C this mutates in-place; in JS we return a new string.
// ============================================================
export function wipeout_text(engr, cnt, seed) {
    let chars = engr.split('');
    let lth = chars.length;
 
    if (lth && cnt > 0) {
        while (cnt-- > 0) {
            let nxt, use_rubout;
            if (!seed) {
                nxt = rn2(lth);
                use_rubout = rn2(4);
            } else {
                nxt = seed % lth;
                seed = (seed * 31) % (BUFSZ - 1);
                use_rubout = seed & 3;
            }
            let s = chars[nxt];
            if (s === ' ') continue;
 
            // rub out unreadable & small punctuation marks
            if ("?.,'`-|_".indexOf(s) >= 0) {
                chars[nxt] = ' ';
                continue;
            }
 
            let i;
            if (!use_rubout) {
                i = rubouts.length;
            } else {
                for (i = 0; i < rubouts.length; i++) {
                    if (s === rubouts[i][0]) {
                        const wipeto = rubouts[i][1];
                        const ln = wipeto.length;
                        let j;
                        if (!seed) {
                            j = rn2(ln);
                        } else {
                            seed = (seed * 31) % (BUFSZ - 1);
                            j = seed % ln;
                        }
                        chars[nxt] = wipeto[j];
                        break;
                    }
                }
            }
 
            // didn't pick rubout; use '?' for unreadable character
            if (i === rubouts.length) chars[nxt] = '?';
        }
    }
 
    // trim trailing spaces
    while (lth > 0 && chars[lth - 1] === ' ') {
        chars[--lth] = '';
    }
    return chars.slice(0, lth).join('');
}
 
// ============================================================
// random_engraving — C ref: engrave.c:51-63
// ============================================================
// C: char *random_engraving(char *outbuf, char *pristine_copy)
// 75% chance: try a rumor (getrumor(0, buf, TRUE=exclude_cookie)).
// If rn2(4)==0, or getrumor returns empty, fall back to engrave file.
export function random_engraving() {
    let pristine;
    // C: if (!rn2(4) || !(rumor = getrumor(0, pristine_copy, TRUE)) || !*rumor)
    //        get_rnd_text(ENGRAVEFILE, pristine_copy, rn2, MD_PAD_RUMORS);
    if (!rn2(4) || !(pristine = getrumor(0, true)) || !pristine) {
        pristine = get_rnd_text(ENGRAVINGS, ENGRAVE_LINE_BYTES, ENGRAVE_CHUNKSIZE, rn2, MD_PAD_RUMORS);
    }
    const wiped = wipeout_text(pristine, Math.trunc(pristine.length / 4), 0);
    return { text: wiped, pristine };
}
 
// ============================================================
// engr_at — C ref: engrave.c:230-241
// ============================================================
export function engr_at(x, y) {
    if (!game.level?.engravings) return null;
    for (const ep of game.level.engravings) {
        if (ep.x === x && ep.y === y) return ep;
    }
    return null;
}
 
// ============================================================
// sengr_at — C ref: engrave.c:250-261
// ============================================================
export function sengr_at(s, x, y, strict) {
    const ep = engr_at(x, y);
    if (ep && ep.engr_type !== HEADSTONE && ep.engr_time <= game.moves) {
        const txt = ep.text || '';
        if (strict) {
            if (txt.toLowerCase() === s.toLowerCase()) return ep;
        } else {
            if (txt.toLowerCase().indexOf(s.toLowerCase()) >= 0) return ep;
        }
    }
    return null;
}
 
// ============================================================
// u_wipe_engr — C ref: engrave.c:263-268
// ============================================================
export function u_wipe_engr(cnt) {
    if (can_reach_floor(true))
        wipe_engr_at(game.u.ux, game.u.uy, cnt, false);
}
 
// ============================================================
// wipe_engr_at — C ref: engrave.c:270-291
// ============================================================
export function wipe_engr_at(x, y, cnt, magical) {
    const ep = engr_at(x, y);
 
    // Headstones and specially marked engravings are indelible
    if (ep && ep.engr_type !== HEADSTONE && !ep.nowipeout) {
        pushRngLogEntry(`^wipe[${x},${y}]`);
        if (ep.engr_type !== BURN || is_ice(x, y) || (magical && !rn2(2))) {
            if (ep.engr_type !== DUST && ep.engr_type !== ENGR_BLOOD) {
                cnt = rn2(1 + Math.trunc(50 / (cnt + 1))) ? 0 : 1;
            }
            let txt = ep.text || '';
            txt = wipeout_text(txt, cnt, 0);
            // strip leading spaces
            txt = txt.replace(/^ +/, '');
            ep.text = txt;
            if (!txt) {
                del_engr(ep);
            }
        }
    }
}
 
// ============================================================
// engr_can_be_felt — C ref: engrave.c:297-316
// ============================================================
export function engr_can_be_felt(ep) {
    switch (ep.engr_type) {
    case ENGRAVE:
    case HEADSTONE:
    case BURN:
        return true;
    case DUST:
    case MARK:
    case ENGR_BLOOD:
    default:
        return false;
    }
}
 
// ============================================================
// read_engr_at — C ref: engrave.c:318-406
// ============================================================
export async function read_engr_at(x, y) {
    const ep = engr_at(x, y);
    if (!ep || !ep.text) return;
 
    const eloc = surface(x, y);
    let sensed = 0;
 
    switch (ep.engr_type) {
    case DUST:
        if (!Blind()) {
            sensed = 1;
            await pline(`Something is written here in the ${is_ice(x, y) ? 'frost' : 'dust'}.`);
        }
        break;
    case ENGRAVE:
    case HEADSTONE:
        if (!Blind() || can_reach_floor(true)) {
            sensed = 1;
            await pline(`Something is engraved here on the ${eloc}.`);
        }
        break;
    case BURN:
        if (!Blind() || can_reach_floor(true)) {
            sensed = 1;
            await pline(`Some text has been ${is_ice(x, y) ? 'melted' : 'burned'} into the ${eloc} here.`);
        }
        break;
    case MARK:
        if (!Blind()) {
            sensed = 1;
            await pline(`There's some graffiti on the ${eloc} here.`);
        }
        break;
    case ENGR_BLOOD:
        if (!Blind()) {
            sensed = 1;
            await You_see('a message scrawled in blood here.');
        }
        break;
    default:
        // impossible
        sensed = 1;
        break;
    }
 
    if (sensed) {
        const text = ep.text;
        // C ref: engrave.c:388-394 — append punctuation unless the pristine
        // source already ended with .?! at the corresponding position.
        // A trailing '?' in the sensed text is treated as uncertainty and
        // still gets a final period.
        let endpunct = '';
        if (text.length > 0) {
            const pristine = ep.pristine_text || text;
            const idx = Math.min(text.length - 1, pristine.length - 1);
            const pristineTail = idx >= 0 ? pristine[idx] : '\0';
            if (text[text.length - 1] === '?' || !'.?!'.includes(pristineTail)) {
                endpunct = '.';
            }
        }
        await You(`${Blind() ? 'feel the words' : 'read'}: "${text}"${endpunct}`);
        ep.eread = 1;
        ep.erevealed = 1;
        if (game.context?.run > 0)
            nomul(0);
    }
}
 
// ============================================================
// make_engr_at — C ref: engrave.c:408-459
// Event: ^engr[type,x,y]
// ============================================================
export function make_engr_at(x, y, s, pristine_s, e_time, e_type) {
    const ep_old = engr_at(x, y);
    if (ep_old) del_engr(ep_old);
 
    const engr_type = (e_type > 0) ? e_type : rnd(N_ENGRAVE - 1);
    const ep = {
        x, y,
        engr_type,
        text: s || '',
        pristine_text: pristine_s || s || '',
        remembered_text: s || '',
        engr_time: e_time || 0,
        eread: 0,
        erevealed: 0,
        nowipeout: 0,
        guardobjects: 0,
    };
 
    if (s === 'Elbereth') {
        if (game.in_mklev)
            ep.guardobjects = 1;
        else
            exercise(A_WIS, true);
    }
 
    pushRngLogEntry(`^engr[${ep.engr_type},${x},${y}]`);
 
    if (game.level) {
        game.level.engravings.push(ep);
    }
}
 
// ============================================================
// del_engr — C ref: engrave.c:1646-1666
// Event: ^dengr[x,y]
// ============================================================
export function del_engr(ep) {
    if (!ep) return;
    pushRngLogEntry(`^dengr[${ep.x},${ep.y}]`);
    if (game.level?.engravings) {
        const idx = game.level.engravings.indexOf(ep);
        if (idx >= 0) game.level.engravings.splice(idx, 1);
    }
}
 
// ============================================================
// del_engr_at — C ref: engrave.c:461-468
// ============================================================
export function del_engr_at(x, y) {
    const ep = engr_at(x, y);
    if (ep) del_engr(ep);
}
 
// ============================================================
// freehand — C ref: engrave.c:474-479
// ============================================================
export function freehand() {
    const uwep = game.u?.uwep;
    const uarms = game.u?.uarms;
    // C: (!uwep || !welded(uwep) || (!bimanual(uwep) && (!uarms || !uarms->cursed)))
    // Simplified: assume welded/bimanual not yet wired
    if (!uwep) return true;
    // TODO: wire welded() and bimanual() when available
    return true;
}
 
// ============================================================
// rloc_engr — C ref: engrave.c:1669-1684
// Randomly relocate an engraving.
// ============================================================
export function rloc_engr(ep) {
    let tryct = 200;
    while (--tryct >= 0) {
        const tx = rn1(COLNO - 3, 2);
        const ty = rn2(ROWNO);
        if (!engr_at(tx, ty) && goodpos(tx, ty, null, 0)) {
            ep.x = tx;
            ep.y = ty;
            newsym(tx, ty);
            return;
        }
    }
}
 
// ============================================================
// make_grave — C ref: engrave.c:1689-1706
// ============================================================
export function make_grave(x, y, str) {
    const loc = game.level?.at(x, y);
    if (!loc) return;
    if (loc.typ !== ROOM && loc.typ !== GRAVE) return;
    if (t_at(x, y)) return;
 
    loc.typ = GRAVE;
    del_engr_at(x, y);
    if (!str) {
        str = get_rnd_text(EPITAPHS, EPITAPH_LINE_BYTES, EPITAPH_CHUNKSIZE, rn2, MD_PAD_RUMORS);
    }
    make_engr_at(x, y, str, null, 0, HEADSTONE);
}
 
// ============================================================
// disturb_grave — C ref: engrave.c:1709-1724
// ============================================================
export async function disturb_grave(x, y) {
    const loc = game.level?.at(x, y);
    if (!loc || !IS_GRAVE(loc.typ)) return;
    if (loc.disturbed) return;

    await You('disturb the undead!');
    loc.disturbed = 1;
    await makemon(PM_GHOUL, x, y, 0);
    exercise(A_WIS, false);
}
 
// ============================================================
// sanitize_engravings — C ref: engrave.c:1499-1507
// ============================================================
export function sanitize_engravings() {
    if (!game.level?.engravings) return;
    for (const ep of game.level.engravings) {
        // Strip control characters
        if (ep.text) {
            ep.text = ep.text.replace(/[\x00-\x1f\x7f]/g, '');
        }
    }
}
 
// ============================================================
// forget_engravings — C ref: engrave.c:1510-1523
// ============================================================
export function forget_engravings() {
    if (!game.level?.engravings) return;
    for (const ep of game.level.engravings) {
        ep.erevealed = 0;
        ep.eread = 0;
    }
}
 
// ============================================================
// see_engraving — C ref: engrave.c:1727-1730
// ============================================================
export function see_engraving(ep) {
    newsym(ep.x, ep.y);
}
 
// ============================================================
// feel_engraving — C ref: engrave.c:1735-1744
// ============================================================
export function feel_engraving(ep) {
    if (engr_can_be_felt(ep)) {
        ep.eread = 1;
        ep.erevealed = 1;
        // map_engraving(ep, 1); // TODO: wire when available
        newsym(ep.x, ep.y);
    }
}
 
// ============================================================
// save_engravings — C ref: engrave.c:1552-1583
// In JS, saving is handled by the level serialization system.
// This is a stub that matches the C interface.
// ============================================================
export function save_engravings() {
    // JS save system serializes game.level.engravings directly.
    // No additional work needed here.
}
 
// ============================================================
// rest_engravings — C ref: engrave.c:1585-1621
// In JS, restoring is handled by the level deserialization system.
// This is a stub that matches the C interface.
// ============================================================
export function rest_engravings() {
    // JS save system deserializes game.level.engravings directly.
    // No additional work needed here.
}
 
// ============================================================
// doengrave — C ref: engrave.c:957-1265
// ============================================================
export async function doengrave() {
    let etype = DUST;
    let index;
    let can_reach = can_reach_floor(true);
    const g = game;
 
    if (u_at(g.u.ux, g.u.uy)) {
        // C: if (u.uswallow) { ... }
        if (g.u.uswallow) {
            await pline("You're joking!  Surrounded by %s and you want to engrave?",
                mbodypart(g.u.ustuck, FLESH));
            return ECMD_FAIL;
        }
    }
 
    if (is_pool(g.u.ux, g.u.uy) || is_lava(g.u.ux, g.u.uy) || g.u.uinwater) {
        await You_cant("engrave while swimming.");
        return ECMD_FAIL;
    }
 
    if (IS_AIR(g.level.at(g.u.ux, g.u.uy).typ) || (Flying() && !can_reach)) {
        await You_cant("reach the %s to engrave it.", surface(g.u.ux, g.u.uy));
        return ECMD_FAIL;
    }
 
    if (g.u.usteed && !can_reach) {
        await You_cant("reach the %s while riding.", surface(g.u.ux, g.u.uy));
        return ECMD_FAIL;
    }
 
    // C ref: stylus_ok (engrave.c:483) — getobj callback for engraving tool
    const otmp = await getobj("write with", (obj) => {
        if (!obj) return GETOBJ_SUGGEST; // bare hands (finger)
        if (obj.oclass === WEAPON_CLASS || obj.oclass === WAND_CLASS
            || obj.oclass === GEM_CLASS || obj.oclass === RING_CLASS)
            return GETOBJ_SUGGEST;
        if (obj.oclass === TOOL_CLASS
            && (obj.otyp === TOWEL || obj.otyp === MAGIC_MARKER))
            return GETOBJ_SUGGEST;
        return GETOBJ_DOWNPLAY;
    }, GETOBJ_PROMPT);
 
    if (otmp === null) return ECMD_CANCEL;
 
    let writing = "";
    if (otmp._hands) {
        // C ref: engrave.c:1185 — You("%s the %s with your %s.", everb, eloc, body_part(FINGERTIP))
        const everb0 = 'write in';
        const eloc0 = is_ice(g.u.ux, g.u.uy) ? 'frost' : 'dust';
        await You("%s the %s with your %s.", everb0, eloc0, body_part(FINGERTIP));
        etype = DUST;
    } else {
        const otyp = otmp.otyp;
        if (otyp === WAN_FIRE || otyp === SPE_FIREBALL || otyp === SCR_FIRE) {
            etype = BURN;
        } else if (otyp === WAN_LIGHTNING) {
            etype = BURN;
        } else if (otyp === WAN_DIGGING || otyp === SPE_DIG) {
            etype = ENGRAVE;
        } else if (otmp.oclass === WAND_CLASS) {
            await pline("You write in the %s with %s.",
                is_ice(g.u.ux, g.u.uy) ? "frost" : "dust",
                doname(otmp));
            etype = DUST;
        } else if (otmp.oclass === RING_CLASS || otmp.oclass === GEM_CLASS) {
            if (game.objects[otyp].oc_material === 8 /* GEMSTONE */ ||
                game.objects[otyp].oc_material === 7 /* MINERAL */) {
                etype = ENGRAVE;
            } else {
                etype = DUST;
            }
        } else if (otmp.oclass === WEAPON_CLASS || otyp === PICK_AXE || otyp === DWARVISH_MATTOCK) {
            if (is_blade(otmp)) {
                etype = ENGRAVE;
            } else {
                etype = DUST;
            }
        }
    }
 
    // C: ynFunction check if already engraved
    const ep = engr_at(g.u.ux, g.u.uy);
    if (ep && ep.text) {
        const res = await ynFunction("Do you want to add to the current engraving?", "ynq", 'y');
        if (res === 'q') return ECMD_CANCEL;
        if (res === 'n') {
            const res2 = await ynFunction("Do you want to wipe it out first?", "ynq", 'y');
            if (res2 === 'q') return ECMD_CANCEL;
            if (res2 === 'y') {
                wipe_engr_at(g.u.ux, g.u.uy, 100, false);
            }
        }
    }
 
    // C ref: engrave.c:1189 — prompt uses everb and eloc
    const everb = etype === ENGRAVE ? 'engrave' : etype === BURN ? 'burn into' : 'write in';
    const eloc = is_ice(g.u.ux, g.u.uy) ? 'frost' : 'dust';
    const buf = await getlin(`What do you want to ${everb} the ${eloc} here?`, "", { logHookedLifecycle: true });
    if (!buf || buf === "\x1b") return ECMD_CANCEL;
    writing = buf.replace(/\s+/g, ' ').trim(); // C: mungspaces
 
    // C ref: engrave.c:1222-1230 — scramble characters for dust/blood
    // writing with finger (DUST) or in blood has 1/25 chance per char
    // of being replaced with a random printable character.
    if (writing) {
        let chars = writing.split('');
        for (let i = 0; i < chars.length; i++) {
            if (chars[i] === ' ') continue;
            if (((etype === DUST || etype === ENGR_BLOOD) && !rn2(25))
                || (Blind() && !rn2(11)) || (Confusion() && !rn2(7))
                || (Stunned() && !rn2(4)) || (Hallucination() && !rn2(2))) {
                chars[i] = String.fromCharCode(33 + rnd(94)); // '!' thru '~'
            }
        }
        writing = chars.join('');
    }
 
    // occupation setup
    g.context.engrave = {
        writing,
        etype,
        otmp: otmp._hands ? null : otmp
    };
 
    set_occupation(engrave, "engraving", 0);
    // C ref: engrave.c doengrave() sets occupation then returns non-time
    // (the occupation callback consumes the action on subsequent moveloop ticks).
    return ECMD_OK;
}
 
// ============================================================
// engrave — occupation callback — C ref: engrave.c:1270-1380
// ============================================================
async function engrave() {
    const g = game;
    const ctx = g.context.engrave;
    if (!ctx) return 0;
 
    const { writing, etype, otmp } = ctx;
    const x = g.u.ux;
    const y = g.u.uy;
 
    // Simplified: finish in one turn for now.
    // C handles multi-turn based on writing length and tool.
    make_engr_at(x, y, writing, writing, g.moves, etype);
 
    if (otmp && otmp.oclass === WAND_CLASS) {
        // C: check for wand charges/breakage
    }
 
    g.context.engrave = null;
    return 0;
}