layout: make it deterministic

old-stable24
Varun Patil 2022-10-17 16:46:43 -07:00
parent 0c30422c2a
commit 70f7ba9c03
2 changed files with 17 additions and 3 deletions

View File

@ -725,6 +725,7 @@ export default class Timeline extends Mixins(GlobalMixin, UserConfig) {
rowHeight: this.rowHeight, rowHeight: this.rowHeight,
squareMode: this.squareMode, squareMode: this.squareMode,
numCols: this.numCols, numCols: this.numCols,
seed: dayId,
}); });
// Check if some rows were added // Check if some rows were added

View File

@ -13,6 +13,7 @@ export function getLayout(
rowHeight: number, rowHeight: number,
squareMode: boolean, squareMode: boolean,
numCols: number, numCols: number,
seed: number,
} }
): { ): {
top: number, top: number,
@ -31,6 +32,9 @@ export function getLayout(
}).boxes; }).boxes;
} }
// RNG
const rand = mulberry32(opts.seed);
// Binary flags // Binary flags
const FLAG_USE = 1 << 0; const FLAG_USE = 1 << 0;
const FLAG_USED = 1 << 1; const FLAG_USED = 1 << 1;
@ -103,7 +107,7 @@ export function getLayout(
(numLeft === 0 || numLeft >= opts.numCols); (numLeft === 0 || numLeft >= opts.numCols);
// Full width breakout // Full width breakout
if (canBreakout && Math.random() < (input.length > 0 ? 0.2 : 0.1)) { if (canBreakout && rand() < (input.length > 0 ? 0.2 : 0.1)) {
matrix[row][col] |= FLAG_BREAKOUT; matrix[row][col] |= FLAG_BREAKOUT;
for (let i = 1; i < opts.numCols; i++) { for (let i = 1; i < opts.numCols; i++) {
matrix[row][i] |= FLAG_USED; matrix[row][i] |= FLAG_USED;
@ -111,7 +115,7 @@ export function getLayout(
} }
// Use 6 vertically // Use 6 vertically
else if (canUse6 && Math.random() < 0.2) { else if (canUse6 && rand() < 0.2) {
matrix[row][col] |= FLAG_USE6; matrix[row][col] |= FLAG_USE6;
matrix[row+1][col] |= FLAG_USED; matrix[row+1][col] |= FLAG_USED;
matrix[row+2][col] |= FLAG_USED; matrix[row+2][col] |= FLAG_USED;
@ -121,7 +125,7 @@ export function getLayout(
} }
// Use 4 box // Use 4 box
else if (canUse4 && Math.random() < ((col % 2) ? 0.67 : 0.4)) { else if (canUse4 && rand() < ((col % 2) ? 0.67 : 0.4)) {
matrix[row][col] |= FLAG_USE4; matrix[row][col] |= FLAG_USE4;
matrix[row+1][col] |= FLAG_USED; matrix[row+1][col] |= FLAG_USED;
matrix[row][col+1] |= FLAG_USED; matrix[row][col+1] |= FLAG_USED;
@ -199,3 +203,12 @@ function flagMatrixStr(matrix: number[][], numFlag: number) {
} }
return str; return str;
} }
function mulberry32(a: number) {
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}