diff --git a/src/procgen/PatchBaseCache.ts b/src/procgen/PatchBaseCache.ts index fa2e35a..8e902bd 100644 --- a/src/procgen/PatchBaseCache.ts +++ b/src/procgen/PatchBaseCache.ts @@ -33,15 +33,17 @@ enum BatchProcessStep { Done, } -const cacheSyncProvider = (batch) => { - batch.kept?.length > 0 && PatchBlocksCache.cleanDeprecated(kept) - batch.created?.forEach((blocksCache: PatchBlocksCache) => PatchBlocksCache.instances.push(blocksCache)) +const cacheSyncProvider = (batch: any) => { + batch.kept?.length > 0 && PatchBlocksCache.cleanDeprecated(batch.kept) + batch.created?.forEach((blocksPatch: PatchBlocksCache) => + PatchBlocksCache.instances.push(blocksPatch), + ) } export class PatchBaseCache extends PatchCache { // eslint-disable-next-line no-use-before-define static instances: PatchBaseCache[] = [] - static bbox = new Box3() + static override bbox = new Box3() static cacheRadius = 20 static batch: { currentStep: BatchProcessStep @@ -56,15 +58,15 @@ export class PatchBaseCache extends PatchCache { // eslint-disable-next-line no-use-before-define skipped: PatchBaseCache[] } = { - currentStep: BatchProcessStep.RegularGen, - startTime: 0, - totalElapsedTime: 0, - count: 0, - totalCount: 0, - regular: [], - transition: [], - skipped: [], - } + currentStep: BatchProcessStep.RegularGen, + startTime: 0, + totalElapsedTime: 0, + count: 0, + totalCount: 0, + regular: [], + transition: [], + skipped: [], + } spawnedEntities: EntityData[] = [] extEntities: EntityData[] = [] @@ -85,15 +87,15 @@ export class PatchBaseCache extends PatchCache { ) } - static getPatch(inputPoint: Vector2 | Vector3) { + static override getPatch(inputPoint: Vector2 | Vector3) { return super.getPatch(inputPoint, this.instances) as PatchBaseCache } - static getPatches(inputBbox: Box3) { + static override getPatches(inputBbox: Box3) { return super.getPatches(inputBbox, this.instances) as PatchBaseCache[] } - getNearPatches() { + override getNearPatches(): PatchBaseCache[] { return super.getNearPatches(PatchBaseCache.instances) as PatchBaseCache[] } @@ -132,7 +134,7 @@ export class PatchBaseCache extends PatchCache { } static updateCache(center: Vector3, syncCache = cacheSyncProvider) { - const {patchSize} = PatchCache + const { patchSize } = PatchCache const { batch, cacheRadius } = PatchBaseCache const cacheSize = patchSize * cacheRadius const bbox = new Box3().setFromCenterAndSize( @@ -207,7 +209,8 @@ export class PatchBaseCache extends PatchCache { batch.totalElapsedTime = 0 batch.currentStep = BatchProcessStep.RegularGen const promise = new Promise(resolve => { - const wrapper = (batch) => batch.created || batch.kept ? syncCache(batch) : resolve(true) + const wrapper = (batch: any) => + batch.created || batch.kept ? syncCache(batch) : resolve(true) PatchBaseCache.buildNextPatch(wrapper) }) @@ -223,8 +226,8 @@ export class PatchBaseCache extends PatchCache { case BatchProcessStep.RegularGen: { const nextPatch = batch.regular.shift() if (nextPatch) { - const blocksCache = nextPatch.genGroundBlocks() - syncCache({ created: [blocksCache] }) + const blocksPatch = nextPatch.genGroundBlocks() + syncCache({ created: [blocksPatch] }) batch.count++ } else { const elapsedTime = Date.now() - batch.startTime @@ -253,8 +256,8 @@ export class PatchBaseCache extends PatchCache { case BatchProcessStep.TransitionGen: { const nextPatch = batch.transition.shift() if (nextPatch) { - const blocksCache = nextPatch.genGroundBlocks() - syncCache({ created: [blocksCache] }) + const blocksPatch = nextPatch.genGroundBlocks() + syncCache({ created: [blocksPatch] }) batch.count++ } else { const elapsedTime = Date.now() - batch.startTime @@ -381,7 +384,9 @@ export class PatchBaseCache extends PatchCache { *overBlocksIter() { const entities = this.getEntities() for (const entity of entities) { - const blocksIter = PatchBlocksCache.getPatch(this.bbox.getCenter(new Vector3())).getBlocks(entity.bbox) + const blocksIter = PatchBlocksCache.getPatch( + this.bbox.getCenter(new Vector3()), + ).getBlocks(entity.bbox) // let item: BlockIteratorRes = blocksIter.next() for (const block of blocksIter) { const overBlocksBuffer = Vegetation.singleton.fillBuffer( @@ -409,13 +414,17 @@ export class PatchBaseCache extends PatchCache { patch .getEntities() .filter(entity => entity.bbox.containsPoint(pos)) - .forEach(entity => - Vegetation.singleton.fillBuffer(pos, entity, buffer), - ) + .forEach(entity => Vegetation.singleton.fillBuffer(pos, entity, buffer)) } return buffer } + // genOvergroundBlocks(){ + // const { min } = this.bbox + // const blocksPatch = new PatchBlocksCache(new Vector2(min.x, min.z)) + // const blocksPatchIter = blocksPatch.iterator() + // } + /** * Gen blocks data that will be sent to external cache */ @@ -424,13 +433,13 @@ export class PatchBaseCache extends PatchCache { const patchId = min.x + ',' + min.z + '-' + max.x + ',' + max.z const prng = alea(patchId) const refPoints = this.isTransitionPatch ? this.buildRefPoints() : [] - const blocksCache = new PatchBlocksCache(new Vector2(min.x, min.z)) - const blocksCacheIter = blocksCache.iterator() - min.y = 255 + const blocksPatch = new PatchBlocksCache(new Vector2(min.x, min.z)) + const blocksPatchIter = blocksPatch.iterator() + min.y = 512 max.y = 0 let blockIndex = 0 - for (const blockData of blocksCacheIter) { + for (const blockData of blocksPatchIter) { blockData.pos.y = 0 // const patchCorner = points.find(pt => pt.distanceTo(blockData.pos) < 2) const biomeType = this.isBiomeTransition @@ -478,13 +487,15 @@ export class PatchBaseCache extends PatchCache { // const levelMax = blockData.cache.level + blockData.cache.overground.length min.y = Math.min(min.y, blockData.pos.y) max.y = Math.max(max.y, blockData.pos.y) - blocksCache.writeBlockAtIndex(blockIndex, blockData.pos.y, blockData.type) + blocksPatch.writeBlockAtIndex(blockIndex, blockData.pos.y, blockData.type) blockIndex++ } + blocksPatch.bbox.min = min + blocksPatch.bbox.max = max this?.bbox.getSize(this.dimensions) PatchBaseCache.bbox.union(this.bbox) PatchBlocksCache.bbox = PatchBaseCache.bbox this.state = PatchState.Filled - return blocksCache + return blocksPatch } } diff --git a/src/procgen/PatchBlocksCache.ts b/src/procgen/PatchBlocksCache.ts index 1848f48..bf170bc 100644 --- a/src/procgen/PatchBlocksCache.ts +++ b/src/procgen/PatchBlocksCache.ts @@ -1,4 +1,5 @@ import { Box3, Vector2, Vector3 } from 'three' + import { BlockType } from './Biome' import { PatchBaseCache } from './PatchBaseCache' import { PatchCache } from './PatchCache' @@ -16,8 +17,9 @@ export type BlockIteratorRes = IteratorResult * Blocks cache */ export class PatchBlocksCache extends PatchCache { + // eslint-disable-next-line no-use-before-define static instances: PatchBlocksCache[] = [] - static bbox = new Box3() + static override bbox = new Box3() static cacheSize = PatchCache.patchSize * 5 static patchCacheProvider: any // blocksCache: Uint16Array = new Uint16Array(Math.pow(PatchCache.patchSize, 2)) @@ -27,18 +29,23 @@ export class PatchBlocksCache extends PatchCache { } constructor(input: Vector2 | PatchBlocksCache) { - super(input instanceof Vector2 ? input : new Vector2(input.bbox.min.x, input.bbox.min.z)) - if (input.blocksCache) { - const bmin = new Vector3(...Object.values(input.bbox.min)) - const bmax = new Vector3(...Object.values(input.bbox.max)) + super( + input instanceof Vector2 + ? input + : new Vector2(input.bbox.min.x, input.bbox.min.z), + ) + if ((input as any).blocksCache) { + const { bbox, dimensions, blocksCache } = input as any + const bmin = new Vector3(...(Object.values(bbox.min) as any)) + const bmax = new Vector3(...(Object.values(bbox.max) as any)) this.bbox = new Box3(bmin, bmax) - this.dimensions = new Vector3(...Object.values(input.dimensions)) - this.blocksCache = input.blocksCache + this.dimensions = new Vector3(...(Object.values(dimensions) as any)) + this.blocksCache = blocksCache } PatchBlocksCache.bbox.union(this.bbox) } - static getPatch(inputPoint) { + static override getPatch(inputPoint: Vector3 | Vector2) { return super.getPatch(inputPoint, this.instances) as PatchBlocksCache // const patchRes = this.patchCache.find(patch => // patch.bbox.min.x === patchBbox.min.x @@ -49,12 +56,14 @@ export class PatchBlocksCache extends PatchCache { // return patchRes } - static getPatches(inputBbox: Box3) { + static override getPatches(inputBbox: Box3) { return super.getPatches(inputBbox, this.instances) as PatchBlocksCache[] } - getNearPatches() { - return super.getNearPatches(PatchBlocksCache.instances) as PatchBlocksCache[] + override getNearPatches(): PatchBlocksCache[] { + return super.getNearPatches( + PatchBlocksCache.instances, + ) as PatchBlocksCache[] } static getGroundBlock(globalPos: Vector3) { @@ -204,12 +213,7 @@ export class PatchBlocksCache extends PatchCache { } } - fromImport() { - - } - - toExport() { - - } + fromImport() {} + toExport() {} } diff --git a/src/procgen/PatchCache.ts b/src/procgen/PatchCache.ts index 48e5421..069795c 100644 --- a/src/procgen/PatchCache.ts +++ b/src/procgen/PatchCache.ts @@ -14,7 +14,7 @@ export class PatchCache { const bmin = new Vector3(patchOrigin.x, 0, patchOrigin.y) const bmax = new Vector3( patchOrigin.x + patchSize, - 255, + 512, patchOrigin.y + patchSize, ) this.bbox = new Box3(bmin, bmax) @@ -64,6 +64,8 @@ export class PatchCache { return res } + getPatchCoords() {} + getNearPatches(patchCache: PatchCache[]) { const dim = this.dimensions const patchCenter = this.bbox.getCenter(new Vector3())