diff --git a/packages/local-service/src/char/actions/action-table.ts b/packages/local-service/src/char/actions/action-table.ts index e9be31c..8240640 100644 --- a/packages/local-service/src/char/actions/action-table.ts +++ b/packages/local-service/src/char/actions/action-table.ts @@ -6,6 +6,7 @@ import type { CharActionHandler } from '../char-action.ts'; import { acceptEula } from './agreement-actions.ts'; import { bulkTrainCrews, trainCrew } from './crew-actions.ts'; import { buyAllModificationsAction, buyModificationAction, buySparesAction, enableModificationsAction } from './modification-actions.ts'; +import { buyResourceAction } from './decor-actions.ts'; import { saveDecorLayoutsAction } from './decor-layout-actions.ts'; import { entitlementsFull, initialMeta, legacyUserData } from './online-init-actions.ts'; import { priceEx } from './price-actions.ts'; @@ -38,6 +39,7 @@ export function actionTable(deps: ActionDependencies): ReadonlyMap { + const name = stringParam(payload, 'name'); + const type = stringParam(payload, 'type'); + if (name === undefined || type === undefined || !isDecorKind(type)) return undefined; + const key = decorKey(type, name, stringParam(payload, 'unitName') ?? ''); + const offered = { silverLions: intParam(payload, 'cost') ?? 0, goldenEagles: intParam(payload, 'costGold') ?? 0 }; + return (profile) => buyDecoration(profile, rules.decor, type, key, offered); + }); +} diff --git a/packages/local-service/src/char/actions/price-actions.ts b/packages/local-service/src/char/actions/price-actions.ts index 35484f3..c5c0e23 100644 --- a/packages/local-service/src/char/actions/price-actions.ts +++ b/packages/local-service/src/char/actions/price-actions.ts @@ -9,6 +9,6 @@ import { priceTable } from '../../research/price-table.ts'; import type { ResearchRules } from '../../research/research-rules.ts'; export function priceEx(rules: ResearchRules): CharActionHandler { - const table = priceTable(rules.techTree, rules.equipment); + const table = priceTable(rules.techTree, rules.equipment, rules.decor); return () => ({ kind: 'data', blk: table }); } diff --git a/packages/local-service/src/decor/decor-purchase.ts b/packages/local-service/src/decor/decor-purchase.ts new file mode 100644 index 0000000..51f0cc3 --- /dev/null +++ b/packages/local-service/src/decor/decor-purchase.ts @@ -0,0 +1,48 @@ +/** + * Buying decorations: cln_buy_resource with name:t, type:t (skin, decal, + * attachable), unitName:t, cost:i, costGold:i (scripts customization/decoratorAcquire.nut). + * A skin's key is "/". The decoration must be for sale (a positive price in + * config/skins.blk, decals.blk or attachable.blk), not owned yet, the offered price must match; + * it then goes into resourcesInfo. + */ +import type { BlkBlock } from '@wto/blk'; +import type { DecorKind, DecorPrice } from '../game-data/datasets/decor-catalogue.ts'; +import { canAfford, pay, type Price } from '../profile/profile-currency.ts'; +import { addResource, ownsResource } from '../profile/profile-resources.ts'; +import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; + +export class DecorShop { + readonly #prices: ReadonlyMap; + + constructor(prices: readonly DecorPrice[]) { + this.#prices = new Map(prices.map((price) => [`${price.kind}:${price.key}`, price])); + } + + all(): Iterable { + return this.#prices.values(); + } + + price(kind: DecorKind, key: string): DecorPrice | undefined { + return this.#prices.get(`${kind}:${key}`); + } +} + +export function decorKey(kind: DecorKind, name: string, vehicle: string): string { + return kind === 'skin' ? `${vehicle}/${name}` : name; +} + +export function isDecorKind(type: string): type is DecorKind { + return type === 'skin' || type === 'decal' || type === 'attachable'; +} + +export function buyDecoration(profile: BlkBlock, shop: DecorShop, kind: DecorKind, key: string, offered: Price): RuleOutcome { + const forSale = shop.price(kind, key); + if (!forSale) return refused('NOT_AVAILABLE'); + if (ownsResource(profile, key)) return refused('NOT_AVAILABLE'); + const price: Price = { silverLions: forSale.silverLions, goldenEagles: forSale.goldenEagles }; + if (price.silverLions !== offered.silverLions || price.goldenEagles !== offered.goldenEagles) return refused('PRICE_CHANGED'); + if (!canAfford(profile, price)) return refused('NOT_ENOUGH_MONEY'); + pay(profile, price); + addResource(profile, key, price); + return ACCEPTED; +} diff --git a/packages/local-service/src/game-data/datasets/decor-catalogue.ts b/packages/local-service/src/game-data/datasets/decor-catalogue.ts new file mode 100644 index 0000000..dd20658 --- /dev/null +++ b/packages/local-service/src/game-data/datasets/decor-catalogue.ts @@ -0,0 +1,44 @@ +/** + * Dataset: the decorations that can be bought. + * + * char.vromfs.bin config/skins.blk, config/decals.blk, config/attachable.blk: one block per + * decoration, named like the client keys it (skins "/", decals and attachables by + * name), with + * cost:i silver lions (missing = -1) + * costGold:i golden eagles (missing = -1; -1 means not for sale) + * unlock:t an unlock that also gives it + * Only decorations with a positive price are recorded: the others are either free (the client + * counts them as owned, aces.exe 0x95e1c5) or not for sale. + */ +import type { BlkBlock } from '@wto/blk'; +import { numberField } from './blk-fields.ts'; +import type { Dataset } from './dataset.ts'; + +export type DecorKind = 'skin' | 'decal' | 'attachable'; + +export interface DecorPrice { + readonly kind: DecorKind; + /** The client's key: "/" for skins, the name otherwise. */ + readonly key: string; + readonly silverLions: number; + readonly goldenEagles: number; +} + +const SOURCES: readonly { kind: DecorKind; path: string }[] = [ + { kind: 'skin', path: 'config/skins.blk' }, + { kind: 'decal', path: 'config/decals.blk' }, + { kind: 'attachable', path: 'config/attachable.blk' }, +]; + +export const decorCatalogue: Dataset = { + id: 'decor-catalogue', + version: 1, + extract: (source) => SOURCES.flatMap(({ kind, path }) => + source.readBlk('char.vromfs.bin', path).blocks.flatMap((entry) => priceOf(kind, entry))), +}; + +function priceOf(kind: DecorKind, entry: BlkBlock): DecorPrice[] { + const silverLions = Math.max(0, numberField(entry, 'cost') ?? 0); + const goldenEagles = Math.max(0, numberField(entry, 'costGold') ?? 0); + return silverLions > 0 || goldenEagles > 0 ? [{ kind, key: entry.name, silverLions, goldenEagles }] : []; +} diff --git a/packages/local-service/src/profile/profile-resources.ts b/packages/local-service/src/profile/profile-resources.ts new file mode 100644 index 0000000..8cacc34 --- /dev/null +++ b/packages/local-service/src/profile/profile-resources.ts @@ -0,0 +1,26 @@ +/** + * Bought decorations in the profile: + * + * resourcesInfo{ core{ "us_m22_locust/us_camo_olive"{ spendGold:i=200 } cow_skull{ spendGold:i=500 } } } + * + * A generic name-keyed storage (FastStrMapStorageBase, aces.exe 0x93da88): one block per bought + * decoration under core, named by the client's key; having a block means owned (player_have_skin, + * player_have_decal, player_have_attachable -> 0x95dfc0). Each entry's params are read by name + * (0x95eaa0): flags:i, spendWP:i, spendGold:i, exteriorInfoParams. + */ +import { ensureBlock, findBlock, setParam, type BlkBlock } from '@wto/blk'; + +const RESOURCES = 'resourcesInfo'; +const CORE = 'core'; + +export function ownsResource(profile: BlkBlock, key: string): boolean { + const resources = findBlock(profile, RESOURCES); + const core = resources && findBlock(resources, CORE); + return core !== undefined && findBlock(core, key) !== undefined; +} + +export function addResource(profile: BlkBlock, key: string, spent: { silverLions: number; goldenEagles: number }): void { + const entry = ensureBlock(ensureBlock(ensureBlock(profile, RESOURCES), CORE), key); + if (spent.silverLions) setParam(entry, 'spendWP', { type: 'int', value: spent.silverLions }); + if (spent.goldenEagles) setParam(entry, 'spendGold', { type: 'int', value: spent.goldenEagles }); +} diff --git a/packages/local-service/src/research/price-table.ts b/packages/local-service/src/research/price-table.ts index 3e1e9be..c979f37 100644 --- a/packages/local-service/src/research/price-table.ts +++ b/packages/local-service/src/research/price-table.ts @@ -18,22 +18,27 @@ * spare{ cost:i costGold:i } * } * } + * skins{ "/"{ cost:i costGold:i } } decorations (get_skin_cost_*, 0x17a8910...) + * decals{ { ... } } + * attachable{ { ... } } * } * * Every entry is read as cost:i (silver lions), costGold:i (golden eagles), discount:i (percent * off, not sent: no discounts), each 0 when missing (so zero prices are left out); openCostGold - * is the outright price of a modification before research progress (0xec0910). Anything the lookup does not find costs 0 for the client, which - * then offers it at 0 (lab round 20). The prices are config/wpcost.blk value and costGold, the - * same the purchase rules charge. + * is the outright price of a modification before research progress (0xec0910). Anything the + * lookup does not find costs 0 for the client, which then offers it at 0 (lab round 20). The + * prices are config/wpcost.blk value and costGold (decorations: config/skins.blk, decals.blk, + * attachable.blk), the same the purchase rules charge. */ import type { BlkBlock, BlkParam } from '@wto/blk'; +import type { DecorShop } from '../decor/decor-purchase.ts'; import type { EquipmentCatalogue } from '../modifications/equipment-catalogue.ts'; import type { TechTree } from './tech-tree.ts'; /** The top-level block every client lookup falls back to (0x908e28). */ const COMMON_SECTION = 'Common'; -export function priceTable(techTree: TechTree, equipment: EquipmentCatalogue): BlkBlock { +export function priceTable(techTree: TechTree, equipment: EquipmentCatalogue, decor: DecorShop): BlkBlock { const vehicles: BlkBlock[] = []; for (const vehicle of techTree.all()) { const entry: BlkBlock = { name: vehicle.name, params: priceParams(vehicle.silverLions, vehicle.goldenEagles), blocks: [] }; @@ -52,7 +57,7 @@ export function priceTable(techTree: TechTree, equipment: EquipmentCatalogue): B } if (entry.blocks.length || vehicle.silverLions > 0 || vehicle.goldenEagles > 0) vehicles.push(entry); } - const common: BlkBlock = { name: COMMON_SECTION, params: [], blocks: [{ name: 'aircrafts', params: [], blocks: vehicles }] }; + const common: BlkBlock = { name: COMMON_SECTION, params: [], blocks: [{ name: 'aircrafts', params: [], blocks: vehicles }, ...decorSections(decor)] }; return { name: '', params: [], blocks: [common] }; } @@ -67,3 +72,17 @@ function priceParams(silverLions: number, goldenEagles: number): BlkParam[] { function intParam(name: string, value: number): BlkParam { return { name, value: { type: 'int', value } }; } + +/** The price-table section each decoration kind is looked up in. */ +const DECOR_SECTIONS = { skin: 'skins', decal: 'decals', attachable: 'attachable' } as const; + +function decorSections(decor: DecorShop): BlkBlock[] { + const sections = new Map(); + for (const price of decor.all()) { + const name = DECOR_SECTIONS[price.kind]; + let section = sections.get(name); + if (!section) sections.set(name, section = { name, params: [], blocks: [] }); + section.blocks.push({ name: price.key, params: priceParams(price.silverLions, price.goldenEagles), blocks: [] }); + } + return [...sections.values()]; +} diff --git a/packages/local-service/src/research/research-rules.ts b/packages/local-service/src/research/research-rules.ts index a6db559..87c0227 100644 --- a/packages/local-service/src/research/research-rules.ts +++ b/packages/local-service/src/research/research-rules.ts @@ -6,7 +6,9 @@ import type { DatasetCache } from '../game-data/datasets/dataset-cache.ts'; import { crewSlotPrices, eraRequirements, type CrewSlotPrices, type EraRequirements } from '../game-data/datasets/rank-settings.ts'; import { researchPointsPerGoldenEagle } from '../game-data/datasets/research-point-rate.ts'; import { vehicleCatalogue } from '../game-data/datasets/vehicle-catalogue.ts'; +import { decorCatalogue } from '../game-data/datasets/decor-catalogue.ts'; import { vehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts'; +import { DecorShop } from '../decor/decor-purchase.ts'; import { vehicleUnlocks, type UnlockDefinition } from '../game-data/datasets/vehicle-unlocks.ts'; import { EquipmentCatalogue } from '../modifications/equipment-catalogue.ts'; import { TechTree } from './tech-tree.ts'; @@ -19,6 +21,8 @@ export interface ResearchRules { readonly researchPointsPerGoldenEagle: number; /** Modifications, ammunition, weapon presets and spares of every vehicle. */ readonly equipment: EquipmentCatalogue; + /** Decorations for sale. */ + readonly decor: DecorShop; } export function loadResearchRules(datasets: DatasetCache): ResearchRules { @@ -29,5 +33,6 @@ export function loadResearchRules(datasets: DatasetCache): ResearchRules { crewSlotPrices: datasets.load(crewSlotPrices), researchPointsPerGoldenEagle: datasets.load(researchPointsPerGoldenEagle), equipment: new EquipmentCatalogue(datasets.load(vehicleEquipment)), + decor: new DecorShop(datasets.load(decorCatalogue)), }; }