All files / js wizard.js

51.21% Statements 168/328
20% Branches 9/45
43.75% Functions 7/16
51.21% Lines 168/328

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 32973x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x     73x 73x 73x 26x 2x 2x 26x 26x 73x 73x 73x                     73x 73x 1x 1x 1x 1x 1x 1x 1x 1x   1x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x               73x 73x                 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 73x 73x 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 1x   1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                       1x 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 73x 73x 73x     73x 73x 73x 73x     73x 73x 73x 73x      
// wizard.js — Wizard of Yendor AI (port of wizard.c)
// C ref: wizard.c — 885 lines
//
// Strategy and tactics for the Wizard and other covetous monsters.
 
import { game } from './gstate.js';
import { rn2, rnd } from './rng.js';
import { pline } from './pline.js';
import { AMULET_OF_YENDOR, BELL_OF_OPENING, CANDELABRUM_OF_INVOCATION, SPE_BOOK_OF_THE_DEAD } from './objects.js';
import { PM_WIZARD_OF_YENDOR, M3_WANTSAMUL, M3_WANTSBELL, M3_WANTSBOOK, M3_WANTSCAND, M3_WANTSARTI, mons } from './monsters.js';
import { is_covetous } from './mondata.js';
import { STRAT_NONE, STRAT_HEAL, STRAT_GROUND, STRAT_MONSTR, STRAT_PLAYER, STRAT_STRATMASK, STRAT_GOAL, STRAT_WAITMASK, STRAT_APPEARMSG, BOLT_LIM, isok, u_at, RLOC_MSG } from './const.js';
import { any_quest_artifact } from './artifact.js';
import { stairway_find_type_dir } from './stairs.js';
import { inhishop } from './shk.js';
import { inhistemple } from './priest.js';
import { monnear, dist2 } from './hacklib.js';
import { rloc, rloc_to } from './teleport.js';
import { noteleport_level } from './teleport.js';
import { m_at } from './map_access.js';
import { mnexto, healmon } from './mon.js';
import { mnearto } from './monmove.js';
 
// ── amulet: check if hero has the Amulet of Yendor ──
export function amulet() {
    return !!game.u?.uhave?.amulet;
}
 
// C ref: wizard.c:106 mon_has_amulet
export function mon_has_amulet(mtmp) {
    for (let otmp = mtmp?.minvent; otmp; otmp = otmp.nobj) {
        if (otmp.otyp === AMULET_OF_YENDOR) return 1;
    }
    return 0;
}
 
// C ref: wizard.c:116 mon_has_special
export function mon_has_special(mtmp) {
    for (let otmp = mtmp?.minvent; otmp; otmp = otmp.nobj) {
        if (otmp.otyp === AMULET_OF_YENDOR
            || any_quest_artifact(otmp)
            || otmp.otyp === BELL_OF_OPENING
            || otmp.otyp === CANDELABRUM_OF_INVOCATION
            || otmp.otyp === SPE_BOOK_OF_THE_DEAD)
            return 1;
    }
    return 0;
}
 
// C ref: wizard.c:142 which_arti
function which_arti(mask) {
    switch (mask) {
    case M3_WANTSAMUL: return AMULET_OF_YENDOR;
    case M3_WANTSBELL: return BELL_OF_OPENING;
    case M3_WANTSCAND: return CANDELABRUM_OF_INVOCATION;
    case M3_WANTSBOOK: return SPE_BOOK_OF_THE_DEAD;
    default: break;
    }
    return 0; // quest artifact
}
 
// C ref: wizard.c:164 mon_has_arti
function mon_has_arti(mtmp, otyp) {
    for (let otmp = mtmp?.minvent; otmp; otmp = otmp.nobj) {
        if (otyp) {
            if (otmp.otyp === otyp) return true;
        } else if (any_quest_artifact(otmp)) return true;
    }
    return false;
}
 
// C ref: wizard.c:183 other_mon_has_arti
function other_mon_has_arti(mtmp, otyp) {
    for (let mtmp2 = game.level?.monlist; mtmp2; mtmp2 = mtmp2.nmon) {
        if (mtmp2 !== mtmp && mon_has_arti(mtmp2, otyp))
            return mtmp2;
    }
    return null;
}
 
// C ref: wizard.c:201 on_ground
function on_ground(otyp) {
    for (let otmp = game.level?.objlist; otmp; otmp = otmp.nobj) {
        if (otyp) {
            if (otmp.otyp === otyp) return otmp;
        } else if (any_quest_artifact(otmp)) return otmp;
    }
    return null;
}
 
// C ref: wizard.c:215 you_have
function you_have(mask) {
    switch (mask) {
    case M3_WANTSAMUL: return !!game.u?.uhave?.amulet;
    case M3_WANTSBELL: return !!game.u?.uhave?.bell;
    case M3_WANTSCAND: return !!game.u?.uhave?.menorah;
    case M3_WANTSBOOK: return !!game.u?.uhave?.book;
    case M3_WANTSARTI: return !!game.u?.uhave?.questart;
    default: break;
    }
    return false;
}
 
// C ref: wizard.c:235 target_on
function target_on(mask, mtmp) {
    if (!(mtmp.data?.mflags3 & mask)) return STRAT_NONE;
 
    const otyp = which_arti(mask);
    if (!mon_has_arti(mtmp, otyp)) {
        if (you_have(mask)) {
            if (!mtmp.mgoal) mtmp.mgoal = {};
            mtmp.mgoal.x = game.u.ux;
            mtmp.mgoal.y = game.u.uy;
            return (STRAT_PLAYER | mask);
        } else {
            const otmp = on_ground(otyp);
            if (otmp) {
                if (!mtmp.mgoal) mtmp.mgoal = {};
                mtmp.mgoal.x = otmp.ox;
                mtmp.mgoal.y = otmp.oy;
                return (STRAT_GROUND | mask);
            } else {
                const mtmp2 = other_mon_has_arti(mtmp, otyp);
                if (mtmp2
                    && (otyp !== AMULET_OF_YENDOR
                        || (!mtmp2.iswiz && !inhistemple(mtmp2)))) {
                    if (!mtmp.mgoal) mtmp.mgoal = {};
                    mtmp.mgoal.x = mtmp2.mx;
                    mtmp.mgoal.y = mtmp2.my;
                    return (STRAT_MONSTR | mask);
                }
            }
        }
    }
    if (!mtmp.mgoal) mtmp.mgoal = {};
    mtmp.mgoal.x = mtmp.mgoal.y = 0;
    return STRAT_NONE;
}
 
// C ref: wizard.c:269 strategy
function strategy(mtmp) {
    let strat, dstrat;
 
    if (!is_covetous(mtmp.data)
        || (mtmp.isshk && inhishop(mtmp))
        || (mtmp.ispriest && inhistemple(mtmp)))
        return STRAT_NONE;
 
    const hpRatio = Math.trunc((mtmp.mhp * 3) / mtmp.mhpmax);
    switch (hpRatio) {
    default:
    case 0:
        return STRAT_HEAL;
    case 1:
        if (mtmp.data !== mons[PM_WIZARD_OF_YENDOR])
            return STRAT_HEAL;
        // FALLTHROUGH
    case 2:
        dstrat = STRAT_HEAL;
        break;
    case 3:
        dstrat = STRAT_NONE;
        break;
    }
 
    if (game.context?.made_amulet) {
        strat = target_on(M3_WANTSAMUL, mtmp);
        if (strat !== STRAT_NONE) return strat;
    }

    if (game.u?.uevent?.invoked) {
        if ((strat = target_on(M3_WANTSARTI, mtmp)) !== STRAT_NONE) return strat;
        if ((strat = target_on(M3_WANTSBOOK, mtmp)) !== STRAT_NONE) return strat;
        if ((strat = target_on(M3_WANTSBELL, mtmp)) !== STRAT_NONE) return strat;
        if ((strat = target_on(M3_WANTSCAND, mtmp)) !== STRAT_NONE) return strat;
    } else {
        if ((strat = target_on(M3_WANTSBOOK, mtmp)) !== STRAT_NONE) return strat;
        if ((strat = target_on(M3_WANTSBELL, mtmp)) !== STRAT_NONE) return strat;
        if ((strat = target_on(M3_WANTSCAND, mtmp)) !== STRAT_NONE) return strat;
        if ((strat = target_on(M3_WANTSARTI, mtmp)) !== STRAT_NONE) return strat;
    }
    return dstrat;
}
 
// C ref: wizard.c:329 choose_stairs
function choose_stairs(out, dir) {
    const g = game;
    const builds_up_fn = () => {
        const d = g.u?.uz ? g.dungeons?.[g.u.uz.dnum] : null;
        return !!d && (d.entry_lev ?? 1) > 1;
    };
    const stdir = builds_up_fn() ? dir : !dir;

    let stway = stairway_find_type_dir(false, stdir);
    if (!stway) {
        stway = stairway_find_type_dir(true, stdir);
        if (!stway) {
            // look for branch stairs in any direction
            for (stway = g.stairs; stway; stway = stway.next) {
                if (stway.tolev?.dnum !== g.u?.uz?.dnum) break;
            }
            if (!stway) {
                stway = stairway_find_type_dir(false, !stdir);
                if (!stway)
                    stway = stairway_find_type_dir(true, !stdir);
            }
        }
    }
    if (stway) {
        out.x = stway.sx;
        out.y = stway.sy;
    }
}
 
// ── nasty: create nasty monsters near hero ──
// C ref: wizard.c:285
export function nasty(mtmp, target) {
    // TODO: full nasty monster creation
    return 0;
}
 
// ── tactics: Wizard AI tactics ──
// C ref: wizard.c:368
export async function tactics(mtmp) {
    const strat = strategy(mtmp);
    let mx, my;
 
    mtmp.mstrategy = (mtmp.mstrategy & (STRAT_WAITMASK | STRAT_APPEARMSG)) | strat;
 
    switch (strat) {
    case STRAT_HEAL: {
        mx = mtmp.mx;
        my = mtmp.my;

        // C ref: wizard.c:381 — expels if swallowed
        // if (game.u.uswallow && game.u.ustuck === mtmp) expels(...)

        const stairs = { x: 0, y: 0 };
        choose_stairs(stairs, !!(mtmp.m_id % 2));
        mtmp.mavenge = 1;

        // C ref: wizard.c:387 In_W_tower check
        if (mtmp.iswiz && !stairs.x && !mon_has_amulet(mtmp)) {
            if (!noteleport_level(mtmp) && !rn2(3 + Math.trunc(mtmp.mhp / 10)))
                await rloc(mtmp, RLOC_MSG);
        } else if (stairs.x && (mx !== stairs.x || my !== stairs.y)) {
            if (!noteleport_level(mtmp)
                && !await mnearto(mtmp, stairs.x, stairs.y, true, RLOC_MSG)) {
                await rloc_to(mtmp, mx, my);
                return 0;
            }
            mx = mtmp.mx;
            my = mtmp.my;
        }
        // C ref: wizard.c:404 — heal if far from player
        if (dist2(game.u.ux, game.u.uy, mx, my) > (BOLT_LIM * BOLT_LIM)) {
            if (mtmp.mhp <= mtmp.mhpmax - 8) {
                healmon(mtmp, rnd(8), 0);
                return 1;
            }
        }
        // FALLTHROUGH to STRAT_NONE
    }
    // fall through
    case STRAT_NONE:
        if (!noteleport_level(mtmp) && !rn2(!mtmp.mflee ? 5 : 33))
            await mnexto(mtmp, RLOC_MSG);
        return 0;
 
    default: {
        // kill, maim, pillage!
        const where = (strat & STRAT_STRATMASK);
        const tx = mtmp.mgoal?.x ?? 0;
        const ty = mtmp.mgoal?.y ?? 0;
        const targ = (strat & STRAT_GOAL);
 
        if (!targ || !isok(tx, ty)) return 0;
        if (noteleport_level(mtmp) && !monnear(mtmp, tx, ty)) return 0;
 
        if (u_at(tx, ty) || where === STRAT_PLAYER) {
            mx = mtmp.mx;
            my = mtmp.my;
            if (noteleport_level(mtmp)
                || !await mnearto(mtmp, tx, ty, false, RLOC_MSG))
                await rloc_to(mtmp, mx, my);
            return 0;
        }
        if (where === STRAT_GROUND) {
            const mon = m_at(tx, ty);
            if (!mon || (mtmp.mx === tx && mtmp.my === ty)) {
                await rloc_to(mtmp, tx, ty);
                // C ref: wizard.c:442 — pick up the object
                // TODO: on_ground + pickup logic
                return 0;
            } else {
                if (!rn2(5) && !noteleport_level(mtmp))
                    await mnexto(mtmp, RLOC_MSG);
                return 0;
            }
        } else {
            // a monster has it
            mx = mtmp.mx;
            my = mtmp.my;
            if (!noteleport_level(mtmp)
                && !await mnearto(mtmp, tx, ty, false, RLOC_MSG))
                await rloc_to(mtmp, mx, my);
            return 0;
        }
    }
    }
}
 
// ── wizdead: check if Wizard is dead ──
export function wizdead() {
    return (game.context?.no_of_wizards ?? 0) === 0;
}
 
// ── resurrect: Wizard comes back from the dead ──
// C ref: wizard.c:715 — stub, real implementation in allmain.js
export function resurrect() {
    // Real resurrect is in allmain.js (needs makemon, pline, etc.)
}
 
// ── intervene: Wizard intervenes remotely ──
// C ref: wizard.c:784 — stub, real implementation in allmain.js
export function intervene() {
    // Real intervene is in allmain.js
}