BlueGit

timfa questward

Files

questward / server / tests / shearing.test.js

shearing.test.js

shearing.test.js 8.2 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 6
99 );
100 assert.equal(recipeDefinition("shears-bronze"), null);
101});
102
103test("sheep and cashmere goat are shearable with fleece/shorn sprites", () =>
104{
105 const sheep = mobDefinition(MobId.sheep);
106 assert.equal(sheep.shearable.clothTier, ClothTier.wool);
107 assert.equal(mobSpriteName(MobId.sheep), "mob:sheep:fleece");
108 assert.equal(mobSpriteName(MobId.sheep, "shorn"), "mob:sheep:shorn");
109 const goat = mobDefinition(MobId.cashmereGoat);
110 assert.equal(goat.shearable.clothTier, ClothTier.cashmere);
111 assert.equal(mobSpriteName(MobId.cashmereGoat, "fleece"), "mob:cashmere-goat:fleece");
112});
113
114test("fairy uses own sprites, dual-channel kit, and random-element melee", () =>
115{
116 const fairy = mobDefinition(MobId.fairy);
117 assert.equal(fairy.spriteKey, undefined);
118 assert.equal(mobSpriteName(MobId.fairy), "mob:fairy:fairy");
119 assert.equal(fairy.bloomBody, true);
120 assert.equal(fairy.dualChannelAppearance, true);
121 assert.equal(fairy.randomElementMelee, true);
122 assert.equal(fairy.lightRadiusTiles, 1);
123 assert.equal(fairy.frames, 2);
124 assert.equal(fairy.drops.length, 1);
125 assert.equal(fairy.drops[0].itemId, ClothItemId.fairySilk);
126 assert.equal(fairy.drops[0].chance, 0.7);
127 assert.equal(MetalItemId.bronzeShears, undefined);
128 assert.equal(itemDefinition(MetalItemId.ironShears)?.equipment?.kind, EquipmentKind.shears);
129});
130
131test("flax needs steel-or-better clippers; linen fibre is named Flax", () =>
132{
133 assert.equal(itemDefinition(ClothItemId.linen).displayName, "Flax");
134 assert.equal(metalTierForCloth(ClothTier.linen), MaterialTier.steel);
135 const skills = normalizePlayerSkills(null);
136 skills.xp[SkillId.herbalism] = totalXpForLevel(20);
137 const bronze = resolvePlantClippers({
138 inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }],
139 equipmentSlots: emptyEquipment().slots,
140 skills,
141 minClothTier: ClothTier.linen
142 });
143 assert.equal(bronze.ok, false);
144 const steel = resolvePlantClippers({
145 inventorySlots: [{ itemId: MetalItemId.steelPlantClippers, quantity: 1 }],
146 equipmentSlots: emptyEquipment().slots,
147 skills,
148 minClothTier: ClothTier.linen
149 });
150 assert.equal(steel.ok, true);
151 const plant = plantGatherForBlock({ typeId: BlockType.flaxPlant, state: "flax-plant" });
152 assert.equal(plant.kind, "flax");
153 assert.equal(plant.requiresClippers, true);
154 assert.equal(plant.minClothTier, "linen");
155});
156
157test("silk cocoons need adamantium-or-better clippers", () =>
158{
159 assert.equal(metalTierForCloth(ClothTier.silk), MaterialTier.adamantium);
160 assert.match(itemDefinition(ClothItemId.silk).description, /cocoon/i);
161 const skills = normalizePlayerSkills(null);
162 skills.xp[SkillId.herbalism] = totalXpForLevel(50);
163 const mithril = resolvePlantClippers({
164 inventorySlots: [{ itemId: MetalItemId.mithrilPlantClippers, quantity: 1 }],
165 equipmentSlots: emptyEquipment().slots,
166 skills,
167 minClothTier: ClothTier.silk
168 });
169 assert.equal(mithril.ok, false);
170 const adamantium = resolvePlantClippers({
171 inventorySlots: [{ itemId: MetalItemId.adamantiumPlantClippers, quantity: 1 }],
172 equipmentSlots: emptyEquipment().slots,
173 skills,
174 minClothTier: ClothTier.silk
175 });
176 assert.equal(adamantium.ok, true);
177 const plant = plantGatherForBlock({ typeId: BlockType.silkCocoon, state: "silk-cocoon" });
178 assert.equal(plant.kind, "silk");
179 assert.equal(plant.requiresClippers, true);
180 assert.equal(plant.minClothTier, "silk");
181});
182
183test("shearing a sheep grants wool and sets shorn state", () =>
184{
185 const harness = makeSimulation({ workerCount: 1 });
186 try
187 {
188 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
189 const playerId = harness.simulation.spawnCharacter(1, character);
190 const worker = workerOf(harness.simulation, playerId);
191 setSkillLevel(worker, playerId, SkillId.herbalism, 10);
192 giveInventory(harness, playerId, [
193 { itemId: MetalItemId.ironShears, quantity: 1 }
194 ]);
195
196 const sheepId = harness.simulation.spawnEntity(mobComponents({
197 tileX: 9,
198 tileY: 8,
199 mobTypeId: MobId.sheep,
200 spawnerId: "test-sheep",
201 displayName: "Woolly"
202 }));
203
204 harness.simulation.enqueue(1, {
205 type: "shear",
206 characterId: character.character_id,
207 targetEntityId: sheepId
208 });
209 advanceMs(harness, 3500);
210
211 const creature = worker.world.ecs.get(
212 worker.world.ecs.handleOf(sheepId),
213 Component.creature
214 );
215 assert.equal(creature.fleeceState, "shorn");
216 assert.ok(creature.woolReadyAt > harness.now());
217 const bag = harness.simulation.bagOf(playerId);
218 const woolId = rawClothItemId(ClothTier.wool);
219 assert.ok(bag.inventory.some((s) => s?.itemId === woolId));
220 assert.equal(
221 clothExhaustDurationMs(ClothTier.wool),
222 tierRank(ClothTier.wool) * 10_000
223 );
224 }
225 finally
226 {
227 harness.close();
228 }
229});