I know how to get the distance from A to B, but with all the terrain factors, its very difficult to calculate how long it will take a creep to travel that path. Is there a built-in way of doing this that I'm missing? Here's what I'm wishing for:
path = creep.pos.findPathTo(target);
creep.ticksForPath(path, {energy: 150);
where {energy: 150} allows you to force the calculation to use that amount of carried energy.
Is this possible or planned?
There is no such thing exactly available in the game docs. But you could use the options avoid argument of the findPath function, to try and avoid swamp tiles. By default it favours least steps, fastest route anyway I believe.
There is a way to calculate the cost by finding a path and then comparing coordinates with a lookAtArea() call.
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 },
...
]
You can then query top/left and bottom/right area with lookAtArea():
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’ }]
}
}
And then loop through the look for each path step you have and check on terrain type.
The 3 types we need:
Something like (untested, incomplete code):
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With