All files / js write.js

8.62% Statements 22/255
100% Branches 1/1
0% Functions 0/2
8.62% Lines 22/255

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 25673x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x                                                       73x 73x 73x                                                                                                                                                                                                                                                                                                                                                                                                                             73x 73x  
// write.js — Writing scrolls and spellbooks (port of write.c)
// C ref: write.c — dowrite(), applying a magic marker to blank paper.
 
import { game } from './gstate.js';
import { rn1, rn2, rnd } from './rng.js';
import { rnl } from './rng.js';
import { pline, You, Your, You_cant, pline_The } from './pline.js';
import { Blind, Hallucination, Glib } from './macros.js';
import { exercise, ACURR } from './attrib.js';
import { A_WIS } from './const.js';
import { SPBOOK_CLASS, SCROLL_CLASS, SCR_BLANK_PAPER, SPE_BLANK_PAPER, SPE_NOVEL, SPE_BOOK_OF_THE_DEAD, SCR_LIGHT, SCR_GOLD_DETECTION, SCR_FOOD_DETECTION, SCR_MAGIC_MAPPING, SCR_AMNESIA, SCR_FIRE, SCR_EARTH, SCR_DESTROY_ARMOR, SCR_CREATE_MONSTER, SCR_PUNISHMENT, SCR_CONFUSE_MONSTER, SCR_IDENTIFY, SCR_ENCHANT_ARMOR, SCR_REMOVE_CURSE, SCR_ENCHANT_WEAPON, SCR_CHARGING, SCR_SCARE_MONSTER, SCR_STINKING_CLOUD, SCR_TAMING, SCR_TELEPORTATION, SCR_GENOCIDE, SCR_MAIL } from './objects.js';
import { PM_WIZARD } from './monsters.js';
import { mksobj, bcsign, weight } from './mkobj.js';
import { getlin } from './input.js';
import { addinv } from './invent.js';
 
// C ref: write.c:12 cost() — base cost of scroll/spellbook for writing
function writeCost(otmp) {
    if (otmp.oclass === SPBOOK_CLASS)
        return 10 * (game.objects[otmp.otyp]?.oc_oc2 ?? 0); // oc_level

    switch (otmp.otyp) {
    case SCR_MAIL: return 2;
    case SCR_LIGHT: case SCR_GOLD_DETECTION: case SCR_FOOD_DETECTION:
    case SCR_MAGIC_MAPPING: case SCR_AMNESIA: case SCR_FIRE: case SCR_EARTH:
        return 8;
    case SCR_DESTROY_ARMOR: case SCR_CREATE_MONSTER: case SCR_PUNISHMENT:
        return 10;
    case SCR_CONFUSE_MONSTER:
        return 12;
    case SCR_IDENTIFY:
        return 14;
    case SCR_ENCHANT_ARMOR: case SCR_REMOVE_CURSE:
    case SCR_ENCHANT_WEAPON: case SCR_CHARGING:
        return 16;
    case SCR_SCARE_MONSTER: case SCR_STINKING_CLOUD:
    case SCR_TAMING: case SCR_TELEPORTATION:
        return 20;
    case SCR_GENOCIDE:
        return 30;
    case SCR_BLANK_PAPER: default:
        return 1000;
    }
}
 
// C ref: write.c:72 dowrite() — apply magic marker to write on blank paper
export async function dowrite(pen) {
    const g = game;
    const u = g.u;

    // C ref: nohands check
    if (!pen) return 0;

    if (Glib()) {
        // pen slips
        await pline('The marker slips from your fingers.');
        return 1; // ECMD_TIME
    }

    // Get paper — for now, find first blank paper in inventory
    let paper = null;
    for (let obj = g.invent; obj; obj = obj.nobj) {
        if (obj.otyp === SCR_BLANK_PAPER || obj.otyp === SPE_BLANK_PAPER) {
            paper = obj;
            break;
        }
    }
    if (!paper) {
        await You('have nothing to write on.');
        return 0;
    }

    const typeword = (paper.otyp === SPE_NOVEL) ? 'book'
        : (paper.oclass === SPBOOK_CLASS) ? 'spellbook'
        : 'scroll';

    if (Blind()) {
        if (!paper.dknown) {
            await You(`don't know whether that ${typeword} is blank or not.`);
            return 0;
        } else if (paper.oclass === SPBOOK_CLASS) {
            await pline("The marker can't create braille text.");
            return 0;
        }
    }

    if (paper.otyp !== SCR_BLANK_PAPER && paper.otyp !== SPE_BLANK_PAPER) {
        await pline(`That ${typeword} is not blank!`);
        exercise(A_WIS, false);
        return 1;
    }

    // What to write
    const display = game?.nhDisplay || null;
    const namebuf = await getlin(`What type of ${typeword} do you want to write?`, display);
    if (!namebuf || namebuf === '\x1b') return 1;

    let nm = namebuf.trim();
    if (nm.toLowerCase().startsWith('scroll ')) nm = nm.slice(7);
    else if (nm.toLowerCase().startsWith('spellbook ')) nm = nm.slice(10);
    if (nm.toLowerCase().startsWith('of ')) nm = nm.slice(3);

    // Search for matching scroll/spellbook
    const first = game.bases[paper.oclass] ?? 0;
    const last = (game.bases[paper.oclass + 1] ?? game.objects.length) - 1;
    let found = -1;
    let byDescr = false;
    let deferred = 0;
    let deferralchance = 0;

    // First loop: name/description match
    for (let i = first; i <= last; i++) {
        const od = game.objects[i];
        if (!od?.oc_name) continue;
        if (od.oc_name.toLowerCase() === nm.toLowerCase()) {
            if (od.oc_name_known || paper.oclass === SPBOOK_CLASS) {
                found = i;
                break;
            } else {
                deferred = i;
                break;
            }
        }
        if (od.oc_descr && od.oc_descr.toLowerCase() === nm.toLowerCase()) {
            byDescr = true;
            found = i;
            break;
        }
    }

    // Second loop: user-assigned name match
    if (found < 0) {
        for (let i = first; i <= last; i++) {
            const od = game.objects[i];
            if (od?.oc_uname && od.oc_uname.toLowerCase() === nm.toLowerCase()
                && !rn2(++deferralchance)) {
                deferred = i;
                byDescr = true;
            }
        }
    }

    if (found < 0 && deferred) found = deferred;

    if (found < 0) {
        await pline(`There is no such ${typeword}!`);
        return 1;
    }

    const i = found;

    if (i === SCR_BLANK_PAPER || i === SPE_BLANK_PAPER) {
        await You_cant('write that!');
        await pline("It's obscene!");
        return 1;
    }

    if (i === SPE_NOVEL) {
        const fanfic = !rn2(3);
        const tearup = !rn2(3);
        if (!fanfic) {
            await You(`${!tearup ? 'prepare' : 'try'} to write the Great Yendorian Novel, but ${!Hallucination() ? 'lack' : 'have too much'} inspiration.`);
        } else {
            await You(`${!tearup ? 'start to ' : ''}produce really ${!Hallucination() ? 'lame' : 'awesome'} fan-fiction.`);
        }
        if (!tearup) {
            await You('give up on the idea.');
        } else {
            await You('tear it up.');
            // useup(paper)
        }
        return 1;
    }

    if (i === SPE_BOOK_OF_THE_DEAD) {
        await pline('No mere dungeon adventurer could write that.');
        return 1;
    }

    if (byDescr && paper.oclass === SPBOOK_CLASS
        && !game.objects[i]?.oc_name_known) {
        await pline("Unfortunately you don't have enough information to go on.");
        return 1;
    }

    // Create the new object
    const newObj = mksobj(i, false, false);
    newObj.bknown = !!(paper.bknown && pen.bknown);

    // Check ink
    const basecost = writeCost(newObj);
    if ((pen.spe ?? 0) < Math.trunc(basecost / 2)) {
        await Your('marker is too dry to write that!');
        return 1;
    }

    // Calculate actual cost
    const actualcost = rn1(Math.trunc(basecost / 2), Math.trunc(basecost / 2));
    const curseval = bcsign(pen) + bcsign(paper);
    exercise(A_WIS, true);

    // Dry out marker
    if ((pen.spe ?? 0) < actualcost) {
        pen.spe = 0;
        await Your('marker dries out!');
        if (paper.oclass === SPBOOK_CLASS) {
            await pline_The('spellbook is left unfinished and your writing fades.');
        } else {
            await pline_The('scroll is now useless and disappears!');
            // useup(paper)
        }
        return 1;
    }
    pen.spe = (pen.spe ?? 0) - actualcost;

    // Knowledge check
    // C: Role_if(PM_WIZARD) = (urole.mnum == PM_WIZARD)
    const roleIsWizard = (g.urole?.mnum === PM_WIZARD);
    if (!game.objects[newObj.otyp]?.oc_name_known
        && !(byDescr && game.objects[newObj.otyp]?.oc_encountered)
        && rnl((roleIsWizard && paper.oclass !== SPBOOK_CLASS) ? 5 : 15)) {
        await You(`${byDescr ? 'fail' : "don't know how"} to write that.`);
        if (paper.oclass === SPBOOK_CLASS) {
            await You('write in your best handwriting:  "My Diary", but it quickly fades.');
        } else {
            await You(`write something and the scroll disappears.`);
            // useup(paper)
        }
        return 1;
    }

    // Blind check
    if (Blind() && rnl(3)) {
        await You('fail to write the scroll correctly and it disappears.');
        // useup(paper)
        return 1;
    }

    // Success
    if (newObj.oclass === SPBOOK_CLASS) {
        const descr = game.objects[newObj.otyp]?.oc_descr || 'strange';
        await pline_The(`spellbook warps strangely, then turns ${descr}.`);
    }
    newObj.blessed = curseval > 0;
    newObj.cursed = curseval < 0;
    newObj.dknown = false;

    // Give to player
    // C: hold_another_object — simplified: add to inventory
    addinv(newObj);

    return 1;
}
 
export { writeCost };