BlueGit

timfa questward

Files

questward / server / tests / shearing.test.js

shearing.test.js

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