Compare commits
5 Commits
59435527b0
...
45aa95ce2d
| Author | SHA1 | Date |
|---|---|---|
|
|
45aa95ce2d | |
|
|
7ec054a917 | |
|
|
71610311fe | |
|
|
47d18fce72 | |
|
|
102d001de1 |
|
|
@ -288,6 +288,25 @@ export class AutomapView {
|
|||
return this.levelViews.get(areaId)!
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch the active level (`currentAreaId` / `currentActId`) within the current game session
|
||||
* without clearing `levelViews` or `serverRevealedRooms` (#549).
|
||||
*/
|
||||
syncActiveLevel(areaId: number, existingLevelView?: LevelView): LevelView {
|
||||
if (areaId > 0) {
|
||||
this.currentAreaId = areaId
|
||||
this.currentActId = getActForArea(areaId)
|
||||
}
|
||||
const targetAreaId = this.currentAreaId
|
||||
if (existingLevelView && existingLevelView.areaId === targetAreaId) {
|
||||
this.levelViews.set(targetAreaId, existingLevelView)
|
||||
} else if (!this.levelViews.has(targetAreaId)) {
|
||||
this.ensureLevelView(targetAreaId)
|
||||
}
|
||||
this.applyRevealModeToViews()
|
||||
return this.levelViews.get(targetAreaId)!
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a server `0x07 AddMapRoom` event.
|
||||
*/
|
||||
|
|
@ -498,36 +517,62 @@ export class AutomapView {
|
|||
/**
|
||||
* Build a `MinimapLevel` descriptor suitable for `Minimap.draw(...)` (`src/ui/minimap.ts`).
|
||||
*/
|
||||
buildMinimapLevel(lang: TblLang = getTblLang()): MinimapLevel {
|
||||
buildMinimapLevel(lang: TblLang = getTblLang(), areaId?: number): MinimapLevel {
|
||||
if (areaId !== undefined && areaId > 0 && areaId !== this.currentAreaId) {
|
||||
this.syncActiveLevel(areaId)
|
||||
}
|
||||
const snap = this.buildSnapshot([], lang)
|
||||
const view = this.ensureLevelView(this.currentAreaId)
|
||||
const allRooms = view.rooms
|
||||
|
||||
let minCellX = 0
|
||||
let minCellY = 0
|
||||
let maxCellX = 40
|
||||
let maxCellY = 40
|
||||
if (allRooms.length > 0) {
|
||||
minCellX = Math.min(...allRooms.map(r => r.tileX))
|
||||
minCellY = Math.min(...allRooms.map(r => r.tileY))
|
||||
maxCellX = Math.max(...allRooms.map(r => r.tileX + r.tileW))
|
||||
maxCellY = Math.max(...allRooms.map(r => r.tileY + r.tileH))
|
||||
}
|
||||
let cellsX = 40
|
||||
let cellsY = 40
|
||||
let subW = 200
|
||||
let blocked: Uint8Array
|
||||
let originX = 0
|
||||
let originY = 0
|
||||
|
||||
const cellsX = Math.max(1, maxCellX - minCellX)
|
||||
const cellsY = Math.max(1, maxCellY - minCellY)
|
||||
const subW = cellsX * 5
|
||||
const subH = cellsY * 5
|
||||
const blocked = new Uint8Array(subW * subH)
|
||||
|
||||
for (let sy = 0; sy < subH; sy++) {
|
||||
for (let sx = 0; sx < subW; sx++) {
|
||||
const absSubX = minCellX * 5 + sx
|
||||
const absSubY = minCellY * 5 + sy
|
||||
if (!isWalkableWorldSubTile(view, absSubX, absSubY)) {
|
||||
blocked[sy * subW + sx] = 1
|
||||
if (
|
||||
view.cellsX > 0 &&
|
||||
view.cellsY > 0 &&
|
||||
view.blocked &&
|
||||
view.blocked.length === view.cellsX * 5 * view.cellsY * 5
|
||||
) {
|
||||
minCellX = view.tileOriginX ?? Math.floor((view.subOriginX ?? 0) / 5)
|
||||
minCellY = view.tileOriginY ?? Math.floor((view.subOriginY ?? 0) / 5)
|
||||
cellsX = view.cellsX
|
||||
cellsY = view.cellsY
|
||||
subW = view.gridWidth || cellsX * 5
|
||||
blocked = view.blocked
|
||||
originX = view.isoOriginX ?? (minCellX - minCellY) * 80
|
||||
originY = view.isoOriginY ?? (minCellX + minCellY) * 40
|
||||
} else {
|
||||
let maxCellX = 40
|
||||
let maxCellY = 40
|
||||
if (allRooms.length > 0) {
|
||||
minCellX = Math.min(...allRooms.map(r => r.tileX))
|
||||
minCellY = Math.min(...allRooms.map(r => r.tileY))
|
||||
maxCellX = Math.max(...allRooms.map(r => r.tileX + r.tileW))
|
||||
maxCellY = Math.max(...allRooms.map(r => r.tileY + r.tileH))
|
||||
}
|
||||
cellsX = Math.max(1, maxCellX - minCellX)
|
||||
cellsY = Math.max(1, maxCellY - minCellY)
|
||||
subW = cellsX * 5
|
||||
const subH = cellsY * 5
|
||||
blocked = new Uint8Array(subW * subH)
|
||||
for (let sy = 0; sy < subH; sy++) {
|
||||
for (let sx = 0; sx < subW; sx++) {
|
||||
const absSubX = minCellX * 5 + sx
|
||||
const absSubY = minCellY * 5 + sy
|
||||
if (!isWalkableWorldSubTile(view, absSubX, absSubY)) {
|
||||
blocked[sy * subW + sx] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
originX = (minCellX - minCellY) * 80
|
||||
originY = (minCellX + minCellY) * 40
|
||||
}
|
||||
|
||||
const markers: MinimapMarker[] = snap.warpMarkers.map(wm => ({
|
||||
|
|
@ -538,6 +583,20 @@ export class AutomapView {
|
|||
targetName: wm.label,
|
||||
}))
|
||||
|
||||
if (Array.isArray(view.entrances)) {
|
||||
for (const ent of view.entrances) {
|
||||
if (ent.toLevelId > 0 && ent.toLevelId !== this.currentAreaId) {
|
||||
markers.push({
|
||||
cellX: ent.x,
|
||||
cellY: ent.y,
|
||||
kind: 'exit',
|
||||
toLevelId: ent.toLevelId,
|
||||
targetName: levelDisplayName(ent.toLevelId, lang),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
key: `automap_${this.mapSeed}_${this.difficulty}_${this.currentAreaId}_${this.mode}`,
|
||||
cellsX,
|
||||
|
|
@ -545,8 +604,8 @@ export class AutomapView {
|
|||
blocked,
|
||||
gridWidth: subW,
|
||||
markers,
|
||||
originX: (minCellX - minCellY) * 80,
|
||||
originY: (minCellX + minCellY) * 40,
|
||||
originX,
|
||||
originY,
|
||||
levelId: this.currentAreaId,
|
||||
levelName: levelDisplayName(this.currentAreaId, lang),
|
||||
...(view.automap !== undefined ? { automap: view.automap } : {}),
|
||||
|
|
|
|||
|
|
@ -897,13 +897,22 @@ export class CommandMapper {
|
|||
return this.lastPath
|
||||
}
|
||||
|
||||
const snap = this.predictor.getSnapshot()
|
||||
const sx = Math.round(snap.logicalX)
|
||||
const sy = Math.round(snap.logicalY)
|
||||
const levelView =
|
||||
this.mapService?.getActiveLevelView() ?? this.automapView?.getLevelView() ?? null
|
||||
(this.mapService && this.world.areaId > 0 && (sx !== 0 || sy !== 0)
|
||||
? this.mapService.findLevelViewAtWorldSubTile(sx, sy, [
|
||||
this.world.areaId,
|
||||
...this.world.getRevealedAreaIds(),
|
||||
])
|
||||
: null) ??
|
||||
(this.world.areaId > 0 ? this.automapView?.getLevelView(this.world.areaId) : undefined) ??
|
||||
this.mapService?.getActiveLevelView() ??
|
||||
this.automapView?.getLevelView() ??
|
||||
null
|
||||
|
||||
if (levelView && levelView.walkable.length > 0) {
|
||||
const snap = this.predictor.getSnapshot()
|
||||
const sx = Math.round(snap.logicalX)
|
||||
const sy = Math.round(snap.logicalY)
|
||||
const ox = levelView.subOriginX ?? 0
|
||||
const oy = levelView.subOriginY ?? 0
|
||||
const inBounds =
|
||||
|
|
|
|||
|
|
@ -958,7 +958,6 @@ async function bootPlayApp(): Promise<PlayAppController> {
|
|||
automapView: session.automapView,
|
||||
hudManager: session.hudManager,
|
||||
nowMs,
|
||||
playerMoving: session.predictor.getSnapshot().moving,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { dir64ToClipDirection } from '../../common/units/direction.ts'
|
||||
import { PLR_MODES } from '../../common/units/plr-mode.ts'
|
||||
import { ADDITIVE_MONSTER_TOKENS, type LoadedMonsterArt } from '../render/monster-art.ts'
|
||||
import { npcDisplayName } from '../../common/units/npc-table.ts'
|
||||
import type { AtlasHandle, SpriteRenderer } from '../render/renderer.ts'
|
||||
|
|
@ -175,19 +176,41 @@ const COMMON_HCIDX_TO_MONSTER_ID: Readonly<Record<number, string>> = {
|
|||
520: 'cain5',
|
||||
}
|
||||
|
||||
const PLAYER_MODE_TO_CLIP: Readonly<Record<number, string>> = {
|
||||
0: 'dt',
|
||||
1: 'nu',
|
||||
2: 'wl',
|
||||
3: 'rn',
|
||||
4: 'gh',
|
||||
5: 'tn',
|
||||
6: 'tw',
|
||||
7: 'a1',
|
||||
8: 'a2',
|
||||
9: 'bl',
|
||||
10: 'sc',
|
||||
17: 'dd',
|
||||
/**
|
||||
* Secondary `PlrMode.txt` tokens that share HTH DCC sequences in `d2char.mpq` when no dedicated
|
||||
* HTH DCC file exists for that mode (`KB`/`BL` -> `gh`, `TH`/`KK` -> `a1`, `S1..S4`/`SQ` -> `sc`).
|
||||
* Primary modes (`dt`, `nu`, `wl`, `rn`, `gh`, `tn`, `tw`, `a1`, `a2`, `sc`, `dd`) never alias.
|
||||
*/
|
||||
const PLAYER_HTH_FALLBACK_CLIP: Readonly<Record<string, string>> = {
|
||||
bl: 'gh',
|
||||
th: 'a1',
|
||||
kk: 'a1',
|
||||
s1: 'sc',
|
||||
s2: 'sc',
|
||||
s3: 'sc',
|
||||
s4: 'sc',
|
||||
sq: 'sc',
|
||||
kb: 'gh',
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute 0-based animation frame index from elapsed time `nowMs` and 1.13c 8.8 fixed-point
|
||||
* animation speed (`speed256`, where `256 = 1.0` frame per 25Hz / 40ms game tick;
|
||||
* `D2Common.dll` `0x6FDBE510` / `0x6FDBEFF0`).
|
||||
*/
|
||||
export function computeAnimDataFrameIndex(
|
||||
nowMs: number,
|
||||
speed256: number,
|
||||
frameCount: number,
|
||||
loop = true,
|
||||
): number {
|
||||
if (frameCount <= 1) return 0
|
||||
const safeTicks = Math.max(0, nowMs) / 40
|
||||
const rawFrame = Math.floor((safeTicks * speed256) / 256)
|
||||
if (!loop) {
|
||||
return Math.min(frameCount - 1, Math.max(0, rawFrame))
|
||||
}
|
||||
return ((rawFrame % frameCount) + frameCount) % frameCount
|
||||
}
|
||||
|
||||
const MONSTER_MODE_TO_CLIP: Readonly<Record<number, string>> = {
|
||||
|
|
@ -345,7 +368,6 @@ export interface SceneSourceRenderContext {
|
|||
readonly automapView?: AutomapView | null | undefined
|
||||
readonly hudManager?: HudManager | null | undefined
|
||||
readonly nowMs?: number | undefined
|
||||
readonly playerMoving?: boolean | undefined
|
||||
readonly onFrameSnapshot?: ((snapshot: SceneFrameSnapshot) => void) | undefined
|
||||
}
|
||||
|
||||
|
|
@ -407,32 +429,42 @@ function drawSceneUnitDrawable(
|
|||
ctx.characterArt ??
|
||||
null
|
||||
if (charArt !== null) {
|
||||
const isMoving =
|
||||
Boolean(ctx.playerMoving) || unit.mode === 2 || unit.mode === 3 || unit.mode === 6
|
||||
const requestedClipKey =
|
||||
Boolean(ctx.playerMoving) && (unit.mode === 1 || unit.mode === 5)
|
||||
? unit.mode === 5 && charArt.clips?.tw
|
||||
? 'tw'
|
||||
: 'wl'
|
||||
: (PLAYER_MODE_TO_CLIP[unit.mode] ?? (isMoving ? 'wl' : 'nu'))
|
||||
const modeToken = PLR_MODES[unit.mode]?.token.toLowerCase()
|
||||
const requestedClipKey = unit.isCorpse && modeToken !== 'dt' ? 'dd' : modeToken
|
||||
if (!requestedClipKey) {
|
||||
reportMissingArt(
|
||||
'player-mode',
|
||||
`${unit.token}:${unit.mode}`,
|
||||
`Invalid player mode=${unit.mode} for token=${unit.token}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const fallbackHthKey = PLAYER_HTH_FALLBACK_CLIP[requestedClipKey]
|
||||
const clip =
|
||||
charArt.clips?.[requestedClipKey] ??
|
||||
(isMoving ? charArt.clips?.wl : charArt.clips?.nu)
|
||||
const dirCount = Math.max(1, clip?.directions ?? charArt.directions)
|
||||
(fallbackHthKey !== undefined ? charArt.clips?.[fallbackHthKey] : undefined)
|
||||
if (!clip || !(clip.speed > 0)) {
|
||||
reportMissingArt(
|
||||
'player-clip',
|
||||
`${unit.token}:${requestedClipKey}`,
|
||||
`Missing player clip or AnimData.d2 speed for token=${unit.token} clip=${requestedClipKey} mode=${unit.mode}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const dirCount = Math.max(1, clip.directions || charArt.directions)
|
||||
const direction = dir64ToClipDirection(unit.direction, dirCount)
|
||||
const baseGroup = clip !== undefined ? clip.group : isMoving ? charArt.walk : charArt.stand
|
||||
const groupIndex = baseGroup + direction
|
||||
const group =
|
||||
charArt.groups[groupIndex] ??
|
||||
charArt.groups[charArt.stand + direction] ??
|
||||
charArt.groups[0]
|
||||
const groupIndex = clip.group + direction
|
||||
const group = charArt.groups[groupIndex]
|
||||
const frameCount = Math.max(1, group?.length ?? 1)
|
||||
const shouldLoop =
|
||||
!unit.isCorpse &&
|
||||
requestedClipKey !== 'dd' &&
|
||||
requestedClipKey !== 'dt' &&
|
||||
clip.loop !== false
|
||||
const frameIndex =
|
||||
unit.isCorpse || requestedClipKey === 'dd'
|
||||
unit.isCorpse && requestedClipKey !== 'dt'
|
||||
? frameCount - 1
|
||||
: isMoving
|
||||
? Math.floor(nowMs / 65) % frameCount
|
||||
: Math.floor(nowMs / 140) % frameCount
|
||||
: computeAnimDataFrameIndex(nowMs, clip.speed, frameCount, shouldLoop)
|
||||
const frame = group?.[frameIndex] ?? group?.[0]
|
||||
if (frame !== undefined) {
|
||||
const drawX =
|
||||
|
|
@ -473,31 +505,43 @@ function drawSceneUnitDrawable(
|
|||
art = resolveOnlineMonsterArt(unit, ctx.monsterArtMap)
|
||||
}
|
||||
if (art !== undefined) {
|
||||
const isMoving = unit.mode === 2 || unit.mode === 13 || unit.mode === 15
|
||||
const isMoving = unit.mode === 2 || unit.mode === 15
|
||||
const requestedClipKey = unit.isCorpse
|
||||
? art.clips?.dd
|
||||
? 'dd'
|
||||
: art.clips?.dt
|
||||
? 'dt'
|
||||
: 'nu'
|
||||
: (MONSTER_MODE_TO_CLIP[unit.mode || 1] ?? (isMoving ? 'wl' : 'nu'))
|
||||
? unit.mode === 0 && art.clips?.dt
|
||||
? 'dt'
|
||||
: art.clips?.dd
|
||||
? 'dd'
|
||||
: art.clips?.dt
|
||||
? 'dt'
|
||||
: 'nu'
|
||||
: (MONSTER_MODE_TO_CLIP[unit.mode] ?? (isMoving ? 'wl' : 'nu'))
|
||||
const clip =
|
||||
art.clips?.[requestedClipKey] ??
|
||||
(isMoving ? art.clips?.wl : undefined) ??
|
||||
art.clips?.nu
|
||||
const dirCount = Math.max(1, clip?.directions ?? art.directions)
|
||||
if (!clip || !(clip.speed > 0)) {
|
||||
reportMissingArt(
|
||||
'monster-clip',
|
||||
`${unit.classId}:${requestedClipKey}`,
|
||||
`Missing monster clip or AnimData.d2 speed for classId=${unit.classId} token=${unit.token} clip=${requestedClipKey}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const dirCount = Math.max(1, clip.directions || art.directions)
|
||||
const direction = dir64ToClipDirection(unit.direction, dirCount)
|
||||
const baseGroup =
|
||||
clip !== undefined ? clip.group : isMoving ? art.walkOffset : art.standOffset
|
||||
const baseGroup = clip.group
|
||||
const groupIndex = baseGroup + direction
|
||||
const group = art.groups[groupIndex] ?? art.groups[art.standOffset + direction] ?? art.groups[0]
|
||||
const frameCount = Math.max(1, group?.length ?? 1)
|
||||
const shouldLoop =
|
||||
!unit.isCorpse &&
|
||||
requestedClipKey !== 'dd' &&
|
||||
requestedClipKey !== 'dt' &&
|
||||
clip.loop !== false
|
||||
const frameIndex =
|
||||
unit.isCorpse || requestedClipKey === 'dd'
|
||||
unit.isCorpse && requestedClipKey !== 'dt'
|
||||
? frameCount - 1
|
||||
: isMoving
|
||||
? Math.floor(nowMs / 75) % frameCount
|
||||
: Math.floor(nowMs / 130) % frameCount
|
||||
: computeAnimDataFrameIndex(nowMs, clip.speed, frameCount, shouldLoop)
|
||||
const frame = group?.[frameIndex] ?? group?.[0]
|
||||
if (frame !== undefined) {
|
||||
const drawX =
|
||||
|
|
@ -682,9 +726,16 @@ function drawSceneUnitDrawable(
|
|||
if (!shouldLoop) {
|
||||
frameIndex = frameCount - 1
|
||||
} else {
|
||||
const speed = objArt.frameDelta?.[modeIdx] || clip?.speed || 128
|
||||
const frameDurationMs = speed > 0 ? Math.max(40, Math.round((256 * 40) / speed)) : 100
|
||||
frameIndex = Math.abs(Math.floor(nowMs / frameDurationMs)) % frameCount
|
||||
const speed = objArt.frameDelta?.[modeIdx] || clip?.speed || 0
|
||||
if (!(speed > 0)) {
|
||||
reportMissingArt(
|
||||
'object-clip',
|
||||
`${unit.classId}:${clipKey}`,
|
||||
`Missing frameDelta/speed for object classId=${unit.classId} mode=${modeIdx}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
frameIndex = computeAnimDataFrameIndex(nowMs, speed, frameCount, true)
|
||||
}
|
||||
}
|
||||
const frame = group?.[frameIndex] ?? group?.[0]
|
||||
|
|
@ -738,11 +789,22 @@ function drawSceneMissileDrawable(
|
|||
ctx.missileArtMap?.get(missile.missileKey) ??
|
||||
ctx.missileArtMap?.get(missile.celFile.toLowerCase())
|
||||
if (art !== undefined) {
|
||||
const animSpeed = art.meta.animSpeed
|
||||
if (!(animSpeed > 0)) {
|
||||
reportMissingArt(
|
||||
'missile-speed',
|
||||
missile.missileKey || missile.celFile || String(missile.missileClassId),
|
||||
`Missing animSpeed for missile key=${missile.missileKey}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const dirCount = Math.max(1, art.meta.directions)
|
||||
const dir = ((missile.direction % dirCount) + dirCount) % dirCount
|
||||
const group = art.frames[dir] ?? art.frames[0]
|
||||
const frameCount = Math.max(1, group?.length ?? 1)
|
||||
const frameIdx = (missile.frame + Math.floor(nowMs / 50)) % frameCount
|
||||
const frameIdx =
|
||||
(missile.frame + computeAnimDataFrameIndex(nowMs, animSpeed * 16, frameCount, true)) %
|
||||
frameCount
|
||||
const frame = group?.[frameIdx] ?? group?.[0]
|
||||
if (frame !== undefined) {
|
||||
const drawX =
|
||||
|
|
@ -777,9 +839,20 @@ function drawSceneOverlayDrawable(
|
|||
ctx.overlayArtMap?.get(overlay.token) ??
|
||||
ctx.overlayArtMap?.get(overlay.token.toLowerCase())
|
||||
if (art !== undefined) {
|
||||
const animRate = art.meta.animRate
|
||||
if (!(animRate > 0)) {
|
||||
reportMissingArt(
|
||||
'overlay-speed',
|
||||
overlay.token || String(overlay.overlayId),
|
||||
`Missing animRate for overlay token=${overlay.token}`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const group = art.frames[0]
|
||||
const frameCount = Math.max(1, group?.length ?? 1)
|
||||
const frameIdx = (overlay.frame + Math.floor(nowMs / 60)) % frameCount
|
||||
const frameIdx =
|
||||
(overlay.frame + computeAnimDataFrameIndex(nowMs, animRate * 16, frameCount, true)) %
|
||||
frameCount
|
||||
const frame = group?.[frameIdx] ?? group?.[0]
|
||||
if (frame !== undefined) {
|
||||
const drawX =
|
||||
|
|
@ -826,7 +899,7 @@ export function renderSceneSourceFrame(ctx: SceneSourceRenderContext): SceneFram
|
|||
renderer.flush()
|
||||
}
|
||||
if (ctx.hudManager) {
|
||||
ctx.hudManager.render(nowMs, ctx.playerMoving ?? false)
|
||||
ctx.hudManager.render(nowMs)
|
||||
}
|
||||
ctx.onFrameSnapshot?.(snapshot)
|
||||
return snapshot
|
||||
|
|
@ -927,11 +1000,19 @@ export function renderSceneSourceFrame(ctx: SceneSourceRenderContext): SceneFram
|
|||
ctx.minimap.visible = false
|
||||
} else {
|
||||
ctx.minimap.visible = true
|
||||
const baseMinimapLevel = ctx.automapView.buildMinimapLevel()
|
||||
if (levelView && levelView.areaId > 0) {
|
||||
ctx.automapView.syncActiveLevel(levelView.areaId, levelView)
|
||||
} else if (snapshot.levelId > 0 && ctx.automapView.getCurrentAreaId() !== snapshot.levelId) {
|
||||
ctx.automapView.syncActiveLevel(snapshot.levelId)
|
||||
}
|
||||
const baseMinimapLevel = ctx.automapView.buildMinimapLevel(
|
||||
undefined,
|
||||
levelView?.areaId ?? snapshot.levelId,
|
||||
)
|
||||
const effectiveMinimapLevel: MinimapLevel = {
|
||||
...baseMinimapLevel,
|
||||
originX: levelView?.isoOriginX ?? 0,
|
||||
originY: levelView?.isoOriginY ?? 0,
|
||||
originX: levelView?.isoOriginX ?? baseMinimapLevel.originX ?? 0,
|
||||
originY: levelView?.isoOriginY ?? baseMinimapLevel.originY ?? 0,
|
||||
preRevealTown: isTownLevel(snapshot.levelId),
|
||||
}
|
||||
const localSub = snapshot.playerLocalSubTile
|
||||
|
|
@ -1011,7 +1092,7 @@ export function renderSceneSourceFrame(ctx: SceneSourceRenderContext): SceneFram
|
|||
}
|
||||
|
||||
if (ctx.hudManager) {
|
||||
ctx.hudManager.render(nowMs, ctx.playerMoving ?? false)
|
||||
ctx.hudManager.render(nowMs)
|
||||
}
|
||||
ctx.onFrameSnapshot?.(snapshot)
|
||||
return snapshot
|
||||
|
|
|
|||
|
|
@ -270,6 +270,7 @@ export class OnlineSession {
|
|||
private disconnectReasonValue: string | null = null
|
||||
private pingMsValue = 0
|
||||
private tickCounter = 0
|
||||
private readonly knownAreaIds = new Set<number>()
|
||||
|
||||
private readonly outboundCommandsLog: ClientCommand[] = []
|
||||
private readonly stateListeners = new Set<(snapshot: OnlineSessionLobbySnapshot) => void>()
|
||||
|
|
@ -373,17 +374,11 @@ export class OnlineSession {
|
|||
// such as Blood Moor as a side effect of 0x07). The rendered level is the one whose DRLG
|
||||
// canvas contains the player's world sub-tile (1.13c: unit → pRoom → pLevel); until such a
|
||||
// level exists the scene reports not-ready.
|
||||
// #549: resolving the active levelView also keeps `world.areaId` and `automapView` in lockstep.
|
||||
const predSnap = this.predictor.getSnapshot()
|
||||
const playerSubX = predSnap.x !== 0 || predSnap.y !== 0 ? predSnap.x : this.world.self.x
|
||||
const playerSubY = predSnap.x !== 0 || predSnap.y !== 0 ? predSnap.y : this.world.self.y
|
||||
const levelView =
|
||||
this.levelView ??
|
||||
(this.mapService && this.world.areaId > 0 && (playerSubX !== 0 || playerSubY !== 0)
|
||||
? this.mapService.findLevelViewAtWorldSubTile(playerSubX, playerSubY, [
|
||||
this.world.areaId,
|
||||
...this.world.getRevealedAreaIds(),
|
||||
])
|
||||
: null)
|
||||
const levelView = this.syncActiveAreaFromPlayerPosition({ emitHudSync: true })
|
||||
const levelId = levelView?.areaId ?? (this.world.areaId > 0 ? this.world.areaId : 0)
|
||||
return {
|
||||
tick: this.tickCounter,
|
||||
|
|
@ -694,6 +689,7 @@ export class OnlineSession {
|
|||
const savedName = this.world.self.name
|
||||
const savedClass = this.world.self.charClass
|
||||
this.world.reset()
|
||||
this.knownAreaIds.clear()
|
||||
this.world.self.name = savedName
|
||||
this.world.self.charClass = savedClass
|
||||
this.predictor.setPosition(0, 0)
|
||||
|
|
@ -921,6 +917,66 @@ export class OnlineSession {
|
|||
}
|
||||
}
|
||||
|
||||
private getCandidateAreaIds(): number[] {
|
||||
const out: number[] = []
|
||||
if (this.world.areaId > 0) {
|
||||
out.push(this.world.areaId)
|
||||
}
|
||||
for (const id of this.world.getRevealedAreaIds()) {
|
||||
if (id > 0 && !out.includes(id)) {
|
||||
out.push(id)
|
||||
}
|
||||
}
|
||||
for (const id of this.knownAreaIds) {
|
||||
if (id > 0 && !out.includes(id)) {
|
||||
out.push(id)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the `LevelView` containing the local player's current world sub-tile and synchronize
|
||||
* `world.areaId` and `automapView` whenever the player crosses into a different area (#549).
|
||||
*/
|
||||
private syncActiveAreaFromPlayerPosition(options?: {
|
||||
readonly emitHudSync?: boolean
|
||||
}): LevelView | null {
|
||||
const predSnap = this.predictor.getSnapshot()
|
||||
const playerSubX = predSnap.x !== 0 || predSnap.y !== 0 ? predSnap.x : this.world.self.x
|
||||
const playerSubY = predSnap.x !== 0 || predSnap.y !== 0 ? predSnap.y : this.world.self.y
|
||||
const candidateIds = this.getCandidateAreaIds()
|
||||
const levelView =
|
||||
this.levelView ??
|
||||
(this.mapService && candidateIds.length > 0 && (playerSubX !== 0 || playerSubY !== 0)
|
||||
? this.mapService.findLevelViewAtWorldSubTile(playerSubX, playerSubY, candidateIds)
|
||||
: null)
|
||||
|
||||
if (levelView && levelView.areaId > 0) {
|
||||
if (this.world.areaId > 0) {
|
||||
this.knownAreaIds.add(this.world.areaId)
|
||||
}
|
||||
this.knownAreaIds.add(levelView.areaId)
|
||||
const areaChanged = this.world.areaId !== levelView.areaId
|
||||
const automapOutOfSync =
|
||||
this.automapView.getCurrentAreaId() !== levelView.areaId ||
|
||||
this.automapView.getLevelView(levelView.areaId) === undefined
|
||||
if (areaChanged) {
|
||||
this.world.areaId = levelView.areaId
|
||||
}
|
||||
if (areaChanged || automapOutOfSync) {
|
||||
this.automapView.syncActiveLevel(levelView.areaId, levelView)
|
||||
}
|
||||
if (areaChanged) {
|
||||
void this.soundService.onEnterArea(levelView.areaId)
|
||||
if (options?.emitHudSync) {
|
||||
this.hudModel.syncFromWorld()
|
||||
}
|
||||
}
|
||||
}
|
||||
return levelView
|
||||
}
|
||||
|
||||
/**
|
||||
* `DUNGEON_IsRoomInTown` for the local player: whether the level containing its sub-tile is a
|
||||
* town level, or `null` while no loaded level contains it. The sub-tile is the one the gate
|
||||
|
|
@ -932,11 +988,37 @@ export class OnlineSession {
|
|||
const levelView = this.mapService.findLevelViewAtWorldSubTile(
|
||||
Math.round(snap.logicalX),
|
||||
Math.round(snap.logicalY),
|
||||
[this.world.areaId, ...this.world.getRevealedAreaIds()],
|
||||
this.getCandidateAreaIds(),
|
||||
)
|
||||
return levelView === null ? null : isTownLevel(levelView.areaId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve whether `(subX, subY)` is in a town level (`DUNGEON_IsRoomInTown` equivalent):
|
||||
* prefers the exact `LevelView` containing `(subX, subY)` when `mapService` is loaded,
|
||||
* and falls back to the active `world.areaId` / `levelView.areaId`.
|
||||
*/
|
||||
private isSubTileInTown(subX: number, subY: number): boolean {
|
||||
if (this.mapService && this.world.areaId > 0 && (subX !== 0 || subY !== 0)) {
|
||||
const levelView = this.mapService.findLevelViewAtWorldSubTile(
|
||||
Math.round(subX),
|
||||
Math.round(subY),
|
||||
[this.world.areaId, ...this.world.getRevealedAreaIds()],
|
||||
)
|
||||
if (levelView !== null) {
|
||||
return isTownLevel(levelView.areaId)
|
||||
}
|
||||
}
|
||||
const activeAreaId =
|
||||
this.world.areaId > 0
|
||||
? this.world.areaId
|
||||
: (this.levelView?.areaId ??
|
||||
this.mapService?.getActiveLevelView()?.areaId ??
|
||||
this.automapView.getLevelView()?.areaId ??
|
||||
0)
|
||||
return isTownLevel(activeAreaId)
|
||||
}
|
||||
|
||||
// ─── Outbound ClientCommand Dispatch ─────────────────────────
|
||||
|
||||
/**
|
||||
|
|
@ -1070,7 +1152,11 @@ export class OnlineSession {
|
|||
break
|
||||
}
|
||||
}
|
||||
const activeView = this.mapService?.getActiveLevelView() ?? undefined
|
||||
this.knownAreaIds.clear()
|
||||
this.knownAreaIds.add(ev.areaId)
|
||||
const activeView =
|
||||
this.mapService?.getActiveLevelView() ??
|
||||
(this.levelView && this.levelView.areaId === ev.areaId ? this.levelView : undefined)
|
||||
this.automapView.setContext(
|
||||
ev.mapSeed,
|
||||
this.world.difficulty,
|
||||
|
|
@ -1082,6 +1168,9 @@ export class OnlineSession {
|
|||
}
|
||||
|
||||
case 'RoomReveal': {
|
||||
if (ev.areaId > 0) {
|
||||
this.knownAreaIds.add(ev.areaId)
|
||||
}
|
||||
if (this.mapService) {
|
||||
try {
|
||||
this.mapService.onAddRoomDataPacket({
|
||||
|
|
@ -1186,12 +1275,29 @@ export class OnlineSession {
|
|||
const evAny = ev as any
|
||||
const evTypeId = typeof evAny.unitType === 'number' ? evAny.unitType : unitTypeToId(ev.unitType)
|
||||
const evUnitId = Number(evAny.id ?? evAny.unitId ?? 1)
|
||||
const toX = Number(evAny.to?.x ?? evAny.targetX ?? evAny.x ?? 0)
|
||||
const toY = Number(evAny.to?.y ?? evAny.targetY ?? evAny.y ?? 0)
|
||||
const unit = this.world.getUnit(evTypeId as UnitType, evUnitId)
|
||||
const rawToX = Number(evAny.to?.x ?? evAny.targetX ?? evAny.x ?? 0)
|
||||
const rawToY = Number(evAny.to?.y ?? evAny.targetY ?? evAny.y ?? 0)
|
||||
const toX = unit?.targetX !== undefined && (rawToX === 0 || evAny.target !== undefined) ? unit.targetX : rawToX
|
||||
const toY = unit?.targetY !== undefined && (rawToY === 0 || evAny.target !== undefined) ? unit.targetY : rawToY
|
||||
const fromX = evAny.from?.x ?? evAny.currentX ?? evAny.x
|
||||
const fromY = evAny.from?.y ?? evAny.currentY ?? evAny.y
|
||||
const isLocalPlayer =
|
||||
evTypeId === 0 && evUnitId === this.world.selfId && this.world.selfId !== 0
|
||||
if (unit && unit.unitType === 'player') {
|
||||
const posForTownX = ev.mode === 'stop' ? toX : Number(fromX ?? unit.x ?? toX)
|
||||
const posForTownY = ev.mode === 'stop' ? toY : Number(fromY ?? unit.y ?? toY)
|
||||
const inTown = this.isSubTileInTown(posForTownX, posForTownY)
|
||||
if (ev.mode === 'stop') {
|
||||
unit.mode = inTown ? 5 : 1
|
||||
} else if (ev.mode === 'run') {
|
||||
unit.mode = 3
|
||||
} else if (ev.mode === 'knockback') {
|
||||
unit.mode = 19
|
||||
} else {
|
||||
unit.mode = inTown ? 6 : 2
|
||||
}
|
||||
}
|
||||
if (isLocalPlayer) {
|
||||
if (ev.mode === 'stop') {
|
||||
this.verifyLocalPlayerPosition(toX, toY, { kind: 'player-stop' })
|
||||
|
|
@ -1271,6 +1377,9 @@ export class OnlineSession {
|
|||
}
|
||||
}
|
||||
|
||||
// Synchronize active area and AutomapView from current player position (#549)
|
||||
this.syncActiveAreaFromPlayerPosition({ emitHudSync: false })
|
||||
|
||||
// Synchronize HudManager via HudModel
|
||||
this.hudModel.syncFromWorld()
|
||||
|
||||
|
|
@ -1303,17 +1412,40 @@ export class OnlineSession {
|
|||
unit.x = state.x
|
||||
unit.y = state.y
|
||||
unit.direction = state.dir64
|
||||
const isMonsterMoveMode =
|
||||
unit.unitType === 'monster' && (unit.mode === 2 || unit.mode === 9 || unit.mode === 15)
|
||||
const isPlayerMoveMode =
|
||||
unit.unitType === 'player' &&
|
||||
(unit.mode === 2 || unit.mode === 3 || unit.mode === 6 || unit.mode === 19)
|
||||
if (!state.moving && unit.lifePct > 0 && (isMonsterMoveMode || isPlayerMoveMode)) {
|
||||
if (unit.unitType === 'player' && unit.id !== this.world.selfId) {
|
||||
const unitInTown = this.isSubTileInTown(state.x, state.y)
|
||||
if (state.moving && unit.moveMode !== 'stop') {
|
||||
if (unit.moveMode === 'knockback') {
|
||||
unit.mode = 19
|
||||
} else if (state.running || unit.mode === 3) {
|
||||
unit.mode = 3
|
||||
} else if (unit.mode === 2 || unit.mode === 6 || unit.mode === 1 || unit.mode === 5) {
|
||||
unit.mode = unitInTown ? 6 : 2
|
||||
}
|
||||
} else if (
|
||||
unit.mode === 2 ||
|
||||
unit.mode === 3 ||
|
||||
unit.mode === 6 ||
|
||||
unit.mode === 19 ||
|
||||
unit.mode === 1 ||
|
||||
unit.mode === 5
|
||||
) {
|
||||
unit.mode = unitInTown ? 5 : 1
|
||||
unit.moveMode = 'stop'
|
||||
}
|
||||
} else if (
|
||||
unit.unitType === 'monster' &&
|
||||
!state.moving &&
|
||||
unit.lifePct > 0 &&
|
||||
(unit.mode === 2 || unit.mode === 9 || unit.mode === 15)
|
||||
) {
|
||||
unit.mode = 1
|
||||
unit.moveMode = 'stop'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.syncActiveAreaFromPlayerPosition({ emitHudSync: true })
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1331,17 +1463,31 @@ export class OnlineSession {
|
|||
const selfUnit = this.world.getUnit('player', this.world.selfId)
|
||||
const isSelfDead = this.world.isSelfDead || this.world.isDead
|
||||
const selfCastState = !isSelfDead ? (selfUnit?.castState ?? this.world.self.castState) : undefined
|
||||
const selfInTown = this.isSubTileInTown(selfX, selfY)
|
||||
const neutralMode = selfInTown ? 5 : 1
|
||||
const walkMode = selfInTown ? 6 : 2
|
||||
const unitExplicitMode =
|
||||
selfUnit?.mode !== undefined &&
|
||||
selfUnit.mode !== 0 &&
|
||||
selfUnit.mode !== 1 &&
|
||||
selfUnit.mode !== 2 &&
|
||||
selfUnit.mode !== 3 &&
|
||||
selfUnit.mode !== 5 &&
|
||||
selfUnit.mode !== 6
|
||||
? selfUnit.mode
|
||||
: undefined
|
||||
const selfMode = isSelfDead
|
||||
? 12
|
||||
? (selfUnit?.mode === 0 ? 0 : 17)
|
||||
: selfCastState
|
||||
? selfCastState.mode || 10
|
||||
: predSnap.moving
|
||||
? predSnap.running
|
||||
? 3
|
||||
: 2
|
||||
: selfUnit?.mode && selfUnit.mode > 1
|
||||
? selfUnit.mode
|
||||
: 1
|
||||
: walkMode
|
||||
: (unitExplicitMode ?? neutralMode)
|
||||
if (selfUnit !== undefined && !selfCastState) {
|
||||
selfUnit.mode = selfMode
|
||||
}
|
||||
const selfDirection =
|
||||
selfCastState && selfUnit !== undefined ? selfUnit.direction : predSnap.dir64
|
||||
out.push({
|
||||
|
|
@ -1368,10 +1514,7 @@ export class OnlineSession {
|
|||
const activeLevelView =
|
||||
this.levelView ??
|
||||
(this.mapService && this.world.areaId > 0 && (selfX !== 0 || selfY !== 0)
|
||||
? this.mapService.findLevelViewAtWorldSubTile(selfX, selfY, [
|
||||
this.world.areaId,
|
||||
...this.world.getRevealedAreaIds(),
|
||||
])
|
||||
? this.mapService.findLevelViewAtWorldSubTile(selfX, selfY, this.getCandidateAreaIds())
|
||||
: null) ??
|
||||
this.mapService?.getActiveLevelView() ??
|
||||
this.automapView.getLevelView()
|
||||
|
|
@ -1444,27 +1587,46 @@ export class OnlineSession {
|
|||
continue
|
||||
}
|
||||
|
||||
const isCorpse = u.unitType === 'monster' && (u.mode === 0 || u.mode === 12 || u.lifePct <= 0)
|
||||
const isCorpse =
|
||||
(u.unitType === 'monster' && (u.mode === 0 || u.mode === 12 || u.lifePct <= 0)) ||
|
||||
(u.unitType === 'player' && (u.mode === 0 || u.mode === 17 || u.lifePct <= 0))
|
||||
const castState = !isCorpse ? u.castState : undefined
|
||||
const mode = isCorpse
|
||||
? u.mode === 0
|
||||
? 0
|
||||
: 12
|
||||
: castState
|
||||
? castState.mode || u.mode || 1
|
||||
: interp?.moving && (u.unitType === 'monster' || u.unitType === 'player')
|
||||
? u.moveMode === 'knockback'
|
||||
? u.unitType === 'monster'
|
||||
? 9
|
||||
: 19
|
||||
: interp.running
|
||||
? u.unitType === 'monster'
|
||||
? 15
|
||||
: 3
|
||||
: 2
|
||||
: u.unitType === 'monster' || u.unitType === 'player'
|
||||
? u.mode || 1
|
||||
: u.mode
|
||||
let mode = castState ? castState.mode || u.mode : u.mode
|
||||
if (u.unitType === 'player' && !isCorpse && !castState) {
|
||||
const unitInTown = this.isSubTileInTown(ux, uy)
|
||||
if (interp?.moving && u.moveMode !== 'stop') {
|
||||
mode =
|
||||
u.moveMode === 'knockback'
|
||||
? 19
|
||||
: interp.running || u.mode === 3
|
||||
? 3
|
||||
: unitInTown
|
||||
? 6
|
||||
: 2
|
||||
} else if (
|
||||
u.mode === 1 ||
|
||||
u.mode === 5 ||
|
||||
(interp !== null &&
|
||||
interp !== undefined &&
|
||||
!interp.moving &&
|
||||
(u.mode === 2 || u.mode === 3 || u.mode === 6 || u.mode === 19))
|
||||
) {
|
||||
mode = unitInTown ? 5 : 1
|
||||
} else if (u.mode === 2 || u.mode === 6) {
|
||||
mode = unitInTown ? 6 : 2
|
||||
}
|
||||
u.mode = mode
|
||||
} else if (u.unitType === 'monster') {
|
||||
if (isCorpse) {
|
||||
mode = u.mode === 0 ? 0 : 12
|
||||
} else if (castState) {
|
||||
mode = castState.mode || u.mode || 1
|
||||
} else if (interp?.moving && u.moveMode !== 'stop') {
|
||||
mode = u.moveMode === 'knockback' ? 9 : interp.running ? 15 : 2
|
||||
} else {
|
||||
mode = u.mode || 1
|
||||
}
|
||||
}
|
||||
const targetX = castState?.targetX ?? u.targetX
|
||||
const targetY = castState?.targetY ?? u.targetY
|
||||
|
||||
|
|
|
|||
|
|
@ -498,6 +498,9 @@ export class Minimap {
|
|||
/** level key -> last player sub-unit coordinates where `AUTOMAP_UpdateVisibility` triggered. */
|
||||
private readonly lastRevealSubPos = new Map<string, { x: number; y: number }>()
|
||||
|
||||
/** Last drawn level key so manual pan resets when switching levels (#549). */
|
||||
private lastLevelKey: string | null = null
|
||||
|
||||
/** Diagnostic telemetry from the most recent `draw()` invocation. */
|
||||
readonly lastRenderStats: AutomapRenderTelemetry = {
|
||||
mode: 'minimap',
|
||||
|
|
@ -869,6 +872,11 @@ export class Minimap {
|
|||
viewport: { readonly width: number; readonly height: number },
|
||||
options?: MinimapDrawOptions,
|
||||
): void {
|
||||
if (this.lastLevelKey !== null && this.lastLevelKey !== level.key) {
|
||||
this.resetPan()
|
||||
}
|
||||
this.lastLevelKey = level.key
|
||||
|
||||
const screenW = Math.max(1, Math.trunc(viewport.width))
|
||||
const screenH = Math.max(1, Math.trunc(viewport.height))
|
||||
const vp = computeAutomapViewport(
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import {
|
|||
} from '../../netproto/index.ts'
|
||||
import { onlineItemDataToUiInventoryItem } from '../../common/items/item-bridge.ts'
|
||||
import { dir64FromPoints } from '../../common/units/direction.ts'
|
||||
import { isTownLevel } from '../../common/world/scene-population.ts'
|
||||
import { ClientUnit, makeUnitKey, type UnitCastState } from './client-unit.ts'
|
||||
import { ClientInventoryManager, type ClientItemContainers } from './inventory.ts'
|
||||
import { MutableSelfPlayerState, type SelfPlayerState } from './self.ts'
|
||||
|
|
@ -430,9 +431,22 @@ export class ClientWorld {
|
|||
return this._soundQueue.splice(0, this._soundQueue.length)
|
||||
}
|
||||
|
||||
private syncPlayerTownModes(): void {
|
||||
const inTown = isTownLevel(this.areaId)
|
||||
for (const unit of this._units.values()) {
|
||||
if (unit.unitType !== 'player') continue
|
||||
if (unit.mode === 1 || unit.mode === 5) {
|
||||
unit.mode = inTown ? 5 : 1
|
||||
} else if (unit.mode === 2 || unit.mode === 6) {
|
||||
unit.mode = inTown ? 6 : 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tick(dtMs = 40): void {
|
||||
this.frameCount++
|
||||
const step = Math.max(1, (Math.max(0, dtMs) / 1000) * 10)
|
||||
const inTown = isTownLevel(this.areaId)
|
||||
for (const unit of this._units.values()) {
|
||||
if (unit.dead) continue
|
||||
if (unit.targetX === undefined || unit.targetY === undefined) continue
|
||||
|
|
@ -443,6 +457,13 @@ export class ClientWorld {
|
|||
if (dist <= step) {
|
||||
unit.x = unit.targetX
|
||||
unit.y = unit.targetY
|
||||
if (unit.unitType === 'player' && (unit.mode === 2 || unit.mode === 3 || unit.mode === 6 || unit.mode === 19)) {
|
||||
unit.mode = inTown ? 5 : 1
|
||||
unit.moveMode = 'stop'
|
||||
} else if (unit.unitType === 'monster' && (unit.mode === 2 || unit.mode === 9 || unit.mode === 15)) {
|
||||
unit.mode = 1
|
||||
unit.moveMode = 'stop'
|
||||
}
|
||||
} else {
|
||||
unit.x += (dx / dist) * step
|
||||
unit.y += (dy / dist) * step
|
||||
|
|
@ -469,12 +490,14 @@ export class ClientWorld {
|
|||
const key = makeUnitKey(unitType, id)
|
||||
let unit = this._units.get(key)
|
||||
if (!unit) {
|
||||
const typeName = unitTypeFromId(unitType)
|
||||
unit = new ClientUnit({
|
||||
unitType: unitTypeFromId(unitType),
|
||||
unitType: typeName,
|
||||
id,
|
||||
classId: 0,
|
||||
x: 0,
|
||||
y: 0,
|
||||
...(typeName === 'player' ? { mode: isTownLevel(this.areaId) ? 5 : 1 } : {}),
|
||||
})
|
||||
this._units.set(key, unit)
|
||||
}
|
||||
|
|
@ -539,6 +562,7 @@ export class ClientWorld {
|
|||
type: 'UnitMove',
|
||||
unitType: unitTypeFromId(ev.unitType ?? 0),
|
||||
id: Number(ev.unitId ?? ev.id ?? 1),
|
||||
mode: ev.mode ?? 'walk',
|
||||
from: {
|
||||
x: Number(ev.currentX ?? ev.x ?? 0),
|
||||
y: Number(ev.currentY ?? ev.y ?? 0),
|
||||
|
|
@ -579,11 +603,12 @@ export class ClientWorld {
|
|||
value: Number(ev.value ?? 0),
|
||||
}
|
||||
} else if (ev.type === 'Damage') {
|
||||
const isPlayerTarget = unitTypeFromId(ev.unitType ?? 1) === 'player'
|
||||
ev = {
|
||||
type: 'UnitMode',
|
||||
unitType: ev.unitType ?? 1,
|
||||
id: Number(ev.unitId ?? ev.id ?? 0),
|
||||
mode: Number(ev.mode ?? (ev.lifePct === 0 ? 12 : 3)),
|
||||
mode: Number(ev.mode ?? (ev.lifePct === 0 ? (isPlayerTarget ? 17 : 12) : (isPlayerTarget ? 4 : 3))),
|
||||
lifePct: Number(ev.lifePct ?? 0),
|
||||
}
|
||||
}
|
||||
|
|
@ -607,6 +632,7 @@ export class ClientWorld {
|
|||
if (ev.objectSeed !== undefined) {
|
||||
this.objectSeed = ev.objectSeed >>> 0
|
||||
}
|
||||
this.syncPlayerTownModes()
|
||||
break
|
||||
|
||||
case 'LoadComplete':
|
||||
|
|
@ -625,6 +651,7 @@ export class ClientWorld {
|
|||
this._revealedRooms.add(`${ev.areaId}:${ev.tileX}:${ev.tileY}`)
|
||||
if (this.areaId === 0 && ev.areaId > 0) {
|
||||
this.areaId = ev.areaId
|
||||
this.syncPlayerTownModes()
|
||||
}
|
||||
break
|
||||
|
||||
|
|
@ -641,6 +668,13 @@ export class ClientWorld {
|
|||
} else {
|
||||
unit.updateFromSnapshot(ev.unit)
|
||||
}
|
||||
if (ev.unit.mode === undefined) {
|
||||
if (unit.unitType === 'player') {
|
||||
unit.mode = isTownLevel(this.areaId) ? 5 : 1
|
||||
} else if (unit.unitType === 'monster') {
|
||||
unit.mode = unit.lifePct <= 0 ? 12 : 1
|
||||
}
|
||||
}
|
||||
if (unit.unitType === 'monster') {
|
||||
this._monsters.set(unit.id, unit)
|
||||
if (this._merc && unit.id === this._merc.mercId) {
|
||||
|
|
@ -703,34 +737,59 @@ export class ClientWorld {
|
|||
unit.x = ev.to.x
|
||||
unit.y = ev.to.y
|
||||
}
|
||||
unit.targetX = ev.to.x
|
||||
unit.targetY = ev.to.y
|
||||
unit.targetUnit = ev.target
|
||||
if (ev.mode) {
|
||||
unit.moveMode = ev.mode
|
||||
let targetX = ev.to.x
|
||||
let targetY = ev.to.y
|
||||
if (
|
||||
ev.target &&
|
||||
((targetX === 0 && targetY === 0) ||
|
||||
(ev.from !== undefined && targetX === ev.from.x && targetY === ev.from.y))
|
||||
) {
|
||||
const targetUnit = this.getUnit(ev.target.unitType, ev.target.id)
|
||||
if (targetUnit && (targetUnit.x !== 0 || targetUnit.y !== 0)) {
|
||||
targetX = targetUnit.x
|
||||
targetY = targetUnit.y
|
||||
} else if (
|
||||
unitTypeFromId(ev.target.unitType) === 'player' &&
|
||||
(ev.target.id === this.selfId || this.selfId === 0) &&
|
||||
(this._self.x !== 0 || this._self.y !== 0)
|
||||
) {
|
||||
targetX = this._self.x
|
||||
targetY = this._self.y
|
||||
}
|
||||
}
|
||||
if (unit.unitType === 'monster' && unit.lifePct > 0 && unit.mode !== 0 && unit.mode !== 12) {
|
||||
unit.mode =
|
||||
ev.mode === 'stop'
|
||||
? 1
|
||||
: ev.mode === 'run'
|
||||
? 15
|
||||
: ev.mode === 'knockback'
|
||||
? 9
|
||||
: 2
|
||||
if (ev.mode !== 'stop') {
|
||||
unit.targetX = targetX
|
||||
unit.targetY = targetY
|
||||
unit.targetUnit = ev.target
|
||||
const moveMode = ev.mode ?? 'walk'
|
||||
unit.moveMode = moveMode
|
||||
if (targetX !== unit.x || targetY !== unit.y) {
|
||||
unit.direction = dir64FromPoints(unit.x, unit.y, targetX, targetY, unit.direction)
|
||||
}
|
||||
if (unit.unitType === 'player' && unit.mode !== 0 && unit.mode !== 16 && unit.mode !== 17) {
|
||||
const inTown = isTownLevel(this.areaId)
|
||||
if (moveMode === 'stop') {
|
||||
unit.mode = inTown ? 5 : 1
|
||||
} else if (moveMode === 'run') {
|
||||
unit.mode = 3
|
||||
} else if (moveMode === 'knockback') {
|
||||
unit.mode = 19
|
||||
} else {
|
||||
unit.mode = inTown ? 6 : 2
|
||||
}
|
||||
if (moveMode !== 'stop') {
|
||||
unit.castState = undefined
|
||||
}
|
||||
} else if (unit.unitType === 'player' && unit.mode !== 0 && unit.mode !== 16 && unit.mode !== 17) {
|
||||
unit.mode =
|
||||
ev.mode === 'stop'
|
||||
? 1
|
||||
: ev.mode === 'run'
|
||||
? 3
|
||||
: ev.mode === 'knockback'
|
||||
? 19
|
||||
: 2
|
||||
if (ev.mode !== 'stop') {
|
||||
} else if (unit.unitType === 'monster' && unit.lifePct > 0 && unit.mode !== 0 && unit.mode !== 12) {
|
||||
if (moveMode === 'stop') {
|
||||
unit.mode = 1
|
||||
} else if (moveMode === 'run') {
|
||||
unit.mode = 15
|
||||
} else if (moveMode === 'knockback') {
|
||||
unit.mode = 9
|
||||
} else {
|
||||
unit.mode = 2
|
||||
}
|
||||
if (moveMode !== 'stop') {
|
||||
unit.castState = undefined
|
||||
}
|
||||
}
|
||||
|
|
@ -742,9 +801,9 @@ export class ClientWorld {
|
|||
this._self.x = ev.to.x
|
||||
this._self.y = ev.to.y
|
||||
}
|
||||
if (ev.to.x !== 0 || ev.to.y !== 0) {
|
||||
this._self.targetX = ev.to.x
|
||||
this._self.targetY = ev.to.y
|
||||
if (targetX !== 0 || targetY !== 0) {
|
||||
this._self.targetX = targetX
|
||||
this._self.targetY = targetY
|
||||
}
|
||||
}
|
||||
break
|
||||
|
|
|
|||
|
|
@ -14,8 +14,17 @@ function clampUnitType(raw: number): UnitType {
|
|||
}
|
||||
|
||||
function resolveMoveMode(rawMoveType: number): 'walk' | 'run' | 'knockback' | 'stop' {
|
||||
// In D2Client.dll 0x6fb82e68: 0x17 (0x67) / 0x18 (0x68) / 0x02 / 0x04 / bit 0x10 = run, 0x20 / 0x0e / 0x0f = knockback
|
||||
if (rawMoveType === 0x20 || rawMoveType === 0x09 || rawMoveType === 0x0e || rawMoveType === 0x0f) {
|
||||
// In D2Client.dll 0x6fac9830 (player) & 0x6fb82e68 (monster):
|
||||
// - 0x17 (0x0F/0x67 run) / 0x18 (0x10/0x68 run) / 0x02 = run
|
||||
// - 0x13/0x14 (player KB), 0x0e/0x0f (monster KB), 0x20/0x09 = knockback
|
||||
if (
|
||||
rawMoveType === 0x20 ||
|
||||
rawMoveType === 0x09 ||
|
||||
rawMoveType === 0x0e ||
|
||||
rawMoveType === 0x0f ||
|
||||
rawMoveType === 0x13 ||
|
||||
rawMoveType === 0x14
|
||||
) {
|
||||
return 'knockback'
|
||||
}
|
||||
if (
|
||||
|
|
|
|||
|
|
@ -0,0 +1,244 @@
|
|||
/**
|
||||
* Issue #549 regression: Minimap / Automap must switch synchronously with the main map
|
||||
* when the player transitions between levels/areas within an Act (outdoor border crossing,
|
||||
* dungeon warp, town portal, or waypoint) without a new `0x03 LoadAct` packet.
|
||||
*/
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import { beforeAll, describe, expect, it } from 'vitest'
|
||||
import '../../tools/lib/node-map-loader.ts'
|
||||
import { AutomapView } from '../../src/client/automap/automap-view.ts'
|
||||
import { MapService } from '../../src/client/map/map-service.ts'
|
||||
import { renderSceneSourceFrame } from '../../src/client/scene/world-renderer.ts'
|
||||
import { OnlineSession } from '../../src/client/session/online-session.ts'
|
||||
import { createSettingsStore } from '../../src/client/settings/settings-store.ts'
|
||||
import { Minimap } from '../../src/client/ui/minimap.ts'
|
||||
import { levelDisplayName } from '../../src/common/world/level-names-zh.ts'
|
||||
|
||||
const FIXTURE_DIR = resolve('tests/fixtures/netproto')
|
||||
const ROGUE_ENCAMPMENT = 1
|
||||
const BLOOD_MOOR = 2
|
||||
const DEN_OF_EVIL = 8
|
||||
|
||||
function createNoopSpriteRenderer() {
|
||||
return {
|
||||
begin() {},
|
||||
draw() {},
|
||||
drawSolidQuad() {},
|
||||
setLighting() {},
|
||||
setPalette() {},
|
||||
flush() {},
|
||||
getGl() {
|
||||
return null
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function createFakeTileGpuCache() {
|
||||
return {
|
||||
loadedPageCount: 1,
|
||||
ensureActPalette() {},
|
||||
getPageHandle() {
|
||||
return { index: 1, width: 2048, height: 1932 }
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
describe('Issue #549: Minimap / Automap switches synchronously with main map on area transitions', () => {
|
||||
let mapService: MapService
|
||||
|
||||
beforeAll(async () => {
|
||||
mapService = await MapService.fromPackedDir(resolve('public/act-packs/tiles'))
|
||||
})
|
||||
|
||||
it('AutomapView.syncActiveLevel switches currentAreaId without wiping serverRevealedRooms across the Act', () => {
|
||||
const automap = new AutomapView({ mapService, initialMode: 'off' })
|
||||
const townView = automap.setContext(0x52e4e6ff, 0, ROGUE_ENCAMPMENT)
|
||||
const townRoom = townView.rooms[0]!
|
||||
automap.onServerAddRoom(townRoom.tileX, townRoom.tileY, ROGUE_ENCAMPMENT)
|
||||
expect(automap.getCurrentAreaId()).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(automap.buildSnapshot().serverRevealedRoomCount).toBe(1)
|
||||
|
||||
const bloodMoorView = mapService.getLevelView(BLOOD_MOOR, 'all')
|
||||
const bmRoom = bloodMoorView.rooms[0]!
|
||||
automap.onServerAddRoom(bmRoom.tileX, bmRoom.tileY, BLOOD_MOOR)
|
||||
|
||||
// Switch to Blood Moor via syncActiveLevel: preserves revealed rooms in both Area 1 and Area 2
|
||||
automap.syncActiveLevel(BLOOD_MOOR, bloodMoorView)
|
||||
expect(automap.getCurrentAreaId()).toBe(BLOOD_MOOR)
|
||||
expect(automap.buildSnapshot().currentAreaId).toBe(BLOOD_MOOR)
|
||||
expect(automap.buildSnapshot().serverRevealedRoomCount).toBe(1)
|
||||
|
||||
const bmMinimapLevel = automap.buildMinimapLevel('ENG')
|
||||
expect(bmMinimapLevel.levelId).toBe(BLOOD_MOOR)
|
||||
expect(bmMinimapLevel.levelName).toBe(levelDisplayName(BLOOD_MOOR, 'ENG'))
|
||||
expect(bmMinimapLevel.cellsX).toBe(bloodMoorView.cellsX)
|
||||
expect(bmMinimapLevel.cellsY).toBe(bloodMoorView.cellsY)
|
||||
expect(bmMinimapLevel.markers.some((m) => m.kind === 'exit')).toBe(true)
|
||||
|
||||
// Switch back to Rogue Encampment: Area 1 revealed rooms are still intact
|
||||
automap.syncActiveLevel(ROGUE_ENCAMPMENT, townView)
|
||||
expect(automap.getCurrentAreaId()).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(automap.buildSnapshot().serverRevealedRoomCount).toBe(1)
|
||||
})
|
||||
|
||||
it('OnlineSession keeps automapView and world.areaId in lockstep across Area 1 -> Area 2 -> Area 8 -> Area 1 transitions', () => {
|
||||
const text = readFileSync(resolve(FIXTURE_DIR, '04-d2gs-idle-60s.d2cap'), 'utf8')
|
||||
const settingsStore = createSettingsStore()
|
||||
settingsStore.set({ automapReveal: 'level' })
|
||||
const session = new OnlineSession({ settingsStore, mapService })
|
||||
session.loadD2CapReplay(text)
|
||||
session.replayAllD2Cap()
|
||||
|
||||
// Initial state: standing in Rogue Encampment (Area 1), even though Area 2 rooms were pre-revealed by 0x07
|
||||
expect(session.world.getRevealedAreaIds()).toContain(BLOOD_MOOR)
|
||||
expect(session.sceneSource.getLevelView()?.areaId).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.world.areaId).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.automapView.getCurrentAreaId()).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.automapView.buildMinimapLevel('ENG').levelId).toBe(ROGUE_ENCAMPMENT)
|
||||
|
||||
// 1. Player walks/moves across border into Blood Moor (Area 2) without a new LoadAct packet
|
||||
const bloodMoorView = mapService.getLevelView(BLOOD_MOOR, 'all')
|
||||
const bmSubX = bloodMoorView.subOriginX + bloodMoorView.spawnX * 5 + 2
|
||||
const bmSubY = bloodMoorView.subOriginY + bloodMoorView.spawnY * 5 + 2
|
||||
session.handleServerEvent({
|
||||
type: 'UnitReassign',
|
||||
unitType: 'player',
|
||||
id: session.world.selfId,
|
||||
at: { x: bmSubX, y: bmSubY },
|
||||
snap: true,
|
||||
})
|
||||
|
||||
expect(session.sceneSource.getLevelView()?.areaId).toBe(BLOOD_MOOR)
|
||||
expect(session.sceneSource.buildFrameSnapshot().levelId).toBe(BLOOD_MOOR)
|
||||
expect(session.world.areaId).toBe(BLOOD_MOOR)
|
||||
expect(session.automapView.getCurrentAreaId()).toBe(BLOOD_MOOR)
|
||||
expect(session.automapView.buildMinimapLevel('ENG').levelId).toBe(BLOOD_MOOR)
|
||||
expect(session.automapView.buildMinimapLevel('ENG').levelName).toBe(
|
||||
levelDisplayName(BLOOD_MOOR, 'ENG'),
|
||||
)
|
||||
|
||||
// 2. Player enters Den of Evil (Area 8): server reveals Area 8 room via 0x07 and reassigns player
|
||||
const denView = mapService.getLevelView(DEN_OF_EVIL, 'all')
|
||||
const denRoom = denView.rooms[0]!
|
||||
session.handleServerEvent({
|
||||
type: 'RoomReveal',
|
||||
areaId: DEN_OF_EVIL,
|
||||
tileX: denRoom.tileX,
|
||||
tileY: denRoom.tileY,
|
||||
})
|
||||
const denSubX = denView.subOriginX + denView.spawnX * 5 + 2
|
||||
const denSubY = denView.subOriginY + denView.spawnY * 5 + 2
|
||||
session.handleServerEvent({
|
||||
type: 'UnitReassign',
|
||||
unitType: 'player',
|
||||
id: session.world.selfId,
|
||||
at: { x: denSubX, y: denSubY },
|
||||
snap: true,
|
||||
})
|
||||
|
||||
expect(session.sceneSource.getLevelView()?.areaId).toBe(DEN_OF_EVIL)
|
||||
expect(session.sceneSource.buildFrameSnapshot().levelId).toBe(DEN_OF_EVIL)
|
||||
expect(session.world.areaId).toBe(DEN_OF_EVIL)
|
||||
expect(session.automapView.getCurrentAreaId()).toBe(DEN_OF_EVIL)
|
||||
expect(session.automapView.buildMinimapLevel('ENG').levelId).toBe(DEN_OF_EVIL)
|
||||
expect(session.automapView.buildMinimapLevel('ENG').levelName).toBe(
|
||||
levelDisplayName(DEN_OF_EVIL, 'ENG'),
|
||||
)
|
||||
|
||||
// 3. Player returns to Rogue Encampment (Area 1) via Town Portal / walk back
|
||||
const townView = mapService.getLevelView(ROGUE_ENCAMPMENT, 'all')
|
||||
const townSubX = townView.subOriginX + townView.spawnX * 5 + 2
|
||||
const townSubY = townView.subOriginY + townView.spawnY * 5 + 2
|
||||
session.handleServerEvent({
|
||||
type: 'UnitReassign',
|
||||
unitType: 'player',
|
||||
id: session.world.selfId,
|
||||
at: { x: townSubX, y: townSubY },
|
||||
snap: true,
|
||||
})
|
||||
|
||||
expect(session.sceneSource.getLevelView()?.areaId).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.sceneSource.buildFrameSnapshot().levelId).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.world.areaId).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.automapView.getCurrentAreaId()).toBe(ROGUE_ENCAMPMENT)
|
||||
expect(session.automapView.buildMinimapLevel('ENG').levelId).toBe(ROGUE_ENCAMPMENT)
|
||||
})
|
||||
|
||||
it('renderSceneSourceFrame + Minimap switches level geometry, HUD title, and resets manual pan when moving across areas', () => {
|
||||
const text = readFileSync(resolve(FIXTURE_DIR, '04-d2gs-idle-60s.d2cap'), 'utf8')
|
||||
const settingsStore = createSettingsStore()
|
||||
settingsStore.set({ automapReveal: 'level' })
|
||||
const session = new OnlineSession({ settingsStore, mapService })
|
||||
session.loadD2CapReplay(text)
|
||||
session.replayAllD2Cap()
|
||||
|
||||
const minimap = new Minimap()
|
||||
minimap.mode = 'full'
|
||||
const renderer = createNoopSpriteRenderer()
|
||||
const tileGpuCache = createFakeTileGpuCache()
|
||||
|
||||
// Frame 1: in Rogue Encampment (Area 1)
|
||||
renderSceneSourceFrame({
|
||||
source: session.sceneSource,
|
||||
lightingPreset: 'fullbright',
|
||||
renderer: renderer as never,
|
||||
tileGpuCache: tileGpuCache as never,
|
||||
minimap,
|
||||
automapView: session.automapView,
|
||||
})
|
||||
expect(minimap.hudTextLastFrame?.levelName).toBe(levelDisplayName(ROGUE_ENCAMPMENT))
|
||||
|
||||
// User manually pans the full automap in Area 1
|
||||
minimap.pan(32, -24)
|
||||
expect(minimap.panX).toBe(32)
|
||||
expect(minimap.panY).toBe(-24)
|
||||
|
||||
// Local predictor moves player into Blood Moor (Area 2) via session.tick()
|
||||
const bloodMoorView = mapService.getLevelView(BLOOD_MOOR, 'all')
|
||||
const bmSubX = bloodMoorView.subOriginX + bloodMoorView.spawnX * 5 + 2
|
||||
const bmSubY = bloodMoorView.subOriginY + bloodMoorView.spawnY * 5 + 2
|
||||
session.predictor.setPosition(bmSubX, bmSubY)
|
||||
session.tick(40, 2000)
|
||||
|
||||
// Frame 2: in Blood Moor (Area 2)
|
||||
const snap2 = renderSceneSourceFrame({
|
||||
source: session.sceneSource,
|
||||
lightingPreset: 'fullbright',
|
||||
renderer: renderer as never,
|
||||
tileGpuCache: tileGpuCache as never,
|
||||
minimap,
|
||||
automapView: session.automapView,
|
||||
})
|
||||
expect(snap2.levelId).toBe(BLOOD_MOOR)
|
||||
expect(session.automapView.getCurrentAreaId()).toBe(BLOOD_MOOR)
|
||||
expect(minimap.hudTextLastFrame?.levelName).toBe(levelDisplayName(BLOOD_MOOR))
|
||||
// Manual pan from Area 1 is automatically reset to (0, 0) upon switching to Area 2
|
||||
expect(minimap.panX).toBe(0)
|
||||
expect(minimap.panY).toBe(0)
|
||||
expect(minimap.revealedRoomsCount).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('CommandMapper resolves local pathfinding against the level containing the player even when 0x07 revealed a neighbouring level', () => {
|
||||
const text = readFileSync(resolve(FIXTURE_DIR, '04-d2gs-idle-60s.d2cap'), 'utf8')
|
||||
const settingsStore = createSettingsStore()
|
||||
settingsStore.set({ automapReveal: 'level' })
|
||||
const session = new OnlineSession({ settingsStore, mapService })
|
||||
session.loadD2CapReplay(text)
|
||||
session.replayAllD2Cap()
|
||||
|
||||
// Precondition: MapService.getActiveLevelView() was switched to Area 2 by 0x07 RoomReveal,
|
||||
// while player is standing in Area 1 (Rogue Encampment).
|
||||
expect(mapService.getActiveLevelView()?.areaId).toBe(BLOOD_MOOR)
|
||||
expect(session.world.areaId).toBe(ROGUE_ENCAMPMENT)
|
||||
|
||||
const playerX = Math.round(session.predictor.getSnapshot().logicalX)
|
||||
const playerY = Math.round(session.predictor.getSnapshot().logicalY)
|
||||
const targetScreen = session.camera.subtileToScreen(playerX + 3, playerY)
|
||||
session.commandMapper.handlePointerDown(targetScreen.x, targetScreen.y, 0, 5000)
|
||||
session.commandMapper.handlePointerUp(0)
|
||||
|
||||
const predAfterClick = session.predictor.getSnapshot()
|
||||
expect(predAfterClick.moving).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,421 @@
|
|||
import { describe, expect, it } from 'vitest'
|
||||
import { AutomapView } from '../../src/client/automap/automap-view.ts'
|
||||
import {
|
||||
clearMissingArtEntries,
|
||||
computeAnimDataFrameIndex,
|
||||
getMissingArtEntries,
|
||||
renderSceneSourceFrame,
|
||||
} from '../../src/client/scene/world-renderer.ts'
|
||||
import type { LoadedCharacter } from '../../src/client/scene/entity-art-loader.ts'
|
||||
import { OnlineSession } from '../../src/client/session/online-session.ts'
|
||||
import { ClientWorldSceneSource, type ClientSceneWorldAdapter } from '../../src/client/view/scene-source.ts'
|
||||
import { decode0x0DPlayerStop, decode0x0FPlayerMove, decode0x10PlayerMoveToTarget } from '../../src/netproto/d2gs/s2c/movement.ts'
|
||||
import { PLR_MODES } from '../../src/common/units/plr-mode.ts'
|
||||
|
||||
function makeTaggedCharArt(): LoadedCharacter {
|
||||
const modes: Array<{ key: string; group: number; frames: number; speed: number; loop: boolean }> = [
|
||||
{ key: 'dt', group: 0, frames: 4, speed: 256, loop: false },
|
||||
{ key: 'nu', group: 1, frames: 8, speed: 128, loop: true },
|
||||
{ key: 'wl', group: 2, frames: 8, speed: 256, loop: true },
|
||||
{ key: 'rn', group: 3, frames: 8, speed: 256, loop: true },
|
||||
{ key: 'gh', group: 4, frames: 4, speed: 256, loop: false },
|
||||
{ key: 'tn', group: 5, frames: 16, speed: 80, loop: true },
|
||||
{ key: 'tw', group: 6, frames: 8, speed: 256, loop: true },
|
||||
{ key: 'a1', group: 7, frames: 8, speed: 256, loop: false },
|
||||
{ key: 'a2', group: 8, frames: 8, speed: 256, loop: false },
|
||||
{ key: 'sc', group: 9, frames: 8, speed: 256, loop: false },
|
||||
{ key: 'dd', group: 10, frames: 1, speed: 256, loop: false },
|
||||
]
|
||||
const groups = modes.map((m) =>
|
||||
Array.from({ length: m.frames }, (_, fIdx) => ({
|
||||
x: m.group * 100 + fIdx,
|
||||
y: 0,
|
||||
width: 32,
|
||||
height: 64,
|
||||
anchorX: -16,
|
||||
anchorY: -56,
|
||||
tag: `${m.key}_${fIdx}`,
|
||||
})),
|
||||
)
|
||||
const clips = Object.fromEntries(
|
||||
modes.map((m) => [
|
||||
m.key,
|
||||
{
|
||||
group: m.group,
|
||||
directions: 1,
|
||||
frames: m.frames,
|
||||
speed: m.speed,
|
||||
loop: m.loop,
|
||||
events: [],
|
||||
},
|
||||
]),
|
||||
)
|
||||
return {
|
||||
handle: { index: 1, width: 512, height: 512 } as never,
|
||||
groups: groups as never,
|
||||
directions: 1,
|
||||
walk: 2,
|
||||
stand: 1,
|
||||
members: 1,
|
||||
clips,
|
||||
}
|
||||
}
|
||||
|
||||
describe('Issue #544: 1.13c Player Animation Modes (TN/TW/RN/DD), Per-Unit Isolation & AnimData.d2 Speed', () => {
|
||||
it('1. isolates local player movement from remote standing players in the same scene', () => {
|
||||
const session = new OnlineSession()
|
||||
session.handleServerEvent({
|
||||
type: 'LoadAct',
|
||||
act: 0,
|
||||
mapSeed: 0x11223344,
|
||||
areaId: 1, // Rogue Encampment (Town)
|
||||
})
|
||||
session.handleServerEvent({
|
||||
type: 'UnitAssign',
|
||||
isSelf: true,
|
||||
unit: {
|
||||
unitType: 0,
|
||||
id: 1,
|
||||
classId: 1, // Sorceress ('so')
|
||||
name: 'LocalSorc',
|
||||
x: 5100,
|
||||
y: 5050,
|
||||
},
|
||||
})
|
||||
session.handleServerEvent({
|
||||
type: 'UnitAssign',
|
||||
isSelf: false,
|
||||
unit: {
|
||||
unitType: 0,
|
||||
id: 2,
|
||||
classId: 1, // Remote Sorceress ('so')
|
||||
name: 'RemoteSorc',
|
||||
x: 5104,
|
||||
y: 5050,
|
||||
},
|
||||
})
|
||||
|
||||
// Start local player walking in town (mode = 6 TW)
|
||||
session.predictor.setPath([{ x: 5120, y: 5050 }], false)
|
||||
session.tick(40, 100)
|
||||
|
||||
const renderables = session.buildRenderableUnits()
|
||||
const localPlayer = renderables.find((u) => u.id === 1)!
|
||||
const remotePlayer = renderables.find((u) => u.id === 2)!
|
||||
expect(localPlayer.mode).toBe(6) // PLRMODE_TOWNWALK (TW)
|
||||
expect(remotePlayer.mode).toBe(5) // PLRMODE_TOWNNEUTRAL (TN)
|
||||
|
||||
// Render scene frame and inspect drawn sprite tags for both players
|
||||
const automap = new AutomapView({ initialMode: 'level' })
|
||||
const levelView = automap.setContext(0x11223344, 0, 1)
|
||||
const baseX = levelView.subOriginX + 50
|
||||
const baseY = levelView.subOriginY + 50
|
||||
|
||||
const drawnTags: string[] = []
|
||||
const fakeRenderer = {
|
||||
begin() {},
|
||||
draw(frame: { tag?: string }) {
|
||||
if (frame.tag) drawnTags.push(frame.tag)
|
||||
},
|
||||
setLighting() {},
|
||||
setPalette() {},
|
||||
flush() {},
|
||||
}
|
||||
const fakeTileGpuCache = {
|
||||
loadedPageCount: 1,
|
||||
ensureActPalette() {},
|
||||
getPageHandle() {
|
||||
return undefined
|
||||
},
|
||||
}
|
||||
|
||||
const source = new ClientWorldSceneSource(
|
||||
(): ClientSceneWorldAdapter => ({
|
||||
tick: 1,
|
||||
actId: 1,
|
||||
levelId: 1,
|
||||
playerSubX: baseX,
|
||||
playerSubY: baseY,
|
||||
units: [
|
||||
{
|
||||
kind: 'player',
|
||||
id: 1,
|
||||
classId: 1,
|
||||
x: baseX,
|
||||
y: baseY,
|
||||
mode: localPlayer.mode,
|
||||
direction: 0,
|
||||
isLocalPlayer: true,
|
||||
},
|
||||
{
|
||||
kind: 'player',
|
||||
id: 2,
|
||||
classId: 1,
|
||||
x: baseX + 4,
|
||||
y: baseY,
|
||||
mode: remotePlayer.mode,
|
||||
direction: 0,
|
||||
isLocalPlayer: false,
|
||||
},
|
||||
],
|
||||
levelView,
|
||||
}),
|
||||
'800x600',
|
||||
)
|
||||
|
||||
const charArt = makeTaggedCharArt()
|
||||
renderSceneSourceFrame({
|
||||
source,
|
||||
lightingPreset: 'fullbright',
|
||||
renderer: fakeRenderer as never,
|
||||
tileGpuCache: fakeTileGpuCache as never,
|
||||
characterArtByToken: new Map([['so', charArt]]),
|
||||
nowMs: 80,
|
||||
})
|
||||
|
||||
// Local player drew TW frame (`tw_2`), while remote standing player drew TN frame (`tn_0`)
|
||||
expect(drawnTags).toContain('tw_2')
|
||||
expect(drawnTags).toContain('tn_0')
|
||||
expect(drawnTags.some((t) => t.startsWith('wl_'))).toBe(false)
|
||||
})
|
||||
|
||||
it('2. transitions remote player mode on 0x0F PlayerMove, 0x10 PlayerMoveToTarget, 0x0D PlayerStop, and segment completion', () => {
|
||||
const session = new OnlineSession()
|
||||
session.handleServerEvent({
|
||||
type: 'LoadAct',
|
||||
act: 0,
|
||||
mapSeed: 0x11223344,
|
||||
areaId: 1, // Town
|
||||
})
|
||||
session.handleServerEvent({
|
||||
type: 'UnitAssign',
|
||||
isSelf: true,
|
||||
unit: { unitType: 0, id: 1, classId: 1, name: 'Self', x: 5100, y: 5050 },
|
||||
})
|
||||
session.handleServerEvent({
|
||||
type: 'UnitAssign',
|
||||
isSelf: false,
|
||||
unit: { unitType: 0, id: 2, classId: 3, name: 'RemotePal', x: 5105, y: 5050 },
|
||||
})
|
||||
|
||||
// Initially standing in town -> mode 5 (TN)
|
||||
expect(session.world.getUnit('player', 2)?.mode).toBe(5)
|
||||
|
||||
// 0x0F PlayerMove (walk: moveType = 0x01) from (5105, 5050) to (5115, 5050)
|
||||
const pkt0x0FWalk = new Uint8Array(16)
|
||||
const dvWalk = new DataView(pkt0x0FWalk.buffer)
|
||||
dvWalk.setUint8(0, 0x0f)
|
||||
dvWalk.setUint8(1, 0) // unitType = player
|
||||
dvWalk.setUint32(2, 2, true) // unitId = 2
|
||||
dvWalk.setUint8(6, 0x01) // moveType = walk
|
||||
dvWalk.setUint16(7, 5115, true) // targetX
|
||||
dvWalk.setUint16(9, 5050, true) // targetY
|
||||
dvWalk.setUint8(11, 0)
|
||||
dvWalk.setUint16(12, 5105, true) // currentX
|
||||
dvWalk.setUint16(14, 5050, true) // currentY
|
||||
session.handleServerEvent(decode0x0FPlayerMove(pkt0x0FWalk))
|
||||
|
||||
expect(session.world.getUnit('player', 2)?.mode).toBe(6) // TW in town
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 2)?.mode).toBe(6)
|
||||
|
||||
// 0x10 PlayerMoveToTarget (run: moveType = 0x18) targeting local player id = 1 at (5100, 5050)
|
||||
const pkt0x10Run = new Uint8Array(16)
|
||||
const dvRun = new DataView(pkt0x10Run.buffer)
|
||||
dvRun.setUint8(0, 0x10)
|
||||
dvRun.setUint8(1, 0) // unitType = player
|
||||
dvRun.setUint32(2, 2, true) // unitId = 2
|
||||
dvRun.setUint8(6, 0x18) // moveType = 0x18 (run to target in 1.13c D2Client 0x6fac9830)
|
||||
dvRun.setUint8(7, 0) // targetType = player
|
||||
dvRun.setUint32(8, 1, true) // targetId = 1
|
||||
dvRun.setUint16(12, 5110, true) // currentX
|
||||
dvRun.setUint16(14, 5050, true) // currentY
|
||||
session.handleServerEvent(decode0x10PlayerMoveToTarget(pkt0x10Run))
|
||||
|
||||
expect(session.world.getUnit('player', 2)?.mode).toBe(3) // RN (even in town)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 2)?.mode).toBe(3)
|
||||
|
||||
// Advance interpolation until remote player reaches target (5100, 5050) -> returns to TN (5)
|
||||
for (let i = 0; i < 30; i++) {
|
||||
session.tick(100, i * 100)
|
||||
}
|
||||
expect(session.world.getUnit('player', 2)?.mode).toBe(5)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 2)?.mode).toBe(5)
|
||||
|
||||
// Start another run, then stop immediately via 0x0D PlayerStop
|
||||
session.handleServerEvent({
|
||||
type: 'UnitMove',
|
||||
unitType: 0,
|
||||
id: 2,
|
||||
mode: 'run',
|
||||
from: { x: 5100, y: 5050 },
|
||||
to: { x: 5120, y: 5050 },
|
||||
})
|
||||
expect(session.world.getUnit('player', 2)?.mode).toBe(3)
|
||||
|
||||
const pkt0x0DStop = new Uint8Array(13)
|
||||
const dvStop = new DataView(pkt0x0DStop.buffer)
|
||||
dvStop.setUint8(0, 0x0d)
|
||||
dvStop.setUint8(1, 0)
|
||||
dvStop.setUint32(2, 2, true)
|
||||
dvStop.setUint8(6, 0x07) // actionCode = neutral
|
||||
dvStop.setUint16(7, 5102, true)
|
||||
dvStop.setUint16(9, 5050, true)
|
||||
dvStop.setUint8(11, 0)
|
||||
dvStop.setUint8(12, 128)
|
||||
session.handleServerEvent(decode0x0DPlayerStop(pkt0x0DStop))
|
||||
|
||||
expect(session.world.getUnit('player', 2)?.mode).toBe(5) // Back to TN in town
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 2)?.mode).toBe(5)
|
||||
})
|
||||
|
||||
it('3. switches local player between town (TN=5, TW=6, RN=3) and wilderness (NU=1, WL=2, RN=3) and uses DD=17 (never KK=12) on death', () => {
|
||||
const session = new OnlineSession()
|
||||
session.handleServerEvent({
|
||||
type: 'LoadAct',
|
||||
act: 0,
|
||||
mapSeed: 0x11223344,
|
||||
areaId: 1, // Rogue Encampment (Town)
|
||||
})
|
||||
session.handleServerEvent({
|
||||
type: 'UnitAssign',
|
||||
isSelf: true,
|
||||
unit: { unitType: 0, id: 1, classId: 1, name: 'Self', x: 5100, y: 5050 },
|
||||
})
|
||||
|
||||
// Town Idle -> TN (5)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 1)?.mode).toBe(5)
|
||||
expect(PLR_MODES[5]?.token).toBe('TN')
|
||||
|
||||
// Town Walk -> TW (6)
|
||||
session.predictor.setPath([{ x: 5120, y: 5050 }], false)
|
||||
session.tick(40, 40)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 1)?.mode).toBe(6)
|
||||
expect(PLR_MODES[6]?.token).toBe('TW')
|
||||
|
||||
// Town Run -> RN (3) (1.13c D2Game sub_6FC7F600: running in town still uses PLRMODE_RUN=3)
|
||||
session.predictor.setPath([{ x: 5140, y: 5050 }], true)
|
||||
session.tick(40, 80)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 1)?.mode).toBe(3)
|
||||
expect(PLR_MODES[3]?.token).toBe('RN')
|
||||
|
||||
// Enter Blood Moor (areaId = 2, non-town) while running -> stays RN (3)
|
||||
session.handleServerEvent({
|
||||
type: 'LoadAct',
|
||||
act: 0,
|
||||
mapSeed: 0x11223344,
|
||||
areaId: 2,
|
||||
})
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 1)?.mode).toBe(3)
|
||||
|
||||
// Switch to walking in Blood Moor -> WL (2)
|
||||
session.predictor.setPath([{ x: 5160, y: 5050 }], false)
|
||||
session.tick(40, 120)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 1)?.mode).toBe(2)
|
||||
expect(PLR_MODES[2]?.token).toBe('WL')
|
||||
|
||||
// Stop in Blood Moor -> NU (1)
|
||||
session.predictor.setPosition(5160, 5050)
|
||||
expect(session.buildRenderableUnits().find((u) => u.id === 1)?.mode).toBe(1)
|
||||
expect(PLR_MODES[1]?.token).toBe('NU')
|
||||
|
||||
// Local player dies -> mode must be 17 (DD = Dead/Corpse), never 12 (KK = Kick)
|
||||
expect(PLR_MODES[12]?.token).toBe('KK')
|
||||
expect(PLR_MODES[17]?.token).toBe('DD')
|
||||
session.handleServerEvent({
|
||||
type: 'LifeMana',
|
||||
life: 0,
|
||||
mana: 0,
|
||||
stamina: 0,
|
||||
at: { x: 5160, y: 5050 },
|
||||
vx: 0,
|
||||
vy: 0,
|
||||
})
|
||||
const deadSelf = session.buildRenderableUnits().find((u) => u.id === 1)!
|
||||
expect(deadSelf.isCorpse).toBe(true)
|
||||
expect(deadSelf.mode).toBe(17)
|
||||
expect(deadSelf.mode).not.toBe(12)
|
||||
})
|
||||
|
||||
it('4. advances frames at 1.13c 25Hz (40ms tick) 8.8 fixed-point AnimData.d2 speed and reports missing clip speed', () => {
|
||||
// speed = 256 -> 1 frame per 40ms tick (25 fps)
|
||||
expect(computeAnimDataFrameIndex(0, 256, 8)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(39, 256, 8)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(40, 256, 8)).toBe(1)
|
||||
expect(computeAnimDataFrameIndex(80, 256, 8)).toBe(2)
|
||||
expect(computeAnimDataFrameIndex(320, 256, 8)).toBe(0) // loops at 8 frames
|
||||
|
||||
// speed = 128 (SONUHTH) -> 1 frame per 80ms (12.5 fps)
|
||||
expect(computeAnimDataFrameIndex(0, 128, 8)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(40, 128, 8)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(79, 128, 8)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(80, 128, 8)).toBe(1)
|
||||
expect(computeAnimDataFrameIndex(160, 128, 8)).toBe(2)
|
||||
|
||||
// speed = 80 (SOTNHTH) -> 1 frame per 128ms (7.8125 fps)
|
||||
expect(computeAnimDataFrameIndex(0, 80, 16)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(127, 80, 16)).toBe(0)
|
||||
expect(computeAnimDataFrameIndex(128, 80, 16)).toBe(1)
|
||||
expect(computeAnimDataFrameIndex(256, 80, 16)).toBe(2)
|
||||
|
||||
// Non-looping death animation (DT, loop = false) clamps to frameCount - 1
|
||||
expect(computeAnimDataFrameIndex(10000, 256, 4, false)).toBe(3)
|
||||
|
||||
// Fail-fast diagnostic when clip speed metadata is missing
|
||||
clearMissingArtEntries()
|
||||
const automap = new AutomapView({ initialMode: 'level' })
|
||||
const levelView = automap.setContext(0x11223344, 0, 1)
|
||||
const baseX = levelView.subOriginX + 50
|
||||
const baseY = levelView.subOriginY + 50
|
||||
const source = new ClientWorldSceneSource(
|
||||
(): ClientSceneWorldAdapter => ({
|
||||
tick: 1,
|
||||
actId: 1,
|
||||
levelId: 1,
|
||||
playerSubX: baseX,
|
||||
playerSubY: baseY,
|
||||
units: [
|
||||
{
|
||||
kind: 'player',
|
||||
id: 1,
|
||||
classId: 1,
|
||||
x: baseX,
|
||||
y: baseY,
|
||||
mode: 5, // TN
|
||||
direction: 0,
|
||||
isLocalPlayer: true,
|
||||
},
|
||||
],
|
||||
levelView,
|
||||
}),
|
||||
'800x600',
|
||||
)
|
||||
const brokenCharArt: LoadedCharacter = {
|
||||
handle: { index: 1, width: 64, height: 64 } as never,
|
||||
groups: [[{ x: 0, y: 0, width: 32, height: 64 }]],
|
||||
directions: 1,
|
||||
walk: 0,
|
||||
stand: 0,
|
||||
members: 1,
|
||||
// Missing 'tn' clip!
|
||||
clips: {},
|
||||
}
|
||||
const fakeRenderer = {
|
||||
begin() {},
|
||||
draw() {},
|
||||
setLighting() {},
|
||||
setPalette() {},
|
||||
flush() {},
|
||||
}
|
||||
renderSceneSourceFrame({
|
||||
source,
|
||||
lightingPreset: 'fullbright',
|
||||
renderer: fakeRenderer as never,
|
||||
characterArtByToken: new Map([['so', brokenCharArt]]),
|
||||
nowMs: 0,
|
||||
})
|
||||
expect(
|
||||
getMissingArtEntries().some((e) => e.category === 'player-clip' && e.id.toLowerCase() === 'so:tn'),
|
||||
).toBe(true)
|
||||
clearMissingArtEntries()
|
||||
})
|
||||
})
|
||||
Loading…
Reference in New Issue