BlueGit

timfa questward

Files

questward / server / tests / shearing.test.js

shearing.test.js

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