@zardoy/flying-squid 0.0.137 → 0.0.139

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -277,9 +277,27 @@ const server = function (serv, options) {
277
277
  return;
278
278
  eyeBusy.add(player);
279
279
  try {
280
- const target = await nearestStronghold(player.world, player.position.x, player.position.z);
280
+ const world = player.world;
281
+ // say why there is nothing to point at (the eye is not used up in these cases)
282
+ if (world.environment !== 'normal') {
283
+ player.chat?.('The eye of ender only points to strongholds in the overworld.');
284
+ return;
285
+ }
286
+ if (!world.strongholds) {
287
+ player.chat?.(`This world has no strongholds (world generator: ${world.generatorName ?? 'unknown'}, only the noise generator makes them).`);
288
+ return;
289
+ }
290
+ let target;
291
+ try {
292
+ target = await nearestStronghold(world, player.position.x, player.position.z);
293
+ }
294
+ catch (e) {
295
+ serv.warn?.('stronghold search failed: ' + e.message);
296
+ player.chat?.('The stronghold search failed: ' + e.message);
297
+ return;
298
+ }
281
299
  if (!target) {
282
- player.chat?.('The eye has nothing to point at here.');
300
+ player.chat?.('No stronghold was found in this world.');
283
301
  return;
284
302
  }
285
303
  const start = player.position.offset(0, 1.62 * 0.5 + 0.2, 0);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.player = exports.server = void 0;
4
4
  const vec3_1 = require("vec3");
5
+ const spawnSearch_1 = require("../spawnSearch");
5
6
  function randomInt(low, high) {
6
7
  return Math.floor(Math.random() * (high - low) + low);
7
8
  }
@@ -14,6 +15,8 @@ const server = function (serv, settings) {
14
15
  if (mcData.blocksByName.flowing_water !== undefined) {
15
16
  waterBlocks.add(mcData.blocksByName.flowing_water.id);
16
17
  }
18
+ const surfaceSpawns = new WeakMap();
19
+ /** old search (fallback when no column in 11 x 11 chunks has a dry top): from y 81 down to the ground, then +x out of water */
17
20
  async function findSpawnZone(world, initialPoint) {
18
21
  let point = initialPoint;
19
22
  while ((await (world.getBlockStateId(point))) === 0 && point.y > 0) {
@@ -42,6 +45,20 @@ const server = function (serv, settings) {
42
45
  if (world.generatorName === 'empty') {
43
46
  return new vec3_1.Vec3(0, 65, 0);
44
47
  }
48
+ // like vanilla: the spawn chunk from the climate (noise worlds), then the first dry surface column in and around it. Found once per world.
49
+ let found = surfaceSpawns.get(world);
50
+ if (!found) {
51
+ found = (async () => {
52
+ const origin = (await world.spawnPosition?.()) ?? { x: randomInt(0, 30), z: randomInt(0, 30) };
53
+ const minY = world['min_y'] ?? 0;
54
+ const maxY = minY + (world['height'] ?? 256);
55
+ return (0, spawnSearch_1.findSurfaceSpawn)(async (cx, cz) => world.getColumn(cx, cz), origin, minY, maxY);
56
+ })();
57
+ surfaceSpawns.set(world, found);
58
+ }
59
+ const surface = await found;
60
+ if (surface)
61
+ return surface.clone();
45
62
  return findSpawnZone(world, new vec3_1.Vec3(randomInt(0, 30), 81, randomInt(0, 30)));
46
63
  };
47
64
  };
@@ -21,6 +21,11 @@ export interface WorldSpec {
21
21
  export interface CustomWorld extends World {
22
22
  /** block positions (x, z) of the strongholds of this world, `limit` = first rings only (noise generator, overworld) */
23
23
  strongholds?: (limit?: number) => Promise<Array<[number, number]>>;
24
+ /** block x/z of the world spawn from the climate (noise generator, overworld; vanilla's spawn chunk) */
25
+ spawnPosition?: () => Promise<{
26
+ x: number;
27
+ z: number;
28
+ } | undefined>;
24
29
  /** releases the chunk generator (worker side caches) */
25
30
  disposeGenerator?: () => Promise<void>;
26
31
  blockEntityData: Record<string, any>;
@@ -225,6 +225,7 @@ const server = async function (serv, options) {
225
225
  serv.overworld.seed = serv.seed = generationOptions.seed;
226
226
  serv.overworld.generatorName = generation.name;
227
227
  serv.overworld.strongholds = originalGenerator.strongholds;
228
+ serv.overworld.spawnPosition = originalGenerator.spawnPosition;
228
229
  patchWorld(serv.overworld);
229
230
  dedupeGetColumn(serv.overworld);
230
231
  // ---- world registry (Multiverse-style): any number of worlds; each has an environment (= client dimension type) and a generator
@@ -332,6 +333,7 @@ const server = async function (serv, options) {
332
333
  world['min_y'] = opts.minY;
333
334
  world.disposeGenerator = generator?.dispose;
334
335
  world.strongholds = generator?.strongholds;
336
+ world.spawnPosition = generator?.spawnPosition;
335
337
  world['height'] = opts.worldHeight;
336
338
  dedupeGetColumn(world);
337
339
  world.seed = worldSeed;
@@ -0,0 +1,20 @@
1
+ import { Vec3 } from 'vec3';
2
+ type ColumnBlock = {
3
+ name: string;
4
+ boundingBox?: string;
5
+ shapes?: number[][];
6
+ getProperties?: () => Record<string, unknown>;
7
+ };
8
+ export type SpawnColumn = {
9
+ getBlock: (pos: Vec3) => ColumnBlock;
10
+ };
11
+ /** getLevelRespawnPos for a dimension without a ceiling: the feet position above the column's top full block, null when the top is a fluid */
12
+ export declare function respawnPosInColumn(column: SpawnColumn, x: number, z: number, minY: number, maxY: number): Vec3 | null;
13
+ /** chunk offsets of vanilla's spawn search spiral: (0, 0) first, then outwards, within -5..5 */
14
+ export declare function spawnChunkSpiral(): Array<[number, number]>;
15
+ /** first valid surface spawn around the block position `origin` (x, z), null when no column in 11 x 11 chunks has a dry top */
16
+ export declare function findSurfaceSpawn(getColumn: (chunkX: number, chunkZ: number) => Promise<SpawnColumn>, origin: {
17
+ x: number;
18
+ z: number;
19
+ }, minY: number, maxY: number): Promise<Vec3 | null>;
20
+ export {};
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.respawnPosInColumn = respawnPosInColumn;
4
+ exports.spawnChunkSpiral = spawnChunkSpiral;
5
+ exports.findSurfaceSpawn = findSurfaceSpawn;
6
+ const vec3_1 = require("vec3");
7
+ /**
8
+ * World spawn on the surface like vanilla (MinecraftServer.setInitialSpawn + PlayerSpawnFinder.getSpawnPosInChunk / getLevelRespawnPos):
9
+ * search the spawn chunk and then the chunks around it (spiral, 11 x 11 chunks) for the first column whose top is dry, and spawn on its
10
+ * top-most block with a full top face. Starting from the top of the column means never a cave; a column topped by water is skipped.
11
+ */
12
+ /** blocks that hold a fluid (vanilla: a non-empty FluidState) */
13
+ const FLUID_BLOCKS = new Set(['water', 'lava', 'bubble_column', 'kelp', 'kelp_plant', 'seagrass', 'tall_seagrass']);
14
+ const hasFluid = (block) => FLUID_BLOCKS.has(block.name) || block.getProperties?.().waterlogged === true;
15
+ /** Block.isFaceFull(collisionShape, UP): one collision box covers the whole top face */
16
+ const hasFullTopFace = (block) => block.boundingBox === 'block' && (block.shapes ?? []).some(([x0, , z0, x1, y1, z1]) => x0 <= 0 && z0 <= 0 && x1 >= 1 && z1 >= 1 && y1 >= 1);
17
+ /** getLevelRespawnPos for a dimension without a ceiling: the feet position above the column's top full block, null when the top is a fluid */
18
+ function respawnPosInColumn(column, x, z, minY, maxY) {
19
+ const local = new vec3_1.Vec3(x & 15, 0, z & 15);
20
+ for (let y = maxY - 1; y >= minY; y--) {
21
+ local.y = y;
22
+ const block = column.getBlock(local);
23
+ if (hasFluid(block))
24
+ return null;
25
+ if (hasFullTopFace(block))
26
+ return new vec3_1.Vec3(x, y + 1, z);
27
+ }
28
+ return null;
29
+ }
30
+ /** chunk offsets of vanilla's spawn search spiral: (0, 0) first, then outwards, within -5..5 */
31
+ function spawnChunkSpiral() {
32
+ const out = [];
33
+ let x = 0;
34
+ let z = 0;
35
+ let dx = 0;
36
+ let dz = -1;
37
+ for (let i = 0; i < 121; i++) {
38
+ if (x >= -5 && x <= 5 && z >= -5 && z <= 5)
39
+ out.push([x, z]);
40
+ if (x === z || (x < 0 && x === -z) || (x > 0 && x === 1 - z)) {
41
+ const t = dx;
42
+ dx = -dz;
43
+ dz = t;
44
+ }
45
+ x += dx;
46
+ z += dz;
47
+ }
48
+ return out;
49
+ }
50
+ /** first valid surface spawn around the block position `origin` (x, z), null when no column in 11 x 11 chunks has a dry top */
51
+ async function findSurfaceSpawn(getColumn, origin, minY, maxY) {
52
+ const ocx = Math.floor(origin.x / 16);
53
+ const ocz = Math.floor(origin.z / 16);
54
+ for (const [dx, dz] of spawnChunkSpiral()) {
55
+ const column = await getColumn(ocx + dx, ocz + dz);
56
+ const x0 = (ocx + dx) * 16;
57
+ const z0 = (ocz + dz) * 16;
58
+ for (let x = x0; x < x0 + 16; x++) {
59
+ for (let z = z0; z < z0 + 16; z++) {
60
+ const pos = respawnPosInColumn(column, x, z, minY, maxY);
61
+ if (pos)
62
+ return pos;
63
+ }
64
+ }
65
+ }
66
+ return null;
67
+ }
@@ -18,6 +18,10 @@
18
18
  declare function generation(options?: any): {
19
19
  (chunkX: number, chunkZ: number): Promise<any>;
20
20
  strongholds(limit?: number): Promise<Array<[number, number]>>;
21
+ spawnPosition(): Promise<{
22
+ x: number;
23
+ z: number;
24
+ } | undefined>;
21
25
  dispose(): Promise<void>;
22
26
  };
23
27
  export = generation;
@@ -301,6 +301,13 @@ function generation(options = {}) {
301
301
  /** releases the worker generator (and the pool when it was the last user) */
302
302
  /** block positions (x, z) of the strongholds (eyes of ender point to them); `limit`: only the first rings (nearest the origin first) */
303
303
  generateChunk.strongholds = async (limit) => (dimension === 'overworld' ? (await getSource()).strongholds(limit) : []);
304
+ /** block x/z of the world spawn from the climate (vanilla's spawn chunk); undefined outside the overworld or with an older minecraft-worldgen */
305
+ generateChunk.spawnPosition = async () => {
306
+ if (dimension !== 'overworld')
307
+ return undefined;
308
+ const source = await getSource();
309
+ return typeof source.spawnPosition === 'function' ? source.spawnPosition() : undefined;
310
+ };
304
311
  generateChunk.dispose = async () => { if (sourceP) {
305
312
  const p = sourceP;
306
313
  sourceP = undefined;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zardoy/flying-squid",
3
3
  "description": "A minecraft server written in TS",
4
- "version": "0.0.137",
4
+ "version": "0.0.139",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "bin": "dist/app.js",
@@ -28,7 +28,7 @@
28
28
  "flatmap": "^0.0.3",
29
29
  "long": "^5.1.0",
30
30
  "mc-bridge": "^0.1.1",
31
- "minecraft-worldgen": "^0.1.4",
31
+ "minecraft-worldgen": "^0.1.7",
32
32
  "minecraft-data": "^3.95.0",
33
33
  "minecraft-protocol": "^1.60.1",
34
34
  "mkdirp": "^2.1.3",