import { Component } from "../ecs/components.js"; import { SkillId, playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js"; import { combatDamageXp, spellCastXpFromDifficulty } from "../../../shared/skills/progression.js"; import { applySkillXp, publicSkillsPayload, storedSkillXp } from "../../../shared/skills/state.js"; import { maxHealthMultiplier } from "../../../shared/equipment/jewelryEffects.js"; export function syncPlayerHealth (ecs, handle) { const character = ecs.get(handle, Component.character); if (!character) { return; } const skills = ecs.get(handle, Component.skills); const health = ecs.get(handle, Component.health); if (!skills || !health) { return; } const equipment = ecs.get(handle, Component.equipment); const max = Math.max(1, Math.floor(playerMaxHealth(skills) * maxHealthMultiplier(equipment))); health.max = max; health.current = Math.min(health.current | 0, max); } export function syncPlayerStamina (ecs, handle) { const character = ecs.get(handle, Component.character); if (!character) { return; } const skills = ecs.get(handle, Component.skills); const stamina = ecs.get(handle, Component.stamina); if (!skills || !stamina) { return; } const max = playerMaxStamina(skills); stamina.max = max; stamina.current = Math.min(Number(stamina.current) || 0, max); } export function awardCombatHitXp (ecs, attackerHandle, profile, damage) { const amount = combatDamageXp(damage); if (!amount) { return null; } const skills = ecs.get(attackerHandle, Component.skills); if (!skills?.xp) { return null; } const equipment = ecs.get(attackerHandle, Component.equipment); const levelUps = []; let changed = false; if (profile.hitSkillId != null) { const hit = applySkillXp(skills, profile.hitSkillId, amount, equipment); if (hit.levelUp) { levelUps.push(hit.levelUp); } changed = changed || hit.changed; } if (profile.damageSkillId != null) { const damageSkill = applySkillXp(skills, profile.damageSkillId, amount, equipment); if (damageSkill.levelUp) { levelUps.push(damageSkill.levelUp); } changed = changed || damageSkill.changed; } if (!changed) { return null; } return { ...publicSkillsPayload(skills), stored: storedSkillXp(skills), levelUps }; } /** Magic XP on a successful cast; Mind XP uses {@link awardCombatHitXp} when the spell lands. */ export function awardSpellCastXp (ecs, casterHandle, spellDifficulty) { const amount = spellCastXpFromDifficulty(spellDifficulty); const skills = ecs.get(casterHandle, Component.skills); if (!skills?.xp) { return null; } const equipment = ecs.get(casterHandle, Component.equipment); const result = applySkillXp(skills, SkillId.magic, amount, equipment); if (!result.changed) { return null; } const character = ecs.get(casterHandle, Component.character); return { characterId: character?.characterId ?? null, ...publicSkillsPayload(skills), stored: storedSkillXp(skills), levelUps: result.levelUp ? [result.levelUp] : [] }; } export function awardDamageHealthXp (ecs, targetHandle, damage) { const amount = Math.max(0, Math.floor(Number(damage) || 0)); if (!amount) { return null; } const health = ecs.get(targetHandle, Component.health); // Lethal hits grant no Health XP — only damage you survive counts. if (!health || (health.current | 0) <= 0) { return null; } const character = ecs.get(targetHandle, Component.character); if (!character) { return null; } const skills = ecs.get(targetHandle, Component.skills); if (!skills?.xp) { return null; } const equipment = ecs.get(targetHandle, Component.equipment); const result = applySkillXp(skills, SkillId.health, amount, equipment); if (!result.changed) { return null; } syncPlayerHealth(ecs, targetHandle); return { characterId: character.characterId, ...publicSkillsPayload(skills), stored: storedSkillXp(skills), levelUps: result.levelUp ? [result.levelUp] : [] }; } /** XP for avoiding damage — Defence on block, Agility on dodge (players only). */ export function awardCombatAvoidanceXp (ecs, targetHandle, skillId, avoidedDamage) { const amount = combatDamageXp(avoidedDamage); if (!amount) { return null; } const character = ecs.get(targetHandle, Component.character); if (!character) { return null; } const skills = ecs.get(targetHandle, Component.skills); if (!skills?.xp) { return null; } const equipment = ecs.get(targetHandle, Component.equipment); const result = applySkillXp(skills, skillId, amount, equipment); if (!result.changed) { return null; } return { characterId: character.characterId, ...publicSkillsPayload(skills), stored: storedSkillXp(skills), levelUps: result.levelUp ? [result.levelUp] : [] }; }