| 1 | /** |
| 2 | * Timed status effects from potions (and later other sources). |
| 3 | * Server applies; clients get visual fields via entity replication + private blind flag. |
| 4 | */ |
| 5 | |
| 6 | export const StatusEffectType = Object.freeze({ |
| 7 | healthRegen: "health-regen", |
| 8 | staminaRegen: "stamina-regen", |
| 9 | strengthBonus: "strength-bonus", |
| 10 | mindBonus: "mind-bonus", |
| 11 | staminaDrain: "stamina-drain", |
| 12 | glow: "glow", |
| 13 | shrink: "shrink", |
| 14 | blind: "blind" |
| 15 | }); |
| 16 | |
| 17 | /** Effects where drinking again extends remaining duration instead of replacing. */ |
| 18 | const EXTEND_TYPES = new Set([ |
| 19 | StatusEffectType.healthRegen, |
| 20 | StatusEffectType.staminaRegen |
| 21 | ]); |
| 22 | |
| 23 | export function emptyStatusEffects () |
| 24 | { |
| 25 | return { effects: [] }; |
| 26 | } |
| 27 | |
| 28 | export function pruneStatusEffects (component, now) |
| 29 | { |
| 30 | if (!component?.effects) |
| 31 | { return component; } |
| 32 | component.effects = component.effects.filter((entry) => Number(entry.expiresAt) > now); |
| 33 | return component; |
| 34 | } |
| 35 | |
| 36 | export function activeStatusEffects (component, now) |
| 37 | { |
| 38 | pruneStatusEffects(component, now); |
| 39 | return component?.effects ?? []; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Apply a potion/status effect definition `{ type, durationMs, ... }`. |
| 44 | * Regen effects stack duration; others refresh the same type. |
| 45 | */ |
| 46 | export function applyStatusEffect (component, effect, now = Date.now()) |
| 47 | { |
| 48 | if (!component || !effect?.type) |
| 49 | { return component; } |
| 50 | const type = effect.type; |
| 51 | const durationMs = Math.max(0, effect.durationMs | 0); |
| 52 | if (!component.effects) |
| 53 | { component.effects = []; } |
| 54 | pruneStatusEffects(component, now); |
| 55 | |
| 56 | const existing = component.effects.find((entry) => entry.type === type); |
| 57 | if (existing && EXTEND_TYPES.has(type) && durationMs > 0) |
| 58 | { |
| 59 | existing.expiresAt = Math.max(Number(existing.expiresAt) || 0, now) + durationMs; |
| 60 | if (effect.amountPerSecond != null) |
| 61 | { existing.amountPerSecond = Number(effect.amountPerSecond) || existing.amountPerSecond; } |
| 62 | if (effect.amount != null) |
| 63 | { existing.amount = effect.amount | 0; } |
| 64 | return component; |
| 65 | } |
| 66 | |
| 67 | if (existing) |
| 68 | { |
| 69 | existing.expiresAt = now + durationMs; |
| 70 | existing.amount = effect.amount | 0 || existing.amount; |
| 71 | existing.amountPerSecond = Number(effect.amountPerSecond) || existing.amountPerSecond; |
| 72 | existing.lightRadiusTiles = effect.lightRadiusTiles ?? existing.lightRadiusTiles; |
| 73 | existing.scale = effect.scale ?? existing.scale; |
| 74 | return component; |
| 75 | } |
| 76 | |
| 77 | component.effects.push({ |
| 78 | type, |
| 79 | expiresAt: now + durationMs, |
| 80 | amount: effect.amount | 0 || 0, |
| 81 | amountPerSecond: Number(effect.amountPerSecond) || 0, |
| 82 | lightRadiusTiles: effect.lightRadiusTiles ?? null, |
| 83 | scale: effect.scale ?? null |
| 84 | }); |
| 85 | return component; |
| 86 | } |
| 87 | |
| 88 | export function statusSkillBonuses (component, now = Date.now()) |
| 89 | { |
| 90 | const bonuses = { strength: 0, mind: 0 }; |
| 91 | for (const entry of activeStatusEffects(component, now)) |
| 92 | { |
| 93 | if (entry.type === StatusEffectType.strengthBonus) |
| 94 | { bonuses.strength += entry.amount | 0; } |
| 95 | if (entry.type === StatusEffectType.mindBonus) |
| 96 | { bonuses.mind += entry.amount | 0; } |
| 97 | } |
| 98 | return bonuses; |
| 99 | } |
| 100 | |
| 101 | export function statusVisualScale (component, now = Date.now()) |
| 102 | { |
| 103 | for (const entry of activeStatusEffects(component, now)) |
| 104 | { |
| 105 | if (entry.type === StatusEffectType.shrink && entry.scale != null) |
| 106 | { return Number(entry.scale) || 1; } |
| 107 | } |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | export function statusExtraLightRadius (component, now = Date.now()) |
| 112 | { |
| 113 | let best = 0; |
| 114 | for (const entry of activeStatusEffects(component, now)) |
| 115 | { |
| 116 | if (entry.type === StatusEffectType.glow) |
| 117 | { best = Math.max(best, Number(entry.lightRadiusTiles) || 0); } |
| 118 | } |
| 119 | return best; |
| 120 | } |
| 121 | |
| 122 | export function hasStatusEffect (component, type, now = Date.now()) |
| 123 | { |
| 124 | return activeStatusEffects(component, now).some((entry) => entry.type === type); |
| 125 | } |
| 126 | |
| 127 | export function publicStatusEffectsView (component, now = Date.now()) |
| 128 | { |
| 129 | return activeStatusEffects(component, now).map((entry) => ({ |
| 130 | type: entry.type, |
| 131 | expiresAt: Number(entry.expiresAt) || 0, |
| 132 | amount: entry.amount | 0, |
| 133 | amountPerSecond: Number(entry.amountPerSecond) || 0, |
| 134 | lightRadiusTiles: entry.lightRadiusTiles, |
| 135 | scale: entry.scale |
| 136 | })); |
| 137 | } |