From 95a06dfb4b5d2ad7c81224b1f6a055db0a2d6208 Mon Sep 17 00:00:00 2001 From: StempunkDev Date: Thu, 15 Jan 2026 19:45:08 +0100 Subject: [PATCH] refactor: improve getNextId function to fill gaps in ice element IDs(optional commit) --- public/modules/ice.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/public/modules/ice.js b/public/modules/ice.js index e1e86b8e..90c7c3e6 100644 --- a/public/modules/ice.js +++ b/public/modules/ice.js @@ -3,10 +3,15 @@ // Ice layer data model - separates ice data from SVG rendering window.Ice = (function () { - // Find next available id for new ice element + // Find next available id for new ice element idealy filling gaps function getNextId() { if (pack.ice.length === 0) return 0; - return Math.max(...pack.ice.map(element => element.i)) + 1; + // find gaps in existing ids + const existingIds = pack.ice.map(e => e.i).sort((a, b) => a - b); + for (let id = 0; id < existingIds[existingIds.length - 1]; id++) { + if (!existingIds.includes(id)) return id; + } + return existingIds[existingIds.length - 1] + 1; } // Generate glaciers and icebergs based on temperature and height