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.
38 lines
1.8 KiB
TypeScript
38 lines
1.8 KiB
TypeScript
/**
|
|
* Picking a research target for a branch that has none.
|
|
*
|
|
* The stock game never leaves a country and vehicle type without a vehicle in research while
|
|
* one can be researched: the char server sets the target itself, and the client only asks the
|
|
* player to confirm it (aces.exe 2.61.0.37 0x17a45e0 shop_get_countries_list_with_autoset_units
|
|
* returns {country, unit = currUnit, prevUnit}; scripts/researches/researchActions.nut
|
|
* checkNonApprovedResearches opens scripts/shop/shopCheckResearch.nut, which keeps a target
|
|
* that is still being researched). Which vehicle the server picks is not in the client, so the
|
|
* pick is offline tuning (tuning/research.jsonc autoTargetPick).
|
|
*/
|
|
import type { AutoTargetPick, ResearchTuning } from './research-tuning.ts';
|
|
import type { TreeVehicle } from './tech-tree.ts';
|
|
|
|
/** The vehicle to research among the branch's researchable candidates; undefined when there are none. */
|
|
export function pickResearchTarget(
|
|
candidates: readonly TreeVehicle[],
|
|
tuning: ResearchTuning,
|
|
previousUnitName: string = '',
|
|
): TreeVehicle | undefined {
|
|
if (tuning.preferLineSuccessor && previousUnitName) {
|
|
const successors = candidates.filter((vehicle) => vehicle.requires === previousUnitName);
|
|
if (successors.length > 0) return [...successors].sort(ORDERS[tuning.autoTargetPick])[0];
|
|
}
|
|
return [...candidates].sort(ORDERS[tuning.autoTargetPick])[0];
|
|
}
|
|
|
|
type Order = (a: TreeVehicle, b: TreeVehicle) => number;
|
|
|
|
const byTree: Order = (a, b) => a.treeOrder - b.treeOrder;
|
|
const byPointsThenTree: Order = (a, b) => a.researchPoints - b.researchPoints || byTree(a, b);
|
|
|
|
const ORDERS: Readonly<Record<AutoTargetPick, Order>> = {
|
|
highestRank: (a, b) => b.rank - a.rank || byPointsThenTree(a, b),
|
|
lowestRank: (a, b) => a.rank - b.rank || byPointsThenTree(a, b),
|
|
treeOrder: byTree,
|
|
};
|