/** * Bringing the research state of a profile up to date. The char * server keeps it current online; the local server does it whenever a profile is handed out * or changed (profile/player-profiles.ts), so the state always follows from the facts: * owned vehicles, research points, earned unlocks, banked excess research points. * * 1. Open ranks and earn unlocks until neither changes anything (an unlock can give a vehicle, * a vehicle can open a rank, a rank can earn an unlock). * 2. Write every vehicle's status into unitResearch: owned vehicles have no entry, locked * vehicles only keep one while they hold research points. * 3. Keep every branch researching while it can: a target that is no * longer researchable (bought, researched, ...) is cleared, and a new one is picked by * auto-research-target.ts when the branch has just finished a vehicle (prevUnit set: the * stock scripts then open the "choose the next research" window, ShopCheckResearch, from * scripts/researches/researchActions.nut checkNonApprovedResearches) or has no banked * exp. With banked RP and nothing just finished it stays empty, so the player chooses * where the RP goes (cln_move_exp_to_unit). A branch with nothing left to research has its * banked RP cleared. */ import type { BlkBlock } from '@wto/blk'; import { ownedVehicles } from '../profile/profile-hangar.ts'; import { branchExcessRp, finishedResearch, researchTarget, setBranchExcessRp, setResearchTarget, } from '../profile/profile-research-targets.ts'; import { earnedUnlocks } from '../profile/profile-unlocks.ts'; import { removeVehicleResearch, researchPointsOf, setVehicleStatus } from '../profile/profile-vehicle-research.ts'; import { pickResearchTarget } from './auto-research-target.ts'; import { openEarnedRanks } from './era-progress.ts'; import type { ResearchRules } from './research-rules.ts'; import type { TreeVehicle } from './tech-tree.ts'; import { VehicleStatus, vehicleStatuses } from './vehicle-status.ts'; import { earnVehicleUnlocks } from './vehicle-unlock-rules.ts'; const MAX_ROUNDS = 16; export function refreshResearchState(profile: BlkBlock, rules: ResearchRules): void { const owned = ownedVehicles(profile); const earned = earnedUnlocks(profile); for (let round = 0; round < MAX_ROUNDS; round++) { const ranksChanged = openEarnedRanks(profile, owned, rules.techTree, rules.eraRequirements); const unlocksChanged = earnVehicleUnlocks(profile, { earned, owned }, rules.vehicleUnlocks, rules.techTree); if (!ranksChanged && !unlocksChanged) break; } const statuses = vehicleStatuses({ profile, owned, earnedUnlocks: earned, techTree: rules.techTree }); for (const vehicle of rules.techTree.all()) { const status = statuses.get(vehicle.name); if (status === undefined || (status === VehicleStatus.LOCKED && researchPointsOf(profile, vehicle.name) === 0)) { removeVehicleResearch(profile, vehicle.name); } else { setVehicleStatus(profile, vehicle.name, status); } } const candidates = researchCandidates(rules, statuses); const unresearchedBranches = branchesWithUnresearchedTreeVehicles(rules, statuses); for (const { country, type } of rules.techTree.branches()) { const key = `${country}/${type}`; if (!unresearchedBranches.has(key)) { setBranchExcessRp(profile, country, type, 0); } const target = researchTarget(profile, country, type); if (target && statuses.get(target) === VehicleStatus.CAN_RESEARCH) continue; if (target) setResearchTarget(profile, country, type, ''); const prev = finishedResearch(profile, country, type); const excess = branchExcessRp(profile, country, type); if (prev !== '' || excess === 0) { const next = pickResearchTarget(candidates.get(key) ?? [], rules.researchTuning, target || prev); setResearchTarget(profile, country, type, next?.name ?? ''); } } } function researchCandidates(rules: ResearchRules, statuses: ReadonlyMap): Map { const byBranch = new Map(); for (const vehicle of rules.techTree.all()) { if (vehicle.kind !== 'research' || statuses.get(vehicle.name) !== VehicleStatus.CAN_RESEARCH) continue; const key = `${vehicle.country}/${vehicle.type}`; const list = byBranch.get(key); if (list) list.push(vehicle); else byBranch.set(key, [vehicle]); } return byBranch; } function branchesWithUnresearchedTreeVehicles( rules: ResearchRules, statuses: ReadonlyMap, ): Set { const branches = new Set(); for (const vehicle of rules.techTree.all()) { if (vehicle.kind !== 'research') continue; const status = statuses.get(vehicle.name); if (status === VehicleStatus.CAN_RESEARCH || status === VehicleStatus.LOCKED) { branches.add(`${vehicle.country}/${vehicle.type}`); } } return branches; } export function currentStatus(profile: BlkBlock, rules: ResearchRules, vehicle: string): VehicleStatus | undefined { return vehicleStatuses({ profile, owned: ownedVehicles(profile), earnedUnlocks: earnedUnlocks(profile), techTree: rules.techTree, }).get(vehicle); }