Skip to content

Commit

Permalink
add map_simple_terrain df to better support floating islands datapacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsjo committed Sep 25, 2024
1 parent b89b7dc commit 747ba85
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
13 changes: 8 additions & 5 deletions src/MapLayers/BiomeLayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as L from "leaflet"
//import { last, range, takeWhile } from "lodash";
import { Climate } from "deepslate";
import { calculateHillshade, getSurfaceDensityFunction, hashCode } from "../util";
import { calculateHillshade, getCustomDensityFunction, hashCode } from "../util";
import MultiNoiseCalculator from "../webworker/MultiNoiseCalculator?worker"
import { useSearchStore } from "../stores/useBiomeSearchStore";
import { useLoadedDimensionStore } from "../stores/useLoadedDimensionStore";
Expand All @@ -18,9 +18,9 @@ type Tile = {
ctx: CanvasRenderingContext2D,
done: L.DoneCallback,
array?: {
climate: Climate.TargetPoint,
surface: number,
biome: string
biome: string,
terrain: number
}[][],
step?: number,
isRendering?: boolean,
Expand Down Expand Up @@ -130,7 +130,9 @@ export class BiomeLayer extends L.GridLayer {
let hillshade = 1.0
const y = project_down ? Math.min(tile.array[x + 1][z + 1].surface, this.y.value) : this.y.value
const belowSurface = y < tile.array[x + 1][z + 1].surface
if (do_hillshade && project_down && !belowSurface) {
if (tile.array[x + 1][z + 1].terrain < 0){
hillshade = 0.15
} else if (do_hillshade && project_down && !belowSurface) {

hillshade = calculateHillshade(
tile.array[x + 2][z + 1].surface - tile.array[x][z + 1].surface,
Expand Down Expand Up @@ -216,7 +218,8 @@ export class BiomeLayer extends L.GridLayer {
if (do_update.dimension) {
update.biomeSourceJson = toRaw(this.loadedDimensionStore.loaded_dimension.biome_source_json)
update.noiseGeneratorSettingsJson = toRaw(this.loadedDimensionStore.loaded_dimension.noise_settings_json)
update.surfaceDensityFunctionId = getSurfaceDensityFunction(this.loadedDimensionStore.loaded_dimension.noise_settings_id!, this.settingsStore.dimension)?.toString() ?? "<none>"
update.surfaceDensityFunctionId = getCustomDensityFunction("snowcapped_surface", this.loadedDimensionStore.loaded_dimension.noise_settings_id!, this.settingsStore.dimension)?.toString() ?? "<none>"
update.terrainDensityFunctionId = getCustomDensityFunction("map_simple_terrain", this.loadedDimensionStore.loaded_dimension.noise_settings_id!, this.settingsStore.dimension)?.toString() ?? "<none>"
}

if (do_update.settings) {
Expand Down
4 changes: 2 additions & 2 deletions src/stores/useLoadedDimensionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { compile, computed, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { getPreset } from "../BuildIn/MultiNoiseBiomeParameterList";
import { VANILLA_ITEMS } from "../BuildIn/VanillaItems";
import { getSurfaceDensityFunction, hashCode } from "../util";
import { getCustomDensityFunction, hashCode } from "../util";
import { useDatapackStore } from "./useDatapackStore";
import { useSettingsStore } from "./useSettingsStore";

Expand Down Expand Up @@ -201,7 +201,7 @@ export const useLoadedDimensionStore = defineStore('loaded_dimension', () => {
})

const surface_density_function = computed(() => {
const surface_density_function_id = getSurfaceDensityFunction(loaded_dimension.noise_settings_id ?? Identifier.create("empty"), settingsStore.dimension)
const surface_density_function_id = getCustomDensityFunction("snowcapped_surface", loaded_dimension.noise_settings_id ?? Identifier.create("empty"), settingsStore.dimension)
if (surface_density_function_id !== undefined){
return new DensityFunction.HolderHolder(Holder.reference(WorldgenRegistries.DENSITY_FUNCTION, surface_density_function_id)).mapAll((random_state.value).createVisitor((noise_generator_settings.value).noise, (noise_generator_settings.value).legacyRandomSource))
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ function idIfExists(id: Identifier) {
return undefined
}

function getDimensionDensityFunction(noise_settings_id: Identifier, dimension_id: Identifier): Identifier {
function getDimensionDensityFunction(name: string, noise_settings_id: Identifier, dimension_id: Identifier): Identifier {
const dimensionName = dimension_id.path.split("/").reverse()[0]
const noiseSettingsPath = noise_settings_id.path.split("/")
noiseSettingsPath[noiseSettingsPath.length - 1] = `${dimensionName}_${noiseSettingsPath[noiseSettingsPath.length - 1]}`
return new Identifier(noise_settings_id.namespace, noiseSettingsPath.join("/") + "/snowcapped_surface")
return new Identifier(noise_settings_id.namespace, noiseSettingsPath.join("/") + "/" + name)
}

export function getSurfaceDensityFunction(noise_settings_id: Identifier, dimension_id: Identifier): Identifier | undefined {
return idIfExists(new Identifier(noise_settings_id.namespace, noise_settings_id.path + "/snowcapped_surface"))
?? idIfExists(getDimensionDensityFunction(noise_settings_id, dimension_id))
?? idIfExists(new Identifier(noise_settings_id.namespace, "snowcapped_surface"))
?? idIfExists(new Identifier("minecraft", "snowcapped_surface"))
export function getCustomDensityFunction(name: string, noise_settings_id: Identifier, dimension_id: Identifier): Identifier | undefined {
return idIfExists(new Identifier(noise_settings_id.namespace, noise_settings_id.path + "/" + name))
?? idIfExists(getDimensionDensityFunction(name, noise_settings_id, dimension_id))
?? idIfExists(new Identifier(noise_settings_id.namespace, name))
?? idIfExists(new Identifier("minecraft", name))
}

const zenith = 20.0 * Math.PI / 180.0;
Expand Down
22 changes: 18 additions & 4 deletions src/webworker/MultiNoiseCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MultiNoiseCalculator {
sampler?: Climate.Sampler,
biomeSource?: BiomeSource,
surfaceDensityFunction?: DensityFunction,
terrainDensityFunction?: DensityFunction,
noiseGeneratorSettings?: NoiseGeneratorSettings
randomState?: RandomState
y: number,
Expand All @@ -32,6 +33,7 @@ class MultiNoiseCalculator {
densityFunctions?: { [key: string]: unknown },
noises?: { [key: string]: unknown },
surfaceDensityFunctionId?: string,
terrainDensityFunctionId?: string,
generationVersion?: number

seed?: bigint,
Expand Down Expand Up @@ -70,16 +72,27 @@ class MultiNoiseCalculator {
this.state.sampler = Climate.Sampler.fromRouter(this.state.randomState.router)
}

if (update.surfaceDensityFunctionId && this.state.randomState && this.state.noiseGeneratorSettings) {
if (this.state.randomState && this.state.noiseGeneratorSettings) {
if (update.surfaceDensityFunctionId === "<none>"){
this.state.surfaceDensityFunction = undefined
} else {
} else if (update.surfaceDensityFunctionId) {
this.state.surfaceDensityFunction = new DensityFunction.HolderHolder(
Holder.reference(
WorldgenRegistries.DENSITY_FUNCTION,
Identifier.parse(update.surfaceDensityFunctionId)
)).mapAll(this.state.randomState.createVisitor(this.state.noiseGeneratorSettings.noise, this.state.noiseGeneratorSettings.legacyRandomSource))
}

if (update.terrainDensityFunctionId === "<none>"){
this.state.terrainDensityFunction = undefined
} else if (update.terrainDensityFunctionId) {
this.state.terrainDensityFunction = new DensityFunction.HolderHolder(
Holder.reference(
WorldgenRegistries.DENSITY_FUNCTION,
Identifier.parse(update.terrainDensityFunctionId)
)).mapAll(this.state.randomState.createVisitor(this.state.noiseGeneratorSettings.noise, this.state.noiseGeneratorSettings.legacyRandomSource))
}

}

this.taskQueue = []
Expand Down Expand Up @@ -109,7 +122,7 @@ class MultiNoiseCalculator {
}

private calculateMultiNoiseValues(key: string, min_x: number, min_z: number, max_x: number, max_z: number, tileSize: number): void {
const array: { surface: number, biome: string }[][] = Array(tileSize + 2)
const array: { surface: number, biome: string, terrain: number }[][] = Array(tileSize + 2)
const step = (max_x - min_x) / tileSize
for (let ix = -1; ix < tileSize + 2; ix++) {
array[ix] = Array(tileSize + 2)
Expand All @@ -119,7 +132,8 @@ class MultiNoiseCalculator {
const surface = this.state.surfaceDensityFunction?.compute(DensityFunction.context(x * 4, 0, z * 4)) ?? Number.POSITIVE_INFINITY
const y = this.state.projectDown ? Math.min(surface, this.state.y) : this.state.y
const biome = this.state.biomeSource?.getBiome(x, y >> 2, z, this.state.sampler!).toString() ?? "minecraft:plains"
array[ix][iz] = { surface, biome }
const terrain = this.state.terrainDensityFunction?.compute(DensityFunction.context(x * 4, y , z * 4)) ?? Number.POSITIVE_INFINITY
array[ix][iz] = { surface, biome, terrain }
}
}

Expand Down

0 comments on commit 747ba85

Please sign in to comment.