Files
Genarrative/src/data/storyRecovery.ts
kdletters cbc27bad4a
Some checks failed
CI / verify (push) Has been cancelled
init with react+axum+spacetimedb
2026-04-26 18:06:23 +08:00

60 lines
1.4 KiB
TypeScript

import type { CompanionState, GameState } from '../types';
import {
getCharacterById,
getCharacterCombatStats,
getCharacterMaxHp,
} from './characterPresets';
function recoverCompanion(
companion: CompanionState,
state: Pick<GameState, 'worldType' | 'customWorldProfile'>,
) {
if (companion.hp <= 0) {
return companion;
}
const character = getCharacterById(companion.characterId);
if (!character) {
return companion;
}
const recovery = getCharacterCombatStats(
character,
state.worldType,
state.customWorldProfile,
).storyRecovery;
const maxHp = Math.max(
companion.maxHp,
getCharacterMaxHp(character, state.worldType, state.customWorldProfile),
);
return {
...companion,
maxHp,
hp: Math.min(maxHp, companion.hp + recovery),
};
}
export function applyStoryReasoningRecovery(state: GameState) {
if (!state.playerCharacter) {
return state;
}
const playerRecovery = state.playerHp > 0
? getCharacterCombatStats(
state.playerCharacter,
state.worldType,
state.customWorldProfile,
).storyRecovery
: 0;
return {
...state,
playerHp: state.playerHp > 0
? Math.min(state.playerMaxHp, state.playerHp + playerRecovery)
: state.playerHp,
companions: state.companions.map(companion => recoverCompanion(companion, state)),
roster: state.roster.map(companion => recoverCompanion(companion, state)),
};
}