Locate biome

This commit is contained in:
2026-10-02 14:16:50 -03:00
parent d2dde8c14f
commit 5a018a0470
4 changed files with 92 additions and 12 deletions
+49
View File
@@ -45,6 +45,7 @@ import {
number,
word,
} from "$/common/commands.ts";
import { BIOMES, estimate_biome } from "$/common/worldgen/overworld.ts";
import type { GameLoop } from "./game_loop.ts";
import { update_crafting_result } from "./crafting.ts";
import type { Menu } from "./menu.ts";
@@ -77,6 +78,9 @@ const PICKUP_REACH_Y = 0.5;
// tiles further than this many chunks from every player don't tick
const SIMULATION_DISTANCE = 6;
const MAX_GIVE = 64 * 36;
// /locatebiome looks at columns this far apart, out to this far, like minecraft's
const LOCATE_STEP = 32;
const LOCATE_RADIUS = 6400;
// the most suggestions sent for one request
const MAX_SUGGESTIONS = 100;
// falls up to this many blocks don't hurt, like minecraft
@@ -1116,6 +1120,27 @@ export class GameServer {
});
}
// like minecraft's /locate biome: columns LOCATE_STEP apart, in square rings out from x, z. the closest in the
// first ring that has one
#locate_biome(biome: string, x: number, z: number) {
for (let ring = 0; ring * LOCATE_STEP <= LOCATE_RADIUS; ring++) {
let best: { x: number; y: number; z: number; distance: number } | undefined;
for (let dx = -ring; dx <= ring; dx++) {
for (let dz = -ring; dz <= ring; dz++) {
if (Math.max(Math.abs(dx), Math.abs(dz)) !== ring) continue;
const [cx, cz] = [x + dx * LOCATE_STEP, z + dz * LOCATE_STEP];
const found = estimate_biome(this.world.seed, cx, cz);
const distance = Math.hypot(dx, dz);
if (found.biome === biome && (!best || distance < best.distance)) {
best = { x: cx, y: found.y, z: cz, distance };
}
}
}
if (best) return best;
}
return undefined;
}
// the engine's own commands, registered like mods' so they're in /help and suggested the same way
#register_commands() {
const register = (node: CommandNode<ServerCommandSource>) => this.mods.register_command("engine", node);
@@ -1234,6 +1259,30 @@ export class GameServer {
),
);
register(
literal<ServerCommandSource>("locatebiome").describe("Finds the nearest biome of a kind").then(
argument<string, ServerCommandSource>("biome", word())
.suggests((partial) =>
Object.keys(BIOMES).filter((id) => id.startsWith(partial)).map((text) => ({ text }))
)
.executes((context) => {
const id = context.get<string>("biome");
if (!BIOMES[id]) throw new CommandError(`There's no biome ${id}`);
const player = context.source.server_player;
const found = this.#locate_biome(id, Math.floor(player.x), Math.floor(player.z));
if (!found) {
throw new CommandError(`There's no ${display_name(id)} within ${LOCATE_RADIUS} blocks`);
}
const distance = Math.round(Math.hypot(found.x - player.x, found.z - player.z));
context.source.send_message(
`The nearest ${
display_name(id)
} is at ${found.x} ${found.y} ${found.z}, ${distance} blocks away`,
);
}),
),
);
register(
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
if (!this.loop) {