BlueGit

timfa questward

Merge branch 'fix/trade-toast-death-attacks'

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
7c2b7cbf9601cf5ce5f7f3c1abbb60f8a8697385
13 files changed +357 −56
client/src/play/main.js +33 −3
@@ -969,19 +969,42 @@ function flushPendingInteract ()
969 969 return;
970 970 }
971 971
972 if (pendingInteract.kind === "trade-request")
973 {
974 const target = view.entities.get(pendingInteract.targetEntityId);
975 if (!target)
976 {
977 clearPendingInteract();
978 return;
979 }
980 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 {
983 approachAdjacentTile(target.tileX, target.tileY);
984 return;
985 }
986 const targetEntityId = pendingInteract.targetEntityId;
987 clearPendingInteract();
988 trade?.flushPendingRequest?.(targetEntityId);
989 return;
990 }
991
972 992 if (pendingInteract.kind === "trade-accept")
973 993 {
974 994 const target = view.entities.get(pendingInteract.fromEntityId);
975 995 if (!target)
976 996 {
977 pendingInteract = null;
997 clearPendingInteract();
978 998 return;
979 999 }
980 1000 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 1001 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 { return; }
1002 {
1003 approachAdjacentTile(target.tileX, target.tileY);
1004 return;
1005 }
983 1006 const invite = pendingInteract.invite;
984 pendingInteract = null;
1007 clearPendingInteract();
985 1008 trade?.flushPendingAccept?.(invite);
986 1009 return;
987 1010 }
@@ -1898,6 +1921,13 @@ async function main ()
1898 1921 },
1899 1922 approachAdjacentTile,
1900 1923 clearPendingActions: clearAllPendingActions,
1924 setPendingTradeRequest (targetEntityId)
1925 {
1926 pendingInteract = {
1927 kind: "trade-request",
1928 targetEntityId: targetEntityId | 0
1929 };
1930 },
1901 1931 setPendingTradeAccept (invite)
1902 1932 {
1903 1933 pendingInteract = {
@@ -969,19 +969,42 @@ function flushPendingInteract ()
969 return;
969 return;
970 }
970 }
971
971
972 if (pendingInteract.kind === "trade-request")
973 {
974 const target = view.entities.get(pendingInteract.targetEntityId);
975 if (!target)
976 {
977 clearPendingInteract();
978 return;
979 }
980 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 {
983 approachAdjacentTile(target.tileX, target.tileY);
984 return;
985 }
986 const targetEntityId = pendingInteract.targetEntityId;
987 clearPendingInteract();
988 trade?.flushPendingRequest?.(targetEntityId);
989 return;
990 }
991
972 if (pendingInteract.kind === "trade-accept")
992 if (pendingInteract.kind === "trade-accept")
973 {
993 {
974 const target = view.entities.get(pendingInteract.fromEntityId);
994 const target = view.entities.get(pendingInteract.fromEntityId);
975 if (!target)
995 if (!target)
976 {
996 {
977 pendingInteract = null;
997 clearPendingInteract();
978 return;
998 return;
979 }
999 }
980 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
1000 if (!isAdjacentTile(local.tileX, local.tileY, target.tileX, target.tileY)
981 && !(local.tileX === target.tileX && local.tileY === target.tileY))
1001 && !(local.tileX === target.tileX && local.tileY === target.tileY))
982 { return; }
1002 {
1003 approachAdjacentTile(target.tileX, target.tileY);
1004 return;
1005 }
983 const invite = pendingInteract.invite;
1006 const invite = pendingInteract.invite;
984 pendingInteract = null;
1007 clearPendingInteract();
985 trade?.flushPendingAccept?.(invite);
1008 trade?.flushPendingAccept?.(invite);
986 return;
1009 return;
987 }
1010 }
@@ -1898,6 +1921,13 @@ async function main ()
1898 },
1921 },
1899 approachAdjacentTile,
1922 approachAdjacentTile,
1900 clearPendingActions: clearAllPendingActions,
1923 clearPendingActions: clearAllPendingActions,
1924 setPendingTradeRequest (targetEntityId)
1925 {
1926 pendingInteract = {
1927 kind: "trade-request",
1928 targetEntityId: targetEntityId | 0
1929 };
1930 },
1901 setPendingTradeAccept (invite)
1931 setPendingTradeAccept (invite)
1902 {
1932 {
1903 pendingInteract = {
1933 pendingInteract = {
client/src/play/toasts.js +12 −2
@@ -32,7 +32,10 @@ export function initToasts ()
32 32 const text = String(message ?? "").trim();
33 33 if (!text)
34 34 { return { dismiss () {} }; }
35 const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
35 const sticky = !!options.sticky;
36 const durationMs = sticky
37 ? null
38 : Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
36 39 const onClick = typeof options.onClick === "function" ? options.onClick : null;
37 40 const actions = Array.isArray(options.actions) ? options.actions : [];
38 41 const el = document.createElement("div");
@@ -43,11 +46,17 @@ export function initToasts ()
43 46 textEl.textContent = text;
44 47 el.append(textEl);
45 48 let dismissed = false;
49 let timer = null;
46 50 const dismiss = () =>
47 51 {
48 52 if (dismissed)
49 53 { return; }
50 54 dismissed = true;
55 if (timer != null)
56 {
57 window.clearTimeout(timer);
58 timer = null;
59 }
51 60 el.classList.remove("toast-visible");
52 61 el.classList.add("toast-leaving");
53 62 window.setTimeout(() => el.remove(), LEAVE_MS);
@@ -88,7 +97,8 @@ export function initToasts ()
88 97 {
89 98 el.classList.add("toast-visible");
90 99 });
91 window.setTimeout(dismiss, durationMs);
100 if (durationMs != null)
101 { timer = window.setTimeout(dismiss, durationMs); }
92 102 return { dismiss };
93 103 }
94 104
@@ -32,7 +32,10 @@ export function initToasts ()
32 const text = String(message ?? "").trim();
32 const text = String(message ?? "").trim();
33 if (!text)
33 if (!text)
34 { return { dismiss () {} }; }
34 { return { dismiss () {} }; }
35 const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
35 const sticky = !!options.sticky;
36 const durationMs = sticky
37 ? null
38 : Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS);
36 const onClick = typeof options.onClick === "function" ? options.onClick : null;
39 const onClick = typeof options.onClick === "function" ? options.onClick : null;
37 const actions = Array.isArray(options.actions) ? options.actions : [];
40 const actions = Array.isArray(options.actions) ? options.actions : [];
38 const el = document.createElement("div");
41 const el = document.createElement("div");
@@ -43,11 +46,17 @@ export function initToasts ()
43 textEl.textContent = text;
46 textEl.textContent = text;
44 el.append(textEl);
47 el.append(textEl);
45 let dismissed = false;
48 let dismissed = false;
49 let timer = null;
46 const dismiss = () =>
50 const dismiss = () =>
47 {
51 {
48 if (dismissed)
52 if (dismissed)
49 { return; }
53 { return; }
50 dismissed = true;
54 dismissed = true;
55 if (timer != null)
56 {
57 window.clearTimeout(timer);
58 timer = null;
59 }
51 el.classList.remove("toast-visible");
60 el.classList.remove("toast-visible");
52 el.classList.add("toast-leaving");
61 el.classList.add("toast-leaving");
53 window.setTimeout(() => el.remove(), LEAVE_MS);
62 window.setTimeout(() => el.remove(), LEAVE_MS);
@@ -88,7 +97,8 @@ export function initToasts ()
88 {
97 {
89 el.classList.add("toast-visible");
98 el.classList.add("toast-visible");
90 });
99 });
91 window.setTimeout(dismiss, durationMs);
100 if (durationMs != null)
101 { timer = window.setTimeout(dismiss, durationMs); }
92 return { dismiss };
102 return { dismiss };
93 }
103 }
94
104
client/src/play/trade.js +29 −3
@@ -19,6 +19,7 @@ export function initTrade ({
19 19 getEntity,
20 20 approachAdjacentTile,
21 21 clearPendingActions,
22 setPendingTradeRequest,
22 23 setPendingTradeAccept,
23 24 resolveSpriteUrl,
24 25 resolveImage,
@@ -209,15 +210,35 @@ export function initTrade ({
209 210 applyState(message);
210 211 }
211 212
212 function requestTrade (targetEntityId)
213 function sendTradeRequest (targetEntityId)
213 214 {
214 clearPendingActions?.();
215 215 socket.send({
216 216 type: MessageType.tradeRequest,
217 217 targetEntityId: targetEntityId | 0
218 218 });
219 219 }
220 220
221 function requestTrade (targetEntityId)
222 {
223 clearPendingActions?.();
224 const id = targetEntityId | 0;
225 const local = getLocalEntity?.();
226 const partner = getEntity?.(id);
227 if (!local || !partner)
228 {
229 sendTradeRequest(id);
230 return;
231 }
232 if (isAdjacentTile(local.tileX, local.tileY, partner.tileX, partner.tileY)
233 || (local.tileX === partner.tileX && local.tileY === partner.tileY))
234 {
235 sendTradeRequest(id);
236 return;
237 }
238 setPendingTradeRequest?.(id);
239 approachAdjacentTile?.(partner.tileX, partner.tileY);
240 }
241
221 242 function respondAccept (invite)
222 243 {
223 244 const local = getLocalEntity?.();
@@ -253,7 +274,8 @@ export function initTrade ({
253 274 const handle = toasts?.show?.(
254 275 `${fromName} wishes to trade with you`,
255 276 {
256 durationMs: Math.max(1000, (message.expiresAt | 0) - Date.now()) || 60_000,
277 // Stay until Accept / Reject / server tradeInviteExpired (clock-safe).
278 sticky: true,
257 279 actions: [
258 280 {
259 281 label: "Accept",
@@ -348,6 +370,10 @@ export function initTrade ({
348 370 requestTrade,
349 371 offerBagSlot,
350 372 inventoryLocked: () => (open && youAccepted) || !!(qtyPanel && !qtyPanel.hidden),
373 flushPendingRequest (targetEntityId)
374 {
375 sendTradeRequest(targetEntityId);
376 },
351 377 flushPendingAccept (invite)
352 378 {
353 379 socket.send({
@@ -19,6 +19,7 @@ export function initTrade ({
19 getEntity,
19 getEntity,
20 approachAdjacentTile,
20 approachAdjacentTile,
21 clearPendingActions,
21 clearPendingActions,
22 setPendingTradeRequest,
22 setPendingTradeAccept,
23 setPendingTradeAccept,
23 resolveSpriteUrl,
24 resolveSpriteUrl,
24 resolveImage,
25 resolveImage,
@@ -209,15 +210,35 @@ export function initTrade ({
209 applyState(message);
210 applyState(message);
210 }
211 }
211
212
212 function requestTrade (targetEntityId)
213 function sendTradeRequest (targetEntityId)
213 {
214 {
214 clearPendingActions?.();
215 socket.send({
215 socket.send({
216 type: MessageType.tradeRequest,
216 type: MessageType.tradeRequest,
217 targetEntityId: targetEntityId | 0
217 targetEntityId: targetEntityId | 0
218 });
218 });
219 }
219 }
220
220
221 function requestTrade (targetEntityId)
222 {
223 clearPendingActions?.();
224 const id = targetEntityId | 0;
225 const local = getLocalEntity?.();
226 const partner = getEntity?.(id);
227 if (!local || !partner)
228 {
229 sendTradeRequest(id);
230 return;
231 }
232 if (isAdjacentTile(local.tileX, local.tileY, partner.tileX, partner.tileY)
233 || (local.tileX === partner.tileX && local.tileY === partner.tileY))
234 {
235 sendTradeRequest(id);
236 return;
237 }
238 setPendingTradeRequest?.(id);
239 approachAdjacentTile?.(partner.tileX, partner.tileY);
240 }
241
221 function respondAccept (invite)
242 function respondAccept (invite)
222 {
243 {
223 const local = getLocalEntity?.();
244 const local = getLocalEntity?.();
@@ -253,7 +274,8 @@ export function initTrade ({
253 const handle = toasts?.show?.(
274 const handle = toasts?.show?.(
254 `${fromName} wishes to trade with you`,
275 `${fromName} wishes to trade with you`,
255 {
276 {
256 durationMs: Math.max(1000, (message.expiresAt | 0) - Date.now()) || 60_000,
277 // Stay until Accept / Reject / server tradeInviteExpired (clock-safe).
278 sticky: true,
257 actions: [
279 actions: [
258 {
280 {
259 label: "Accept",
281 label: "Accept",
@@ -348,6 +370,10 @@ export function initTrade ({
348 requestTrade,
370 requestTrade,
349 offerBagSlot,
371 offerBagSlot,
350 inventoryLocked: () => (open && youAccepted) || !!(qtyPanel && !qtyPanel.hidden),
372 inventoryLocked: () => (open && youAccepted) || !!(qtyPanel && !qtyPanel.hidden),
373 flushPendingRequest (targetEntityId)
374 {
375 sendTradeRequest(targetEntityId);
376 },
351 flushPendingAccept (invite)
377 flushPendingAccept (invite)
352 {
378 {
353 socket.send({
379 socket.send({
client/src/render/worldView.js +1 −1
@@ -3366,7 +3366,7 @@ export class WorldView
3366 3366 entity.skullMarker = null;
3367 3367 return;
3368 3368 }
3369 const tint = skullTintColor(entity.skullValue | 0);
3369 const tint = skullTintColor(entity.skullValue | 0, this.settings);
3370 3370 if (!tint)
3371 3371 {
3372 3372 entity.skullMarker?.destroy();
@@ -3366,7 +3366,7 @@ export class WorldView
3366 entity.skullMarker = null;
3366 entity.skullMarker = null;
3367 return;
3367 return;
3368 }
3368 }
3369 const tint = skullTintColor(entity.skullValue | 0);
3369 const tint = skullTintColor(entity.skullValue | 0, this.settings);
3370 if (!tint)
3370 if (!tint)
3371 {
3371 {
3372 entity.skullMarker?.destroy();
3372 entity.skullMarker?.destroy();
server/src/combat/service.js +40 −11
@@ -128,6 +128,43 @@ function dropCombatTarget (world, entity)
128 128 syncCreatureChaseGait(world, entity.handle, 0, false);
129 129 }
130 130
131 /**
132 * Stop everyone who was attacking / auto-casting at `targetEntityId`
133 * (melee engage, spell repeat, pending cast, approach path).
134 */
135 export function clearAttackersTargeting (ecs, targetEntityId)
136 {
137 const targetId = targetEntityId | 0;
138 if (!targetId)
139 { return; }
140 for (const entity of ecs.query(Component.combat))
141 {
142 if (entity.combat.targetEntityId !== targetId)
143 { continue; }
144 entity.combat.targetEntityId = null;
145 entity.combat.seekCover = false;
146 entity.combat.fleeFrom = null;
147 clearCombatApproachPath(ecs, entity.handle);
148 }
149 for (const entity of ecs.query(Component.spellcasting))
150 {
151 const casting = entity.spellcasting;
152 if (!casting)
153 { continue; }
154 const pendingTarget = casting.pendingCast?.targetEntityId;
155 const hitting = casting.repeatTargetEntityId === targetId
156 || (pendingTarget != null && (pendingTarget | 0) === targetId);
157 if (!hitting)
158 { continue; }
159 casting.repeatSpellId = null;
160 casting.repeatTargetEntityId = null;
161 casting.repeatTileX = null;
162 casting.repeatTileY = null;
163 casting.pendingCast = null;
164 clearCombatApproachPath(ecs, entity.handle);
165 }
166 }
167
131 168 export function attackProfileOf (ecs, handle)
132 169 {
133 170 const creature = ecs.get(handle, Component.creature);
@@ -880,7 +917,7 @@ function pickAggressionTargetForTownGuard (world, entity, range)
880 917 }
881 918 for (const player of world.ecs.query(Component.character, Component.position))
882 919 {
883 if (!skullAttractsTownGuards(player.character?.skullValue))
920 if (!skullAttractsTownGuards(player.character?.skullValue, world.settings))
884 921 { continue; }
885 922 const dist = considerAggressionCandidate(world, entity, player.handle, player.entityId, player.position, range);
886 923 if (dist != null && dist < bestDist)
@@ -2250,11 +2287,7 @@ function applyLocalMeleeHit (
2250 2287 {
2251 2288 clearEngage(world.ecs, attackerId);
2252 2289 clearEngage(world.ecs, targetId);
2253 for (const entity of world.ecs.query(Component.combat))
2254 {
2255 if (entity.combat.targetEntityId === targetId)
2256 { entity.combat.targetEntityId = null; }
2257 }
2290 clearAttackersTargeting(world.ecs, targetId);
2258 2291 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2259 2292 {
2260 2293 const path = world.ecs.get(targetHandle, Component.path);
@@ -2427,11 +2460,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
2427 2460 {
2428 2461 clearEngage(world.ecs, attackerId);
2429 2462 clearEngage(world.ecs, targetId);
2430 for (const entity of world.ecs.query(Component.combat))
2431 {
2432 if (entity.combat.targetEntityId === targetId)
2433 { entity.combat.targetEntityId = null; }
2434 }
2463 clearAttackersTargeting(world.ecs, targetId);
2435 2464 const targetCharacter = world.ecs.get(targetHandle, Component.character);
2436 2465 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2437 2466 {
@@ -128,6 +128,43 @@ function dropCombatTarget (world, entity)
128 syncCreatureChaseGait(world, entity.handle, 0, false);
128 syncCreatureChaseGait(world, entity.handle, 0, false);
129}
129}
130
130
131/**
132 * Stop everyone who was attacking / auto-casting at `targetEntityId`
133 * (melee engage, spell repeat, pending cast, approach path).
134 */
135export function clearAttackersTargeting (ecs, targetEntityId)
136{
137 const targetId = targetEntityId | 0;
138 if (!targetId)
139 { return; }
140 for (const entity of ecs.query(Component.combat))
141 {
142 if (entity.combat.targetEntityId !== targetId)
143 { continue; }
144 entity.combat.targetEntityId = null;
145 entity.combat.seekCover = false;
146 entity.combat.fleeFrom = null;
147 clearCombatApproachPath(ecs, entity.handle);
148 }
149 for (const entity of ecs.query(Component.spellcasting))
150 {
151 const casting = entity.spellcasting;
152 if (!casting)
153 { continue; }
154 const pendingTarget = casting.pendingCast?.targetEntityId;
155 const hitting = casting.repeatTargetEntityId === targetId
156 || (pendingTarget != null && (pendingTarget | 0) === targetId);
157 if (!hitting)
158 { continue; }
159 casting.repeatSpellId = null;
160 casting.repeatTargetEntityId = null;
161 casting.repeatTileX = null;
162 casting.repeatTileY = null;
163 casting.pendingCast = null;
164 clearCombatApproachPath(ecs, entity.handle);
165 }
166}
167
131export function attackProfileOf (ecs, handle)
168export function attackProfileOf (ecs, handle)
132{
169{
133 const creature = ecs.get(handle, Component.creature);
170 const creature = ecs.get(handle, Component.creature);
@@ -880,7 +917,7 @@ function pickAggressionTargetForTownGuard (world, entity, range)
880 }
917 }
881 for (const player of world.ecs.query(Component.character, Component.position))
918 for (const player of world.ecs.query(Component.character, Component.position))
882 {
919 {
883 if (!skullAttractsTownGuards(player.character?.skullValue))
920 if (!skullAttractsTownGuards(player.character?.skullValue, world.settings))
884 { continue; }
921 { continue; }
885 const dist = considerAggressionCandidate(world, entity, player.handle, player.entityId, player.position, range);
922 const dist = considerAggressionCandidate(world, entity, player.handle, player.entityId, player.position, range);
886 if (dist != null && dist < bestDist)
923 if (dist != null && dist < bestDist)
@@ -2250,11 +2287,7 @@ function applyLocalMeleeHit (
2250 {
2287 {
2251 clearEngage(world.ecs, attackerId);
2288 clearEngage(world.ecs, attackerId);
2252 clearEngage(world.ecs, targetId);
2289 clearEngage(world.ecs, targetId);
2253 for (const entity of world.ecs.query(Component.combat))
2290 clearAttackersTargeting(world.ecs, targetId);
2254 {
2255 if (entity.combat.targetEntityId === targetId)
2256 { entity.combat.targetEntityId = null; }
2257 }
2258 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2291 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2259 {
2292 {
2260 const path = world.ecs.get(targetHandle, Component.path);
2293 const path = world.ecs.get(targetHandle, Component.path);
@@ -2427,11 +2460,7 @@ export function applyDamageAndKill (world, attackerId, targetId, damage, now = D
2427 {
2460 {
2428 clearEngage(world.ecs, attackerId);
2461 clearEngage(world.ecs, attackerId);
2429 clearEngage(world.ecs, targetId);
2462 clearEngage(world.ecs, targetId);
2430 for (const entity of world.ecs.query(Component.combat))
2463 clearAttackersTargeting(world.ecs, targetId);
2431 {
2432 if (entity.combat.targetEntityId === targetId)
2433 { entity.combat.targetEntityId = null; }
2434 }
2435 const targetCharacter = world.ecs.get(targetHandle, Component.character);
2464 const targetCharacter = world.ecs.get(targetHandle, Component.character);
2436 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2465 if (targetCharacter && !world.ecs.has(targetHandle, Component.dead))
2437 {
2466 {
server/src/simulation/workerWorld.js +2 −5
@@ -61,6 +61,7 @@ import {
61 61 applyRemoteMeleeSwing,
62 62 applyRemoteRangedShot,
63 63 attackProfileOf,
64 clearAttackersTargeting,
64 65 clearEngage,
65 66 engageTarget,
66 67 ensureMobCombat,
@@ -230,11 +231,7 @@ export class WorkerWorld
230 231 cancelGather(this.ecs, handle);
231 232 clearFollow(this.ecs, message.entityId);
232 233 }
233 for (const entity of this.ecs.query(Component.combat))
234 {
235 if (entity.combat.targetEntityId === message.targetEntityId)
236 { entity.combat.targetEntityId = null; }
237 }
234 clearAttackersTargeting(this.ecs, message.targetEntityId ?? message.entityId);
238 235 return { ok: true };
239 236 }
240 237 case WorkerMessage.setItemLastSales:
@@ -61,6 +61,7 @@ import {
61 applyRemoteMeleeSwing,
61 applyRemoteMeleeSwing,
62 applyRemoteRangedShot,
62 applyRemoteRangedShot,
63 attackProfileOf,
63 attackProfileOf,
64 clearAttackersTargeting,
64 clearEngage,
65 clearEngage,
65 engageTarget,
66 engageTarget,
66 ensureMobCombat,
67 ensureMobCombat,
@@ -230,11 +231,7 @@ export class WorkerWorld
230 cancelGather(this.ecs, handle);
231 cancelGather(this.ecs, handle);
231 clearFollow(this.ecs, message.entityId);
232 clearFollow(this.ecs, message.entityId);
232 }
233 }
233 for (const entity of this.ecs.query(Component.combat))
234 clearAttackersTargeting(this.ecs, message.targetEntityId ?? message.entityId);
234 {
235 if (entity.combat.targetEntityId === message.targetEntityId)
236 { entity.combat.targetEntityId = null; }
237 }
238 return { ok: true };
235 return { ok: true };
239 }
236 }
240 case WorkerMessage.setItemLastSales:
237 case WorkerMessage.setItemLastSales:
server/tests/combat.test.js +31 −2
@@ -627,9 +627,9 @@ test("town guards aggro players whose skull tint lerps toward red", () =>
627 627 try
628 628 {
629 629 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
630 harness.stateDb.addKillCounter(character.character_id, SKULL_KILL_BONUS, harness.now());
630 harness.stateDb.addKillCounter(character.character_id, SKULL_KILL_BONUS + 1, harness.now());
631 631 const playerId = harness.simulation.spawnCharacter(1, character);
632 assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS);
632 assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS + 1);
633 633
634 634 const guardId = harness.simulation.spawnEntity(mobComponents({
635 635 tileX: 10,
@@ -652,6 +652,35 @@ test("town guards aggro players whose skull tint lerps toward red", () =>
652 652 { harness.close(); }
653 653 });
654 654
655 test("town guards ignore players at black-max skull (not yet red)", () =>
656 {
657 const harness = makeSimulation({ workerCount: 1 });
658 try
659 {
660 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
661 harness.stateDb.setKillCounter(character.character_id, SKULL_KILL_BONUS, harness.now());
662 const playerId = harness.simulation.spawnCharacter(1, character);
663 assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS);
664
665 const guardId = harness.simulation.spawnEntity(mobComponents({
666 tileX: 10,
667 tileY: 8,
668 mobTypeId: MobId.townGuard,
669 spawnerId: "0:0:guard",
670 originTileX: 10,
671 originTileY: 8,
672 wanderRangeTiles: 8,
673 level: 10
674 }));
675
676 harness.simulation.step();
677 assert.equal(componentsOf(harness.simulation, guardId).combat?.targetEntityId, null);
678 assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, null);
679 }
680 finally
681 { harness.close(); }
682 });
683
655 684 test("town guards ignore players with white-only skull", () =>
656 685 {
657 686 const harness = makeSimulation({ workerCount: 1 });
@@ -627,9 +627,9 @@ test("town guards aggro players whose skull tint lerps toward red", () =>
627 try
627 try
628 {
628 {
629 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
629 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
630 harness.stateDb.addKillCounter(character.character_id, SKULL_KILL_BONUS, harness.now());
630 harness.stateDb.addKillCounter(character.character_id, SKULL_KILL_BONUS + 1, harness.now());
631 const playerId = harness.simulation.spawnCharacter(1, character);
631 const playerId = harness.simulation.spawnCharacter(1, character);
632 assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS);
632 assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS + 1);
633
633
634 const guardId = harness.simulation.spawnEntity(mobComponents({
634 const guardId = harness.simulation.spawnEntity(mobComponents({
635 tileX: 10,
635 tileX: 10,
@@ -652,6 +652,35 @@ test("town guards aggro players whose skull tint lerps toward red", () =>
652 { harness.close(); }
652 { harness.close(); }
653});
653});
654
654
655test("town guards ignore players at black-max skull (not yet red)", () =>
656{
657 const harness = makeSimulation({ workerCount: 1 });
658 try
659 {
660 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
661 harness.stateDb.setKillCounter(character.character_id, SKULL_KILL_BONUS, harness.now());
662 const playerId = harness.simulation.spawnCharacter(1, character);
663 assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS);
664
665 const guardId = harness.simulation.spawnEntity(mobComponents({
666 tileX: 10,
667 tileY: 8,
668 mobTypeId: MobId.townGuard,
669 spawnerId: "0:0:guard",
670 originTileX: 10,
671 originTileY: 8,
672 wanderRangeTiles: 8,
673 level: 10
674 }));
675
676 harness.simulation.step();
677 assert.equal(componentsOf(harness.simulation, guardId).combat?.targetEntityId, null);
678 assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, null);
679 }
680 finally
681 { harness.close(); }
682});
683
655test("town guards ignore players with white-only skull", () =>
684test("town guards ignore players with white-only skull", () =>
656{
685{
657 const harness = makeSimulation({ workerCount: 1 });
686 const harness = makeSimulation({ workerCount: 1 });
server/tests/death.test.js +74 −0
@@ -276,6 +276,80 @@ test("dead players stop combat and active auto-repeat and cannot be targeted", (
276 276 });
277 277 });
278 278
279 test("attackers drop melee and spell auto-targets when the victim dies", () =>
280 {
281 withDeterministicCombat(() =>
282 {
283 const harness = makeSimulation({ workerCount: 1 });
284 try
285 {
286 const victim = makeCharacter(harness.stateDb, {
287 tileX: 8,
288 tileY: 8,
289 firstName: "Vic",
290 lastName: "Victim"
291 });
292 const caster = makeCharacter(harness.stateDb, {
293 tileX: 10,
294 tileY: 8,
295 firstName: "Cas",
296 lastName: "Caster"
297 });
298 const victimId = harness.simulation.spawnCharacter(1, victim);
299 const casterId = harness.simulation.spawnCharacter(2, caster);
300 const deerId = harness.simulation.spawnEntity(mobComponents({
301 tileX: 9,
302 tileY: 8,
303 mobTypeId: MobId.deer,
304 spawnerId: null
305 }));
306 const worker = harness.simulation.workers[
307 harness.simulation.entityRecord(victimId).workerId
308 ];
309 const ecs = worker.world.ecs;
310 const deerHandle = ecs.handleOf(deerId);
311 const casterHandle = ecs.handleOf(casterId);
312 const victimHandle = ecs.handleOf(victimId);
313 ecs.get(deerHandle, Component.health).current = 1_000_000;
314 ecs.get(deerHandle, Component.health).max = 1_000_000;
315 ecs.get(victimHandle, Component.health).current = 1;
316 ecs.add(deerHandle, Component.combat, {
317 targetEntityId: victimId,
318 nextSwingAt: 0,
319 seekCover: false,
320 fleeFrom: null
321 });
322 ecs.add(casterHandle, Component.spellcasting, {
323 armedSpellId: null,
324 repeatSpellId: "firebolt",
325 repeatTargetEntityId: victimId,
326 repeatTileX: null,
327 repeatTileY: null,
328 pendingCast: { spellId: "firebolt", tileX: 8, tileY: 8, targetEntityId: victimId },
329 nextCastAt: 0
330 });
331 const casterPath = ecs.get(casterHandle, Component.path)
332 ?? ecs.add(casterHandle, Component.path, { tiles: [] });
333 casterPath.tiles = [{ x: 8, y: 8 }];
334
335 applyDamageAndKill(worker.world, deerId, victimId, 999, Date.now());
336
337 assert.ok(ecs.has(victimHandle, Component.dead));
338 assert.equal(
339 ecs.get(deerHandle, Component.combat)?.targetEntityId ?? null,
340 null,
341 "melee auto-attack must stop on death"
342 );
343 const casting = ecs.get(casterHandle, Component.spellcasting);
344 assert.equal(casting?.repeatTargetEntityId ?? null, null);
345 assert.equal(casting?.pendingCast ?? null, null);
346 assert.deepEqual(ecs.get(casterHandle, Component.path)?.tiles ?? [], []);
347 }
348 finally
349 { harness.close(); }
350 });
351 });
352
279 353 test("player death emits entity-despawn for other viewers", () =>
280 354 {
281 355 withDeterministicCombat(() =>
@@ -276,6 +276,80 @@ test("dead players stop combat and active auto-repeat and cannot be targeted", (
276 });
276 });
277});
277});
278
278
279test("attackers drop melee and spell auto-targets when the victim dies", () =>
280{
281 withDeterministicCombat(() =>
282 {
283 const harness = makeSimulation({ workerCount: 1 });
284 try
285 {
286 const victim = makeCharacter(harness.stateDb, {
287 tileX: 8,
288 tileY: 8,
289 firstName: "Vic",
290 lastName: "Victim"
291 });
292 const caster = makeCharacter(harness.stateDb, {
293 tileX: 10,
294 tileY: 8,
295 firstName: "Cas",
296 lastName: "Caster"
297 });
298 const victimId = harness.simulation.spawnCharacter(1, victim);
299 const casterId = harness.simulation.spawnCharacter(2, caster);
300 const deerId = harness.simulation.spawnEntity(mobComponents({
301 tileX: 9,
302 tileY: 8,
303 mobTypeId: MobId.deer,
304 spawnerId: null
305 }));
306 const worker = harness.simulation.workers[
307 harness.simulation.entityRecord(victimId).workerId
308 ];
309 const ecs = worker.world.ecs;
310 const deerHandle = ecs.handleOf(deerId);
311 const casterHandle = ecs.handleOf(casterId);
312 const victimHandle = ecs.handleOf(victimId);
313 ecs.get(deerHandle, Component.health).current = 1_000_000;
314 ecs.get(deerHandle, Component.health).max = 1_000_000;
315 ecs.get(victimHandle, Component.health).current = 1;
316 ecs.add(deerHandle, Component.combat, {
317 targetEntityId: victimId,
318 nextSwingAt: 0,
319 seekCover: false,
320 fleeFrom: null
321 });
322 ecs.add(casterHandle, Component.spellcasting, {
323 armedSpellId: null,
324 repeatSpellId: "firebolt",
325 repeatTargetEntityId: victimId,
326 repeatTileX: null,
327 repeatTileY: null,
328 pendingCast: { spellId: "firebolt", tileX: 8, tileY: 8, targetEntityId: victimId },
329 nextCastAt: 0
330 });
331 const casterPath = ecs.get(casterHandle, Component.path)
332 ?? ecs.add(casterHandle, Component.path, { tiles: [] });
333 casterPath.tiles = [{ x: 8, y: 8 }];
334
335 applyDamageAndKill(worker.world, deerId, victimId, 999, Date.now());
336
337 assert.ok(ecs.has(victimHandle, Component.dead));
338 assert.equal(
339 ecs.get(deerHandle, Component.combat)?.targetEntityId ?? null,
340 null,
341 "melee auto-attack must stop on death"
342 );
343 const casting = ecs.get(casterHandle, Component.spellcasting);
344 assert.equal(casting?.repeatTargetEntityId ?? null, null);
345 assert.equal(casting?.pendingCast ?? null, null);
346 assert.deepEqual(ecs.get(casterHandle, Component.path)?.tiles ?? [], []);
347 }
348 finally
349 { harness.close(); }
350 });
351});
352
279test("player death emits entity-despawn for other viewers", () =>
353test("player death emits entity-despawn for other viewers", () =>
280{
354{
281 withDeterministicCombat(() =>
355 withDeterministicCombat(() =>
server/tests/skull.test.js +26 −7
@@ -5,11 +5,13 @@ import
5 5 skullValue,
6 6 skullTintColor,
7 7 skullAttractsTownGuards,
8 resolveSkullThresholds,
8 9 decayKillCounter,
9 10 SKULL_PURE_RED_VALUE,
10 11 SKULL_DARK_RED_VALUE,
11 12 SKULL_KILL_BONUS,
12 SKULL_RED_LERP_START
13 SKULL_BLACK_MAX_DEFAULT,
14 SKULL_RED_MAX_DEFAULT
13 15 } from "../../shared/combat/skull.js";
14 16 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
15 17 import { Component } from "../src/ecs/components.js";
@@ -58,10 +60,12 @@ test("kill counter decays one point per second", () =>
58 60 assert.deepEqual(decayKillCounter(2, 10_000), { killCounter: 0, consumedMs: 2000 });
59 61 });
60 62
61 test("skull tint: invisible, white, red, dark red", () =>
63 test("skull tint: invisible, white→black, black→red, dark red", () =>
62 64 {
63 65 assert.equal(skullTintColor(0), null);
64 assert.deepEqual(skullTintColor(1), { r: 1, g: 1, b: 1 });
66 const nearWhite = skullTintColor(1);
67 assert.ok(nearWhite.r > 0.99 && nearWhite.g > 0.99 && nearWhite.b > 0.99);
68 assert.deepEqual(skullTintColor(SKULL_BLACK_MAX_DEFAULT), { r: 0, g: 0, b: 0 });
65 69 assert.deepEqual(skullTintColor(SKULL_PURE_RED_VALUE), { r: 1, g: 0, b: 0 });
66 70 const dark = skullTintColor(SKULL_DARK_RED_VALUE);
67 71 assert.ok(Math.abs(dark.r - 128 / 255) < 1e-9);
@@ -69,12 +73,27 @@ test("skull tint: invisible, white, red, dark red", () =>
69 73 assert.equal(dark.b, 0);
70 74 });
71 75
72 test("skull attracts town guards once tint starts lerping red", () =>
76 test("skull thresholds honour world settings", () =>
77 {
78 const custom = { skullBlackMax: 100, skullRedMax: 1000 };
79 assert.deepEqual(resolveSkullThresholds(custom), {
80 blackMax: 100,
81 redMax: 1000,
82 darkRedMax: 2000
83 });
84 assert.deepEqual(skullTintColor(100, custom), { r: 0, g: 0, b: 0 });
85 assert.deepEqual(skullTintColor(1000, custom), { r: 1, g: 0, b: 0 });
86 assert.equal(skullAttractsTownGuards(100, custom), false);
87 assert.equal(skullAttractsTownGuards(101, custom), true);
88 assert.equal(SKULL_RED_MAX_DEFAULT, SKULL_PURE_RED_VALUE);
89 });
90
91 test("skull attracts town guards once past black max (red lerp)", () =>
73 92 {
74 93 assert.equal(skullAttractsTownGuards(0), false);
75 assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START), false);
76 assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START + 1), true);
77 assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), true);
94 assert.equal(skullAttractsTownGuards(SKULL_BLACK_MAX_DEFAULT), false);
95 assert.equal(skullAttractsTownGuards(SKULL_BLACK_MAX_DEFAULT + 1), true);
96 assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), false);
78 97 });
79 98
80 99 test("PvP kill awards temporary skull points that replicate", () =>
@@ -5,11 +5,13 @@ import
5 skullValue,
5 skullValue,
6 skullTintColor,
6 skullTintColor,
7 skullAttractsTownGuards,
7 skullAttractsTownGuards,
8 resolveSkullThresholds,
8 decayKillCounter,
9 decayKillCounter,
9 SKULL_PURE_RED_VALUE,
10 SKULL_PURE_RED_VALUE,
10 SKULL_DARK_RED_VALUE,
11 SKULL_DARK_RED_VALUE,
11 SKULL_KILL_BONUS,
12 SKULL_KILL_BONUS,
12 SKULL_RED_LERP_START
13 SKULL_BLACK_MAX_DEFAULT,
14 SKULL_RED_MAX_DEFAULT
13 } from "../../shared/combat/skull.js";
15 } from "../../shared/combat/skull.js";
14import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
16import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
15import { Component } from "../src/ecs/components.js";
17import { Component } from "../src/ecs/components.js";
@@ -58,10 +60,12 @@ test("kill counter decays one point per second", () =>
58 assert.deepEqual(decayKillCounter(2, 10_000), { killCounter: 0, consumedMs: 2000 });
60 assert.deepEqual(decayKillCounter(2, 10_000), { killCounter: 0, consumedMs: 2000 });
59});
61});
60
62
61test("skull tint: invisible, white, red, dark red", () =>
63test("skull tint: invisible, white→black, black→red, dark red", () =>
62{
64{
63 assert.equal(skullTintColor(0), null);
65 assert.equal(skullTintColor(0), null);
64 assert.deepEqual(skullTintColor(1), { r: 1, g: 1, b: 1 });
66 const nearWhite = skullTintColor(1);
67 assert.ok(nearWhite.r > 0.99 && nearWhite.g > 0.99 && nearWhite.b > 0.99);
68 assert.deepEqual(skullTintColor(SKULL_BLACK_MAX_DEFAULT), { r: 0, g: 0, b: 0 });
65 assert.deepEqual(skullTintColor(SKULL_PURE_RED_VALUE), { r: 1, g: 0, b: 0 });
69 assert.deepEqual(skullTintColor(SKULL_PURE_RED_VALUE), { r: 1, g: 0, b: 0 });
66 const dark = skullTintColor(SKULL_DARK_RED_VALUE);
70 const dark = skullTintColor(SKULL_DARK_RED_VALUE);
67 assert.ok(Math.abs(dark.r - 128 / 255) < 1e-9);
71 assert.ok(Math.abs(dark.r - 128 / 255) < 1e-9);
@@ -69,12 +73,27 @@ test("skull tint: invisible, white, red, dark red", () =>
69 assert.equal(dark.b, 0);
73 assert.equal(dark.b, 0);
70});
74});
71
75
72test("skull attracts town guards once tint starts lerping red", () =>
76test("skull thresholds honour world settings", () =>
77{
78 const custom = { skullBlackMax: 100, skullRedMax: 1000 };
79 assert.deepEqual(resolveSkullThresholds(custom), {
80 blackMax: 100,
81 redMax: 1000,
82 darkRedMax: 2000
83 });
84 assert.deepEqual(skullTintColor(100, custom), { r: 0, g: 0, b: 0 });
85 assert.deepEqual(skullTintColor(1000, custom), { r: 1, g: 0, b: 0 });
86 assert.equal(skullAttractsTownGuards(100, custom), false);
87 assert.equal(skullAttractsTownGuards(101, custom), true);
88 assert.equal(SKULL_RED_MAX_DEFAULT, SKULL_PURE_RED_VALUE);
89});
90
91test("skull attracts town guards once past black max (red lerp)", () =>
73{
92{
74 assert.equal(skullAttractsTownGuards(0), false);
93 assert.equal(skullAttractsTownGuards(0), false);
75 assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START), false);
94 assert.equal(skullAttractsTownGuards(SKULL_BLACK_MAX_DEFAULT), false);
76 assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START + 1), true);
95 assert.equal(skullAttractsTownGuards(SKULL_BLACK_MAX_DEFAULT + 1), true);
77 assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), true);
96 assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), false);
78});
97});
79
98
80test("PvP kill awards temporary skull points that replicate", () =>
99test("PvP kill awards temporary skull points that replicate", () =>
server/tests/spawner-bounty.test.js +17 −1
@@ -15,7 +15,7 @@ import { mobComponents } from "../src/simulation/spawners.js";
15 15 import { Component } from "../src/ecs/components.js";
16 16 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
17 17 import { ItemId } from "../../shared/definitions/items.js";
18 import { attackProfileOf } from "../src/combat/service.js";
18 import { attackProfileOf, applyDamageAndKill } from "../src/combat/service.js";
19 19 import { installDamagingCombatRng } from "./helpers/combatRng.js";
20 20
21 21 test("spawner config defaults bounty 0 and level 1", () =>
@@ -264,6 +264,22 @@ test("killing a bounty mob awards player bounty and can wake town guards", () =>
264 264 const guardHandle = worker.world.ecs.handleOf(guardId);
265 265 const guardCombat = worker.world.ecs.get(guardHandle, Component.combat);
266 266 assert.equal(guardCombat?.targetEntityId, playerId);
267
268 const playerHandle = worker.world.ecs.handleOf(playerId);
269 worker.world.ecs.get(playerHandle, Component.health).current = 1;
270 applyDamageAndKill(worker.world, guardId, playerId, 999, Date.now());
271 assert.ok(worker.world.ecs.has(playerHandle, Component.dead));
272 assert.equal(
273 worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null,
274 null,
275 "guard must drop the bounty-killer when they die"
276 );
277 harness.simulation.step();
278 assert.equal(
279 worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null,
280 null,
281 "guard must not re-aggro a dead player"
282 );
267 283 }
268 284 finally
269 285 {
@@ -15,7 +15,7 @@ import { mobComponents } from "../src/simulation/spawners.js";
15import { Component } from "../src/ecs/components.js";
15import { Component } from "../src/ecs/components.js";
16import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
16import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
17import { ItemId } from "../../shared/definitions/items.js";
17import { ItemId } from "../../shared/definitions/items.js";
18import { attackProfileOf } from "../src/combat/service.js";
18import { attackProfileOf, applyDamageAndKill } from "../src/combat/service.js";
19import { installDamagingCombatRng } from "./helpers/combatRng.js";
19import { installDamagingCombatRng } from "./helpers/combatRng.js";
20
20
21test("spawner config defaults bounty 0 and level 1", () =>
21test("spawner config defaults bounty 0 and level 1", () =>
@@ -264,6 +264,22 @@ test("killing a bounty mob awards player bounty and can wake town guards", () =>
264 const guardHandle = worker.world.ecs.handleOf(guardId);
264 const guardHandle = worker.world.ecs.handleOf(guardId);
265 const guardCombat = worker.world.ecs.get(guardHandle, Component.combat);
265 const guardCombat = worker.world.ecs.get(guardHandle, Component.combat);
266 assert.equal(guardCombat?.targetEntityId, playerId);
266 assert.equal(guardCombat?.targetEntityId, playerId);
267
268 const playerHandle = worker.world.ecs.handleOf(playerId);
269 worker.world.ecs.get(playerHandle, Component.health).current = 1;
270 applyDamageAndKill(worker.world, guardId, playerId, 999, Date.now());
271 assert.ok(worker.world.ecs.has(playerHandle, Component.dead));
272 assert.equal(
273 worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null,
274 null,
275 "guard must drop the bounty-killer when they die"
276 );
277 harness.simulation.step();
278 assert.equal(
279 worker.world.ecs.get(guardHandle, Component.combat)?.targetEntityId ?? null,
280 null,
281 "guard must not re-aggro a dead player"
282 );
267 }
283 }
268 finally
284 finally
269 {
285 {
shared/combat/skull.js +66 −21
@@ -2,18 +2,26 @@
2 2
3 3 export const SKULL_KILL_BONUS = 500;
4 4 export const SKULL_DECAY_PER_SECOND = 1;
5
5 6 /**
6 * Tint stays white at this value; above it, color lerps toward red.
7 * Town guards treat players above this as hostiles (same as aggressive mobs).
7 * Default skull value at pure black. White→black below this; red lerp (and
8 * town-guard hostility) start strictly above it.
9 * Overridable via world setting `skullBlackMax`.
8 10 */
9 export const SKULL_RED_LERP_START = 1;
10 /** Skull value at which tint is pure red. */
11 export const SKULL_PURE_RED_VALUE = 500_000;
11 export const SKULL_BLACK_MAX_DEFAULT = 500;
12 12 /**
13 * Past pure red, tint reaches dark red (128,0,0) at this value and clamps.
14 * Same span as white→red (500k).
13 * Default skull value at pure red. Black→red between black max and this;
14 * beyond it, tint leans toward dark red.
15 * Overridable via world setting `skullRedMax`.
15 16 */
16 export const SKULL_DARK_RED_VALUE = 1_000_000;
17 export const SKULL_RED_MAX_DEFAULT = 500_000;
18
19 /** @deprecated Use {@link SKULL_BLACK_MAX_DEFAULT}. */
20 export const SKULL_RED_LERP_START = SKULL_BLACK_MAX_DEFAULT;
21 /** @deprecated Use {@link SKULL_RED_MAX_DEFAULT}. */
22 export const SKULL_PURE_RED_VALUE = SKULL_RED_MAX_DEFAULT;
23 /** @deprecated Dark-red clamp is derived as 2× red max. */
24 export const SKULL_DARK_RED_VALUE = SKULL_RED_MAX_DEFAULT * 2;
17 25
18 26 /**
19 27 * @param {number} bountyCopper
@@ -24,10 +32,39 @@ export function skullValue (bountyCopper, killCounter)
24 32 return Math.max(0, Math.floor(Number(bountyCopper) || 0) + Math.floor(Number(killCounter) || 0));
25 33 }
26 34
27 /** True once the skull tint starts lerping from white toward red. */
28 export function skullAttractsTownGuards (value)
35 /**
36 * Resolve tint / guard thresholds from world settings (falls back to defaults).
37 * @param {{ skullBlackMax?: number, skullRedMax?: number } | null | undefined} settings
38 */
39 export function resolveSkullThresholds (settings = null)
29 40 {
30 return Math.max(0, Math.floor(Number(value) || 0)) > SKULL_RED_LERP_START;
41 const blackMax = Math.max(
42 1,
43 Math.floor(Number(settings?.skullBlackMax) || SKULL_BLACK_MAX_DEFAULT)
44 );
45 let redMax = Math.max(
46 1,
47 Math.floor(Number(settings?.skullRedMax) || SKULL_RED_MAX_DEFAULT)
48 );
49 if (redMax <= blackMax)
50 { redMax = blackMax + 1; }
51 return {
52 blackMax,
53 redMax,
54 darkRedMax: redMax * 2
55 };
56 }
57
58 /**
59 * True once the skull tint starts lerping from black toward red
60 * (skull value strictly above black max).
61 * @param {number} value
62 * @param {{ skullBlackMax?: number, skullRedMax?: number } | null | undefined} [settings]
63 */
64 export function skullAttractsTownGuards (value, settings = null)
65 {
66 const { blackMax } = resolveSkullThresholds(settings);
67 return Math.max(0, Math.floor(Number(value) || 0)) > blackMax;
31 68 }
32 69
33 70 /**
@@ -53,26 +90,34 @@ function lerp (a, b, t)
53 90
54 91 /**
55 92 * Magenta-tint target color for a skull value (0 = invisible / unused).
56 * 1 → white; … → pure red at 500_000; then → dark red (128,0,0) by 1_000_000.
93 * 1…blackMax → white→black; …→ pure red at redMax; then → dark red (128,0,0).
94 * @param {number} value
95 * @param {{ skullBlackMax?: number, skullRedMax?: number } | null | undefined} [settings]
57 96 * @returns {{ r: number, g: number, b: number } | null} 0–1 channels, or null if invisible
58 97 */
59 export function skullTintColor (value)
98 export function skullTintColor (value, settings = null)
60 99 {
61 100 const v = Math.max(0, Math.floor(Number(value) || 0));
62 101 if (v <= 0)
63 102 { return null; }
64 if (v <= SKULL_RED_LERP_START)
65 { return Object.freeze({ r: 1, g: 1, b: 1 }); }
66 if (v <= SKULL_PURE_RED_VALUE)
103 const { blackMax, redMax, darkRedMax } = resolveSkullThresholds(settings);
104 if (v <= blackMax)
105 {
106 const t = v / blackMax;
107 const c = lerp(1, 0, t);
108 return Object.freeze({ r: c, g: c, b: c });
109 }
110 if (v <= redMax)
67 111 {
68 const t = (v - SKULL_RED_LERP_START) / (SKULL_PURE_RED_VALUE - SKULL_RED_LERP_START);
112 // Black (0,0,0) → pure red (1,0,0).
113 const t = (v - blackMax) / (redMax - blackMax);
69 114 return Object.freeze({
70 r: 1,
71 g: lerp(1, 0, t),
72 b: lerp(1, 0, t)
115 r: lerp(0, 1, t),
116 g: 0,
117 b: 0
73 118 });
74 119 }
75 const t = Math.min(1, (v - SKULL_PURE_RED_VALUE) / (SKULL_DARK_RED_VALUE - SKULL_PURE_RED_VALUE));
120 const t = Math.min(1, (v - redMax) / (darkRedMax - redMax));
76 121 const dark = 128 / 255;
77 122 return Object.freeze({
78 123 r: lerp(1, dark, t),
@@ -2,18 +2,26 @@
2
2
3export const SKULL_KILL_BONUS = 500;
3export const SKULL_KILL_BONUS = 500;
4export const SKULL_DECAY_PER_SECOND = 1;
4export const SKULL_DECAY_PER_SECOND = 1;
5
5/**
6/**
6 * Tint stays white at this value; above it, color lerps toward red.
7 * Default skull value at pure black. White→black below this; red lerp (and
7 * Town guards treat players above this as hostiles (same as aggressive mobs).
8 * town-guard hostility) start strictly above it.
9 * Overridable via world setting `skullBlackMax`.
8 */
10 */
9export const SKULL_RED_LERP_START = 1;
11export const SKULL_BLACK_MAX_DEFAULT = 500;
10/** Skull value at which tint is pure red. */
11export const SKULL_PURE_RED_VALUE = 500_000;
12/**
12/**
13 * Past pure red, tint reaches dark red (128,0,0) at this value and clamps.
13 * Default skull value at pure red. Black→red between black max and this;
14 * Same span as white→red (500k).
14 * beyond it, tint leans toward dark red.
15 * Overridable via world setting `skullRedMax`.
15 */
16 */
16export const SKULL_DARK_RED_VALUE = 1_000_000;
17export const SKULL_RED_MAX_DEFAULT = 500_000;
18
19/** @deprecated Use {@link SKULL_BLACK_MAX_DEFAULT}. */
20export const SKULL_RED_LERP_START = SKULL_BLACK_MAX_DEFAULT;
21/** @deprecated Use {@link SKULL_RED_MAX_DEFAULT}. */
22export const SKULL_PURE_RED_VALUE = SKULL_RED_MAX_DEFAULT;
23/** @deprecated Dark-red clamp is derived as 2× red max. */
24export const SKULL_DARK_RED_VALUE = SKULL_RED_MAX_DEFAULT * 2;
17
25
18/**
26/**
19 * @param {number} bountyCopper
27 * @param {number} bountyCopper
@@ -24,10 +32,39 @@ export function skullValue (bountyCopper, killCounter)
24 return Math.max(0, Math.floor(Number(bountyCopper) || 0) + Math.floor(Number(killCounter) || 0));
32 return Math.max(0, Math.floor(Number(bountyCopper) || 0) + Math.floor(Number(killCounter) || 0));
25}
33}
26
34
27/** True once the skull tint starts lerping from white toward red. */
35/**
28export function skullAttractsTownGuards (value)
36 * Resolve tint / guard thresholds from world settings (falls back to defaults).
37 * @param {{ skullBlackMax?: number, skullRedMax?: number } | null | undefined} settings
38 */
39export function resolveSkullThresholds (settings = null)
29{
40{
30 return Math.max(0, Math.floor(Number(value) || 0)) > SKULL_RED_LERP_START;
41 const blackMax = Math.max(
42 1,
43 Math.floor(Number(settings?.skullBlackMax) || SKULL_BLACK_MAX_DEFAULT)
44 );
45 let redMax = Math.max(
46 1,
47 Math.floor(Number(settings?.skullRedMax) || SKULL_RED_MAX_DEFAULT)
48 );
49 if (redMax <= blackMax)
50 { redMax = blackMax + 1; }
51 return {
52 blackMax,
53 redMax,
54 darkRedMax: redMax * 2
55 };
56}
57
58/**
59 * True once the skull tint starts lerping from black toward red
60 * (skull value strictly above black max).
61 * @param {number} value
62 * @param {{ skullBlackMax?: number, skullRedMax?: number } | null | undefined} [settings]
63 */
64export function skullAttractsTownGuards (value, settings = null)
65{
66 const { blackMax } = resolveSkullThresholds(settings);
67 return Math.max(0, Math.floor(Number(value) || 0)) > blackMax;
31}
68}
32
69
33/**
70/**
@@ -53,26 +90,34 @@ function lerp (a, b, t)
53
90
54/**
91/**
55 * Magenta-tint target color for a skull value (0 = invisible / unused).
92 * Magenta-tint target color for a skull value (0 = invisible / unused).
56 * 1 → white; … → pure red at 500_000; then → dark red (128,0,0) by 1_000_000.
93 * 1…blackMax → white→black; …→ pure red at redMax; then → dark red (128,0,0).
94 * @param {number} value
95 * @param {{ skullBlackMax?: number, skullRedMax?: number } | null | undefined} [settings]
57 * @returns {{ r: number, g: number, b: number } | null} 0–1 channels, or null if invisible
96 * @returns {{ r: number, g: number, b: number } | null} 0–1 channels, or null if invisible
58 */
97 */
59export function skullTintColor (value)
98export function skullTintColor (value, settings = null)
60{
99{
61 const v = Math.max(0, Math.floor(Number(value) || 0));
100 const v = Math.max(0, Math.floor(Number(value) || 0));
62 if (v <= 0)
101 if (v <= 0)
63 { return null; }
102 { return null; }
64 if (v <= SKULL_RED_LERP_START)
103 const { blackMax, redMax, darkRedMax } = resolveSkullThresholds(settings);
65 { return Object.freeze({ r: 1, g: 1, b: 1 }); }
104 if (v <= blackMax)
66 if (v <= SKULL_PURE_RED_VALUE)
105 {
106 const t = v / blackMax;
107 const c = lerp(1, 0, t);
108 return Object.freeze({ r: c, g: c, b: c });
109 }
110 if (v <= redMax)
67 {
111 {
68 const t = (v - SKULL_RED_LERP_START) / (SKULL_PURE_RED_VALUE - SKULL_RED_LERP_START);
112 // Black (0,0,0) → pure red (1,0,0).
113 const t = (v - blackMax) / (redMax - blackMax);
69 return Object.freeze({
114 return Object.freeze({
70 r: 1,
115 r: lerp(0, 1, t),
71 g: lerp(1, 0, t),
116 g: 0,
72 b: lerp(1, 0, t)
117 b: 0
73 });
118 });
74 }
119 }
75 const t = Math.min(1, (v - SKULL_PURE_RED_VALUE) / (SKULL_DARK_RED_VALUE - SKULL_PURE_RED_VALUE));
120 const t = Math.min(1, (v - redMax) / (darkRedMax - redMax));
76 const dark = 128 / 255;
121 const dark = 128 / 255;
77 return Object.freeze({
122 return Object.freeze({
78 r: lerp(1, dark, t),
123 r: lerp(1, dark, t),
shared/definitions/worldSettings.js +22 −0
@@ -165,6 +165,28 @@ export const WORLD_SETTING_CATEGORIES = [
165 165 }
166 166 ]
167 167 },
168 {
169 id: "combat",
170 label: "Combat",
171 fields: [
172 {
173 key: "skullBlackMax",
174 label: "Skull black max",
175 hint: "Skull score at pure black. Below this, tint lerps white→black. Above this, town guards treat the player as hostile and tint lerps toward red.",
176 type: "number",
177 min: 1,
178 step: 1
179 },
180 {
181 key: "skullRedMax",
182 label: "Skull red max",
183 hint: "Skull score at pure red. Between black max and this, tint lerps black→red; beyond it, toward dark red (clamped at 2× this value).",
184 type: "number",
185 min: 1,
186 step: 1
187 }
188 ]
189 },
168 190 {
169 191 id: "starterEquipment",
170 192 label: "Starter Equipment",
@@ -165,6 +165,28 @@ export const WORLD_SETTING_CATEGORIES = [
165 }
165 }
166 ]
166 ]
167 },
167 },
168 {
169 id: "combat",
170 label: "Combat",
171 fields: [
172 {
173 key: "skullBlackMax",
174 label: "Skull black max",
175 hint: "Skull score at pure black. Below this, tint lerps white→black. Above this, town guards treat the player as hostile and tint lerps toward red.",
176 type: "number",
177 min: 1,
178 step: 1
179 },
180 {
181 key: "skullRedMax",
182 label: "Skull red max",
183 hint: "Skull score at pure red. Between black max and this, tint lerps black→red; beyond it, toward dark red (clamped at 2× this value).",
184 type: "number",
185 min: 1,
186 step: 1
187 }
188 ]
189 },
168 {
190 {
169 id: "starterEquipment",
191 id: "starterEquipment",
170 label: "Starter Equipment",
192 label: "Starter Equipment",
shared/protocol/constants.js +4 −0
@@ -66,6 +66,10 @@ export const DEFAULT_WORLD_SETTINGS = Object.freeze({
66 66 pigeonCruiseTilesPerSec: 8,
67 67 pigeonLandingTilesPerSec: 3,
68 68 pigeonPhaseTiles: 5,
69 /** Skull value at pure black (white→black below; guards aggro above). */
70 skullBlackMax: 500,
71 /** Skull value at pure red (black→red below; dark red beyond, up to 2×). */
72 skullRedMax: 500_000,
69 73 /** New characters only: equipment slot → itemId (or null). */
70 74 starterEquipment: DEFAULT_STARTER_EQUIPMENT,
71 75 /** New characters only: inventory stacks `{ itemId, quantity }[]`. */
@@ -66,6 +66,10 @@ export const DEFAULT_WORLD_SETTINGS = Object.freeze({
66 pigeonCruiseTilesPerSec: 8,
66 pigeonCruiseTilesPerSec: 8,
67 pigeonLandingTilesPerSec: 3,
67 pigeonLandingTilesPerSec: 3,
68 pigeonPhaseTiles: 5,
68 pigeonPhaseTiles: 5,
69 /** Skull value at pure black (white→black below; guards aggro above). */
70 skullBlackMax: 500,
71 /** Skull value at pure red (black→red below; dark red beyond, up to 2×). */
72 skullRedMax: 500_000,
69 /** New characters only: equipment slot → itemId (or null). */
73 /** New characters only: equipment slot → itemId (or null). */
70 starterEquipment: DEFAULT_STARTER_EQUIPMENT,
74 starterEquipment: DEFAULT_STARTER_EQUIPMENT,
71 /** New characters only: inventory stacks `{ itemId, quantity }[]`. */
75 /** New characters only: inventory stacks `{ itemId, quantity }[]`. */