Make town guards auto-aggro players once skull tint lerps toward red.
@@ -18,6 +18,7 @@ import {
18
18
playerOutlevelsMobAggression
19
19
} from "../../../shared/combat/combatLevel.js";
20
20
import { chunkPeaceAggressionImmunity } from "../../../shared/navigation/chunkPresence.js";
21
import { skullAttractsTownGuards } from "../../../shared/combat/skull.js";
21
22
import { OVERWORLD_ID } from "../../../shared/world/dimensions.js";
22
23
import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions/spells.js";
23
24
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";
@@ -692,7 +693,8 @@ function missProjectileTile (targetTileX, targetTileY, rng = Math.random)
692
693
693
694
/**
694
695
* Aggressive mobs auto-engage the nearest player or town guard within aggro range
695
* when they have line of sight. Town guards auto-engage aggressive creatures.
696
* when they have line of sight. Town guards auto-engage aggressive creatures and
697
* players whose skull tint has started lerping toward red.
696
698
* Does nothing while already engaged.
697
699
*/
698
700
export function stepAggression (world, now = Date.now())
@@ -722,7 +724,7 @@ export function stepAggression (world, now = Date.now())
722
724
723
725
const range = mobAggroRangeTiles(def);
724
726
const bestId = townGuard
725
? pickAggressionTargetAggressiveMob(world, entity, range)
727
? pickAggressionTargetForTownGuard(world, entity, range)
726
728
: pickAggressionTargetPlayerOrGuard(world, entity, range, now);
727
729
if (bestId == null)
728
730
{ continue; }
@@ -817,8 +819,11 @@ function playerIgnoresAggressiveMob (world, mobEntity, playerHandle, now)
817
819
);
818
820
}
819
821
820
/** Nearest aggressive hostile creature in range with LOS (for town guards). */
821
function pickAggressionTargetAggressiveMob (world, entity, range)
822
/**
823
* Nearest aggressive hostile creature or red-lerping skulled player in range
824
* with LOS (for town guards).
825
*/
826
function pickAggressionTargetForTownGuard (world, entity, range)
822
827
{
823
828
let bestId = null;
824
829
let bestDist = Infinity;
@@ -837,6 +842,17 @@ function pickAggressionTargetAggressiveMob (world, entity, range)
837
842
bestId = other.entityId;
838
843
}
839
844
}
845
for (const player of world.ecs.query(Component.character, Component.position))
846
{
847
if (!skullAttractsTownGuards(player.character?.skullValue))
848
{ continue; }
849
const dist = considerAggressionCandidate(world, entity, player.handle, player.entityId, player.position, range);
850
if (dist != null && dist < bestDist)
851
{
852
bestDist = dist;
853
bestId = player.entityId;
854
}
855
}
840
856
return bestId;
841
857
}
842
858
@@ -18,6 +18,7 @@ import {18
playerOutlevelsMobAggression18
playerOutlevelsMobAggression19
} from "../../../shared/combat/combatLevel.js";19
} from "../../../shared/combat/combatLevel.js";20
import { chunkPeaceAggressionImmunity } from "../../../shared/navigation/chunkPresence.js";20
import { chunkPeaceAggressionImmunity } from "../../../shared/navigation/chunkPresence.js";21
import { skullAttractsTownGuards } from "../../../shared/combat/skull.js";21
import { OVERWORLD_ID } from "../../../shared/world/dimensions.js";22
import { OVERWORLD_ID } from "../../../shared/world/dimensions.js";22
import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions/spells.js";23
import { spellDefinition, spellMinMagicLevel } from "../../../shared/definitions/spells.js";23
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";24
import { resolvePhysicalAttack } from "../../../shared/combat/rolls.js";@@ -692,7 +693,8 @@ function missProjectileTile (targetTileX, targetTileY, rng = Math.random)692
693
693
/**694
/**694
* Aggressive mobs auto-engage the nearest player or town guard within aggro range695
* Aggressive mobs auto-engage the nearest player or town guard within aggro range695
* when they have line of sight. Town guards auto-engage aggressive creatures.696
* when they have line of sight. Town guards auto-engage aggressive creatures and697
* players whose skull tint has started lerping toward red.696
* Does nothing while already engaged.698
* Does nothing while already engaged.697
*/699
*/698
export function stepAggression (world, now = Date.now())700
export function stepAggression (world, now = Date.now())@@ -722,7 +724,7 @@ export function stepAggression (world, now = Date.now())722
724
723
const range = mobAggroRangeTiles(def);725
const range = mobAggroRangeTiles(def);724
const bestId = townGuard726
const bestId = townGuard725
? pickAggressionTargetAggressiveMob(world, entity, range)727
? pickAggressionTargetForTownGuard(world, entity, range)726
: pickAggressionTargetPlayerOrGuard(world, entity, range, now);728
: pickAggressionTargetPlayerOrGuard(world, entity, range, now);727
if (bestId == null)729
if (bestId == null)728
{ continue; }730
{ continue; }@@ -817,8 +819,11 @@ function playerIgnoresAggressiveMob (world, mobEntity, playerHandle, now)817
);819
);818
}820
}819
821
820
/** Nearest aggressive hostile creature in range with LOS (for town guards). */822
/**821
function pickAggressionTargetAggressiveMob (world, entity, range)823
* Nearest aggressive hostile creature or red-lerping skulled player in range824
* with LOS (for town guards).825
*/826
function pickAggressionTargetForTownGuard (world, entity, range)822
{827
{823
let bestId = null;828
let bestId = null;824
let bestDist = Infinity;829
let bestDist = Infinity;@@ -837,6 +842,17 @@ function pickAggressionTargetAggressiveMob (world, entity, range)837
bestId = other.entityId;842
bestId = other.entityId;838
}843
}839
}844
}845
for (const player of world.ecs.query(Component.character, Component.position))846
{847
if (!skullAttractsTownGuards(player.character?.skullValue))848
{ continue; }849
const dist = considerAggressionCandidate(world, entity, player.handle, player.entityId, player.position, range);850
if (dist != null && dist < bestDist)851
{852
bestDist = dist;853
bestId = player.entityId;854
}855
}840
return bestId;856
return bestId;841
}857
}842
858
@@ -42,6 +42,7 @@ export const WorkerMessage = Object.freeze({
42
42
stationExchangeClerk: "stationExchangeClerk",
43
43
releaseExchangeClerk: "releaseExchangeClerk",
44
44
applyAppearance: "applyAppearance",
45
applySkullValue: "applySkullValue",
45
46
engageTarget: "engageTarget",
46
47
listTownGuardsNear: "listTownGuardsNear",
47
48
pinDialogue: "pinDialogue",
@@ -42,6 +42,7 @@ export const WorkerMessage = Object.freeze({42
stationExchangeClerk: "stationExchangeClerk",42
stationExchangeClerk: "stationExchangeClerk",43
releaseExchangeClerk: "releaseExchangeClerk",43
releaseExchangeClerk: "releaseExchangeClerk",44
applyAppearance: "applyAppearance",44
applyAppearance: "applyAppearance",45
applySkullValue: "applySkullValue",45
engageTarget: "engageTarget",46
engageTarget: "engageTarget",46
listTownGuardsNear: "listTownGuardsNear",47
listTownGuardsNear: "listTownGuardsNear",47
pinDialogue: "pinDialogue",48
pinDialogue: "pinDialogue",@@ -461,7 +461,8 @@ export class Simulation
461
461
},
462
462
character: {
463
463
characterId: character.character_id ?? row.character_id,
464
accountId: character.account_id ?? row.account_id
464
accountId: character.account_id ?? row.account_id,
465
skullValue: this.characterSkullValue(character.character_id ?? row.character_id)
465
466
},
466
467
networkIdentity: { sessionId },
467
468
chunkLoader: { ...DEFAULT_CHUNK_LOADER },
@@ -536,6 +537,11 @@ export class Simulation
536
537
if (!force && (record.skullValue | 0) === value)
537
538
{ return null; }
538
539
record.skullValue = value;
540
this.workers[record.workerId]?.call({
541
type: WorkerMessage.applySkullValue,
542
entityId,
543
skullValue: value
544
});
539
545
const event = {
540
546
type: "entity-skull",
541
547
entityId,
@@ -553,7 +559,13 @@ export class Simulation
553
559
const record = this.directory.get(entityId);
554
560
if (!record)
555
561
{ return; }
556
record.skullValue = this.characterSkullValue(characterId);
562
const value = this.characterSkullValue(characterId);
563
record.skullValue = value;
564
this.workers[record.workerId]?.call({
565
type: WorkerMessage.applySkullValue,
566
entityId,
567
skullValue: value
568
});
557
569
}
558
570
559
571
#stepSkullDecay ()
@@ -461,7 +461,8 @@ export class Simulation461
},461
},462
character: {462
character: {463
characterId: character.character_id ?? row.character_id,463
characterId: character.character_id ?? row.character_id,464
accountId: character.account_id ?? row.account_id464
accountId: character.account_id ?? row.account_id,465
skullValue: this.characterSkullValue(character.character_id ?? row.character_id)465
},466
},466
networkIdentity: { sessionId },467
networkIdentity: { sessionId },467
chunkLoader: { ...DEFAULT_CHUNK_LOADER },468
chunkLoader: { ...DEFAULT_CHUNK_LOADER },@@ -536,6 +537,11 @@ export class Simulation536
if (!force && (record.skullValue | 0) === value)537
if (!force && (record.skullValue | 0) === value)537
{ return null; }538
{ return null; }538
record.skullValue = value;539
record.skullValue = value;540
this.workers[record.workerId]?.call({541
type: WorkerMessage.applySkullValue,542
entityId,543
skullValue: value544
});539
const event = {545
const event = {540
type: "entity-skull",546
type: "entity-skull",541
entityId,547
entityId,@@ -553,7 +559,13 @@ export class Simulation553
const record = this.directory.get(entityId);559
const record = this.directory.get(entityId);554
if (!record)560
if (!record)555
{ return; }561
{ return; }556
record.skullValue = this.characterSkullValue(characterId);562
const value = this.characterSkullValue(characterId);563
record.skullValue = value;564
this.workers[record.workerId]?.call({565
type: WorkerMessage.applySkullValue,566
entityId,567
skullValue: value568
});557
}569
}558
570
559
#stepSkullDecay ()571
#stepSkullDecay ()@@ -176,6 +176,8 @@ export class WorkerWorld
176
176
return this.#applyBag(message.entityId, message.bag);
177
177
case WorkerMessage.applyAppearance:
178
178
return this.#applyAppearance(message.entityId, message.appearance);
179
case WorkerMessage.applySkullValue:
180
return this.#applySkullValue(message.entityId, message.skullValue);
179
181
case WorkerMessage.engageTarget:
180
182
return this.#engageTargetMessage(message);
181
183
case WorkerMessage.listTownGuardsNear:
@@ -845,6 +847,18 @@ export class WorkerWorld
845
847
return publicEntityView(this.ecs, entityId);
846
848
}
847
849
850
#applySkullValue (entityId, skullValue)
851
{
852
const handle = this.ecs.handleOf(entityId);
853
if (handle == null)
854
{ return null; }
855
const character = this.ecs.get(handle, Component.character);
856
if (!character)
857
{ return null; }
858
character.skullValue = Math.max(0, skullValue | 0);
859
return { ok: true, skullValue: character.skullValue };
860
}
861
848
862
#updateFlight (entityId, flightPatch, now = Date.now())
849
863
{
850
864
const handle = this.ecs.handleOf(entityId);
@@ -176,6 +176,8 @@ export class WorkerWorld176
return this.#applyBag(message.entityId, message.bag);176
return this.#applyBag(message.entityId, message.bag);177
case WorkerMessage.applyAppearance:177
case WorkerMessage.applyAppearance:178
return this.#applyAppearance(message.entityId, message.appearance);178
return this.#applyAppearance(message.entityId, message.appearance);179
case WorkerMessage.applySkullValue:180
return this.#applySkullValue(message.entityId, message.skullValue);179
case WorkerMessage.engageTarget:181
case WorkerMessage.engageTarget:180
return this.#engageTargetMessage(message);182
return this.#engageTargetMessage(message);181
case WorkerMessage.listTownGuardsNear:183
case WorkerMessage.listTownGuardsNear:@@ -845,6 +847,18 @@ export class WorkerWorld845
return publicEntityView(this.ecs, entityId);847
return publicEntityView(this.ecs, entityId);846
}848
}847
849
850
#applySkullValue (entityId, skullValue)851
{852
const handle = this.ecs.handleOf(entityId);853
if (handle == null)854
{ return null; }855
const character = this.ecs.get(handle, Component.character);856
if (!character)857
{ return null; }858
character.skullValue = Math.max(0, skullValue | 0);859
return { ok: true, skullValue: character.skullValue };860
}861
848
#updateFlight (entityId, flightPatch, now = Date.now())862
#updateFlight (entityId, flightPatch, now = Date.now())849
{863
{850
const handle = this.ecs.handleOf(entityId);864
const handle = this.ecs.handleOf(entityId);@@ -16,6 +16,7 @@ import { SpellId } from "../../shared/definitions/spells.js";
16
16
import { totalXpForLevel } from "../../shared/skills/progression.js";
17
17
import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";
18
18
import { combatDamageRollLevel } from "../../shared/combat/rolls.js";
19
import { SKULL_KILL_BONUS } from "../../shared/combat/skull.js";
19
20
import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
20
21
import { withDamagingCombatRng, installDamagingCombatRng, installMissDodgeRng, installBlockRng } from "./helpers/combatRng.js";
21
22
@@ -589,6 +590,66 @@ test("aggressive mobs and town guards aggro each other", () =>
589
590
{ harness.close(); }
590
591
});
591
592
593
test("town guards aggro players whose skull tint lerps toward red", () =>
594
{
595
const harness = makeSimulation({ workerCount: 1 });
596
try
597
{
598
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
599
harness.stateDb.addKillCounter(character.character_id, SKULL_KILL_BONUS, harness.now());
600
const playerId = harness.simulation.spawnCharacter(1, character);
601
assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS);
602
603
const guardId = harness.simulation.spawnEntity(mobComponents({
604
tileX: 10,
605
tileY: 8,
606
mobTypeId: MobId.townGuard,
607
spawnerId: "0:0:guard",
608
originTileX: 10,
609
originTileY: 8,
610
wanderRangeTiles: 8,
611
level: 10
612
}));
613
614
harness.simulation.step();
615
assert.equal(
616
componentsOf(harness.simulation, guardId).combat?.targetEntityId,
617
playerId
618
);
619
}
620
finally
621
{ harness.close(); }
622
});
623
624
test("town guards ignore players with white-only skull", () =>
625
{
626
const harness = makeSimulation({ workerCount: 1 });
627
try
628
{
629
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
630
harness.stateDb.setKillCounter(character.character_id, 1, harness.now());
631
const playerId = harness.simulation.spawnCharacter(1, character);
632
assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, 1);
633
634
const guardId = harness.simulation.spawnEntity(mobComponents({
635
tileX: 10,
636
tileY: 8,
637
mobTypeId: MobId.townGuard,
638
spawnerId: "0:0:guard",
639
originTileX: 10,
640
originTileY: 8,
641
wanderRangeTiles: 8,
642
level: 10
643
}));
644
645
harness.simulation.step();
646
assert.equal(componentsOf(harness.simulation, guardId).combat?.targetEntityId, null);
647
assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, null);
648
}
649
finally
650
{ harness.close(); }
651
});
652
592
653
test("spawner mobs far from home path chunk-step toward the origin", () =>
593
654
{
594
655
const harness = makeSimulation({ workerCount: 1, maxChunkX: 5 });
@@ -16,6 +16,7 @@ import { SpellId } from "../../shared/definitions/spells.js";16
import { totalXpForLevel } from "../../shared/skills/progression.js";16
import { totalXpForLevel } from "../../shared/skills/progression.js";17
import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";17
import { resolveMeleeSwing, attackProfileOf, hasCombatLineOfSight, queueMeleeApproach, markSeekCover, engageTarget, applyDamageAndKill, markFleeOnHit, stepCombat, chebyshev } from "../src/combat/service.js";18
import { combatDamageRollLevel } from "../../shared/combat/rolls.js";18
import { combatDamageRollLevel } from "../../shared/combat/rolls.js";19
import { SKULL_KILL_BONUS } from "../../shared/combat/skull.js";19
import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";20
import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";20
import { withDamagingCombatRng, installDamagingCombatRng, installMissDodgeRng, installBlockRng } from "./helpers/combatRng.js";21
import { withDamagingCombatRng, installDamagingCombatRng, installMissDodgeRng, installBlockRng } from "./helpers/combatRng.js";21
22
@@ -589,6 +590,66 @@ test("aggressive mobs and town guards aggro each other", () =>589
{ harness.close(); }590
{ harness.close(); }590
});591
});591
592
593
test("town guards aggro players whose skull tint lerps toward red", () =>594
{595
const harness = makeSimulation({ workerCount: 1 });596
try597
{598
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });599
harness.stateDb.addKillCounter(character.character_id, SKULL_KILL_BONUS, harness.now());600
const playerId = harness.simulation.spawnCharacter(1, character);601
assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, SKULL_KILL_BONUS);602
603
const guardId = harness.simulation.spawnEntity(mobComponents({604
tileX: 10,605
tileY: 8,606
mobTypeId: MobId.townGuard,607
spawnerId: "0:0:guard",608
originTileX: 10,609
originTileY: 8,610
wanderRangeTiles: 8,611
level: 10612
}));613
614
harness.simulation.step();615
assert.equal(616
componentsOf(harness.simulation, guardId).combat?.targetEntityId,617
playerId618
);619
}620
finally621
{ harness.close(); }622
});623
624
test("town guards ignore players with white-only skull", () =>625
{626
const harness = makeSimulation({ workerCount: 1 });627
try628
{629
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });630
harness.stateDb.setKillCounter(character.character_id, 1, harness.now());631
const playerId = harness.simulation.spawnCharacter(1, character);632
assert.equal(componentsOf(harness.simulation, playerId).character?.skullValue, 1);633
634
const guardId = harness.simulation.spawnEntity(mobComponents({635
tileX: 10,636
tileY: 8,637
mobTypeId: MobId.townGuard,638
spawnerId: "0:0:guard",639
originTileX: 10,640
originTileY: 8,641
wanderRangeTiles: 8,642
level: 10643
}));644
645
harness.simulation.step();646
assert.equal(componentsOf(harness.simulation, guardId).combat?.targetEntityId, null);647
assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, null);648
}649
finally650
{ harness.close(); }651
});652
592
test("spawner mobs far from home path chunk-step toward the origin", () =>653
test("spawner mobs far from home path chunk-step toward the origin", () =>593
{654
{594
const harness = makeSimulation({ workerCount: 1, maxChunkX: 5 });655
const harness = makeSimulation({ workerCount: 1, maxChunkX: 5 });@@ -4,10 +4,12 @@ import
4
4
{
5
5
skullValue,
6
6
skullTintColor,
7
skullAttractsTownGuards,
7
8
decayKillCounter,
8
9
SKULL_PURE_RED_VALUE,
9
10
SKULL_DARK_RED_VALUE,
10
SKULL_KILL_BONUS
11
SKULL_KILL_BONUS,
12
SKULL_RED_LERP_START
11
13
} from "../../shared/combat/skull.js";
12
14
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
13
15
import { Component } from "../src/ecs/components.js";
@@ -67,6 +69,14 @@ test("skull tint: invisible, white, red, dark red", () =>
67
69
assert.equal(dark.b, 0);
68
70
});
69
71
72
test("skull attracts town guards once tint starts lerping red", () =>
73
{
74
assert.equal(skullAttractsTownGuards(0), false);
75
assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START), false);
76
assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START + 1), true);
77
assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), true);
78
});
79
70
80
test("PvP kill awards temporary skull points that replicate", () =>
71
81
{
72
82
const random = Math.random;
@@ -4,10 +4,12 @@ import4
{4
{5
skullValue,5
skullValue,6
skullTintColor,6
skullTintColor,7
skullAttractsTownGuards,7
decayKillCounter,8
decayKillCounter,8
SKULL_PURE_RED_VALUE,9
SKULL_PURE_RED_VALUE,9
SKULL_DARK_RED_VALUE,10
SKULL_DARK_RED_VALUE,10
SKULL_KILL_BONUS11
SKULL_KILL_BONUS,12
SKULL_RED_LERP_START11
} from "../../shared/combat/skull.js";13
} from "../../shared/combat/skull.js";12
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";14
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";13
import { Component } from "../src/ecs/components.js";15
import { Component } from "../src/ecs/components.js";@@ -67,6 +69,14 @@ test("skull tint: invisible, white, red, dark red", () =>67
assert.equal(dark.b, 0);69
assert.equal(dark.b, 0);68
});70
});69
71
72
test("skull attracts town guards once tint starts lerping red", () =>73
{74
assert.equal(skullAttractsTownGuards(0), false);75
assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START), false);76
assert.equal(skullAttractsTownGuards(SKULL_RED_LERP_START + 1), true);77
assert.equal(skullAttractsTownGuards(SKULL_KILL_BONUS), true);78
});79
70
test("PvP kill awards temporary skull points that replicate", () =>80
test("PvP kill awards temporary skull points that replicate", () =>71
{81
{72
const random = Math.random;82
const random = Math.random;