diff --git a/src/api/world-compute.ts b/src/api/world-compute.ts index fd489cf..8b3b42b 100644 --- a/src/api/world-compute.ts +++ b/src/api/world-compute.ts @@ -9,6 +9,7 @@ import { import { Biome, BiomeInfluence, BiomeType, BlockType } from '../procgen/Biome' import { Heightmap } from '../procgen/Heightmap' import { + Block, BlockData, GroundBlock, LandscapesConf, @@ -58,20 +59,20 @@ const getBiomeBoundsInfluences = (bounds: Box2) => { const { xMyM, xMyP, xPyM, xPyP } = PatchBoundId // eval biome at patch corners const equals = (v1: BiomeInfluence, v2: BiomeInfluence) => { - const different = Object.keys(v1).find( - k => v1[k as BiomeType] !== v2[k as BiomeType], - ) + const different = Object.keys(v1) + // .map(k => parseInt(k) as BiomeType) + .find(k => v1[k as BiomeType] !== v2[k as BiomeType]) return !different } const boundsPoints = getPatchBoundingPoints(bounds) const boundsInfluences = {} as PatchBoundingBiomes - ;[xMyM, xMyP, xPyM, xPyP].map(key => { - const boundPos = boundsPoints[key] as Vector2 - const biomeInfluence = Biome.instance.getBiomeInfluence(boundPos) - boundsInfluences[key] = biomeInfluence - // const block = computeGroundBlock(asVect3(pos), biomeInfluence) - return biomeInfluence - }) + ;[xMyM, xMyP, xPyM, xPyP].map(key => { + const boundPos = boundsPoints[key] as Vector2 + const biomeInfluence = Biome.instance.getBiomeInfluence(asVect3(boundPos)) + boundsInfluences[key] = biomeInfluence + // const block = computeGroundBlock(asVect3(pos), biomeInfluence) + return biomeInfluence + }) const allEquals = equals(boundsInfluences[xMyM], boundsInfluences[xPyM]) && equals(boundsInfluences[xMyM], boundsInfluences[xMyP]) && @@ -117,11 +118,10 @@ export const computeGroundBlock = ( biomeInfluence, ) let usedConf = nominalConf - // const pos = new Vector3(blockPos.x, level, blockPos.z) if (nominalConf.next?.data) { const variation = Biome.instance.posRandomizer.eval( blockPos.clone().multiplyScalar(50), - ) // Math.cos(0.1 * blockPos.length()) / 100 + ) const min = new Vector2(nominalConf.data.x, nominalConf.data.y) const max = new Vector2(nominalConf.next.data.x, nominalConf.next.data.y) const rangeBox = new Box2(min, max) @@ -169,7 +169,7 @@ export const computeBlocksBatch = async ( level: 0, type: BlockType.NONE, } - const block: GroundBlock = { + const block: Block = { pos: asVect3(pos), data, } @@ -187,6 +187,7 @@ export const computeBlocksBatch = async ( biomeBoundsInfluences, ) block.data = computeGroundBlock(block.pos, blockBiome) + // const {level, type } = // override with last block if specified if (params.includeEntitiesBlocks) { const lastBlockData = await queryLastBlockData(asVect2(block.pos)) diff --git a/src/common/utils.ts b/src/common/utils.ts index 7b19274..4f28608 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -13,6 +13,10 @@ import { LandscapesConf, } from './types' +const typesNumbering = (types: Record, offset = 0) => Object.keys(types).forEach( + (key, i) => (types[key] = offset + i), +) + // Clamp number between two values: const clamp = (num: number, min: number, max: number) => Math.min(Math.max(num, min), max) @@ -361,10 +365,10 @@ const parseBox3Stub = (stub: Box3) => { const parseThreeStub = (stub: any) => { return stub ? parseBox3Stub(stub) || - parseVect3Stub(stub) || - parseBox2Stub(stub) || - parseVect2Stub(stub) || - stub + parseVect3Stub(stub) || + parseBox2Stub(stub) || + parseVect2Stub(stub) || + stub : stub } @@ -477,6 +481,7 @@ const chunkBoxFromKey = (chunkKey: string, chunkDims: Vector3) => { } export { + typesNumbering, roundToDec, vectRoundToDec, smoothstep, diff --git a/src/procgen/Biome.ts b/src/procgen/Biome.ts index c93ff72..6810aaa 100644 --- a/src/procgen/Biome.ts +++ b/src/procgen/Biome.ts @@ -18,11 +18,18 @@ import { ProcLayer } from './ProcLayer' // reserved native block types export enum BlockType { NONE, + HOLE, + BEDROCK, + WATER, + ICE, MUD, TRUNK, + SAND, + GRASS, + ROCK, + SNOW, FOLIAGE_LIGHT, FOLIAGE_DARK, - HOLE, LAST_PLACEHOLDER, } @@ -63,6 +70,15 @@ export enum BiomeType { // Tropical = 'tropical', } +export const BiomeNumericType: Record = { + [BiomeType.Temperate]: 0, + [BiomeType.Artic]: 0, + [BiomeType.Desert]: 0 +} +Utils.typesNumbering(BiomeNumericType) +export const ReverseBiomeNumericType: Record = {} +Object.keys(BiomeNumericType).forEach((type, i) => ReverseBiomeNumericType[i] = type as BiomeType) + type Contribution = Record const translateContribution = ( @@ -166,10 +182,10 @@ export class Biome { getBiomeType(input: Vector3 | BiomeInfluence) { const biomeContribs = input instanceof Vector3 ? this.getBiomeInfluence(input) : input - const mainBiome = Object.entries(biomeContribs).sort( + const dominantBiome = Object.entries(biomeContribs).sort( (a, b) => b[1] - a[1], )[0]?.[0] as string - return mainBiome as BiomeType + return dominantBiome as BiomeType } calculateContributions(value: number) {