BlueGit

timfa questward

Files

questward / server / tests / shearing.test.js

shearing.test.js

shearing.test.js 9.5 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, itemEquipSprite } from "../../shared/definitions/items.js";
9import {
10 ClothTier,
11 MaterialTier,
12 metalTierForCloth,
13 metalTierForHerbTier,
14 clothExhaustDurationMs,
15 tierRank
16} from "../../shared/definitions/materialTiers.js";
17import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js";
18import { EquipmentKind } from "../../shared/definitions/equipmentKinds.js";
19import { recipeDefinition, recipesForStation } from "../../shared/crafting/stationRecipes.js";
20import { resolveItemDualTint, resolveEquipTint, resolveEquipDualTint } from "../../shared/render/tint.js";
21import { resolvePlantClippers } from "../../shared/definitions/gathering.js";
22import { SkillId } from "../../shared/definitions/skills.js";
23import { normalizePlayerSkills } from "../../shared/skills/state.js";
24import { totalXpForLevel } from "../../shared/skills/progression.js";
25import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
26import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
27import { Component } from "../src/ecs/components.js";
28import { mobComponents } from "../src/simulation/spawners.js";
29
30function workerOf (simulation, playerId)
31{
32 const record = simulation.entityRecord(playerId);
33 return simulation.workers[record.workerId];
34}
35
36function advanceMs (harness, ms)
37{
38 const ticks = Math.ceil(ms / 50) + 3;
39 for (let i = 0; i < ticks; i++)
40 {
41 harness.advance(50);
42 harness.simulation.step();
43 }
44}
45
46function setSkillLevel (worker, playerId, skillId, level)
47{
48 const handle = worker.world.ecs.handleOf(playerId);
49 const skills = worker.world.ecs.get(handle, Component.skills);
50 skills.xp[skillId] = totalXpForLevel(level);
51}
52
53function giveInventory (harness, playerId, items)
54{
55 const inventory = emptySlots();
56 items.forEach((stack, index) =>
57 {
58 inventory[index] = {
59 itemId: stack.itemId,
60 quantity: stack.quantity,
61 ...(stack.instance ? { instance: structuredClone(stack.instance) } : {})
62 };
63 });
64 harness.simulation.applyBag(playerId, {
65 inventory,
66 equipment: emptyEquipment(),
67 vault: [],
68 vaultReserved: []
69 });
70}
71
72test("plant clippers use dual metal/leather tint and bar+leather recipe", () =>
73{
74 const def = itemDefinition(MetalItemId.bronzePlantClippers);
75 assert.equal(def.iconKey, "plant-clippers");
76 assert.equal(def.dualTintFromTier, true);
77 assert.equal(def.leatherTier, "softLeather");
78 const dual = resolveItemDualTint({ itemId: MetalItemId.bronzePlantClippers }, def);
79 assert.ok(dual?.shaft);
80 assert.ok(dual?.tip);
81 assert.equal(itemEquipSprite(MetalItemId.bronzePlantClippers), "item:plant-clippers:plant-clippers:equip");
82 assert.equal(def.equipment?.tintFromTier, true);
83 const equipTint = resolveEquipTint(MetalItemId.bronzePlantClippers);
84 assert.ok(equipTint);
85 assert.equal(resolveEquipDualTint({ itemId: MetalItemId.bronzePlantClippers }, def), null);
86 const recipe = recipeDefinition("plant-clippers-bronze");
87 assert.deepEqual(recipe.inputs, [
88 { itemId: ItemId.bronzeBar, quantity: 1 },
89 { itemId: LeatherItemId.softLeather, quantity: 1 }
90 ]);
91 assert.equal(recipe.stationId, CraftingStationId.anvil);
92});
93
94test("shears are magenta-tinted metal and craft from one bar", () =>
95{
96 const def = itemDefinition(MetalItemId.ironShears);
97 assert.equal(def.equipment.kind, EquipmentKind.shears);
98 assert.equal(def.tintFromTier, true);
99 assert.equal(def.dualTintFromTier, undefined);
100 const recipe = recipeDefinition("shears-iron");
101 assert.deepEqual(recipe.inputs, [{ itemId: ItemId.ironBar, quantity: 1 }]);
102 assert.equal(
103 recipesForStation(CraftingStationId.anvil).filter((r) => r.craftGroup === "shears").length,
104 6
105 );
106 assert.equal(recipeDefinition("shears-bronze"), null);
107});
108
109test("sheep and cashmere goat are shearable with fleece/shorn sprites", () =>
110{
111 const sheep = mobDefinition(MobId.sheep);
112 assert.equal(sheep.shearable.clothTier, ClothTier.wool);
113 assert.equal(mobSpriteName(MobId.sheep), "mob:sheep:fleece");
114 assert.equal(mobSpriteName(MobId.sheep, "shorn"), "mob:sheep:shorn");
115 const goat = mobDefinition(MobId.cashmereGoat);
116 assert.equal(goat.shearable.clothTier, ClothTier.cashmere);
117 assert.equal(mobSpriteName(MobId.cashmereGoat, "fleece"), "mob:cashmere-goat:fleece");
118});
119
120test("fairy uses own sprites, dual-channel kit, and random-element spell bolts", () =>
121{
122 const fairy = mobDefinition(MobId.fairy);
123 assert.equal(fairy.spriteKey, undefined);
124 assert.equal(mobSpriteName(MobId.fairy), "mob:fairy:fairy");
125 assert.equal(fairy.bloomBody, true);
126 assert.equal(fairy.dualChannelAppearance, true);
127 assert.equal(fairy.preferSpellAttack, true);
128 assert.equal(fairy.randomElementSpell, true);
129 assert.equal(fairy.fleeOnHitTiles, 2);
130 assert.equal(fairy.randomElementMelee, undefined);
131 assert.equal(fairy.lightRadiusTiles, 1);
132 assert.equal(fairy.frames, 2);
133 assert.equal(fairy.drops.length, 1);
134 assert.equal(fairy.drops[0].itemId, ClothItemId.fairySilk);
135 assert.equal(fairy.drops[0].chance, 0.7);
136 assert.equal(MetalItemId.bronzeShears, undefined);
137 assert.equal(itemDefinition(MetalItemId.ironShears)?.equipment?.kind, EquipmentKind.shears);
138});
139
140test("flax needs steel-or-better clippers; linen fibre is named Flax", () =>
141{
142 assert.equal(itemDefinition(ClothItemId.linen).displayName, "Flax");
143 assert.equal(metalTierForCloth(ClothTier.linen), MaterialTier.steel);
144 const skills = normalizePlayerSkills(null);
145 skills.xp[SkillId.herbalism] = totalXpForLevel(20);
146 const bronze = resolvePlantClippers({
147 inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }],
148 equipmentSlots: emptyEquipment().slots,
149 skills,
150 minClothTier: ClothTier.linen
151 });
152 assert.equal(bronze.ok, false);
153 const steel = resolvePlantClippers({
154 inventorySlots: [{ itemId: MetalItemId.steelPlantClippers, quantity: 1 }],
155 equipmentSlots: emptyEquipment().slots,
156 skills,
157 minClothTier: ClothTier.linen
158 });
159 assert.equal(steel.ok, true);
160 const plant = plantGatherForBlock({ typeId: BlockType.flaxPlant, state: "flax-plant" });
161 assert.equal(plant.kind, "flax");
162 assert.equal(plant.requiresClippers, true);
163 assert.equal(plant.minClothTier, "linen");
164});
165
166test("silk cocoons need adamantium-or-better clippers", () =>
167{
168 assert.equal(metalTierForCloth(ClothTier.silk), MaterialTier.adamantium);
169 assert.match(itemDefinition(ClothItemId.silk).description, /cocoon/i);
170 const skills = normalizePlayerSkills(null);
171 skills.xp[SkillId.herbalism] = totalXpForLevel(50);
172 const mithril = resolvePlantClippers({
173 inventorySlots: [{ itemId: MetalItemId.mithrilPlantClippers, quantity: 1 }],
174 equipmentSlots: emptyEquipment().slots,
175 skills,
176 minClothTier: ClothTier.silk
177 });
178 assert.equal(mithril.ok, false);
179 const adamantium = resolvePlantClippers({
180 inventorySlots: [{ itemId: MetalItemId.adamantiumPlantClippers, quantity: 1 }],
181 equipmentSlots: emptyEquipment().slots,
182 skills,
183 minClothTier: ClothTier.silk
184 });
185 assert.equal(adamantium.ok, true);
186 const plant = plantGatherForBlock({ typeId: BlockType.silkCocoon, state: "silk-cocoon" });
187 assert.equal(plant.kind, "silk");
188 assert.equal(plant.requiresClippers, true);
189 assert.equal(plant.minClothTier, "silk");
190});
191
192test("herb clippers require matching metal tier (sage → iron+)", () =>
193{
194 assert.equal(metalTierForHerbTier(2), MaterialTier.iron);
195 assert.equal(metalTierForHerbTier(7), MaterialTier.dragon);
196 const skills = normalizePlayerSkills(null);
197 skills.xp[SkillId.herbalism] = totalXpForLevel(10);
198 const bronze = resolvePlantClippers({
199 inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }],
200 equipmentSlots: emptyEquipment().slots,
201 skills,
202 minMetalTier: metalTierForHerbTier(2)
203 });
204 assert.equal(bronze.ok, false);
205 const iron = resolvePlantClippers({
206 inventorySlots: [{ itemId: MetalItemId.ironPlantClippers, quantity: 1 }],
207 equipmentSlots: emptyEquipment().slots,
208 skills,
209 minMetalTier: metalTierForHerbTier(2)
210 });
211 assert.equal(iron.ok, true);
212});
213
214test("shearing a sheep grants wool and sets shorn state", () =>
215{
216 const harness = makeSimulation({ workerCount: 1 });
217 try
218 {
219 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
220 const playerId = harness.simulation.spawnCharacter(1, character);
221 const worker = workerOf(harness.simulation, playerId);
222 setSkillLevel(worker, playerId, SkillId.herbalism, 10);
223 giveInventory(harness, playerId, [
224 { itemId: MetalItemId.ironShears, quantity: 1 }
225 ]);
226
227 const sheepId = harness.simulation.spawnEntity(mobComponents({
228 tileX: 9,
229 tileY: 8,
230 mobTypeId: MobId.sheep,
231 spawnerId: "test-sheep",
232 displayName: "Woolly"
233 }));
234
235 harness.simulation.enqueue(1, {
236 type: "shear",
237 characterId: character.character_id,
238 targetEntityId: sheepId
239 });
240 advanceMs(harness, 3500);
241
242 const creature = worker.world.ecs.get(
243 worker.world.ecs.handleOf(sheepId),
244 Component.creature
245 );
246 assert.equal(creature.fleeceState, "shorn");
247 assert.ok(creature.woolReadyAt > harness.now());
248 const bag = harness.simulation.bagOf(playerId);
249 const woolId = rawClothItemId(ClothTier.wool);
250 assert.ok(bag.inventory.some((s) => s?.itemId === woolId));
251 assert.equal(
252 clothExhaustDurationMs(ClothTier.wool),
253 tierRank(ClothTier.wool) * 10_000
254 );
255 }
256 finally
257 {
258 harness.close();
259 }
260});