Skip to content

Commit

Permalink
fix: formatting + include trailing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-85 committed Aug 14, 2024
1 parent 7619741 commit 3506442
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 509 deletions.
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ export type ChunkKey = string
export type ChunkId = Vector3

export type WorldChunk = {
key: ChunkKey,
key: ChunkKey
data: Uint16Array | null
}
56 changes: 53 additions & 3 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MappingRange,
MappingRanges,
PatchId,
PatchKey,
} from './types'

// Clamp number between two values:
Expand Down Expand Up @@ -223,7 +224,52 @@ const isVect3Stub = (stub: Vector3Like) => {
}

const parseThreeStub = (stub: any) => {
return parseBox3Stub(stub) || parseVect3Stub(stub) || stub
return stub ? parseBox3Stub(stub) || parseVect3Stub(stub) || stub : stub
}

const parsePatchKey = (patchKey: PatchKey) => {
const patchId = new Vector2(
parseInt(patchKey.split('_')[1] as string),
parseInt(patchKey.split('_')[2] as string),
)
return patchId
}

const convertPosToPatchId = (position: Vector3, patchSize: number) => {
const orig_x = Math.floor(position.x / patchSize)
const orig_z = Math.floor(position.z / patchSize)
const patchCoords = new Vector2(orig_x, orig_z)
return patchCoords
}

const computePatchKey = (
patchId: Box3 | Vector3 | Vector2,
patchSize: number,
) => {
const inputCopy: Vector3 | Box3 =
patchId instanceof Vector2
? new Vector3(patchId.x, 0, patchId.y)
: patchId.clone()
const point =
inputCopy instanceof Box3
? (inputCopy as Box3).getCenter(new Vector3())
: (inputCopy as Vector3).clone()

const patchOrigin = convertPosToPatchId(point, patchSize)
const { x, y } = patchOrigin
const patchKey = `patch_${x}_${y}`
return patchKey
}

const getBboxFromPatchKey = (patchKey: string, patchSize: number) => {
const patchCoords = parsePatchKey(patchKey)
const bmin = vect2ToVect3(patchCoords.clone().multiplyScalar(patchSize))
const bmax = vect2ToVect3(
patchCoords.clone().addScalar(1).multiplyScalar(patchSize),
)
bmax.y = 512
const bbox = new Box3(bmin, bmax)
return bbox
}

const parseChunkKey = (chunkKey: ChunkKey) => {
Expand All @@ -248,7 +294,7 @@ function genChunkIds(patchId: PatchId, ymin: number, ymax: number) {
return chunk_ids
}

const getChunkBboxFromId = (chunkId: ChunkId, patchSize: number) => {
const getBboxFromChunkId = (chunkId: ChunkId, patchSize: number) => {
const bmin = chunkId.clone().multiplyScalar(patchSize)
const bmax = chunkId.clone().addScalar(1).multiplyScalar(patchSize)
const chunkBbox = new Box3(bmin, bmax)
Expand All @@ -271,8 +317,12 @@ export {
parseThreeStub,
vect2ToVect3,
vect3ToVect2,
parsePatchKey,
convertPosToPatchId,
computePatchKey,
getBboxFromPatchKey,
parseChunkKey,
serializeChunkId,
getBboxFromChunkId,
genChunkIds,
getChunkBboxFromId
}
48 changes: 24 additions & 24 deletions src/compute/WorldComputeApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Vector3 } from "three"
import { PatchKey } from "../common/types"
import { BlockData, BlocksPatch } from "../data/DataContainers"
import { BlockType, WorldCompute } from "../index"
import { Vector3 } from 'three'

import { PatchKey } from '../common/types'
import { BlockData, BlocksPatch } from '../data/DataContainers'
import { WorldCompute } from '../index'

export enum ComputeApiCall {
PatchCompute = 'computePatch',
Expand All @@ -11,9 +12,16 @@ export enum ComputeApiCall {
}

interface ComputeApiInterface {
computeBlocksBatch(blockPosBatch: Vector3[], params?: any): BlockData[] | Promise<BlockData[]>
computeBlocksBatch(
blockPosBatch: Vector3[],
params?: any,
): BlockData[] | Promise<BlockData[]>
// computePatch(patchKey: PatchKey): BlocksPatch | Promise<BlocksPatch>
iterPatchCompute(patchKeysBatch: PatchKey[]): Generator<BlocksPatch, void, unknown> | AsyncGenerator<BlocksPatch, void, unknown>
iterPatchCompute(
patchKeysBatch: PatchKey[],
):
| Generator<BlocksPatch, void, unknown>
| AsyncGenerator<BlocksPatch, void, unknown>
}

export class WorldComputeApi implements ComputeApiInterface {
Expand All @@ -29,25 +37,16 @@ export class WorldComputeApi implements ComputeApiInterface {
return this.singleton
}

static set worker(worker: Worker) {
// eslint-disable-next-line no-undef
static useWorker(worker: Worker) {
this.singleton = new WorldComputeProxy(worker)
}

computeBlocksBatch(blockPosBatch: Vector3[], params = { includeEntitiesBlocks: true }) {
const blocksBatch = blockPosBatch.map(({ x, z }) => {
const block_pos = new Vector3(x, 0, z)
const block = WorldCompute.computeGroundBlock(block_pos)
if (params.includeEntitiesBlocks) {
const blocksBuffer = WorldCompute.computeBlocksBuffer(block_pos)
const lastBlockIndex = blocksBuffer.findLastIndex(elt => elt)
if (lastBlockIndex >= 0) {
block.pos.y += lastBlockIndex
block.type = blocksBuffer[lastBlockIndex] as BlockType
}
}
return block
})
return blocksBatch
computeBlocksBatch(
blockPosBatch: Vector3[],
params = { includeEntitiesBlocks: true },
) {
return WorldCompute.computeBlocksBatch(blockPosBatch, params)
}

*iterPatchCompute(patchKeysBatch: PatchKey[]) {
Expand All @@ -62,6 +61,7 @@ export class WorldComputeApi implements ComputeApiInterface {
* Proxying requests to worker instead of internal world compute
*/
export class WorldComputeProxy implements ComputeApiInterface {
// eslint-disable-next-line no-undef
worker: Worker
count = 0
resolvers: Record<number, any> = {}
Expand Down Expand Up @@ -114,10 +114,10 @@ export class WorldComputeProxy implements ComputeApiInterface {
// const emptyPatch = new BlocksPatch(patchKey)
const patchStub = await this.workerCall(
ComputeApiCall.PatchCompute,
[patchKey] //[emptyPatch.bbox]
[patchKey], // [emptyPatch.bbox]
)
const patch = BlocksPatch.fromStub(patchStub)
yield patch
}
}
}
}
51 changes: 38 additions & 13 deletions src/compute/world-compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,41 @@ import {
EntityData,
RepeatableEntitiesMap,
} from '../procgen/EntitiesMap'

import { BlockData, BlocksContainer, BlocksPatch, EntityChunk } from '../data/DataContainers'
import {
BlockData,
BlocksContainer,
BlocksPatch,
EntityChunk,
} from '../data/DataContainers'
import { PatchKey } from '../common/types'


export const computePatch = (patchKey: PatchKey) => {
export const computePatch = (patchKey: PatchKey) => {
const patch = new BlocksPatch(patchKey)
genGroundBlocks(patch)
genEntitiesBlocks(patch)
return patch
}

export const computeBlocksBatch = (
blockPosBatch: Vector3[],
params = { includeEntitiesBlocks: true },
) => {
const blocksBatch = blockPosBatch.map(({ x, z }) => {
const block_pos = new Vector3(x, 0, z)
const block = computeGroundBlock(block_pos)
if (params.includeEntitiesBlocks) {
const blocksBuffer = computeBlocksBuffer(block_pos)
const lastBlockIndex = blocksBuffer.findLastIndex(elt => elt)
if (lastBlockIndex >= 0) {
block.pos.y += lastBlockIndex
block.type = blocksBuffer[lastBlockIndex] as BlockType
}
}
return block
})
return blocksBatch
}

export const computeGroundBlock = (blockPos: Vector3) => {
const biomeContribs = Biome.instance.getBiomeInfluence(blockPos)
const mainBiome = Biome.instance.getMainBiome(biomeContribs)
Expand Down Expand Up @@ -60,11 +83,7 @@ export const computeBlocksBuffer = (blockPos: Vector3) => {
entity.bbox.min.y = entityLevel
entity.bbox.max.y = entityLevel + 10
entity.type = entityType
blocksBuffer = EntitiesMap.fillBlockBuffer(
blockPos,
entity,
blocksBuffer,
)
blocksBuffer = EntitiesMap.fillBlockBuffer(blockPos, entity, blocksBuffer)
}
}
return blocksBuffer
Expand Down Expand Up @@ -93,7 +112,9 @@ const buildEntityChunk = (patch: BlocksContainer, entity: EntityData) => {
}

const genEntitiesBlocks = (blocksContainer: BlocksContainer) => {
const entitiesIter = RepeatableEntitiesMap.instance.iterate(blocksContainer.bbox)
const entitiesIter = RepeatableEntitiesMap.instance.iterate(
blocksContainer.bbox,
)
for (const entity of entitiesIter) {
// use global coords in case entity center is from adjacent patch
const entityPos = entity.bbox.getCenter(new Vector3())
Expand Down Expand Up @@ -121,15 +142,19 @@ const genEntitiesBlocks = (blocksContainer: BlocksContainer) => {
}

/**
* Fill container with ground blocks
* Fill container with ground blocks
*/
const genGroundBlocks = (blocksContainer: BlocksContainer) => {
const { min, max } = blocksContainer.bbox
// const patchId = min.x + ',' + min.z + '-' + max.x + ',' + max.z
// const prng = alea(patchId)
// const refPoints = this.isTransitionPatch ? this.buildRefPoints() : []
// const blocksPatch = new PatchBlocksCache(new Vector2(min.x, min.z))
const blocksPatchIter = blocksContainer.iterOverBlocks(undefined, false, false)
const blocksPatchIter = blocksContainer.iterOverBlocks(
undefined,
false,
false,
)
min.y = 512
max.y = 0
let blockIndex = 0
Expand All @@ -151,4 +176,4 @@ const genGroundBlocks = (blocksContainer: BlocksContainer) => {

// blocksContainer.state = PatchState.Filled
return blocksContainer
}
}
Loading

0 comments on commit 3506442

Please sign in to comment.