diff --git a/common/worldgen/overworld.ts b/common/worldgen/overworld.ts index b8cfd20..1f33153 100644 --- a/common/worldgen/overworld.ts +++ b/common/worldgen/overworld.ts @@ -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, diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index f17aa5c..8b689df 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -105,15 +105,16 @@ Commands can only be registered during `setup`. ## The Engine's Commands -| Command | What it does | -| ------------------------------------------------------- | -------------------------------------------------------- | -| `/give [count]` | Gives you an item | -| `/effect [seconds]` | Gives you a status effect, for 30 seconds by default | -| `/summon ` | Spawns an entity where you are | -| `/time` | Shows the time | -| `/time set ` | Jumps to the next time of day. Time never goes back | -| `/time add ` | Moves time forward | -| `/tp ` | 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 [count]` | Gives you an item | +| `/effect [seconds]` | Gives you a status effect, for 30 seconds by default | +| `/summon ` | Spawns an entity where you are | +| `/time` | Shows the time | +| `/time set ` | Jumps to the next time of day. Time never goes back | +| `/time add ` | Moves time forward | +| `/locatebiome ` | Says where the nearest biome of that kind is, up to 6400 blocks away | +| `/tp ` | 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 | diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 1480e56..907c93b 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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) => this.mods.register_command("engine", node); @@ -1234,6 +1259,30 @@ export class GameServer { ), ); + register( + literal("locatebiome").describe("Finds the nearest biome of a kind").then( + argument("biome", word()) + .suggests((partial) => + Object.keys(BIOMES).filter((id) => id.startsWith(partial)).map((text) => ({ text })) + ) + .executes((context) => { + const id = context.get("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("tps").describe("Shows how the game loop keeps up").executes((context) => { if (!this.loop) { diff --git a/tests/commands_test.ts b/tests/commands_test.ts index 2813da4..d25e038 100644 --- a/tests/commands_test.ts +++ b/tests/commands_test.ts @@ -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 []"); }); + +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")); +});