Skip to content

Commit

Permalink
misc(biome): support numeric+string biome type + native block type pl…
Browse files Browse the repository at this point in the history
…aceholders
  • Loading branch information
etienne-85 committed Oct 24, 2024
1 parent 2a384e7 commit 767d76f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
27 changes: 14 additions & 13 deletions src/api/world-compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { Biome, BiomeInfluence, BiomeType, BlockType } from '../procgen/Biome'
import { Heightmap } from '../procgen/Heightmap'
import {
Block,
BlockData,
GroundBlock,
LandscapesConf,
Expand Down Expand Up @@ -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]) &&
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -169,7 +169,7 @@ export const computeBlocksBatch = async (
level: 0,
type: BlockType.NONE,
}
const block: GroundBlock = {
const block: Block<BlockData> = {
pos: asVect3(pos),
data,
}
Expand All @@ -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))
Expand Down
13 changes: 9 additions & 4 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
LandscapesConf,
} from './types'

const typesNumbering = (types: Record<string, number>, 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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -477,6 +481,7 @@ const chunkBoxFromKey = (chunkKey: string, chunkDims: Vector3) => {
}

export {
typesNumbering,
roundToDec,
vectRoundToDec,
smoothstep,
Expand Down
22 changes: 19 additions & 3 deletions src/procgen/Biome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -63,6 +70,15 @@ export enum BiomeType {
// Tropical = 'tropical',
}

export const BiomeNumericType: Record<BiomeType, number> = {
[BiomeType.Temperate]: 0,
[BiomeType.Artic]: 0,
[BiomeType.Desert]: 0
}
Utils.typesNumbering(BiomeNumericType)
export const ReverseBiomeNumericType: Record<number, BiomeType> = {}
Object.keys(BiomeNumericType).forEach((type, i) => ReverseBiomeNumericType[i] = type as BiomeType)

Check failure on line 80 in src/procgen/Biome.ts

View workflow job for this annotation

GitHub Actions / lint

Arrow function should not return assignment

type Contribution = Record<Level, number>

const translateContribution = <T extends HeatLevel | RainLevel>(
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 767d76f

Please sign in to comment.