BlueGit

timfa questward

Files

questward / server / tests / shearing.test.js

shearing.test.js

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