Skip to content

Commit

Permalink
feat: d2r improvements (#332)
Browse files Browse the repository at this point in the history
* feat(memory): remove d2:classic support focus on d2r

* feat(memory): read units

* build: binparse 2.1

* feat: more d2r struts

* refactor: cleanup lint

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
blacha and kodiakhq[bot] authored May 9, 2022
1 parent 266557b commit c2cc311
Show file tree
Hide file tree
Showing 34 changed files with 1,058 additions and 375 deletions.
2 changes: 1 addition & 1 deletion packages/bintools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@diablo2/data": "^0.9.0",
"@diablo2/mpq": "^0.9.0",
"binparse": "^2.0.1",
"binparse": "^2.1.0",
"ulid": "^2.3.0"
},
"publishConfig": {
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/game.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ export class Diablo2GameSession {
this.log.warn({ code: pkt.code, x: pkt.x, y: pkt.y, item: pkt.name, action: pkt.action.name }, 'ItemDropped');

const trackItem: Diablo2ItemJson = {
type: 'item',
id: pkt.id,
code: pkt.code,
x: pkt.x,
y: pkt.y,
category: pkt.category,
quality: pkt.quality,
level: pkt.level,
updatedAt: Date.now(),
name: pkt.name ?? 'Unknown',
};
Expand All @@ -126,17 +125,17 @@ export class Diablo2GameSession {
});

// Handle NPC Movement
this.parser.on(server.NpcMove, (pkt) => this.state.moveNpc(pkt, pkt.unitId, pkt.x, pkt.y));
this.parser.on(server.NpcAttack, (pkt) => this.state.moveNpc(pkt, pkt.unitId, pkt.x, pkt.y));
this.parser.on(server.NpcMoveToTarget, (pkt) => this.state.moveNpc(pkt, pkt.unitId, pkt.x, pkt.y));
this.parser.on(server.NpcStop, (pkt) => this.state.moveNpc(pkt, pkt.unitId, pkt.x, pkt.y, pkt.life));
this.parser.on(server.NpcMove, (pkt) => this.state.moveNpc(pkt.unitId, pkt.x, pkt.y));
this.parser.on(server.NpcAttack, (pkt) => this.state.moveNpc(pkt.unitId, pkt.x, pkt.y));
this.parser.on(server.NpcMoveToTarget, (pkt) => this.state.moveNpc(pkt.unitId, pkt.x, pkt.y));
this.parser.on(server.NpcStop, (pkt) => this.state.moveNpc(pkt.unitId, pkt.x, pkt.y, pkt.life));

this.parser.on(server.NpcUpdate, (pkt) => {
// TODO why are state 8 & 9 = dead?
if (pkt.state === 0x09 || pkt.state === 0x08) pkt.life = 0;

// console.log('NpcUpdate', pkt.unitId, pkt.unitLife);
this.state.moveNpc(pkt, pkt.unitId, pkt.x, pkt.y, pkt.life);
this.state.moveNpc(pkt.unitId, pkt.x, pkt.y, pkt.life);
});

// this.parser.on(server.NpcAction, (pkt) => {
Expand All @@ -158,7 +157,7 @@ export class Diablo2GameSession {
x: pkt.x,
y: pkt.y,
code: pkt.code,
flags: pkt.flags ?? {},
isNormal: true,
updatedAt: Date.now(),
enchants: pkt.enchants ?? [],
});
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/state/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ export class Diablo2State {

trackXp(num: number, isSet = false): void {
const xp = this.player.xp;
const startXp = xp.current;
if (xp.current === -1 || isSet) xp.current = num;
else xp.current += num;
if (xp.start === -1) xp.start = xp.current;
if (startXp === xp.current) return;

xp.diff = xp.current - xp.start;
this.dirty();
Expand Down Expand Up @@ -159,6 +161,7 @@ export class Diablo2State {

trackKill(u: GameJson.Diablo2NpcJson): void {
if (u.code === -1) return;
if (u.type !== 'npc') return;
let kill = this.kills.get(u.code);
if (kill == null) {
kill = {
Expand All @@ -169,11 +172,11 @@ export class Diablo2State {
this.kills.set(u.code, kill);
}
kill.total += 1;
if (u.flags.isMinion) kill.isMinion = (kill.isMinion || 0) + 1;
if (u.flags.isChampion) kill.isChampion = (kill.isChampion || 0) + 1;
if (u.flags.isSuperUnique) kill.isSuperUnique = (kill.isSuperUnique || 0) + 1;
if (u.flags.isGhostly) kill.isGhostly = (kill.isGhostly || 0) + 1;
if (u.flags.isSuperUnique) {
if (u.isMinion) kill.isMinion = (kill.isMinion || 0) + 1;
if (u.isChampion) kill.isChampion = (kill.isChampion || 0) + 1;
if (u.isSuperUnique) kill.isSuperUnique = (kill.isSuperUnique || 0) + 1;
if (u.isGhostly) kill.isGhostly = (kill.isGhostly || 0) + 1;
if (u.isSuperUnique) {
kill.special = kill.special ?? [];
kill.special.push(u.name);
return;
Expand All @@ -184,7 +187,7 @@ export class Diablo2State {
}
}

moveNpc(pkt: Diablo2ParsedPacket<unknown>, id: number, x: number, y: number, life = -1): void {
moveNpc(id: number, x: number, y: number, life = -1): void {
if (x === 0 || y === 0) return;
let unit = this.units.get(id);

Expand All @@ -207,10 +210,11 @@ export class Diablo2State {
code: -1,
name: 'Unknown',
enchants: [],
flags: {},
isNormal: true,
};
this.units.set(id, unit);
}
if (unit.x === x && unit.y === y && unit.life === life) return;
unit.x = x;
unit.y = y;
unit.life = life;
Expand Down Expand Up @@ -255,7 +259,7 @@ export class Diablo2State {
player: this.player,
map: this.map,
objects: [...this.objects.values()],
units: [...this.units.values()],
units: [...this.units.values()].sort((a, b) => a.id - b.id),
items: [...this.items.values()].sort(latestUpdated).slice(0, 25),
kills: [...this.kills.values()],
};
Expand Down
25 changes: 25 additions & 0 deletions packages/data/src/__test__/npc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import o from 'ospec';
import { getNpcFlags } from '../npc.js';

o.spec('NpcFlags', () => {
o('should ignore weird values', () => {
o(getNpcFlags(-1)).deepEquals({ isNormal: true });
o(getNpcFlags(372)).deepEquals({ isNormal: true });
});

o('should get all the flags', () => {
o(getNpcFlags(0b11111111)).deepEquals({
isNormal: false,
isSuperUnique: true,
isChampion: true,
isUnique: true,
isMinion: true,
isPossessed: true,
isGhostly: true,
});
});

o('should get some flags', () => {
o(getNpcFlags(0b1010101)).deepEquals({ isNormal: false, isChampion: true, isMinion: true, isGhostly: true });
});
});
Loading

0 comments on commit c2cc311

Please sign in to comment.