Suppose I have a list of Grid Sectors, that have the possibility of having between 1 & 4 exits per grid sector. Each grid sector consists of 50x50 x,y positions, and there can be any number of grid sectors. The "Exits" are guaranteed to be one of the 0,N - 49,N or N,0 - N,49 positions. How can I find the shortest path from any given position in a grid sector, through to the farthest position away. The list of grid-sectors, is not in any particular order. I must traverse each room in the list, even if I have to traverse a grid sector twice, for example, grid sectors with only one exit.
In this screenshot, the Green would represent the starting position, and the Light Blue would represent the end position. Every other Grid Sector here MUST be visited at least once, starting with the closest, and ending with the farthest. Each red line represents an exit that shares a border with a nearby grid sector. Again, each is guaranteed to have at least 1 exit, and no more than 4.
I'm really only looking for an optimal Grid Sector path from each grid to the next, given the exits, however BONUS POINTS to anyone that can also return an array of X,Y coordinates in order from the 25,25 position of the first grid sector. In order to do that, recognize that the top left of each grid sector is 0,0 and the bottom right is 49,49 And each room is organized in a similar fashion, albeit at a larger scale.
I attempted to do something like this, but it got me a result that had grid sectors that were diagonally closest to farthest instead of pathed closest to farthest. Another solution I had considered was to use flood-fill to check each room, and add it only if it was connected to the next available position, but that also netted me unexpected results.
setFrontiers(room: Room) {
let frontiers: string[] = []
let currentRoomGlobalPos = Utils.Utility.roomNameToCoords(this.name)
for (let wx = currentRoomGlobalPos.wx - 10; wx <= currentRoomGlobalPos.wx + 10; wx++) {
for (let wy = currentRoomGlobalPos.wy - 10; wy <= currentRoomGlobalPos.wy + 10; wy++) {
let prospectFrontier = Utils.Utility.roomNameFromCoords(wx, wy)
let result = Game.map.describeExits(prospectFrontier)
if (result != null &&
result != this.name &&
Game.map.getRoomStatus(prospectFrontier).status == Game.map.getRoomStatus(room.name).status) {
frontiers.push(prospectFrontier)
}
}
}
frontiers = _.sortByOrder(frontiers, (roomName: string) => {
let roomGlobalPos = Utils.Utility.roomNameToCoords(roomName)
let dx = roomGlobalPos.wx - currentRoomGlobalPos.wx
let dy = roomGlobalPos.wy - currentRoomGlobalPos.wy
return Math.abs(dx) + Math.abs(dy)
}, 'asc')
frontiers.splice(0, 1)
room.memory.frontiers = frontiers
}
The results of the above code, is this, however the starting position was from the center of the red circle.

