我知道如何获得从A到B的距离,但是在所有地形因素的情况下,很难计算出这条路径需要多长时间。有没有内置的方式来做到这一点,我错过了?这就是我所希望的:
path = creep.pos.findPathTo(target);
creep.ticksForPath(path, {energy: 150);
其中{energy: 150}
允许您强制计算使用该数量的携带能量。
这是可行的还是有计划的?
答案 0 :(得分:3)
game docs中没有这样的东西。但您可以使用findPath函数的选项avoid
参数来尝试避免使用沼泽切片。默认情况下,无论如何,它都支持最少的步骤,最快的路线。
有一种方法可以通过查找路径然后将坐标与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种类型:
类似(未经测试的,不完整的代码):
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;
}