/** * Per-vehicle fields of owned vehicles: hangar{ { ... } }, * read by name by AircraftInfo::serialize (aces.exe 2.61.0.37 0x940510). Only the fields the * local server maintains are handled here: * investMode:t the modification being researched, empty = none (AircraftInfo+0x100) * investModePrev:t the modification whose research just finished (+0x108) * tierNumber:i the highest open modification tier (script is_tier_available) * spareAircraftsCount:i owned spares in the saved profile, encoded as a negative i64 on the wire * excessExp:i64 banked modification research points (+0x70) * freeRepairs:i64 free repairs used (0x941cef; shop_get_free_repairs_used at 0x17a4b80) */ import { findBlock, findParam, setParam, type BlkBlock } from '@wto/blk'; import { intField } from './blk-fields.ts'; const HANGAR = 'hangar'; const FREE_REPAIRS = 'freeRepairs'; const RESEARCH_TARGET = 'investMode'; const FINISHED_TARGET = 'investModePrev'; const OPEN_TIER = 'tierNumber'; const SPARES = 'spareAircraftsCount'; const EXCESS_EXP = 'excessExp'; export function moduleResearchTarget(profile: BlkBlock, vehicle: string): string { const value = fieldOf(profile, vehicle, RESEARCH_TARGET); return value?.type === 'string' ? value.value : ''; } export function setModuleResearchTarget(profile: BlkBlock, vehicle: string, modification: string): void { const entry = vehicleEntry(profile, vehicle); if (entry) setParam(entry, RESEARCH_TARGET, { type: 'string', value: modification }); } export function finishedModuleResearch(profile: BlkBlock, vehicle: string): string { const value = fieldOf(profile, vehicle, FINISHED_TARGET); return value?.type === 'string' ? value.value : ''; } export function setFinishedModuleResearch(profile: BlkBlock, vehicle: string, modification: string): void { const entry = vehicleEntry(profile, vehicle); if (entry) setParam(entry, FINISHED_TARGET, { type: 'string', value: modification }); } export function setOpenTier(profile: BlkBlock, vehicle: string, tier: number): void { const entry = vehicleEntry(profile, vehicle); if (entry) setParam(entry, OPEN_TIER, { type: 'int', value: tier }); } export function spareCount(profile: BlkBlock, vehicle: string): number { const value = fieldOf(profile, vehicle, SPARES); return value?.type === 'int' ? value.value : 0; } export function setSpareCount(profile: BlkBlock, vehicle: string, count: number): void { const entry = vehicleEntry(profile, vehicle); if (entry) setParam(entry, SPARES, { type: 'int', value: count }); } /** SpareAircraftStat reads i64 and clears positive values. The getter negates the stat * (aces.exe 2.61.0.58 0x9447bb, 0x94f7ab, 0x17abe70). */ export function clientHangarSpares(hangar: BlkBlock): BlkBlock { const blocks = hangar.blocks.map((vehicle) => { const count = findParam(vehicle, SPARES)?.value; if (count?.type !== 'int') return vehicle; const clientVehicle = { ...vehicle, params: [...vehicle.params] }; setParam(clientVehicle, SPARES, { type: 'int64', value: -BigInt(count.value) }); return clientVehicle; }); return { ...hangar, blocks }; } /** Uses one free repair on an owned vehicle if it still has an allowance. */ export function consumeFreeRepair(profile: BlkBlock, vehicle: string, allowance: number): boolean { const entry = vehicleEntry(profile, vehicle); if (!entry) return false; const used = Math.max(0, intField(entry, FREE_REPAIRS)); if (used >= allowance) return false; setParam(entry, FREE_REPAIRS, { type: 'int64', value: BigInt(used + 1) }); return true; } export function vehicleExcessRp(profile: BlkBlock, vehicle: string): number { const value = fieldOf(profile, vehicle, EXCESS_EXP); if (value?.type === 'int64') return Math.max(0, Number(value.value)); return value?.type === 'int' ? Math.max(0, value.value) : 0; } export function setVehicleExcessRp(profile: BlkBlock, vehicle: string, amount: number): void { const entry = vehicleEntry(profile, vehicle); if (entry) setParam(entry, EXCESS_EXP, { type: 'int64', value: BigInt(Math.max(0, Math.trunc(amount))) }); } function fieldOf(profile: BlkBlock, vehicle: string, name: string) { const entry = vehicleEntry(profile, vehicle); return entry && findParam(entry, name)?.value; } function vehicleEntry(profile: BlkBlock, vehicle: string): BlkBlock | undefined { const hangar = findBlock(profile, HANGAR); return hangar && findBlock(hangar, vehicle); }