| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType, plantGatherForBlock } from "../../shared/definitions/tiles.js"; |
| 4 | import { CraftingStationId } from "../../shared/definitions/craftingStations.js"; |
| 5 | import { MetalItemId } from "../../shared/definitions/metalItems.js"; |
| 6 | import { LeatherItemId } from "../../shared/definitions/leatherItems.js"; |
| 7 | import { ClothItemId, rawClothItemId } from "../../shared/definitions/clothItems.js"; |
| 8 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 9 | import { |
| 10 | ClothTier, |
| 11 | MaterialTier, |
| 12 | metalTierForCloth, |
| 13 | clothExhaustDurationMs, |
| 14 | tierRank |
| 15 | } from "../../shared/definitions/materialTiers.js"; |
| 16 | import { MobId, mobDefinition, mobSpriteName } from "../../shared/definitions/mobs.js"; |
| 17 | import { EquipmentKind } from "../../shared/definitions/equipmentKinds.js"; |
| 18 | import { recipeDefinition, recipesForStation } from "../../shared/crafting/stationRecipes.js"; |
| 19 | import { resolveItemDualTint } from "../../shared/render/tint.js"; |
| 20 | import { resolvePlantClippers } from "../../shared/definitions/gathering.js"; |
| 21 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 22 | import { normalizePlayerSkills } from "../../shared/skills/state.js"; |
| 23 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 24 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 25 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 26 | import { Component } from "../src/ecs/components.js"; |
| 27 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 28 | |
| 29 | function workerOf (simulation, playerId) |
| 30 | { |
| 31 | const record = simulation.entityRecord(playerId); |
| 32 | return simulation.workers[record.workerId]; |
| 33 | } |
| 34 | |
| 35 | function 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 | |
| 45 | function 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 | |
| 52 | function 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 | |
| 71 | test("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 | |
| 88 | test("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 | |
| 103 | test("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 | |
| 114 | test("fairy reuses sprite art and drops fairy silk", () => |
| 115 | { |
| 116 | const fairy = mobDefinition(MobId.fairy); |
| 117 | assert.equal(fairy.spriteKey, "sprite"); |
| 118 | assert.equal(mobSpriteName(MobId.fairy), "mob:sprite:sprite"); |
| 119 | assert.equal(fairy.drops.length, 1); |
| 120 | assert.equal(fairy.drops[0].itemId, ClothItemId.fairySilk); |
| 121 | assert.equal(fairy.drops[0].chance, 0.7); |
| 122 | assert.equal(MetalItemId.bronzeShears, undefined); |
| 123 | assert.equal(itemDefinition(MetalItemId.ironShears)?.equipment?.kind, EquipmentKind.shears); |
| 124 | }); |
| 125 | |
| 126 | test("flax needs steel-or-better clippers; linen fibre is named Flax", () => |
| 127 | { |
| 128 | assert.equal(itemDefinition(ClothItemId.linen).displayName, "Flax"); |
| 129 | assert.equal(metalTierForCloth(ClothTier.linen), MaterialTier.steel); |
| 130 | const skills = normalizePlayerSkills(null); |
| 131 | skills.xp[SkillId.herbalism] = totalXpForLevel(20); |
| 132 | const bronze = resolvePlantClippers({ |
| 133 | inventorySlots: [{ itemId: MetalItemId.bronzePlantClippers, quantity: 1 }], |
| 134 | equipmentSlots: emptyEquipment().slots, |
| 135 | skills, |
| 136 | minClothTier: ClothTier.linen |
| 137 | }); |
| 138 | assert.equal(bronze.ok, false); |
| 139 | const steel = resolvePlantClippers({ |
| 140 | inventorySlots: [{ itemId: MetalItemId.steelPlantClippers, quantity: 1 }], |
| 141 | equipmentSlots: emptyEquipment().slots, |
| 142 | skills, |
| 143 | minClothTier: ClothTier.linen |
| 144 | }); |
| 145 | assert.equal(steel.ok, true); |
| 146 | const plant = plantGatherForBlock({ typeId: BlockType.flaxPlant, state: "flax-plant" }); |
| 147 | assert.equal(plant.kind, "flax"); |
| 148 | assert.equal(plant.requiresClippers, true); |
| 149 | assert.equal(plant.minClothTier, "linen"); |
| 150 | }); |
| 151 | |
| 152 | test("silk cocoons need adamantium-or-better clippers", () => |
| 153 | { |
| 154 | assert.equal(metalTierForCloth(ClothTier.silk), MaterialTier.adamantium); |
| 155 | assert.match(itemDefinition(ClothItemId.silk).description, /cocoon/i); |
| 156 | const skills = normalizePlayerSkills(null); |
| 157 | skills.xp[SkillId.herbalism] = totalXpForLevel(50); |
| 158 | const mithril = resolvePlantClippers({ |
| 159 | inventorySlots: [{ itemId: MetalItemId.mithrilPlantClippers, quantity: 1 }], |
| 160 | equipmentSlots: emptyEquipment().slots, |
| 161 | skills, |
| 162 | minClothTier: ClothTier.silk |
| 163 | }); |
| 164 | assert.equal(mithril.ok, false); |
| 165 | const adamantium = resolvePlantClippers({ |
| 166 | inventorySlots: [{ itemId: MetalItemId.adamantiumPlantClippers, quantity: 1 }], |
| 167 | equipmentSlots: emptyEquipment().slots, |
| 168 | skills, |
| 169 | minClothTier: ClothTier.silk |
| 170 | }); |
| 171 | assert.equal(adamantium.ok, true); |
| 172 | const plant = plantGatherForBlock({ typeId: BlockType.silkCocoon, state: "silk-cocoon" }); |
| 173 | assert.equal(plant.kind, "silk"); |
| 174 | assert.equal(plant.requiresClippers, true); |
| 175 | assert.equal(plant.minClothTier, "silk"); |
| 176 | }); |
| 177 | |
| 178 | test("shearing a sheep grants wool and sets shorn state", () => |
| 179 | { |
| 180 | const harness = makeSimulation({ workerCount: 1 }); |
| 181 | try |
| 182 | { |
| 183 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 184 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 185 | const worker = workerOf(harness.simulation, playerId); |
| 186 | setSkillLevel(worker, playerId, SkillId.herbalism, 10); |
| 187 | giveInventory(harness, playerId, [ |
| 188 | { itemId: MetalItemId.ironShears, quantity: 1 } |
| 189 | ]); |
| 190 | |
| 191 | const sheepId = harness.simulation.spawnEntity(mobComponents({ |
| 192 | tileX: 9, |
| 193 | tileY: 8, |
| 194 | mobTypeId: MobId.sheep, |
| 195 | spawnerId: "test-sheep", |
| 196 | displayName: "Woolly" |
| 197 | })); |
| 198 | |
| 199 | harness.simulation.enqueue(1, { |
| 200 | type: "shear", |
| 201 | characterId: character.character_id, |
| 202 | targetEntityId: sheepId |
| 203 | }); |
| 204 | advanceMs(harness, 3500); |
| 205 | |
| 206 | const creature = worker.world.ecs.get( |
| 207 | worker.world.ecs.handleOf(sheepId), |
| 208 | Component.creature |
| 209 | ); |
| 210 | assert.equal(creature.fleeceState, "shorn"); |
| 211 | assert.ok(creature.woolReadyAt > harness.now()); |
| 212 | const bag = harness.simulation.bagOf(playerId); |
| 213 | const woolId = rawClothItemId(ClothTier.wool); |
| 214 | assert.ok(bag.inventory.some((s) => s?.itemId === woolId)); |
| 215 | assert.equal( |
| 216 | clothExhaustDurationMs(ClothTier.wool), |
| 217 | tierRank(ClothTier.wool) * 10_000 |
| 218 | ); |
| 219 | } |
| 220 | finally |
| 221 | { |
| 222 | harness.close(); |
| 223 | } |
| 224 | }); |