All files / js mcastu.js

80.55% Statements 348/432
72.48% Branches 108/149
72.72% Functions 8/11
80.55% Lines 348/432

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 43373x 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 386x 386x 386x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x                           73x 73x 73x 160x 160x 160x 160x 160x 160x 160x 160x 160x     160x 160x 160x 160x 3x 160x 160x 1x 160x 160x 160x 2x 160x 160x 1x 160x 160x 160x 1x 160x 160x 1x 160x 160x 22x 160x 160x 42x 160x 29x 160x 23x 160x 17x 160x 160x 18x 160x 160x 73x 73x 73x 301x 301x 301x 301x 301x 301x 301x 301x 301x 32x 32x 301x 301x 36x 301x 24x 301x 18x 301x 301x 32x 301x 17x 301x 301x 34x 301x 301x 43x 301x 301x 34x 301x 23x 301x 301x 22x 301x 301x 73x 73x 73x 469x 469x 469x 73x 73x 73x 127x 127x 127x 127x 127x 127x 127x 107x 107x 127x       127x       127x 23x 23x 10x 127x 42x 42x 42x 42x   127x 40x 40x   127x       127x 12x 127x 73x 73x 73x 73x 73x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x 301x 301x 461x 461x 460x 460x 449x 449x 11x 11x 461x 461x 461x 461x 461x 12x 12x 461x 461x       12x 461x 12x 12x 12x 12x 13x 461x       12x 12x 461x 3x 3x     3x 3x 461x 4x 4x 9x 9x 9x 10x 8x 438x   1x 1x 1x 9x 438x 9x 9x 9x 9x 438x             461x             461x             461x 461x 9x 9x 8x 8x 461x 8x 436x 8x 461x 73x 73x 73x 73x 73x 9x 9x 9x                 9x 7x 7x 7x 9x 1x 1x 1x 1x               9x 1x 1x 1x 1x 9x 8x 8x 9x 73x 73x 73x 73x 386x 386x   386x       386x                         386x  
// mcastu.js — Monster spellcasting (port of mcastu.c)
// Key RNG consumers:
//   choose_magic_spell() — rn2(m_lev), while loop with rn2(25)/rn2(spellval)
//   choose_clerical_spell() — rn2(m_lev), while loop with rn2(16)/rn2(spellnum)
//   castmu() — rn2(ml*10) fumble check, d() damage
//   cursetxt() — rn2(4) for hearing check
//
// Spell EFFECTS are mostly stubbed (no core RNG). The spell selection
// and fumble checks are the critical path for RNG parity.
 
import { game } from './gstate.js';
import { rn2, rnd, c_d, pushRngLogEntry } from './rng.js';
import { pline, pline_The, You, You_feel, Your } from './pline.js';
import {
    Antimagic, Hallucination, Deaf, Blind, Blind_telepat,
    See_invisible, Invis, Stunned, Confusion, Half_spell_damage,
    Fire_resistance, Cold_resistance, Shock_resistance,
} from './macros.js';
import { nomul } from './cmd.js';
import { cansee, couldsee } from './vision.js';
import { mdamageu } from './mhitu.js';
import { shieldeff, canspotmon, canseemon } from './display.js';
import { cvt_adtyp_to_mseenres, m_seenres, perceives } from './mondata.js';
import { lined_up } from './mthrowu.js';
import { buzz, flash_str } from './zap.js';
import { mon_adjust_speed } from './worn.js';
import { AD_FIRE, AD_COLD, AD_MAGM, AD_CLRC, AD_SPEL, AD_SPC2 } from './monsters.js';
import { Monnam, healmon, mon_nam } from './mon.js';
import { BZ_OFS_AD, M_SEEN_MAGR } from './const.js';
import { destroy_arm } from './do_wear.js';
import { monstseesu, monstunseesu } from './mondata.js';
 
// ── Spell constants from mcastu.h ──
const MCAST_PSI_BOLT = 0;
const MCAST_OPEN_WOUNDS = 1;
const MCAST_LIGHTNING = 2;
const MCAST_FIRE_PILLAR = 3;
const MCAST_GEYSER = 4;
const MCAST_DEATH_TOUCH = 5;
const MCAST_CURE_SELF = 6;
const MCAST_HASTE_SELF = 7;
const MCAST_DISAPPEAR = 8;
const MCAST_AGGRAVATION = 9;
const MCAST_STUN_YOU = 10;
const MCAST_WEAKEN_YOU = 11;
const MCAST_CONFUSE_YOU = 12;
const MCAST_PARALYZE = 13;
const MCAST_BLIND_YOU = 14;
const MCAST_DESTRY_ARMR = 15;
const MCAST_CURSE_ITEMS = 16;
const MCAST_INSECTS = 17;
const MCAST_SUMMON_MONS = 18;
const MCAST_CLONE_WIZ = 19;
 
// Spell flags
const MCF_NONE = 0x0000;
const MCF_INDIRECT = 0x0001;
const MCF_SIGHT = 0x0002;
const MCF_HOSTILE = 0x0004;
 
// mcast_flags[] — indexed by spell number
const mcast_flags = [
    MCF_HOSTILE | MCF_SIGHT,                        // PSI_BOLT
    MCF_HOSTILE | MCF_SIGHT,                        // OPEN_WOUNDS
    MCF_HOSTILE | MCF_SIGHT,                        // LIGHTNING
    MCF_HOSTILE | MCF_SIGHT,                        // FIRE_PILLAR
    MCF_HOSTILE | MCF_SIGHT,                        // GEYSER
    MCF_HOSTILE | MCF_SIGHT,                        // DEATH_TOUCH
    MCF_INDIRECT,                                    // CURE_SELF
    MCF_INDIRECT,                                    // HASTE_SELF
    MCF_INDIRECT,                                    // DISAPPEAR
    MCF_INDIRECT | MCF_HOSTILE | MCF_SIGHT,          // AGGRAVATION
    MCF_HOSTILE | MCF_SIGHT,                        // STUN_YOU
    MCF_HOSTILE | MCF_SIGHT,                        // WEAKEN_YOU
    MCF_HOSTILE | MCF_SIGHT,                        // CONFUSE_YOU
    MCF_HOSTILE | MCF_SIGHT,                        // PARALYZE
    MCF_HOSTILE | MCF_SIGHT,                        // BLIND_YOU
    MCF_HOSTILE | MCF_SIGHT,                        // DESTRY_ARMR
    MCF_HOSTILE | MCF_SIGHT,                        // CURSE_ITEMS
    MCF_HOSTILE | MCF_INDIRECT | MCF_SIGHT,          // INSECTS
    MCF_HOSTILE | MCF_INDIRECT | MCF_SIGHT,          // SUMMON_MONS
    MCF_HOSTILE | MCF_INDIRECT | MCF_SIGHT,          // CLONE_WIZ
];
 
// Return values
const M_ATTK_MISS = 0;
const M_ATTK_HIT = 1;
 
function BZ_VALID_ADTYP(adtyp) {
    return adtyp >= AD_MAGM && adtyp <= AD_SPC2;
}
function BZ_M_SPELL(bztyp) { return -10 - bztyp; } // C: (-10 - (x))
 
// ── Helper stubs (no RNG) ──
// canseemon imported from display.js
// canspotmon imported from display.js
// Monnam: imported from mon.js
// mon_nam: imported from mon.js
function has_aggravatables(_mtmp) { return true; } // simplified
// healmon: imported from mon.js
// mdamageu imported from mhitu.js
// shieldeff imported from display.js
// C ref: mcastu.c:45
async function cursetxt(mtmp, undirected) {
    if (canseemon(mtmp) && couldsee(mtmp.mx, mtmp.my)) {
        let point_msg;
        if (undirected)
            point_msg = 'all around, then curses';
        else
            point_msg = 'at you, then curses';
        await pline(`${Monnam(mtmp)} points ${point_msg}.`);
    } else if (!((game.moves || 0) % 4) || !rn2(4)) {
        if (!Deaf())
            await pline('You hear a mumbled curse.');
    }
}
 
// ── choose_magic_spell — RNG: rn2(m_lev) + while loop ──
// C ref: mcastu.c:73
function choose_magic_spell(mtmp) {
    let spellval = rn2(mtmp.m_lev || 1);
 
    while (spellval > 24 && rn2(25))
        spellval = rn2(spellval);
 
    switch (spellval) {
    case 24:
    case 23:
        if (Antimagic() || Hallucination())
            return MCAST_PSI_BOLT;
        // FALLTHROUGH
    case 22:
    case 21:
    case 20:
        return MCAST_DEATH_TOUCH;
    case 19:
    case 18:
        return MCAST_CLONE_WIZ;
    case 17:
    case 16:
    case 15:
        return MCAST_SUMMON_MONS;
    case 14:
    case 13:
        return MCAST_AGGRAVATION;
    case 12:
    case 11:
    case 10:
        return MCAST_CURSE_ITEMS;
    case 9:
    case 8:
        return MCAST_DESTRY_ARMR;
    case 7:
    case 6:
        return MCAST_WEAKEN_YOU;
    case 5:
    case 4:
        return MCAST_DISAPPEAR;
    case 3:
        return MCAST_STUN_YOU;
    case 2:
        return MCAST_HASTE_SELF;
    case 1:
        return MCAST_CURE_SELF;
    case 0:
    default:
        return MCAST_PSI_BOLT;
    }
}
 
// ── choose_clerical_spell — RNG: rn2(m_lev) + while loop ──
// C ref: mcastu.c:128
function choose_clerical_spell(mtmp) {
    let spellnum = rn2(mtmp.m_lev || 1);
 
    while (spellnum > 15 && rn2(16))
        spellnum = rn2(spellnum);
 
    switch (spellnum) {
    case 15:
    case 14:
        if (rn2(3))
            return MCAST_OPEN_WOUNDS;
        // FALLTHROUGH
    case 13:
        return MCAST_GEYSER;
    case 12:
        return MCAST_FIRE_PILLAR;
    case 11:
        return MCAST_LIGHTNING;
    case 10:
    case 9:
        return MCAST_CURSE_ITEMS;
    case 8:
        return MCAST_INSECTS;
    case 7:
    case 6:
        return MCAST_BLIND_YOU;
    case 5:
    case 4:
        return MCAST_PARALYZE;
    case 3:
    case 2:
        return MCAST_CONFUSE_YOU;
    case 1:
        return MCAST_CURE_SELF;
    case 0:
    default:
        return MCAST_OPEN_WOUNDS;
    }
}
 
// ── is_undirected_spell ──
// C ref: mcastu.c:950
function is_undirected_spell(spellnum) {
    return !!(mcast_flags[spellnum] & MCF_INDIRECT);
}
 
// ── spell_would_be_useless ──
// C ref: mcastu.c:959
function spell_would_be_useless(mtmp, spellnum) {
    // hostile-only spells
    if ((mcast_flags[spellnum] & MCF_HOSTILE) && mtmp.mpeaceful)
        return true;
    // sight-required spells
    if ((mcast_flags[spellnum] & MCF_SIGHT) && !couldsee(mtmp.mx, mtmp.my))
        return true;
 
    switch (spellnum) {
    case MCAST_CLONE_WIZ:
        if (!mtmp.iswiz || (game.context?.no_of_wizards ?? 0) > 1)
            return true;
        break;
    case MCAST_AGGRAVATION:
        if (!has_aggravatables(mtmp))
            return rn2(100) ? true : false;
        break;
    case MCAST_HASTE_SELF:
        if (mtmp.permspeed === 2) // MFAST
            return true;
        break;
    case MCAST_DISAPPEAR:
        if (mtmp.minvis || mtmp.invis_blkd)
            return true;
        if (mtmp.mpeaceful && !See_invisible())
            return true;
        break;
    case MCAST_CURE_SELF:
        if (mtmp.mhp === mtmp.mhpmax)
            return true;
        break;
    case MCAST_BLIND_YOU:
        if (Blind())
            return true;
        break;
    }
    return false;
}
 
// ── castmu — main monster spellcasting entry point ──
// C ref: mcastu.c:177
// RNG: choose_*_spell (1+ calls), rn2(ml*10) fumble, d() damage
export async function castmu(mtmp, mattk, thinks_it_foundyou, foundyou) {
    const ml = mtmp.m_lev || 0;
    let ret;
    let spellnum = 0;
 
    if ((mattk.adtyp === AD_SPEL || mattk.adtyp === AD_CLRC) && ml) {
        let cnt = 40;
 
        do {
            if (mattk.adtyp === AD_SPEL)
                spellnum = choose_magic_spell(mtmp);
            else
                spellnum = choose_clerical_spell(mtmp);
            // not trying to attack? don't allow directed spells
            if (!thinks_it_foundyou) {
                if (!is_undirected_spell(spellnum)
                    || spell_would_be_useless(mtmp, spellnum)) {
                    return M_ATTK_MISS;
                }
                break;
            }
        } while (--cnt > 0
                 && spell_would_be_useless(mtmp, spellnum));
        if (cnt === 0)
            return M_ATTK_MISS;
    }
 
    // monster unable to cast?
    if (mtmp.mcan || mtmp.mspec_used || !ml
        || m_seenres(mtmp, cvt_adtyp_to_mseenres(mattk.adtyp))) {
        await cursetxt(mtmp, is_undirected_spell(spellnum));
        return M_ATTK_MISS;
    }
 
    if (mattk.adtyp === AD_SPEL || mattk.adtyp === AD_CLRC) {
        mtmp.mspec_used = (mtmp.m_lev < 8) ? (10 - mtmp.m_lev) : 2;
    }
 
    // directed spell at wrong place?
    if (!foundyou && thinks_it_foundyou
        && !is_undirected_spell(spellnum)) {
        await pline(`${canseemon(mtmp) ? Monnam(mtmp) : 'Something'} casts a spell at thin air!`);
        return M_ATTK_MISS;
    }
 
    nomul(0);
    if (rn2(ml * 10) < (mtmp.mconf ? 100 : 20)) {
        // fumbled attack
        if (canseemon(mtmp) && !Deaf()) {
            await pline_The(`air crackles around ${mon_nam(mtmp)}.`);
        }
        return M_ATTK_MISS;
    }
    if (canspotmon(mtmp) || !is_undirected_spell(spellnum)) {
        await pline(`${canspotmon(mtmp) ? Monnam(mtmp) : 'Something'} casts a spell${is_undirected_spell(spellnum) ? '' : ' at you'}!`);
    }
 
    // Damage calculation
    let dmg;
    if (!foundyou) {
        dmg = 0;
    } else if (mattk.damd) {
        dmg = c_d(Math.trunc(ml / 2) + (mattk.damn || 0), mattk.damd);
    } else {
        dmg = c_d(Math.trunc(ml / 2) + 1, 6);
    }
    if (Half_spell_damage())
        dmg = Math.trunc((dmg + 1) / 2);
 
    ret = M_ATTK_HIT;
 
    switch (mattk.adtyp) {
    case AD_FIRE:
        await pline("You're enveloped in flames.");
        if (Fire_resistance()) {
            await pline('But you resist the effects.');
            dmg = 0;
        }
        break;
    case AD_COLD:
        await pline("You're covered in frost.");
        if (Cold_resistance()) {
            await pline('But you resist the effects.');
            dmg = 0;
        }
        break;
    case AD_MAGM:
        await You('are hit by a shower of missiles!');
        if (Antimagic()) {
            await pline_The('missiles bounce off!');
            dmg = 0;
        }
        break;
    case AD_SPEL:
    case AD_CLRC:
        // Spell effects — mostly messages, minimal core RNG
        await mcast_spell(mtmp, dmg, spellnum);
        dmg = 0;
        break;
    }
    if (dmg)
        await mdamageu(mtmp, dmg);
    return ret;
}
 
// ── mcast_spell — dispatch spell effects ──
// C ref: mcastu.c:862
// Most effects are messages + state changes, no core RNG.
// m_cure_self uses d(3,6) for healing.
async function mcast_spell(mtmp, dmg, spellnum) {
    switch (spellnum) {
    case MCAST_CURE_SELF:
        // d(3,6) for healing amount
        if (mtmp.mhp < mtmp.mhpmax) {
            if (canseemon(mtmp))
                await pline(`${Monnam(mtmp)} looks better.`);
            healmon(mtmp, c_d(3, 6), 0);
        }
        dmg = 0;
        break;
    case MCAST_HASTE_SELF:
        await mon_adjust_speed(mtmp, 1, null);
        dmg = 0;
        break;
    case MCAST_DESTRY_ARMR:
        if (Antimagic()) {
            shieldeff(game.u.ux, game.u.uy);
            monstseesu(M_SEEN_MAGR);
            await pline('A field of force surrounds you!');
        } else if (!await destroy_arm()) {
            await Your('skin itches.');
        } else {
            monstunseesu(M_SEEN_MAGR);
        }
        dmg = 0;
        break;
    default:
        // Most spell effects are not yet ported (messages + state changes).
        // They don't consume core RNG, so stubbing them doesn't affect parity.
        dmg = 0;
        break;
    }
    if (dmg)
        await mdamageu(mtmp, dmg);
}
 
// ── buzzmu — ranged spell (beam) ──
// C ref: mcastu.c:1031
export async function buzzmu(mtmp, mattk) {
    if (!BZ_VALID_ADTYP(mattk.adtyp))
        return M_ATTK_MISS;

    if (mtmp.mcan || m_seenres(mtmp, cvt_adtyp_to_mseenres(mattk.adtyp))) {
        await cursetxt(mtmp, false);
        return M_ATTK_MISS;
    }
    if (lined_up(mtmp) && rn2(3)) {
        nomul(0);
        if (canseemon(mtmp)) {
            await pline('%s zaps you with a %s!', Monnam(mtmp),
                flash_str(BZ_OFS_AD(mattk.adtyp)));
        }
        game.buzzer = mtmp;
        await buzz(BZ_M_SPELL(BZ_OFS_AD(mattk.adtyp)), mattk.damn || 0,
            mtmp.mx, mtmp.my, Math.sign(game.tbx || 0), Math.sign(game.tby || 0));
        game.buzzer = null;
        return M_ATTK_HIT;
    }
    return M_ATTK_MISS;
}