/** * Cross-branch research-point redirection in profile.blk ("Research helicopters by playing * ground vehicles", gui.vromfs.bin scripts/shop/shop.nut:2341-2354). * * Profile contract (verified in aces.exe 2.61.0.37 0x930150..0x93082a & 0x903ca0): * researchUnits{ * country_usa{ * destTypeRPTank:t="Helicopter" * } * } * - Reader (0x93020e..0x930247): reads "destTypeRP" inside researchUnits. * and maps it against VEHICLE_TYPES (0..6); missing or unrecognized values become -1 * (ES_UNIT_TYPE_INVALID), exposed to Squirrel by getDestinationRPUnitType (0x15532f0). * - Writer (0x9307eb..0x93082a): writes "destTypeRP" only when 0 <= destType <= 6 * and omits the parameter when disabled (-1). * Battle settlement applies it in battle/battle-rp-investment.ts. */ import { ensureBlock, findBlock, type BlkBlock } from '@wto/blk'; import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; import type { TechTree } from '../research/tech-tree.ts'; import { VEHICLE_TYPES, vehicleTypeById, type VehicleType } from '../research/vehicle-types.ts'; import { removeParam, stringField } from './blk-fields.ts'; const RESEARCH_UNITS = 'researchUnits'; const DEST_TYPE_PREFIX = 'destTypeRP'; const INVALID_UNIT_TYPE = -1; export type VehicleDomain = VehicleType; const VALID_DOMAINS: ReadonlySet = new Set(VEHICLE_TYPES); export function destinationRpDomain( profile: BlkBlock, country: string, sourceDomain: VehicleDomain, ): VehicleDomain | undefined { const countryBlock = findBlock(findBlock(profile, RESEARCH_UNITS) ?? { name: '', params: [], blocks: [] }, country); const raw = stringField(countryBlock, `${DEST_TYPE_PREFIX}${sourceDomain}`); if (!raw || !VALID_DOMAINS.has(raw) || raw === sourceDomain) return undefined; return raw as VehicleDomain; } export function setDestinationRpDomain( profile: BlkBlock, country: string, sourceDomain: VehicleDomain, destinationDomain: VehicleDomain | undefined, ): void { const countryBlock = ensureBlock(ensureBlock(profile, RESEARCH_UNITS), country); const paramName = `${DEST_TYPE_PREFIX}${sourceDomain}`; if (!destinationDomain || destinationDomain === sourceDomain) { removeParam(countryBlock, paramName); return; } removeParam(countryBlock, paramName); countryBlock.params.push({ name: paramName, value: { type: 'string', value: destinationDomain } }); } /** Validate and apply cln_set_dest_rp_unit_type (country:t, srcType:i, destType:i). */ export function applySetDestinationRpRule( profile: BlkBlock, techTree: TechTree, country: string, srcTypeId: number, destTypeId: number, ): RuleOutcome { const sourceDomain = vehicleTypeById(srcTypeId); if (!country || !techTree.hasCountry(country) || !sourceDomain) return refused('BAD_PARAMS'); if (destTypeId === INVALID_UNIT_TYPE) { setDestinationRpDomain(profile, country, sourceDomain, undefined); return ACCEPTED; } const destinationDomain = vehicleTypeById(destTypeId); if (!destinationDomain || destinationDomain === sourceDomain) return refused('BAD_PARAMS'); const hasDestinationBranch = techTree.branches().some( (branch) => branch.country === country && branch.type === destinationDomain, ); if (!hasDestinationBranch) return refused('NOT_AVAILABLE'); setDestinationRpDomain(profile, country, sourceDomain, destinationDomain); return ACCEPTED; }