All files / js minion.js

32.51% Statements 213/655
44.44% Branches 8/18
19.04% Functions 4/21
32.51% Lines 213/655

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 65673x 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 105115x 105115x 105115x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1x 1x 2x 2x 2x   1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 97x 97x 97x 97x 97x 97x 97x 97x 97x 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 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  
// minion.js — Minion summoning (port of minion.c)
// C ref: minion.c — 568 lines
 
import { game } from './gstate.js';
import { reset_faint, Hear_again } from './eat.js';
import { rn2, rnd, rn1, c_d } from './rng.js';
import { ROLL_FROM, sgn, s_suffix } from './hacklib.js';
import { You_feel, pline, pline_The, verbalize } from './pline.js';
import { canseemon, canspotmon, newsym, SET_BOTL, setBotl } from './display.js';
import { makemon } from './makemon.js';
import { mkclass } from './makemon.js';
import { enexto } from './makemon.js';
import { mongets } from './makemon.js';
import { mons, PM_AIR_ELEMENTAL, PM_FIRE_ELEMENTAL, PM_EARTH_ELEMENTAL, PM_WATER_ELEMENTAL,
         PM_ANGEL, PM_ARCHON, PM_WIZARD_OF_YENDOR, PM_BONE_DEVIL, PM_SKELETON,
         PM_SHOPKEEPER, PM_GUARD, PM_ALIGNED_CLERIC, PM_HIGH_CLERIC,
         PM_JUIBLEX, PM_YEENOGHU, PM_ORCUS, PM_DEMOGORGON,
         S_ANGEL, S_DEMON, G_UNIQ } from './monsters.js';
import { monsndx, is_demon, is_lord, is_prince, is_minion } from './mondata.js';
import { msummon_environ } from './mondata.js';
import { mon_has_amulet } from './wizard.js';
import { Amonnam, DEADMONSTER, Monnam, mon_nam, mongone, x_monnam } from './mon.js';
import { stop_occupation } from './mon.js';
import { set_malign } from './dog.js';
import { money_cnt, livelog_printf } from './hack.js';
import { unmul } from './hack.js';
import { nomul } from './cmd.js';
import { currency } from './invent.js';
import { rloc, tele_restrict } from './teleport.js';
import { getlin } from './input.js';
import { mksobj, bless } from './mkobj.js';
import { mpickobj } from './steal.js';
import { which_armor, m_dowear } from './worn.js';
import { mk_roamer, mon_aligntyp } from './priest.js';
import { ACURR } from './attrib.js';
import { SetVoice } from './sounds.js';
import { align_gname } from './pray.js';
import { show_transient_light, transient_light_cleanup } from './light.js';
import { money2mon } from './shk.js';
import { Blind, Conflict, Deaf, In_endgame, Inhell, is_fainted } from './macros.js';
import {
    NON_PM, A_NONE, A_LAWFUL, A_NEUTRAL, A_CHAOTIC,
    MM_EMIN, MM_NOMSG, G_GONE,
    STRAT_APPEARMSG, RLOC_MSG,
    ARTICLE_A, EXACT_NAME,
    A_CHA, LL_UMONST, W_ARMS,
    EMIN, EPRI,
} from './const.js';
import { ART_EXCALIBUR, ART_DEMONBANE } from './artifacts.js';
import { u_wield_art } from './artifact.js';
import { AMULET_OF_REFLECTION, SHIELD_OF_REFLECTION, SILVER_SABER } from './objects.js';
import { WEAPON_CLASS } from './objects.js';
 
// ── Local stubs for unported dependencies ──
 
// C: u_wield_art(artid) — checks if hero wields a specific artifact
// u_wield_art: imported from artifact.js
function is_dprince(ptr) {
    return is_demon(ptr) && is_prince(ptr);
}
 
// C: #define is_dlord(ptr) (is_demon(ptr) && is_lord(ptr))
function is_dlord(ptr) {
    return is_demon(ptr) && is_lord(ptr);
}
 
// C: #define is_ndemon(ptr) (is_demon(ptr) && ((ptr->mflags2 & (M2_LORD|M2_PRINCE)) == 0))
function is_ndemon(ptr) {
    return is_demon(ptr) && !is_lord(ptr) && !is_prince(ptr);
}
 
// C: #define is_lminion(mon) (is_minion((mon)->data) && mon_aligntyp(mon) == A_LAWFUL)
export function is_lminion(mon) {
    if (!mon) return false;
    return is_minion(mon.data) && mon_aligntyp(mon) === A_LAWFUL;
}
 
// C ref: priest.c:280 mon_aligntyp()
 
// Inhell: imported from macros.js
 
// mon_has_amulet: imported from wizard.js
 
// is_fainted: imported from macros.js
 
// reset_faint: imported from eat.js
 
 
// C: mongone(mtmp) — remove monster from game entirely
// C: select_hwep(mtmp) — select best handheld weapon
function select_hwep(_mtmp) {
    for (let otmp = _mtmp?.minvent; otmp; otmp = otmp.nobj) {
        if (otmp.oclass === WEAPON_CLASS)
            return otmp;
    }
    return null;
}
 
// SET_BOTL imported from display.js
 
// ── Static data ──
 
// C: elementals[4] — the four basic elementals
const elementals = [
    PM_AIR_ELEMENTAL, PM_FIRE_ELEMENTAL,
    PM_EARTH_ELEMENTAL, PM_WATER_ELEMENTAL
];
 
// ── newemin: allocate emin structure ──
// C ref: minion.c:17
export function newemin(mtmp) {
    if (!mtmp.mextra) mtmp.mextra = {};
    if (!mtmp.mextra.emin) {
        mtmp.mextra.emin = {
            min_align: 0,
            renegade: false,
            parentmid: mtmp.m_id,
        };
    }
}
 
// ── free_emin: deallocate emin structure ──
// C ref: minion.c:28
export function free_emin(mtmp) {
    if (mtmp.mextra && mtmp.mextra.emin) {
        mtmp.mextra.emin = null;
    }
    mtmp.isminion = 0;
}
 
// ── monster_census: count monsters on level ──
// C ref: minion.c:39
export function monster_census(spotted) {
    let count = 0;
    for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
        if (DEADMONSTER(mtmp))
            continue;
        if (mtmp.isgd && mtmp.mx === 0)
            continue;
        if (spotted && !canspotmon(mtmp))
            continue;
        ++count;
    }
    return count;
}
 
// ── msummon: monster summons allies ──
// C ref: minion.c:59
export async function msummon(mon) {
    let ptr;
    let dtype = NON_PM, cnt = 0, result = 0, census;
    let xlight;
    let atyp;
    let mtmp;

    if (mon) {
        ptr = mon.data;

        if (u_wield_art(ART_DEMONBANE) && is_demon(ptr)) {
            if (canseemon(mon))
                await pline(`${Monnam(mon)} looks puzzled for a moment.`);
            return 0;
        }

        atyp = mon.ispriest ? EPRI(mon).shralign
             : mon.isminion ? EMIN(mon).min_align
               : (ptr.maligntyp === A_NONE) ? A_NONE
                 : sgn(ptr.maligntyp);
    } else {
        ptr = mons[PM_WIZARD_OF_YENDOR];
        atyp = (ptr.maligntyp === A_NONE) ? A_NONE : sgn(ptr.maligntyp);
    }

    if (is_dprince(ptr) || (ptr === mons[PM_WIZARD_OF_YENDOR])) {
        // C: (!rn2(20)) ? dprince(atyp) : (!rn2(4)) ? dlord(atyp) : ndemon(atyp)
        // Hoist RNG calls: C evaluates left-to-right with short-circuit
        const r1 = rn2(20);
        if (!r1) {
            dtype = dprince(atyp);
        } else {
            const r2 = rn2(4);
            if (!r2) {
                dtype = dlord(atyp);
            } else {
                dtype = ndemon(atyp);
            }
        }
        cnt = ((dtype !== NON_PM)
               && !rn2(4) && is_ndemon(mons[dtype])) ? 2 : 1;
    } else if (is_dlord(ptr)) {
        const r1 = rn2(50);
        if (!r1) {
            dtype = dprince(atyp);
        } else {
            const r2 = rn2(20);
            if (!r2) {
                dtype = dlord(atyp);
            } else {
                dtype = ndemon(atyp);
            }
        }
        cnt = ((dtype !== NON_PM)
               && !rn2(4) && is_ndemon(mons[dtype])) ? 2 : 1;
    } else if (ptr === mons[PM_BONE_DEVIL]) {
        dtype = PM_SKELETON;
        cnt = 1;
    } else if (is_ndemon(ptr)) {
        const r1 = rn2(20);
        if (!r1) {
            dtype = dlord(atyp);
        } else {
            const r2 = rn2(6);
            if (!r2) {
                dtype = ndemon(atyp);
            } else {
                dtype = monsndx(ptr);
            }
        }
        cnt = 1;
    } else if (is_lminion(mon)) {
        if (is_lord(ptr) && !rn2(20)) {
            dtype = llord();
        } else if (is_lord(ptr) || !rn2(6)) {
            dtype = lminion();
        } else {
            dtype = monsndx(ptr);
        }
        cnt = ((dtype !== NON_PM)
               && !rn2(4) && !is_lord(mons[dtype])) ? 2 : 1;
    } else if (ptr === mons[PM_ANGEL]) {
        /* non-lawful angels can also summon */
        if (!rn2(6)) {
            switch (atyp) { /* see summon_minion */
            case A_NEUTRAL:
                dtype = ROLL_FROM(elementals);
                break;
            case A_CHAOTIC:
            case A_NONE:
                dtype = ndemon(atyp);
                break;
            }
        } else {
            dtype = PM_ANGEL;
        }
        cnt = ((dtype !== NON_PM)
               && !rn2(4) && !is_lord(mons[dtype])) ? 2 : 1;
    }

    if (dtype === NON_PM)
        return 0;

    /* sanity checks */
    if (cnt > 1 && (mons[dtype].geno & G_UNIQ) !== 0)
        cnt = 1;
    /*
     * If this daemon is unique and being re-summoned (the only way we
     * could get this far with an extinct dtype), try another.
     */
    if (((game.mvitals?.[dtype]?.mvflags ?? 0) & G_GONE) !== 0) {
        dtype = ndemon(atyp);
        if (dtype === NON_PM)
            return 0;
    }

    /* some candidates can generate a group of monsters, so simple
       count of non-null makemon() result is not sufficient */
    census = monster_census(false);
    xlight = false;

    while (cnt > 0) {
        mtmp = await makemon(mons[dtype], game.u.ux, game.u.uy, MM_EMIN | MM_NOMSG);
        if (mtmp) {
            result++;
            /* an angel's alignment should match the summoner */
            if (dtype === PM_ANGEL) {
                mtmp.isminion = 1;
                newemin(mtmp);
                EMIN(mtmp).min_align = atyp;
                /* renegade if same alignment but not peaceful
                   or peaceful but different alignment */
                EMIN(mtmp).renegade =
                    (atyp !== game.u.ualign.type) ^ !mtmp.mpeaceful;
            }

            if (mtmp.data.mlet === S_ANGEL && !Blind()) {
                /* for any 'A', 'cloud of smoke' will be 'flash of light' */
                show_transient_light(null, mtmp.mx, mtmp.my);
                xlight = true;
            }

            if (cnt === 1 && canseemon(mtmp)) {
                const cloud_ref = {};
                const what = msummon_environ(mtmp.data, cloud_ref);
                const cloud = cloud_ref.val || 'cloud';

                await pline(`${Amonnam(mtmp)} appears in a ${cloud} of ${what}!`);
            }
        }
        cnt--;
    }

    if (xlight) {
        transient_light_cleanup();
    }

    /* how many monsters exist now compared to before? */
    if (result)
        result = monster_census(false) - census;

    return result;
}
 
// ── summon_minion: summon a minion for prayer/altar ──
// C ref: minion.c:198
export async function summon_minion(alignment, talk) {
    let mon;
    let mnum;

    switch (alignment) {
    case A_LAWFUL:
        mnum = lminion();
        break;
    case A_NEUTRAL:
        mnum = ROLL_FROM(elementals);
        break;
    case A_CHAOTIC:
    case A_NONE:
        mnum = ndemon(alignment);
        break;
    default:
        // impossible("unaligned player?");
        mnum = ndemon(A_NONE);
        break;
    }
    if (mnum === NON_PM) {
        mon = null;
    } else if (mnum === PM_ANGEL) {
        mon = await makemon(mons[mnum], game.u.ux, game.u.uy, MM_EMIN | MM_NOMSG);
        if (mon) {
            mon.isminion = 1;
            newemin(mon);
            EMIN(mon).min_align = alignment;
            EMIN(mon).renegade = false;
        }
    } else if (mnum !== PM_SHOPKEEPER && mnum !== PM_GUARD
               && mnum !== PM_ALIGNED_CLERIC && mnum !== PM_HIGH_CLERIC) {
        mon = await makemon(mons[mnum], game.u.ux, game.u.uy, MM_EMIN | MM_NOMSG);
        if (mon) {
            mon.isminion = 1;
            newemin(mon);
            EMIN(mon).min_align = alignment;
            EMIN(mon).renegade = false;
        }
    } else {
        mon = await makemon(mons[mnum], game.u.ux, game.u.uy, MM_NOMSG);
    }
    if (mon) {
        if (talk) {
            if (!Deaf())
                await pline_The(`voice of ${align_gname(alignment)} booms:`);
            else
                await You_feel(`${s_suffix(align_gname(alignment))} booming voice:`);
            SetVoice(mon, 0, 80, 0);
            await verbalize("Thou shalt pay for thine indiscretion!");
            if (canspotmon(mon))
                await pline(`${Amonnam(mon)} appears before you.`);
            mon.mstrategy &= ~STRAT_APPEARMSG;
        }
        mon.mpeaceful = false;
        /* don't call set_malign(); player was naughty */
    }
}
 
// ── Athome macro ──
// C: #define Athome (Inhell && (mtmp->cham == NON_PM))
function Athome(mtmp) {
    return Inhell() && (mtmp.cham === NON_PM);
}
 
// ── demon_talk: demon offers a bribe ──
// C ref: minion.c:263
// returns 1 if it won't attack.
export async function demon_talk(mtmp) {
    let cash, demand, offer;

    if (u_wield_art(ART_EXCALIBUR) || u_wield_art(ART_DEMONBANE)) {
        if (canspotmon(mtmp))
            await pline(`${Amonnam(mtmp)} looks very angry.`);
        else
            await You_feel("tension building.");
        mtmp.mpeaceful = mtmp.mtame = 0;
        set_malign(mtmp);
        newsym(mtmp.mx, mtmp.my);
        return 0;
    }

    if (is_fainted()) {
        reset_faint(); /* if fainted - wake up */
    } else {
        await stop_occupation();
        if (game.multi > 0) {
            nomul(0);
            await unmul(null);
        }
    }

    /* Slight advantage given. */
    if (is_dprince(mtmp.data) && mtmp.minvis) {
        const wasunseen = !canspotmon(mtmp);

        mtmp.minvis = mtmp.perminvis = 0;
        if (wasunseen && canspotmon(mtmp)) {
            await pline(`${Amonnam(mtmp)} appears before you.`);
            mtmp.mstrategy &= ~STRAT_APPEARMSG;
        }
        newsym(mtmp.mx, mtmp.my);
    }
    if (game.youmonst?.data?.mlet === S_DEMON) { /* Won't blackmail their own. */
        if (!Deaf())
            await pline(`${Amonnam(mtmp)} says, "Good hunting, ${game.flags.female ? 'Sister' : 'Brother'}."`);
        else if (canseemon(mtmp))
            await pline(`${Amonnam(mtmp)} says something.`);
        if (!tele_restrict(mtmp))
            await rloc(mtmp, RLOC_MSG);
        return 1;
    }
    cash = money_cnt(game.invent);
    demand = Math.trunc((cash * (rnd(80) + 20 * (Athome(mtmp) ? 1 : 0)))
           / (100 * (1 + (sgn(game.u.ualign.type) === sgn(mtmp.data.maligntyp) ? 1 : 0))));

    if (!demand || game.multi < 0) { /* you have no gold or can't move */
        mtmp.mpeaceful = 0;
        set_malign(mtmp);
        return 0;
    } else {
        /* make sure that the demand is unmeetable if the monster
           has the Amulet, preventing monster from being satisfied
           and removed from the game (along with said Amulet...) */
        if (mon_has_amulet(mtmp) || Deaf())
            /* 125: 5*25 in case hero has maximum possible charisma */
            demand = cash + rn1(1000, 125);

        if (!Deaf())
            await pline(`${Amonnam(mtmp)} demands ${demand} ${currency(demand)} for safe passage.`);
        else if (canseemon(mtmp))
            await pline(`${Amonnam(mtmp)} seems to be demanding something.`);
        offer = 0;
        if (!Deaf()
            && ((offer = await bribe(mtmp, "How much will you offer?")) >= demand)) {
            await pline(`${Amonnam(mtmp)} vanishes, laughing about cowardly mortals.`);
        } else if (offer > 0
                   && rnd(5 * ACURR(A_CHA)) > (demand - offer)) {
            await pline(`${Amonnam(mtmp)} scowls at you menacingly, then vanishes.`);
        } else {
            await pline(`${Amonnam(mtmp)} gets angry...`);
            mtmp.mpeaceful = 0;
            set_malign(mtmp);
            return 0;
        }
    }
    livelog_printf(LL_UMONST, "bribed %s with %d %s for safe passage",
                   x_monnam(mtmp, ARTICLE_A, null, EXACT_NAME, false),
                   offer, (offer === 1) ? "zorkmid" : "zorkmids");
    mongone(mtmp);
    return 1;
}
 
// ── bribe: ask player how much to offer ──
// C ref: minion.c:361
export async function bribe(mtmp, prompt) {
    let offer;
    const umoney = money_cnt(game.invent);

    const buf = await getlin(prompt);
    offer = parseInt(buf, 10);
    if (isNaN(offer)) offer = 0;

    if (offer < 0) {
        await pline(`You try to shortchange ${mon_nam(mtmp)}, but fumble.`);
        return 0;
    } else if (offer === 0) {
        await pline("You refuse.");
        return 0;
    } else if (offer >= umoney) {
        await pline(`You give ${mon_nam(mtmp)} all your gold.`);
        offer = umoney;
    } else {
        await pline(`You give ${mon_nam(mtmp)} ${offer} ${currency(offer)}.`);
    }
    await money2mon(mtmp, offer);
    setBotl('bribe');
    return offer;
}
 
// ── dprince: pick a demon prince for alignment ──
// C ref: minion.c:391
export function dprince(atyp) {
    let tryct, pm;

    for (tryct = !In_endgame(game.u.uz) ? 20 : 0; tryct > 0; --tryct) {
        pm = rn1(PM_DEMOGORGON + 1 - PM_ORCUS, PM_ORCUS);
        if (!((game.mvitals?.[pm]?.mvflags ?? 0) & G_GONE)
            && (atyp === A_NONE || sgn(mons[pm].maligntyp) === sgn(atyp)))
            return pm;
    }
    return dlord(atyp); /* approximate */
}
 
// ── dlord: pick a demon lord for alignment ──
// C ref: minion.c:405
export function dlord(atyp) {
    let tryct, pm;

    for (tryct = !In_endgame(game.u.uz) ? 20 : 0; tryct > 0; --tryct) {
        pm = rn1(PM_YEENOGHU + 1 - PM_JUIBLEX, PM_JUIBLEX);
        if (!((game.mvitals?.[pm]?.mvflags ?? 0) & G_GONE)
            && (atyp === A_NONE || sgn(mons[pm].maligntyp) === sgn(atyp)))
            return pm;
    }
    return ndemon(atyp); /* approximate */
}
 
// ── llord: create lawful (good) lord ──
// C ref: minion.c:419
export function llord() {
    if (!((game.mvitals?.[PM_ARCHON]?.mvflags ?? 0) & G_GONE))
        return PM_ARCHON;

    return lminion(); /* approximate */
}
 
// ── lminion: pick a lawful minion ──
// C ref: minion.c:429
export function lminion() {
    let tryct;
    let ptr;

    for (tryct = 0; tryct < 20; tryct++) {
        ptr = mkclass(S_ANGEL, 0);
        if (ptr && !is_lord(ptr))
            return monsndx(ptr);
    }

    return NON_PM;
}
 
// ── ndemon: pick a random demon ──
// C ref: minion.c:443
export function ndemon(atyp) {
    let ptr;

    ptr = mkclass_aligned(S_DEMON, 0, atyp);
    return (ptr && is_ndemon(ptr)) ? monsndx(ptr) : NON_PM;
}
 
// ── mkclass_aligned: local import wrapper ──
// mkclass_aligned is not exported from makemon.js, so we import mkclass
// and implement alignment filtering here matching C ref: makemon.c:1877
function mkclass_aligned(monclass, spc, atyp) {
    // C: mkclass_aligned calls mkclass with alignment filtering
    // For A_NONE, any alignment is acceptable (same as mkclass)
    if (atyp === A_NONE) return mkclass(monclass, spc);

    // Try up to 20 times to get an aligned monster
    for (let tryct = 0; tryct < 20; tryct++) {
        const ptr = mkclass(monclass, spc);
        if (ptr && sgn(ptr.maligntyp) === sgn(atyp))
            return ptr;
    }
    return null;
}
 
// ── lose_guardian_angel: guardian angel abandons hero ──
// C ref: minion.c:467
// TODO: needs enexto, mk_roamer — stub for now
export async function lose_guardian_angel(mon) {
    // TODO: full port when enexto/mk_roamer are available
    if (mon) {
        if (canspotmon(mon)) {
            if (!Deaf()) {
                await pline(`${Monnam(mon)} rebukes you, saying:`);
                SetVoice(mon, 0, 80, 0);
                await verbalize("Since you desire conflict, have some more!");
            } else {
                await pline(`${Monnam(mon)} vanishes!`);
            }
        }
        mongone(mon);
    }
    /* create 2 to 4 hostile angels to replace the lost guardian */
    // TODO: for (i = rn1(3, 2); i > 0; --i) enexto + mk_roamer
    const _cnt = rn1(3, 2); // consume RNG to match C
}
 
// ── gain_guardian_angel: receive tame angel on Astral Plane ──
// C ref: minion.c:497
export async function gain_guardian_angel() {
    const u = game.u;
    const mm = { x: u.ux, y: u.uy };
 
    await Hear_again(); // C: attempt to cure deafness before divine messages
 
    if (Conflict()) {
        if (!Deaf())
            await pline('A voice booms:');
        else
            await You_feel('a booming voice:');
        SetVoice(null, 0, 80, 0);
        await verbalize('Thy desire for conflict shall be fulfilled!');
        await lose_guardian_angel(null);
    } else if ((u.ualign?.record ?? 0) > 8) {
        if (!Deaf())
            await pline('A voice whispers:');
        else
            await You_feel('a soft voice:');
        SetVoice(null, 0, 80, 0);
        await verbalize('Thou hast been worthy of me!');
        if (enexto(mm, mm.x, mm.y, mons[PM_ANGEL])) {
            const mtmp = await mk_roamer(mons[PM_ANGEL], u.ualign?.type ?? A_NONE,
                mm.x, mm.y, true);
            if (mtmp) {
                let otmp;
                mtmp.mstrategy &= ~STRAT_APPEARMSG;
                if (u.uconduct?.pets) {
                    mtmp.mtame = 10;
                    u.uconduct.pets++;
                }
                newsym(mtmp.mx, mtmp.my);
                if (!Blind())
                    await pline('An angel appears near you.');
                else
                    await You_feel('the presence of a friendly angel near you.');
                mtmp.m_lev = rn1(8, 15);
                mtmp.mhp = mtmp.mhpmax = c_d(mtmp.m_lev, 10) + 30 + rnd(30);
                otmp = select_hwep(mtmp);
                if (!otmp) {
                    otmp = mksobj(SILVER_SABER, false, false);
                    if (await mpickobj(mtmp, otmp)) {
                        throw new Error('merged weapon in gain_guardian_angel');
                    }
                }
                bless(otmp);
                if ((otmp.spe ?? 0) < 4)
                    otmp.spe = (otmp.spe ?? 0) + rnd(4);
                otmp = which_armor(mtmp, W_ARMS);
                if (!otmp || otmp.otyp !== SHIELD_OF_REFLECTION) {
                    await mongets(mtmp, AMULET_OF_REFLECTION);
                    await m_dowear(mtmp, true);
                }
            }
        }
    }
}
 
/*minion.c*/