Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

screeps

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?

like image 407
muirbot Avatar asked Nov 06 '25 16:11

muirbot


1 Answers

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:

  1. Plain land – simple ground with a movement cost of 2
  2. Swamps increase a movement cost to 10.
  3. Roads decrease a movement cost to 1.

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;
}
like image 164
Anima-t3d Avatar answered Nov 09 '25 06:11

Anima-t3d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!