BlueGit

timfa questward

Prefer Attack as default click on aggressive talkable humanoids.

LMB was choosing Talk (adjacent approach) which looked like broken staff melee; drop the client cast-instead-of-attack workaround and replicate aggressive for the client.

timfa Tim Falken · pushed by timfa

Authored 2026-09-02 · 2 months ago

Hash
c2178a805ca5a2aa82a29f0883937eb6438feb92
Parent
33e69150
7 files changed +223 −47
client/src/play/main.js +22 −26
@@ -1,3 +1,4 @@
1 import { defaultLeftClickEntityAction } from "/shared/combat/entityClick.js";
1 2 import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";
2 3 import { validateCharacterNames } from "/shared/characters/names.js";
3 4 import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";
@@ -996,19 +997,6 @@ function requestAttack (targetEntityId)
996 997 {
997 998 clearAllPendingActions();
998 999 setAutorunFocusEntity(targetEntityId);
999 // Staff + castable favourite attack spell: use the spell cast/approach path (spell range),
1000 // not melee Attack. Server keeps auto-repeat for that staff combat target.
1001 const staffSpellId = spells?.getStaffAttackSpellId?.();
1002 if (staffSpellId)
1003 {
1004 socket.send({ type: MessageType.armSpell, spellId: staffSpellId });
1005 socket.send({
1006 type: MessageType.castSpellEntity,
1007 spellId: staffSpellId,
1008 targetEntityId
1009 });
1010 return;
1011 }
1012 1000 socket.send({ type: MessageType.attack, targetEntityId });
1013 1001 }
1014 1002
@@ -1065,27 +1053,26 @@ function handleLeftClick (mouseX, mouseY)
1065 1053 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
1066 1054 {
1067 1055 clearPendingPickup();
1068 if (entity.characterId)
1056 const clickAction = defaultLeftClickEntityAction(entity);
1057 if (clickAction === "inspect")
1069 1058 {
1070 1059 clearPendingInteract();
1071 1060 social.inspect(entity);
1072 1061 return;
1073 1062 }
1074 // Dialogue (or shop/talkable) wins over attack as the default click.
1075 if (canTalkToEntity(entity))
1063 if (clickAction === "attack")
1064 {
1065 requestAttack(entity.entityId);
1066 return;
1067 }
1068 if (clickAction === "talk")
1076 1069 {
1077 // Same intent as context-menu Talk / Shop so dialogue pin matches.
1078 1070 const purpose = entity.hasDialogue || (entity.talkable && !entity.hasShop)
1079 1071 ? "talk"
1080 1072 : (entity.hasShop ? "shop" : "talk");
1081 1073 requestTalk(entity.entityId, purpose);
1082 1074 return;
1083 1075 }
1084 if (isAttackableEntity(entity))
1085 {
1086 requestAttack(entity.entityId);
1087 return;
1088 }
1089 1076 // Clicked an entity we don't act on — do not fall through to walk (that cancels combat).
1090 1077 return;
1091 1078 }
@@ -1211,6 +1198,17 @@ function handleRightClick (event, mouseX, mouseY)
1211 1198 });
1212 1199 if (entity.untargetable)
1213 1200 { continue; }
1201 const aggressive = !!(entity.aggressive || entity.hostile);
1202 const pushAttack = () =>
1203 {
1204 items.push({
1205 label: `Attack ${targetName}`,
1206 onSelect: () => requestAttack(entity.entityId)
1207 });
1208 };
1209 // Aggressive hostiles: Attack first (matches LMB default); Talk/Shop still available.
1210 if (aggressive && isAttackableEntity(entity))
1211 { pushAttack(); }
1214 1212 if (entity.hasShop)
1215 1213 {
1216 1214 items.push({
@@ -1243,10 +1241,8 @@ function handleRightClick (event, mouseX, mouseY)
1243 1241 }
1244 1242 });
1245 1243 }
1246 items.push({
1247 label: `Attack ${targetName}`,
1248 onSelect: () => requestAttack(entity.entityId)
1249 });
1244 if (!(aggressive && isAttackableEntity(entity)))
1245 { pushAttack(); }
1250 1246 }
1251 1247
1252 1248 for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY))
@@ -1,3 +1,4 @@
1import { defaultLeftClickEntityAction } from "/shared/combat/entityClick.js";
1import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";
2import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";
2import { validateCharacterNames } from "/shared/characters/names.js";
3import { validateCharacterNames } from "/shared/characters/names.js";
3import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";
4import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";
@@ -996,19 +997,6 @@ function requestAttack (targetEntityId)
996{
997{
997 clearAllPendingActions();
998 clearAllPendingActions();
998 setAutorunFocusEntity(targetEntityId);
999 setAutorunFocusEntity(targetEntityId);
999 // Staff + castable favourite attack spell: use the spell cast/approach path (spell range),
1000 // not melee Attack. Server keeps auto-repeat for that staff combat target.
1001 const staffSpellId = spells?.getStaffAttackSpellId?.();
1002 if (staffSpellId)
1003 {
1004 socket.send({ type: MessageType.armSpell, spellId: staffSpellId });
1005 socket.send({
1006 type: MessageType.castSpellEntity,
1007 spellId: staffSpellId,
1008 targetEntityId
1009 });
1010 return;
1011 }
1012 socket.send({ type: MessageType.attack, targetEntityId });
1000 socket.send({ type: MessageType.attack, targetEntityId });
1013}
1001}
1014
1002
@@ -1065,27 +1053,26 @@ function handleLeftClick (mouseX, mouseY)
1065 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
1053 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
1066 {
1054 {
1067 clearPendingPickup();
1055 clearPendingPickup();
1068 if (entity.characterId)
1056 const clickAction = defaultLeftClickEntityAction(entity);
1057 if (clickAction === "inspect")
1069 {
1058 {
1070 clearPendingInteract();
1059 clearPendingInteract();
1071 social.inspect(entity);
1060 social.inspect(entity);
1072 return;
1061 return;
1073 }
1062 }
1074 // Dialogue (or shop/talkable) wins over attack as the default click.
1063 if (clickAction === "attack")
1075 if (canTalkToEntity(entity))
1064 {
1065 requestAttack(entity.entityId);
1066 return;
1067 }
1068 if (clickAction === "talk")
1076 {
1069 {
1077 // Same intent as context-menu Talk / Shop so dialogue pin matches.
1078 const purpose = entity.hasDialogue || (entity.talkable && !entity.hasShop)
1070 const purpose = entity.hasDialogue || (entity.talkable && !entity.hasShop)
1079 ? "talk"
1071 ? "talk"
1080 : (entity.hasShop ? "shop" : "talk");
1072 : (entity.hasShop ? "shop" : "talk");
1081 requestTalk(entity.entityId, purpose);
1073 requestTalk(entity.entityId, purpose);
1082 return;
1074 return;
1083 }
1075 }
1084 if (isAttackableEntity(entity))
1085 {
1086 requestAttack(entity.entityId);
1087 return;
1088 }
1089 // Clicked an entity we don't act on — do not fall through to walk (that cancels combat).
1076 // Clicked an entity we don't act on — do not fall through to walk (that cancels combat).
1090 return;
1077 return;
1091 }
1078 }
@@ -1211,6 +1198,17 @@ function handleRightClick (event, mouseX, mouseY)
1211 });
1198 });
1212 if (entity.untargetable)
1199 if (entity.untargetable)
1213 { continue; }
1200 { continue; }
1201 const aggressive = !!(entity.aggressive || entity.hostile);
1202 const pushAttack = () =>
1203 {
1204 items.push({
1205 label: `Attack ${targetName}`,
1206 onSelect: () => requestAttack(entity.entityId)
1207 });
1208 };
1209 // Aggressive hostiles: Attack first (matches LMB default); Talk/Shop still available.
1210 if (aggressive && isAttackableEntity(entity))
1211 { pushAttack(); }
1214 if (entity.hasShop)
1212 if (entity.hasShop)
1215 {
1213 {
1216 items.push({
1214 items.push({
@@ -1243,10 +1241,8 @@ function handleRightClick (event, mouseX, mouseY)
1243 }
1241 }
1244 });
1242 });
1245 }
1243 }
1246 items.push({
1244 if (!(aggressive && isAttackableEntity(entity)))
1247 label: `Attack ${targetName}`,
1245 { pushAttack(); }
1248 onSelect: () => requestAttack(entity.entityId)
1249 });
1250 }
1246 }
1251
1247
1252 for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY))
1248 for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY))
client/src/play/spells.js +0 −17
@@ -72,22 +72,6 @@ export function initSpells ({
72 72 renderBar();
73 73 }
74 74
75 /**
76 * Favourite attack spell used for staff LMB / retaliate, or null if unequipped / unavailable.
77 */
78 function getStaffAttackSpellId ()
79 {
80 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
81 { return null; }
82 const spellId = firstFavoriteAttackSpellId({ favorites: favoriteOrder });
83 if (!spellId)
84 { return null; }
85 const spell = spells.find((entry) => entry.spellId === spellId);
86 if (!spell?.canCast || spell.target !== "entity")
87 { return null; }
88 return spellId;
89 }
90
91 75 function disarmLocal ()
92 76 {
93 77 if (!armedSpellId)
@@ -419,7 +403,6 @@ export function initSpells ({
419 403 return spells.map((spell) => spell.spellId);
420 404 },
421 405 refreshMaxHitDisplay,
422 getStaffAttackSpellId,
423 406 handleSpellDisarmed ()
424 407 {
425 408 disarmLocal();
@@ -72,22 +72,6 @@ export function initSpells ({
72 renderBar();
72 renderBar();
73 }
73 }
74
74
75 /**
76 * Favourite attack spell used for staff LMB / retaliate, or null if unequipped / unavailable.
77 */
78 function getStaffAttackSpellId ()
79 {
80 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
81 { return null; }
82 const spellId = firstFavoriteAttackSpellId({ favorites: favoriteOrder });
83 if (!spellId)
84 { return null; }
85 const spell = spells.find((entry) => entry.spellId === spellId);
86 if (!spell?.canCast || spell.target !== "entity")
87 { return null; }
88 return spellId;
89 }
90
91 function disarmLocal ()
75 function disarmLocal ()
92 {
76 {
93 if (!armedSpellId)
77 if (!armedSpellId)
@@ -419,7 +403,6 @@ export function initSpells ({
419 return spells.map((spell) => spell.spellId);
403 return spells.map((spell) => spell.spellId);
420 },
404 },
421 refreshMaxHitDisplay,
405 refreshMaxHitDisplay,
422 getStaffAttackSpellId,
423 handleSpellDisarmed ()
406 handleSpellDisarmed ()
424 {
407 {
425 disarmLocal();
408 disarmLocal();
client/src/render/worldView.js +3 −0
@@ -2326,6 +2326,9 @@ export class WorldView
2326 2326 if (payload.level != null)
2327 2327 { entity.level = payload.level | 0; }
2328 2328 entity.hostile = payload.hostile != null ? !!payload.hostile : !!entity.hostile;
2329 entity.aggressive = payload.aggressive != null
2330 ? !!payload.aggressive
2331 : (payload.hostile != null ? !!payload.hostile : !!entity.aggressive);
2329 2332 entity.canFight = payload.canFight != null
2330 2333 ? !!payload.canFight
2331 2334 : !!(entity.canFight || (payload.mobTypeId && !payload.talkable));
@@ -2326,6 +2326,9 @@ export class WorldView
2326 if (payload.level != null)
2326 if (payload.level != null)
2327 { entity.level = payload.level | 0; }
2327 { entity.level = payload.level | 0; }
2328 entity.hostile = payload.hostile != null ? !!payload.hostile : !!entity.hostile;
2328 entity.hostile = payload.hostile != null ? !!payload.hostile : !!entity.hostile;
2329 entity.aggressive = payload.aggressive != null
2330 ? !!payload.aggressive
2331 : (payload.hostile != null ? !!payload.hostile : !!entity.aggressive);
2329 entity.canFight = payload.canFight != null
2332 entity.canFight = payload.canFight != null
2330 ? !!payload.canFight
2333 ? !!payload.canFight
2331 : !!(entity.canFight || (payload.mobTypeId && !payload.talkable));
2334 : !!(entity.canFight || (payload.mobTypeId && !payload.talkable));
server/src/ecs/serialize.js +21 −3
@@ -6,11 +6,27 @@ import {
6 6 statusVisualScale,
7 7 publicStatusEffectsView
8 8 } from "../../../shared/statusEffects/rules.js";
9 import { mobDefinition } from "../../../shared/definitions/mobs.js";
10 import { mobCanFight } from "../../../shared/definitions/mobCombat.js";
9 import { mobDefinition, isTownGuardMob } from "../../../shared/definitions/mobs.js";
10 import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js";
11 11 import { shopHasInterface } from "../shop/service.js";
12 12 import { composeAppearanceLayers } from "../../../shared/appearance/hair.js";
13 13
14 /** Matches server aggression: spawnOrigin.aggressive, else mob def (not town guards / summons). */
15 export function entityIsAggressiveHostile (ecs, handle)
16 {
17 const character = ecs.get(handle, Component.character);
18 const creature = ecs.get(handle, Component.creature);
19 if (!creature || character)
20 { return false; }
21 if (ecs.get(handle, Component.summon))
22 { return false; }
23 const origin = ecs.get(handle, Component.spawnOrigin);
24 if (origin?.townGuard || isTownGuardMob(creature.mobTypeId))
25 { return false; }
26 if (origin)
27 { return !!origin.aggressive; }
28 return mobIsAggressive(mobDefinition(creature.mobTypeId));
29 }
14 30 export function normalizeMobileComponents (components)
15 31 {
16 32 const next = { ...(components ?? {}) };
@@ -100,7 +116,8 @@ export function publicEntityView (ecs, entityId)
100 116 mobTypeId: creature?.mobTypeId ?? null,
101 117 element: creature?.element ?? null,
102 118 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
103 hostile: !!mob?.hostile,
119 aggressive: entityIsAggressiveHostile(ecs, handle),
120 hostile: entityIsAggressiveHostile(ecs, handle),
104 121 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
105 122 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
106 123 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
@@ -148,6 +165,7 @@ export function replicationSpawn (view)
148 165 layers: view.layers ?? [],
149 166 mobTypeId: view.mobTypeId ?? null,
150 167 canFight: !!view.canFight,
168 aggressive: !!view.aggressive,
151 169 hostile: !!view.hostile,
152 170 talkable: !!view.talkable,
153 171 hasDialogue: !!view.hasDialogue,
@@ -6,11 +6,27 @@ import {
6 statusVisualScale,
6 statusVisualScale,
7 publicStatusEffectsView
7 publicStatusEffectsView
8} from "../../../shared/statusEffects/rules.js";
8} from "../../../shared/statusEffects/rules.js";
9import { mobDefinition } from "../../../shared/definitions/mobs.js";
9import { mobDefinition, isTownGuardMob } from "../../../shared/definitions/mobs.js";
10import { mobCanFight } from "../../../shared/definitions/mobCombat.js";
10import { mobCanFight, mobIsAggressive } from "../../../shared/definitions/mobCombat.js";
11import { shopHasInterface } from "../shop/service.js";
11import { shopHasInterface } from "../shop/service.js";
12import { composeAppearanceLayers } from "../../../shared/appearance/hair.js";
12import { composeAppearanceLayers } from "../../../shared/appearance/hair.js";
13
13
14/** Matches server aggression: spawnOrigin.aggressive, else mob def (not town guards / summons). */
15export function entityIsAggressiveHostile (ecs, handle)
16{
17 const character = ecs.get(handle, Component.character);
18 const creature = ecs.get(handle, Component.creature);
19 if (!creature || character)
20 { return false; }
21 if (ecs.get(handle, Component.summon))
22 { return false; }
23 const origin = ecs.get(handle, Component.spawnOrigin);
24 if (origin?.townGuard || isTownGuardMob(creature.mobTypeId))
25 { return false; }
26 if (origin)
27 { return !!origin.aggressive; }
28 return mobIsAggressive(mobDefinition(creature.mobTypeId));
29}
14export function normalizeMobileComponents (components)
30export function normalizeMobileComponents (components)
15{
31{
16 const next = { ...(components ?? {}) };
32 const next = { ...(components ?? {}) };
@@ -100,7 +116,8 @@ export function publicEntityView (ecs, entityId)
100 mobTypeId: creature?.mobTypeId ?? null,
116 mobTypeId: creature?.mobTypeId ?? null,
101 element: creature?.element ?? null,
117 element: creature?.element ?? null,
102 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
118 canFight: mob ? mobCanFight(mob) : (!!character && !ecs.has(handle, Component.dead)),
103 hostile: !!mob?.hostile,
119 aggressive: entityIsAggressiveHostile(ecs, handle),
120 hostile: entityIsAggressiveHostile(ecs, handle),
104 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
121 talkable: (!!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop))) && !ecs.has(handle, Component.dead),
105 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
122 hasDialogue: !!(ecs.get(handle, Component.spawnOrigin)?.dialogueId) && !ecs.has(handle, Component.dead),
106 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
123 hasShop: shopHasInterface(ecs.get(handle, Component.shop)) && !ecs.has(handle, Component.dead),
@@ -148,6 +165,7 @@ export function replicationSpawn (view)
148 layers: view.layers ?? [],
165 layers: view.layers ?? [],
149 mobTypeId: view.mobTypeId ?? null,
166 mobTypeId: view.mobTypeId ?? null,
150 canFight: !!view.canFight,
167 canFight: !!view.canFight,
168 aggressive: !!view.aggressive,
151 hostile: !!view.hostile,
169 hostile: !!view.hostile,
152 talkable: !!view.talkable,
170 talkable: !!view.talkable,
153 hasDialogue: !!view.hasDialogue,
171 hasDialogue: !!view.hasDialogue,
server/src/gateway/websocket.js +2 −1
@@ -2323,7 +2323,8 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
2323 2323 layers: record.layers ?? event.layers ?? [],
2324 2324 mobTypeId: record.mobTypeId ?? event.mobTypeId ?? null,
2325 2325 canFight: !!(record.canFight ?? event.canFight),
2326 hostile: !!(record.hostile ?? event.hostile),
2326 aggressive: !!(record.aggressive ?? event.aggressive ?? record.hostile ?? event.hostile),
2327 hostile: !!(record.hostile ?? event.hostile ?? record.aggressive ?? event.aggressive),
2327 2328 talkable: !!(record.talkable ?? event.talkable),
2328 2329 hasDialogue: !!(record.hasDialogue ?? event.hasDialogue),
2329 2330 hasShop: !!(record.hasShop ?? event.hasShop),
@@ -2323,7 +2323,8 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
2323 layers: record.layers ?? event.layers ?? [],
2323 layers: record.layers ?? event.layers ?? [],
2324 mobTypeId: record.mobTypeId ?? event.mobTypeId ?? null,
2324 mobTypeId: record.mobTypeId ?? event.mobTypeId ?? null,
2325 canFight: !!(record.canFight ?? event.canFight),
2325 canFight: !!(record.canFight ?? event.canFight),
2326 hostile: !!(record.hostile ?? event.hostile),
2326 aggressive: !!(record.aggressive ?? event.aggressive ?? record.hostile ?? event.hostile),
2327 hostile: !!(record.hostile ?? event.hostile ?? record.aggressive ?? event.aggressive),
2327 talkable: !!(record.talkable ?? event.talkable),
2328 talkable: !!(record.talkable ?? event.talkable),
2328 hasDialogue: !!(record.hasDialogue ?? event.hasDialogue),
2329 hasDialogue: !!(record.hasDialogue ?? event.hasDialogue),
2329 hasShop: !!(record.hasShop ?? event.hasShop),
2330 hasShop: !!(record.hasShop ?? event.hasShop),
server/tests/entityClick.test.js added +139 −0
@@ -0,0 +1,139 @@
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import { defaultLeftClickEntityAction } from "../../shared/combat/entityClick.js";
4 import { mobComponents } from "../src/simulation/spawners.js";
5 import { MobId } from "../../shared/definitions/mobs.js";
6 import { GemItemId } from "../../shared/definitions/gemItems.js";
7 import { SpellId } from "../../shared/definitions/spells.js";
8 import { Component } from "../src/ecs/components.js";
9 import { publicEntityView, replicationSpawn } from "../src/ecs/serialize.js";
10 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
11 import { chebyshev } from "../src/combat/service.js";
12
13 test("default LMB prefers attack on aggressive talkable humanoids", () =>
14 {
15 assert.equal(defaultLeftClickEntityAction({
16 mobTypeId: MobId.humanoid,
17 canFight: true,
18 talkable: true,
19 aggressive: true
20 }), "attack");
21 assert.equal(defaultLeftClickEntityAction({
22 mobTypeId: MobId.humanoid,
23 canFight: true,
24 talkable: true,
25 hostile: true
26 }), "attack");
27 });
28
29 test("default LMB prefers talk on peaceful talkable humanoids", () =>
30 {
31 assert.equal(defaultLeftClickEntityAction({
32 mobTypeId: MobId.humanoid,
33 canFight: true,
34 talkable: true,
35 aggressive: false
36 }), "talk");
37 assert.equal(defaultLeftClickEntityAction({
38 mobTypeId: MobId.humanoid,
39 canFight: true,
40 hasShop: true,
41 talkable: true
42 }), "talk");
43 });
44
45 test("default LMB attacks non-talkable hostiles and inspects players", () =>
46 {
47 assert.equal(defaultLeftClickEntityAction({
48 mobTypeId: MobId.rat,
49 canFight: true,
50 aggressive: true
51 }), "attack");
52 assert.equal(defaultLeftClickEntityAction({
53 characterId: "c1",
54 canFight: true
55 }), "inspect");
56 assert.equal(defaultLeftClickEntityAction({ untargetable: true, canFight: true }), null);
57 });
58
59 test("aggressive talkable humanoid snapshots expose aggressive for clients", () =>
60 {
61 const harness = makeSimulation({ workerCount: 1 });
62 try
63 {
64 const npcId = harness.simulation.spawnEntity(mobComponents({
65 tileX: 12,
66 tileY: 8,
67 mobTypeId: MobId.humanoid,
68 spawnerId: "bandit-1",
69 aggressive: true,
70 displayName: "Bandit",
71 sex: "male"
72 }));
73 const worker = harness.simulation.workers[0];
74 const view = publicEntityView(worker.world.ecs, npcId);
75 assert.equal(view.talkable, true);
76 assert.equal(view.canFight, true);
77 assert.equal(view.aggressive, true);
78 assert.equal(view.hostile, true);
79 const spawn = replicationSpawn(view);
80 assert.equal(spawn.aggressive, true);
81 assert.equal(spawn.hostile, true);
82 assert.equal(defaultLeftClickEntityAction(spawn), "attack");
83
84 const record = harness.simulation.entityRecord(npcId);
85 assert.equal(record.aggressive, true);
86 }
87 finally
88 { harness.close(); }
89 });
90
91 test("staff click-attack on aggressive humanoid uses spell range not talk-adjacent", () =>
92 {
93 const harness = makeSimulation({ workerCount: 1 });
94 try
95 {
96 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
97 const playerId = harness.simulation.spawnCharacter(1, character);
98 const npcId = harness.simulation.spawnEntity(mobComponents({
99 tileX: 28,
100 tileY: 8,
101 mobTypeId: MobId.humanoid,
102 spawnerId: "bandit-staff",
103 aggressive: true,
104 displayName: "Bandit",
105 sex: "male"
106 }));
107 const worker = harness.simulation.workers[0];
108 const handle = worker.world.ecs.handleOf(playerId);
109 const equipment = worker.world.ecs.get(handle, Component.equipment);
110 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
111 const spells = worker.world.ecs.get(handle, Component.spells);
112 spells.known.push(SpellId.manaBullet);
113 spells.favorites = [SpellId.manaBullet];
114 spells.autoCast = false;
115
116 harness.simulation.enqueue(1, {
117 type: "attack",
118 characterId: character.character_id,
119 targetEntityId: npcId
120 });
121 harness.simulation.step();
122
123 const casting = worker.world.ecs.get(handle, Component.spellcasting);
124 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
125 assert.equal(casting.repeatTargetEntityId, npcId);
126
127 const path = worker.world.ecs.get(handle, Component.path);
128 const tiles = path?.tiles ?? [];
129 assert.ok(tiles.length > 0, "should approach into spell range");
130 const last = tiles[tiles.length - 1];
131 const standDist = chebyshev(last.x, last.y, 28, 8);
132 assert.ok(
133 standDist > 1,
134 `spell approach should stop outside talk/melee adjacency, got dist=${standDist}`
135 );
136 }
137 finally
138 { harness.close(); }
139 });
@@ -0,0 +1,139 @@
1import test from "node:test";
2import assert from "node:assert/strict";
3import { defaultLeftClickEntityAction } from "../../shared/combat/entityClick.js";
4import { mobComponents } from "../src/simulation/spawners.js";
5import { MobId } from "../../shared/definitions/mobs.js";
6import { GemItemId } from "../../shared/definitions/gemItems.js";
7import { SpellId } from "../../shared/definitions/spells.js";
8import { Component } from "../src/ecs/components.js";
9import { publicEntityView, replicationSpawn } from "../src/ecs/serialize.js";
10import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
11import { chebyshev } from "../src/combat/service.js";
12
13test("default LMB prefers attack on aggressive talkable humanoids", () =>
14{
15 assert.equal(defaultLeftClickEntityAction({
16 mobTypeId: MobId.humanoid,
17 canFight: true,
18 talkable: true,
19 aggressive: true
20 }), "attack");
21 assert.equal(defaultLeftClickEntityAction({
22 mobTypeId: MobId.humanoid,
23 canFight: true,
24 talkable: true,
25 hostile: true
26 }), "attack");
27});
28
29test("default LMB prefers talk on peaceful talkable humanoids", () =>
30{
31 assert.equal(defaultLeftClickEntityAction({
32 mobTypeId: MobId.humanoid,
33 canFight: true,
34 talkable: true,
35 aggressive: false
36 }), "talk");
37 assert.equal(defaultLeftClickEntityAction({
38 mobTypeId: MobId.humanoid,
39 canFight: true,
40 hasShop: true,
41 talkable: true
42 }), "talk");
43});
44
45test("default LMB attacks non-talkable hostiles and inspects players", () =>
46{
47 assert.equal(defaultLeftClickEntityAction({
48 mobTypeId: MobId.rat,
49 canFight: true,
50 aggressive: true
51 }), "attack");
52 assert.equal(defaultLeftClickEntityAction({
53 characterId: "c1",
54 canFight: true
55 }), "inspect");
56 assert.equal(defaultLeftClickEntityAction({ untargetable: true, canFight: true }), null);
57});
58
59test("aggressive talkable humanoid snapshots expose aggressive for clients", () =>
60{
61 const harness = makeSimulation({ workerCount: 1 });
62 try
63 {
64 const npcId = harness.simulation.spawnEntity(mobComponents({
65 tileX: 12,
66 tileY: 8,
67 mobTypeId: MobId.humanoid,
68 spawnerId: "bandit-1",
69 aggressive: true,
70 displayName: "Bandit",
71 sex: "male"
72 }));
73 const worker = harness.simulation.workers[0];
74 const view = publicEntityView(worker.world.ecs, npcId);
75 assert.equal(view.talkable, true);
76 assert.equal(view.canFight, true);
77 assert.equal(view.aggressive, true);
78 assert.equal(view.hostile, true);
79 const spawn = replicationSpawn(view);
80 assert.equal(spawn.aggressive, true);
81 assert.equal(spawn.hostile, true);
82 assert.equal(defaultLeftClickEntityAction(spawn), "attack");
83
84 const record = harness.simulation.entityRecord(npcId);
85 assert.equal(record.aggressive, true);
86 }
87 finally
88 { harness.close(); }
89});
90
91test("staff click-attack on aggressive humanoid uses spell range not talk-adjacent", () =>
92{
93 const harness = makeSimulation({ workerCount: 1 });
94 try
95 {
96 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
97 const playerId = harness.simulation.spawnCharacter(1, character);
98 const npcId = harness.simulation.spawnEntity(mobComponents({
99 tileX: 28,
100 tileY: 8,
101 mobTypeId: MobId.humanoid,
102 spawnerId: "bandit-staff",
103 aggressive: true,
104 displayName: "Bandit",
105 sex: "male"
106 }));
107 const worker = harness.simulation.workers[0];
108 const handle = worker.world.ecs.handleOf(playerId);
109 const equipment = worker.world.ecs.get(handle, Component.equipment);
110 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
111 const spells = worker.world.ecs.get(handle, Component.spells);
112 spells.known.push(SpellId.manaBullet);
113 spells.favorites = [SpellId.manaBullet];
114 spells.autoCast = false;
115
116 harness.simulation.enqueue(1, {
117 type: "attack",
118 characterId: character.character_id,
119 targetEntityId: npcId
120 });
121 harness.simulation.step();
122
123 const casting = worker.world.ecs.get(handle, Component.spellcasting);
124 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
125 assert.equal(casting.repeatTargetEntityId, npcId);
126
127 const path = worker.world.ecs.get(handle, Component.path);
128 const tiles = path?.tiles ?? [];
129 assert.ok(tiles.length > 0, "should approach into spell range");
130 const last = tiles[tiles.length - 1];
131 const standDist = chebyshev(last.x, last.y, 28, 8);
132 assert.ok(
133 standDist > 1,
134 `spell approach should stop outside talk/melee adjacency, got dist=${standDist}`
135 );
136 }
137 finally
138 { harness.close(); }
139});
shared/combat/entityClick.js added +36 −0
@@ -0,0 +1,36 @@
1 /**
2 * Client LMB default against world entities (snapshot fields only).
3 * Aggressive hostiles prefer Attack even when they are also talkable.
4 */
5
6 /**
7 * @param {{
8 * untargetable?: boolean,
9 * characterId?: string|number|null,
10 * canFight?: boolean,
11 * mobTypeId?: string|number|null,
12 * talkable?: boolean,
13 * hasDialogue?: boolean,
14 * hasShop?: boolean,
15 * aggressive?: boolean,
16 * hostile?: boolean
17 * }} entity
18 * @returns {"inspect"|"attack"|"talk"|null}
19 */
20 export function defaultLeftClickEntityAction (entity)
21 {
22 if (!entity || entity.untargetable)
23 { return null; }
24 if (entity.characterId)
25 { return "inspect"; }
26 const attackable = !!entity.canFight
27 || (!!entity.mobTypeId && !entity.talkable);
28 const aggressive = !!(entity.aggressive || entity.hostile);
29 if (attackable && aggressive)
30 { return "attack"; }
31 if (entity.hasDialogue || entity.talkable || entity.hasShop)
32 { return "talk"; }
33 if (attackable)
34 { return "attack"; }
35 return null;
36 }
@@ -0,0 +1,36 @@
1/**
2 * Client LMB default against world entities (snapshot fields only).
3 * Aggressive hostiles prefer Attack even when they are also talkable.
4 */
5
6/**
7 * @param {{
8 * untargetable?: boolean,
9 * characterId?: string|number|null,
10 * canFight?: boolean,
11 * mobTypeId?: string|number|null,
12 * talkable?: boolean,
13 * hasDialogue?: boolean,
14 * hasShop?: boolean,
15 * aggressive?: boolean,
16 * hostile?: boolean
17 * }} entity
18 * @returns {"inspect"|"attack"|"talk"|null}
19 */
20export function defaultLeftClickEntityAction (entity)
21{
22 if (!entity || entity.untargetable)
23 { return null; }
24 if (entity.characterId)
25 { return "inspect"; }
26 const attackable = !!entity.canFight
27 || (!!entity.mobTypeId && !entity.talkable);
28 const aggressive = !!(entity.aggressive || entity.hostile);
29 if (attackable && aggressive)
30 { return "attack"; }
31 if (entity.hasDialogue || entity.talkable || entity.hasShop)
32 { return "talk"; }
33 if (attackable)
34 { return "attack"; }
35 return null;
36}