diff --git a/packages/local-service/src/battle/battle-repair.ts b/packages/local-service/src/battle/battle-repair.ts index cae3be8..fbd3160 100644 --- a/packages/local-service/src/battle/battle-repair.ts +++ b/packages/local-service/src/battle/battle-repair.ts @@ -17,6 +17,9 @@ * - Several lives of one vehicle add up, at most the full cost (offline design). * - The cost becomes damage: hp = 1 - cost / full cost, so that the client's own repair cost * (1 - hp) x full cost (repair/repair-rules.ts) is that cost. + * - One available free repair covers the vehicle's new damage at settlement. Consuming it + * once per damaged vehicle per battle, regardless of the paid auto-repair setting, is inferred. + * Reserve vehicles have no repair cost, so they never need to spend an allowance. * - "Automatically repair all vehicles after the battle" (autoRepairAircrafts, on by default, * aces.exe 0x90b6a0) repairs the vehicles damaged in the battle when the Silver Lions suffice. * - Premium account insurance: with automatic repair and automatic ammunition purchase on, when @@ -27,6 +30,7 @@ import type { BlkBlock } from '@wto/blk'; import type { RepairMode } from '../game-data/datasets/vehicle-repair.ts'; import { canAfford, pay } from '../profile/profile-currency.ts'; import { clearVehicleDamage, setVehicleDamage, vehicleCondition } from '../profile/profile-vehicle-condition.ts'; +import { consumeFreeRepair } from '../profile/profile-vehicle-state.ts'; import type { ServerTime } from '../profile/server-time.ts'; import { gameModeIndexOf, type RepairRules } from '../repair/repair-rules.ts'; import type { BattleReport } from './battle-report.ts'; @@ -38,7 +42,7 @@ const BATTLE_END_TOLERANCE_SEC = 1; export interface VehicleDamage { readonly vehicle: string; - /** Condition after the battle, before any repair. */ + /** Condition after the battle and any free repair, before a paid repair. */ readonly hp: number; } @@ -65,7 +69,14 @@ export function damageBattleVehicles( const full = rules.fullCost(profile, vehicle, mode); const before = vehicleCondition(profile, vehicle)?.hp ?? 1; const hp = full > 0 ? Math.max(0, before - Math.min(full, cost) / full) : before; - if (hp < before) setVehicleDamage(profile, vehicle, hp, gameModeIndexOf(mode), now); + if (hp < before) { + if (consumeFreeRepair(profile, vehicle, rules.vehicle(vehicle)?.freeRepairs ?? 0)) { + clearVehicleDamage(profile, vehicle); + damages.push({ vehicle, hp: 1 }); + continue; + } + setVehicleDamage(profile, vehicle, hp, gameModeIndexOf(mode), now); + } damages.push({ vehicle, hp }); } return damages; diff --git a/packages/local-service/src/game-data/datasets/vehicle-repair.ts b/packages/local-service/src/game-data/datasets/vehicle-repair.ts index 46133bb..2b4628c 100644 --- a/packages/local-service/src/game-data/datasets/vehicle-repair.ts +++ b/packages/local-service/src/game-data/datasets/vehicle-repair.ts @@ -6,6 +6,7 @@ * repairCostPerMin:i repair cost per minute of the vehicle's lifetime in a battle * repairTimeHrs:r full repair time in hours with a crew * repairTimeHrsNoCrew:r full repair time in hours when no crew has the vehicle + * freeRepairs:i free repair allowance (falls back to warpoints.blk, then 0) * modifications{ { repairCostCoef:r repairCostCoef:r } } added to the cost factor * char.vromfs.bin config/warpoints.blk: rTimeNoCrewMul:r (no-crew time = repairTimeHrs x it when * repairTimeHrsNoCrew is missing). @@ -30,6 +31,8 @@ export interface VehicleRepair { readonly name: string; readonly cost: PerRepairMode; readonly costPerMin: PerRepairMode; + /** Allowance shown by scripts/unit/unit.nut and scripts/airinfo.nut. */ + readonly freeRepairs: number; /** Hours; 0 when the data has none. */ readonly timeHours: PerRepairMode; /** Hours; -1 when the data has none (the client's default, 0x964e30). */ @@ -61,8 +64,10 @@ const SPECIALIZATION_ADDS = 4; export const vehicleRepair: Dataset = { id: 'vehicle-repair', - version: 2, + version: 3, extract: (source) => { + const warpoints = source.readBlk('char.vromfs.bin', 'config/warpoints.blk'); + const defaultFreeRepairs = numberField(warpoints, 'freeRepairs') ?? 0; const vehicles = source.readBlk('char.vromfs.bin', 'config/wpcost.blk').blocks.flatMap((vehicle): VehicleRepair[] => { const cost = perMode(vehicle, 'repairCost', 0); if (REPAIR_MODES.every((mode) => cost[mode] <= 0)) return []; @@ -76,12 +81,12 @@ export const vehicleRepair: Dataset = { name: vehicle.name, cost, costPerMin: perMode(vehicle, 'repairCostPerMin', 0), + freeRepairs: Math.max(0, Math.trunc(numberField(vehicle, 'freeRepairs') ?? defaultFreeRepairs)), timeHours: perMode(vehicle, 'repairTimeHrs', 0), noCrewTimeHours: perMode(vehicle, 'repairTimeHrsNoCrew', -1), modCoefs, }]; }); - const warpoints = source.readBlk('char.vromfs.bin', 'config/warpoints.blk'); return { vehicles, noCrewTimeMul: numberField(warpoints, 'rTimeNoCrewMul') ?? 1, diff --git a/packages/local-service/src/profile/profile-vehicle-state.ts b/packages/local-service/src/profile/profile-vehicle-state.ts index 918ae0a..8936590 100644 --- a/packages/local-service/src/profile/profile-vehicle-state.ts +++ b/packages/local-service/src/profile/profile-vehicle-state.ts @@ -7,10 +7,13 @@ * tierNumber:i the highest open modification tier (script is_tier_available) * spareAircraftsCount:i spares the player has for this vehicle * excessExp:i64 banked modification research points (+0x70) + * freeRepairs:i64 free repairs used (0x941cef; shop_get_free_repairs_used at 0x17a4b80) */ import { findBlock, findParam, setParam, type BlkBlock } from '@wto/blk'; +import { intField } from './blk-fields.ts'; const HANGAR = 'hangar'; +const FREE_REPAIRS = 'freeRepairs'; const RESEARCH_TARGET = 'investMode'; const FINISHED_TARGET = 'investModePrev'; const OPEN_TIER = 'tierNumber'; @@ -52,6 +55,16 @@ export function setSpareCount(profile: BlkBlock, vehicle: string, count: number) if (entry) setParam(entry, SPARES, { type: 'int', value: count }); } +/** Uses one free repair on an owned vehicle if it still has an allowance. */ +export function consumeFreeRepair(profile: BlkBlock, vehicle: string, allowance: number): boolean { + const entry = vehicleEntry(profile, vehicle); + if (!entry) return false; + const used = Math.max(0, intField(entry, FREE_REPAIRS)); + if (used >= allowance) return false; + setParam(entry, FREE_REPAIRS, { type: 'int64', value: BigInt(used + 1) }); + return true; +} + export function vehicleExcessRp(profile: BlkBlock, vehicle: string): number { const value = fieldOf(profile, vehicle, EXCESS_EXP); if (value?.type === 'int64') return Math.max(0, Number(value.value));