From 7615394e621c06b0e71ed6311e6903a950ab3f1e Mon Sep 17 00:00:00 2001 From: chomp Date: Mon, 6 Mar 2023 18:17:43 +0000 Subject: [PATCH] Fix: don't overfill magazine inside `fillMagazineWithCartridge()` (!33) Co-authored-by: Dev Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/33 --- project/src/helpers/ItemHelper.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/project/src/helpers/ItemHelper.ts b/project/src/helpers/ItemHelper.ts index 2c1d71c9..677283a1 100644 --- a/project/src/helpers/ItemHelper.ts +++ b/project/src/helpers/ItemHelper.ts @@ -869,18 +869,25 @@ class ItemHelper // Get max number of cartridges in magazine, choose random value between min/max const magazineCartridgeMaxCount = magTemplate._props.Cartridges[0]._max_count; - const stackCount = this.randomUtil.getInt(Math.round(minSizePercent * magazineCartridgeMaxCount), magazineCartridgeMaxCount); + const desiredStackCount = this.randomUtil.getInt(Math.round(minSizePercent * magazineCartridgeMaxCount), magazineCartridgeMaxCount); // Loop over cartridge count and add stacks to magazine let currentStoredCartridgeCount = 0; let location = 0; - while (currentStoredCartridgeCount < stackCount) + while (currentStoredCartridgeCount < desiredStackCount) { // Get stack size of cartridges - const cartridgeCountToAdd = (stackCount <= cartridgeMaxStackSize) - ? stackCount + let cartridgeCountToAdd = (desiredStackCount <= cartridgeMaxStackSize) + ? desiredStackCount : cartridgeMaxStackSize; + // Ensure we don't go over the max stackcount size + const remainingSpace = desiredStackCount - currentStoredCartridgeCount; + if (cartridgeCountToAdd > remainingSpace) + { + cartridgeCountToAdd = remainingSpace; + } + // Add cartridge item object into items array magazine.push(this.createCartridges(magazine[0]._id, cartridgeTpl, cartridgeCountToAdd, location));