diff --git a/packages/local-service/src/char/actions/action-table.ts b/packages/local-service/src/char/actions/action-table.ts index 4650f22..fe5d469 100644 --- a/packages/local-service/src/char/actions/action-table.ts +++ b/packages/local-service/src/char/actions/action-table.ts @@ -5,7 +5,7 @@ import type { ResearchRules } from '../../research/research-rules.ts'; import type { CharActionHandler } from '../char-action.ts'; import { acceptEula } from './agreement-actions.ts'; import { bulkTrainCrews, trainCrew } from './crew-actions.ts'; -import { buyAllModificationsAction, buyModificationAction, buySparesAction, enableModificationsAction } from './modification-actions.ts'; +import { buyAllModificationsAction, buyModificationAction, buySparesAction, buyWeaponsAction, enableModificationsAction } from './modification-actions.ts'; import { purchaseSkillPointsAction, specializeCrewAction, upgradeCrewAction } from './crew-skill-actions.ts'; import { savePilotAppearanceAction, selectTitleAction, usersTerseInfoAction } from './player-card-actions.ts'; import { buyResourceAction } from './decor-actions.ts'; @@ -40,6 +40,7 @@ export function actionTable(deps: ActionDependencies): ReadonlyMap 0x29a890; lab round 18): * { :b=yes ... } ... */ @@ -14,6 +15,7 @@ import type { BlkBlock } from '@wto/blk'; import type { AccountStore } from '../../accounts/account-store.ts'; import { buyAllModifications, buyModification, enableModifications } from '../../modifications/modification-purchase.ts'; import { buySpares } from '../../modifications/spare-purchase.ts'; +import { buyWeapons } from '../../modifications/weapon-purchase.ts'; import type { PlayerProfiles } from '../../profile/player-profiles.ts'; import type { ResearchRules } from '../../research/research-rules.ts'; import type { CharActionHandler } from '../char-action.ts'; @@ -56,6 +58,16 @@ export function buySparesAction(profiles: PlayerProfiles, accounts: AccountStore }); } +export function buyWeaponsAction(profiles: PlayerProfiles, accounts: AccountStore, rules: ResearchRules): CharActionHandler { + return ruleAction(profiles, accounts, (payload) => { + const vehicle = stringParam(payload, 'aircraft'); + const weapon = stringParam(payload, 'weapon'); + if (vehicle === undefined || weapon === undefined) return undefined; + const order = { vehicle, weapon, amount: intParam(payload, 'count') ?? 1, offered: offeredPrice(payload) }; + return (profile) => buyWeapons(profile, rules.equipment, order); + }); +} + export function enableModificationsAction(profiles: PlayerProfiles, accounts: AccountStore): CharActionHandler { return ruleAction(profiles, accounts, (payload) => { const switches = payload.blocks.flatMap((vehicle) => vehicle.params.flatMap((param) => diff --git a/packages/local-service/src/game-data/datasets/vehicle-equipment.ts b/packages/local-service/src/game-data/datasets/vehicle-equipment.ts index a706796..4b5fa76 100644 --- a/packages/local-service/src/game-data/datasets/vehicle-equipment.ts +++ b/packages/local-service/src/game-data/datasets/vehicle-equipment.ts @@ -8,6 +8,8 @@ * reqModification:t (repeated) maxCount:i ammoCount:i ... } } * Ammunition and expendables are modifications too (tier 0, maxCount, ammoCount). * weapons{ { value:i costGold:i maxCount:i reqModification:t ... } } + * Paid presets (bombs, rockets) are bought by the piece, up to maxCount; the rest are free, + * custom_presets (the custom preset slots) among them. * spare{ value:i costGold:i } * * Only what the file says is recorded; what it means is the rules layer's (modifications/). @@ -38,10 +40,13 @@ export interface ModificationFacts { readonly ammunition: boolean; } -export interface PriceFacts { +export interface WeaponFacts { readonly name: string; + /** value: silver lions per piece. */ readonly silverLions: number; readonly goldenEagles: number; + /** maxCount; 0 when the preset is not bought by the piece. */ + readonly maxCount: number; } export interface VehicleEquipment { @@ -49,14 +54,14 @@ export interface VehicleEquipment { /** needBuyToOpenNextInTier1..4 (index 0 = tier 1). */ readonly tierRequirements: readonly number[]; readonly modifications: readonly ModificationFacts[]; - readonly weapons: readonly PriceFacts[]; + readonly weapons: readonly WeaponFacts[]; /** spare; absent when the vehicle has no spare price. */ readonly spare?: { readonly silverLions: number; readonly goldenEagles: number }; } export const vehicleEquipment: Dataset = { id: 'vehicle-equipment', - version: 1, + version: 2, extract: (source) => source.readBlk('char.vromfs.bin', VEHICLE_ECONOMY).blocks.map(equipmentOf), }; @@ -68,7 +73,7 @@ function equipmentOf(vehicle: BlkBlock): VehicleEquipment { vehicle: vehicle.name, tierRequirements, modifications: (findBlock(vehicle, 'modifications')?.blocks ?? []).map(modificationOf), - weapons: (findBlock(vehicle, 'weapons')?.blocks ?? []).map(priceOf), + weapons: (findBlock(vehicle, 'weapons')?.blocks ?? []).map(weaponOf), ...(spare ? { spare: { silverLions: numberField(spare, 'value') ?? 0, goldenEagles: numberField(spare, 'costGold') ?? 0 } } : {}), }; } @@ -87,6 +92,11 @@ function modificationOf(modification: BlkBlock): ModificationFacts { }; } -function priceOf(entry: BlkBlock): PriceFacts { - return { name: entry.name, silverLions: numberField(entry, 'value') ?? 0, goldenEagles: numberField(entry, 'costGold') ?? 0 }; +function weaponOf(entry: BlkBlock): WeaponFacts { + return { + name: entry.name, + silverLions: numberField(entry, 'value') ?? 0, + goldenEagles: numberField(entry, 'costGold') ?? 0, + maxCount: numberField(entry, 'maxCount') ?? 0, + }; } diff --git a/packages/local-service/src/modifications/equipment-catalogue.ts b/packages/local-service/src/modifications/equipment-catalogue.ts index 875c012..4adf95c 100644 --- a/packages/local-service/src/modifications/equipment-catalogue.ts +++ b/packages/local-service/src/modifications/equipment-catalogue.ts @@ -2,7 +2,7 @@ * Lookup over the vehicle-equipment dataset (game-data/datasets/vehicle-equipment.ts), plus the * classifications the rules need. */ -import type { ModificationFacts, VehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts'; +import type { ModificationFacts, VehicleEquipment, WeaponFacts } from '../game-data/datasets/vehicle-equipment.ts'; export class EquipmentCatalogue { readonly #byVehicle: ReadonlyMap; @@ -18,6 +18,24 @@ export class EquipmentCatalogue { modification(vehicle: string, name: string): ModificationFacts | undefined { return this.#byVehicle.get(vehicle)?.modifications.find((modification) => modification.name === name); } + + weapon(vehicle: string, name: string): WeaponFacts | undefined { + return this.#byVehicle.get(vehicle)?.weapons.find((weapon) => weapon.name === name); + } +} + +/** Bought by the piece (bombs, rockets): has a price and a maxCount. */ +export function isPaidWeapon(weapon: WeaponFacts): boolean { + return (weapon.silverLions > 0 || weapon.goldenEagles > 0) && weapon.maxCount > 0; +} + +/** + * Comes with the vehicle. The client lets a preset be chosen only when the player has some of + * it (scripts weaponry/weaponryPresetsWnd.nut doItemAction: getItemAmount, which is + * shop_is_weapon_purchased, the record's count), so a free preset needs a record too. + */ +export function isFreeWeapon(weapon: WeaponFacts): boolean { + return weapon.silverLions === 0 && weapon.goldenEagles === 0; } /** Has to be researched before it can be bought (tier 1 and up, with research points). */ @@ -33,6 +51,17 @@ export function countsForTier(modification: ModificationFacts): boolean { return isResearchable(modification) && modification.goldenEagles === 0 && !modification.ammunition; } +/** + * Comes with the vehicle: no price, no outright price, no research points and nothing required + * (most modifications of reserve and premium vehicles). The client counts such a modification as + * owned without a record (scripts weaponry/initWeaponryTypes.nut MODIFICATION getAmount), but + * whether it is mounted comes from the record alone (shopIsModificationEnabled). + */ +export function isFree(modification: ModificationFacts): boolean { + return modification.silverLions === 0 && modification.goldenEagles === 0 && modification.openGoldenEagles === 0 + && modification.researchPoints === 0 && modification.requires.length === 0; +} + /** Bought in amounts (ammunition, expendables): maxCount above 1. */ export function isCounted(modification: ModificationFacts): boolean { return modification.maxCount > 1; diff --git a/packages/local-service/src/modifications/module-state.ts b/packages/local-service/src/modifications/module-state.ts index 4e35187..916b7da 100644 --- a/packages/local-service/src/modifications/module-state.ts +++ b/packages/local-service/src/modifications/module-state.ts @@ -2,22 +2,30 @@ * Bringing the modification state of a profile up to date, like research/research-state.ts does * for vehicles: whenever a profile is handed out or changed (profiles-setup.ts), for every owned * vehicle + * modifications a record for every free modification (equipment-catalogue.ts + * isFree), so it comes mounted and can be switched off and on + * weapons a record for every free weapon preset (isFreeWeapon), so it + * can be chosen * hangar//tierNumber the highest open tier * modsResearch the status of every modification not owned yet; locked ones * only keep an entry while they hold research points * hangar//investMode cleared when that modification is no longer researchable */ import type { BlkBlock } from '@wto/blk'; +import type { VehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts'; import { ownedVehicles } from '../profile/profile-hangar.ts'; +import { ownsModification, writeModificationRecord } from '../profile/profile-modifications.ts'; import { moduleResearchPoints, removeModuleResearch, setModuleStatus } from '../profile/profile-module-research.ts'; +import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts'; import { moduleResearchTarget, setModuleResearchTarget, setOpenTier } from '../profile/profile-vehicle-state.ts'; -import type { EquipmentCatalogue } from './equipment-catalogue.ts'; +import { isFree, isFreeWeapon, type EquipmentCatalogue } from './equipment-catalogue.ts'; import { ModuleStatus, vehicleModuleState } from './module-status.ts'; export function refreshModuleState(profile: BlkBlock, catalogue: EquipmentCatalogue): void { for (const vehicle of ownedVehicles(profile)) { const equipment = catalogue.vehicle(vehicle); if (!equipment) continue; + grantFreeEquipment(profile, equipment); const state = vehicleModuleState(profile, equipment); setOpenTier(profile, vehicle, state.openTier); for (const modification of equipment.modifications) { @@ -38,3 +46,14 @@ export function currentModuleStatus(profile: BlkBlock, catalogue: EquipmentCatal if (!equipment || !ownedVehicles(profile).has(vehicle)) return undefined; return vehicleModuleState(profile, equipment).statuses.get(modification); } + +function grantFreeEquipment(profile: BlkBlock, equipment: VehicleEquipment): void { + for (const modification of equipment.modifications) { + if (!isFree(modification) || ownsModification(profile, equipment.vehicle, modification.name)) continue; + writeModificationRecord(profile, equipment.vehicle, modification.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, enabled: true, count: 1 }); + } + for (const weapon of equipment.weapons) { + if (!isFreeWeapon(weapon) || weaponRecord(profile, equipment.vehicle, weapon.name)) continue; + writeWeaponRecord(profile, equipment.vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: 1 }); + } +} diff --git a/packages/local-service/src/modifications/weapon-purchase.ts b/packages/local-service/src/modifications/weapon-purchase.ts new file mode 100644 index 0000000..1067cb6 --- /dev/null +++ b/packages/local-service/src/modifications/weapon-purchase.ts @@ -0,0 +1,41 @@ +/** + * Buying weapon presets (cln_buy_weapon: aircraft:t weapon:t count:i cost:i costGold:i, scripts + * weaponry/weaponsPurchase.nut sendPurchaseWeaponRequest; implementation notes). + * + * Paid presets (bombs, rockets) are bought by the piece: the price is the wpcost.blk value times + * the amount, and the player holds at most maxCount pieces (wp_get_weapon_max_count; the client + * offers maxCount minus what it has). Pieces are spent in battle. Free presets come with the + * vehicle (module-state.ts) and are never bought. + */ +import type { BlkBlock } from '@wto/blk'; +import { canAfford, pay, type Price } from '../profile/profile-currency.ts'; +import { ownsVehicle } from '../profile/profile-hangar.ts'; +import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts'; +import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts'; +import { isPaidWeapon, type EquipmentCatalogue } from './equipment-catalogue.ts'; + +export interface WeaponOrder { + readonly vehicle: string; + readonly weapon: string; + readonly amount: number; + readonly offered: Price; +} + +export function buyWeapons(profile: BlkBlock, catalogue: EquipmentCatalogue, order: WeaponOrder): RuleOutcome { + const weapon = catalogue.weapon(order.vehicle, order.weapon); + if (!weapon || !ownsVehicle(profile, order.vehicle) || order.amount < 1) return refused('BAD_PARAMS'); + if (!isPaidWeapon(weapon)) return refused('NOT_AVAILABLE'); + const record = weaponRecord(profile, order.vehicle, order.weapon); + const held = record?.count ?? 0; + if (held + order.amount > weapon.maxCount) return refused('NOT_AVAILABLE'); + const price: Price = { silverLions: weapon.silverLions * order.amount, goldenEagles: weapon.goldenEagles * order.amount }; + if (price.silverLions !== order.offered.silverLions || price.goldenEagles !== order.offered.goldenEagles) return refused('PRICE_CHANGED'); + if (!canAfford(profile, price)) return refused('NOT_ENOUGH_MONEY'); + pay(profile, price); + writeWeaponRecord(profile, order.vehicle, order.weapon, { + silverLionsSpent: (record?.silverLionsSpent ?? 0) + price.silverLions, + goldenEaglesSpent: (record?.goldenEaglesSpent ?? 0) + price.goldenEagles, + count: held + order.amount, + }); + return ACCEPTED; +} diff --git a/packages/local-service/src/profile/profile-module-research.ts b/packages/local-service/src/profile/profile-module-research.ts index 4da3b94..f67dab8 100644 --- a/packages/local-service/src/profile/profile-module-research.ts +++ b/packages/local-service/src/profile/profile-module-research.ts @@ -1,19 +1,22 @@ /** * Modification research in the profile: * - * modsResearch{ us_m22_locust_new_tank_tracks{ unitStatus:i=2 exp:i64=120 } } + * modsResearch{ us_m22_locust_new_tank_tracks{ exp:i64=120 modStatus:i=2 } } * * One block per modification of an owned vehicle that is not bought yet, named - * _; each block has the layout of a unitResearch entry (the client reads - * both with aces.exe 0x943990): unitStatus is the status the server works out, - * exp the research points already put into it. + * _. The client reads each block with aces.exe 0x9403b0, by name: + * exp:i64 research points already put into it + * modStatus:i the status the server works out; 1 (locked) when absent + * (it also reads goldSpent:i, which the offline service never sets). An entry is always + * rewritten whole, so it holds nothing else. */ -import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk'; +import { ensureBlock, findBlock, findParam, type BlkBlock, type BlkParam } from '@wto/blk'; import { recordName } from './profile-modifications.ts'; const MODS_RESEARCH = 'modsResearch'; -const STATUS = 'unitStatus'; +const STATUS = 'modStatus'; const RESEARCH_POINTS = 'exp'; +const LOCKED = 1; export function moduleResearchPoints(profile: BlkBlock, vehicle: string, modification: string): number { const entry = entryOf(profile, vehicle, modification); @@ -23,11 +26,11 @@ export function moduleResearchPoints(profile: BlkBlock, vehicle: string, modific } export function setModuleResearchPoints(profile: BlkBlock, vehicle: string, modification: string, amount: number): void { - setParam(ensureEntry(profile, vehicle, modification), RESEARCH_POINTS, { type: 'int64', value: BigInt(amount) }); + writeEntry(profile, vehicle, modification, amount, moduleStatusOf(profile, vehicle, modification)); } export function setModuleStatus(profile: BlkBlock, vehicle: string, modification: string, status: number): void { - setParam(ensureEntry(profile, vehicle, modification), STATUS, { type: 'int', value: status }); + writeEntry(profile, vehicle, modification, moduleResearchPoints(profile, vehicle, modification), status); } export function removeModuleResearch(profile: BlkBlock, vehicle: string, modification: string): void { @@ -36,11 +39,20 @@ export function removeModuleResearch(profile: BlkBlock, vehicle: string, modific if (block) block.blocks = block.blocks.filter((entry) => entry.name !== name); } +function moduleStatusOf(profile: BlkBlock, vehicle: string, modification: string): number { + const entry = entryOf(profile, vehicle, modification); + const value = entry && findParam(entry, STATUS)?.value; + return value?.type === 'int' ? value.value : LOCKED; +} + +function writeEntry(profile: BlkBlock, vehicle: string, modification: string, researchPoints: number, status: number): void { + const params: BlkParam[] = []; + if (researchPoints) params.push({ name: RESEARCH_POINTS, value: { type: 'int64', value: BigInt(researchPoints) } }); + params.push({ name: STATUS, value: { type: 'int', value: status } }); + ensureBlock(ensureBlock(profile, MODS_RESEARCH), recordName(vehicle, modification)).params = params; +} + function entryOf(profile: BlkBlock, vehicle: string, modification: string): BlkBlock | undefined { const block = findBlock(profile, MODS_RESEARCH); return block && findBlock(block, recordName(vehicle, modification)); } - -function ensureEntry(profile: BlkBlock, vehicle: string, modification: string): BlkBlock { - return ensureBlock(ensureBlock(profile, MODS_RESEARCH), recordName(vehicle, modification)); -} diff --git a/packages/local-service/src/profile/profile-weapons.ts b/packages/local-service/src/profile/profile-weapons.ts new file mode 100644 index 0000000..30aaf90 --- /dev/null +++ b/packages/local-service/src/profile/profile-weapons.ts @@ -0,0 +1,53 @@ +/** + * Weapon presets the player has, in the profile: + * + * weapons{ + * p-26a_34_m2_P-26A_1934_default{ count:i64=1 } + * p-26a_34_m2_P-26A_1934_2x100lbs{ wpSpent:i=450 count:i64=30 } + * } + * + * One block per preset, named _ (aces.exe 0x15562f0 shop_is_weapon_purchased + * builds the same key; presets named custom... share _custom_presets). The client reads + * a block's params strictly in this order and stops at the first that does not match + * (0x942040, log "WEAPONS: Not all params was read"), so a record is always rewritten whole: + * wpSpent:i goldSpent:i lastWpCost:i lastGoldCost:i count:i64 + * count is what shop_is_weapon_purchased returns: the pieces the player has; no block means 0. + */ +import { ensureBlock, findBlock, type BlkBlock, type BlkParam } from '@wto/blk'; + +const WEAPONS = 'weapons'; + +export interface WeaponRecord { + readonly silverLionsSpent: number; + readonly goldenEaglesSpent: number; + readonly count: number; +} + +export function weaponRecord(profile: BlkBlock, vehicle: string, weapon: string): WeaponRecord | undefined { + const block = findBlock(profile, WEAPONS); + const record = block && findBlock(block, recordName(vehicle, weapon)); + if (!record) return undefined; + return { silverLionsSpent: intOf(record, 'wpSpent'), goldenEaglesSpent: intOf(record, 'goldSpent'), count: intOf(record, 'count') }; +} + +export function writeWeaponRecord(profile: BlkBlock, vehicle: string, weapon: string, record: WeaponRecord): void { + ensureBlock(ensureBlock(profile, WEAPONS), recordName(vehicle, weapon)).params = orderedParams(record); +} + +function recordName(vehicle: string, weapon: string): string { + return `${vehicle}_${weapon}`; +} + +function orderedParams(record: WeaponRecord): BlkParam[] { + const params: BlkParam[] = []; + if (record.silverLionsSpent) params.push({ name: 'wpSpent', value: { type: 'int', value: record.silverLionsSpent } }); + if (record.goldenEaglesSpent) params.push({ name: 'goldSpent', value: { type: 'int', value: record.goldenEaglesSpent } }); + if (record.count) params.push({ name: 'count', value: { type: 'int64', value: BigInt(record.count) } }); + return params; +} + +function intOf(block: BlkBlock, name: string): number { + const value = block.params.find((param) => param.name === name)?.value; + if (value?.type === 'int64') return Number(value.value); + return value?.type === 'int' ? value.value : 0; +}