aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_starter_equipment.mjs (47608B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs/promises';
      3 import {createRequire} from 'node:module';
      4 import {GameTest} from './game_test.mjs';
      5 import {startStarter, finishStarter, enterOmniTraining} from './create_starter.mjs';
      6 
      7 const {chromium} = createRequire(import.meta.url)('playwright');
      8 const [url, output] = process.argv.slice(2);
      9 assert(url && output, 'Run through TestBrowserStarterEquipment');
     10 const browser = await chromium.launch({executablePath: '/usr/bin/chromium',
     11   args: ['--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage',
     12     '--disable-background-timer-throttling', '--disable-renderer-backgrounding']});
     13 const games = [];
     14 
     15 // Clear the raised mission doorway lips with ordinary forward jumps.
     16 async function jump(game) {
     17   const start = game.local().position;
     18   game.record({kind: 'action', action: 'jump'});
     19   await game.page.keyboard.down('KeyW');
     20   try {
     21     await game.key('Space');
     22     await game.wait('jump across raised lip', state => {
     23       const position = game.local(state).position;
     24       return Math.hypot(position[0] - start[0], position[2] - start[2]) >= 2.5;
     25     });
     26   } finally {
     27     const sequence = game.sequence;
     28     await game.page.keyboard.up('KeyW');
     29     await game.waitInput(sequence, 769);
     30   }
     31 }
     32 
     33 // Use the issued laboratory on its real unlocks and retain every charge debit.
     34 async function recover(game, label) {
     35   const count = game.local().inventory.find(item => item.templates[0] === 116628)?.count ?? 0;
     36   await game.key('F1');
     37   await game.key('KeyX');
     38   await game.wait('seated for laboratory use', state => game.local(state).stats[430] === 2);
     39   let uses = 0, sequence = game.sequence;
     40   const deadline = Date.now() + 600000;
     41   for (;;) {
     42     await game.snapshot();
     43     const server = await (await game.page.request.get(url + '/test-state')).json();
     44     if (game.local().stats[27] === server.maximumHealth && server.health === server.maximumHealth) break;
     45     assert(Date.now() < deadline, 'Starter health did not recover in both client and server');
     46     const unlocked = game.events.some(event => {
     47       if (event.sequence <= sequence || event.direction !== 'framereceived' || event.opcode !== '5e477770') return false;
     48       const packet = Buffer.from(event.hex, 'hex');
     49       return packet.readUInt32BE(packet.length - 20) === 164 && packet.readUInt32BE(packet.length - 4) === 124;
     50     });
     51     if (uses === count || server.treatmentLocked || (uses && (!unlocked || server.maximumHealth - server.health < 15))) continue;
     52     sequence = game.sequence;
     53     await game.click(681, 45, 'right');
     54     ++uses;
     55     await game.wait('one laboratory charge consumed', state =>
     56       (game.local(state).inventory.find(item => item.templates[0] === 116628)?.count ?? 0) === count - uses);
     57     if (uses < count) await game.click(681, 45, 'right');
     58     const locked = await (await game.page.request.get(url + '/test-state')).json();
     59     assert.equal(locked.inventory.Items.find(item => item.Template === 116628)?.Count ?? 0, count - uses);
     60     assert(locked.health >= Math.min(server.maximumHealth, server.health + 15));
     61   }
     62   assert.equal(game.local().inventory.find(item => item.templates[0] === 116628)?.count ?? 0, count - uses);
     63   await game.key('KeyX');
     64   await game.save(output + '/' + label);
     65   return uses;
     66 }
     67 
     68 // Frame a stationary world object through real native camera input.
     69 async function frame(game, type, id) {
     70   const target = () => game.state.actors.find(actor => actor.type === type && actor.id === id);
     71   if (target().screenPosition[1] < 350 || target().screenPosition[1] > 540) {
     72     const cameraPoint = await game.point(512, 300);
     73     let mouseY = cameraPoint.y, sequence = game.sequence;
     74     const framingDeadline = Date.now() + 10000;
     75     await game.page.mouse.move(cameraPoint.x, mouseY);
     76     await game.page.mouse.down({button: 'right'});
     77     try {
     78       await game.waitInput(sequence, 1025);
     79       for (;;) {
     80         await game.snapshot();
     81         const screenY = target().screenPosition[1];
     82         if (screenY >= 350 && screenY <= 540) break;
     83         assert(Date.now() < framingDeadline, 'Starter did not frame the world object above chat');
     84         mouseY += screenY > 540 ? 20 : -20;
     85         await game.page.mouse.move(cameraPoint.x, mouseY, {steps: 2});
     86       }
     87     } finally {
     88       sequence = game.sequence;
     89       await game.page.mouse.up({button: 'right'});
     90     }
     91     await game.waitInput(sequence, 1026);
     92   }
     93 }
     94 
     95 // Each encounter starts at full health and uses native targeting, attacks and loot.
     96 async function fight(game, enemy, label) {
     97   const experience = game.local().stats[52], credits = game.local().stats[61];
     98   const attackRating = game.local().stats[54] === 1 ? 27 : 32;
     99   if (game.local().stats[22] !== attackRating) {
    100     await game.page.keyboard.press('Control+8');
    101     await game.key('F1');
    102     await game.click(857, 128, 'right');
    103     await game.wait("Thug's Delight active", state => game.local(state).stats[22] === attackRating);
    104     await game.page.keyboard.press('Control+8');
    105   }
    106   const targetingDeadline = Date.now() + 30000;
    107   while (game.state.selected[0] !== 50000 || game.state.selected[1] !== enemy.id) {
    108     assert(Date.now() < targetingDeadline, 'Native Tab did not select the encounter');
    109     const selected = game.state.selected;
    110     await game.key('Tab');
    111     await game.wait('native target cycle advanced', state =>
    112       state.selected[0] !== selected[0] || state.selected[1] !== selected[1]);
    113   }
    114   const beforeFight = await (await game.page.request.get(url + '/test-state')).json();
    115   const healthyEnemy = beforeFight.enemies.find(actor => actor.id === enemy.id);
    116   assert.equal(beforeFight.health, beforeFight.maximumHealth);
    117   assert.equal(game.local().stats[27], beforeFight.maximumHealth);
    118   assert.equal(healthyEnemy.health, 20);
    119   assert.equal(healthyEnemy.maximumHealth, 20);
    120   await game.save(output + '/' + label + '/fight-start');
    121   await fs.writeFile(output + '/' + label + '/fight-start/server.json', JSON.stringify(beforeFight, null, 2));
    122   const attack = game.sequence;
    123   await game.key('KeyQ');
    124   await game.wait('native attack request', () => game.events.some(event =>
    125     event.sequence > attack && event.direction === 'framesent' && event.opcode === '28494070'));
    126   const request = Buffer.from(game.events.find(event => event.sequence > attack &&
    127     event.direction === 'framesent' && event.opcode === '28494070').hex, 'hex');
    128   // The native request ends with the target identity and one attack flag.
    129   assert.equal(request.readUInt32BE(request.length - 9), 50000);
    130   assert.equal(request.readUInt32BE(request.length - 5), enemy.id);
    131   let special = game.sequence;
    132   await game.key('KeyN');
    133   const deadline = Date.now() + 120000;
    134   for (;;) {
    135     await game.snapshot();
    136     assert(game.local().stats[27] > 0, 'Ordinary starter died during ' + label);
    137     if (game.state.actors.some(actor => actor.type === 51050 && actor.id === enemy.id)) break;
    138     assert(Date.now() < deadline, 'Ordinary Leet fight did not finish');
    139     const unlocked = game.events.some(event => {
    140       if (event.sequence <= special || event.direction !== 'framereceived' || event.opcode !== '5e477770') return false;
    141       const packet = Buffer.from(event.hex, 'hex');
    142       return packet.readUInt32BE(packet.length - 20) === 164 && packet.readUInt32BE(packet.length - 4) === 147;
    143     });
    144     if (unlocked) {
    145       special = game.sequence;
    146       await game.key('KeyN');
    147     }
    148   }
    149   await game.wait('earned kill XP', state => game.local(state).stats[52] === experience + 100);
    150   let levelAward = null;
    151   if (game.local().stats[54] !== beforeFight.level) {
    152     const event = game.events.find(event => event.sequence > attack &&
    153       event.direction === 'framereceived' && event.opcode === '7f405a16');
    154     assert(event, 'Earned level did not publish its native award');
    155     const packet = Buffer.from(event.hex, 'hex');
    156     levelAward = Array.from({length: 8}, (_, index) => packet.readUInt32BE(packet.length - 32 + index * 4));
    157     assert.deepEqual(levelAward, [game.local().id, beforeFight.stats.ImprovementPoints + 4000,
    158       1500, 1450, 4050, 0, 2, 100]);
    159   }
    160   await game.save(output + '/' + label + '/earned-kill');
    161 
    162   // Use the actual remains twice, then take every generated item exactly once.
    163   const killed = await (await game.page.request.get(url + '/test-state')).json();
    164   await fs.writeFile(output + '/' + label + '/earned-kill/server.json', JSON.stringify(killed, null, 2));
    165   const corpse = killed.corpses.find(corpse => corpse.id === enemy.id);
    166   assert(corpse && corpse.credits >= 1 && corpse.credits <= 5);
    167   const corpseDistance = state => {
    168     const position = game.local(state).position;
    169     const remains = state.actors.find(actor => actor.type === 51050 && actor.id === enemy.id);
    170     return Math.hypot(position[0] - remains.position[0], position[1] - remains.position[1],
    171       position[2] - remains.position[2]);
    172   };
    173   // Expose the small corpse above chat while staying within the five-metre Use range.
    174   const viewingDeadline = Date.now() + 20000;
    175   while (corpseDistance(game.state) < 2.5) {
    176     assert(Date.now() < viewingDeadline, 'Starter did not clear its corpse view');
    177     const sequence = game.sequence;
    178     await game.page.keyboard.press('KeyS', {delay: 200});
    179     await game.waitInput(sequence, 769);
    180     await game.snapshot();
    181     assert.equal(game.local().playfield, killed.playfield, 'Corpse retreat crossed a zone boundary');
    182   }
    183   assert(corpseDistance(game.state) < 5);
    184 
    185   await frame(game, 51050, enemy.id);
    186   let [x, y] = await game.pick(51050, enemy.id);
    187   await game.click(x, y, 'right');
    188   await game.wait('earned corpse cash', state => game.local(state).stats[61] === credits + corpse.credits);
    189   await game.wait('corpse window open', state => state.regions.some(
    190     ([left, top, right, bottom]) => left === 0 && top === 0 && right === 178 && bottom === 128));
    191   await game.click(170, 8);
    192   const reopen = game.sequence;
    193   [x, y] = await game.pick(51050, enemy.id);
    194   await game.click(x, y, 'right');
    195   await game.wait('native corpse reopen request', () => game.events.some(event => {
    196     if (event.sequence <= reopen || event.direction !== 'framesent' || event.opcode !== '52526858') return false;
    197     const packet = Buffer.from(event.hex, 'hex');
    198     return packet.readUInt32BE(packet.length - 8) === 51050 && packet.readUInt32BE(packet.length - 4) === enemy.id;
    199   }));
    200   await game.wait('corpse window reopened', state => state.regions.some(
    201     ([left, top, right, bottom]) => left === 0 && top === 0 && right === 178 && bottom === 128));
    202   await game.save(output + '/' + label + '/corpse-loot');
    203   const templates = new Set();
    204   for (const [slot, item] of (corpse.items ?? []).entries()) {
    205     assert(item.Template === 42640 || item.Template === 70562);
    206     assert(!templates.has(item.Template));
    207     templates.add(item.Template);
    208     assert(item.Quality >= 1 && item.Quality <= (healthyEnemy.level === 1 ? 1 : 3));
    209     assert.deepEqual(item, {ID: item.ID, Type: item.Template === 42640 ? 51005 : 51022,
    210       Template: item.Template, Quality: item.Quality, Slot: slot, Count: 1, Ammo: 0});
    211     const cell = game.state.grids.flatMap(grid => grid.cells).find(cell =>
    212       cell.identity[0] === 107 && cell.identity[1] === item.Slot && !cell.dummy);
    213     assert(cell, 'generated item has a native corpse cell');
    214     await game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2);
    215     await game.wait('generated item in native inventory', state =>
    216       game.local(state).inventory.some(held => held.id === item.ID));
    217   }
    218   await game.click(170, 8);
    219   const combatUses = await recover(game, label + '/recovery');
    220 
    221   const after = await (await game.page.request.get(url + '/test-state')).json();
    222   assert.equal(after.inventory.Credits, credits + corpse.credits);
    223   const slots = new Set(killed.inventory.Items.map(item => item.Slot));
    224   for (const item of corpse.items ?? []) {
    225     let slot = 64;
    226     while (slots.has(slot)) ++slot;
    227     slots.add(slot);
    228     assert.deepEqual(after.inventory.Items.find(held => held.ID === item.ID), {...item, Slot: slot});
    229   }
    230   console.log('PASS: ' + label + ', full-health kill, exact corpse ledger and recovery');
    231   return {fightStart: {health: beforeFight.health, enemy: healthyEnemy}, corpse, combatUses, levelAward,
    232     loot: after.inventory.Items.filter(item => (corpse.items ?? []).some(drop => drop.ID === item.ID))};
    233 }
    234 
    235 // Approach an ordinary full-health outdoor encounter at the requested starter level.
    236 async function outdoorFight(game, level, label) {
    237   const population = await (await game.page.request.get(url + '/test-state')).json();
    238   const position = game.local().position;
    239   const enemies = population.enemies.filter(enemy => enemy.level === level && enemy.health === 20);
    240   enemies.sort((a, b) => Math.hypot(a.position[0] - position[0], a.position[2] - position[2]) -
    241     Math.hypot(b.position[0] - position[0], b.position[2] - position[2]));
    242   const enemy = enemies[0];
    243   assert(enemy, 'No full-health ordinary Leet at level ' + level);
    244   const deadline = Date.now() + 30000;
    245   for (;;) {
    246     await game.snapshot();
    247     assert(Date.now() < deadline, 'Did not approach the ordinary Leet');
    248     const target = game.state.actors.find(actor => actor.type === 50000 && actor.id === enemy.id);
    249     const position = game.local().position;
    250     if (Math.hypot(target.position[0] - position[0], target.position[1] - position[1],
    251       target.position[2] - position[2]) > 2.5) {
    252       await game.walk(target.position[0], target.position[2] - 2);
    253       continue;
    254     }
    255     const server = await (await game.page.request.get(url + '/test-state')).json();
    256     const actual = server.enemies.find(actor => actor.id === enemy.id);
    257     if (Math.hypot(actual.position[0] - position[0], actual.position[1] - position[1],
    258       actual.position[2] - position[2]) <= 3) break;
    259   }
    260   return fight(game, enemy, label);
    261 }
    262 
    263 try {
    264   // Create both characters through the original creator and Arrival Hall.
    265   for (const name of ['Starterowner', 'Starterwatch']) {
    266     const context = await browser.newContext({viewport: {width: 1024, height: 768}});
    267     const page = await context.newPage();
    268     const game = await GameTest.attach(page);
    269     games.push(game);
    270     await page.goto(url);
    271     await game.ready();
    272     await startStarter(game);
    273     await finishStarter(game, name, 'starter regression password');
    274     await enterOmniTraining(game);
    275     assert.equal(game.local().playfield, 950);
    276     assert.equal(game.local().inventory.length, 7);
    277     assert.equal(game.local().stats[54], 1);
    278     assert.equal(game.local().stats[52], 0);
    279     await game.save(output + '/' + name + '/created');
    280   }
    281   const [owner, observer] = games;
    282   await owner.wait('ordinary observer in the training ground', state =>
    283     state.actors.some(actor => actor.id === observer.state.localId));
    284   const issued = owner.local().inventory;
    285   const watched = observer.local().inventory;
    286   const bat = issued.find(item => item.templates[0] === 121564);
    287   assert(bat);
    288   const before = (await (await owner.page.request.get(url + '/test-state')).json()).inventory;
    289 
    290   // The native inventory's initial three-column layout puts the issued bat third.
    291   await owner.key('i');
    292   await owner.save(output + '/inventory');
    293   await owner.click(789, 45, 'right');
    294   await owner.wait('issued bat equipped', state => owner.local(state).inventory.some(item => item.id === bat.id && item.slot === 6));
    295   const equipped = (await (await owner.page.request.get(url + '/test-state')).json()).inventory;
    296   assert.deepEqual(equipped.Items, before.Items.map(item => item.ID === bat.id ? {...item, Slot: 6} : item));
    297   assert.deepEqual(owner.local().inventory.map(item => item.id).sort(), issued.map(item => item.id).sort());
    298   await observer.snapshot();
    299   assert.deepEqual(observer.local().inventory, watched);
    300   console.log('PASS: normally issued bat equips without changing any owned identity or observer inventory');
    301 
    302   await owner.page.reload();
    303   await owner.ready();
    304   assert(owner.local().inventory.some(item => item.id === bat.id && item.slot === 6));
    305   await owner.page.keyboard.press('Control+1');
    306   await owner.snapshot();
    307   await owner.save(output + '/wear');
    308   console.log('PASS: page reconnect retains the same equipped starter instance');
    309 
    310   // Remove through Wear, then equip the same instance from its original inventory cell.
    311   await owner.click(867, 206, 'right');
    312   await owner.wait('issued bat returned to inventory', state => owner.local(state).inventory.some(item => item.id === bat.id && item.slot === bat.slot));
    313   const removed = (await (await owner.page.request.get(url + '/test-state')).json()).inventory;
    314   assert.deepEqual(removed.Items, before.Items);
    315   assert.deepEqual(owner.local().inventory, issued);
    316   await owner.page.keyboard.press('Control+1');
    317   await owner.key('i');
    318   await owner.save(output + '/removed');
    319   await owner.click(789, 45, 'right');
    320   await owner.wait('same issued bat equipped again', state => owner.local(state).inventory.some(item => item.id === bat.id && item.slot === 6));
    321   const reworn = (await (await owner.page.request.get(url + '/test-state')).json()).inventory;
    322   assert.deepEqual(reworn.Items, equipped.Items);
    323   await observer.snapshot();
    324   assert.deepEqual(observer.local().inventory, watched);
    325   console.log('PASS: native remove/rewear preserves both starter loadouts and charges');
    326 
    327   // Train the health, weapon and nano requirements through the native skill form.
    328   await owner.key('KeyI');
    329   await owner.key('KeyU');
    330   for (const [y, points] of [[226, 3], [256, 3], [346, 5]])
    331     for (let point = 0; point < points; ++point) await owner.click(835, y);
    332   await owner.click(445, 622);
    333   await owner.key('KeyU');
    334   await owner.click(302, 272);
    335   for (const [y, points] of [[166, 5], [436, 4]])
    336     for (let point = 0; point < points; ++point) await owner.click(835, y);
    337   await owner.click(288, 504);
    338   for (const [y, points] of [[226, 4], [316, 4], [376, 2]])
    339     for (let point = 0; point < points; ++point) await owner.click(835, y);
    340   await owner.click(445, 622);
    341   await owner.wait('ordinary skill purchase', state => owner.local(state).stats[53] === 1170);
    342   const trained = await (await owner.page.request.get(url + '/test-state')).json();
    343   assert.equal(trained.stats.ImprovementPoints, 1170);
    344   assert.equal(trained.stats.Abilities.Stamina, 13);
    345   assert.equal(trained.stats.Abilities.Intelligence, 6);
    346   assert.equal(trained.stats.Body.BodyDevelopment, 10);
    347   assert.equal(trained.stats.Melee.OneHandBlunt, 10);
    348   assert.equal(trained.stats.Melee.FastAttack, 9);
    349   assert.equal(trained.stats.NanoAiding.PsychologicalModification, 9);
    350   assert.equal(trained.stats.NanoAiding.SensoryImprovement, 9);
    351   assert.equal(trained.stats.NanoAiding.Treatment, 7);
    352   assert.equal(trained.stats.NanoAiding.FirstAid, 5);
    353   assert.equal(trained.maximumHealth, 84);
    354   await owner.key('KeyI');
    355   const trainingUses = await recover(owner, 'trained-recovery');
    356 
    357   // Wear the issued shirt and learn/cast the issued Thug's Delight crystal.
    358   const crystal = issued.find(item => item.templates[0] === 43379);
    359   const shirt = issued.find(item => item.templates[0] === 29426);
    360   await owner.click(681, 154, 'right');
    361   await owner.wait('issued shirt worn', state => owner.local(state).inventory.some(item => item.id === shirt.id && item.slot === 21));
    362   await owner.click(681, 99, 'right');
    363   await owner.wait('issued crystal consumed', state => !owner.local(state).inventory.some(item => item.id === crystal.id));
    364   await owner.page.keyboard.press('Control+8');
    365   await owner.click(927, 96);
    366   await owner.key('F1');
    367   await owner.click(857, 128, 'right');
    368   await owner.wait('Thug\'s Delight active', state => owner.local(state).stats[22] === 27);
    369   await owner.page.keyboard.press('Control+8');
    370 
    371   // Drag the native aggression thumb, then approach a full-health spawned Leet.
    372   await owner.page.keyboard.press('Control+9');
    373   const thumb = await owner.point(927, 190), aggressive = await owner.point(1006, 190);
    374   await owner.page.mouse.move(thumb.x, thumb.y);
    375   await owner.snapshot();
    376   await owner.page.mouse.down();
    377   try {
    378     await owner.snapshot();
    379     await owner.page.mouse.move(aggressive.x, aggressive.y, {steps: 12});
    380   } finally { await owner.page.mouse.up(); }
    381   await owner.wait('full aggression', state => owner.local(state).stats[51] === 100);
    382   await owner.page.keyboard.press('Control+9');
    383   // Cross the authored opening before approaching the selected roaming Leet.
    384   for (const [x, z] of [[62, 65], [62, 74]])
    385     await owner.walk(x, z);
    386   if (!owner.state.camera.firstPerson) await owner.key('F8');
    387 
    388   const fights = [await outdoorFight(owner, 1, 'outdoor')];
    389 
    390   // Spend one earned credit on the first advertised contract at the original booth.
    391   for (const [x, z] of [[62, 74], [62, 65], [52.5, 57.6], [52.5973, 55.1362]]) {
    392     await owner.walk(x, z);
    393     if (owner.local().playfield === 3000) break;
    394   }
    395   await owner.wait('ordinary backyard arrival', state => owner.local(state)?.playfield === 3000, 60000);
    396   await owner.ready();
    397   for (const [x, z] of [[102.5, 102], [102.5, 143], [104, 149.5], [103, 149.1]])
    398     await owner.walk(x, z);
    399   await frame(owner, 56001, 3221228472);
    400   let [x, y] = await owner.pick(56001, 3221228472);
    401   await owner.click(x, y, 'right');
    402   await owner.wait('visible mission difficulty window', state => state.regions.some(
    403     ([left, top, right, bottom]) => left === 150 && top === 180 && right === 220 && bottom === 200));
    404   await owner.click(100, 163);
    405   await owner.wait('earned mission request fee', state =>
    406     owner.local(state).stats[61] === fights[0].corpse.credits - 1);
    407   const advertised = (await (await owner.page.request.get(url + '/test-state')).json()).offer;
    408   assert(advertised && advertised.Credits === 500 && advertised.RewardQuality === 1);
    409   await owner.save(output + '/mission-offer');
    410   await owner.click(30, 45);
    411   await owner.click(74, 190);
    412   await owner.wait('ordinary mission key', state => owner.local(state).inventory.some(item => item.templates[0] === 28577));
    413   const accepted = await (await owner.page.request.get(url + '/test-state')).json();
    414   assert.equal(accepted.missions.length, 1);
    415   const mission = accepted.missions[0];
    416   // Follow the saved room orientation, including retained contracts after reload.
    417   const missionReversed = Buffer.from(mission.layout, 'base64')[24] === 0;
    418   const missionPoint = (x, z) => missionReversed ? [40 - x, 60 - z] : [x, z];
    419   for (const field of ['Description', 'Playfield', 'Entrance', 'Position', 'ObjectiveItem',
    420     'Credits', 'Experience', 'RewardItem', 'RewardQuality'])
    421     assert.deepEqual(mission.definition[field], advertised[field]);
    422   assert.equal(mission.completed, false);
    423   const key = accepted.inventory.Items.find(item => item.ID === mission.id);
    424   assert(key && key.Type === 51053 && key.Template === 28577);
    425   const nativeKey = owner.local().inventory.find(item => item.templates[0] === 28577);
    426   assert.equal(nativeKey.slot, key.Slot - 16);
    427   assert.equal(nativeKey.quality, 1);
    428   assert.equal(nativeKey.count, 1);
    429   await owner.save(output + '/mission-accepted');
    430 
    431   // Enter with the issued key and clear all five authored, full-health encounters.
    432   for (const [x, z] of [[96.4, 150.1], [89.3, 153.6], [89.3, 151.2]]) {
    433     await owner.walk(x, z);
    434     if (owner.local().playfield === mission.world) break;
    435   }
    436   await owner.wait('private mission arrival', state => owner.local(state)?.playfield === mission.world, 60000);
    437   await owner.ready();
    438   const entered = await (await owner.page.request.get(url + '/test-state')).json();
    439   assert.equal(entered.enemies.length, 5);
    440   assert(entered.enemies.every(enemy => enemy.health === 20 && enemy.maximumHealth === 20));
    441   assert.equal(entered.missions[0].layout, mission.layout);
    442   mission.objective = entered.missions[0].objective;
    443   const encounters = entered.enemies.sort((a, b) => (missionReversed ? -1 : 1) * (a.position[2] - b.position[2]));
    444   await owner.save(output + '/mission-entered');
    445   let book;
    446   for (let index = 0; index < encounters.length; ++index) {
    447     const z = [16.6, 27.5, 33.25, 40.25, 43.25][index];
    448     await owner.walk(...missionPoint(15, z));
    449     fights.push(await fight(owner, encounters[index], 'mission-' + (index + 1)));
    450     if (index === 0) {
    451       for (const z of [16.6, 23.5]) {
    452         await owner.walk(...missionPoint(15, z));
    453         await jump(owner);
    454       }
    455     }
    456     if (index === 1) {
    457       // Pick up the exact world book before later loot fills the visible bag rows.
    458       await owner.walk(...missionPoint(15, 31));
    459       await frame(owner, 51005, mission.objective);
    460       const beforePickup = (await (await owner.page.request.get(url + '/test-state')).json()).inventory;
    461       [x, y] = await owner.pick(51005, mission.objective);
    462       await owner.click(x, y);
    463       const pickup = owner.sequence;
    464       await owner.key('KeyR');
    465       for (const direction of ['framesent', 'framereceived']) {
    466         await owner.wait('exact mission book pickup ' + direction, () => owner.events.some(event => {
    467           if (event.sequence <= pickup || event.direction !== direction || event.opcode !== '47537a24') return false;
    468           const packet = Buffer.from(event.hex, 'hex');
    469           return packet.readUInt32BE(packet.length - 20) === 51005 &&
    470             packet.readUInt32BE(packet.length - 16) === mission.objective &&
    471             packet.readUInt32BE(packet.length - 12) === 0 && packet.readUInt32BE(packet.length - 8) === 0 &&
    472             packet.readUInt32BE(packet.length - 4) === 111;
    473         }));
    474       }
    475       const collected = (await (await owner.page.request.get(url + '/test-state')).json()).inventory;
    476       const occupied = new Set(beforePickup.Items.map(item => item.Slot));
    477       let slot = 64;
    478       while (occupied.has(slot)) ++slot;
    479       assert.deepEqual(collected.Items, [...beforePickup.Items, {ID: mission.objective,
    480         Type: 51005, Template: 40822, Quality: 1, Slot: slot, Count: 1, Ammo: 0}]);
    481       // client/game/kkr.dll/lifted-0017.c::sub_3602D580 leaves the
    482       // simple proxy's identity fields zero.
    483       await owner.wait('mission book proxy in its native slot', state =>
    484         owner.local(state).inventory.some(item => item.slot === slot - 16 && item.templates[0] === 40822));
    485       book = owner.local().inventory.find(item => item.slot === slot - 16);
    486       assert.deepEqual(book, {slot: slot - 16, type: 0, id: 0, quality: 1,
    487         templates: [40822, 40822], count: 1});
    488       await owner.save(output + '/mission-book');
    489     }
    490   }
    491   assert.equal(owner.local().stats[52], 600);
    492   const cleared = await (await owner.page.request.get(url + '/test-state')).json();
    493   assert.deepEqual(cleared.enemies, []);
    494   assert.equal(cleared.missions[0].completed, false);
    495 
    496   // Return the allocated book to the issuing booth through native item-on-item Use.
    497   await owner.walk(...missionPoint(15, 26.5));
    498   await jump(owner);
    499   for (const z of [12.5, 10]) {
    500     await owner.walk(...missionPoint(15, z));
    501     if (owner.local().playfield === 3000) break;
    502   }
    503   await owner.wait('mission exterior return', state => owner.local(state)?.playfield === 3000, 60000);
    504   await owner.ready();
    505   for (const [x, z] of [[89.3, 153.6], [96.4, 153.6], [99.8, 154], [99.8, 151.4]]) await owner.walk(x, z);
    506   await frame(owner, 56001, 3221228472);
    507   const bookCell = book.slot - 48;
    508   await owner.click(681 + bookCell % 3 * 54, 45 + Math.floor(bookCell / 3) * 54);
    509   [x, y] = await owner.pick(56001, 3221228472);
    510   const handIn = owner.sequence;
    511   await owner.click(x, y, 'right');
    512   let credits = fights.reduce((total, fight) => total + fight.corpse.credits, 0) - 1 + advertised.Credits;
    513   await owner.wait('exact book and key consumed with advertised cash', state =>
    514     !owner.local(state).inventory.some(item => item.templates[0] === 28577 || item.templates[0] === 40822) &&
    515     owner.local(state).stats[61] === credits);
    516   await owner.wait('native exact-book hand-in request', () => owner.events.some(event => {
    517     if (event.sequence <= handIn || event.direction !== 'framesent' || event.opcode !== '52526858') return false;
    518     const packet = Buffer.from(event.hex, 'hex');
    519     return packet.readUInt32BE(37) === 5 && packet.readUInt32BE(packet.length - 8) === 56001 &&
    520       packet.readUInt32BE(packet.length - 4) === 3221228472;
    521   }));
    522   const completed = await (await owner.page.request.get(url + '/test-state')).json();
    523   assert.equal(completed.missions[0].completed, true);
    524   const rewards = completed.inventory.Items.filter(item =>
    525     !cleared.inventory.Items.some(held => held.ID === item.ID));
    526   assert.equal(rewards.length, 1);
    527   assert.equal(rewards[0].Template, advertised.RewardItem);
    528   assert.equal(rewards[0].Quality, advertised.RewardQuality);
    529   assert.equal(rewards[0].Count, 1);
    530   const occupied = new Set(cleared.inventory.Items.filter(item =>
    531     item.ID !== mission.id && item.ID !== mission.objective).map(item => item.Slot));
    532   let rewardSlot = 64;
    533   while (occupied.has(rewardSlot)) ++rewardSlot;
    534   assert.equal(rewards[0].Slot, rewardSlot);
    535   await owner.wait('awarded reward proxy in its native slot', state => owner.local(state).inventory.some(item =>
    536     item.slot === rewardSlot - 16 && item.templates[0] === rewards[0].Template && item.quality === 1 && item.count === 1));
    537   await owner.save(output + '/mission-completed');
    538 
    539   // Train the actual rolled reward's requirements and retain any replaced starter item.
    540   const rewardTrainingCost = rewards[0].Template === 122691 ? 29 : 26;
    541   await owner.key('KeyU');
    542   await owner.click(334, 196);
    543   if (rewards[0].Template === 122691) {
    544     await owner.click(835, 286);
    545     await owner.click(315, 233);
    546     await owner.click(835, 256);
    547   } else {
    548     await owner.click(835, 196);
    549     await owner.click(835, 196);
    550   }
    551   await owner.click(445, 622);
    552   await owner.wait('earned reward requirements trained', state =>
    553     owner.local(state).stats[53] === 1170 - rewardTrainingCost);
    554   let displaced;
    555   if (rewards[0].Template === 122691 || rewards[0].Template === 85686) {
    556     const original = completed.inventory.Items.find(item => item.ID ===
    557       (rewards[0].Template === 122691 ? bat.id : shirt.id));
    558     const occupied = new Set(completed.inventory.Items.map(item => item.Slot));
    559     let slot = 64;
    560     while (occupied.has(slot)) ++slot;
    561     displaced = {...original, Slot: slot};
    562     await owner.page.keyboard.press('Control+1');
    563     if (rewards[0].Template === 122691) await owner.click(867, 206, 'right');
    564     else { await owner.click(953, 53); await owner.click(927, 148, 'right'); }
    565     await owner.wait('issued predecessor retained in inventory', state =>
    566       owner.local(state).inventory.some(item => item.id === original.ID && item.slot === slot - 16));
    567     await owner.page.keyboard.press('Control+1');
    568   }
    569   const wornReward = {...rewards[0], Slot: {
    570     85596: 24, 85742: 25, 85638: 30, 85512: 27, 85557: 18, 85686: 21, 122691: 6,
    571   }[rewards[0].Template]};
    572   assert(wornReward.Slot, 'Unexpected starter reward family');
    573   const rewardCell = rewards[0].Slot - 64;
    574   await owner.click(681 + rewardCell % 3 * 54, 45 + Math.floor(rewardCell / 3) * 54, 'right');
    575   await owner.wait('earned reward worn in its native slot', state => owner.local(state).inventory.some(item =>
    576     item.slot === wornReward.Slot && item.templates[0] === wornReward.Template && item.quality === 1 && item.count === 1));
    577   const worn = await (await owner.page.request.get(url + '/test-state')).json();
    578   if (rewards[0].Template === 122691) {
    579     assert.equal(worn.stats.Abilities.Sense, 4);
    580     assert.equal(worn.stats.Body.Dimach, 6);
    581   } else assert.equal(worn.stats.Abilities.Agility, 8);
    582   assert.deepEqual(worn.inventory.Items, completed.inventory.Items.map(item =>
    583     item.ID === wornReward.ID ? wornReward : item.ID === displaced?.ID ? displaced : item));
    584   await owner.save(output + '/earned-reward-worn');
    585 
    586   // Earn the first level from nine further full-health ordinary fights.
    587   for (const [x, z] of [[102.5, 143], [102.5, 102], [109, 102], [112, 101]]) {
    588     await owner.walk(x, z);
    589     if (owner.local().playfield === 950) break;
    590   }
    591   await owner.wait('training return for earned leveling', state => owner.local(state)?.playfield === 950, 60000);
    592   await owner.ready();
    593   for (const [x, z] of [[62, 65], [62, 74]]) await owner.walk(x, z);
    594   while (owner.local().stats[52] < 1500)
    595     fights.push(await outdoorFight(owner, 1, 'earned-kill-' + (fights.length + 1)));
    596   assert.equal(owner.local().stats[52], 1500);
    597   assert.equal(owner.local().stats[54], 2);
    598   assert.equal(owner.local().stats[53], 5170 - rewardTrainingCost);
    599   assert.equal(fights.filter(fight => fight.levelAward).length, 1);
    600   await owner.save(output + '/earned-level-two');
    601 
    602   // Spend earned IP on health and the worn weapon, then recover both native/server HP.
    603   await owner.key('KeyU');
    604   await owner.click(334, 196);
    605   for (let point = 0; point < 5; ++point) await owner.click(835, 346);
    606   await owner.click(302, 272);
    607   for (let point = 0; point < 5; ++point) await owner.click(835, 166);
    608   await owner.click(445, 622);
    609   await owner.wait('earned level-two health and weapon training', state =>
    610     owner.local(state).stats[53] === 5050 - rewardTrainingCost && owner.local(state).stats[1] === 111 &&
    611     owner.local(state).stats[22] === 32);
    612   const levelTrainingUses = await recover(owner, 'earned-level-two-training');
    613   while (!owner.local().inventory.some(item => item.templates[0] === 42640))
    614     fights.push(await outdoorFight(owner, 1, 'earned-sale-part-' + fights.length));
    615   credits = fights.reduce((total, fight) => total + fight.corpse.credits, 0) - 1 + advertised.Credits;
    616   const funded = await (await owner.page.request.get(url + '/test-state')).json();
    617   assert.equal(funded.inventory.Credits, credits);
    618   assert.equal(funded.stats.Body.BodyDevelopment, 15);
    619   assert.equal(funded.stats.Melee.OneHandBlunt, 15);
    620   const sale = funded.inventory.Items.find(item => item.Template === 42640);
    621   assert(sale && sale.Quality === 1 && credits >= 500);
    622 
    623   // Walk the original streets to the medical shop without crossing unverified geometry.
    624   for (const [x, z] of [[62, 74], [62, 65], [52.5, 57.6], [52.5973, 55.1362]]) {
    625     await owner.walk(x, z);
    626     if (owner.local().playfield === 3000) break;
    627   }
    628   assert.equal(owner.local().playfield, 3000);
    629   for (const [x, z] of [[102.5, 102], [102.5, 143], [104, 149.5], [105.161, 155.124]]) {
    630     await owner.walk(x, z);
    631     if (owner.local().playfield === 705) break;
    632   }
    633   assert.equal(owner.local().playfield, 705);
    634   for (const [x, z] of [[951, 843], [920.6, 843], [920.6, 860.2], [917.3, 860.5],
    635     [900.94, 860.47], [884, 860.2], [884, 889], [859.05, 888.49], [830.7, 888.62],
    636     [831.68, 872.85], [825.73, 872.65], [804, 865.2], [804, 855], [804, 835], [804, 815],
    637     [804, 795], [804, 775], [804, 754.35], [784.07, 693.33], [784.04, 653.34],
    638     [800.88, 624.31], [801.06, 549.34], [797.11, 524.32], [793.24, 521.24], [781.33, 517.08], [766, 515]]) {
    639     await owner.walk(x, z);
    640     if (owner.local().playfield === 1183) break;
    641   }
    642   assert.equal(owner.local().playfield, 1183);
    643   for (const [x, z] of [[187, 129.66], [186.98, 135.26], [185.7, 135.7]]) await owner.walk(x, z);
    644   await frame(owner, 51035, 3221423263);
    645   [x, y] = await owner.pick(51035, 3221423263);
    646   await owner.click(x, y, 'right');
    647   await owner.wait('original medical shop open', state => state.regions.some(
    648     ([left, top, right, bottom]) => left === 832 && top === 0 && right === 1024 && bottom === 494));
    649   await owner.click(864, 85);
    650   await owner.wait('funded laboratory selected for cancellation', state => state.grids.some(grid =>
    651     grid.rows === 1 && grid.cells.some(cell => cell.identity[0] === 111 && !cell.dummy)));
    652   await owner.save(output + '/affordable-laboratory');
    653   assert.deepEqual((await (await owner.page.request.get(url + '/test-state')).json()).inventory, funded.inventory);
    654 
    655   // Cancel and retry the exact earned part sale before accepting its six-credit quote.
    656   const saleCell = sale.Slot - 64;
    657   for (let attempt = 0; attempt < 2; ++attempt) {
    658     await owner.click(681 + saleCell % 3 * 54, 45 + Math.floor(saleCell / 3) * 54);
    659     const offer = owner.sequence;
    660     await owner.click(864, 416);
    661     await owner.wait('native part sale offered', () => owner.events.some(event =>
    662       event.sequence > offer && event.direction === 'framesent' && event.opcode === '36284f6e'));
    663     assert.deepEqual((await (await owner.page.request.get(url + '/test-state')).json()).inventory, funded.inventory);
    664     if (attempt === 0) {
    665       await owner.click(945, 475);
    666       await owner.wait('cancelled sale restores exact native part', state => owner.local(state).inventory.some(item =>
    667         item.id === sale.ID && item.slot === sale.Slot - 16));
    668       await owner.save(output + '/earned-sale-cancelled');
    669       [x, y] = await owner.pick(51035, 3221423263);
    670       await owner.click(x, y, 'right');
    671       await owner.wait('medical shop reopened', state => state.regions.some(
    672         ([left, top, right, bottom]) => left === 832 && top === 0 && right === 1024 && bottom === 494));
    673     }
    674   }
    675   await owner.click(874, 475);
    676   credits += 6;
    677   await owner.wait('earned part sale committed once', state => owner.local(state).stats[61] === credits);
    678   const sold = await (await owner.page.request.get(url + '/test-state')).json();
    679   assert.deepEqual(sold.inventory.Items, funded.inventory.Items.filter(item => item.ID !== sale.ID));
    680   await owner.save(output + '/earned-sale-accepted');
    681 
    682   // Buy one original package, then reconstruct its owned identity before self-use.
    683   [x, y] = await owner.pick(51035, 3221423263);
    684   await owner.click(x, y, 'right');
    685   await owner.wait('medical stock ready for purchase', state => state.regions.some(
    686     ([left, top, right, bottom]) => left === 832 && top === 0 && right === 1024 && bottom === 494));
    687   const purchase = owner.sequence;
    688   await owner.click(924, 85);
    689   await owner.wait('native Nano Kit purchase offered', () => owner.events.some(event =>
    690     event.sequence > purchase && event.direction === 'framesent' && event.opcode === '36284f6e'));
    691   await owner.click(874, 475);
    692   credits -= 20;
    693   await owner.wait('one funded Nano Kit purchased', state => owner.local(state).stats[61] === credits);
    694   const bought = await (await owner.page.request.get(url + '/test-state')).json();
    695   const kit = bought.inventory.Items.find(item => item.Template === 85366);
    696   const occupiedAfterSale = new Set(sold.inventory.Items.map(item => item.Slot));
    697   let kitSlot = 64;
    698   while (occupiedAfterSale.has(kitSlot)) ++kitSlot;
    699   assert.deepEqual(kit, {ID: kit.ID, Type: 51005, Template: 85366, Quality: 1, Slot: kitSlot, Count: 25, Ammo: 0});
    700   assert.deepEqual(bought.inventory.Items, [...sold.inventory.Items, kit]);
    701   await owner.page.reload();
    702   await owner.ready();
    703   await owner.key('KeyI');
    704   const kitCell = kit.Slot - 64;
    705   assert(owner.local().inventory.some(item => item.id === kit.ID && item.count === 25));
    706   await owner.page.keyboard.press('Control+8');
    707   await owner.click(927, 96);
    708   await owner.key('F1');
    709   const cast = owner.sequence;
    710   await owner.click(857, 128, 'right');
    711   await owner.wait('ordinary buff recast completed', () => owner.events.some(event => {
    712     if (event.sequence <= cast || event.direction !== 'framereceived' || event.opcode !== '5e477770') return false;
    713     const packet = Buffer.from(event.hex, 'hex');
    714     return packet.readUInt32BE(packet.length - 20) === 107 && packet.readUInt32BE(packet.length - 4) === 43371;
    715   }));
    716   await owner.page.keyboard.press('Control+8');
    717   await owner.click(681 + kitCell % 3 * 54, 45 + Math.floor(kitCell / 3) * 54, 'right');
    718   await owner.save(output + '/untrained-kit-use');
    719   assert.deepEqual((await (await owner.page.request.get(url + '/test-state')).json()).inventory, bought.inventory);
    720   await owner.key('KeyU');
    721   await owner.click(288, 504);
    722   await owner.click(835, 346);
    723   await owner.click(835, 346);
    724   await owner.click(445, 622);
    725   await owner.wait('earned First Aid training', state => owner.local(state).stats[53] === 5032 - rewardTrainingCost);
    726   const kitUse = owner.sequence;
    727   await owner.click(681 + kitCell % 3 * 54, 45 + Math.floor(kitCell / 3) * 54, 'right');
    728   await owner.wait('one bought kit charge and original lock', state =>
    729     owner.local(state).inventory.some(item => item.id === kit.ID && item.count === 24) &&
    730     owner.events.some(event => {
    731       if (event.sequence <= kitUse || event.direction !== 'framereceived' || event.opcode !== '5e477770') return false;
    732       const packet = Buffer.from(event.hex, 'hex');
    733       return packet.readUInt32BE(packet.length - 20) === 170 && packet.readUInt32BE(packet.length - 8) === 123 &&
    734         packet.readUInt32BE(packet.length - 4) === 40;
    735     }));
    736   const kitAck = owner.events.find(event => event.sequence > kitUse && event.direction === 'framereceived' &&
    737     event.opcode === '52526858');
    738   assert(kitAck, 'Bought-kit use did not receive its acknowledgement');
    739   await owner.wait('bought-kit nano restoration', () => owner.events.some(event => {
    740     if (event.sequence <= kitAck.sequence || event.direction !== 'framereceived' || event.opcode !== '2b333d6e') return false;
    741     const packet = Buffer.from(event.hex, 'hex');
    742     return packet.readUInt32BE(packet.length - 8) === 214;
    743   }));
    744   const nanoUpdates = owner.events.filter(event => event.direction === 'framereceived' && event.opcode === '2b333d6e')
    745     .map(event => {
    746       const packet = Buffer.from(event.hex, 'hex');
    747       return {sequence: event.sequence, stat: packet.readUInt32BE(packet.length - 8),
    748         value: packet.readUInt32BE(packet.length - 4)};
    749     }).filter(event => event.stat === 214);
    750   const nanoBefore = nanoUpdates.filter(event => event.sequence < kitAck.sequence).at(-1).value;
    751   const nanoAfter = nanoUpdates.find(event => event.sequence > kitAck.sequence).value;
    752   assert(nanoAfter - nanoBefore >= 10 && nanoAfter - nanoBefore <= 12);
    753   const repeat = owner.sequence;
    754   await owner.click(681 + kitCell % 3 * 54, 45 + Math.floor(kitCell / 3) * 54, 'right');
    755   await owner.wait('locked bought-kit use rejected', () => owner.events.some(event => {
    756     if (event.sequence <= repeat || event.direction !== 'framereceived' || event.opcode !== '52526858') return false;
    757     const packet = Buffer.from(event.hex, 'hex');
    758     return packet.readUInt32BE(packet.length - 32) === 2 && packet.readUInt32BE(packet.length - 4) === kit.Slot - 16;
    759   }));
    760   const used = await (await owner.page.request.get(url + '/test-state')).json();
    761   assert.deepEqual(used.inventory.Items, bought.inventory.Items.map(item => item.ID === kit.ID ? {...item, Count: 24} : item));
    762   assert.equal(used.stats.NanoAiding.FirstAid, 7);
    763   await owner.save(output + '/earned-kit-used');
    764 
    765   // Return along the observed narrow road, checking each arrival before indoor points.
    766   if (!owner.state.camera.firstPerson) await owner.key('F8');
    767   for (const [x, z] of [[187, 124], [187, 116]]) {
    768     await owner.walk(x, z);
    769     if (owner.local().playfield === 705) break;
    770   }
    771   assert.equal(owner.local().playfield, 705);
    772   for (const [x, z] of [[781, 517], [793, 521], [797, 524], [801, 549], [801, 624],
    773     [784, 653], [784, 693], [804, 754], [804, 774], [804, 794], [804, 814], [804, 834],
    774     [804, 854], [804, 865], [825, 872], [831, 872], [830.5, 888], [885, 888], [885, 860],
    775     [900, 860], [920, 843], [938, 843], [945, 843.5], [951, 843.5], [968, 843], [983, 843], [990, 843]]) {
    776     await owner.walk(x, z);
    777     if (owner.local().playfield === 3000) break;
    778   }
    779   assert.equal(owner.local().playfield, 3000);
    780   for (const [x, z] of [[104.3, 150.2], [102.5, 143], [102.5, 102], [109, 102], [112, 101]]) {
    781     await owner.walk(x, z);
    782     if (owner.local().playfield === 950) break;
    783   }
    784   assert.equal(owner.local().playfield, 950);
    785   for (const [x, z] of [[62.8, 65], [63, 75], [85, 75], [120, 75], [160, 70], [176.3, 63.2]])
    786     await owner.walk(x, z);
    787   fights.push(await outdoorFight(owner, 2, 'authored-level-two'));
    788   assert.equal(fights.at(-1).fightStart.enemy.level, 2);
    789   credits += fights.at(-1).corpse.credits;
    790 
    791   // Account for every issued identity, consumed crystal, charge and generated item.
    792   const combatUses = fights.reduce((total, fight) => total + fight.combatUses, 0);
    793   const expected = before.Items.filter(item => item.ID !== crystal.id &&
    794     (item.Template !== 116628 || trainingUses + combatUses + levelTrainingUses < 25)).map(item => {
    795     if (item.ID === displaced?.ID) return displaced;
    796     if (item.ID === bat.id) return {...item, Slot: 6};
    797     if (item.ID === shirt.id) return {...item, Slot: 21};
    798     if (item.Template === 116628) return {...item, Count: 25 - trainingUses - combatUses - levelTrainingUses};
    799     return item;
    800   });
    801   for (const fight of fights) expected.push(...fight.loot.filter(item => item.ID !== sale.ID));
    802   expected.push(wornReward, {...kit, Count: 24});
    803   expected.sort((a, b) => a.ID - b.ID);
    804   const final = await (await owner.page.request.get(url + '/test-state')).json();
    805   assert.deepEqual(final.inventory.Items, expected);
    806   assert.equal(final.inventory.Credits, credits);
    807   assert.equal(final.level, 2);
    808   assert.equal(final.experience, fights.length * 100 - 1450);
    809   assert.equal(final.stats.ImprovementPoints, 5032 - rewardTrainingCost);
    810   assert.equal(final.health, 111);
    811   assert.deepEqual(final.inventory.LearnedNanos, [43371]);
    812   await fs.writeFile(output + '/ledger.json', JSON.stringify({issued: before, trained: trained.stats,
    813     fights, mission, advertised, rewards, wornReward, displaced, rewardTrainingCost, sale, kit,
    814     kitNano: {before: nanoBefore, after: nanoAfter},
    815     trainingUses, combatUses, levelTrainingUses, stats: final.stats, level: final.level,
    816     experience: final.experience, inventory: final.inventory, health: final.health}, null, 2) + '\n');
    817   const reconnectPose = {playfield: owner.local().playfield, position: owner.local().position};
    818   await owner.page.reload();
    819   await owner.ready();
    820   assert.equal(owner.local().playfield, reconnectPose.playfield);
    821   assert(Math.hypot(owner.local().position[0] - reconnectPose.position[0],
    822     owner.local().position[1] - reconnectPose.position[1], owner.local().position[2] - reconnectPose.position[2]) < 0.5,
    823     'Page reconnect changed the stopped native position');
    824   assert.deepEqual(owner.local().inventory.map(item => item.id).sort(), expected.map(item => item.ID).sort());
    825   for (const item of expected) {
    826     const actual = owner.local().inventory.find(actual => actual.id === item.ID);
    827     assert.equal(actual.type, item.Type);
    828     assert.equal(actual.slot, item.Slot >= 64 ? item.Slot - 16 : item.Slot);
    829     assert.equal(actual.count, item.Count);
    830     assert.equal(actual.quality, item.Quality);
    831     assert.deepEqual(actual.templates, item.Template === 42640 && item.Quality > 1
    832       ? [42640, 42641] : [item.Template, item.Template]);
    833   }
    834   await owner.key('KeyI');
    835   await owner.page.keyboard.press('Control+1');
    836   await owner.snapshot();
    837   await owner.save(output + '/final-weapon');
    838   await owner.click(953, 53);
    839   await owner.page.keyboard.press('Control+9');
    840   await owner.snapshot();
    841   await observer.snapshot();
    842   assert.deepEqual(observer.local().inventory, watched);
    843   assert.equal(observer.local().stats[52], 0);
    844   assert.equal(observer.local().stats[53], 1500);
    845   assert.equal(observer.local().stats[61], 0);
    846   for (const game of games) assert.deepEqual(game.errors, []);
    847   console.log('PASS: ordinary starter level, earned reward wear, funded kit, next-level kill and page ownership');
    848 } finally {
    849   try {
    850     for (const [index, game] of games.entries()) await game.save(output + '/client-' + index);
    851   } finally {
    852     await browser.close();
    853   }
    854 }