Skip to content

Commit

Permalink
fix: load correct positions of chunks!
Browse files Browse the repository at this point in the history
add basic vitest
  • Loading branch information
zardoy committed Oct 6, 2023
1 parent 6a8410a commit 30bfb8d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"lint": "standard test/*.test.js src/**/*.js src/**/**/*.js src/*.js examples/*.js *.js",
"fix": "standard --fix test/*.test.js src/**/*.js src/**/**/*.js src/*.js examples/*.js *.js",
"mocha_test": "mocha --reporter spec --timeout 3000 --exit",
"vitest": "vitest",
"test": "npm run mocha_test",
"pretest": "npm run lint"
},
Expand Down Expand Up @@ -52,7 +53,6 @@
"random-seed": "^0.3.0",
"range": "^0.0.3",
"readline": "^1.3.0",
"spiralloop": "^1.0.2",
"uuid-1345": "^1.0.1",
"vec3": "^0.1.6",
"yargs": "^17.0.1"
Expand All @@ -64,13 +64,19 @@
"bugs": {
"url": "http://github.com/PrismarineJS/flying-squid/issues"
},
"standard": {
"ignore": [
"src/**/*.ts"
]
},
"devDependencies": {
"expect": "^29.1.2",
"flying-squid": "file:.",
"longjohn": "^0.2.12",
"minecraft-wrap": "^1.2.3",
"mineflayer": "^4.0.0",
"mocha": "^10.0.0",
"standard": "^17.0.0"
"standard": "^17.0.0",
"vitest": "^0.34.6"
}
}
24 changes: 8 additions & 16 deletions src/lib/plugins/world.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const spiralloop = require('spiralloop')
const Vec3 = require('vec3').Vec3

const generations = require('flying-squid').generations
Expand All @@ -7,6 +6,7 @@ const fs = require('fs')
const { level } = require('prismarine-provider-anvil')

const playerDat = require('../playerDat')
const { spiral } = require('../../utils')

const fsStat = promisify(fs.stat)
const fsMkdir = promisify(fs.mkdir)
Expand Down Expand Up @@ -223,7 +223,7 @@ module.exports.player = function (player, serv, settings) {
location: {
x,
y,
z,
z
},
action,
nbtData: blockEntity
Expand All @@ -233,28 +233,20 @@ module.exports.player = function (player, serv, settings) {
})
}

function spiral (arr) {
const t = []
spiralloop(arr, (x, z) => {
t.push([x, z])
})
return t
}

player.sendNearbyChunks = (view, group) => {
player.sendNearbyChunks = (viewDistance, group) => {
player.lastPositionChunkUpdated = player.position
const playerChunkX = Math.floor(player.position.x / 16)
const playerChunkZ = Math.floor(player.position.z / 16)

Object.keys(player.loadedChunks)
.map((key) => key.split(',').map(a => parseInt(a)))
.filter(([x, z]) => Math.abs(x - playerChunkX) > view || Math.abs(z - playerChunkZ) > view)
.filter(([x, z]) => Math.abs(x - playerChunkX) > viewDistance || Math.abs(z - playerChunkZ) > viewDistance)
.forEach(([x, z]) => player._unloadChunk(x, z))

return spiral([view * 2, view * 2])
return spiral(viewDistance)
.map(t => ({
chunkX: playerChunkX + t[0] - view,
chunkZ: playerChunkZ + t[1] - view
chunkX: playerChunkX + t[0],
chunkZ: playerChunkZ + t[1]
}))
.filter(({ chunkX, chunkZ }) => serv._loadPlayerChunk(chunkX, chunkZ, player))
.reduce((acc, { chunkX, chunkZ }) => {
Expand All @@ -271,7 +263,7 @@ module.exports.player = function (player, serv, settings) {
}

player.sendMap = () => {
return player.sendNearbyChunks(Math.min(3, settings['view-distance']))
return player.sendNearbyChunks(settings['view-distance'])
.catch((err) => setTimeout(() => { throw err }), 0)
}

Expand Down
28 changes: 28 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export function spiral(distance: number) {
// eg if distance is 1, we get 9 chunks
const arr: [number, number][] = []
for (let i = -distance; i <= distance; i++) {
for (let j = -distance; j <= distance; j++) {
arr.push([i, j])
}
}
return arr
}

export class ViewRect {
x0: number
x1: number
z0: number
z1: number

constructor(cx: number, cz: number, viewDistance: number) {
this.x0 = cx - viewDistance
this.x1 = cx + viewDistance
this.z0 = cz - viewDistance
this.z1 = cz + viewDistance
}

contains(x, z) {
return this.x0 < x && x <= this.x1 && this.z0 < z && z <= this.z1
}
}
15 changes: 15 additions & 0 deletions src/utils/simpleUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { spiral, ViewRect } from './index'
import { test, expect } from 'vitest'

test('spiral', () => {
expect(spiral(0)).toEqual([[-0, -0]])
expect(spiral(1)).toEqual([[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]])
expect(spiral(2).length).toEqual(25)
})

test('ViewRect', () => {
const rect = new ViewRect(0, 0, 1)
expect(rect.contains(0, 0)).toEqual(true)
expect(rect.contains(0, 1)).toEqual(true)
expect(rect.contains(0, 2)).toEqual(false)
})
5 changes: 5 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
root: 'src'
})

0 comments on commit 30bfb8d

Please sign in to comment.