有没有办法计算 creep 从 A 到 B 所需的滴答数?

Is there a way to calculate the number of ticks it will take for a creep to get from A to B?

我知道如何计算从 A 到 B 的距离,但是考虑到所有地形因素,很难计算 creep 走完那条路需要多长时间。有没有我缺少的内置方法?这就是我的愿望:

path = creep.pos.findPathTo(target);
creep.ticksForPath(path, {energy: 150);

其中 {energy: 150} 允许您强制计算使用该携带能量。

这是可能的还是计划中的?

game docs. But you could use the options avoid argument of the findPath 函数中没有这样的东西可以用来尝试避免沼泽地块。默认情况下,它支持最少的步数,无论如何我相信最快的路线。

有一种方法可以通过找到路径然后将坐标与 lookAtArea() 调用进行比较来计算成本。

var path = creep.room.findPath(creep, target);

path returns:

[
    { x: 10, y: 5, dx: 1,  dy: 0, direction: Game.RIGHT },
    { x: 10, y: 6, dx: 0,  dy: 1, direction: Game.BOTTOM },
    { x: 9,  y: 7, dx: -1, dy: 1, direction: Game.BOTTOM_LEFT },
    ...
]

然后您可以使用 lookAtArea():

查询 top/left 和 bottom/right 区域
var look = creep.room.lookAtArea(10,5,11,7);

look returns:

// 10,5,11,7
{
    10: {
        5: [{ type: ‘creep’, creep: {...} },
            { type: ‘terrain’, terrain: ‘swamp’ }],
        6: [{ type: ‘terrain’, terrain: ‘swamp’ }],
        7: [{ type: ‘terrain’, terrain: ‘swamp’ }]
    },
    11: {
        5: [{ type: ‘terrain’, terrain: ‘normal’ }],
        6: [{ type: ‘spawn’, spawn: {...} },
            { type: ‘terrain’, terrain: ‘swamp’ }],
        7: [{ type: ‘terrain’, terrain: ‘wall’ }]
    }
}

然后循环查找您拥有的每个路径步骤并检查地形类型。

我们需要的3种类型:

  1. 平地——移动成本为 2 的简单地面
  2. 沼泽将移动成本增加到 10。
  3. 道路将移动成本降低到 1。

类似于(未经测试、不完整的代码):

function terrainSpeed(terrainType) {
  var speed = 0;
  switch(terrainType) {
    case 'swamp':
      speed = 10;
      break;
    case 'normal':
      speed = 2;
      break;
    case 'road':
      speed = 1;
      break;
  }
  return speed;
}

// Traverse through path and check how many thicks it would take 
// based on carried energy and body
function calculateTicksRequired(path,look,energy,body) {
  var ticksRequired = 0;
  for (var i = 0; i < path.length; i++) {
    var terrainForStep = look[path[i].y][path[i].x].terrain];
    var defaultTicks = terrainSpeed(terrainForStep);
    var moveCost = 1;
    // Perform calculation by looking at bodymove cost (based on body) 
    // and carry cost (based on energy)

    // LOGIC TO CALCULATE MOVECOST
    // ...

    // Multiply that by defaultTicks
    ticksRequired += defaultTicks * moveCost;
  }
  return ticksRequired;
}