| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { defaultLeftClickEntityAction, preferAttackInContextMenu } 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 | import { SKULL_BLACK_MAX_DEFAULT, SKULL_KILL_BONUS } from "../../shared/combat/skull.js"; |
| 13 | |
| 14 | test("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 | |
| 30 | test("default LMB prefers talk when dialogue or shop is available", () => |
| 31 | { |
| 32 | assert.equal(defaultLeftClickEntityAction({ |
| 33 | mobTypeId: MobId.humanoid, |
| 34 | canFight: true, |
| 35 | talkable: true, |
| 36 | hasDialogue: true, |
| 37 | aggressive: false |
| 38 | }), "talk"); |
| 39 | assert.equal(defaultLeftClickEntityAction({ |
| 40 | mobTypeId: MobId.humanoid, |
| 41 | canFight: true, |
| 42 | hasShop: true, |
| 43 | talkable: true |
| 44 | }), "talk"); |
| 45 | assert.equal(preferAttackInContextMenu({ |
| 46 | mobTypeId: MobId.humanoid, |
| 47 | canFight: true, |
| 48 | talkable: true, |
| 49 | hasDialogue: true, |
| 50 | aggressive: false |
| 51 | }), false); |
| 52 | }); |
| 53 | |
| 54 | test("default LMB approaches talkable humanoids without dialogue or shop", () => |
| 55 | { |
| 56 | assert.equal(defaultLeftClickEntityAction({ |
| 57 | mobTypeId: MobId.humanoid, |
| 58 | canFight: true, |
| 59 | talkable: true, |
| 60 | hasDialogue: false, |
| 61 | aggressive: false |
| 62 | }), "approach"); |
| 63 | }); |
| 64 | |
| 65 | test("default LMB approaches peaceful humanoids without talk or shop", () => |
| 66 | { |
| 67 | assert.equal(defaultLeftClickEntityAction({ |
| 68 | mobTypeId: MobId.humanoid, |
| 69 | canFight: true, |
| 70 | talkable: false, |
| 71 | aggressive: false |
| 72 | }), "approach"); |
| 73 | assert.equal(defaultLeftClickEntityAction({ |
| 74 | mobTypeId: MobId.townGuard, |
| 75 | canFight: true, |
| 76 | talkable: false, |
| 77 | aggressive: false |
| 78 | }), "approach"); |
| 79 | }); |
| 80 | |
| 81 | test("default LMB attacks town guards only when local skull attracts them", () => |
| 82 | { |
| 83 | const guard = { |
| 84 | mobTypeId: MobId.townGuard, |
| 85 | canFight: true, |
| 86 | talkable: false, |
| 87 | aggressive: false |
| 88 | }; |
| 89 | assert.equal(defaultLeftClickEntityAction(guard, { |
| 90 | localSkullValue: SKULL_BLACK_MAX_DEFAULT |
| 91 | }), "approach"); |
| 92 | assert.equal(defaultLeftClickEntityAction(guard, { |
| 93 | localSkullValue: SKULL_BLACK_MAX_DEFAULT + 1 |
| 94 | }), "attack"); |
| 95 | assert.equal(preferAttackInContextMenu(guard, { |
| 96 | localSkullValue: SKULL_KILL_BONUS + 1 |
| 97 | }), true); |
| 98 | }); |
| 99 | |
| 100 | test("default LMB attacks when already in combat with the local player", () => |
| 101 | { |
| 102 | assert.equal(defaultLeftClickEntityAction({ |
| 103 | mobTypeId: MobId.humanoid, |
| 104 | canFight: true, |
| 105 | talkable: true, |
| 106 | aggressive: false |
| 107 | }, { inCombatWithLocal: true }), "attack"); |
| 108 | }); |
| 109 | |
| 110 | test("default LMB attacks non-talkable hostiles and inspects players", () => |
| 111 | { |
| 112 | assert.equal(defaultLeftClickEntityAction({ |
| 113 | mobTypeId: MobId.rat, |
| 114 | canFight: true, |
| 115 | aggressive: true |
| 116 | }), "attack"); |
| 117 | assert.equal(defaultLeftClickEntityAction({ |
| 118 | mobTypeId: MobId.deer, |
| 119 | canFight: true, |
| 120 | aggressive: false |
| 121 | }), "attack"); |
| 122 | assert.equal(defaultLeftClickEntityAction({ |
| 123 | characterId: "c1", |
| 124 | canFight: true |
| 125 | }), "inspect"); |
| 126 | assert.equal(defaultLeftClickEntityAction({ untargetable: true, canFight: true }), null); |
| 127 | }); |
| 128 | |
| 129 | test("aggressive talkable humanoid snapshots expose aggressive for clients", () => |
| 130 | { |
| 131 | const harness = makeSimulation({ workerCount: 1 }); |
| 132 | try |
| 133 | { |
| 134 | const npcId = harness.simulation.spawnEntity(mobComponents({ |
| 135 | tileX: 12, |
| 136 | tileY: 8, |
| 137 | mobTypeId: MobId.humanoid, |
| 138 | spawnerId: "bandit-1", |
| 139 | aggressive: true, |
| 140 | displayName: "Bandit", |
| 141 | sex: "male" |
| 142 | })); |
| 143 | const worker = harness.simulation.workers[0]; |
| 144 | const view = publicEntityView(worker.world.ecs, npcId); |
| 145 | assert.equal(view.talkable, true); |
| 146 | assert.equal(view.canFight, true); |
| 147 | assert.equal(view.aggressive, true); |
| 148 | assert.equal(view.hostile, true); |
| 149 | const spawn = replicationSpawn(view); |
| 150 | assert.equal(spawn.aggressive, true); |
| 151 | assert.equal(spawn.hostile, true); |
| 152 | assert.equal(defaultLeftClickEntityAction(spawn), "attack"); |
| 153 | |
| 154 | const record = harness.simulation.entityRecord(npcId); |
| 155 | assert.equal(record.aggressive, true); |
| 156 | } |
| 157 | finally |
| 158 | { harness.close(); } |
| 159 | }); |
| 160 | |
| 161 | test("staff click-attack on aggressive humanoid uses spell range not talk-adjacent", () => |
| 162 | { |
| 163 | const harness = makeSimulation({ workerCount: 1 }); |
| 164 | try |
| 165 | { |
| 166 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 167 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 168 | const npcId = harness.simulation.spawnEntity(mobComponents({ |
| 169 | tileX: 28, |
| 170 | tileY: 8, |
| 171 | mobTypeId: MobId.humanoid, |
| 172 | spawnerId: "bandit-staff", |
| 173 | aggressive: true, |
| 174 | displayName: "Bandit", |
| 175 | sex: "male" |
| 176 | })); |
| 177 | const worker = harness.simulation.workers[0]; |
| 178 | const handle = worker.world.ecs.handleOf(playerId); |
| 179 | const equipment = worker.world.ecs.get(handle, Component.equipment); |
| 180 | equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; |
| 181 | const spells = worker.world.ecs.get(handle, Component.spells); |
| 182 | spells.known.push(SpellId.manaBullet); |
| 183 | spells.favorites = [SpellId.manaBullet]; |
| 184 | spells.autoCast = false; |
| 185 | |
| 186 | harness.simulation.enqueue(1, { |
| 187 | type: "attack", |
| 188 | characterId: character.character_id, |
| 189 | targetEntityId: npcId |
| 190 | }); |
| 191 | harness.simulation.step(); |
| 192 | |
| 193 | const casting = worker.world.ecs.get(handle, Component.spellcasting); |
| 194 | assert.equal(casting.repeatSpellId, SpellId.manaBullet); |
| 195 | assert.equal(casting.repeatTargetEntityId, npcId); |
| 196 | |
| 197 | const path = worker.world.ecs.get(handle, Component.path); |
| 198 | const tiles = path?.tiles ?? []; |
| 199 | assert.ok(tiles.length > 0, "should approach into spell range"); |
| 200 | const last = tiles[tiles.length - 1]; |
| 201 | const standDist = chebyshev(last.x, last.y, 28, 8); |
| 202 | assert.ok( |
| 203 | standDist > 1, |
| 204 | `spell approach should stop outside talk/melee adjacency, got dist=${standDist}` |
| 205 | ); |
| 206 | } |
| 207 | finally |
| 208 | { harness.close(); } |
| 209 | }); |