/** * Classic-item `autoConsume` wrappers (`810001+`), recycled craft fragments and re-synthesis * generators. The fragments are the distinct recycleItemResult * values of config/items.blk; the recipes are tuning (tuning/recycling.jsonc). */ import type { ItemDefinition } from '../game-data/datasets/item-catalogue.ts'; import { makeLocalizedItemDef, type CouponAction, type GeneratorDefinition, type GeneratorDrop, type GeneratorRecipe, type LocalizedItemDef, } from './itemdef-types.ts'; import type { RecyclingTuning } from './recycling-tuning.ts'; const RECYCLE_SINGLE_OFFSET = 20; const RECYCLE_GENERIC_GENERATOR_ID = 299030; const FIRST_WRAPPER_ID = 810001; export interface RecyclingBuildResult { readonly defs: readonly LocalizedItemDef[]; readonly coupons: ReadonlyArray; readonly generators: readonly GeneratorDefinition[]; readonly wrappersByPart: ReadonlyMap; } export function buildRecyclingDefs(classicItems: readonly ItemDefinition[], tuning: RecyclingTuning): RecyclingBuildResult { const defs: LocalizedItemDef[] = []; const coupons: Array = []; const generators: GeneratorDefinition[] = []; const partIds = [...new Set(classicItems.flatMap((item) => (item.recycleItemResult === undefined ? [] : [item.recycleItemResult])))].sort((a, b) => a - b); const wrappersByPart = buildClassicWrappers(classicItems, partIds, tuning, defs, coupons); const allWrappers = [...new Set([...wrappersByPart.values()].flat())]; for (const partId of partIds) { registerSinglePartSynthesizer(partId, wrappersByPart.get(partId) ?? allWrappers, tuning, defs, generators); } registerMixedSynthesizer(partIds, allWrappers, defs, generators); return { defs, coupons, generators, wrappersByPart }; } function registerSinglePartSynthesizer( partId: number, pool: readonly number[], tuning: RecyclingTuning, defs: LocalizedItemDef[], generators: GeneratorDefinition[], ): void { const singleGenId = partId + RECYCLE_SINGLE_OFFSET; const title = tuning.partNames.get(partId) ?? { en: `Recycled Part #${partId}`, zh: `回收组件 #${partId}` }; const needed = tuning.partsPerItem; defs.push(makeLocalizedItemDef(partId, { type: 'item', marketable: false, item_quality: 1, used_to_create: `${singleGenId}x1;${RECYCLE_GENERIC_GENERATOR_ID}x1`, tags: 'type:craft_part;hideMarketablePropDesc:yes', icon_url: '!#ui/gameuiskin#item_type_craftpart.svg', }, title, { en: `Obtained by recycling classic items; ${needed} fragments craft a new random item.`, zh: `回收经典道具所得;每 ${needed} 个组件可重新合成 1 件随机道具。`, })); defs.push(makeLocalizedItemDef(singleGenId, { type: 'generator', marketable: false, item_quality: 1, exchange: `${partId}x${needed}`, bundle: pool.map((id) => `${id}x1`).join(';'), tags: 'devItem:yes', icon_url: '!#ui/gameuiskin#item_type_craftpart.svg', }, { en: 'Item Synthesizer', zh: '道具合成器' }, { en: '', zh: '' })); generators.push({ itemdefId: singleGenId, isChestGenerator: false, recipes: [{ components: [{ itemdefId: partId, quantity: needed }] }], drops: pool.map((id): GeneratorDrop => ({ itemdefId: id, weight: 10 })), }); } function registerMixedSynthesizer( partIds: readonly number[], allWrappers: readonly number[], defs: LocalizedItemDef[], generators: GeneratorDefinition[], ): void { const { pairRecipes, pairStrings } = buildPairRecipes(partIds); defs.push(makeLocalizedItemDef(RECYCLE_GENERIC_GENERATOR_ID, { type: 'generator', marketable: false, item_quality: 1, exchange: pairStrings.join(';'), bundle: allWrappers.map((id) => `${id}x1`).join(';'), tags: 'devItem:yes', icon_url: '!#ui/gameuiskin#item_type_craftpart.svg', }, { en: 'Mixed Item Synthesizer', zh: '混搭道具合成器' }, { en: '', zh: '' })); generators.push({ itemdefId: RECYCLE_GENERIC_GENERATOR_ID, isChestGenerator: false, recipes: pairRecipes, drops: allWrappers.map((id): GeneratorDrop => ({ itemdefId: id, weight: 10 })), }); } function buildClassicWrappers( classicItems: readonly ItemDefinition[], partIds: readonly number[], tuning: RecyclingTuning, defs: LocalizedItemDef[], coupons: Array, ): ReadonlyMap { const byRecyclePart = new Map(); for (const partId of partIds) byRecyclePart.set(partId, []); // Sort deterministically by item id so wrapper IDs and drop pools do not depend on BLK order. const sortedPermanent = classicItems .filter((item) => item.expiresAt === undefined && item.type !== 'personalDiscounts') .sort((a, b) => a.name.localeCompare(b.name)); const preferredByPart = new Map(); for (const item of sortedPermanent) { const partId = item.recycleItemResult; if (partId === undefined) continue; let bucket = preferredByPart.get(partId); if (!bucket) preferredByPart.set(partId, bucket = []); if (bucket.length < tuning.itemsPerPart) bucket.push(item); } let nextWrapperId = FIRST_WRAPPER_ID; for (const partId of partIds) { const targetList = byRecyclePart.get(partId) ?? []; for (const def of preferredByPart.get(partId) ?? []) { const wrapperId = nextWrapperId++; const count = tuning.piecesPerItem.get(def.name) ?? 1; defs.push(makeLocalizedItemDef(wrapperId, { type: 'item', marketable: false, item_quality: 1, tags: 'type:internal_item;autoConsume:yes;showAsContentItem:yes;hideMarketablePropDesc:yes', meta: `item:t="${def.name}"\ncount:i=${count}`, icon_url: '!#ui/gameuiskin#item_type_trophies.svg', }, { en: def.name, zh: def.name }, { en: '', zh: '' })); coupons.push([wrapperId, { kind: 'classic-item', itemName: def.name, count }]); targetList.push(wrapperId); } byRecyclePart.set(partId, targetList); } const fallbackPool = tuning.fallbackParts.flatMap((partId) => byRecyclePart.get(partId) ?? []); for (const partId of partIds) { if ((byRecyclePart.get(partId)?.length ?? 0) === 0) byRecyclePart.set(partId, fallbackPool); } return byRecyclePart; } function buildPairRecipes(partIds: readonly number[]): { readonly pairRecipes: readonly GeneratorRecipe[]; readonly pairStrings: readonly string[] } { const pairRecipes: GeneratorRecipe[] = []; const pairStrings: string[] = []; for (let i = 0; i < partIds.length; i += 1) { for (let j = i + 1; j < partIds.length; j += 1) { const a = partIds[i]!; const b = partIds[j]!; pairRecipes.push({ components: [{ itemdefId: a, quantity: 1 }, { itemdefId: b, quantity: 1 }] }); pairStrings.push(`${a}x1,${b}x1`); } } return { pairRecipes, pairStrings }; }