import test from "node:test"; import assert from "node:assert/strict"; import { defaultLeftClickEntityAction, preferAttackInContextMenu } from "../../shared/combat/entityClick.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { GemItemId } from "../../shared/definitions/gemItems.js"; import { SpellId } from "../../shared/definitions/spells.js"; import { Component } from "../src/ecs/components.js"; import { publicEntityView, replicationSpawn } from "../src/ecs/serialize.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { chebyshev } from "../src/combat/service.js"; import { SKULL_BLACK_MAX_DEFAULT, SKULL_KILL_BONUS } from "../../shared/combat/skull.js"; test("default LMB prefers attack on aggressive talkable humanoids", () => { assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, talkable: true, aggressive: true }), "attack"); assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, talkable: true, hostile: true }), "attack"); }); test("default LMB prefers talk when dialogue or shop is available", () => { assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, talkable: true, hasDialogue: true, aggressive: false }), "talk"); assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, hasShop: true, talkable: true }), "talk"); assert.equal(preferAttackInContextMenu({ mobTypeId: MobId.humanoid, canFight: true, talkable: true, hasDialogue: true, aggressive: false }), false); }); test("default LMB approaches talkable humanoids without dialogue or shop", () => { assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, talkable: true, hasDialogue: false, aggressive: false }), "approach"); }); test("default LMB approaches peaceful humanoids without talk or shop", () => { assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, talkable: false, aggressive: false }), "approach"); assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.townGuard, canFight: true, talkable: false, aggressive: false }), "approach"); }); test("default LMB attacks town guards only when local skull attracts them", () => { const guard = { mobTypeId: MobId.townGuard, canFight: true, talkable: false, aggressive: false }; assert.equal(defaultLeftClickEntityAction(guard, { localSkullValue: SKULL_BLACK_MAX_DEFAULT }), "approach"); assert.equal(defaultLeftClickEntityAction(guard, { localSkullValue: SKULL_BLACK_MAX_DEFAULT + 1 }), "attack"); assert.equal(preferAttackInContextMenu(guard, { localSkullValue: SKULL_KILL_BONUS + 1 }), true); }); test("default LMB attacks when already in combat with the local player", () => { assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.humanoid, canFight: true, talkable: true, aggressive: false }, { inCombatWithLocal: true }), "attack"); }); test("default LMB attacks non-talkable hostiles and inspects players", () => { assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.rat, canFight: true, aggressive: true }), "attack"); assert.equal(defaultLeftClickEntityAction({ mobTypeId: MobId.deer, canFight: true, aggressive: false }), "attack"); assert.equal(defaultLeftClickEntityAction({ characterId: "c1", canFight: true }), "inspect"); assert.equal(defaultLeftClickEntityAction({ untargetable: true, canFight: true }), null); }); test("aggressive talkable humanoid snapshots expose aggressive for clients", () => { const harness = makeSimulation({ workerCount: 1 }); try { const npcId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.humanoid, spawnerId: "bandit-1", aggressive: true, displayName: "Bandit", sex: "male" })); const worker = harness.simulation.workers[0]; const view = publicEntityView(worker.world.ecs, npcId); assert.equal(view.talkable, true); assert.equal(view.canFight, true); assert.equal(view.aggressive, true); assert.equal(view.hostile, true); const spawn = replicationSpawn(view); assert.equal(spawn.aggressive, true); assert.equal(spawn.hostile, true); assert.equal(defaultLeftClickEntityAction(spawn), "attack"); const record = harness.simulation.entityRecord(npcId); assert.equal(record.aggressive, true); } finally { harness.close(); } }); test("staff click-attack on aggressive humanoid uses spell range not talk-adjacent", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const npcId = harness.simulation.spawnEntity(mobComponents({ tileX: 28, tileY: 8, mobTypeId: MobId.humanoid, spawnerId: "bandit-staff", aggressive: true, displayName: "Bandit", sex: "male" })); const worker = harness.simulation.workers[0]; const handle = worker.world.ecs.handleOf(playerId); const equipment = worker.world.ecs.get(handle, Component.equipment); equipment.slots.weapon = { itemId: GemItemId.pineStaff, quantity: 1 }; const spells = worker.world.ecs.get(handle, Component.spells); spells.known.push(SpellId.manaBullet); spells.favorites = [SpellId.manaBullet]; spells.autoCast = false; harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: npcId }); harness.simulation.step(); const casting = worker.world.ecs.get(handle, Component.spellcasting); assert.equal(casting.repeatSpellId, SpellId.manaBullet); assert.equal(casting.repeatTargetEntityId, npcId); const path = worker.world.ecs.get(handle, Component.path); const tiles = path?.tiles ?? []; assert.ok(tiles.length > 0, "should approach into spell range"); const last = tiles[tiles.length - 1]; const standDist = chebyshev(last.x, last.y, 28, 8); assert.ok( standDist > 1, `spell approach should stop outside talk/melee adjacency, got dist=${standDist}` ); } finally { harness.close(); } });