BlueGit

timfa questward

Files

questward / server / tests / entityClick.test.js

entityClick.test.js

entityClick.test.js 5.8 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { defaultLeftClickEntityAction, preferAttackInContextMenu } 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";
12import { SKULL_BLACK_MAX_DEFAULT, SKULL_KILL_BONUS } from "../../shared/combat/skull.js";
13
14test("default LMB prefers attack on aggressive talkable humanoids", () =>
15{
16 assert.equal(defaultLeftClickEntityAction({
17 mobTypeId: MobId.humanoid,
18 canFight: true,
19 talkable: true,
20 aggressive: true
21 }), "attack");
22 assert.equal(defaultLeftClickEntityAction({
23 mobTypeId: MobId.humanoid,
24 canFight: true,
25 talkable: true,
26 hostile: true
27 }), "attack");
28});
29
30test("default LMB prefers talk on peaceful talkable humanoids", () =>
31{
32 assert.equal(defaultLeftClickEntityAction({
33 mobTypeId: MobId.humanoid,
34 canFight: true,
35 talkable: true,
36 aggressive: false
37 }), "talk");
38 assert.equal(defaultLeftClickEntityAction({
39 mobTypeId: MobId.humanoid,
40 canFight: true,
41 hasShop: true,
42 talkable: true
43 }), "talk");
44 assert.equal(preferAttackInContextMenu({
45 mobTypeId: MobId.humanoid,
46 canFight: true,
47 talkable: true,
48 aggressive: false
49 }), false);
50});
51
52test("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
68test("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
87test("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");
95});
96
97test("default LMB attacks non-talkable hostiles and inspects players", () =>
98{
99 assert.equal(defaultLeftClickEntityAction({
100 mobTypeId: MobId.rat,
101 canFight: true,
102 aggressive: true
103 }), "attack");
104 assert.equal(defaultLeftClickEntityAction({
105 mobTypeId: MobId.deer,
106 canFight: true,
107 aggressive: false
108 }), "attack");
109 assert.equal(defaultLeftClickEntityAction({
110 characterId: "c1",
111 canFight: true
112 }), "inspect");
113 assert.equal(defaultLeftClickEntityAction({ untargetable: true, canFight: true }), null);
114});
115
116test("aggressive talkable humanoid snapshots expose aggressive for clients", () =>
117{
118 const harness = makeSimulation({ workerCount: 1 });
119 try
120 {
121 const npcId = harness.simulation.spawnEntity(mobComponents({
122 tileX: 12,
123 tileY: 8,
124 mobTypeId: MobId.humanoid,
125 spawnerId: "bandit-1",
126 aggressive: true,
127 displayName: "Bandit",
128 sex: "male"
129 }));
130 const worker = harness.simulation.workers[0];
131 const view = publicEntityView(worker.world.ecs, npcId);
132 assert.equal(view.talkable, true);
133 assert.equal(view.canFight, true);
134 assert.equal(view.aggressive, true);
135 assert.equal(view.hostile, true);
136 const spawn = replicationSpawn(view);
137 assert.equal(spawn.aggressive, true);
138 assert.equal(spawn.hostile, true);
139 assert.equal(defaultLeftClickEntityAction(spawn), "attack");
140
141 const record = harness.simulation.entityRecord(npcId);
142 assert.equal(record.aggressive, true);
143 }
144 finally
145 { harness.close(); }
146});
147
148test("staff click-attack on aggressive humanoid uses spell range not talk-adjacent", () =>
149{
150 const harness = makeSimulation({ workerCount: 1 });
151 try
152 {
153 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
154 const playerId = harness.simulation.spawnCharacter(1, character);
155 const npcId = harness.simulation.spawnEntity(mobComponents({
156 tileX: 28,
157 tileY: 8,
158 mobTypeId: MobId.humanoid,
159 spawnerId: "bandit-staff",
160 aggressive: true,
161 displayName: "Bandit",
162 sex: "male"
163 }));
164 const worker = harness.simulation.workers[0];
165 const handle = worker.world.ecs.handleOf(playerId);
166 const equipment = worker.world.ecs.get(handle, Component.equipment);
167 equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 };
168 const spells = worker.world.ecs.get(handle, Component.spells);
169 spells.known.push(SpellId.manaBullet);
170 spells.favorites = [SpellId.manaBullet];
171 spells.autoCast = false;
172
173 harness.simulation.enqueue(1, {
174 type: "attack",
175 characterId: character.character_id,
176 targetEntityId: npcId
177 });
178 harness.simulation.step();
179
180 const casting = worker.world.ecs.get(handle, Component.spellcasting);
181 assert.equal(casting.repeatSpellId, SpellId.manaBullet);
182 assert.equal(casting.repeatTargetEntityId, npcId);
183
184 const path = worker.world.ecs.get(handle, Component.path);
185 const tiles = path?.tiles ?? [];
186 assert.ok(tiles.length > 0, "should approach into spell range");
187 const last = tiles[tiles.length - 1];
188 const standDist = chebyshev(last.x, last.y, 28, 8);
189 assert.ok(
190 standDist > 1,
191 `spell approach should stop outside talk/melee adjacency, got dist=${standDist}`
192 );
193 }
194 finally
195 { harness.close(); }
196});