From 9a294f0c98cce15d03a33a72b3687453a1d2f798 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 01:40:39 -0500 Subject: [PATCH] feat: Add Point class to represent 3D coordinates with distance calculations and neighbor retrieval --- server/pathfinding/Point.js | 100 ++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 server/pathfinding/Point.js diff --git a/server/pathfinding/Point.js b/server/pathfinding/Point.js new file mode 100644 index 0000000..963d875 --- /dev/null +++ b/server/pathfinding/Point.js @@ -0,0 +1,100 @@ +/** + * Point - Represents a 3D coordinate in the Minecraft world + * Inspired by runi95/turtle-control-panel Point class + */ +export class Point { + constructor(x, y, z) { + this.x = x; + this.y = y; + this.z = z; + } + + /** + * Returns the Manhattan distance to another point + */ + distanceTo(other) { + return Math.abs(this.x - other.x) + Math.abs(this.y - other.y) + Math.abs(this.z - other.z); + } + + /** + * Returns the Euclidean distance to another point (used for D* Lite heuristic) + */ + euclideanDistanceTo(other) { + const dx = this.x - other.x; + const dy = this.y - other.y; + const dz = this.z - other.z; + return Math.sqrt(dx * dx + dy * dy + dz * dz); + } + + /** + * Check equality with another point + */ + equals(other) { + if (!other) return false; + return this.x === other.x && this.y === other.y && this.z === other.z; + } + + /** + * Get all 6 neighboring points (up, down, north, south, east, west) + */ + getNeighbors() { + return [ + new Point(this.x + 1, this.y, this.z), // East + new Point(this.x - 1, this.y, this.z), // West + new Point(this.x, this.y + 1, this.z), // Up + new Point(this.x, this.y - 1, this.z), // Down + new Point(this.x, this.y, this.z + 1), // South + new Point(this.x, this.y, this.z - 1), // North + ]; + } + + /** + * Get the cardinal direction needed to move from this point to another adjacent point + * Returns: 'forward', 'back', 'up', 'down', or facing adjustment needed + */ + directionTo(other) { + const dx = other.x - this.x; + const dy = other.y - this.y; + const dz = other.z - this.z; + + if (dy === 1) return 'up'; + if (dy === -1) return 'down'; + if (dx === 1) return { facing: 1 }; // East + if (dx === -1) return { facing: 3 }; // West + if (dz === 1) return { facing: 2 }; // South + if (dz === -1) return { facing: 0 }; // North + + return null; + } + + /** + * Unique string key for maps + */ + toKey() { + return `${this.x},${this.y},${this.z}`; + } + + /** + * Create Point from key string + */ + static fromKey(key) { + const [x, y, z] = key.split(',').map(Number); + return new Point(x, y, z); + } + + /** + * Create Point from object with x,y,z properties + */ + static from(obj) { + if (!obj) return null; + return new Point(obj.x, obj.y, obj.z); + } + + toString() { + return `(${this.x}, ${this.y}, ${this.z})`; + } + + toJSON() { + return { x: this.x, y: this.y, z: this.z }; + } +}