BlueGit

timfa questward

Files

questward / server / tests / entityClick.test.js

entityClick.test.js

entityClick.test.js 4.3 KiB Download Raw
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});