From 71ba962e75507c52a20ba3de19798baf0dc63f7d Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:14:00 +0800 Subject: [PATCH] feat(equipment): stock newly unlocked ammunition and consumables Extract weapon prerequisites, default equipment states, and exclusive groups. Grant eligible zero-cost modifications in dependency order and initialize newly unlocked consumables and weapon presets at capacity. When automatic restocking is enabled, replenish existing stock subject to the Silver Lions balance. Enforce purchase prerequisites and exclusive equipment groups. Type checking, patch checks, and local data extraction and restocking checks passed. In-game acceptance is pending. --- .../src/char/actions/action-table.ts | 2 +- .../src/char/actions/modification-actions.ts | 4 +- .../game-data/datasets/vehicle-equipment.ts | 79 ++++++++-- .../src/modifications/ammo-refill.ts | 136 ++++++++++++++++++ .../src/modifications/equipment-catalogue.ts | 13 +- .../modifications/modification-purchase.ts | 32 ++++- .../src/modifications/module-state.ts | 33 ++--- .../src/modifications/module-status.ts | 6 +- .../src/modifications/weapon-purchase.ts | 3 +- .../src/profile/profile-client-settings.ts | 9 ++ 10 files changed, 268 insertions(+), 49 deletions(-) create mode 100644 packages/local-service/src/modifications/ammo-refill.ts diff --git a/packages/local-service/src/char/actions/action-table.ts b/packages/local-service/src/char/actions/action-table.ts index b9dda17..0213852 100644 --- a/packages/local-service/src/char/actions/action-table.ts +++ b/packages/local-service/src/char/actions/action-table.ts @@ -119,7 +119,7 @@ function coreActionEntries(deps: ActionDependencies): Array<[string, CharActionH ['cln_buy_spare_aircrafts', buySparesAction(deps.profiles, deps.accounts, deps.equipment)], ['cln_buy_unlock', buyUnlockAction(deps.profiles, deps.accounts, deps.unlocks)], ['cln_buy_weapon', buyWeaponsAction(deps.profiles, deps.accounts, deps.equipment)], - ['cln_enable_modifications', enableModificationsAction(deps.profiles, deps.accounts)], + ['cln_enable_modifications', enableModificationsAction(deps.profiles, deps.accounts, deps.equipment)], ['cln_upgrade_crew', upgradeCrewAction(deps.profiles, deps.accounts, deps.crew)], ['cln_purchase_skill_points', purchaseSkillPointsAction(deps.profiles, deps.accounts, deps.crew)], ['cln_specialize_crew', specializeCrewAction(deps.profiles, deps.accounts, deps.crew)], diff --git a/packages/local-service/src/char/actions/modification-actions.ts b/packages/local-service/src/char/actions/modification-actions.ts index 3cb4ebd..37fc2be 100644 --- a/packages/local-service/src/char/actions/modification-actions.ts +++ b/packages/local-service/src/char/actions/modification-actions.ts @@ -68,11 +68,11 @@ export function buyWeaponsAction(profiles: PlayerProfiles, accounts: AccountStor }); } -export function enableModificationsAction(profiles: PlayerProfiles, accounts: AccountStore): CharActionHandler { +export function enableModificationsAction(profiles: PlayerProfiles, accounts: AccountStore, equipment: EquipmentCatalogue): CharActionHandler { return ruleAction(profiles, accounts, (payload) => { const switches = payload.blocks.flatMap((vehicle) => vehicle.params.flatMap((param) => param.value.type === 'bool' ? [{ vehicle: vehicle.name, modification: param.name, enabled: param.value.value }] : [])); - return (profile) => enableModifications(profile, switches); + return (profile) => enableModifications(profile, equipment, switches); }); } diff --git a/packages/local-service/src/game-data/datasets/vehicle-equipment.ts b/packages/local-service/src/game-data/datasets/vehicle-equipment.ts index 4b5fa76..8cdd732 100644 --- a/packages/local-service/src/game-data/datasets/vehicle-equipment.ts +++ b/packages/local-service/src/game-data/datasets/vehicle-equipment.ts @@ -16,10 +16,11 @@ */ import type { BlkBlock } from '@wto/blk'; import { findBlock } from '@wto/blk'; -import { numberField, stringList } from './blk-fields.ts'; +import { boolField, numberField, stringField, stringList } from './blk-fields.ts'; import type { Dataset } from './dataset.ts'; const VEHICLE_ECONOMY = 'config/wpcost.blk'; +const MODIFICATIONS_CONFIG = 'config/modifications.blk'; /** wpcost.blk has needBuyToOpenNextInTier1..4; the modification tree has tiers 1..5 at most. */ const TIER_REQUIREMENT_COUNT = 4; @@ -38,6 +39,10 @@ export interface ModificationFacts { readonly maxCount: number; /** ammoCount present: an ammunition belt or shell type. */ readonly ammunition: boolean; + /** Initial enabled state when acquired (`turn_it_off` / `isEnabledByDefaultForAddMrank`). */ + readonly defaultEnabled: boolean; + /** Non-bullet `group` name when modifications in the same group are mutually exclusive. */ + readonly exclusiveGroup?: string; } export interface WeaponFacts { @@ -45,6 +50,8 @@ export interface WeaponFacts { /** value: silver lions per piece. */ readonly silverLions: number; readonly goldenEagles: number; + /** reqModification (direct + inherited from `sum_weapons` containers, initUnitWeapons.nut). */ + readonly requires: readonly string[]; /** maxCount; 0 when the preset is not bought by the piece. */ readonly maxCount: number; } @@ -59,26 +66,58 @@ export interface VehicleEquipment { readonly spare?: { readonly silverLions: number; readonly goldenEagles: number }; } +interface GlobalModificationMeta { + readonly turnOffByDefault: boolean; + readonly bulletGroup: boolean; + readonly group?: string; +} + export const vehicleEquipment: Dataset = { id: 'vehicle-equipment', - version: 2, - extract: (source) => source.readBlk('char.vromfs.bin', VEHICLE_ECONOMY).blocks.map(equipmentOf), + version: 3, + extract: (source) => { + const modMeta = readGlobalModificationMeta(source.readBlk('char.vromfs.bin', MODIFICATIONS_CONFIG)); + return source.readBlk('char.vromfs.bin', VEHICLE_ECONOMY).blocks.map((vehicle) => equipmentOf(vehicle, modMeta)); + }, }; -function equipmentOf(vehicle: BlkBlock): VehicleEquipment { +function readGlobalModificationMeta(configRoot: BlkBlock): ReadonlyMap { + const modsBlock = findBlock(configRoot, 'modifications'); + const map = new Map(); + for (const entry of modsBlock?.blocks ?? []) { + const effects = findBlock(entry, 'effects'); + const bulletGroup = Boolean( + effects?.params.some((p) => p.name === 'bulletMod' || p.name === 'additiveBulletMod') + || effects?.blocks.some((b) => b.name === 'bulletMod' || b.name === 'additiveBulletMod'), + ); + const group = stringField(entry, 'group'); + map.set(entry.name, { + turnOffByDefault: boolField(entry, 'turn_it_off') === true, + bulletGroup, + ...(group !== undefined ? { group } : {}), + }); + } + return map; +} + +function equipmentOf(vehicle: BlkBlock, modMeta: ReadonlyMap): VehicleEquipment { const tierRequirements: number[] = []; for (let tier = 1; tier <= TIER_REQUIREMENT_COUNT; tier++) tierRequirements.push(numberField(vehicle, `needBuyToOpenNextInTier${tier}`) ?? 0); const spare = findBlock(vehicle, 'spare'); return { vehicle: vehicle.name, tierRequirements, - modifications: (findBlock(vehicle, 'modifications')?.blocks ?? []).map(modificationOf), - weapons: (findBlock(vehicle, 'weapons')?.blocks ?? []).map(weaponOf), + modifications: (findBlock(vehicle, 'modifications')?.blocks ?? []).map((mod) => modificationOf(mod, modMeta.get(mod.name))), + weapons: weaponsOf(findBlock(vehicle, 'weapons')), ...(spare ? { spare: { silverLions: numberField(spare, 'value') ?? 0, goldenEagles: numberField(spare, 'costGold') ?? 0 } } : {}), }; } -function modificationOf(modification: BlkBlock): ModificationFacts { +function modificationOf(modification: BlkBlock, meta: GlobalModificationMeta | undefined): ModificationFacts { + const ammunition = numberField(modification, 'ammoCount') !== undefined; + const defaultEnabled = !(meta?.turnOffByDefault ?? false) && (boolField(modification, 'isEnabledByDefaultForAddMrank') ?? true); + const group = stringField(modification, 'group') ?? meta?.group; + const exclusiveGroup = !ammunition && !(meta?.bulletGroup ?? false) ? group : undefined; return { name: modification.name, tier: numberField(modification, 'tier') ?? 0, @@ -88,15 +127,37 @@ function modificationOf(modification: BlkBlock): ModificationFacts { openGoldenEagles: numberField(modification, 'openCostGold') ?? 0, requires: stringList(modification, 'reqModification'), maxCount: numberField(modification, 'maxCount') ?? 0, - ammunition: numberField(modification, 'ammoCount') !== undefined, + ammunition, + defaultEnabled, + ...(exclusiveGroup !== undefined ? { exclusiveGroup } : {}), }; } -function weaponOf(entry: BlkBlock): WeaponFacts { +function weaponsOf(weaponsBlock: BlkBlock | undefined): WeaponFacts[] { + if (!weaponsBlock) return []; + const containerRequires = new Map(); + for (const entry of weaponsBlock.blocks) { + if (boolField(entry, 'isWeaponForCustomSlot') === true) { + containerRequires.set(entry.name, stringList(entry, 'reqModification')); + } + } + return weaponsBlock.blocks + .filter((entry) => boolField(entry, 'isWeaponForCustomSlot') !== true) + .map((entry) => weaponOf(entry, containerRequires)); +} + +function weaponOf(entry: BlkBlock, containerRequires: ReadonlyMap): WeaponFacts { + const requires = [...stringList(entry, 'reqModification')]; + for (const param of findBlock(entry, 'sum_weapons')?.params ?? []) { + for (const req of containerRequires.get(param.name) ?? []) { + if (!requires.includes(req)) requires.push(req); + } + } return { name: entry.name, silverLions: numberField(entry, 'value') ?? 0, goldenEagles: numberField(entry, 'costGold') ?? 0, + requires, maxCount: numberField(entry, 'maxCount') ?? 0, }; } diff --git a/packages/local-service/src/modifications/ammo-refill.ts b/packages/local-service/src/modifications/ammo-refill.ts new file mode 100644 index 0000000..0ef6c18 --- /dev/null +++ b/packages/local-service/src/modifications/ammo-refill.ts @@ -0,0 +1,136 @@ +/** + * Initial full stocking and `autoRefillWeapons` Silver-Lion top-up for an owned vehicle's + * modifications, ammunition belts/shells, expendables (`tank_medical_kit_expendable`), and + * secondary weapon presets. + * + * Rules: + * 1. Zero-cost modifications (`isFree`) whose `requires` are all owned cannot be bought by the + * client (`weaponsPurchase.nut` `canBuyItem` rejects zero cost) and receive `count = 1` in + * name + topological order, honoring `defaultEnabled` and non-bullet `exclusiveGroup` mutual + * exclusion. + * 2. Free secondary weapon presets (`isFreeWeapon`, excluding `isWeaponForCustomSlot` containers) + * whose `requires` (direct + `sum_weapons` container inheritance) are all owned receive + * `count = 1` so `shop_is_weapon_purchased` allows selecting them. + * 3. Silver-Lion counted modifications (`isCounted` with `goldenEagles === 0`, covering all + * 12,800 shells, gun belts, and `tank_medical_kit_expendable`) and paid weapon presets + * (`isPaidWeapon` with `goldenEagles === 0`) whose `requires` are all owned: + * - When no profile record exists yet (`record === undefined`, i.e. newly acquired vehicle or + * newly unlocked prerequisite modification), receive their initial full load (`maxCount`) + * at no extra Silver-Lion charge (`itemInfo.nut` `getAllModsCost` skips `maxAmount > 1`). + * - When a record already exists with `count < maxCount` (consumed in battle) and hangar + * `autoRefillWeapons` is enabled, buy as many missing pieces with Silver Lions in + * deterministic name order as the player's balance affords. + */ +import type { BlkBlock } from '@wto/blk'; +import type { ModificationFacts, VehicleEquipment, WeaponFacts } from '../game-data/datasets/vehicle-equipment.ts'; +import { balanceOf, pay } from '../profile/profile-currency.ts'; +import { modificationRecord, ownsModification, writeModificationRecord } from '../profile/profile-modifications.ts'; +import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts'; +import { isCounted, isFree, isFreeWeapon, isPaidWeapon } from './equipment-catalogue.ts'; +import { arePrerequisitesOwned } from './module-status.ts'; + +export function stockAndRefillEquipment(profile: BlkBlock, equipment: VehicleEquipment, autoRefill: boolean): void { + const modifications = [...equipment.modifications].sort((a, b) => a.name.localeCompare(b.name)); + const weapons = [...equipment.weapons].sort((a, b) => a.name.localeCompare(b.name)); + grantUnlockedFreeModifications(profile, equipment.vehicle, modifications); + grantUnlockedFreeWeapons(profile, equipment.vehicle, weapons); + for (const modification of modifications) { + stockOrRefillCountedModification(profile, equipment.vehicle, modification, autoRefill); + } + for (const weapon of weapons) { + stockOrRefillPaidWeapon(profile, equipment.vehicle, weapon, autoRefill); + } +} + +function grantUnlockedFreeModifications(profile: BlkBlock, vehicle: string, modifications: readonly ModificationFacts[]): void { + let progressed = false; + do { + progressed = false; + for (const modification of modifications) { + if (!isFree(modification) || ownsModification(profile, vehicle, modification.name)) continue; + if (!arePrerequisitesOwned(profile, vehicle, modification.requires)) continue; + const enabled = modification.defaultEnabled && !hasEnabledExclusivePeer(profile, vehicle, modifications, modification.exclusiveGroup); + writeModificationRecord(profile, vehicle, modification.name, { + silverLionsSpent: 0, + goldenEaglesSpent: 0, + enabled, + count: Math.max(1, modification.maxCount), + }); + progressed = true; + } + } while (progressed); +} + +function hasEnabledExclusivePeer( + profile: BlkBlock, + vehicle: string, + modifications: readonly ModificationFacts[], + exclusiveGroup: string | undefined, +): boolean { + if (!exclusiveGroup) return false; + return modifications.some((peer) => peer.exclusiveGroup === exclusiveGroup && modificationRecord(profile, vehicle, peer.name)?.enabled === true); +} + +function grantUnlockedFreeWeapons(profile: BlkBlock, vehicle: string, weapons: readonly WeaponFacts[]): void { + for (const weapon of weapons) { + if (!isFreeWeapon(weapon) || weaponRecord(profile, vehicle, weapon.name)) continue; + if (!arePrerequisitesOwned(profile, vehicle, weapon.requires)) continue; + writeWeaponRecord(profile, vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: 1 }); + } +} + +function stockOrRefillCountedModification( + profile: BlkBlock, + vehicle: string, + modification: ModificationFacts, + autoRefill: boolean, +): void { + if (!isCounted(modification) || modification.goldenEagles > 0) return; + if (!arePrerequisitesOwned(profile, vehicle, modification.requires)) return; + const record = modificationRecord(profile, vehicle, modification.name); + if (record === undefined) { + writeModificationRecord(profile, vehicle, modification.name, { + silverLionsSpent: 0, + goldenEaglesSpent: 0, + enabled: modification.defaultEnabled, + count: modification.maxCount, + }); + return; + } + if (!autoRefill || record.count >= modification.maxCount) return; + const toBuy = affordableUnits(profile, modification.maxCount - record.count, modification.silverLions); + if (toBuy <= 0) return; + const cost = toBuy * modification.silverLions; + if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 }); + writeModificationRecord(profile, vehicle, modification.name, { + ...record, + silverLionsSpent: record.silverLionsSpent + cost, + count: record.count + toBuy, + }); +} + +function stockOrRefillPaidWeapon(profile: BlkBlock, vehicle: string, weapon: WeaponFacts, autoRefill: boolean): void { + if (!isPaidWeapon(weapon) || weapon.goldenEagles > 0) return; + if (!arePrerequisitesOwned(profile, vehicle, weapon.requires)) return; + const record = weaponRecord(profile, vehicle, weapon.name); + if (record === undefined) { + writeWeaponRecord(profile, vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: weapon.maxCount }); + return; + } + if (!autoRefill || record.count >= weapon.maxCount) return; + const toBuy = affordableUnits(profile, weapon.maxCount - record.count, weapon.silverLions); + if (toBuy <= 0) return; + const cost = toBuy * weapon.silverLions; + if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 }); + writeWeaponRecord(profile, vehicle, weapon.name, { + silverLionsSpent: record.silverLionsSpent + cost, + goldenEaglesSpent: record.goldenEaglesSpent, + count: record.count + toBuy, + }); +} + +function affordableUnits(profile: BlkBlock, missing: number, unitSilverCost: number): number { + if (missing <= 0) return 0; + if (unitSilverCost <= 0) return missing; + return Math.min(missing, Math.floor(balanceOf(profile).silverLions / unitSilverCost)); +} diff --git a/packages/local-service/src/modifications/equipment-catalogue.ts b/packages/local-service/src/modifications/equipment-catalogue.ts index 4adf95c..98ed066 100644 --- a/packages/local-service/src/modifications/equipment-catalogue.ts +++ b/packages/local-service/src/modifications/equipment-catalogue.ts @@ -52,14 +52,15 @@ export function countsForTier(modification: ModificationFacts): boolean { } /** - * Comes with the vehicle: no price, no outright price, no research points and nothing required - * (most modifications of reserve and premium vehicles). The client counts such a modification as - * owned without a record (scripts weaponry/initWeaponryTypes.nut MODIFICATION getAmount), but - * whether it is mounted comes from the record alone (shopIsModificationEnabled). + * Zero-cost, unresearched modification (no Silver Lions, Golden Eagles, outright GE, or RP): + * includes both ungated modifications on reserve/premium vehicles and prerequisite-gated free + * modifications such as chaff belts (`countermeasures_launcher_chaff`) or premium gun upgrades. + * Because `weaponsPurchase.nut` `canBuyItem` refuses zero-cost purchases, the server grants the + * owned record automatically once all `requires` are owned. */ export function isFree(modification: ModificationFacts): boolean { - return modification.silverLions === 0 && modification.goldenEagles === 0 && modification.openGoldenEagles === 0 - && modification.researchPoints === 0 && modification.requires.length === 0; + return modification.silverLions === 0 && modification.goldenEagles === 0 + && modification.openGoldenEagles === 0 && modification.researchPoints === 0; } /** Bought in amounts (ammunition, expendables): maxCount above 1. */ diff --git a/packages/local-service/src/modifications/modification-purchase.ts b/packages/local-service/src/modifications/modification-purchase.ts index 0c65eb9..ede87df 100644 --- a/packages/local-service/src/modifications/modification-purchase.ts +++ b/packages/local-service/src/modifications/modification-purchase.ts @@ -41,7 +41,7 @@ export function buyModification(profile: BlkBlock, catalogue: EquipmentCatalogue if (purchase.price.silverLions !== order.offered.silverLions || purchase.price.goldenEagles !== order.offered.goldenEagles) return refused('PRICE_CHANGED'); if (!canAfford(profile, purchase.price)) return refused('NOT_ENOUGH_MONEY'); pay(profile, purchase.price); - addToRecord(profile, order.vehicle, modification, purchase); + addToRecord(profile, catalogue, order.vehicle, modification, purchase); return ACCEPTED; } @@ -60,15 +60,22 @@ export function buyAllModifications(profile: BlkBlock, catalogue: EquipmentCatal 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); - for (const { modification, purchase } of purchases) addToRecord(profile, vehicle, modification, purchase); + for (const { modification, purchase } of purchases) addToRecord(profile, catalogue, vehicle, modification, purchase); return ACCEPTED; } -/** Switches are applied to bought modifications only; the rest of the request is ignored. */ -export function enableModifications(profile: BlkBlock, switches: readonly { vehicle: string; modification: string; enabled: boolean }[]): RuleOutcome { +/** Switches are applied to bought modifications only; enabling an exclusive-group mod unmounts peers. */ +export function enableModifications( + profile: BlkBlock, + catalogue: EquipmentCatalogue, + switches: readonly { vehicle: string; modification: string; enabled: boolean }[], +): RuleOutcome { for (const { vehicle, modification, enabled } of switches) { const record = modificationRecord(profile, vehicle, modification); - if (record && record.enabled !== enabled) writeModificationRecord(profile, vehicle, modification, { ...record, enabled }); + if (!record) continue; + if (record.enabled !== enabled) writeModificationRecord(profile, vehicle, modification, { ...record, enabled }); + const facts = catalogue.modification(vehicle, modification); + if (enabled && facts) disableExclusiveGroupPeers(profile, catalogue, vehicle, facts); } return ACCEPTED; } @@ -125,14 +132,25 @@ function buyAllEntry(profile: BlkBlock, vehicle: string, modification: Modificat return purchase; } -function addToRecord(profile: BlkBlock, vehicle: string, modification: ModificationFacts, purchase: Purchase): void { +function addToRecord(profile: BlkBlock, catalogue: EquipmentCatalogue, vehicle: string, modification: ModificationFacts, purchase: Purchase): void { const record = modificationRecord(profile, vehicle, modification.name); + const enabled = record?.enabled ?? modification.defaultEnabled; writeModificationRecord(profile, vehicle, modification.name, { silverLionsSpent: (record?.silverLionsSpent ?? 0) + purchase.price.silverLions, goldenEaglesSpent: (record?.goldenEaglesSpent ?? 0) + purchase.price.goldenEagles, - enabled: record?.enabled ?? true, + enabled, count: (record?.count ?? 0) + purchase.amount, }); + if (enabled) disableExclusiveGroupPeers(profile, catalogue, vehicle, modification); removeModuleResearch(profile, vehicle, modification.name); if (moduleResearchTarget(profile, vehicle) === modification.name) setModuleResearchTarget(profile, vehicle, ''); } + +function disableExclusiveGroupPeers(profile: BlkBlock, catalogue: EquipmentCatalogue, vehicle: string, active: ModificationFacts): void { + if (!active.exclusiveGroup) return; + for (const peer of catalogue.vehicle(vehicle)?.modifications ?? []) { + if (peer.name === active.name || peer.exclusiveGroup !== active.exclusiveGroup) continue; + const peerRecord = modificationRecord(profile, vehicle, peer.name); + if (peerRecord?.enabled) writeModificationRecord(profile, vehicle, peer.name, { ...peerRecord, enabled: false }); + } +} diff --git a/packages/local-service/src/modifications/module-state.ts b/packages/local-service/src/modifications/module-state.ts index 916b7da..78bce57 100644 --- a/packages/local-service/src/modifications/module-state.ts +++ b/packages/local-service/src/modifications/module-state.ts @@ -1,31 +1,31 @@ /** * Bringing the modification state of a profile up to date, like research/research-state.ts does * for vehicles: whenever a profile is handed out or changed (profiles-setup.ts), for every owned - * vehicle - * modifications a record for every free modification (equipment-catalogue.ts - * isFree), so it comes mounted and can be switched off and on - * weapons a record for every free weapon preset (isFreeWeapon), so it - * can be chosen + * vehicle (sorted by name for deterministic auto-refill deduction): + * modifications & weapons unlocked free items, initial full stock of newly unlocked + * ammunition / expendables / paid presets, and Silver-Lion + * top-ups when `autoRefillWeapons` is enabled (ammo-refill.ts) * hangar//tierNumber the highest open tier * modsResearch the status of every modification not owned yet; locked ones * only keep an entry while they hold research points * hangar//investMode cleared when that modification is no longer researchable */ import type { BlkBlock } from '@wto/blk'; -import type { VehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts'; +import { isAutoRefillWeaponsEnabled } from '../profile/profile-client-settings.ts'; import { ownedVehicles } from '../profile/profile-hangar.ts'; -import { ownsModification, writeModificationRecord } from '../profile/profile-modifications.ts'; import { moduleResearchPoints, removeModuleResearch, setModuleStatus } from '../profile/profile-module-research.ts'; -import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts'; import { moduleResearchTarget, setModuleResearchTarget, setOpenTier } from '../profile/profile-vehicle-state.ts'; -import { isFree, isFreeWeapon, type EquipmentCatalogue } from './equipment-catalogue.ts'; +import { stockAndRefillEquipment } from './ammo-refill.ts'; +import type { EquipmentCatalogue } from './equipment-catalogue.ts'; import { ModuleStatus, vehicleModuleState } from './module-status.ts'; export function refreshModuleState(profile: BlkBlock, catalogue: EquipmentCatalogue): void { - for (const vehicle of ownedVehicles(profile)) { + const autoRefill = isAutoRefillWeaponsEnabled(profile); + const owned = [...ownedVehicles(profile)].sort((a, b) => a.localeCompare(b)); + for (const vehicle of owned) { const equipment = catalogue.vehicle(vehicle); if (!equipment) continue; - grantFreeEquipment(profile, equipment); + stockAndRefillEquipment(profile, equipment, autoRefill); const state = vehicleModuleState(profile, equipment); setOpenTier(profile, vehicle, state.openTier); for (const modification of equipment.modifications) { @@ -46,14 +46,3 @@ export function currentModuleStatus(profile: BlkBlock, catalogue: EquipmentCatal if (!equipment || !ownedVehicles(profile).has(vehicle)) return undefined; return vehicleModuleState(profile, equipment).statuses.get(modification); } - -function grantFreeEquipment(profile: BlkBlock, equipment: VehicleEquipment): void { - for (const modification of equipment.modifications) { - if (!isFree(modification) || ownsModification(profile, equipment.vehicle, modification.name)) continue; - writeModificationRecord(profile, equipment.vehicle, modification.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, enabled: true, count: 1 }); - } - for (const weapon of equipment.weapons) { - if (!isFreeWeapon(weapon) || weaponRecord(profile, equipment.vehicle, weapon.name)) continue; - writeWeaponRecord(profile, equipment.vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: 1 }); - } -} diff --git a/packages/local-service/src/modifications/module-status.ts b/packages/local-service/src/modifications/module-status.ts index 29518e1..562672c 100644 --- a/packages/local-service/src/modifications/module-status.ts +++ b/packages/local-service/src/modifications/module-status.ts @@ -52,6 +52,10 @@ export function isModuleResearched(profile: BlkBlock, vehicle: string, modificat || moduleResearchPoints(profile, vehicle, modification.name) >= modification.researchPoints; } +export function arePrerequisitesOwned(profile: BlkBlock, vehicle: string, requires: readonly string[]): boolean { + return requires.every((required) => ownsModification(profile, vehicle, required)); +} + function openTierOf(profile: BlkBlock, equipment: VehicleEquipment): number { let tier = 1; while (tier <= equipment.tierRequirements.length) { @@ -70,5 +74,5 @@ function statusOf(profile: BlkBlock, vehicle: string, modification: Modification if (modification.tier > openTier) return ModuleStatus.LOCKED; return isModuleResearched(profile, vehicle, modification) ? ModuleStatus.CAN_BUY : ModuleStatus.CAN_RESEARCH; } - return modification.requires.every((required) => ownsModification(profile, vehicle, required)) ? ModuleStatus.CAN_BUY : ModuleStatus.LOCKED; + return arePrerequisitesOwned(profile, vehicle, modification.requires) ? ModuleStatus.CAN_BUY : ModuleStatus.LOCKED; } diff --git a/packages/local-service/src/modifications/weapon-purchase.ts b/packages/local-service/src/modifications/weapon-purchase.ts index 1067cb6..0727941 100644 --- a/packages/local-service/src/modifications/weapon-purchase.ts +++ b/packages/local-service/src/modifications/weapon-purchase.ts @@ -13,6 +13,7 @@ import { ownsVehicle } from '../profile/profile-hangar.ts'; import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts'; import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; import { isPaidWeapon, type EquipmentCatalogue } from './equipment-catalogue.ts'; +import { arePrerequisitesOwned } from './module-status.ts'; export interface WeaponOrder { readonly vehicle: string; @@ -24,7 +25,7 @@ export interface WeaponOrder { export function buyWeapons(profile: BlkBlock, catalogue: EquipmentCatalogue, order: WeaponOrder): RuleOutcome { const weapon = catalogue.weapon(order.vehicle, order.weapon); if (!weapon || !ownsVehicle(profile, order.vehicle) || order.amount < 1) return refused('BAD_PARAMS'); - if (!isPaidWeapon(weapon)) return refused('NOT_AVAILABLE'); + if (!isPaidWeapon(weapon) || !arePrerequisitesOwned(profile, order.vehicle, weapon.requires)) return refused('NOT_AVAILABLE'); const record = weaponRecord(profile, order.vehicle, order.weapon); const held = record?.count ?? 0; if (held + order.amount > weapon.maxCount) return refused('NOT_AVAILABLE'); diff --git a/packages/local-service/src/profile/profile-client-settings.ts b/packages/local-service/src/profile/profile-client-settings.ts index 9a9f84b..ffe494e 100644 --- a/packages/local-service/src/profile/profile-client-settings.ts +++ b/packages/local-service/src/profile/profile-client-settings.ts @@ -23,6 +23,15 @@ const BOOL_SETTINGS = [ 'allowToBeAddedToLB', ] as const; +/** + * Whether hangar ammunition/expendable auto-refill is enabled (`EconomicState` +0xe979). + * Defaults to `true` when absent from `profile.blk`, matching `aces.exe` 0x90b6a0 and 0x918ef0. + */ +export function isAutoRefillWeaponsEnabled(profile: BlkBlock): boolean { + const param = findParam(profile, 'autoRefillWeapons')?.value; + return param?.type === 'bool' ? param.value : true; +} + export function applyClientProfileUpdate(profile: BlkBlock, request: BlkBlock): void { for (const name of BOOL_SETTINGS) { const param = findParam(request, name)?.value;