| 1 | import { ItemId, itemDefinition } from "../definitions/items.js"; |
| 2 | import { ItemFamilyId } from "../definitions/tierFamilies.js"; |
| 3 | import { HerbItemId } from "../definitions/herbs.js"; |
| 4 | import { CRAFT_RECIPES, recipeStationIds } from "../crafting/stationRecipes.js"; |
| 5 | import |
| 6 | { |
| 7 | CraftOriginVerb, |
| 8 | GatherOriginVerb, |
| 9 | craftOrigin, |
| 10 | craftOriginVerbForStation, |
| 11 | gatherOrigin |
| 12 | } from "../items/origin.js"; |
| 13 | import { QUESTWARD_GIFT_AUTHOR } from "./giftLetters.js"; |
| 14 | |
| 15 | const HERB_ITEM_IDS = new Set(Object.values(HerbItemId)); |
| 16 | |
| 17 | /** @type {Map<number, string>|null} */ |
| 18 | let craftStationByOutputId = null; |
| 19 | |
| 20 | function craftStationForOutput (itemId) |
| 21 | { |
| 22 | if (!craftStationByOutputId) |
| 23 | { |
| 24 | craftStationByOutputId = new Map(); |
| 25 | for (const recipe of CRAFT_RECIPES) |
| 26 | { |
| 27 | const outId = recipe?.output?.itemId | 0; |
| 28 | if (!outId || craftStationByOutputId.has(outId)) |
| 29 | { continue; } |
| 30 | const stationId = recipeStationIds(recipe)[0]; |
| 31 | if (stationId) |
| 32 | { craftStationByOutputId.set(outId, stationId); } |
| 33 | } |
| 34 | } |
| 35 | return craftStationByOutputId.get(itemId | 0) ?? null; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Natural player-style origin for an admin gift, attributed to John Questward |
| 40 | * (e.g. `Smithed by John Questward`, `Mined by John Questward`). |
| 41 | */ |
| 42 | export function giftItemOrigin (itemId, displayName = QUESTWARD_GIFT_AUTHOR) |
| 43 | { |
| 44 | const name = String(displayName ?? "").trim() || QUESTWARD_GIFT_AUTHOR; |
| 45 | const id = itemId | 0; |
| 46 | const def = itemDefinition(id); |
| 47 | const key = String(def?.key ?? ""); |
| 48 | const familyId = def?.familyId ?? null; |
| 49 | |
| 50 | if ( |
| 51 | familyId === ItemFamilyId.ore |
| 52 | || def?.defaultState === "ore" |
| 53 | || key.endsWith("-ore") |
| 54 | || key === "coal" |
| 55 | ) |
| 56 | { return gatherOrigin(GatherOriginVerb.mined, name); } |
| 57 | |
| 58 | if (familyId === ItemFamilyId.log || def?.defaultState === "log" || key.endsWith("-log")) |
| 59 | { return gatherOrigin(GatherOriginVerb.felled, name); } |
| 60 | |
| 61 | if (def?.fishSpeciesId && (def.cookState === "raw" || key.startsWith("uncooked-"))) |
| 62 | { return gatherOrigin(GatherOriginVerb.caught, name); } |
| 63 | |
| 64 | if (HERB_ITEM_IDS.has(id) || id === ItemId.flower) |
| 65 | { return gatherOrigin(GatherOriginVerb.picked, name); } |
| 66 | |
| 67 | if (def?.defaultState === "wool") |
| 68 | { return gatherOrigin(GatherOriginVerb.sheared, name); } |
| 69 | |
| 70 | if (id === ItemId.potion || def?.potion) |
| 71 | { return craftOrigin(CraftOriginVerb.brewed, name); } |
| 72 | |
| 73 | if (id === ItemId.dye) |
| 74 | { return craftOrigin(CraftOriginVerb.mixed, name); } |
| 75 | |
| 76 | const stationId = craftStationForOutput(id); |
| 77 | if (stationId) |
| 78 | { return craftOrigin(craftOriginVerbForStation(stationId), name); } |
| 79 | |
| 80 | return craftOrigin(CraftOriginVerb.made, name); |
| 81 | } |