BlueGit

timfa questward

Files

questward / shared / statusEffects / rules.js

rules.js

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