BlueGit

timfa questward

Stop awarding Magic XP when a spell hits.

Cast already grants Magic XP; landings should only add Mind XP from damage.

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
60aff71aecf67d44feeb0b27cc601ff293da11fe
Parent
4cb4ddbe
3 files changed +88 −13
server/src/skills/service.js +17 −8
@@ -43,13 +43,22 @@ export function awardCombatHitXp (ecs, attackerHandle, profile, damage)
43 43 { return null; }
44 44 const equipment = ecs.get(attackerHandle, Component.equipment);
45 45 const levelUps = [];
46 const hit = applySkillXp(skills, profile.hitSkillId, amount, equipment);
47 if (hit.levelUp)
48 { levelUps.push(hit.levelUp); }
49 const damageSkill = applySkillXp(skills, profile.damageSkillId, amount, equipment);
50 if (damageSkill.levelUp)
51 { levelUps.push(damageSkill.levelUp); }
52 if (!hit.changed && !damageSkill.changed)
46 let changed = false;
47 if (profile.hitSkillId != null)
48 {
49 const hit = applySkillXp(skills, profile.hitSkillId, amount, equipment);
50 if (hit.levelUp)
51 { levelUps.push(hit.levelUp); }
52 changed = changed || hit.changed;
53 }
54 if (profile.damageSkillId != null)
55 {
56 const damageSkill = applySkillXp(skills, profile.damageSkillId, amount, equipment);
57 if (damageSkill.levelUp)
58 { levelUps.push(damageSkill.levelUp); }
59 changed = changed || damageSkill.changed;
60 }
61 if (!changed)
53 62 { return null; }
54 63 return {
55 64 ...publicSkillsPayload(skills),
@@ -58,7 +67,7 @@ export function awardCombatHitXp (ecs, attackerHandle, profile, damage)
58 67 };
59 68 }
60 69
61 /** Magic XP on a successful cast; damage XP uses {@link awardCombatHitXp} when the spell lands. */
70 /** Magic XP on a successful cast; Mind XP uses {@link awardCombatHitXp} when the spell lands. */
62 71 export function awardSpellCastXp (ecs, casterHandle, spellDifficulty)
63 72 {
64 73 const amount = spellCastXpFromDifficulty(spellDifficulty);
@@ -43,13 +43,22 @@ export function awardCombatHitXp (ecs, attackerHandle, profile, damage)
43 { return null; }
43 { return null; }
44 const equipment = ecs.get(attackerHandle, Component.equipment);
44 const equipment = ecs.get(attackerHandle, Component.equipment);
45 const levelUps = [];
45 const levelUps = [];
46 const hit = applySkillXp(skills, profile.hitSkillId, amount, equipment);
46 let changed = false;
47 if (hit.levelUp)
47 if (profile.hitSkillId != null)
48 { levelUps.push(hit.levelUp); }
48 {
49 const damageSkill = applySkillXp(skills, profile.damageSkillId, amount, equipment);
49 const hit = applySkillXp(skills, profile.hitSkillId, amount, equipment);
50 if (damageSkill.levelUp)
50 if (hit.levelUp)
51 { levelUps.push(damageSkill.levelUp); }
51 { levelUps.push(hit.levelUp); }
52 if (!hit.changed && !damageSkill.changed)
52 changed = changed || hit.changed;
53 }
54 if (profile.damageSkillId != null)
55 {
56 const damageSkill = applySkillXp(skills, profile.damageSkillId, amount, equipment);
57 if (damageSkill.levelUp)
58 { levelUps.push(damageSkill.levelUp); }
59 changed = changed || damageSkill.changed;
60 }
61 if (!changed)
53 { return null; }
62 { return null; }
54 return {
63 return {
55 ...publicSkillsPayload(skills),
64 ...publicSkillsPayload(skills),
@@ -58,7 +67,7 @@ export function awardCombatHitXp (ecs, attackerHandle, profile, damage)
58 };
67 };
59}
68}
60
69
61/** Magic XP on a successful cast; damage XP uses {@link awardCombatHitXp} when the spell lands. */
70/** Magic XP on a successful cast; Mind XP uses {@link awardCombatHitXp} when the spell lands. */
62export function awardSpellCastXp (ecs, casterHandle, spellDifficulty)
71export function awardSpellCastXp (ecs, casterHandle, spellDifficulty)
63{
72{
64 const amount = spellCastXpFromDifficulty(spellDifficulty);
73 const amount = spellCastXpFromDifficulty(spellDifficulty);
server/src/spells/service.js +3 −3
@@ -65,8 +65,8 @@ import { itemDefinition } from "../../../shared/definitions/items.js";
65 65 import { bagView } from "../items/bags.js";
66 66 import { applyTindersAtTile, tindersTileStillNeedsCast } from "./tinders.js";
67 67
68 const MAGIC_ATTACK_PROFILE = Object.freeze({
69 hitSkillId: SkillId.magic,
68 /** Spell hits award Mind XP only; Magic XP comes from the cast itself. */
69 const MAGIC_HIT_XP_PROFILE = Object.freeze({
70 70 damageSkillId: SkillId.mind
71 71 });
72 72
@@ -768,7 +768,7 @@ function awardMagicHitXp (world, impact, damage)
768 768 const casterHandle = world.ecs.handleOf(impact.casterEntityId);
769 769 if (casterHandle == null || !impact.casterCharacterId)
770 770 { return events; }
771 const xp = awardCombatHitXp(world.ecs, casterHandle, MAGIC_ATTACK_PROFILE, damage);
771 const xp = awardCombatHitXp(world.ecs, casterHandle, MAGIC_HIT_XP_PROFILE, damage);
772 772 if (xp)
773 773 {
774 774 events.push({
@@ -65,8 +65,8 @@ import { itemDefinition } from "../../../shared/definitions/items.js";
65import { bagView } from "../items/bags.js";
65import { bagView } from "../items/bags.js";
66import { applyTindersAtTile, tindersTileStillNeedsCast } from "./tinders.js";
66import { applyTindersAtTile, tindersTileStillNeedsCast } from "./tinders.js";
67
67
68const MAGIC_ATTACK_PROFILE = Object.freeze({
68/** Spell hits award Mind XP only; Magic XP comes from the cast itself. */
69 hitSkillId: SkillId.magic,
69const MAGIC_HIT_XP_PROFILE = Object.freeze({
70 damageSkillId: SkillId.mind
70 damageSkillId: SkillId.mind
71});
71});
72
72
@@ -768,7 +768,7 @@ function awardMagicHitXp (world, impact, damage)
768 const casterHandle = world.ecs.handleOf(impact.casterEntityId);
768 const casterHandle = world.ecs.handleOf(impact.casterEntityId);
769 if (casterHandle == null || !impact.casterCharacterId)
769 if (casterHandle == null || !impact.casterCharacterId)
770 { return events; }
770 { return events; }
771 const xp = awardCombatHitXp(world.ecs, casterHandle, MAGIC_ATTACK_PROFILE, damage);
771 const xp = awardCombatHitXp(world.ecs, casterHandle, MAGIC_HIT_XP_PROFILE, damage);
772 if (xp)
772 if (xp)
773 {
773 {
774 events.push({
774 events.push({
server/tests/spells.test.js +68 −2
@@ -1,6 +1,6 @@
1 1 import test from "node:test";
2 2 import assert from "node:assert/strict";
3 import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";
3 import { SpellId, spellDefinition, spellDifficulty } from "../../shared/definitions/spells.js";
4 4 import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../shared/spells/constants.js";
5 5 import { mobComponents } from "../src/simulation/spawners.js";
6 6 import { MobId } from "../../shared/definitions/mobs.js";
@@ -17,7 +17,7 @@ import { itemDefinition } from "../../shared/definitions/items.js";
17 17 import { normalizePlayerSpells } from "../../shared/spells/state.js";
18 18 import { createSpellbookStack } from "../src/spells/service.js";
19 19 import { SkillId } from "../../shared/definitions/skills.js";
20 import { totalXpForLevel } from "../../shared/skills/progression.js";
20 import { totalXpForLevel, spellCastXpFromDifficulty } from "../../shared/skills/progression.js";
21 21 import { engageTarget, ensureMobCombat } from "../src/combat/service.js";
22 22
23 23 function workerOf (simulation, entityId)
@@ -709,3 +709,69 @@ test("reorder-spell-favorites swaps favourite order", () =>
709 709 finally
710 710 { harness.close(); }
711 711 });
712
713 test("spell hit awards mind XP but not extra magic XP", () =>
714 {
715 const harness = makeSimulation({ workerCount: 1 });
716 const random = Math.random;
717 Math.random = () => 0.999;
718 try
719 {
720 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
721 const playerId = harness.simulation.spawnCharacter(1, character);
722 const deerId = harness.simulation.spawnEntity(mobComponents({
723 tileX: 10,
724 tileY: 8,
725 mobTypeId: MobId.deer,
726 level: 1
727 }));
728 const worker = workerOf(harness.simulation, playerId);
729 const handle = worker.world.ecs.handleOf(playerId);
730 const deerHandle = worker.world.ecs.handleOf(deerId);
731 worker.world.ecs.get(deerHandle, Component.health).max = 500;
732 worker.world.ecs.get(deerHandle, Component.health).current = 500;
733 const spells = worker.world.ecs.get(handle, Component.spells);
734 spells.known.push(SpellId.manaBullet);
735 const skills = worker.world.ecs.get(handle, Component.skills);
736 const magicBefore = totalXpForLevel(20);
737 const mindBefore = totalXpForLevel(20);
738 skills.xp[SkillId.magic] = magicBefore;
739 skills.xp[SkillId.mind] = mindBefore;
740 const castXp = spellCastXpFromDifficulty(spellDifficulty(spellDefinition(SpellId.manaBullet)));
741
742 harness.simulation.enqueue(1, {
743 type: "cast-spell-entity",
744 characterId: character.character_id,
745 spellId: SpellId.manaBullet,
746 targetEntityId: deerId
747 });
748 const cast = harness.simulation.step();
749 assert.ok(
750 cast.events.some((event) => event.type === "spell-projectile"),
751 JSON.stringify(cast.events.map((e) => e.type))
752 );
753 assert.equal(skills.xp[SkillId.magic], magicBefore + castXp);
754 assert.equal(skills.xp[SkillId.mind], mindBefore);
755
756 harness.advance(SPELL_PROJECTILE_MS + 50);
757 const impact = harness.simulation.step();
758 assert.ok(
759 impact.events.some((event) => event.type === "combat-hit"),
760 JSON.stringify(impact.events.map((e) => e.type))
761 );
762 assert.equal(
763 skills.xp[SkillId.magic],
764 magicBefore + castXp,
765 "hitting must not grant additional Magic XP"
766 );
767 assert.ok(
768 skills.xp[SkillId.mind] > mindBefore,
769 "hitting should grant Mind XP"
770 );
771 }
772 finally
773 {
774 Math.random = random;
775 harness.close();
776 }
777 });
@@ -1,6 +1,6 @@
1import test from "node:test";
1import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { SpellId, spellDefinition } from "../../shared/definitions/spells.js";
3import { SpellId, spellDefinition, spellDifficulty } from "../../shared/definitions/spells.js";
4import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../shared/spells/constants.js";
4import { SPELL_PROJECTILE_MS, SPELL_COOLDOWN_MS } from "../../shared/spells/constants.js";
5import { mobComponents } from "../src/simulation/spawners.js";
5import { mobComponents } from "../src/simulation/spawners.js";
6import { MobId } from "../../shared/definitions/mobs.js";
6import { MobId } from "../../shared/definitions/mobs.js";
@@ -17,7 +17,7 @@ import { itemDefinition } from "../../shared/definitions/items.js";
17import { normalizePlayerSpells } from "../../shared/spells/state.js";
17import { normalizePlayerSpells } from "../../shared/spells/state.js";
18import { createSpellbookStack } from "../src/spells/service.js";
18import { createSpellbookStack } from "../src/spells/service.js";
19import { SkillId } from "../../shared/definitions/skills.js";
19import { SkillId } from "../../shared/definitions/skills.js";
20import { totalXpForLevel } from "../../shared/skills/progression.js";
20import { totalXpForLevel, spellCastXpFromDifficulty } from "../../shared/skills/progression.js";
21import { engageTarget, ensureMobCombat } from "../src/combat/service.js";
21import { engageTarget, ensureMobCombat } from "../src/combat/service.js";
22
22
23function workerOf (simulation, entityId)
23function workerOf (simulation, entityId)
@@ -709,3 +709,69 @@ test("reorder-spell-favorites swaps favourite order", () =>
709 finally
709 finally
710 { harness.close(); }
710 { harness.close(); }
711});
711});
712
713test("spell hit awards mind XP but not extra magic XP", () =>
714{
715 const harness = makeSimulation({ workerCount: 1 });
716 const random = Math.random;
717 Math.random = () => 0.999;
718 try
719 {
720 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
721 const playerId = harness.simulation.spawnCharacter(1, character);
722 const deerId = harness.simulation.spawnEntity(mobComponents({
723 tileX: 10,
724 tileY: 8,
725 mobTypeId: MobId.deer,
726 level: 1
727 }));
728 const worker = workerOf(harness.simulation, playerId);
729 const handle = worker.world.ecs.handleOf(playerId);
730 const deerHandle = worker.world.ecs.handleOf(deerId);
731 worker.world.ecs.get(deerHandle, Component.health).max = 500;
732 worker.world.ecs.get(deerHandle, Component.health).current = 500;
733 const spells = worker.world.ecs.get(handle, Component.spells);
734 spells.known.push(SpellId.manaBullet);
735 const skills = worker.world.ecs.get(handle, Component.skills);
736 const magicBefore = totalXpForLevel(20);
737 const mindBefore = totalXpForLevel(20);
738 skills.xp[SkillId.magic] = magicBefore;
739 skills.xp[SkillId.mind] = mindBefore;
740 const castXp = spellCastXpFromDifficulty(spellDifficulty(spellDefinition(SpellId.manaBullet)));
741
742 harness.simulation.enqueue(1, {
743 type: "cast-spell-entity",
744 characterId: character.character_id,
745 spellId: SpellId.manaBullet,
746 targetEntityId: deerId
747 });
748 const cast = harness.simulation.step();
749 assert.ok(
750 cast.events.some((event) => event.type === "spell-projectile"),
751 JSON.stringify(cast.events.map((e) => e.type))
752 );
753 assert.equal(skills.xp[SkillId.magic], magicBefore + castXp);
754 assert.equal(skills.xp[SkillId.mind], mindBefore);
755
756 harness.advance(SPELL_PROJECTILE_MS + 50);
757 const impact = harness.simulation.step();
758 assert.ok(
759 impact.events.some((event) => event.type === "combat-hit"),
760 JSON.stringify(impact.events.map((e) => e.type))
761 );
762 assert.equal(
763 skills.xp[SkillId.magic],
764 magicBefore + castXp,
765 "hitting must not grant additional Magic XP"
766 );
767 assert.ok(
768 skills.xp[SkillId.mind] > mindBefore,
769 "hitting should grant Mind XP"
770 );
771 }
772 finally
773 {
774 Math.random = random;
775 harness.close();
776 }
777});