From 55f6ca58c01a24c87a745d36e587037a9dafa8f0 Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 24 Sep 2021 06:00:27 +0200 Subject: [PATCH] Fix previews after wrapping lists --- src/app/Utils.ts | 14 ++++++++++++++ src/app/previews/BiomeSource.ts | 8 ++++---- src/app/previews/Decorator.ts | 4 ++-- src/app/previews/NoiseSettings.ts | 4 ++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/app/Utils.ts b/src/app/Utils.ts index aa12a858..9d4732dc 100644 --- a/src/app/Utils.ts +++ b/src/app/Utils.ts @@ -128,3 +128,17 @@ export function deepEqual(a: any, b: any) { } return a !== a && b !== b } + +export function unwrapLists(value: any): any { + if (Array.isArray(value)) { + return value.map(v => unwrapLists(v.node)) + } else if (typeof value === 'object' && value !== null) { + const res: Record = {} + Object.entries(value).map(([k, v]) => { + res[k] = unwrapLists(v) + }) + return res + } else { + return value + } +} diff --git a/src/app/previews/BiomeSource.ts b/src/app/previews/BiomeSource.ts index 05a6add2..c73fa2a0 100644 --- a/src/app/previews/BiomeSource.ts +++ b/src/app/previews/BiomeSource.ts @@ -2,7 +2,7 @@ import type { BiomeSource, Climate, NoiseOctaves } from 'deepslate' import { FixedBiome, MultiNoise, NoiseGeneratorSettings, NoiseSampler, NormalNoise, Random } from 'deepslate' import { fetchPreset } from '../DataFetcher' import type { VersionId } from '../Schemas' -import { deepClone, deepEqual, square, stringToColor } from '../Utils' +import { deepClone, deepEqual, square, stringToColor, unwrapLists } from '../Utils' type BiomeColors = Record type BiomeSourceOptions = { @@ -77,7 +77,7 @@ async function getBiomeSource(state: any, options: BiomeSourceOptions): Promise< return { getBiome(x: number, _y: number, z: number) { const i = (((x >> shift) + (z >> shift)) % numBiomes + numBiomes) % numBiomes - return (state.biomes?.[i] as string) + return (state.biomes?.[i].node as string) }, } @@ -91,7 +91,7 @@ async function getBiomeSource(state: any, options: BiomeSourceOptions): Promise< break } if (options.version === '1.18') { - return MultiNoise.fromJson(state) + return MultiNoise.fromJson(unwrapLists(state)) } else { const noise = ['altitude', 'temperature', 'humidity', 'weirdness'] .map((id, i) => { @@ -106,7 +106,7 @@ async function getBiomeSource(state: any, options: BiomeSourceOptions): Promise< const n = noise.map(n => n.sample(x, z, 0)) let minDist = Infinity let minBiome = '' - for (const { biome, parameters: p } of state.biomes) { + for (const { biome, parameters: p } of unwrapLists(state.biomes)) { const dist = square(p.altitude - n[0]) + square(p.temperature - n[1]) + square(p.humidity - n[2]) + square(p.weirdness - n[3]) + square(p.offset) if (dist < minDist) { minDist = dist diff --git a/src/app/previews/Decorator.ts b/src/app/previews/Decorator.ts index 9f6e81cd..476f6b22 100644 --- a/src/app/previews/Decorator.ts +++ b/src/app/previews/Decorator.ts @@ -1,6 +1,6 @@ import { PerlinNoise, Random } from 'deepslate' import type { VersionId } from '../Schemas' -import { clamp, stringToColor } from '../Utils' +import { clamp, stringToColor, unwrapLists } from '../Utils' type BlockPos = [number, number, number] type Placement = [BlockPos, number] @@ -49,7 +49,7 @@ export function decorator(state: any, img: ImageData, options: DecoratorOptions) for (let x = 0; x < options.size[0] / 16; x += 1) { for (let z = 0; z < options.size[2] / 16; z += 1) { - getPlacements([x * 16, 0, z * 16], state, ctx) + getPlacements([x * 16, 0, z * 16], unwrapLists(state), ctx) } } diff --git a/src/app/previews/NoiseSettings.ts b/src/app/previews/NoiseSettings.ts index a876f208..b35d6d65 100644 --- a/src/app/previews/NoiseSettings.ts +++ b/src/app/previews/NoiseSettings.ts @@ -2,7 +2,7 @@ import type { BlockPos, BlockState } from 'deepslate' import { Chunk, ChunkPos, FixedBiome, NoiseChunkGenerator, NoiseGeneratorSettings } from 'deepslate' import type { VersionId } from '../Schemas' import { checkVersion } from '../Schemas' -import { deepClone, deepEqual } from '../Utils' +import { deepClone, deepEqual, unwrapLists } from '../Utils' import { NoiseChunkGenerator as OldNoiseChunkGenerator } from './noise/NoiseChunkGenerator' export type NoiseSettingsOptions = { @@ -70,7 +70,7 @@ export function noiseSettings(state: any, img: ImageData, options: NoiseSettings } function getCached(state: unknown, options: NoiseSettingsOptions) { - const settings = NoiseGeneratorSettings.fromJson(state) + const settings = NoiseGeneratorSettings.fromJson(unwrapLists(state)) // Temporary fix for slides settings.noise.bottomSlide.target *= 128 settings.noise.topSlide.target *= 128