Created
March 10, 2016 22:39
-
-
Save laverdet/25a91b9d5e07490d84a6 to your computer and use it in GitHub Desktop.
Find a very long path in Screeps
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
function longPath(from, to) { | |
let rooms = {}; | |
Game.map.findRoute(from.roomName, to.roomName).forEach(function(step, ii, route) { | |
function generateCostMatrix(dir1, dir2) { | |
let cm = new PathFinder.CostMatrix; | |
let dir = 1 << dir1 | 1 << dir2; | |
for (let ii = 0; ii < 49; ++ii) { | |
if (!(dir & 1 << TOP)) { | |
cm.set(ii, 0, 0xff); | |
} | |
if (!(dir & 1 << RIGHT)) { | |
cm.set(49, ii, 0xff); | |
} | |
if (!(dir & 1 << BOTTOM)) { | |
cm.set(ii, 49, 0xff); | |
} | |
if (!(dir & 1 << LEFT)) { | |
cm.set(0, ii, 0xff); | |
} | |
} | |
return cm; | |
} | |
if (ii === 0) { | |
rooms[from.roomName] = generateCostMatrix(step.exit, 0); | |
} | |
rooms[step.room] = generateCostMatrix((step.exit + 3) % 8 + 1, route[ii + 1] ? route[ii + 1].exit : 0); | |
}); | |
return PathFinder.search( | |
from, to, | |
{ | |
plainCost: 1, | |
swampCost: 5, | |
heuristicWeight: 1.2, | |
maxOps: 8000, | |
roomCallback: function(roomName) { | |
return rooms[roomName]; | |
}, | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment