Skip to content

Commit

Permalink
fix: ts errors, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-85 authored and Sceat committed Jul 10, 2024
1 parent f1b4b1b commit 3622435
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 51 deletions.
75 changes: 43 additions & 32 deletions src/procgen/PatchBaseCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[] = []
Expand All @@ -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[]
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
})

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
40 changes: 22 additions & 18 deletions src/procgen/PatchBlocksCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Box3, Vector2, Vector3 } from 'three'

import { BlockType } from './Biome'
import { PatchBaseCache } from './PatchBaseCache'
import { PatchCache } from './PatchCache'
Expand All @@ -16,8 +17,9 @@ export type BlockIteratorRes = IteratorResult<BlockData, void>
* 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))
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -204,12 +213,7 @@ export class PatchBlocksCache extends PatchCache {
}
}

fromImport() {

}

toExport() {

}
fromImport() {}

toExport() {}
}
4 changes: 3 additions & 1 deletion src/procgen/PatchCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -64,6 +64,8 @@ export class PatchCache {
return res
}

getPatchCoords() {}

getNearPatches(patchCache: PatchCache[]) {
const dim = this.dimensions
const patchCenter = this.bbox.getCenter(new Vector3())
Expand Down

0 comments on commit 3622435

Please sign in to comment.