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