Skip to content

Instantly share code, notes, and snippets.

@socantre
Created March 24, 2017 00:24
Show Gist options
  • Save socantre/96e77685bc5164f4b510c1be752236b6 to your computer and use it in GitHub Desktop.
Save socantre/96e77685bc5164f4b510c1be752236b6 to your computer and use it in GitHub Desktop.
screeps room distance transform
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);
}
@shixish
Copy link

shixish commented Jan 2, 2022

This is really clever. I was scratching my head on how to do this. Super cool! Thanks

@mkaulfers
Copy link

@shixish here's an updated conditional that resolves the deprecation.

Game.map.getRoomTerrain(roomName).get(x, y) == TERRAIN_MASK_WALL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment