Screeps Wiki
Advertisement

function (firstArg, secondArg, opts) {

   if (!this.my) {
     return C.ERR_NOT_OWNER;
   }
   if (data(this.id).spawning) {
     return C.ERR_BUSY;
   }
   if (data(this.id).fatigue > 0) {
     return C.ERR_TIRED;
   }
   if (this.getActiveBodyparts(C.MOVE) == 0) {
     return C.ERR_NO_BODYPART;
   }
   var $__1 = utils.fetchXYArguments(firstArg, secondArg, globals),
       x = $__1[0],
       y = $__1[1],
       roomName = $__1[2];
   roomName = roomName || this.pos.roomName;
   if (_.isUndefined(x) || _.isUndefined(y)) {
     return C.ERR_INVALID_TARGET;
   }
   if (_.isObject(firstArg)) {
     opts = secondArg;
   }
   opts = opts || {};
   if (_.isUndefined(opts.reusePath)) {
     opts.reusePath = 5;
   }
   if (x == this.pos.x && y == this.pos.y && roomName == this.pos.roomName) {
     return C.OK;
   }
   if (this.room.memory && _.isObject(this.room.memory) && this.room.memory._move) {
     delete this.room.memory._move;
   }
   if (opts.reusePath && this.memory && _.isObject(this.memory) && this.memory._move) {
     var _move = this.memory._move;
     if (runtimeData.time > _move.time + parseInt(opts.reusePath) || _move.room != this.pos.roomName) {
       delete this.memory._move;
     } else if (_move.dest.room == this.pos.roomName && _move.dest.x == x && _move.dest.y == y || _move.dest.room != this.pos.roomName && _move.dest.room == roomName) {
       var idx = _.findIndex(_move.path, {
         x: this.pos.x,
         y: this.pos.y
       });
       if (idx != -1) {
         _move.path.splice(0, idx + 1);
       }
       if (_move.path.length == 0) {
         return C.OK;
       }
       var result = this.moveByPath(_move.path);
       if (result == C.OK) {
         return C.OK;
       }
     }
   }
   if (opts.noPathFinding) {
     return C.ERR_NOT_FOUND;
   }
   var path = this.pos.findPathTo(new globals.RoomPosition(x, y, roomName), opts);
   if (opts.reusePath && this.memory && _.isObject(this.memory)) {
     this.memory._move = {
       dest: {
         x: x,
         y: y,
         room: roomName
       },
       time: runtimeData.time,
       path: _.clone(path),
       room: this.pos.roomName
     };
   }
   if (path.length == 0) {
     return C.ERR_NO_PATH;
   }
   this.move(path[0].direction);
   return C.OK;
 }
Advertisement