xonecas.ioDon't click! Sean Caetano Martin Sign in

tiny-adventure / commits / f35dceb

Fights show effects and dodges by name, the threat card lists enemy abilities, and the bot stops looping on fights it can't take yet

The client names every ability from the event's id, so gear abilities and effects play correctly, and dodged hits show as Dodge. The inventory lists gear abilities. The fight replay checks debuffs and dodges. The bot remembers a hopeless fight until it levels, takes a fight after backing off twice for being hurt, and never gambles on the boss below 90%.

Co-Authored-By: Claude Opus 5.5

sacenox1 day agof35dcebfc045798daae592c7057c2f9a950c1f43parent 41107f39 files changed

scripts/bot.mjs+6-3
@@ -4,6 +4,7 @@ import { maxHp } from '../web/js/stats.js';
44 const SAFE = 80;
55 const FLOOR = 50;
66 const WOUNDS = 50;
7+const BOSS = 90;
78 const SHOP_KINDS = ['camp', 'village'];
89
910 const score = (it) => it.bonus.maxHp / 3 + it.bonus.attack + it.bonus.defense + it.bonus.magic + it.bonus.speed / 2;
@@ -50,19 +51,21 @@ function target(s, seen) {
5051 export function createBot(io, { safe = SAFE, note = () => {} } = {}) {
5152 let bar = safe;
5253 const seen = {};
54+ const worn = {};
5355
5456 async function engage(s) {
5557 const t = s.threat;
5658 const id = t.poi || t.band;
5759 const boss = id === s.campaign.main.steps.at(-1).poi;
58- const costly = t.wounds > WOUNDS && !full(s);
59- if (!t.barred && !costly && t.odds >= (boss ? Math.min(95, bar + 10) : bar)) {
60+ const costly = t.wounds > WOUNDS && !full(s) && (worn[id] || 0) < 2;
61+ if (!t.barred && !costly && t.odds >= (boss ? BOSS : bar)) {
6062 note('fight', `${id} at ${t.odds}% odds, ${t.wounds}% wounds`);
6163 const after = await io.fight(s);
6264 if (after.phase !== 'defeat') bar = safe;
6365 return after;
6466 }
65- seen[id] = { lv: levels(s), hurt: !full(s) };
67+ if (t.wounds > WOUNDS && !full(s)) worn[id] = (worn[id] || 0) + 1;
68+ seen[id] = { lv: levels(s), hurt: !full(s) && t.odds >= bar };
6669 note('back off', `${id} at ${t.odds}% odds, ${t.wounds}% wounds${t.barred ? ', barred' : ''}`);
6770 return io.back(s);
6871 }
scripts/fights.mjs+4-2
@@ -19,7 +19,9 @@ export function replayFight(before, after) {
1919 const same = heroes.has(ev.actor) === heroes.has(ev.target);
2020 if (!(hp.get(ev.actor) > 0) && ev.ability !== 'thorns') out.push(`a fallen unit acts: ${ev.text}`);
2121 if (!(hp.get(ev.target) > 0)) out.push(`a fallen unit is targeted: ${ev.text}`);
22- if ((ev.kind === 'heal' || ev.kind === 'buff') !== same) out.push(`${ev.kind} crosses sides the wrong way: ${ev.text}`);
22+ const friendly = ev.kind === 'heal' || ev.kind === 'buff';
23+ if (ev.ability !== 'thorns' && (friendly || ev.kind === 'debuff' || ev.kind === 'damage' || ev.kind === 'aoe') && friendly !== same) out.push(`${ev.kind} crosses sides the wrong way: ${ev.text}`);
24+ if (ev.dodged && ev.amount !== 0) out.push(`a dodged hit lands: ${ev.text}`);
2325 applyEvent(hp, ev, cap.get(ev.target));
2426 }
2527 const was = Object.fromEntries(before.party.units.map((u) => [u.id, u.level]));
@@ -51,7 +53,7 @@ if (import.meta.url === `file://${process.argv[1]}`) {
5153 const [base, ...seeds] = process.argv.slice(2);
5254 const api = async (path, body) => (await fetch(base + path, body === undefined ? { headers: { 'X-Player': 'f'.repeat(32) } } : { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Player': 'f'.repeat(32) }, body: JSON.stringify(body) })).json();
5355 const r = await checkFights(api, seeds.length ? seeds.map(Number) : [1, 2, 3, 4, 5]);
54- console.log(`${r.fights} fights, kinds ${JSON.stringify(r.kinds)}, mend ${r.abilities.mend || 0}, rally ${r.abilities.rally || 0}, lifesteal ${r.abilities.lifesteal || 0}`);
56+ console.log(`${r.fights} fights, kinds ${JSON.stringify(r.kinds)}, abilities ${JSON.stringify(r.abilities)}`);
5557 for (const p of r.problems.slice(0, 10)) console.log(`FAIL ${p}`);
5658 process.exit(r.problems.length ? 1 : 0);
5759 }
scripts/verify.mjs+2-2
@@ -270,9 +270,9 @@ mkdirSync(outDir, { recursive: true });
270270 const FIGHT_SEEDS = [1, 2, 3, 4, 5];
271271 console.log(`== fights through the API, seeds ${FIGHT_SEEDS.join(', ')}`);
272272 report.fights = await checkFights(api, FIGHT_SEEDS);
273-for (const want of ['heal', 'buff']) if (!report.fights.kinds[want]) report.fights.problems.push(`no ${want} event in ${report.fights.fights} fights, so it went unchecked`);
273+for (const want of ['heal', 'buff', 'debuff', 'aoe']) if (!report.fights.kinds[want]) report.fights.problems.push(`no ${want} event in ${report.fights.fights} fights, so it went unchecked`);
274274 failures += report.fights.problems.length;
275-console.log(` ${report.fights.problems.length ? 'FAIL' : 'ok '} ${report.fights.fights} fights replayed, mend ${report.fights.abilities.mend || 0}, rally ${report.fights.abilities.rally || 0}, lifesteal ${report.fights.abilities.lifesteal || 0}`);
275+console.log(` ${report.fights.problems.length ? 'FAIL' : 'ok '} ${report.fights.fights} fights replayed, kinds ${JSON.stringify(report.fights.kinds)}`);
276276 for (const p of report.fights.problems.slice(0, 10)) console.log(` FAIL ${p}`);
277277 for (const r of RESOLUTIONS) {
278278 const tag = `${r[0]}x${r[1]}`;
web/js/abilities.js+1-0
@@ -0,0 +1 @@
1+export const abilityName = (id) => id.split('-').map((w) => w[0].toUpperCase() + w.slice(1)).join(' ');
web/js/combat.js+10-13
@@ -5,6 +5,7 @@ import { maxHp } from './stats.js';
55 import { T } from './theme.js';
66 import { track } from './debug.js';
77 import { applyEvent } from './playback.js';
8+import { abilityName } from './abilities.js';
89
910 const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1011 const RANGE = 2.4;
@@ -66,7 +67,7 @@ export function createCombat(scene, party, cam, ui, sound) {
6667 tween(0.18, (p) => body.scale.set(1 + Math.sin(p * Math.PI) * 0.18, 1 - Math.sin(p * Math.PI) * 0.22, 1 + Math.sin(p * Math.PI) * 0.18));
6768 }
6869
69- function strike(ev, t, ab, heal) {
70+ function strike(ev, t, heal) {
7071 const next = applyEvent(hp, ev, t.max);
7172 if (!t.ally) setBar(t.bar, next / t.max);
7273 if (!heal && ev.amount > 0) {
@@ -79,7 +80,7 @@ export function createCombat(scene, party, cam, ui, sound) {
7980 sound.play('heal');
8081 }
8182 const [sx, sy] = cam.screenOf(t.fig);
82- ui.floater(sx, sy, heal ? `+${ev.amount}` : ev.amount > 0 ? `${ev.amount}${ev.crit ? '!' : ''}` : ab ? ab.name : '', heal ? 'heal' : ev.crit ? 'crit' : t.ally ? 'hurt' : 'dmg');
83+ ui.floater(sx, sy, heal ? `+${ev.amount}` : ev.dodged ? 'Dodge' : `${ev.amount}${ev.crit ? '!' : ''}`, heal ? 'heal' : ev.dodged ? 'gain' : ev.crit ? 'crit' : t.ally ? 'hurt' : 'dmg');
8384 if (ev.killed) {
8485 sound.play('fall');
8586 cam.shake(0.35);
@@ -94,11 +95,11 @@ export function createCombat(scene, party, cam, ui, sound) {
9495 }
9596 }
9697
97- function boost(ev, t) {
98+ function effect(ev, t) {
9899 flash(t.fig, T.accent);
99100 sound.play('heal');
100101 const [sx, sy] = cam.screenOf(t.fig);
101- ui.floater(sx, sy, `+${ev.amount}% power`, 'gain');
102+ ui.floater(sx, sy, abilityName(ev.ability), 'gain');
102103 }
103104
104105 async function play(state, before, at) {
@@ -115,11 +116,9 @@ export function createCombat(scene, party, cam, ui, sound) {
115116 enemies = foes;
116117 const ids = new Map();
117118 hp.clear();
118- const abilities = new Map();
119119 foes.forEach((f) => {
120120 ids.set(f.unit.id, f);
121121 hp.set(f.unit.id, f.hp);
122- f.unit.abilities.forEach((a) => abilities.set(a.id, a));
123122 });
124123 const prev = Object.fromEntries(before.party.units.map((u) => [u.id, u]));
125124 state.party.units.forEach((u) => {
@@ -127,7 +126,6 @@ export function createCombat(scene, party, cam, ui, sound) {
127126 const p = prev[u.id] || u;
128127 ids.set(u.id, { unit: u, fig, ally: true, max: maxHp(p) });
129128 hp.set(u.id, p.stats.hp);
130- u.abilities.forEach((a) => abilities.set(a.id, a));
131129 });
132130 for (const f of party.figures.values()) f.userData.mode = 'idle';
133131 const mid = party.pos.clone().addScaledVector(dir, RANGE / 2);
@@ -143,17 +141,16 @@ export function createCombat(scene, party, cam, ui, sound) {
143141 const turn = fast ? 220 : base;
144142 const a = ids.get(ev.actor);
145143 const t = ids.get(ev.target);
146- const ab = abilities.get(ev.ability);
147144 const heal = ev.kind === 'heal';
148- const buff = ev.kind === 'buff';
145+ const cast = heal || ev.kind === 'buff' || ev.kind === 'debuff';
149146 if (a) {
150- a.fig.userData.mode = heal || buff ? 'cast' : 'fight';
147+ a.fig.userData.mode = cast ? 'cast' : 'fight';
151148 a.fig.userData.swing?.();
152- if (t && a !== t && !heal && !buff) lunge(a.fig, t.fig.position, (turn / 1000) * 0.9);
149+ if (t && a !== t && !cast) lunge(a.fig, t.fig.position, (turn / 1000) * 0.9);
153150 }
154151 await sleep(turn * 0.45);
155- if (t && buff) boost(ev, t);
156- else if (t) strike(ev, t, ab, heal);
152+ if (t && cast && !heal) effect(ev, t);
153+ else if (t) strike(ev, t, heal);
157154 ui.logLine(ev.text, ev.killed ? 'kill' : ev.crit ? 'crit' : '');
158155 ui.render(before, hp);
159156 await sleep(turn * 0.55);
web/js/inventory.js+2-1
@@ -3,6 +3,7 @@ import { chrome, PAD } from './chrome.js';
33 import { T, rarityColor } from './theme.js';
44 import { total, maxHp, bonusText, compareText } from './stats.js';
55 import { traitText, traitName, heroTraits } from './traits.js';
6+import { abilityName } from './abilities.js';
67 import { oddsWord } from './threat.js';
78
89 export const SLOTS = ['weapon', 'armor', 'helm', 'trinket'];
@@ -66,7 +67,7 @@ export function createInventory(layer, m, f) {
6667 });
6768 const abY = statY + 76;
6869 drawText(ctx, 'Abilities', PAD, abY, { h: 13, size: 12, color: T.faint });
69- drawText(ctx, u.abilities.map((a) => a.name).join(', '), PAD, abY + 13, { h: 18, size: 13, color: T.muted, maxW: leftW, fit: true });
70+ drawText(ctx, [...new Set([...u.abilities.map((a) => a.id), ...Object.values(u.gear || {}).flatMap((it) => it?.abilities || [])])].map(abilityName).join(', '), PAD, abY + 13, { h: 18, size: 13, color: T.muted, maxW: leftW, fit: true });
7071 const traits = heroTraits(u);
7172 drawText(ctx, 'Traits', PAD, abY + 34, { h: 13, size: 12, color: T.faint });
7273 drawText(ctx, traits.length ? traits.map(traitName).join(', ') : 'None yet. Gear with a trait adds one.', PAD, abY + 47, { h: 18, size: 13, color: traits.length ? T.fg : T.faint, maxW: leftW, fit: true });
web/js/playback.js+1-1
@@ -1,6 +1,6 @@
11 export function applyEvent(hp, ev, cap) {
22 const cur = hp.get(ev.target) ?? 0;
3- if (ev.kind === 'buff') return cur;
3+ if (ev.kind === 'buff' || ev.kind === 'debuff') return cur;
44 const next = ev.kind === 'heal' ? Math.min(cap, cur + ev.amount) : Math.max(0, cur - ev.amount);
55 hp.set(ev.target, next);
66 return next;
web/js/threat.js+4-4
@@ -6,10 +6,9 @@ import { nature, traitName } from './traits.js';
66 const ROW = 22;
77
88 export const oddsWord = (t) => (t.odds >= 85 ? (t.wounds < 35 ? 'Easy' : 'Costly') : t.odds >= 60 ? 'Likely' : t.odds >= 35 ? 'Risky' : 'Deadly');
9-export const threatHeight = (t) => 318 + t.enemies.length * ROW;
9+export const threatHeight = (t) => 338 + t.enemies.length * ROW;
1010 const list = (xs) => xs.map(traitName).join(', ');
11-const ELEMENTS = ['fire', 'frost', 'holy', 'shadow'];
12-const foeTraits = (xs) => xs.map((t) => (ELEMENTS.includes(t) ? `${traitName(t)} attacks` : traitName(t))).join(', ');
11+const foeAbilities = (foes) => [...new Set(foes.flatMap((e) => e.abilities.map((a) => a.name)))].join(', ');
1312
1413 export function createThreat(layer, m, f) {
1514 return layer.add('threat', {
@@ -37,7 +36,8 @@ export function createThreat(layer, m, f) {
3736 y += 20;
3837 };
3938 y += 4;
40- line('Traits', foeTraits(n.traits), T.fg);
39+ line('Abilities', foeAbilities(threat.enemies), T.fg);
40+ line('Traits', list(n.traits), T.fg);
4141 line('Weak to', list(n.weak), T.fg);
4242 line('Resists', list(n.resist), T.muted);
4343 line('Your edge', e.hits.join(', ') + (e.blunted.length ? `${e.hits.length ? '. ' : ''}Resisted: ${e.blunted.join(', ')}` : ''), T.fg);
web/js/traits.js+7-7
@@ -1,17 +1,17 @@
1+const TYPES = ['poison', 'fire', 'frost', 'lightning', 'holy', 'shadow'];
2+const cap = (w) => w[0].toUpperCase() + w.slice(1);
3+
14 const TRAITS = {
25 lifesteal: ['Lifesteal', 'heals 30% of damage dealt'],
36 first: ['First strike', 'acts first, opening blow +50%'],
4- cleave: ['Cleave', 'hits splash a second foe'],
7+ sweep: ['Sweep', 'single hits splash a second foe for half'],
58 pierce: ['Pierce', 'ignores defense and armor'],
6- fire: ['Fire', 'attacks deal fire damage'],
7- frost: ['Frost', 'attacks deal frost damage'],
8- holy: ['Holy', 'attacks deal holy damage'],
9- shadow: ['Shadow', 'attacks deal shadow damage'],
109 thorns: ['Thorns', 'melee attackers take 25% back'],
1110 taunt: ['Taunt', 'foes must strike this hero'],
12- ward: ['Ward', 'elemental damage taken -30%'],
13- armored: ['Armored', 'defense +50%, pierce cuts through'],
11+ armored: ['Armored', 'defense +3, pierce cuts through'],
1412 swarm: ['Swarm', 'more of them, each weaker'],
13+ ...Object.fromEntries(TYPES.map((t) => [t, [cap(t), `physical abilities deal ${t} damage`]])),
14+ ...Object.fromEntries(TYPES.map((t) => [`guard-${t}`, [`${cap(t)} guard`, `defense also cuts ${t} damage, and ${t} hazards are spared`]])),
1515 };
1616
1717 export const traitName = (t) => TRAITS[t]?.[0] || t;