【问题标题】:Is there a way to calculate the number of ticks it will take for a creep to get from A to B?有没有办法计算小兵从 A 到 B 所需的滴答数?
【发布时间】:2015-03-16 06:54:48
【问题描述】:

我知道如何计算从 A 到 B 的距离,但考虑到所有地形因素,很难计算小兵要走多长时间才能走这条路。有没有我想念的内置方法?这是我的愿望:

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

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

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

【问题讨论】:

    标签: screeps


    【解决方案1】:

    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种类型:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-03
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多