Integrate role asset studio into custom world agent flow

This commit is contained in:
2026-04-14 20:16:41 +08:00
parent 0981d6ee1b
commit bc2999ffb9
118 changed files with 31211 additions and 1232 deletions

View File

@@ -1,7 +1,9 @@
import { describe, expect, it } from 'vitest';
import type { GameState, StoryMoment } from '../types';
import { WorldType } from '../types';
import {
rehydrateSavedSnapshot,
resolveHydratedSnapshotState,
} from './runtimeSnapshot';
@@ -16,6 +18,97 @@ function createStory(
};
}
function createHydratedBattleSnapshot(
gameStateOverrides: Partial<GameState> = {},
) {
return {
version: 3,
savedAt: '2026-04-14T00:00:00.000Z',
bottomTab: 'adventure' as const,
currentStory: createStory('战斗故事'),
gameState: {
worldType: WorldType.WUXIA,
customWorldProfile: null,
playerCharacter: {
id: 'hero',
},
runtimeActionVersion: 3,
runtimeSessionId: 'runtime-main',
currentScene: 'Story',
runtimeStats: {
playTimeMs: 0,
lastPlayTickAt: null,
hostileNpcsDefeated: 0,
questsAccepted: 0,
itemsUsed: 0,
scenesTraveled: 0,
},
storyHistory: [],
characterChats: {},
animationState: 'idle',
currentEncounter: {
kind: 'npc',
id: 'npc-fighter',
npcName: '断桥客',
npcDescription: '拦路的刀客',
context: '断桥对峙',
hostile: false,
},
npcInteractionActive: false,
currentScenePreset: null,
sceneHostileNpcs: [
{
id: 'npc-fighter',
name: '断桥客',
hp: 18,
maxHp: 32,
description: '拦路的刀客',
},
],
playerX: 0,
playerOffsetY: 0,
playerFacing: 'right',
playerActionMode: 'idle',
scrollWorld: false,
inBattle: true,
playerHp: 40,
playerMaxHp: 40,
playerMana: 18,
playerMaxMana: 18,
playerSkillCooldowns: {},
activeCombatEffects: [],
playerCurrency: 0,
playerInventory: [],
playerEquipment: {
weapon: null,
armor: null,
relic: null,
},
npcStates: {
'npc-fighter': {
affinity: -16,
chattedCount: 0,
helpUsed: false,
giftsGiven: 0,
inventory: [],
recruited: false,
},
},
quests: [],
roster: [],
companions: [],
currentBattleNpcId: 'npc-fighter',
currentNpcBattleMode: 'fight',
currentNpcBattleOutcome: null,
sparReturnEncounter: null,
sparPlayerHpBefore: null,
sparPlayerMaxHpBefore: null,
sparStoryHistoryBefore: null,
...gameStateOverrides,
} as unknown as GameState,
};
}
describe('runtimeSnapshot', () => {
it('keeps server-hydrated snapshots unchanged', () => {
const snapshot = {
@@ -72,4 +165,65 @@ describe('runtimeSnapshot', () => {
expect(hydrated.gameState.playerMaxMana).toBe(12);
expect(hydrated.gameState.playerMana).toBe(12);
});
it('rehydrates minimal npc battle snapshots into renderable combatants', () => {
const snapshot = createHydratedBattleSnapshot();
const hydrated = rehydrateSavedSnapshot(snapshot);
const hostileNpc = hydrated.gameState.sceneHostileNpcs[0];
expect(hydrated).not.toBe(snapshot);
expect(hostileNpc).toEqual(
expect.objectContaining({
id: 'npc-fighter',
name: '断桥客',
description: '拦路的刀客',
hp: 18,
maxHp: 32,
attackRange: expect.any(Number),
speed: expect.any(Number),
animation: 'idle',
renderKind: 'npc',
encounter: expect.objectContaining({
kind: 'npc',
id: 'npc-fighter',
npcName: '断桥客',
xMeters: expect.any(Number),
}),
}),
);
});
it('does not rewrite already renderable npc battle snapshots', () => {
const snapshot = createHydratedBattleSnapshot({
sceneHostileNpcs: [
{
id: 'npc-fighter',
name: '断桥客',
action: '摆开架势,随时准备出手',
description: '拦路的刀客',
animation: 'idle',
xMeters: 3.2,
yOffset: 0,
facing: 'left',
attackRange: 1.8,
speed: 7,
hp: 18,
maxHp: 32,
renderKind: 'npc',
encounter: {
kind: 'npc',
id: 'npc-fighter',
npcName: '断桥客',
npcDescription: '拦路的刀客',
npcAvatar: '',
context: '断桥对峙',
xMeters: 3.2,
},
},
],
});
expect(rehydrateSavedSnapshot(snapshot)).toBe(snapshot);
});
});

View File

@@ -1,4 +1,15 @@
import type { GameState, StoryMoment } from '../types';
import {
buildInitialNpcState,
createNpcBattleMonster,
} from '../data/npcInteractions';
import type {
Encounter,
GameState,
NpcPersistentState,
SceneHostileNpc,
StoryMoment,
} from '../types';
import { WorldType } from '../types';
import type { BottomTab } from '../types/navigation';
import type {
HydratableGameState,
@@ -37,6 +48,199 @@ function createEmptyEquipmentLoadout() {
} satisfies GameState['playerEquipment'];
}
function resolveHydrationWorldType(worldType: GameState['worldType']) {
const normalizedWorldType =
typeof worldType === 'string' ? worldType.toUpperCase() : worldType;
if (normalizedWorldType === WorldType.WUXIA) {
return WorldType.WUXIA;
}
if (normalizedWorldType === WorldType.XIANXIA) {
return WorldType.XIANXIA;
}
if (normalizedWorldType === WorldType.CUSTOM) {
return WorldType.CUSTOM;
}
return null;
}
function hasRenderableRuntimeNpcBattleFields(hostileNpc: SceneHostileNpc) {
const candidate = hostileNpc as Partial<SceneHostileNpc>;
return Boolean(
candidate.encounter &&
typeof candidate.animation === 'string' &&
typeof candidate.xMeters === 'number' &&
typeof candidate.yOffset === 'number' &&
typeof candidate.facing === 'string' &&
typeof candidate.attackRange === 'number' &&
typeof candidate.speed === 'number',
);
}
function normalizeRuntimeBattleEncounter(
encounter: GameState['currentEncounter'],
): Encounter | null {
if (!encounter || encounter.kind !== 'npc') {
return null;
}
const npcName =
typeof encounter.npcName === 'string' ? encounter.npcName.trim() : '';
if (!npcName) {
return null;
}
return {
...encounter,
kind: 'npc',
npcName,
npcDescription:
typeof encounter.npcDescription === 'string'
? encounter.npcDescription
: '',
npcAvatar:
typeof encounter.npcAvatar === 'string' ? encounter.npcAvatar : '',
context: typeof encounter.context === 'string' ? encounter.context : '',
hostile: true,
} satisfies Encounter;
}
function resolveRuntimeNpcBattleState(
gameState: Pick<
GameState,
| 'currentBattleNpcId'
| 'currentEncounter'
| 'customWorldProfile'
| 'npcStates'
| 'sceneHostileNpcs'
| 'worldType'
>,
) {
const encounter = normalizeRuntimeBattleEncounter(gameState.currentEncounter);
if (!encounter || gameState.sceneHostileNpcs.length === 0) {
return null;
}
const npcStateKey =
gameState.currentBattleNpcId ??
encounter.id ??
encounter.npcName;
const npcState =
gameState.npcStates[npcStateKey] ??
buildInitialNpcState(
encounter,
resolveHydrationWorldType(gameState.worldType),
gameState as GameState,
);
return {
encounter,
npcState,
};
}
function hydrateRuntimeNpcBattleMonster(params: {
hostileNpc: SceneHostileNpc;
encounter: Encounter;
npcState: NpcPersistentState;
gameState: Pick<GameState, 'customWorldProfile' | 'worldType'>;
battleMode: NonNullable<GameState['currentNpcBattleMode']>;
}) {
const template = createNpcBattleMonster(
params.encounter,
params.npcState,
params.battleMode,
{
worldType: resolveHydrationWorldType(params.gameState.worldType),
customWorldProfile: params.gameState.customWorldProfile,
},
);
const candidate = params.hostileNpc as Partial<SceneHostileNpc>;
const xMeters =
typeof candidate.xMeters === 'number' ? candidate.xMeters : template.xMeters;
const yOffset =
typeof candidate.yOffset === 'number' ? candidate.yOffset : template.yOffset;
return {
...template,
id:
typeof candidate.id === 'string' && candidate.id.trim()
? candidate.id
: template.id,
name:
typeof candidate.name === 'string' && candidate.name.trim()
? candidate.name
: template.name,
description:
typeof candidate.description === 'string'
? candidate.description
: template.description,
hp: typeof candidate.hp === 'number' ? candidate.hp : template.hp,
maxHp:
typeof candidate.maxHp === 'number' ? candidate.maxHp : template.maxHp,
animation:
typeof candidate.animation === 'string'
? candidate.animation
: template.animation,
xMeters,
yOffset,
facing:
candidate.facing === 'left' || candidate.facing === 'right'
? candidate.facing
: template.facing,
attackRange:
typeof candidate.attackRange === 'number'
? candidate.attackRange
: template.attackRange,
speed:
typeof candidate.speed === 'number' ? candidate.speed : template.speed,
encounter: {
...template.encounter,
xMeters,
},
} satisfies SceneHostileNpc;
}
export function hydrateRuntimeNpcBattleGameState(
gameState: HydratedGameState,
): HydratedGameState {
const battleMode = gameState.currentNpcBattleMode;
if (
gameState.inBattle !== true ||
(battleMode !== 'fight' && battleMode !== 'spar') ||
gameState.currentEncounter?.kind !== 'npc' ||
gameState.sceneHostileNpcs.length === 0 ||
gameState.sceneHostileNpcs.every(hasRenderableRuntimeNpcBattleFields)
) {
return gameState;
}
const resolvedState = resolveRuntimeNpcBattleState(gameState);
if (!resolvedState) {
return gameState;
}
return {
...gameState,
sceneHostileNpcs: gameState.sceneHostileNpcs.map((hostileNpc) =>
hasRenderableRuntimeNpcBattleFields(hostileNpc)
? hostileNpc
: hydrateRuntimeNpcBattleMonster({
hostileNpc,
encounter: resolvedState.encounter,
npcState: resolvedState.npcState,
gameState,
battleMode,
}),
),
};
}
export function normalizeSavedStory(story: StoryMoment | null) {
if (!story) {
return null;
@@ -57,7 +261,7 @@ export function normalizeSavedGameState(gameState: GameState) {
const playerMaxHp = Math.max(1, hydratableState.playerMaxHp);
const playerMaxMana = Math.max(1, hydratableState.playerMaxMana);
return {
return hydrateRuntimeNpcBattleGameState({
...hydratableState,
playerMaxHp,
playerHp: Math.min(hydratableState.playerHp, playerMaxHp),
@@ -72,7 +276,7 @@ export function normalizeSavedGameState(gameState: GameState) {
typeof hydratableState.runtimeSessionId === 'string'
? hydratableState.runtimeSessionId
: null,
} satisfies HydratedGameState;
} satisfies HydratedGameState);
}
export function hydrateSnapshotState(snapshot: {
@@ -105,8 +309,23 @@ export function isHydratedSnapshotState(
);
}
export function rehydrateSavedSnapshot<T extends HydratedSnapshotState>(
snapshot: T,
): T {
const hydratedGameState = hydrateRuntimeNpcBattleGameState(snapshot.gameState);
if (hydratedGameState === snapshot.gameState) {
return snapshot;
}
return {
...snapshot,
gameState: hydratedGameState,
};
}
export function resolveHydratedSnapshotState(snapshot: SnapshotState) {
return isHydratedSnapshotState(snapshot)
? snapshot
? rehydrateSavedSnapshot(snapshot)
: hydrateSnapshotState(snapshot);
}