4

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

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

where{energy: 150}允许你强制计算使用携带的能量。

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

4

1 回答 1

4

游戏文档中没有这样的东西。但是您可以使用findPathavoid函数的 options参数来尝试避免使用沼泽瓷砖。默认情况下,我相信它倾向于最少的步骤,最快的路线。

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

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

path返回:

[
    { 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()查询顶部/左侧和底部/右侧区域:

var look = creep.room.lookAtArea(10,5,11,7);

look返回:

// 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;
}
于 2015-01-18T10:39:57.750 回答