Files
WT-Offline/apps/server/src/battle/battle-economy.ts
T
N0VAN0M4LYandduduyagame 6d0abf9caf fix(battle): award Air Arcade mini-base score
Use the native health update identified in PR #10 to report enemy mini-base damage and destruction. Preserve fractional personal score across hits and use the selected mode to gate both the HUD reward and settlement.

Verified server and launcher types, MSVC compilation, Quirrel bytecode, and patch resolution on 2.61.0.37, 2.61.0.58, and 2.61.0.60. In-game acceptance is pending.

Co-authored-by: duduyagame <[email protected]>
2026-12-04 06:13:21 +08:00

259 lines
11 KiB
TypeScript

/**
* Prices the local player's combat events: the one place battle rewards are computed.
*
* The in-battle banner and the settlement use the same two steps, so what the banner shows is
* what the settlement pays:
* 1. baseReward(vehicle, event, victim group): Silver Lions and Research Points of the event
* before the destroyed unit's class factor (sent to the client at launch, `bannerParams`);
* 2. x class factor of the victim's expClass (warpoints.blk / rank.blk units{}), x aiKillK when
* the victim is not a player, x damage points for naval damage.
* The formula and the source of every factor are in tuning/battle-economy.jsonc.
*/
import type { BattleEconomyData, EconomyDifficulty, VehicleEconomy } from '../game-data/datasets/battle-economy.ts';
import type { ModeDifficulty } from '../game-modes/catalogue/game-mode-declaration.ts';
import { SCORED_EVENTS, VICTIM_GROUPS, type BattleEconomyTuning, type ScoredEvent, type VictimGroup } from './battle-economy-tuning.ts';
/** EXP_EVENT_* codes the LocalMP host reports (gui.vromfs.bin globalScripts/expEventConsts.nut). */
export const ExpEvent = {
CAPTURE_ZONE: 2,
DESTROY_ZONE: 3,
KILL: 5,
KILL_GROUND: 6,
ASSIST: 9,
CRITICAL_HIT: 10,
HIT: 13,
DAMAGE_ZONE: 17,
UNIT_DAMAGE: 24,
SEVERE_DAMAGE: 35,
KILL_HUMAN: 39,
} as const;
/** Debriefing rows (`stat_get_exp()` `num<Row>`/`wp<Row>`/`exp<Row>`, aces.exe 0x1053b70) an event lands in. */
export type DebriefingRow =
| 'AirKills' | 'GroundKills' | 'NavalKills' | 'HumanKills' | 'FriendlyKills'
| 'Assist' | 'AirSevereDamage' | 'ShipSevereDamage' | 'Critical' | 'Hit'
| 'Captures' | 'Damage' | 'Destruction' | 'AwardDamage';
/** LocalMP reports mini-base health as basis points (local_mp_zones.c). */
const ZONE_HEALTH_SCALE = 10_000;
function isBombingZoneEvent(code: number): boolean {
return code === ExpEvent.DAMAGE_ZONE || code === ExpEvent.DESTROY_ZONE;
}
/** What the client reports for one combat event (patches/gui/battle/record-local-battle-awards). */
export interface CombatEventFact {
readonly code: number;
readonly expClass: string;
readonly victimIsPlayer: boolean;
readonly friendly: boolean;
/** Naval damage points or, for DAMAGE_ZONE, basis points of the zone's full health. */
readonly damage: number;
}
export interface PricedEvent {
readonly row: DebriefingRow;
/** How much the stock row's counter grows. Damage counts TNT mass, which is unavailable here. */
readonly count: number;
readonly wp: number;
readonly exp: number;
}
export interface RewardPair {
readonly wp: number;
readonly exp: number;
}
/**
* Launch-time parameters of the in-battle banner (`battle.awards` of the launch message):
* vehicles.<name>["<code>:<group>"] = [wp, exp] before the class factor; capture uses the group
* "own". The client multiplies by slClass/rpClass of the victim's expClass, by aiKillK for a
* victim that is not a player, by the damage points for naval damage, and shows a friendly kill as
* -wp x friendlyKillPenaltyMul.
*/
export interface BannerParams {
readonly vehicles: Readonly<Record<string, Readonly<Record<string, readonly [number, number]>>>>;
readonly groups: Readonly<Record<string, VictimGroup>>;
readonly slClass: Readonly<Record<string, number>>;
readonly rpClass: Readonly<Record<string, number>>;
readonly aiKillK: number;
readonly friendlyKillPenaltyMul: number;
readonly zoneHealthScale: number;
}
export interface RewardMultipliers {
readonly silverLions: number;
readonly researchPoints: number;
}
export function economyDifficulty(difficulty: ModeDifficulty): EconomyDifficulty {
return difficulty === 'arcade' ? 'arcade' : difficulty === 'realistic' ? 'historical' : 'simulation';
}
export class BattleEconomy {
readonly data: BattleEconomyData;
readonly #tuning: BattleEconomyTuning;
readonly #multipliers: RewardMultipliers;
readonly #premiumVehicles: ReadonlySet<string>;
/** `premiumVehicles`: vehicles the client counts as special (isUnitSpecial: costGold > 0 or premPackAir). */
constructor(
data: BattleEconomyData,
tuning: BattleEconomyTuning,
multipliers: RewardMultipliers,
premiumVehicles: ReadonlySet<string>,
) {
this.data = data;
this.#tuning = tuning;
this.#multipliers = multipliers;
this.#premiumVehicles = premiumVehicles;
}
/** Premium vehicles earn the talisman research bonus without a talisman (scripts/airinfo.nut hasTalisman). */
isPremiumVehicle(name: string): boolean {
return this.#premiumVehicles.has(name);
}
get multipliers(): RewardMultipliers {
return this.#multipliers;
}
vehicle(name: string): VehicleEconomy | undefined {
return this.data.vehicles[name];
}
victimGroup(expClass: string): VictimGroup {
return this.#tuning.victimGroups.get(expClass) ?? 'ground';
}
/** Reward of one event before the victim's class factor; undefined for an event that pays nothing. */
baseReward(vehicle: string, difficulty: EconomyDifficulty, code: number, group: VictimGroup): RewardPair | undefined {
const economy = this.vehicle(vehicle);
if (!economy) return undefined;
const score = this.#score(code, group);
if (score === undefined) return undefined;
const reference = this.#tuning.referenceScore[code === ExpEvent.UNIT_DAMAGE ? 'naval' : group];
return {
wp: this.data.killSilverLions * economy.rewardMul[difficulty] * (score / reference) * this.#multipliers.silverLions,
exp: score * economy.expMul * this.#multipliers.researchPoints,
};
}
price(vehicle: string, difficulty: EconomyDifficulty, modeName: string, fact: CombatEventFact): PricedEvent | undefined {
const economy = this.vehicle(vehicle);
if (!economy) return undefined;
const isCapture = fact.code === ExpEvent.CAPTURE_ZONE;
const isBombingZone = isBombingZoneEvent(fact.code);
const isZoneDamage = fact.code === ExpEvent.DAMAGE_ZONE;
if (isBombingZone && (modeName !== 'air_arcade' || fact.friendly)) return undefined;
if (isZoneDamage && (!Number.isInteger(fact.damage) || fact.damage < 1 || fact.damage > ZONE_HEALTH_SCALE)) return undefined;
const isObjective = isCapture || isBombingZone;
const group = isBombingZone ? 'ground' : this.victimGroup(isCapture ? economy.unitClass : fact.expClass);
const row = debriefingRow(fact, group);
const base = this.baseReward(vehicle, difficulty, fact.code, group);
if (!row || !base) return undefined;
const count = fact.code === ExpEvent.UNIT_DAMAGE ? Math.max(0, Math.trunc(fact.damage)) : 1;
const quantity = isZoneDamage ? fact.damage / ZONE_HEALTH_SCALE : count;
const ai = isObjective || fact.victimIsPlayer ? 1 : this.data.aiKillK;
const slClass = isObjective ? 1 : this.data.silverLionsClassFactors[fact.expClass] ?? 1;
const rpClass = isObjective ? 1 : this.data.researchClassFactors[fact.expClass] ?? 1;
if (fact.friendly) {
return { row: 'FriendlyKills', count: 1, wp: -Math.round(base.wp * slClass * ai * this.#tuning.friendlyKillPenaltyMul), exp: 0 };
}
return {
row,
count: isZoneDamage ? 0 : count,
wp: Math.round(base.wp * slClass * ai * quantity),
exp: Math.round(base.exp * rpClass * ai * quantity),
};
}
bannerParams(vehicles: Iterable<string>, difficulty: EconomyDifficulty, modeName: string): BannerParams {
const table: Record<string, Record<string, [number, number]>> = {};
for (const name of vehicles) {
const economy = this.vehicle(name);
if (!economy || table[name]) continue;
const entries: Record<string, [number, number]> = {};
for (const code of Object.values(ExpEvent)) {
const isBombingZone = isBombingZoneEvent(code);
if (isBombingZone && modeName !== 'air_arcade') continue;
const groups: readonly (VictimGroup | 'own')[] = code === ExpEvent.CAPTURE_ZONE ? ['own']
: isBombingZone ? ['ground'] : VICTIM_GROUPS;
for (const group of groups) {
const priceGroup = group === 'own' ? this.victimGroup(economy.unitClass) : group;
const base = this.baseReward(name, difficulty, code, priceGroup);
if (base) entries[`${code}:${group}`] = [base.wp, base.exp];
}
}
table[name] = entries;
}
return {
vehicles: table,
groups: Object.fromEntries(this.#tuning.victimGroups),
slClass: this.data.silverLionsClassFactors,
rpClass: this.data.researchClassFactors,
aiKillK: this.data.aiKillK,
friendlyKillPenaltyMul: this.#tuning.friendlyKillPenaltyMul,
zoneHealthScale: ZONE_HEALTH_SCALE,
};
}
/**
* The scoreboard score of each combat event, for the mission settings block customScoreParams
* that the native LocalMP bridge reads (native/wto-net/src/local_mp_scores.c lists the keys):
* the official mission scores of tuning/battle-economy.jsonc, and aiKillK for victims that are
* not players, as in the rewards.
*/
missionScores(modeName: string): Readonly<Record<string, number>> {
const scores: Record<string, number> = {};
for (const group of VICTIM_GROUPS) {
for (const event of SCORED_EVENTS) scores[`wtoScore_${event}_${group}`] = this.#tuning.eventScores[group][event];
scores[`wtoScore_capture_${group}`] = this.#tuning.captureZoneScore[group];
}
scores.wtoScore_damagePerPoint = this.#tuning.navalDamageScorePerPoint;
scores.wtoScore_aiMul = this.data.aiKillK;
if (modeName === 'air_arcade') {
scores.wtoScore_zoneDamage = this.#tuning.bombingZone.damageScore;
scores.wtoScore_zoneDestruction = this.#tuning.bombingZone.destructionScore;
}
return scores;
}
#score(code: number, group: VictimGroup): number | undefined {
const scored = (event: ScoredEvent): number => this.#tuning.eventScores[group][event];
switch (code) {
case ExpEvent.KILL:
case ExpEvent.KILL_GROUND:
case ExpEvent.KILL_HUMAN: return scored('kill');
case ExpEvent.ASSIST: return scored('assist');
case ExpEvent.CRITICAL_HIT: return scored('criticalHit');
case ExpEvent.HIT: return scored('hit');
case ExpEvent.SEVERE_DAMAGE: return scored('severeDamage');
case ExpEvent.CAPTURE_ZONE: return this.#tuning.captureZoneScore[group];
case ExpEvent.DAMAGE_ZONE: return this.#tuning.bombingZone.damageScore;
case ExpEvent.DESTROY_ZONE: return this.#tuning.bombingZone.destructionScore;
case ExpEvent.UNIT_DAMAGE: return this.#tuning.navalDamageScorePerPoint;
default: return undefined;
}
}
}
function debriefingRow(fact: CombatEventFact, group: VictimGroup): DebriefingRow | undefined {
switch (fact.code) {
case ExpEvent.KILL:
case ExpEvent.KILL_GROUND:
case ExpEvent.KILL_HUMAN:
if (fact.friendly) return 'FriendlyKills';
return group === 'air' ? 'AirKills' : group === 'naval' ? 'NavalKills' : group === 'human' ? 'HumanKills' : 'GroundKills';
case ExpEvent.ASSIST: return 'Assist';
case ExpEvent.CRITICAL_HIT: return 'Critical';
case ExpEvent.HIT: return 'Hit';
case ExpEvent.SEVERE_DAMAGE: return group === 'naval' ? 'ShipSevereDamage' : 'AirSevereDamage';
case ExpEvent.CAPTURE_ZONE: return 'Captures';
case ExpEvent.DAMAGE_ZONE: return 'Damage';
case ExpEvent.DESTROY_ZONE: return 'Destruction';
case ExpEvent.UNIT_DAMAGE: return 'AwardDamage';
default: return undefined;
}
}