/** * Timed status effects from potions (and later other sources). * Server applies; clients get visual fields via entity replication + private blind flag. */ export const StatusEffectType = Object.freeze({ healthRegen: "health-regen", staminaRegen: "stamina-regen", strengthBonus: "strength-bonus", mindBonus: "mind-bonus", staminaDrain: "stamina-drain", glow: "glow", shrink: "shrink", blind: "blind" }); /** Effects where drinking again extends remaining duration instead of replacing. */ const EXTEND_TYPES = new Set([ StatusEffectType.healthRegen, StatusEffectType.staminaRegen ]); export function emptyStatusEffects () { return { effects: [] }; } export function pruneStatusEffects (component, now) { if (!component?.effects) { return component; } component.effects = component.effects.filter((entry) => Number(entry.expiresAt) > now); return component; } export function activeStatusEffects (component, now) { pruneStatusEffects(component, now); return component?.effects ?? []; } /** * Apply a potion/status effect definition `{ type, durationMs, ... }`. * Regen effects stack duration; others refresh the same type. */ export function applyStatusEffect (component, effect, now = Date.now()) { if (!component || !effect?.type) { return component; } const type = effect.type; const durationMs = Math.max(0, effect.durationMs | 0); if (!component.effects) { component.effects = []; } pruneStatusEffects(component, now); const existing = component.effects.find((entry) => entry.type === type); if (existing && EXTEND_TYPES.has(type) && durationMs > 0) { existing.expiresAt = Math.max(Number(existing.expiresAt) || 0, now) + durationMs; if (effect.amountPerSecond != null) { existing.amountPerSecond = Number(effect.amountPerSecond) || existing.amountPerSecond; } if (effect.amount != null) { existing.amount = effect.amount | 0; } return component; } if (existing) { existing.expiresAt = now + durationMs; existing.amount = effect.amount | 0 || existing.amount; existing.amountPerSecond = Number(effect.amountPerSecond) || existing.amountPerSecond; existing.lightRadiusTiles = effect.lightRadiusTiles ?? existing.lightRadiusTiles; existing.scale = effect.scale ?? existing.scale; return component; } component.effects.push({ type, expiresAt: now + durationMs, amount: effect.amount | 0 || 0, amountPerSecond: Number(effect.amountPerSecond) || 0, lightRadiusTiles: effect.lightRadiusTiles ?? null, scale: effect.scale ?? null }); return component; } export function statusSkillBonuses (component, now = Date.now()) { const bonuses = { strength: 0, mind: 0 }; for (const entry of activeStatusEffects(component, now)) { if (entry.type === StatusEffectType.strengthBonus) { bonuses.strength += entry.amount | 0; } if (entry.type === StatusEffectType.mindBonus) { bonuses.mind += entry.amount | 0; } } return bonuses; } export function statusVisualScale (component, now = Date.now()) { for (const entry of activeStatusEffects(component, now)) { if (entry.type === StatusEffectType.shrink && entry.scale != null) { return Number(entry.scale) || 1; } } return 1; } export function statusExtraLightRadius (component, now = Date.now()) { let best = 0; for (const entry of activeStatusEffects(component, now)) { if (entry.type === StatusEffectType.glow) { best = Math.max(best, Number(entry.lightRadiusTiles) || 0); } } return best; } export function hasStatusEffect (component, type, now = Date.now()) { return activeStatusEffects(component, now).some((entry) => entry.type === type); } export function publicStatusEffectsView (component, now = Date.now()) { return activeStatusEffects(component, now).map((entry) => ({ type: entry.type, expiresAt: Number(entry.expiresAt) || 0, amount: entry.amount | 0, amountPerSecond: Number(entry.amountPerSecond) || 0, lightRadiusTiles: entry.lightRadiusTiles, scale: entry.scale })); }