| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { GAIT } from "../../shared/protocol/constants.js"; |
| 4 | import { SkillId, skillLevel } from "../../shared/definitions/skills.js"; |
| 5 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 6 | import { |
| 7 | staminaCostPerRunStep, |
| 8 | STAMINA_REGEN_PER_SECOND |
| 9 | } from "../../shared/stamina/rules.js"; |
| 10 | import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; |
| 11 | |
| 12 | function workerOf (simulation, entityId) |
| 13 | { |
| 14 | const record = simulation.entityRecord(entityId); |
| 15 | assert.ok(record); |
| 16 | return simulation.workers[record.workerId]; |
| 17 | } |
| 18 | |
| 19 | function componentsOf (simulation, entityId) |
| 20 | { |
| 21 | const worker = workerOf(simulation, entityId); |
| 22 | const handle = worker.world.ecs.handleOf(entityId); |
| 23 | assert.ok(handle != null); |
| 24 | return worker.world.ecs.componentsOf(handle); |
| 25 | } |
| 26 | |
| 27 | function setStaminaXp (harness, characterId, level) |
| 28 | { |
| 29 | const skills = harness.stateDb.getSkills(characterId); |
| 30 | skills.xp.stamina = totalXpForLevel(level); |
| 31 | harness.stateDb.saveSkills(characterId, skills); |
| 32 | } |
| 33 | |
| 34 | test("staminaCostPerRunStep drains one point per second of continuous running", () => |
| 35 | { |
| 36 | assert.equal(staminaCostPerRunStep(2), 0.25); |
| 37 | assert.equal(staminaCostPerRunStep(1), 0.5); |
| 38 | }); |
| 39 | |
| 40 | test("set-gait to run is rejected when stamina is empty", () => |
| 41 | { |
| 42 | const harness = makeSimulation({ workerCount: 1 }); |
| 43 | try |
| 44 | { |
| 45 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 46 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 47 | const comps = componentsOf(harness.simulation, playerId); |
| 48 | comps.stamina.current = 0; |
| 49 | |
| 50 | harness.simulation.enqueue(1, { |
| 51 | type: "set-gait", |
| 52 | characterId: character.character_id, |
| 53 | gait: GAIT.run |
| 54 | }); |
| 55 | const result = harness.simulation.step(); |
| 56 | assert.ok(result.events.some((event) => |
| 57 | event.type === "command-error" && /exhausted/i.test(event.message) |
| 58 | )); |
| 59 | assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk); |
| 60 | } |
| 61 | finally |
| 62 | { harness.close(); } |
| 63 | }); |
| 64 | |
| 65 | test("running drains stamina, awards XP, and forces walk at zero", () => |
| 66 | { |
| 67 | const harness = makeSimulation({ workerCount: 1 }); |
| 68 | try |
| 69 | { |
| 70 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 71 | setStaminaXp(harness, character.character_id, 6); |
| 72 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 73 | assert.equal(harness.simulation.staminaOf(playerId).max, 6); |
| 74 | // Drain a short bar so the test finishes quickly. |
| 75 | componentsOf(harness.simulation, playerId).stamina.current = 2; |
| 76 | |
| 77 | harness.simulation.enqueue(1, { |
| 78 | type: "set-gait", |
| 79 | characterId: character.character_id, |
| 80 | gait: GAIT.run |
| 81 | }); |
| 82 | harness.simulation.enqueue(1, { |
| 83 | type: "move-to", |
| 84 | characterId: character.character_id, |
| 85 | tileX: 30, |
| 86 | tileY: 8 |
| 87 | }); |
| 88 | |
| 89 | const xpBefore = harness.stateDb.getSkills(character.character_id).xp.stamina; |
| 90 | let forcedWalk = false; |
| 91 | for (let i = 0; i < 120; i++) |
| 92 | { |
| 93 | harness.advance(50); |
| 94 | const events = harness.simulation.step().events; |
| 95 | const stamina = harness.simulation.staminaOf(playerId); |
| 96 | if (events.some((event) => event.type === "stamina" && event.gait === GAIT.walk && event.current <= 0)) |
| 97 | { forcedWalk = true; } |
| 98 | if (stamina.current <= 0) |
| 99 | { |
| 100 | assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk); |
| 101 | forcedWalk = true; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | assert.equal(forcedWalk, true); |
| 106 | assert.ok(harness.simulation.staminaOf(playerId).current <= 0); |
| 107 | assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk); |
| 108 | |
| 109 | const xpAfter = harness.stateDb.getSkills(character.character_id).xp.stamina; |
| 110 | assert.ok(xpAfter > xpBefore, "running should award Stamina XP"); |
| 111 | assert.equal(skillLevel({ xp: { stamina: xpAfter } }, SkillId.stamina) >= 6, true); |
| 112 | } |
| 113 | finally |
| 114 | { harness.close(); } |
| 115 | }); |
| 116 | |
| 117 | test("walking regenerates stamina over time", () => |
| 118 | { |
| 119 | const harness = makeSimulation({ workerCount: 1 }); |
| 120 | try |
| 121 | { |
| 122 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 123 | setStaminaXp(harness, character.character_id, 3); |
| 124 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 125 | const comps = componentsOf(harness.simulation, playerId); |
| 126 | comps.stamina.current = 0; |
| 127 | comps.movement.gait = GAIT.walk; |
| 128 | |
| 129 | const seconds = 2.2; |
| 130 | const ticks = Math.ceil(seconds * 20); |
| 131 | for (let i = 0; i < ticks; i++) |
| 132 | { |
| 133 | harness.advance(50); |
| 134 | harness.simulation.step(); |
| 135 | } |
| 136 | const stamina = harness.simulation.staminaOf(playerId); |
| 137 | assert.ok( |
| 138 | stamina.current >= seconds * STAMINA_REGEN_PER_SECOND - 0.2, |
| 139 | `expected ~${seconds} regen, got ${stamina.current}` |
| 140 | ); |
| 141 | assert.ok(stamina.current <= 3); |
| 142 | } |
| 143 | finally |
| 144 | { harness.close(); } |
| 145 | }); |
| 146 | |
| 147 | test("running does not regenerate stamina while moving", () => |
| 148 | { |
| 149 | const harness = makeSimulation({ workerCount: 1 }); |
| 150 | try |
| 151 | { |
| 152 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 153 | setStaminaXp(harness, character.character_id, 5); |
| 154 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 155 | const comps = componentsOf(harness.simulation, playerId); |
| 156 | comps.stamina.current = 2; |
| 157 | comps.movement.gait = GAIT.run; |
| 158 | |
| 159 | harness.simulation.enqueue(1, { |
| 160 | type: "move-to", |
| 161 | characterId: character.character_id, |
| 162 | tileX: 30, |
| 163 | tileY: 8 |
| 164 | }); |
| 165 | |
| 166 | stepTimes(harness.simulation, 40); |
| 167 | assert.ok(harness.simulation.staminaOf(playerId).current <= 2.01); |
| 168 | } |
| 169 | finally |
| 170 | { harness.close(); } |
| 171 | }); |
| 172 | |
| 173 | test("run gait while idle still regenerates stamina", () => |
| 174 | { |
| 175 | const harness = makeSimulation({ workerCount: 1 }); |
| 176 | try |
| 177 | { |
| 178 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 179 | setStaminaXp(harness, character.character_id, 5); |
| 180 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 181 | const comps = componentsOf(harness.simulation, playerId); |
| 182 | comps.stamina.current = 1; |
| 183 | comps.movement.gait = GAIT.run; |
| 184 | |
| 185 | const seconds = 1.5; |
| 186 | const ticks = Math.ceil(seconds * 20); |
| 187 | for (let i = 0; i < ticks; i++) |
| 188 | { |
| 189 | harness.advance(50); |
| 190 | harness.simulation.step(); |
| 191 | } |
| 192 | const stamina = harness.simulation.staminaOf(playerId); |
| 193 | assert.ok( |
| 194 | stamina.current >= 1 + seconds * STAMINA_REGEN_PER_SECOND - 0.2, |
| 195 | `expected idle run regen, got ${stamina.current}` |
| 196 | ); |
| 197 | } |
| 198 | finally |
| 199 | { harness.close(); } |
| 200 | }); |