diff options
Diffstat (limited to 'features/waypoints')
-rw-r--r-- | features/waypoints/index.js | 42 | ||||
-rw-r--r-- | features/waypoints/pathfind.js | 96 |
2 files changed, 126 insertions, 12 deletions
diff --git a/features/waypoints/index.js b/features/waypoints/index.js index 755780d..d13a897 100644 --- a/features/waypoints/index.js +++ b/features/waypoints/index.js @@ -15,18 +15,20 @@ class Waypoints extends Feature { onEnable(){ this.initVariables() - new SettingBase("/addwaypoint [name] [x] [y] [z] [r?] [g?] [b?]", "Allows you to create a waypoint", undefined, "create_waypoint", this) + new SettingBase("/addwaypoint [name] [x] [y] [z] [r?] [g?] [b?] [area?]", "Allows you to create a waypoint", undefined, "create_waypoint", this) new SettingBase("/delwaypoint [name]", "Allows you to delete a waypoint", undefined, "delete_waypoint", this) new SettingBase("/clearwaypoints", "Allows you to clear all the waypoints", undefined, "clear_waypoints", this) new SettingBase("/savewaypoints", "Copys the waypoints to your clipboard", undefined, "save_waypoints", this) new SettingBase("/loadwaypoints", "Loads waypoints from your clipboard", undefined, "load_waypoints", this) this.waypoints = [] - this.userWaypoints = {} + this.userWaypoints = JSON.parse(FileLib.read("soopyAddonsData", "soopyv2userwaypoints.json") || "{}") + this.userWaypointsArr = Object.values(this.userWaypoints) + this.waypointsChanged = false this.registerForge(RenderWorldLastEvent, this.renderWorldLast) - this.registerCommand("addwaypoint", (name, x=Math.floor(Player.getX()).toString(), y=Math.floor(Player.getY()).toString(), z=Math.floor(Player.getZ()).toString(), r="0", g="255", b="0")=>{ + this.registerCommand("addwaypoint", (name, x=Math.floor(Player.getX()).toString(), y=Math.floor(Player.getY()).toString(), z=Math.floor(Player.getZ()).toString(), r="0", g="255", b="0", area)=>{ let lx = 0 let ly = 0 let lz = 0 @@ -43,23 +45,31 @@ class Waypoints extends Feature { } this.userWaypoints[name] = { - x: parseFloat(x.replace("l", lx)), - y: parseFloat(y.replace("l", ly)), - z: parseFloat(z.replace("l", lz)), - r: parseInt(r), - g: parseInt(g), - b: parseInt(b), + x: parseFloat(x.replace("l", lx).replace('p', Math.floor(Player.getX()))), + y: parseFloat(y.replace("l", ly).replace('p', Math.floor(Player.getY()))), + z: parseFloat(z.replace("l", lz).replace('p', Math.floor(Player.getZ()))), + r: parseInt(r)/255, + g: parseInt(g)/255, + b: parseInt(b)/255, + area: area==="a"?this.FeatureManager.features["dataLoader"].class.area:area.replace(/_/g," "), options: {name: ChatLib.addColor(name.replace(/_/g," "))} } + + this.userWaypointsArr = Object.values(this.userWaypoints) + this.waypointsChanged = true ChatLib.chat(this.FeatureManager.messagePrefix + "Added waypoint "+ name + "!") }) this.registerCommand("delwaypoint", (name)=>{ delete this.userWaypoints[name] + this.userWaypointsArr = Object.values(this.userWaypoints) + this.waypointsChanged = true ChatLib.chat(this.FeatureManager.messagePrefix + "Deleted waypoint "+ name + "!") }) this.registerCommand("clearwaypoints", ()=>{ this.userWaypoints = {} + this.userWaypointsArr = [] + this.waypointsChanged = true ChatLib.chat(this.FeatureManager.messagePrefix + "Cleared waypoints!") }) this.registerCommand("savewaypoints", ()=>{ @@ -69,6 +79,9 @@ class Waypoints extends Feature { this.registerCommand("loadwaypoints", ()=>{ try{ this.userWaypoints = JSON.parse(Java.type("net.minecraft.client.gui.GuiScreen")[m.getClipboardString]()) + + this.userWaypointsArr = Object.values(this.userWaypoints) + this.waypointsChanged = true ChatLib.chat(this.FeatureManager.messagePrefix + "Loaded waypoints from clipboard!") }catch(e){ ChatLib.chat(this.FeatureManager.messagePrefix + "Error loading from clipboard!") @@ -92,9 +105,10 @@ class Waypoints extends Feature { for(let waypoint of this.waypoints){ drawCoolWaypoint(waypoint.x, waypoint.y, waypoint.z, waypoint.r, waypoint.g, waypoint.b, waypoint.options) } - let waypoints2 = Object.values(this.userWaypoints) - for(let waypoint of waypoints2){ - drawCoolWaypoint(waypoint.x, waypoint.y, waypoint.z, waypoint.r, waypoint.g, waypoint.b, {...waypoint.options}) + for(let waypoint of this.userWaypointsArr){ //TODO: Make hash from waypoint.area -> waypoints[] + if(!waypoint.area || this.FeatureManager.features["dataLoader"].class.area === waypoint.area){ + drawCoolWaypoint(waypoint.x, waypoint.y, waypoint.z, waypoint.r, waypoint.g, waypoint.b, {...waypoint.options}) + } } } @@ -103,6 +117,10 @@ class Waypoints extends Feature { } onDisable(){ + if(this.waypointsChanged){ + FileLib.write("soopyAddonsData", "soopyv2userwaypoints.json", JSON.stringify(this.userWaypoints)) + } + this.initVariables() } } diff --git a/features/waypoints/pathfind.js b/features/waypoints/pathfind.js new file mode 100644 index 0000000..42135d8 --- /dev/null +++ b/features/waypoints/pathfind.js @@ -0,0 +1,96 @@ +import FlatQueue from "../../datastructures/flatqueue" + +const directions = [ + [1,0,0,1], + [0,1,0,1], + [0,0,1,1], + [-1,0,0,1], + [0,-1,0,1], + [0,0,-1,1], + [1,1,0,Math.SQRT2], + [1,-1,0,Math.SQRT2], + [-1,1,0,Math.SQRT2], + [-1,-1,0,Math.SQRT2], + [1,0,1,Math.SQRT2], + [1,0,-1,Math.SQRT2], + [-1,0,1,Math.SQRT2], + [-1,0,-1,Math.SQRT2], + [0,1,1,Math.SQRT2], + [0,1,-1,Math.SQRT2], + [0,-1,1,Math.SQRT2], + [0,-1,-1,Math.SQRT2] +] + +class Path{ + constructor(startLocation, endLocation){ + this.startLocation = startLocation + this.endLocation = endLocation + + this.points = [] + } + + getPoints(){ + return this.points + } + + calculate(){ + this.locs = {} + this.points = [] + this.locs[this.startLocation.x+","+this.startLocation.y+","+this.startLocation.z]= { + location: this.startLocation, + parent: null, + distance: 0, + huristic: (Math.abs(this.startLocation.x-this.endLocation.x)+Math.abs(this.startLocation.y-this.endLocation.y)+Math.abs(this.startLocation.z-this.endLocation.z)) + } + + this.heap = new FlatQueue() + + this.heap.push(this.startLocation.x+","+this.startLocation.y+","+this.startLocation.z, this.locs[this.startLocation.x+","+this.startLocation.y+","+this.startLocation.z].huristic) + + let i = 0 + while(!this._iterate() && i<100000){ + i++ + } + } + + _iterate(){ + let currentKey = this.heap.pop() + let current = this.locs[currentKey] + if(current.location.x === this.endLocation.x && current.location.y === this.endLocation.y && current.location.z === this.endLocation.z){ + this.points.push([current.location.x, current.location.y, current.location.z]) + while(current.parent){ + current = this.locs[current.parent] + + this.points.push([current.location.x, current.location.y, current.location.z]) + } + return true + } + + for(let dir of directions){ + + let newLoc = { + x: current.location.x + dir[0], + y: current.location.y + dir[1], + z: current.location.z + dir[2] + } + + if(!this.locs[newLoc.x + "," + newLoc.y + "," + newLoc.z]){ + + if(World.getBlockAt(newLoc.x, newLoc.y, newLoc.z).getType().getID() === 0 && World.getBlockAt(newLoc.x, newLoc.y+1, newLoc.z).getType().getID() === 0){ + this.locs[newLoc.x + "," + newLoc.y + "," + newLoc.z] = { + location: newLoc, + parent: currentKey, + distance: current.distance+1, + huristic: Math.hypot((newLoc.x-this.endLocation.x),(newLoc.y-this.endLocation.y),(newLoc.z-this.endLocation.z))+(current.distance+dir[3])/2 + } + + this.heap.push(newLoc.x + "," + newLoc.y + "," + newLoc.z, this.locs[newLoc.x + "," + newLoc.y + "," + newLoc.z].huristic) + } + } + } + + return false + } +} + +export default Path
\ No newline at end of file |