Screeps Wiki
Advertisement

function (fromPos, toPos, opts) {

   if (fromPos.roomName != this.name) {
     return [];
   }
   var fromX = fromPos.x,
       fromY = fromPos.y,
       path,
       cacheKeySuffix = ;
   opts = _.clone(opts || {});
   if (opts.ignoreCreeps) {
     cacheKeySuffix += '_ignoreCreeps';
   }
   if (opts.ignoreDestructibleStructures) {
     cacheKeySuffix += '_ignoreDestructibleStructures';
   }
   if (opts.withinRampartsOnly) {
     cacheKeySuffix += '_withinRampartsOnly';
   }
   if (opts.avoid) {
     cacheKeySuffix += '_avoid' + privateStore[this.name].positionsSetCache.key(opts.avoid);
   }
   if (opts.ignore) {
     cacheKeySuffix += '_ignore' + privateStore[this.name].positionsSetCache.key(opts.ignore);
   }
   if (_.isNumber(toPos)) {
     if (!privateStore[this.name].pfEndNodes[toPos]) {
       return [];
     }
     var grid = getPathfindingGrid(this.name, opts, toPos);
     path = privateStore[this.name].pfDijkstraFinder.findPath(fromX, fromY, -999, -999, grid);
   } else {
     if (toPos.roomName != this.name) {
       return [];
     }
     var toX = toPos.x,
         toY = toPos.y,
         cacheKey = (fromX + "," + fromY + "," + toX + "," + toY + cacheKeySuffix);
     if (privateStore[this.name].pathCache[cacheKey]) {
       return _.cloneDeep(privateStore[this.name].pathCache[cacheKey]);
     }
     if (fromX == toX && fromY == toY)
       return [];
     if (fromX < 0 || fromY < 0 || toX < 0 || toY < 0 || fromX >= 50 || fromY >= 50 || toX >= 50 || toY >= 50)
       return [];
     if (abs(fromX - toX) < 2 && abs(fromY - toY) < 2) {
       return [{
         x: toX,
         y: toY,
         dx: toX - fromX,
         dy: toY - fromY,
         direction: utils.getDirection(toX - fromX, toY - fromY)
       }];
     }
     var grid = getPathfindingGrid(this.name, opts),
         finder = getPathfinder(this.name, opts);
     grid.setWalkableAt(toX, toY, true);
     path = finder.findPath(fromX, fromY, toX, toY, grid);
   }
   path.splice(0, 1);
   var curX = fromX,
       curY = fromY;
   var resultPath = _.map(path, (function(step) {
     var result = {
       x: step[0],
       y: step[1],
       dx: step[0] - curX,
       dy: step[1] - curY,
       direction: utils.getDirection(step[0] - curX, step[1] - curY)
     };
     curX = result.x;
     curY = result.y;
     return result;
   }));
   if (resultPath.length > 0) {
     var lastStep = resultPath[resultPath.length - 1],
         cacheKey = (fromX + "," + fromY + "," + lastStep.x + "," + lastStep.y + cacheKeySuffix);
     privateStore[this.name].pathCache[cacheKey] = _.cloneDeep(resultPath);
   }
   return resultPath;
 }

Advertisement