/** * What the player's active boosters (rateBooster items, config/items.blk rateBoosterParams) add * to a battle, and how much of each the battle uses up. Online the server applies them; the * client only shows the result (debriefing column "booster", log affectedBoosters). * * Rules and where they come from: * - A booster adds its percent of the base reward (without premium account or talisman): the * percents of booster, premium account and talisman are added, not multiplied (official wiki * "Boosters" https://wiki.warthunder.com/awards/5028-boosters : "1.42 x (100% + 500% + 50%)"). * - Stacking (scripts globals/ranks_common_shared.nut calc_public_boost, used for personal and * "(all allies)" boosters alike; items/boosterEffect.nut getBoostersEffects): boosters are grouped * by event type (none, kill, kill_ground, assist) and by personal / all allies; within a group * the percents, largest first, count x1, x0.6, x0.4, x0.2, then x0.1 (at least 1 point each), * rounded to a whole percent; the groups add up. The wiki's "penalty in order of increasing * bonus" says the same. * - Battles used (stop type sessionsStarted): a battle counts once, and with spentInSessionTimeMin * every such span counts as one more (items/itemsClasses/itemBooster.nut getLeftStopSessions; * wiki: "in a battle that lasted 1 hour and 20 minutes, a booster for 1 battle will only be * effective for the first 30 minutes of the battle ... 3 battles will be deducted"). So a booster * with R battles left covers the first R spans of the battle. * - Event boosters (rateBoosterParams/event kill, kill_ground, assist with targetIsPlayer) add only * to the reward of that event. The event names are the client's expEvent names (ExpEvent.KILL, * KILL_GROUND, ASSIST; inferred). Their stop killedPlayers / killedGround ends them after N kills; * which kills count is inferred: killedPlayers = kills of players, killedGround = kills of ground * vehicles. Event-free boosters add to every reward of the battle, including the mission reward * (lang "for all battle actions and mission reward"). * - Not boosted (offline design; nothing official found): battle awards (streaks), the skill bonus. * Other stop types (none in 2.61.0.37) count the battle once. */ import type { BoosterDefinition } from '../game-data/datasets/item-catalogue.ts'; import { ExpEvent } from './battle-economy.ts'; import type { BattleReport, ReportedCombatEvent } from './battle-report.ts'; export interface ActiveBooster { readonly uid: string; readonly id: string; readonly booster: BoosterDefinition; /** Stop condition progress before the battle (items//progress). */ readonly progress: number; } export interface BoosterPercent { readonly wp: number; readonly rp: number; } export interface BoosterUse { readonly booster: ActiveBooster; /** Stop condition progress after the battle. */ readonly progress: number; /** The stop condition is reached: the booster is used up. */ readonly finished: boolean; } /** Rates the settlement asks for (battle-rewards.ts). */ export interface BoosterRates { /** Percents for the reward of one combat event. */ forEvent(event: ReportedCombatEvent): BoosterPercent; /** Percents averaged over time spans, for rewards earned over time. */ overSpans(spans: readonly { readonly start: number; readonly end: number }[]): BoosterPercent; } export const NO_BOOSTER: BoosterPercent = { wp: 0, rp: 0 }; const STACK_FACTORS = [1.0, 0.6, 0.4, 0.2, 0.1] as const; const EVENT_CODES: Readonly> = { kill: ExpEvent.KILL, kill_ground: ExpEvent.KILL_GROUND, assist: ExpEvent.ASSIST }; const KILL_CODES: ReadonlySet = new Set([ExpEvent.KILL, ExpEvent.KILL_GROUND, ExpEvent.KILL_HUMAN]); /** One booster in one battle: until when it covers, and what it applies to. */ interface Coverage { readonly booster: ActiveBooster; /** The event code it is limited to; undefined for every reward. */ readonly eventCode?: number; /** An event booster that requires the target to be a player. */ readonly playersOnly: boolean; /** Mission time up to which it covers. */ readonly end: number; /** The end itself is covered (kill-counted boosters cover the kill that ends them). */ readonly endCovered: boolean; readonly used: number; } export class BattleBoosters implements BoosterRates { readonly #coverage: readonly Coverage[]; constructor(boosters: readonly ActiveBooster[], report: Pick) { this.#coverage = boosters.flatMap((booster) => { const coverage = coverageOf(booster, report); return coverage ? [coverage] : []; }); } /** The boosters that took part in the battle (log affectedBoosters). */ get applied(): readonly ActiveBooster[] { return this.#coverage.map((coverage) => coverage.booster); } /** What the battle used of each booster. */ get uses(): readonly BoosterUse[] { return this.#coverage.map(({ booster, used }) => { const progress = booster.progress + used; return { booster, progress, finished: progress >= booster.booster.stop.count }; }); } forEvent(event: ReportedCombatEvent): BoosterPercent { if (event.friendly) return NO_BOOSTER; const active = this.#coverage.filter((coverage) => covers(coverage, event.time) && (coverage.eventCode === undefined || (coverage.eventCode === event.code && (!coverage.playersOnly || event.victimIsPlayer)))); return percentOf(active); } overSpans(spans: readonly { readonly start: number; readonly end: number }[]): BoosterPercent { const general = this.#coverage.filter((coverage) => coverage.eventCode === undefined); let length = 0; let wp = 0; let rp = 0; for (const span of spans) { const cuts = [span.start, ...general.map((c) => c.end).filter((t) => t > span.start && t < span.end), span.end].sort((a, b) => a - b); for (let i = 0; i + 1 < cuts.length; i++) { const from = cuts[i]!; const to = cuts[i + 1]!; if (to <= from) continue; const percent = percentOf(general.filter((c) => covers(c, (from + to) / 2))); length += to - from; wp += percent.wp * (to - from); rp += percent.rp * (to - from); } } return length > 0 ? { wp: wp / length, rp: rp / length } : NO_BOOSTER; } } function coverageOf(booster: ActiveBooster, report: Pick): Coverage | undefined { const def = booster.booster; const left = def.stop.count - booster.progress; if (left <= 0 || (def.wpPercent <= 0 && def.rpPercent <= 0)) return undefined; const eventType = def.event?.type; const eventCode = eventType === undefined ? undefined : EVENT_CODES[eventType]; if (eventType !== undefined && eventCode === undefined) return undefined; const playersOnly = def.event?.conditions.some((condition) => condition.type === 'targetIsPlayer') ?? false; const base = { booster, ...(eventCode !== undefined ? { eventCode } : {}), playersOnly }; if (def.stop.type === 'killedPlayers' || def.stop.type === 'killedGround') { const kills = report.events .filter((e) => !e.friendly && (def.stop.type === 'killedPlayers' ? KILL_CODES.has(e.code) && e.victimIsPlayer : e.code === ExpEvent.KILL_GROUND)) .map((e) => e.time) .sort((a, b) => a - b); const last = kills[left - 1]; return { ...base, end: last ?? Infinity, endCovered: true, used: Math.min(left, kills.length) }; } const duration = Math.max(0, report.sessionEnd - report.sessionStart); if (def.stop.type !== 'sessionsStarted' || def.minutesPerBattle <= 0) { return { ...base, end: Infinity, endCovered: true, used: 1 }; } const span = def.minutesPerBattle * 60; return { ...base, end: report.sessionStart + left * span, endCovered: false, used: Math.min(left, 1 + Math.floor(duration / span)), }; } function covers(coverage: Coverage, time: number): boolean { return time < coverage.end || (coverage.endCovered && time === coverage.end); } function percentOf(active: readonly Coverage[]): BoosterPercent { const groups = new Map(); for (const { booster, eventCode } of active) { const key = `${eventCode ?? ''}/${booster.booster.personal}`; let group = groups.get(key); if (!group) groups.set(key, group = { wp: [], rp: [] }); if (booster.booster.wpPercent > 0) group.wp.push(booster.booster.wpPercent); if (booster.booster.rpPercent > 0) group.rp.push(booster.booster.rpPercent); } let wp = 0; let rp = 0; for (const group of groups.values()) { wp += stacked(group.wp); rp += stacked(group.rp); } return { wp, rp }; } /** calc_public_boost (= calc_personal_boost) of scripts globals/ranks_common_shared.nut. */ export function stacked(percents: readonly number[]): number { const last = STACK_FACTORS[STACK_FACTORS.length - 1]!; let total = 0; [...percents].sort((a, b) => b - a).forEach((percent, i) => { if (i < STACK_FACTORS.length) total += STACK_FACTORS[i]! * percent; else total += last * percent > 1 ? last * percent : 1; }); return Math.round(total); }