2026-04-02 13:52:15 +08:00
|
|
|
import {Entity} from "../utils/entity";
|
2026-04-01 13:36:16 +08:00
|
|
|
import {Part} from "./part";
|
|
|
|
|
import {RNG} from "../utils/rng";
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export type Region = {
|
|
|
|
|
id: string;
|
2026-04-01 13:36:16 +08:00
|
|
|
axes: RegionAxis[];
|
2026-04-02 13:52:15 +08:00
|
|
|
children: Entity<Part>[];
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type RegionAxis = {
|
|
|
|
|
name: string;
|
|
|
|
|
min?: number;
|
|
|
|
|
max?: number;
|
|
|
|
|
align?: 'start' | 'end' | 'center';
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export function applyAlign(region: Entity<Region>) {
|
|
|
|
|
region.produce(applyAlignCore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyAlignCore(region: Region) {
|
2026-04-01 17:48:40 +08:00
|
|
|
if (region.children.length === 0) return;
|
|
|
|
|
|
|
|
|
|
for (let axisIndex = 0; axisIndex < region.axes.length; axisIndex++) {
|
|
|
|
|
const axis = region.axes[axisIndex];
|
|
|
|
|
if (!axis.align) continue;
|
2026-04-01 17:34:21 +08:00
|
|
|
|
2026-04-01 18:33:09 +08:00
|
|
|
const positionValues = new Set<number>();
|
2026-04-02 13:52:15 +08:00
|
|
|
for (const child of region.children) {
|
|
|
|
|
positionValues.add(child.value.position[axisIndex] ?? 0);
|
2026-04-01 18:33:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sortedPositions = Array.from(positionValues).sort((a, b) => a - b);
|
|
|
|
|
const positionMap = new Map<number, number>();
|
2026-04-01 17:34:21 +08:00
|
|
|
|
|
|
|
|
if (axis.align === 'start' && axis.min !== undefined) {
|
2026-04-01 18:33:09 +08:00
|
|
|
sortedPositions.forEach((pos, index) => {
|
|
|
|
|
positionMap.set(pos, axis.min! + index);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
} else if (axis.align === 'end' && axis.max !== undefined) {
|
2026-04-01 18:33:09 +08:00
|
|
|
const count = sortedPositions.length;
|
|
|
|
|
sortedPositions.forEach((pos, index) => {
|
|
|
|
|
positionMap.set(pos, axis.max! - (count - 1 - index));
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
} else if (axis.align === 'center') {
|
2026-04-01 18:33:09 +08:00
|
|
|
const count = sortedPositions.length;
|
2026-04-01 17:34:21 +08:00
|
|
|
const min = axis.min ?? 0;
|
|
|
|
|
const max = axis.max ?? count - 1;
|
|
|
|
|
const range = max - min;
|
|
|
|
|
const center = min + range / 2;
|
2026-04-01 17:48:40 +08:00
|
|
|
|
2026-04-01 18:33:09 +08:00
|
|
|
sortedPositions.forEach((pos, index) => {
|
2026-04-01 17:34:21 +08:00
|
|
|
const offset = index - (count - 1) / 2;
|
2026-04-01 18:33:09 +08:00
|
|
|
positionMap.set(pos, center + offset);
|
2026-04-01 17:34:21 +08:00
|
|
|
});
|
|
|
|
|
}
|
2026-04-01 18:33:09 +08:00
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
for (const child of region.children) {
|
|
|
|
|
child.produce(draft => {
|
|
|
|
|
const currentPos = draft.position[axisIndex] ?? 0;
|
|
|
|
|
draft.position[axisIndex] = positionMap.get(currentPos) ?? currentPos;
|
|
|
|
|
});
|
2026-04-01 18:33:09 +08:00
|
|
|
}
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
2026-04-01 18:33:09 +08:00
|
|
|
|
|
|
|
|
region.children.sort((a, b) => {
|
|
|
|
|
for (let i = 0; i < region.axes.length; i++) {
|
|
|
|
|
const diff = (a.value.position[i] ?? 0) - (b.value.position[i] ?? 0);
|
|
|
|
|
if (diff !== 0) return diff;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export function shuffle(region: Entity<Region>, rng: RNG) {
|
|
|
|
|
region.produce(region => shuffleCore(region, rng));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shuffleCore(region: Region, rng: RNG){
|
2026-04-01 17:34:21 +08:00
|
|
|
if (region.children.length <= 1) return;
|
|
|
|
|
|
|
|
|
|
const children = [...region.children];
|
|
|
|
|
for (let i = children.length - 1; i > 0; i--) {
|
|
|
|
|
const j = rng.nextInt(i + 1);
|
2026-04-02 13:52:15 +08:00
|
|
|
const posI = [...children[i].value.position];
|
|
|
|
|
const posJ = [...children[j].value.position];
|
|
|
|
|
children[i].produce(draft => {
|
|
|
|
|
draft.position = posJ;
|
|
|
|
|
});
|
|
|
|
|
children[j].produce(draft => {
|
|
|
|
|
draft.position = posI;
|
|
|
|
|
});
|
2026-04-01 17:34:21 +08:00
|
|
|
}
|
2026-04-02 13:52:15 +08:00
|
|
|
}
|