/** * The status of each modification of an owned vehicle. The client * works out "owned" and "mounted" from the modifications records and "in research" from * `investMode` itself; the server supplies the rest through `modsResearch`. */ import type { BlkBlock } from '@wto/blk'; import type { ModificationFacts, VehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts'; import { ownsModification } from '../profile/profile-modifications.ts'; 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_* (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]; export interface VehicleModuleState { /** The highest open tier (hangar tierNumber). */ readonly openTier: number; /** Status of every modification the player does not own. */ readonly statuses: ReadonlyMap; } export function vehicleModuleState( profile: BlkBlock, equipment: VehicleEquipment, 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, byName, index)); } return { openTier, statuses }; } export function isModuleResearched( profile: BlkBlock, vehicle: string, modification: ModificationFacts, index?: ProfileEquipmentIndex, ): boolean { return isOwned(profile, vehicle, modification.name, index) || researchPointsOf(profile, vehicle, modification.name, index) >= modification.researchPoints; } export function arePrerequisitesOwned( profile: BlkBlock, vehicle: string, requires: readonly string[], index?: ProfileEquipmentIndex, ): boolean { return requires.every((required) => isOwned(profile, vehicle, required, index)); } function isOwned(profile: BlkBlock, vehicle: string, modification: string, index?: ProfileEquipmentIndex): boolean { return index ? index.ownsModification(vehicle, modification) : ownsModification(profile, vehicle, modification); } function researchPointsOf(profile: BlkBlock, vehicle: string, modification: string, index?: ProfileEquipmentIndex): number { return index ? index.moduleResearchPoints(vehicle, modification) : moduleResearchPoints(profile, vehicle, modification); } function openTierOf(profile: BlkBlock, equipment: VehicleEquipment, index?: ProfileEquipmentIndex): number { let tier = 1; while (tier <= equipment.tierRequirements.length) { const needed = equipment.tierRequirements[tier - 1] ?? 0; const tierModifications = equipment.modifications.filter((modification) => modification.tier === tier && countsForTier(modification)); if (tierModifications.length === 0) break; const researched = tierModifications.filter((modification) => isModuleResearched(profile, equipment.vehicle, modification, index)).length; if (researched < needed) break; tier++; } 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; 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; }