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 | 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 73x 73x 73x 73x 73x 73x 87x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 493x 493x 279x 36x 493x 73x 73x 464x 464x 464x 464x 216x 464x 464x 75x 464x 464x 38x 464x 135x 464x 464x 73x 73x 73x 544x 544x 544x 544x 80x 80x 80x 80x 80x 544x 464x 464x 464x 270x 270x 464x 194x 194x 194x 464x 464x 544x 544x 544x 544x 544x 544x 544x 73x 73x 73x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 342x 342x 19x 6x 6x 19x 342x 57x 57x 57x 342x 342x 342x 338x 338x 338x 338x 342x 342x 342x 342x 342x 342x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 73x 73x 73x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 64x 64x 64x 64x 64x 81x 81x 81x 81x 81x 81x 81x 81x 81x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 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 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 64x 64x 64x 73x 73x 73x 330x 330x 330x 328x 328x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 2x 2x 330x 328x 328x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 73x 73x 73x 9x 9x 9x 9x 9x 9x 9x 9x 45x 45x 45x 9x 9x 9x 9x 9x | // exper.js — Experience and leveling (port of exper.c)
// C ref: exper.c — experience points, level changes, spell power
import { game } from './gstate.js';
import { rn1, rn2, rnd, pushRngLogEntry } from './rng.js';
import { pline, You_feel } from './pline.js';
import { ACURR, adjabil, adjattrib, newhp } from './attrib.js';
import { Amphibious, Role_if, Role_switch, Upolyd } from './macros.js';
import { setBotl } from './display.js';
import {
AT_BUTT, AT_WEAP, AT_MAGC, AD_PHYS, AD_BLND,
AD_DRLI, AD_STON, AD_SLIM, AD_WRAP, S_EEL,
PM_BARBARIAN, PM_CLERIC, PM_GRAY_DRAGON, PM_HEALER, PM_KNIGHT, PM_VALKYRIE, PM_WIZARD,
S_DRAGON,
} from './monsters.js';
import { A_WIS, MAXULEV, NORMAL_SPEED } from './const.js';
import { livelog_printf } from './hack.js';
import { record_achievement, count_achievements } from './insight.js';
import { is_golem, minuhpmax, monsndx, resists_drli } from './mondata.js';
import { golemhp } from './makemon.js';
import { find_mac } from './worn.js';
import { exp_percent_changing, xlev_to_rank } from './botl.js';
import { rehumanize, setuhpmax } from './polyself.js';
const NATTK = 6; // C: NATTK from permonst.h — max attack slots per monster
// STUBS: functions not yet ported (NO RNG consumption)
function Goodbye() { return 'Goodbye'; }
function extra_nasty(ptr) { return !!(ptr?.mflags2 & 0x02000000); } // M2_NASTY
// Amphibious: imported from macros.js
// Role_switch: imported from macros.js
// C ref: makemon.c:987 — monhp_per_lvl: HP gain/loss per level (consumes RNG)
function monhp_per_lvl(mon) {
const ptr = mon.data;
let hp = rnd(8); // default is d8
if (is_golem(ptr)) {
hp = Math.trunc(golemhp(monsndx(ptr)) / (ptr.mlevel || 1));
} else if (ptr.mlevel > 49) {
hp = 4 + rnd(4); // 5..8
} else if (ptr.mlet === S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
hp = 4 + rn2(5); // 4..8
} else if (!mon.m_lev) {
hp = rnd(4); // level 0 monsters use 1d4
}
return hp;
}
// setuhpmax: imported from polyself.js
// minuhpmax: imported from mondata.js
function achieve_rank(_rank) { return 0; }
// livelog_printf imported from hack.js
function SoundAchievement(..._args) { /* no-op */ }
// NOTE: differs from canonical polyself.js — local is a no-op;
// canonical has full rehumanize logic. Kept to avoid circular dep (polyself imports exper)
// PM_BARBARIAN, PM_CLERIC, PM_HEALER, PM_KNIGHT, PM_VALKYRIE, PM_WIZARD
// imported from monsters.js (previously hardcoded role indices, not PM indices)
const LONG_MAX = Number.MAX_SAFE_INTEGER;
// C ref: exper.c:14 newuexp() — XP needed to reach level
export function newuexp(lev) {
if (lev < 1) return 0;
if (lev < 10) return 10 * (1 << lev);
if (lev < 20) return 10000 * (1 << (lev - 10));
return 10000000 * (lev - 19);
}
// C ref: exper.c:26 enermod() — energy modifier by role
function enermod(en) {
switch (Role_switch()) {
case PM_CLERIC:
case PM_WIZARD:
return 2 * en;
case PM_HEALER:
case PM_KNIGHT:
return Math.trunc((3 * en) / 2);
case PM_BARBARIAN:
case PM_VALKYRIE:
return Math.trunc((3 * en) / 4);
default:
return en;
}
}
// C ref: exper.c:44 newpw() — spell power for new level
export function newpw() {
const u = game.u;
let en = 0;
if (u.ulevel === 0) {
en = (game.urole?.enadv?.infix ?? 0) + (game.urace?.enadv?.infix ?? 0);
const roleRnd = game.urole?.enadv?.inrnd ?? 0;
const raceRnd = game.urace?.enadv?.inrnd ?? 0;
if (roleRnd > 0) en += rnd(roleRnd);
if (raceRnd > 0) en += rnd(raceRnd);
} else {
let enrnd = Math.trunc(ACURR(A_WIS) / 2);
let enfix;
if (u.ulevel < (game.urole?.xlev ?? 14)) {
enrnd += (game.urole?.enadv?.lornd ?? 0) + (game.urace?.enadv?.lornd ?? 0);
enfix = (game.urole?.enadv?.lofix ?? 0) + (game.urace?.enadv?.lofix ?? 0);
} else {
enrnd += (game.urole?.enadv?.hirnd ?? 0) + (game.urace?.enadv?.hirnd ?? 0);
enfix = (game.urole?.enadv?.hifix ?? 0) + (game.urace?.enadv?.hifix ?? 0);
}
en = enermod(rn1(enrnd, enfix));
}
if (en <= 0) en = 1;
if (u.ulevel < MAXULEV) {
if (!u.ueninc) u.ueninc = [];
u.ueninc[u.ulevel] = en;
} else {
let lim = 4 - Math.trunc(u.uenmax / 200);
lim = Math.max(lim, 1);
if (en > lim) en = lim;
}
return en;
}
// C ref: exper.c:84 experience() — XP value of killed monster
export function experience(mtmp, nk) {
const ptr = mtmp.data;
let tmp = 1 + mtmp.m_lev * mtmp.m_lev;
// Extra XP for high AC
const mac = find_mac(mtmp);
if (mac < 3) tmp += (7 - mac) * (mac < 0 ? 2 : 1);
// Extra XP for fast monsters
if (ptr.mmove > NORMAL_SPEED)
tmp += (ptr.mmove > Math.trunc(3 * NORMAL_SPEED / 2)) ? 5 : 3;
// Extra XP for special attack types
for (let i = 0; i < NATTK; i++) {
const tmp2 = ptr.mattk[i]?.aatyp ?? 0;
if (tmp2 > AT_BUTT) {
if (tmp2 === AT_WEAP) tmp += 5;
else if (tmp2 === AT_MAGC) tmp += 10;
else tmp += 3;
}
}
// Extra XP for special damage types
for (let i = 0; i < NATTK; i++) {
const tmp2 = ptr.mattk[i]?.adtyp ?? 0;
if (tmp2 > AD_PHYS && tmp2 < AD_BLND)
tmp += 2 * mtmp.m_lev;
else if (tmp2 === AD_DRLI || tmp2 === AD_STON || tmp2 === AD_SLIM)
tmp += 50;
else if (tmp2 !== AD_PHYS)
tmp += mtmp.m_lev;
// Extra heavy damage bonus
if ((ptr.mattk[i]?.damd ?? 0) * (ptr.mattk[i]?.damn ?? 0) > 23)
tmp += mtmp.m_lev;
if (tmp2 === AD_WRAP && ptr.mlet === S_EEL && !Amphibious())
tmp += 1000;
}
// Extra nasty monsters
if (extra_nasty(ptr)) tmp += 7 * mtmp.m_lev;
// Higher level bonus
if (mtmp.m_lev > 8) tmp += 50;
// Reduce for repeated kills of revived/cloned monsters
if (mtmp.mrevived || mtmp.mcloned) {
let tmp2 = 20;
let tmpNk = nk;
for (let i = 0; tmpNk > tmp2 && tmp > 1; ++i) {
tmp = Math.trunc((tmp + 1) / 2);
tmpNk -= tmp2;
if (i & 1) tmp2 += 20;
}
}
return tmp;
}
// C ref: exper.c:168 more_experienced() — add experience points
export function more_experienced(exper, rexp) {
const u = game.u;
const oldexp = u.uexp;
const oldrexp = u.urexp ?? 0;
let newexp = oldexp + exper;
const rexpincr = 4 * exper + rexp;
let newrexp = oldrexp + rexpincr;
// Cap on wraparound
if (newexp < 0 && exper > 0) newexp = LONG_MAX;
if (newrexp < 0 && rexpincr > 0) newrexp = LONG_MAX;
if (newexp !== oldexp) {
u.uexp = newexp;
// C ref: exper.c:186-191 — SET_BOTL() emits event + sets flag
if (game.flags?.showexp) setBotl('more_experienced');
if (!game.disp?.botl && exp_percent_changing()) setBotl('more_experienced');
}
if (newrexp !== oldrexp) {
u.urexp = newrexp;
}
// C ref: exper.c:204-206 — permanent XP instrumentation
if (exper || rexp)
pushRngLogEntry(`^xp[+${exper}xp +${rexp}rexp total=${newexp} need=${newuexp(u.ulevel)} lvl=${u.ulevel}]`);
if ((u.urexp ?? 0) >= (Role_if(PM_WIZARD) ? 1000 : 2000))
game.flags.beginner = false;
}
// C ref: exper.c:206 losexp() — lose an experience level
export async function losexp(drainer) {
const u = game.u;
// Wizard mode #levelchange override
if (drainer && drainer === '#levelchange') drainer = null;
else if (resists_drli(game.youmonst)) return;
if (u.ulevel > 1 || drainer) {
await pline(`${Goodbye()} level ${u.ulevel}.`);
}
if (u.ulevel > 1) {
u.ulevel -= 1;
await adjabil(u.ulevel + 1, u.ulevel);
} else {
if (drainer) {
// Fatal drain at level 1 — TODO: done(DIED)
return;
}
if (u.ulevel > 1) return;
u.uexp = 0;
}
const olduhpmax = u.uhpmax;
const uhpmin = minuhpmax(10);
const num_hp = u.uhpinc?.[u.ulevel] ?? 0;
u.uhpmax -= num_hp;
if (u.uhpmax < uhpmin) setuhpmax(uhpmin, true);
if (u.uhpmax > olduhpmax) setuhpmax(olduhpmax, true);
u.uhp -= num_hp;
if (u.uhp < 1) u.uhp = 1;
else if (u.uhp > u.uhpmax) u.uhp = u.uhpmax;
const num_en = u.ueninc?.[u.ulevel] ?? 0;
u.uenmax -= num_en;
if (u.uenmax < 0) u.uenmax = 0;
u.uen -= num_en;
if (u.uen < 0) u.uen = 0;
else if (u.uen > u.uenmax) u.uen = u.uenmax;
if (u.uexp > 0) u.uexp = newuexp(u.ulevel) - 1;
if (Upolyd()) {
const num_mhp = monhp_per_lvl(game.youmonst);
u.mhmax -= num_mhp;
u.mh -= num_mhp;
if (u.mh <= 0) await rehumanize();
}
setBotl('losexp');
}
// C ref: exper.c:300 newexplevel() — check for level-up
export async function newexplevel() {
if (game.u.ulevel < MAXULEV && game.u.uexp >= newuexp(game.u.ulevel))
await pluslvl(true);
}
// C ref: exper.c:306 pluslvl() — gain an experience level
export async function pluslvl(incr) {
const u = game.u;
if (!incr) {
await You_feel('more experienced.');
}
// Increase HP (polymorph form first)
if (Upolyd()) {
const hpinc = monhp_per_lvl(game.youmonst);
u.mh += hpinc;
setuhpmax(u.mhmax, false);
}
const hpinc = newhp();
u.uhp += hpinc;
setuhpmax(u.uhpmax + hpinc, true);
// Increase spell power
const eninc = newpw();
u.uenmax += eninc;
if (u.uenmax > (u.uenpeak ?? 0)) u.uenpeak = u.uenmax;
u.uen += eninc;
// Increase level
if (u.ulevel < MAXULEV) {
const oldrank = xlev_to_rank(u.ulevel);
if (incr) {
const tmp = newuexp(u.ulevel + 1);
if (u.uexp >= tmp) u.uexp = tmp - 1;
} else {
u.uexp = newuexp(u.ulevel);
}
++u.ulevel;
await pline(`Welcome ${(u.ulevelmax ?? 0) < u.ulevel ? '' : 'back '}to experience level ${u.ulevel}.`);
if ((u.ulevelmax ?? 0) < u.ulevel) u.ulevelmax = u.ulevel;
await adjabil(u.ulevel - 1, u.ulevel);
const newrank = xlev_to_rank(u.ulevel);
if (newrank > oldrank) record_achievement(achieve_rank(newrank));
if (u.ulevel > (u.ulevelpeak ?? 0)) u.ulevelpeak = u.ulevel;
}
setBotl('pluslvl');
}
// C ref: exper.c:377 rndexp() — random XP for polyself
export function rndexp(gaining) {
const u = game.u;
const minexp = u.ulevel === 1 ? 0 : newuexp(u.ulevel - 1);
const maxexp = newuexp(u.ulevel);
let diff = maxexp - minexp;
let factor = 1;
// C: LARGEST_INT = 32767 (16-bit); scale down for rn2's range
while (diff >= 32767) {
diff = Math.trunc(diff / 2);
factor *= 2;
}
let result = minexp + factor * rn2(Math.max(1, Math.trunc(diff)));
if (u.ulevel === MAXULEV && gaining) {
result += (u.uexp - minexp);
if (result < u.uexp) result = u.uexp;
}
return result;
}
|