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
+14
View File
@@ -188,6 +188,20 @@ function generators_for(seed: string): SeedGenerators {
// fills blocks (indexed y * CHUNK_AREA + z * CHUNK_SIZE + x) and each column's surface height and biome.
// blocks trees put in other chunks go to spill
// a column's biome without generating its chunk, from its height before 3d noise, so it can be a little off near
// coasts and cliffs. for searching far away, like /locatebiome
export function estimate_biome(seed: string, x: number, z: number): { biome: string; y: number } {
const { terrain } = generators_for(seed);
const column = terrain.column(x, z);
const y = Math.round(column.island ? column.island.top : column.height);
const flat = pick_biome(column, y, false);
// most columns are the same either way, the neighbors are only worth sampling when they aren't
if (flat === pick_biome(column, y, true)) return { biome: flat, y };
const neighbors = [[x - 1, z], [x + 1, z], [x, z - 1], [x, z + 1]].map(([nx, nz]) => terrain.column(nx, nz));
const steep = neighbors.some((neighbor) => Math.abs(neighbor.height - column.height) >= STEEP);
return { biome: steep ? pick_biome(column, y, true) : flat, y };
}
export function generate_overworld(
blocks: Uint32Array,
heights: Int32Array,
+13 -12
View File
@@ -105,15 +105,16 @@ Commands can only be registered during `setup`.
## The Engine's Commands
| Command | What it does |
| ------------------------------------------------------- | -------------------------------------------------------- |
| `/give <item> [count]` | Gives you an item |
| `/effect <effect> [seconds]` | Gives you a status effect, for 30 seconds by default |
| `/summon <entity>` | Spawns an entity where you are |
| `/time` | Shows the time |
| `/time set <day\|noon\|sunset\|night\|midnight\|ticks>` | Jumps to the next time of day. Time never goes back |
| `/time add <ticks>` | Moves time forward |
| `/tp <x> <y> <z>` | Teleports you there |
| `/tps` | Shows how the game loop keeps up |
| `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default |
| `/help [command]` | Every command's usage, or one's |
| Command | What it does |
| ------------------------------------------------------- | -------------------------------------------------------------------- |
| `/give <item> [count]` | Gives you an item |
| `/effect <effect> [seconds]` | Gives you a status effect, for 30 seconds by default |
| `/summon <entity>` | Spawns an entity where you are |
| `/time` | Shows the time |
| `/time set <day\|noon\|sunset\|night\|midnight\|ticks>` | Jumps to the next time of day. Time never goes back |
| `/time add <ticks>` | Moves time forward |
| `/locatebiome <biome>` | Says where the nearest biome of that kind is, up to 6400 blocks away |
| `/tp <x> <y> <z>` | Teleports you there |
| `/tps` | Shows how the game loop keeps up |
| `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default |
| `/help [command]` | Every command's usage, or one's |
+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) {
+16
View File
@@ -10,6 +10,7 @@ import {
literal,
word,
} from "$/common/commands.ts";
import { estimate_biome } from "$/common/worldgen/overworld.ts";
import { test_game } from "./helpers.ts";
interface Source {
@@ -157,3 +158,18 @@ Deno.test("/give suggests item ids as they're typed", async () => {
send(1, { type: "chat", text: "/give" });
assertEquals(take(1).find((m) => m.type === "chat").text, "Not enough: /give <item> [<count>]");
});
Deno.test("/locatebiome says where the nearest biome of a kind is", async () => {
const { game, join, take, send } = await test_game("mods");
join(1, "alice");
take(1);
const say = (text: string) => {
send(1, { type: "chat", text });
return take(1).filter((m) => m.type === "chat").map((m) => m.text).join("\n");
};
const answer = say("/locatebiome bworld:plains");
const [x, , z] = answer.match(/at (-?\d+) (-?\d+) (-?\d+)/)!.slice(1).map(Number);
assertEquals(estimate_biome(game.world.seed, x, z).biome, "bworld:plains");
assert(say("/locatebiome bworld:nowhere").includes("There's no biome bworld:nowhere"));
});