Prefer talk/shop/approach over attack for friendly humanoids.
LMB on peaceful NPCs starts dialogue or shop, or walks up; Attack stays RMB-only unless they are aggressive, already fighting you, or town guards reacting to a red skull. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -1,4 +1,4 @@
1
import { defaultLeftClickEntityAction } from "/shared/combat/entityClick.js";
1
import { defaultLeftClickEntityAction, preferAttackInContextMenu } from "/shared/combat/entityClick.js";
2
2
import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";
3
3
import { validateCharacterNames } from "/shared/characters/names.js";
4
4
import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";
@@ -115,10 +115,47 @@ let autorunFocusTile = null;
115
115
let autorunWaitingStamina = false;
116
116
/** Framework can report mousePressed for two frames; latch so one physical click is one command. */
117
117
let leftClickConsumed = false;
118
/** Mob/NPC entity ids recently trading blows with the local player (LMB prefers Attack). */
119
const combatWithLocalIds = new Set();
118
120
119
121
const AUTORUN_DISTANCE_TILES = 3;
120
122
const AUTORUN_STAMINA_RESUME_RATIO = 0.5;
121
123
124
function noteCombatWithLocal (attackerId, targetId)
125
{
126
const localId = view?.localEntityId;
127
if (localId == null)
128
{ return; }
129
const a = attackerId | 0;
130
const t = targetId | 0;
131
if (a === localId && t)
132
{ combatWithLocalIds.add(t); }
133
else if (t === localId && a)
134
{ combatWithLocalIds.add(a); }
135
}
136
137
function clearCombatWithLocal (entityId = null)
138
{
139
if (entityId == null)
140
{
141
combatWithLocalIds.clear();
142
return;
143
}
144
combatWithLocalIds.delete(entityId | 0);
145
}
146
147
function leftClickContextFor (entity)
148
{
149
const local = view?.entities.get(view.localEntityId);
150
const entityId = entity?.entityId | 0;
151
return {
152
localSkullValue: local?.skullValue | 0,
153
skullSettings: view?.settings ?? null,
154
inCombatWithLocal: combatWithLocalIds.has(entityId)
155
|| (autorunFocusEntityId | 0) === entityId
156
};
157
}
158
122
159
function sendPlayerMoveTo (tileX, tileY)
123
160
{
124
161
followTargetId = null;
@@ -1076,10 +1113,20 @@ function flushPendingPickup ()
1076
1113
clearPendingPickup();
1077
1114
}
1078
1115
1116
function requestApproachEntity (targetEntityId)
1117
{
1118
clearAllPendingActions();
1119
const target = view?.entities.get(targetEntityId);
1120
if (!target)
1121
{ return; }
1122
approachAdjacentTile(target.tileX, target.tileY);
1123
}
1124
1079
1125
function requestAttack (targetEntityId)
1080
1126
{
1081
1127
clearAllPendingActions();
1082
1128
setAutorunFocusEntity(targetEntityId);
1129
combatWithLocalIds.add(targetEntityId | 0);
1083
1130
socket.send({ type: MessageType.attack, targetEntityId });
1084
1131
}
1085
1132
@@ -1136,7 +1183,7 @@ function handleLeftClick (mouseX, mouseY)
1136
1183
if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
1137
1184
{
1138
1185
clearPendingPickup();
1139
const clickAction = defaultLeftClickEntityAction(entity);
1186
const clickAction = defaultLeftClickEntityAction(entity, leftClickContextFor(entity));
1140
1187
if (clickAction === "inspect")
1141
1188
{
1142
1189
clearPendingInteract();
@@ -1156,6 +1203,11 @@ function handleLeftClick (mouseX, mouseY)
1156
1203
requestTalk(entity.entityId, purpose);
1157
1204
return;
1158
1205
}
1206
if (clickAction === "approach")
1207
{
1208
requestApproachEntity(entity.entityId);
1209
return;
1210
}
1159
1211
// Clicked an entity we don't act on — do not fall through to walk (that cancels combat).
1160
1212
return;
1161
1213
}
@@ -1281,16 +1333,19 @@ function handleRightClick (event, mouseX, mouseY)
1281
1333
});
1282
1334
if (entity.untargetable)
1283
1335
{ continue; }
1284
const aggressive = !!(entity.aggressive || entity.hostile);
1336
const clickCtx = leftClickContextFor(entity);
1337
const attackFirst = preferAttackInContextMenu(entity, clickCtx);
1285
1338
const pushAttack = () =>
1286
1339
{
1340
if (!isAttackableEntity(entity))
1341
{ return; }
1287
1342
items.push({
1288
1343
label: `Attack ${targetName}`,
1289
1344
onSelect: () => requestAttack(entity.entityId)
1290
1345
});
1291
1346
};
1292
// Aggressive hostiles: Attack first (matches LMB default); Talk/Shop still available.
1293
if (aggressive && isAttackableEntity(entity))
1347
// Prefer Attack first when that matches LMB (aggressive / combat / hostile guards).
1348
if (attackFirst)
1294
1349
{ pushAttack(); }
1295
1350
if (entity.hasShop)
1296
1351
{
@@ -1332,7 +1387,7 @@ function handleRightClick (event, mouseX, mouseY)
1332
1387
}
1333
1388
});
1334
1389
}
1335
if (!(aggressive && isAttackableEntity(entity)))
1390
if (!attackFirst)
1336
1391
{ pushAttack(); }
1337
1392
}
1338
1393
@@ -2135,6 +2190,7 @@ async function main ()
2135
2190
case MessageType.entityDespawn:
2136
2191
if (message.entityId === autorunFocusEntityId)
2137
2192
{ clearAutorunFocus(); }
2193
clearCombatWithLocal(message.entityId);
2138
2194
view?.despawnEntity(message.entityId, {
2139
2195
combatDeath: message.cause === "death"
2140
2196
});
@@ -2265,9 +2321,11 @@ async function main ()
2265
2321
social?.handleFriends(message);
2266
2322
break;
2267
2323
case MessageType.combatSwing:
2324
noteCombatWithLocal(message.attackerId, message.targetId);
2268
2325
view?.spawnCombatSwing(message);
2269
2326
break;
2270
2327
case MessageType.combatProjectile:
2328
noteCombatWithLocal(message.attackerId, message.targetId);
2271
2329
view?.spawnPhysicalProjectileFx({
2272
2330
...message,
2273
2331
sprite: "fx:projectile:arrow"
@@ -2334,6 +2392,7 @@ async function main ()
2334
2392
break;
2335
2393
case MessageType.combatHit:
2336
2394
{
2395
noteCombatWithLocal(message.attackerId, message.targetId);
2337
2396
const applyHit = () =>
2338
2397
{
2339
2398
view?.updateEntityHealth(message.targetId, message.health, message.maxHealth);
@@ -2353,11 +2412,13 @@ async function main ()
2353
2412
break;
2354
2413
}
2355
2414
case MessageType.combatMiss:
2415
noteCombatWithLocal(message.attackerId, message.targetId);
2356
2416
view?.spawnCombatDodge?.(message);
2357
2417
view?.markProjectileMiss?.(message);
2358
2418
break;
2359
2419
case MessageType.combatBlock:
2360
2420
{
2421
noteCombatWithLocal(message.attackerId, message.targetId);
2361
2422
const applyBlock = () =>
2362
2423
{
2363
2424
view?.flashBlock?.(message.targetId);
@@ -2512,6 +2573,7 @@ function returnToMenu (reason = "")
2512
2573
followTargetId = null;
2513
2574
moveDest = null;
2514
2575
clearAutorunFocus();
2576
clearCombatWithLocal();
2515
2577
autorunWaitingStamina = false;
2516
2578
clearAllPendingActions();
2517
2579
clearUiFocus();
@@ -1,4 +1,4 @@1
import { defaultLeftClickEntityAction } from "/shared/combat/entityClick.js";1
import { defaultLeftClickEntityAction, preferAttackInContextMenu } from "/shared/combat/entityClick.js";2
import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";2
import { MessageType, GAIT, CHUNK_SIZE, STATIC_CHUNK_RADIUS } from "/shared/protocol/constants.js";3
import { validateCharacterNames } from "/shared/characters/names.js";3
import { validateCharacterNames } from "/shared/characters/names.js";4
import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";4
import { COMBAT_RANGE_TILES } from "/shared/spells/constants.js";@@ -115,10 +115,47 @@ let autorunFocusTile = null;115
let autorunWaitingStamina = false;115
let autorunWaitingStamina = false;116
/** Framework can report mousePressed for two frames; latch so one physical click is one command. */116
/** Framework can report mousePressed for two frames; latch so one physical click is one command. */117
let leftClickConsumed = false;117
let leftClickConsumed = false;118
/** Mob/NPC entity ids recently trading blows with the local player (LMB prefers Attack). */119
const combatWithLocalIds = new Set();118
120
119
const AUTORUN_DISTANCE_TILES = 3;121
const AUTORUN_DISTANCE_TILES = 3;120
const AUTORUN_STAMINA_RESUME_RATIO = 0.5;122
const AUTORUN_STAMINA_RESUME_RATIO = 0.5;121
123
124
function noteCombatWithLocal (attackerId, targetId)125
{126
const localId = view?.localEntityId;127
if (localId == null)128
{ return; }129
const a = attackerId | 0;130
const t = targetId | 0;131
if (a === localId && t)132
{ combatWithLocalIds.add(t); }133
else if (t === localId && a)134
{ combatWithLocalIds.add(a); }135
}136
137
function clearCombatWithLocal (entityId = null)138
{139
if (entityId == null)140
{141
combatWithLocalIds.clear();142
return;143
}144
combatWithLocalIds.delete(entityId | 0);145
}146
147
function leftClickContextFor (entity)148
{149
const local = view?.entities.get(view.localEntityId);150
const entityId = entity?.entityId | 0;151
return {152
localSkullValue: local?.skullValue | 0,153
skullSettings: view?.settings ?? null,154
inCombatWithLocal: combatWithLocalIds.has(entityId)155
|| (autorunFocusEntityId | 0) === entityId156
};157
}158
122
function sendPlayerMoveTo (tileX, tileY)159
function sendPlayerMoveTo (tileX, tileY)123
{160
{124
followTargetId = null;161
followTargetId = null;@@ -1076,10 +1113,20 @@ function flushPendingPickup ()1076
clearPendingPickup();1113
clearPendingPickup();1077
}1114
}1078
1115
1116
function requestApproachEntity (targetEntityId)1117
{1118
clearAllPendingActions();1119
const target = view?.entities.get(targetEntityId);1120
if (!target)1121
{ return; }1122
approachAdjacentTile(target.tileX, target.tileY);1123
}1124
1079
function requestAttack (targetEntityId)1125
function requestAttack (targetEntityId)1080
{1126
{1081
clearAllPendingActions();1127
clearAllPendingActions();1082
setAutorunFocusEntity(targetEntityId);1128
setAutorunFocusEntity(targetEntityId);1129
combatWithLocalIds.add(targetEntityId | 0);1083
socket.send({ type: MessageType.attack, targetEntityId });1130
socket.send({ type: MessageType.attack, targetEntityId });1084
}1131
}1085
1132
@@ -1136,7 +1183,7 @@ function handleLeftClick (mouseX, mouseY)1136
if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)1183
if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)1137
{1184
{1138
clearPendingPickup();1185
clearPendingPickup();1139
const clickAction = defaultLeftClickEntityAction(entity);1186
const clickAction = defaultLeftClickEntityAction(entity, leftClickContextFor(entity));1140
if (clickAction === "inspect")1187
if (clickAction === "inspect")1141
{1188
{1142
clearPendingInteract();1189
clearPendingInteract();@@ -1156,6 +1203,11 @@ function handleLeftClick (mouseX, mouseY)1156
requestTalk(entity.entityId, purpose);1203
requestTalk(entity.entityId, purpose);1157
return;1204
return;1158
}1205
}1206
if (clickAction === "approach")1207
{1208
requestApproachEntity(entity.entityId);1209
return;1210
}1159
// Clicked an entity we don't act on — do not fall through to walk (that cancels combat).1211
// Clicked an entity we don't act on — do not fall through to walk (that cancels combat).1160
return;1212
return;1161
}1213
}@@ -1281,16 +1333,19 @@ function handleRightClick (event, mouseX, mouseY)1281
});1333
});1282
if (entity.untargetable)1334
if (entity.untargetable)1283
{ continue; }1335
{ continue; }1284
const aggressive = !!(entity.aggressive || entity.hostile);1336
const clickCtx = leftClickContextFor(entity);1337
const attackFirst = preferAttackInContextMenu(entity, clickCtx);1285
const pushAttack = () =>1338
const pushAttack = () =>1286
{1339
{1340
if (!isAttackableEntity(entity))1341
{ return; }1287
items.push({1342
items.push({1288
label: `Attack ${targetName}`,1343
label: `Attack ${targetName}`,1289
onSelect: () => requestAttack(entity.entityId)1344
onSelect: () => requestAttack(entity.entityId)1290
});1345
});1291
};1346
};1292
// Aggressive hostiles: Attack first (matches LMB default); Talk/Shop still available.1347
// Prefer Attack first when that matches LMB (aggressive / combat / hostile guards).1293
if (aggressive && isAttackableEntity(entity))1348
if (attackFirst)1294
{ pushAttack(); }1349
{ pushAttack(); }1295
if (entity.hasShop)1350
if (entity.hasShop)1296
{1351
{@@ -1332,7 +1387,7 @@ function handleRightClick (event, mouseX, mouseY)1332
}1387
}1333
});1388
});1334
}1389
}1335
if (!(aggressive && isAttackableEntity(entity)))1390
if (!attackFirst)1336
{ pushAttack(); }1391
{ pushAttack(); }1337
}1392
}1338
1393
@@ -2135,6 +2190,7 @@ async function main ()2135
case MessageType.entityDespawn:2190
case MessageType.entityDespawn:2136
if (message.entityId === autorunFocusEntityId)2191
if (message.entityId === autorunFocusEntityId)2137
{ clearAutorunFocus(); }2192
{ clearAutorunFocus(); }2193
clearCombatWithLocal(message.entityId);2138
view?.despawnEntity(message.entityId, {2194
view?.despawnEntity(message.entityId, {2139
combatDeath: message.cause === "death"2195
combatDeath: message.cause === "death"2140
});2196
});@@ -2265,9 +2321,11 @@ async function main ()2265
social?.handleFriends(message);2321
social?.handleFriends(message);2266
break;2322
break;2267
case MessageType.combatSwing:2323
case MessageType.combatSwing:2324
noteCombatWithLocal(message.attackerId, message.targetId);2268
view?.spawnCombatSwing(message);2325
view?.spawnCombatSwing(message);2269
break;2326
break;2270
case MessageType.combatProjectile:2327
case MessageType.combatProjectile:2328
noteCombatWithLocal(message.attackerId, message.targetId);2271
view?.spawnPhysicalProjectileFx({2329
view?.spawnPhysicalProjectileFx({2272
...message,2330
...message,2273
sprite: "fx:projectile:arrow"2331
sprite: "fx:projectile:arrow"@@ -2334,6 +2392,7 @@ async function main ()2334
break;2392
break;2335
case MessageType.combatHit:2393
case MessageType.combatHit:2336
{2394
{2395
noteCombatWithLocal(message.attackerId, message.targetId);2337
const applyHit = () =>2396
const applyHit = () =>2338
{2397
{2339
view?.updateEntityHealth(message.targetId, message.health, message.maxHealth);2398
view?.updateEntityHealth(message.targetId, message.health, message.maxHealth);@@ -2353,11 +2412,13 @@ async function main ()2353
break;2412
break;2354
}2413
}2355
case MessageType.combatMiss:2414
case MessageType.combatMiss:2415
noteCombatWithLocal(message.attackerId, message.targetId);2356
view?.spawnCombatDodge?.(message);2416
view?.spawnCombatDodge?.(message);2357
view?.markProjectileMiss?.(message);2417
view?.markProjectileMiss?.(message);2358
break;2418
break;2359
case MessageType.combatBlock:2419
case MessageType.combatBlock:2360
{2420
{2421
noteCombatWithLocal(message.attackerId, message.targetId);2361
const applyBlock = () =>2422
const applyBlock = () =>2362
{2423
{2363
view?.flashBlock?.(message.targetId);2424
view?.flashBlock?.(message.targetId);@@ -2512,6 +2573,7 @@ function returnToMenu (reason = "")2512
followTargetId = null;2573
followTargetId = null;2513
moveDest = null;2574
moveDest = null;2514
clearAutorunFocus();2575
clearAutorunFocus();2576
clearCombatWithLocal();2515
autorunWaitingStamina = false;2577
autorunWaitingStamina = false;2516
clearAllPendingActions();2578
clearAllPendingActions();2517
clearUiFocus();2579
clearUiFocus();@@ -1,6 +1,6 @@
1
1
import test from "node:test";
2
2
import assert from "node:assert/strict";
3
import { defaultLeftClickEntityAction } from "../../shared/combat/entityClick.js";
3
import { defaultLeftClickEntityAction, preferAttackInContextMenu } from "../../shared/combat/entityClick.js";
4
4
import { mobComponents } from "../src/simulation/spawners.js";
5
5
import { MobId } from "../../shared/definitions/mobs.js";
6
6
import { GemItemId } from "../../shared/definitions/gemItems.js";
@@ -9,6 +9,7 @@ import { Component } from "../src/ecs/components.js";
9
9
import { publicEntityView, replicationSpawn } from "../src/ecs/serialize.js";
10
10
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
11
11
import { chebyshev } from "../src/combat/service.js";
12
import { SKULL_BLACK_MAX_DEFAULT, SKULL_KILL_BONUS } from "../../shared/combat/skull.js";
12
13
13
14
test("default LMB prefers attack on aggressive talkable humanoids", () =>
14
15
{
@@ -40,6 +41,57 @@ test("default LMB prefers talk on peaceful talkable humanoids", () =>
40
41
hasShop: true,
41
42
talkable: true
42
43
}), "talk");
44
assert.equal(preferAttackInContextMenu({
45
mobTypeId: MobId.humanoid,
46
canFight: true,
47
talkable: true,
48
aggressive: false
49
}), false);
50
});
51
52
test("default LMB approaches peaceful humanoids without talk or shop", () =>
53
{
54
assert.equal(defaultLeftClickEntityAction({
55
mobTypeId: MobId.humanoid,
56
canFight: true,
57
talkable: false,
58
aggressive: false
59
}), "approach");
60
assert.equal(defaultLeftClickEntityAction({
61
mobTypeId: MobId.townGuard,
62
canFight: true,
63
talkable: false,
64
aggressive: false
65
}), "approach");
66
});
67
68
test("default LMB attacks town guards only when local skull attracts them", () =>
69
{
70
const guard = {
71
mobTypeId: MobId.townGuard,
72
canFight: true,
73
talkable: false,
74
aggressive: false
75
};
76
assert.equal(defaultLeftClickEntityAction(guard, {
77
localSkullValue: SKULL_BLACK_MAX_DEFAULT
78
}), "approach");
79
assert.equal(defaultLeftClickEntityAction(guard, {
80
localSkullValue: SKULL_BLACK_MAX_DEFAULT + 1
81
}), "attack");
82
assert.equal(preferAttackInContextMenu(guard, {
83
localSkullValue: SKULL_KILL_BONUS + 1
84
}), true);
85
});
86
87
test("default LMB attacks when already in combat with the local player", () =>
88
{
89
assert.equal(defaultLeftClickEntityAction({
90
mobTypeId: MobId.humanoid,
91
canFight: true,
92
talkable: true,
93
aggressive: false
94
}, { inCombatWithLocal: true }), "attack");
43
95
});
44
96
45
97
test("default LMB attacks non-talkable hostiles and inspects players", () =>
@@ -49,6 +101,11 @@ test("default LMB attacks non-talkable hostiles and inspects players", () =>
49
101
canFight: true,
50
102
aggressive: true
51
103
}), "attack");
104
assert.equal(defaultLeftClickEntityAction({
105
mobTypeId: MobId.deer,
106
canFight: true,
107
aggressive: false
108
}), "attack");
52
109
assert.equal(defaultLeftClickEntityAction({
53
110
characterId: "c1",
54
111
canFight: true
@@ -1,6 +1,6 @@1
import test from "node:test";1
import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { defaultLeftClickEntityAction } from "../../shared/combat/entityClick.js";3
import { defaultLeftClickEntityAction, preferAttackInContextMenu } from "../../shared/combat/entityClick.js";4
import { mobComponents } from "../src/simulation/spawners.js";4
import { mobComponents } from "../src/simulation/spawners.js";5
import { MobId } from "../../shared/definitions/mobs.js";5
import { MobId } from "../../shared/definitions/mobs.js";6
import { GemItemId } from "../../shared/definitions/gemItems.js";6
import { GemItemId } from "../../shared/definitions/gemItems.js";@@ -9,6 +9,7 @@ import { Component } from "../src/ecs/components.js";9
import { publicEntityView, replicationSpawn } from "../src/ecs/serialize.js";9
import { publicEntityView, replicationSpawn } from "../src/ecs/serialize.js";10
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";10
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";11
import { chebyshev } from "../src/combat/service.js";11
import { chebyshev } from "../src/combat/service.js";12
import { SKULL_BLACK_MAX_DEFAULT, SKULL_KILL_BONUS } from "../../shared/combat/skull.js";12
13
13
test("default LMB prefers attack on aggressive talkable humanoids", () =>14
test("default LMB prefers attack on aggressive talkable humanoids", () =>14
{15
{@@ -40,6 +41,57 @@ test("default LMB prefers talk on peaceful talkable humanoids", () =>40
hasShop: true,41
hasShop: true,41
talkable: true42
talkable: true42
}), "talk");43
}), "talk");44
assert.equal(preferAttackInContextMenu({45
mobTypeId: MobId.humanoid,46
canFight: true,47
talkable: true,48
aggressive: false49
}), false);50
});51
52
test("default LMB approaches peaceful humanoids without talk or shop", () =>53
{54
assert.equal(defaultLeftClickEntityAction({55
mobTypeId: MobId.humanoid,56
canFight: true,57
talkable: false,58
aggressive: false59
}), "approach");60
assert.equal(defaultLeftClickEntityAction({61
mobTypeId: MobId.townGuard,62
canFight: true,63
talkable: false,64
aggressive: false65
}), "approach");66
});67
68
test("default LMB attacks town guards only when local skull attracts them", () =>69
{70
const guard = {71
mobTypeId: MobId.townGuard,72
canFight: true,73
talkable: false,74
aggressive: false75
};76
assert.equal(defaultLeftClickEntityAction(guard, {77
localSkullValue: SKULL_BLACK_MAX_DEFAULT78
}), "approach");79
assert.equal(defaultLeftClickEntityAction(guard, {80
localSkullValue: SKULL_BLACK_MAX_DEFAULT + 181
}), "attack");82
assert.equal(preferAttackInContextMenu(guard, {83
localSkullValue: SKULL_KILL_BONUS + 184
}), true);85
});86
87
test("default LMB attacks when already in combat with the local player", () =>88
{89
assert.equal(defaultLeftClickEntityAction({90
mobTypeId: MobId.humanoid,91
canFight: true,92
talkable: true,93
aggressive: false94
}, { inCombatWithLocal: true }), "attack");43
});95
});44
96
45
test("default LMB attacks non-talkable hostiles and inspects players", () =>97
test("default LMB attacks non-talkable hostiles and inspects players", () =>@@ -49,6 +101,11 @@ test("default LMB attacks non-talkable hostiles and inspects players", () =>49
canFight: true,101
canFight: true,50
aggressive: true102
aggressive: true51
}), "attack");103
}), "attack");104
assert.equal(defaultLeftClickEntityAction({105
mobTypeId: MobId.deer,106
canFight: true,107
aggressive: false108
}), "attack");52
assert.equal(defaultLeftClickEntityAction({109
assert.equal(defaultLeftClickEntityAction({53
characterId: "c1",110
characterId: "c1",54
canFight: true111
canFight: true