-
Notifications
You must be signed in to change notification settings - Fork 5
Add exclusion zones to movement settings (like upstream mineflayer-pathfinder) #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
XaXayo12
wants to merge
3
commits into
Minecraft-Pathfinding:2026-rewrite
from
XaXayo12:feature/exclusion-zones
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| 'use strict' | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Exclusion zones example | ||
| // | ||
| // An "exclusion area" is just a function (block) => number that returns the | ||
| // extra cost of using a block: 0 = fine, a positive number = soft avoid, | ||
| // Infinity = hard "keep out". The library ships no ready-made shapes on | ||
| // purpose, so the small box/radius builders below are yours to copy and adapt. | ||
| // | ||
| // They go into three movement settings: | ||
| // exclusionAreasStep -> blocks the bot may stand in / walk into | ||
| // exclusionAreasBreak -> blocks the bot may break (mine) | ||
| // exclusionAreasPlace -> blocks the bot may place (build on) | ||
| // | ||
| // Run a local server, then: node examples/exclusionZones.js | ||
| // In chat: "goto <x> <y> <z>", "zones on", "zones off". | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const { createBot } = require('mineflayer') | ||
| const { Vec3 } = require('vec3') | ||
| const { createPlugin, goals } = require('../dist') | ||
|
|
||
| const { GoalBlock } = goals | ||
|
|
||
| // --- copy these helpers into your own project ------------------------------ | ||
|
|
||
| // A box between two opposite corners (inclusive, any order). | ||
| function boxExclusion (corner1, corner2, cost = Infinity) { | ||
| const minX = Math.min(corner1.x, corner2.x) | ||
| const minY = Math.min(corner1.y, corner2.y) | ||
| const minZ = Math.min(corner1.z, corner2.z) | ||
| const maxX = Math.max(corner1.x, corner2.x) | ||
| const maxY = Math.max(corner1.y, corner2.y) | ||
| const maxZ = Math.max(corner1.z, corner2.z) | ||
| return (block) => { | ||
| const p = block.position | ||
| const inside = | ||
| p.x >= minX && p.x <= maxX && | ||
| p.y >= minY && p.y <= maxY && | ||
| p.z >= minZ && p.z <= maxZ | ||
| return inside ? cost : 0 | ||
| } | ||
| } | ||
|
|
||
| // A ball (sphere) of the given radius around a center point. | ||
| function radiusExclusion (center, radius, cost = Infinity) { | ||
| const r2 = radius * radius | ||
| return (block) => { | ||
| const dx = block.position.x - center.x | ||
| const dy = block.position.y - center.y | ||
| const dz = block.position.z - center.z | ||
| return dx * dx + dy * dy + dz * dz <= r2 ? cost : 0 | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const bot = createBot({ | ||
| username: 'exclusion-demo', | ||
| auth: 'offline', | ||
| host: 'localhost', | ||
| port: 25565 | ||
| }) | ||
|
|
||
| bot.loadPlugin(createPlugin()) | ||
|
|
||
| // Example zones (tweak the coordinates to match your world): | ||
| const noGoBox = boxExclusion(new Vec3(-8, 60, -8), new Vec3(8, 80, 8)) // hard | ||
| const softBall = radiusExclusion(new Vec3(30, 64, 30), 6, 50) // soft | ||
|
|
||
| function enableZones () { | ||
| bot.pathfinder.setMoveOptions({ | ||
| exclusionAreasStep: [noGoBox, softBall] | ||
| }) | ||
| bot.chat('Exclusion zones: ON') | ||
| } | ||
|
|
||
| function disableZones () { | ||
| bot.pathfinder.setMoveOptions({ | ||
| exclusionAreasStep: [], | ||
| exclusionAreasBreak: [], | ||
| exclusionAreasPlace: [] | ||
| }) | ||
| bot.chat('Exclusion zones: OFF') | ||
| } | ||
|
|
||
| bot.once('spawn', () => { | ||
| enableZones() | ||
| bot.chat('Ready. Try: "goto <x> <y> <z>", "zones on", "zones off".') | ||
| }) | ||
|
|
||
| bot.on('chat', async (username, message) => { | ||
| if (username === bot.username) return | ||
|
|
||
| const [cmd, ...args] = message.trim().split(/\s+/) | ||
|
|
||
| if (cmd === 'zones') { | ||
| if (args[0] === 'off') disableZones() | ||
| else enableZones() | ||
| return | ||
| } | ||
|
|
||
| if (cmd === 'goto') { | ||
| const [x, y, z] = args.map(Number) | ||
| if ([x, y, z].some(Number.isNaN)) { | ||
| bot.chat('Usage: goto <x> <y> <z>') | ||
| return | ||
| } | ||
|
|
||
| bot.chat(`Heading to ${x} ${y} ${z}, avoiding the zones...`) | ||
| try { | ||
| await bot.pathfinder.goto(new GoalBlock(x, y, z)) | ||
| bot.chat('Arrived!') | ||
| } catch (err) { | ||
| bot.chat(`Could not get there: ${err.message}`) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| bot.on('kicked', console.log) | ||
| bot.on('error', console.log) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import type { BlockInfo } from '../world/cacheWorld' | ||
|
|
||
| /** | ||
| * An exclusion area: a function that returns the extra cost of letting the bot | ||
| * use a given block. | ||
| * | ||
| * - `0` -> no opinion on this block. | ||
| * - a positive number -> a soft penalty; the bot avoids the block when it can. | ||
| * - `>= COST_INF` -> a hard "keep out"; the bot will never use the block. | ||
| * | ||
| * These are stored in the three movement settings `exclusionAreasStep`, | ||
| * `exclusionAreasBreak` and `exclusionAreasPlace`. The pathfinder intentionally | ||
| * ships no ready-made shapes — write your own, or copy the box/radius helpers | ||
| * from `examples/exclusionZones.js`. This mirrors upstream mineflayer-pathfinder. | ||
| */ | ||
| export type ExclusionArea = (block: BlockInfo) => number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docs are too conversational. Make this more concise.