Skip to content

Commit

Permalink
fix: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-85 authored and Sceat committed Dec 20, 2024
1 parent 61d31a4 commit de01b89
Show file tree
Hide file tree
Showing 24 changed files with 432 additions and 381 deletions.
9 changes: 2 additions & 7 deletions src/api/WorldComputeProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import workerpool, { Pool } from 'workerpool'
import { ChunkContainer, ChunkStub } from '../datacontainers/ChunkContainer'
import { WorldEnv } from '../index'
import { SpawnedItems } from '../misc/ItemsInventory'
import {
ChunkId,
ChunkKey,
PatchId,
PatchKey,
} from '../utils/types'
import { ChunkId, ChunkKey, PatchId, PatchKey } from '../utils/types'

import { ComputeTask } from './world-compute'

Expand Down Expand Up @@ -73,7 +68,7 @@ export class WorldComputeProxy {
this.customProxy = customProxy
}

isReady(){
isReady() {
return !!WorldComputeProxy.workerPool
}

Expand Down
41 changes: 21 additions & 20 deletions src/api/WorldProcessing.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { BlocksBatch } from "../datacontainers/BlocksBatch"
import { WorldUtils } from "../index"
import { ProcessType, WorldProcess } from "../utils/types"
import { BlocksBatch } from '../datacontainers/BlocksBatch'
import { WorldUtils } from '../index'
import { ProcessType, WorldProcess } from '../utils/types'

type WorldProcessImpl = new (args: any) => WorldProcess //typeof WorldProcess
type WorldProcessImpl = new (args: any) => WorldProcess // typeof WorldProcess

export const ProcessMapping: Record<ProcessType, WorldProcessImpl> = {
[ProcessType.BlocksBatch]: BlocksBatch
[ProcessType.BlocksBatch]: BlocksBatch,
}

export class WorldProcessing {
static parseArgs(rawArgs: any) {
const args = rawArgs.map((arg: any) =>
arg instanceof Array
? arg.map(item => WorldUtils.convert.parseThreeStub(item))
: WorldUtils.convert.parseThreeStub(arg),
)
return args
}
static async process(processType: ProcessType, processArgs: any) {
processArgs = WorldProcessing.parseArgs(processArgs)
const processClass = ProcessMapping[processType]
const processInstance = new processClass(processArgs)
await processInstance.process()
return processInstance.toStub()
}
static parseArgs(rawArgs: any) {
const args = rawArgs.map((arg: any) =>
arg instanceof Array
? arg.map(item => WorldUtils.convert.parseThreeStub(item))
: WorldUtils.convert.parseThreeStub(arg),
)
return args
}

static async process(processType: ProcessType, processArgs: any) {
processArgs = WorldProcessing.parseArgs(processArgs)
const ProcessClass = ProcessMapping[processType]
const processInstance = new ProcessClass(processArgs)
await processInstance.process()
return processInstance.toStub()
}
}
4 changes: 3 additions & 1 deletion src/api/world-compute-worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WorldUtils } from '../index'
import { ProcessType } from '../utils/types'

import { WorldComputeApi } from './world-compute'
import { WorldProcessing } from './WorldProcessing'

Expand Down Expand Up @@ -32,6 +33,7 @@ export const WorldWorkerInit = (workerPool: any) => {

workerPool.worker({
...worldComputeApiWrap,
[ProcessType.BlocksBatch]: async (...rawArgs: any) => WorldProcessing.process(ProcessType.BlocksBatch, rawArgs[0])
[ProcessType.BlocksBatch]: async (...rawArgs: any) =>
WorldProcessing.process(ProcessType.BlocksBatch, rawArgs[0]),
})
}
16 changes: 11 additions & 5 deletions src/api/world-compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import {
import { PatchBase } from '../datacontainers/PatchBase'
import { GroundChunk } from '../datacontainers/ChunkFactory'



const defaultDistribution: DistributionParams = {
...DistributionProfiles[DistributionProfile.MEDIUM],
minDistance: 10,
Expand Down Expand Up @@ -140,7 +138,9 @@ export const bakePatch = (boundsOrPatchKey: PatchKey | Box2) => {
export const bakeGroundLayer = (boundsOrPatchKey: PatchKey | Box2) => {
const groundPatch = new GroundPatch(boundsOrPatchKey)
const { valueRange } = groundPatch
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(groundPatch.bounds)
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(
groundPatch.bounds,
)
const blocks = groundPatch.iterBlocksQuery(undefined, false)
let blockIndex = 0
for (const block of blocks) {
Expand Down Expand Up @@ -174,7 +174,11 @@ export const retrieveOvergroundItems = async (bounds: Box2) => {
asVect2(defaultItemDims),
)
for (const pos of spawnPlaces) {
const blockBiome = WorldUtils.process.getBlockBiome(pos, bounds, boundsBiomeInfluences)
const blockBiome = WorldUtils.process.getBlockBiome(
pos,
bounds,
boundsBiomeInfluences,
)
const { level, biome, landscapeIndex } = computeGroundBlock(
asVect3(pos),
blockBiome,
Expand Down Expand Up @@ -216,7 +220,9 @@ export const queryLastBlockData = async (queriedLoc: Vector2) => {
getPatchId(spawnOrigin, WorldEnv.current.patchDimensions),
)
const groundPatch = new GroundPatch(patchKey)
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(groundPatch.bounds)
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(
groundPatch.bounds,
)
const blockBiome = WorldUtils.process.getBlockBiome(
spawnOrigin,
groundPatch.bounds,
Expand Down
216 changes: 112 additions & 104 deletions src/datacontainers/BlocksBatch.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,126 @@
import { Vector2 } from "three"
import { computeGroundBlock } from "../api/world-compute"
import { WorldEnv, Biome, WorldUtils, WorldComputeProxy } from "../index"
import { serializePatchId, getPatchId, asVect3 } from "../utils/convert"
import { PatchKey, GroundBlock, WorldProcess, ProcessType } from "../utils/types"
import { GroundPatch } from "./GroundPatch"
import { Vector2 } from 'three'

import { computeGroundBlock } from '../api/world-compute'
import { WorldEnv, Biome, WorldUtils, WorldComputeProxy } from '../index'
import { serializePatchId, getPatchId, asVect3 } from '../utils/convert'
import {
PatchKey,
GroundBlock,
WorldProcess,
ProcessType,
} from '../utils/types'

import { GroundPatch } from './GroundPatch'

export type BlocksBatchArgs = {
posBatch: Vector2[]
posBatch: Vector2[]
}

export class BlocksBatch implements WorldProcess {
patchIndex: Record<PatchKey, Vector2[]> = {}
blocks: any[] = []
constructor(posBatch: Vector2[]) {
// sort input blocks by patch
// const blocksByPatch: Record<PatchKey, GroundBlock[]> = {}
for (const pos of posBatch) {
const patchId = getPatchId(pos, WorldEnv.current.patchDimensions)
const patchKey = serializePatchId(patchId)
this.patchIndex[patchKey] = this.patchIndex[patchKey] || []
this.patchIndex[patchKey]?.push(pos)
}
patchIndex: Record<PatchKey, Vector2[]> = {}
blocks: any[] = []
constructor(posBatch: Vector2[]) {
// sort input blocks by patch
// const blocksByPatch: Record<PatchKey, GroundBlock[]> = {}
for (const pos of posBatch) {
const patchId = getPatchId(pos, WorldEnv.current.patchDimensions)
const patchKey = serializePatchId(patchId)
this.patchIndex[patchKey] = this.patchIndex[patchKey] || []
this.patchIndex[patchKey]?.push(pos)
}
}

bake() {

}
bake() {}

async process() {
// const blocksBatch = posBatch.map(pos => {
async process() {
// const blocksBatch = posBatch.map(pos => {

// const data: BlockData = {
// level: 0,
// type: BlockType.NONE,
// }
// const block: Block<BlockData> = {
// pos: asVect3(pos),
// data,
// }
// const data: BlockData = {
// level: 0,
// type: BlockType.NONE,
// }
// const block: Block<BlockData> = {
// pos: asVect3(pos),
// data,
// }

// return block
// })
const blocksBatch = []
for (const [patchKey, posBatch] of Object.entries(this.patchIndex)) {
const groundPatch = new GroundPatch(patchKey)
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(groundPatch.bounds)
for (const blockPos of posBatch) {


const blockBiome = WorldUtils.process.getBlockBiome(
blockPos,
groundPatch.bounds,
biomeBoundsInfluences,
)
const blockData = computeGroundBlock(asVect3(blockPos), blockBiome)
// const {level, type } =
// override with last block if specified
// if (params.includeEntitiesBlocks) {
// const lastBlockData = await queryLastBlockData(blockPos)
// block.data =
// lastBlockData.level > 0 && lastBlockData.type
// ? lastBlockData
// : (block.data as any)
// }
blockPos.y = blockData.level
const block = {
pos: asVect3(blockPos),
data: blockData
}
blocksBatch.push(block)
}
// return block
// })
const blocksBatch = []
for (const [patchKey, posBatch] of Object.entries(this.patchIndex)) {
const groundPatch = new GroundPatch(patchKey)
const biomeBoundsInfluences = Biome.instance.getBoundsInfluences(
groundPatch.bounds,
)
for (const blockPos of posBatch) {
const blockBiome = WorldUtils.process.getBlockBiome(
blockPos,
groundPatch.bounds,
biomeBoundsInfluences,
)
const blockData = computeGroundBlock(asVect3(blockPos), blockBiome)
// const {level, type } =
// override with last block if specified
// if (params.includeEntitiesBlocks) {
// const lastBlockData = await queryLastBlockData(blockPos)
// block.data =
// lastBlockData.level > 0 && lastBlockData.type
// ? lastBlockData
// : (block.data as any)
// }
blockPos.y = blockData.level
const block = {
pos: asVect3(blockPos),
data: blockData,
}
this.blocks = blocksBatch
// return blocksBatch
// const blocksBatch = blockPosBatch.map((pos) => {
// const blockPos = asVect3(pos)
// const blockData = computeGroundBlock(blockPos)
// const { spawnableItems } = blockData
// const queriedLoc = new Box2().setFromPoints([asVect2(blockPos)])
// queriedLoc.max.addScalar(1)
// false && includeEntitiesBlocks && spawnableItems.forEach(itemType => {
// // several (overlapping) objects may be found at queried position
// const [spawnedEntity] = ItemsInventory.querySpawnedEntities(itemType, queriedLoc)
// const lastBlockIndex = blocksBuffer?.findLastIndex(elt => elt)
// if (blocksBuffer && lastBlockIndex && lastBlockIndex >= 0) {
// blockData.level += lastBlockIndex
// blockData.type = blocksBuffer[lastBlockIndex] as BlockType
// }
// })
// blockPos.y = blockData.level
// const block: Block = {
// pos: blockPos,
// data: blockData,
// }
// return block
// })
blocksBatch.push(block)
}
}
this.blocks = blocksBatch
// return blocksBatch
// const blocksBatch = blockPosBatch.map((pos) => {
// const blockPos = asVect3(pos)
// const blockData = computeGroundBlock(blockPos)
// const { spawnableItems } = blockData
// const queriedLoc = new Box2().setFromPoints([asVect2(blockPos)])
// queriedLoc.max.addScalar(1)
// false && includeEntitiesBlocks && spawnableItems.forEach(itemType => {
// // several (overlapping) objects may be found at queried position
// const [spawnedEntity] = ItemsInventory.querySpawnedEntities(itemType, queriedLoc)
// const lastBlockIndex = blocksBuffer?.findLastIndex(elt => elt)
// if (blocksBuffer && lastBlockIndex && lastBlockIndex >= 0) {
// blockData.level += lastBlockIndex
// blockData.type = blocksBuffer[lastBlockIndex] as BlockType
// }
// })
// blockPos.y = blockData.level
// const block: Block = {
// pos: blockPos,
// data: blockData,
// }
// return block
// })
}

toStub() {
return this.blocks
}
toStub() {
return this.blocks
}

// byProxy
static async proxyGen(posBatch: Vector2[], processingUnit = WorldComputeProxy.workerPool) {
const res = await processingUnit
.exec(ProcessType.BlocksBatch, [posBatch])
.then((blocksStubs: GroundBlock[]) => {
// parse worker's data to recreate original objects
const blocks = blocksStubs.map(blockStub => {
blockStub.pos = WorldUtils.convert.parseThreeStub(blockStub.pos)
return blockStub
}) as GroundBlock[]
return blocks
}) as GroundBlock[]
return res
}
}
// byProxy
static async proxyGen(
posBatch: Vector2[],
processingUnit = WorldComputeProxy.workerPool,
) {
const res = (await processingUnit
.exec(ProcessType.BlocksBatch, [posBatch])
.then((blocksStubs: GroundBlock[]) => {
// parse worker's data to recreate original objects
const blocks = blocksStubs.map(blockStub => {
blockStub.pos = WorldUtils.convert.parseThreeStub(blockStub.pos)
return blockStub
}) as GroundBlock[]
return blocks
})) as GroundBlock[]
return res
}
}
Loading

0 comments on commit de01b89

Please sign in to comment.