diff --git a/apps/server/src/modifications/modification-purchase.ts b/apps/server/src/modifications/modification-purchase.ts index 4e0a3f0..63f4e70 100644 --- a/apps/server/src/modifications/modification-purchase.ts +++ b/apps/server/src/modifications/modification-purchase.ts @@ -21,7 +21,7 @@ import { moduleResearchTarget, setModuleResearchTarget } from '../profile/profil import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; import { isCounted, isResearchable, type EquipmentCatalogue } from './equipment-catalogue.ts'; import { currentModuleStatus } from './module-state.ts'; -import { ModuleStatus } from './module-status.ts'; +import { ModuleStatus, vehicleModuleState } from './module-status.ts'; export interface ModificationOrder { readonly vehicle: string; @@ -48,8 +48,9 @@ export function buyModification(profile: BlkBlock, catalogue: EquipmentCatalogue export function buyAllModifications(profile: BlkBlock, catalogue: EquipmentCatalogue, vehicle: string, outright: boolean, offered: Price): RuleOutcome { const equipment = catalogue.vehicle(vehicle); if (!equipment || !ownsVehicle(profile, vehicle)) return refused('BAD_PARAMS'); + const { statuses } = vehicleModuleState(profile, equipment); const purchases = equipment.modifications.flatMap((modification) => { - const purchase = buyAllEntry(profile, vehicle, modification, catalogue, outright); + const purchase = buyAllEntry(profile, vehicle, modification, statuses.get(modification.name), outright); return purchase ? [{ modification, purchase }] : []; }); const price = purchases.reduce((sum, { purchase }) => ({ @@ -114,17 +115,24 @@ function outrightPurchase(profile: BlkBlock, vehicle: string, modification: Modi /** * One entry of "buy all", as the client sums it (scripts weaponry/itemInfo.nut getAllModsCost): * skip what is at its maximum; with forceOpen every modification starts at its outright price - * (whatever its status); a buyable one (canBuyMod) with a non-zero price takes its own price - * instead, and is skipped altogether when counted or priced in golden eagles. + * (whatever its status); a buyable or researched one (canBuyMod || isModStatusResearched) with a + * non-zero price takes its own price instead, and is skipped altogether when counted or priced in + * golden eagles. */ -function buyAllEntry(profile: BlkBlock, vehicle: string, modification: ModificationFacts, catalogue: EquipmentCatalogue, outright: boolean): Purchase | undefined { +function buyAllEntry( + profile: BlkBlock, + vehicle: string, + modification: ModificationFacts, + status: ModuleStatus | undefined, + outright: boolean, +): Purchase | undefined { const record = modificationRecord(profile, vehicle, modification.name); const maxAmount = Math.max(1, modification.maxCount); if ((record?.count ?? 0) >= maxAmount) return undefined; let purchase: Purchase | undefined; const openPrice = outright && isResearchable(modification) ? outrightPrice(profile, vehicle, modification) : 0; if (openPrice > 0) purchase = { price: { silverLions: 0, goldenEagles: openPrice }, amount: 1 }; - const buyable = record !== undefined || currentModuleStatus(profile, catalogue, vehicle, modification.name) === ModuleStatus.CAN_BUY; + const buyable = record !== undefined || status === ModuleStatus.CAN_BUY || status === ModuleStatus.RESEARCHED; if (buyable && (modification.silverLions > 0 || modification.goldenEagles > 0)) { if (isCounted(modification) || modification.goldenEagles > 0) return undefined; purchase = { price: { silverLions: modification.silverLions, goldenEagles: 0 }, amount: 1 }; diff --git a/apps/server/src/modifications/module-status.ts b/apps/server/src/modifications/module-status.ts index 0ba129e..ad85ca0 100644 --- a/apps/server/src/modifications/module-status.ts +++ b/apps/server/src/modifications/module-status.ts @@ -10,11 +10,12 @@ import { moduleResearchPoints } from '../profile/profile-module-research.ts'; import { countsForTier, isResearchable } from './equipment-catalogue.ts'; import type { ProfileEquipmentIndex } from './profile-equipment-index.ts'; -/** shopItemConsts ES_ITEM_STATUS_*; the same values as the vehicle statuses. */ +/** shopItemConsts ES_ITEM_STATUS_* (aces.exe 2.61.0.58 0x17e2894..0x17e294b); same values as VehicleStatus. */ export const ModuleStatus = { LOCKED: 1, CAN_RESEARCH: 2, CAN_BUY: 8, + RESEARCHED: 0x40, } as const; export type ModuleStatus = (typeof ModuleStatus)[keyof typeof ModuleStatus]; @@ -32,10 +33,11 @@ export function vehicleModuleState( index?: ProfileEquipmentIndex, ): VehicleModuleState { const openTier = openTierOf(profile, equipment, index); + const byName = new Map(equipment.modifications.map((mod) => [mod.name, mod])); const statuses = new Map(); for (const modification of equipment.modifications) { if (isOwned(profile, equipment.vehicle, modification.name, index)) continue; - statuses.set(modification.name, statusOf(profile, equipment.vehicle, modification, openTier, index)); + statuses.set(modification.name, statusOf(profile, equipment.vehicle, modification, openTier, byName, index)); } return { openTier, statuses }; } @@ -80,16 +82,38 @@ function openTierOf(profile: BlkBlock, equipment: VehicleEquipment, index?: Prof return tier; } +function arePrerequisitesResearched( + profile: BlkBlock, + vehicle: string, + requires: readonly string[], + byName: ReadonlyMap, + index?: ProfileEquipmentIndex, +): boolean { + return requires.every((required) => { + if (isOwned(profile, vehicle, required, index)) return true; + const mod = byName.get(required); + return mod !== undefined && isResearchable(mod) && isModuleResearched(profile, vehicle, mod, index); + }); +} + function statusOf( profile: BlkBlock, vehicle: string, modification: ModificationFacts, openTier: number, + byName: ReadonlyMap, index?: ProfileEquipmentIndex, ): ModuleStatus { if (isResearchable(modification)) { if (modification.tier > openTier) return ModuleStatus.LOCKED; - return isModuleResearched(profile, vehicle, modification, index) ? ModuleStatus.CAN_BUY : ModuleStatus.CAN_RESEARCH; + if (isModuleResearched(profile, vehicle, modification, index)) { + return arePrerequisitesOwned(profile, vehicle, modification.requires, index) + ? ModuleStatus.CAN_BUY + : ModuleStatus.RESEARCHED; + } + return arePrerequisitesResearched(profile, vehicle, modification.requires, byName, index) + ? ModuleStatus.CAN_RESEARCH + : ModuleStatus.LOCKED; } return arePrerequisitesOwned(profile, vehicle, modification.requires, index) ? ModuleStatus.CAN_BUY : ModuleStatus.LOCKED; }