'use strict'; /** * Opponent miniature layout for online and Offline Strijd * (t1125, t1127, t1129, t1131, t1133). * Wide screens: minis sit beside the field, anchored to the outer * screen edges, filling inward (1 = left outer, 2 = right outer, * 3 = left next inward, …). Extra minis wrap to a new row upward * only when a side's row is full. Narrow / portrait: a row above * the field. */ var MiniLayout = (function () { var GAP = 12; var VGAP = 8; var EDGE = 16; var MIN_TILE_FOR_SIDES = 18; var LABEL_H = 32; /* Extra space above the field in the wide (sides) layout so enemy↔player arcs can route around the top edge (t1129). */ var ARC_TOP_PAD = 100; /* Below this width, or in a narrow portrait, keep the top row (t1131). */ var PORTRAIT_NARROW = 700; var PORTRAIT_MAX = 850; function preferredMini () { return { w: 80, h: 104, outerW: 90, outerH: 104 + LABEL_H }; } function canvasSize (n, mode, availH, rows) { n = n || 0; if (mode === 'sides') { rows = Math.max(1, rows || 1); if (availH && rows > 1) { var maxOuter = Math.floor((availH - (rows - 1) * VGAP) / rows); var ch = Math.max(40, Math.min(104, maxOuter - LABEL_H)); var cw = Math.max(32, Math.round(ch * 10 / 13)); return { w: cw, h: ch, outerW: Math.max(cw + 8, 56), outerH: ch + LABEL_H }; } return preferredMini(); } if (n >= 10) { return { w: 40, h: 52, outerW: 56, outerH: 52 + LABEL_H }; } if (n >= 6) { return { w: 56, h: 73, outerW: 68, outerH: 73 + LABEL_H }; } return { w: 80, h: 104, outerW: 90, outerH: 104 + LABEL_H }; } /** * How many minis fit in one row on a side, between the screen edge * and the field: EDGE + n*outerW + n*GAP <= sideWidth. */ function colsThatFit (sideWidth, mini) { if (!mini || !mini.outerW) { return 0; } var stride = mini.outerW + GAP; if (stride <= 0) { return 0; } return Math.max(0, Math.floor((sideWidth - EDGE) / stride)); } /** * Place n opponents. Anchored to the OUTER screen edges, filling * inward toward the field (t1133). Row 0 is bottom-aligned with the * field; further rows stack upward only after a side's row is full * (t1127). * * Index 0 → left outer, 1 → right outer, 2 → left next inward, … */ function sidePositions (n, fieldX, fieldY, fieldW, fieldH, mini, hudH, vw, colsPerSide) { var positions = []; if (n <= 0) { return positions; } vw = vw || (fieldX * 2 + fieldW); var leftSpace = fieldX; var rightSpace = vw - (fieldX + fieldW); var colsL = colsPerSide != null ? colsPerSide : Math.max(1, colsThatFit(leftSpace, mini)); var colsR = colsPerSide != null ? colsPerSide : Math.max(1, colsThatFit(rightSpace, mini)); if (colsL < 1) { colsL = 1; } if (colsR < 1) { colsR = 1; } var leftCount = Math.ceil(n / 2); var rightCount = Math.floor(n / 2); var rows = Math.max( Math.ceil(leftCount / colsL), rightCount ? Math.ceil(rightCount / colsR) : 1 ); var stackH = rows * mini.outerH + Math.max(0, rows - 1) * VGAP; var bottom = fieldY + fieldH; var top = bottom - stackH; if (top < hudH + 4) { top = hudH + 4; bottom = top + stackH; } for (var i = 0; i < n; i++) { var isLeft = (i % 2) === 0; var slot = Math.floor(i / 2); var cols = isLeft ? colsL : colsR; var row = Math.floor(slot / cols); var fromOuter = slot % cols; var x = isLeft ? EDGE + fromOuter * (mini.outerW + GAP) : vw - EDGE - mini.outerW - fromOuter * (mini.outerW + GAP); var y = bottom - (row + 1) * mini.outerH - row * VGAP; positions.push({ index: i, side: isLeft ? 'left' : 'right', row: row, fromOuter: fromOuter, colsPerSide: cols, x: x, y: y, w: mini.outerW, h: mini.outerH, canvasW: mini.w, canvasH: mini.h }); } return positions; } /* ---- t1203: top-row minis scale down so they all fit above the field ---- */ /** Horizontal / vertical gap between top-row minis (px). */ var TOP_GAP = 8; var TOP_VGAP = 4; /** Padding above the first top row (matches #minis padding-top). */ var TOP_PAD = 6; /** Largest and smallest mini canvas width in the top row (px). 32 px is * 3.2 px per column: the smallest size that still reads as a field. */ var TOP_MAX_W = 80; var TOP_MIN_W = 32; /** Label font: 11 px at full size, never below 9 px. */ var TOP_FONT_MAX = 11; var TOP_FONT_MIN = 9; /** Size of one top-row mini for a canvas width `w` (px). */ function topMiniForWidth (w) { w = Math.max(TOP_MIN_W, Math.min(TOP_MAX_W, Math.round(w))); var h = Math.round(w * 13 / 10); var font = Math.max(TOP_FONT_MIN, Math.min(TOP_FONT_MAX, Math.round(TOP_FONT_MAX * w / TOP_MAX_W))); var labelH = Math.round(font * 2.9); var outerW = w + Math.max(4, Math.round(10 * w / TOP_MAX_W)); return { w: w, h: h, outerW: outerW, outerH: h + labelH + 2, font: font, labelH: labelH, scale: w / TOP_MAX_W }; } function topGrid (n, availW, mini) { var cols = Math.max(1, Math.floor((availW + TOP_GAP) / (mini.outerW + TOP_GAP))); cols = Math.min(cols, Math.max(1, n)); var rows = Math.max(1, Math.ceil(n / cols)); // Balance the rows (6 → 3 + 3 rather than 5 + 1). cols = Math.max(1, Math.ceil(n / rows)); return { cols: cols, rows: rows, height: rows * mini.outerH + (rows - 1) * TOP_VGAP }; } /** * The largest top-row mini so that all `n` fit in `availW` × `bandH` * (t1203). If even TOP_MIN_W does not fit, return the minimum size with * fits = false and `capacity` = how many still fit above the field. */ function topMiniFit (n, availW, bandH) { n = Math.max(0, n || 0); for (var w = TOP_MAX_W; w >= TOP_MIN_W; w--) { var m = topMiniForWidth(w); var g = topGrid(n, availW, m); if (g.height <= bandH) { m.cols = g.cols; m.rows = g.rows; m.fits = true; m.capacity = Math.max(n, g.cols * g.rows); return m; } } var min = topMiniForWidth(TOP_MIN_W); var maxCols = Math.max(1, Math.floor((availW + TOP_GAP) / (min.outerW + TOP_GAP))); var rowsFit = Math.max(1, Math.floor((bandH + TOP_VGAP) / (min.outerH + TOP_VGAP))); min.cols = maxCols; min.rows = Math.ceil(n / maxCols); min.fits = false; min.capacity = maxCols * rowsFit; return min; } /** * Slot per opponent (t1203). When everything fits, slot = index (order * unchanged). Only when they do not fit: alive opponents first, then the * eliminated ones, each group in its original order (stable), so the * ones that end up behind the field are the defeated ones. * @param {boolean[]} eliminated per original index * @return {number[]} slot for each original index */ function topSlots (eliminated, fits) { var n = eliminated.length; var slots = new Array(n); if (fits) { for (var i = 0; i < n; i++) { slots[i] = i; } return slots; } var next = 0; for (var a = 0; a < n; a++) { if (!eliminated[a]) { slots[a] = next++; } } for (var e = 0; e < n; e++) { if (eliminated[e]) { slots[e] = next++; } } return slots; } /** Viewport position of top-row slot `slot` (rows centred). */ function topSlotPosition (slot, n, plan) { var mini = plan.mini; var cols = mini.cols || 1; var row = Math.floor(slot / cols); var inRow = Math.min(cols, n - row * cols); var rowW = inRow * mini.outerW + (inRow - 1) * TOP_GAP; var x0 = Math.round((plan.vw - rowW) / 2); return { slot: slot, row: row, x: x0 + (slot % cols) * (mini.outerW + TOP_GAP), y: plan.hudH + TOP_PAD + row * (mini.outerH + TOP_VGAP), w: mini.outerW, h: mini.outerH, canvasW: mini.w, canvasH: mini.h, font: mini.font, aboveField: (plan.hudH + TOP_PAD + row * (mini.outerH + TOP_VGAP) + mini.outerH) <= plan.fieldY + 0.5 }; } /** Positions per original index for the top row (t1203). */ function topPositions (eliminated, plan) { var n = eliminated.length; var slots = topSlots(eliminated, plan.mini && plan.mini.fits); var out = []; for (var i = 0; i < n; i++) { var pos = topSlotPosition(slots[i], n, plan); pos.index = i; pos.eliminated = !!eliminated[i]; out.push(pos); } return out; } function isPortraitNarrow (vw, vh) { if (vw < PORTRAIT_NARROW) { return true; } if (vw < vh && vw < PORTRAIT_MAX) { return true; } return false; } /** * Compute the field and mini plan for a viewport. * `n` is the number of opponents (0 = solo, no minis). */ function plan (vw, vh, n, hudH, margin) { vw = vw || 320; vh = vh || 480; n = n || 0; hudH = hudH == null ? 56 : hudH; margin = margin == null ? Math.max(6, Math.round(Math.min(vw, vh) * 0.015)) : margin; function finish (mode, tile, fieldX, fieldY, miniH, mini, positions, cols) { return { mode: mode, tile: tile, fieldW: tile * 10, fieldH: tile * 13, fieldX: fieldX, fieldY: fieldY, miniH: miniH, mini: mini, positions: positions || [], colsPerSide: cols || 0, n: n }; } if (n > 0 && !isPortraitNarrow(vw, vh)) { var availHSide = Math.max(80, vh - hudH - margin); var probe = preferredMini(); var availWSide = Math.max(80, vw - margin * 2 - 2 * (probe.outerW + GAP)); var availHForField = Math.max(80, availHSide - ARC_TOP_PAD); var tileSide = Math.max(14, Math.floor(Math.min(availWSide / 10, availHForField / 13))); var fieldWSide = tileSide * 10; var leftover = (vw - fieldWSide) / 2; var cols = colsThatFit(leftover, probe); // If a large mini does not fit, shrink it so a small one still can (t1131). if (cols < 1) { var shrinkStride = leftover - EDGE; if (shrinkStride >= 56 + GAP) { var ow = Math.max(56, shrinkStride - GAP); var oh = Math.round(ow * (104 + LABEL_H) / 90); var ch = Math.max(40, oh - LABEL_H); var cw = Math.max(32, Math.round(ch * 10 / 13)); probe = { w: cw, h: ch, outerW: ow, outerH: ch + LABEL_H }; cols = colsThatFit(leftover, probe); } } var need = probe.outerW + GAP + EDGE; if (leftover >= need && tileSide >= MIN_TILE_FOR_SIDES && cols >= 1) { var fieldX = Math.floor((vw - fieldWSide) / 2); var fieldHSide = tileSide * 13; var fieldY = hudH + ARC_TOP_PAD + Math.floor((availHForField - fieldHSide) / 2); if (fieldY < hudH + ARC_TOP_PAD) { fieldY = hudH + ARC_TOP_PAD; } var leftCount = Math.ceil(n / 2); var rows = Math.max(1, Math.ceil(leftCount / cols)); var stackAvail = fieldY + fieldHSide - (hudH + 4); if (rows > 1) { probe = canvasSize(n, 'sides', stackAvail, rows); cols = Math.max(1, colsThatFit(leftover, probe)); rows = Math.max(1, Math.ceil(leftCount / cols)); } return finish('sides', tileSide, fieldX, fieldY, 0, probe, sidePositions(n, fieldX, fieldY, fieldWSide, fieldHSide, probe, hudH, vw, cols), cols); } } var miniTop = canvasSize(n, 'top', 0); var miniH = 0; if (n > 0) { var share = n > 6 ? 0.26 : 0.20; miniH = Math.min(n > 6 ? 180 : 150, Math.max(86, Math.round(vh * share))); } var availW = Math.max(80, vw - margin * 2); var availH = Math.max(80, vh - hudH - miniH - margin); var tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); var fieldW = tile * 10; var fieldH = tile * 13; var fieldX = Math.floor((vw - fieldW) / 2); var topBand = hudH + miniH; var fieldY = topBand + Math.floor((vh - topBand - fieldH - margin / 2) / 2); if (fieldY < topBand) { fieldY = topBand; } if (n > 0) { // t1203: scale the minis so all of them fit in the band above the // field (band = from below the HUD to the top of the field). var bandH = Math.max(0, fieldY - hudH - TOP_PAD - 2); miniTop = topMiniFit(n, Math.max(40, vw - 2 * TOP_GAP), bandH); } var res = finish('top', tile, fieldX, fieldY, miniH, miniTop, [], 0); res.vw = vw; res.hudH = hudH; if (n > 0) { var none = []; for (var k = 0; k < n; k++) { none.push(false); } res.positions = topPositions(none, res); } return res; } var api = { GAP: GAP, VGAP: VGAP, EDGE: EDGE, MIN_TILE_FOR_SIDES: MIN_TILE_FOR_SIDES, ARC_TOP_PAD: ARC_TOP_PAD, PORTRAIT_NARROW: PORTRAIT_NARROW, LABEL_H: LABEL_H, canvasSize: canvasSize, TOP_GAP: TOP_GAP, TOP_VGAP: TOP_VGAP, TOP_PAD: TOP_PAD, TOP_MAX_W: TOP_MAX_W, TOP_MIN_W: TOP_MIN_W, TOP_FONT_MIN: TOP_FONT_MIN, topMiniForWidth: topMiniForWidth, topMiniFit: topMiniFit, topSlots: topSlots, topSlotPosition: topSlotPosition, topPositions: topPositions, colsThatFit: colsThatFit, sidePositions: sidePositions, plan: plan }; if (typeof module !== 'undefined' && module.exports) { module.exports = api; } else if (typeof globalThis !== 'undefined') { globalThis.MiniLayout = api; } return api; })(); if (typeof module !== 'undefined' && module.exports && typeof MiniLayout !== 'undefined') { module.exports = MiniLayout; }