Created
March 24, 2017 00:24
-
-
Save socantre/96e77685bc5164f4b510c1be752236b6 to your computer and use it in GitHub Desktop.
screeps room distance transform
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 distanceTransform(roomName) { | |
let vis = new RoomVisual(roomName); | |
let topDownPass = new PathFinder.CostMatrix(); | |
for (let y = 0; y < 50; ++y) { | |
for (let x = 0; x < 50; ++x) { | |
if (Game.map.getTerrainAt(x, y, roomName) == 'wall') { | |
topDownPass.set(x, y, 0); | |
} | |
else { | |
topDownPass.set(x, y, | |
Math.min(topDownPass.get(x-1, y-1), topDownPass.get(x, y-1), | |
topDownPass.get(x+1, y-1), topDownPass.get(x-1, y)) + 1); | |
} | |
} | |
} | |
for (let y = 49; y >= 0; --y) { | |
for (let x = 49; x >= 0; --x) { | |
let value = Math.min(topDownPass.get(x, y), | |
topDownPass.get(x+1, y+1) + 1, topDownPass.get(x, y+1) + 1, | |
topDownPass.get(x-1, y+1) + 1, topDownPass.get(x+1, y) + 1); | |
topDownPass.set(x, y, value); | |
vis.circle(x, y, {radius:value/25}); | |
} | |
} | |
return topDownPass; | |
} | |
module.exports.loop = function() { | |
let start = Game.cpu.getUsed(); | |
distanceTransform('sim'); | |
let end = Game.cpu.getUsed() | |
console.log("cpu:", end - start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@shixish here's an updated conditional that resolves the deprecation.
Game.map.getRoomTerrain(roomName).get(x, y) == TERRAIN_MASK_WALL