/** * The profile's hangar has one sub-block per owned vehicle, as in config/default_user.blk. * * hangar{ * bf2c_1{ * } * } */ import { ensureBlock, findBlock, setParam, type BlkBlock } from '@wto/blk'; import { intField } from './blk-fields.ts'; const HANGAR = 'hangar'; const MRANK_DELTA = 'mrankDelta'; function ownedVehicle(profile: BlkBlock, vehicle: string): BlkBlock | undefined { const hangar = findBlock(profile, HANGAR); return hangar && findBlock(hangar, vehicle); } export function ownsVehicle(profile: BlkBlock, vehicle: string): boolean { return ownedVehicle(profile, vehicle) !== undefined; } export function ownedVehicles(profile: BlkBlock): Set { return new Set((findBlock(profile, HANGAR)?.blocks ?? []).map((owned) => owned.name)); } /** Add the vehicle to the owned vehicles; nothing changes when the player owns it already. */ export function addOwnedVehicle(profile: BlkBlock, vehicle: string): void { const hangar = ensureBlock(profile, HANGAR); if (!hangar.blocks.some((owned) => owned.name === vehicle)) hangar.blocks.push({ name: vehicle, params: [], blocks: [] }); } /** Update an owned vehicle's hangar entry in place; ignores vehicles the player does not own. */ export function updateOwnedVehicle(profile: BlkBlock, vehicle: string, update: (entry: BlkBlock) => void): void { const entry = ownedVehicle(profile, vehicle); if (entry) update(entry); } /** The stock client uses the base rank until it has a non-negative selection. */ export function vehicleMrankDelta(profile: BlkBlock, vehicle: string): number { return Math.max(0, intField(ownedVehicle(profile, vehicle), MRANK_DELTA)); } export function setVehicleMrankDelta(profile: BlkBlock, vehicle: string, delta: number): void { const entry = ownedVehicle(profile, vehicle); if (entry) setParam(entry, MRANK_DELTA, { type: 'int', value: delta }); }