Skip to content

Instantly share code, notes, and snippets.

@lethern
Created June 27, 2020 18:25
Show Gist options
  • Save lethern/d7b851ccf88ee60f6e4934c1954ac293 to your computer and use it in GitHub Desktop.
Save lethern/d7b851ccf88ee60f6e4934c1954ac293 to your computer and use it in GitHub Desktop.
const PLAIN_COST = 3;
const SWAMP_COST = 6;
const WALL_COST = 30 * PLAIN_COST;
const EXISTING_PATH_COST = PLAIN_COST - 2;
function getDestinations(room) {
let destinations = room.find(FIND_SOURCES);
destinations.push({ pos: room.getPositionAt(1, 25) });
return destinations;
}
/** @param {Room} room */
function pathfinding(room) {
let terrain = room.getTerrain();
//terrain.
const matrix = new PathFinder.CostMatrix();
//const terrain = Game.map.getRoomTerrain(room.name);
for (let y = 0; y < 50; ++y) {
for (let x = 0; x < 50; ++x) {
switch (terrain.get(x, y)) {
case TERRAIN_MASK_SWAMP:
matrix.set(x, y, SWAMP_COST);
break;
case TERRAIN_MASK_WALL:
if (x != 0 && y != 0 && x != 49 && y != 49) {
// Can't tunnel through walls on edge tiles
matrix.set(x, y, WALL_COST);
}
break;
default: // plain
matrix.set(x, y, PLAIN_COST);
break;
}
}
}
const callback = (roomName) => {
if (roomName == 'sim') { // only route through colony rooms
return matrix;
}
};
let start = Game.spawns.Spawn1.pos;
let destinations = getDestinations(room);
let goals = destinations.map(dest => ({ pos: dest.pos, range: 1 }));
goals.forEach((goal) => {
const ret = PathFinder.search(start, goal, { roomCallback: callback, maxOps: 40000 });
for (const i of _.range(ret.path.length)) {
const pos = ret.path[i];
//if (i % 2 == 0 && !pos.isEdge) {
matrix.set(pos.x, pos.y, EXISTING_PATH_COST);
//}
}
});
let map = {};
let length = 0;
goals.forEach((goal) => {
const ret = PathFinder.search(start, goal, { roomCallback: callback, maxOps: 40000 });
ret.path.forEach((pos) => {
if (map[pos.x + 'x' + pos.y]) return;
room.visual.circle(pos, { radius: 0.07, fill: '#ff0044', opacity: 1 });
length++;
map[pos.x + 'x' + pos.y] = 1;
})
});
console.log('trivial = ' + length);
};
module.exports.loop = function () {
let room = Game.rooms.sim;
pathfinding(room);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment