BlueGit

timfa questward

Files

questward / server / tests / shearing.test.js

shearing.test.js

shearing.test.js 6.4 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType, plantGatherForBlock } from "../../shared/definitions/tiles.js";
4import { CraftingStationId } from "../../shared/definitions/craftingStations.js";
5import { MetalItemId } from "../../shared/definitions/metalItems.js";
6import { LeatherItemId } from "../../shared/definitions/leatherItems.js";
7import { ClothItemId, rawClothItemId } from "../../shared/definitions/clothItems.js";
8import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
9import {
10 ClothTier,
11 MaterialTier,
12 metalTierForCloth,
13 clothExhaustDurationMs,
14 tierRank
15} from "../../shared/definitions/materialTiers.js";
16import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js";
17import { EquipmentKind } from "../../shared/definitions/equipmentKinds.js";
18import { recipeDefinition, recipesForStation } from "../../shared/crafting/stationRecipes.js";
19import { resolveItemDualTint } from "../../shared/render/tint.js";
20import { resolvePlantClippers } from "../../shared/definitions/gathering.js";
21import { SkillId } from "../../shared/definitions/skills.js";
22import { normalizePlayerSkills } from "../../shared/skills/state.js";
23import { totalXpForLevel } from "../../shared/skills/progression.js";
24import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
25import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
26import { Component } from "../src/ecs/components.js";
27import { mobComponents } from "../src/simulation/spawners.js";
28
29function workerOf (simulation, playerId)
30{
31 const record = simulation.entityRecord(playerId);
32 return simulation.workers[record.workerId];
33}
34
35function advanceMs (harness, ms)
36{
37 const ticks = Math.ceil(ms / 50) + 3;
38 for (let i = 0; i < ticks; i++)
39 {
40 harness.advance(50);
41 harness.simulation.step();
42 }
43}
44
45function setSkillLevel (worker, playerId, skillId, level)
46{
47 const handle = worker.world.ecs.handleOf(playerId);
48 const skills = worker.world.ecs.get(handle, Component.skills);
49 skills.xp[skillId] = totalXpForLevel(level);
50}
51
52function giveInventory (harness, playerId, items)
53{
54 const inventory = emptySlots();
55 items.forEach((stack, index) =>
56 {
57 inventory[index] = {
58 itemId: stack.itemId,
59 quantity: stack.quantity,
60 ...(stack.instance ? { instance: structuredClone(stack.instance) } : {})
61 };
62 });
63 harness.simulation.applyBag(playerId, {
64 inventory,
65 equipment: emptyEquipment(),
66 vault: [],
67 vaultReserved: []
68 });
69}
70
71test("plant clippers use dual metal/leather tint and bar+leather recipe", () =>
72{
73 const def = itemDefinition(MetalItemId.bronzePlantClippers);
74 assert.equal(def.iconKey, "plant-clippers");
75 assert.equal(def.dualTintFromTier, true);
76 assert.equal(def.leatherTier, "softLeather");
77 const dual = resolveItemDualTint({ itemId: MetalItemId.bronzePlantClippers }, def);
78 assert.ok(dual?.shaft);
79 assert.ok(dual?.tip);
80 const recipe = recipeDefinition("plant-clippers-bronze");
81 assert.deepEqual(recipe.inputs, [
82 { itemId: ItemId.bronzeBar, quantity: 1 },
83 { itemId: LeatherItemId.softLeather, quantity: 1 }
84 ]);
85 assert.equal(recipe.stationId, CraftingStationId.anvil);
86});
87
88test("shears are magenta-tinted metal and craft from one bar", () =>
89{
90 const def = itemDefinition(MetalItemId.ironShears);
91 assert.equal(def.equipment.kind, EquipmentKind.shears);
92 assert.equal(def.tintFromTier, true);
93 assert.equal(def.dualTintFromTier, undefined);
94 const recipe = recipeDefinition("shears-iron");
95 assert.deepEqual(recipe.inputs, [{ itemId: ItemId.ironBar, quantity: 1 }]);
96 assert.equal(
97 recipesForStation(CraftingStationId.anvil).filter((r) => r.craftGroup === "shears").length,
98 7
99 );
100});
101
102test("sheep and cashmere goat are shearable with fleece/shorn sprites", () =>
103{
104 const sheep = mobDefinition(MobId.sheep);
105 assert.equal(sheep.shearable.clothTier, ClothTier.wool);
106 assert.equal(mobSpriteName(MobId.sheep), "mob:sheep:fleece");
107 assert.equal(mobSpriteName(MobId.sheep, "shorn"), "mob:sheep:shorn");
108 const goat = mobDefinition(MobId.cashmereGoat);
109 assert.equal(goat.shearable.clothTier, ClothTier.cashmere);
110 assert.equal(mobSpriteName(MobId.cashmereGoat, "fleece"), "mob:cashmere-goat:fleece");
111});
112
113test("flax needs steel-or-better clippers; linen fibre is named Flax", () =>
114{
115 assert.equal(itemDefinition(ClothItemId.linen).displayName, "Flax");
116 assert.equal(metalTierForCloth(ClothTier.linen), MaterialTier.steel);
117 const skills = normalizePlayerSkills(null);
118 skills.xp[SkillId.herbalism] = totalXpForLevel(20);
119 const bronze = resolvePlantClippers({
120 inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }],
121 equipmentSlots: emptyEquipment().slots,
122 skills,
123 minClothTier: ClothTier.linen
124 });
125 assert.equal(bronze.ok, false);
126 const steel = resolvePlantClippers({
127 inventorySlots: [{ itemId: MetalItemId.steelPlantClippers, quantity: 1 }],
128 equipmentSlots: emptyEquipment().slots,
129 skills,
130 minClothTier: ClothTier.linen
131 });
132 assert.equal(steel.ok, true);
133 const plant = plantGatherForBlock({ typeId: BlockType.flaxPlant, state: "flax-plant" });
134 assert.equal(plant.kind, "flax");
135 assert.equal(plant.requiresClippers, true);
136 assert.equal(plant.minClothTier, "linen");
137});
138
139test("shearing a sheep grants wool and sets shorn state", () =>
140{
141 const harness = makeSimulation({ workerCount: 1 });
142 try
143 {
144 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
145 const playerId = harness.simulation.spawnCharacter(1, character);
146 const worker = workerOf(harness.simulation, playerId);
147 setSkillLevel(worker, playerId, SkillId.herbalism, 10);
148 giveInventory(harness, playerId, [
149 { itemId: MetalItemId.ironShears, quantity: 1 }
150 ]);
151
152 const sheepId = harness.simulation.spawnEntity(mobComponents({
153 tileX: 9,
154 tileY: 8,
155 mobTypeId: MobId.sheep,
156 spawnerId: "test-sheep",
157 displayName: "Woolly"
158 }));
159
160 harness.simulation.enqueue(1, {
161 type: "shear",
162 characterId: character.character_id,
163 targetEntityId: sheepId
164 });
165 advanceMs(harness, 3500);
166
167 const creature = worker.world.ecs.get(
168 worker.world.ecs.handleOf(sheepId),
169 Component.creature
170 );
171 assert.equal(creature.fleeceState, "shorn");
172 assert.ok(creature.woolReadyAt > harness.now());
173 const bag = harness.simulation.bagOf(playerId);
174 const woolId = rawClothItemId(ClothTier.wool);
175 assert.ok(bag.inventory.some((s) => s?.itemId === woolId));
176 assert.equal(
177 clothExhaustDurationMs(ClothTier.wool),
178 tierRank(ClothTier.wool) * 10_000
179 );
180 }
181 finally
182 {
183 harness.close();
184 }
185});