Files
WT-Offline/packages/local-service/src/research/research-progress.ts
T
N0VAN0M4LY a09ac41299 Keep a default vehicle research target for each branch
The accelerated research dialog reads currUnit when it first opens.
Assign a valid target whenever an eligible vehicle exists and replace
stale selections when refreshing a profile.

Prefer an available direct successor when configured. Otherwise use
the configured rank or tech tree order. Type checking and profile
initialization passed. In-game acceptance is pending.
2026-11-25 13:41:29 +08:00

68 lines
4.1 KiB
TypeScript

/**
* Choosing what to research and putting research points into a vehicle (implementation notes
* sections 1.2, 4.3 and 5).
*
* - Choosing a research target (cln_set_researchable) makes a researchable vehicle the
* target of its country and type and closes the "research finished" prompt.
* - Dismissing that prompt (cln_reset_researchable) only closes it.
* - Converting free research points (cln_convert_exp) costs the points and golden eagles:
* one golden eagle per researchPointsPerGoldenEagle points, rounded up. The stock window
* (scripts/convertExp/convertExpHandler.nut) never asks for more than the vehicle still
* needs; more is cut down to that.
* - When a vehicle's research points reach what it needs and it is the target, the target is
* cleared and the vehicle recorded as just finished, which makes the client offer the next
* research; the refresh after the change (research-state.ts) then picks the next target.
*/
import type { BlkBlock } from '@wto/blk';
import { balanceOf, pay } from '../profile/profile-currency.ts';
import { freeResearchPoints, spendFreeResearchPoints } from '../profile/profile-research-points.ts';
import { researchTarget, setFinishedResearch, setResearchTarget } from '../profile/profile-research-targets.ts';
import { researchPointsOf, setResearchPoints } from '../profile/profile-vehicle-research.ts';
import { currentStatus } from './research-state.ts';
import type { ResearchRules } from './research-rules.ts';
import { ACCEPTED, refused, type RuleOutcome } from './rule-outcome.ts';
import type { TreeVehicle } from './tech-tree.ts';
import { VehicleStatus } from './vehicle-status.ts';
import { vehicleTypeById } from './vehicle-types.ts';
export function chooseResearchTarget(profile: BlkBlock, rules: ResearchRules, vehicleName: string, typeId: number): RuleOutcome {
const vehicle = rules.techTree.vehicle(vehicleName);
if (!vehicle || vehicleTypeById(typeId) !== vehicle.type) return refused('BAD_PARAMS');
if (currentStatus(profile, rules, vehicleName) !== VehicleStatus.CAN_RESEARCH) return refused('NOT_AVAILABLE');
setResearchTarget(profile, vehicle.country, vehicle.type, vehicle.name);
setFinishedResearch(profile, vehicle.country, vehicle.type, '');
return ACCEPTED;
}
export function dismissFinishedResearch(profile: BlkBlock, rules: ResearchRules, country: string, typeId: number): RuleOutcome {
const type = vehicleTypeById(typeId);
if (!type || !rules.techTree.hasCountry(country)) return refused('BAD_PARAMS');
setFinishedResearch(profile, country, type, '');
return ACCEPTED;
}
export function convertFreeResearchPoints(profile: BlkBlock, rules: ResearchRules, vehicleName: string, requested: number): RuleOutcome {
const vehicle = rules.techTree.vehicle(vehicleName);
if (!vehicle || !Number.isSafeInteger(requested) || requested <= 0) return refused('BAD_PARAMS');
if (currentStatus(profile, rules, vehicleName) !== VehicleStatus.CAN_RESEARCH) return refused('NOT_AVAILABLE');
const amount = Math.min(requested, vehicle.researchPoints - researchPointsOf(profile, vehicle.name));
const goldenEagles = Math.ceil(amount / rules.researchPointsPerGoldenEagle);
if (freeResearchPoints(profile) < amount) return refused('NOT_ENOUGH_RESEARCH_POINTS');
if (balanceOf(profile).goldenEagles < goldenEagles) return refused('NOT_ENOUGH_MONEY');
pay(profile, { silverLions: 0, goldenEagles });
spendFreeResearchPoints(profile, amount);
addResearchPoints(profile, vehicle, amount);
return ACCEPTED;
}
/** Add research points to a vehicle; finishing the research target opens the next-research prompt. */
export function addResearchPoints(profile: BlkBlock, vehicle: TreeVehicle, amount: number): void {
const before = researchPointsOf(profile, vehicle.name);
setResearchPoints(profile, vehicle.name, before + amount);
const finished = before < vehicle.researchPoints && before + amount >= vehicle.researchPoints;
if (finished && researchTarget(profile, vehicle.country, vehicle.type) === vehicle.name) {
setResearchTarget(profile, vehicle.country, vehicle.type, '');
setFinishedResearch(profile, vehicle.country, vehicle.type, vehicle.name);
}
}