Screeps:计算身体的构建成本

时间:2014-11-24 09:01:14

标签: javascript screeps

在玩游戏screeps.com时,我想计算构建所需身体的成本。请参阅下面的我的尝试,其中cfg.body是bodypart数组,例如[Game.ATTACK, Game.MOVE, Game.WORK, Game.CARRY, Game.MOVE]

var buildCost = 0;

for(var bodypart in cfg.body){
  switch(bodypart){
    case "MOVE":
    case "CARRY":
       buildCost+=50;
       break;
    case "WORK":
       buildCost+=20;
       break;
    case "HEAL":
       buildCost+=200;
       break;
    case "TOUGH":
       buildCost+=20;
       break;
    case "ATTACK":
       buildCost+=80;
       break;
    case "RANGED_ATTACK":
       buildCost+=150;
       break;
  }
  console.log(bodypart + " costs " + buildCost);
}

bodypart打印到控制台时,它会显示索引(0,1,2,3,...),而buildCost仍为0。

费用和零件的描述为on the Screeps page

6 个答案:

答案 0 :(得分:18)

也许以前BODYPART_COST常量不可用,但是你可以这样做:

function bodyCost (body) {
    return body.reduce(function (cost, part) {
        return cost + BODYPART_COST[part];
    }, 0);
}

答案 1 :(得分:6)

更像JavaScripty的解决方案,无需切换,可能如下所示:

var _ = require("lodash");
var body = [Game.WORK, Game.CARRY, Game.MOVE, Game.TOUGH];
var bodyCost = {
  "move": 50,
  "carry": 50,
  "work": 20,
  "heal": 200,
  "tough": 20,
  "attack": 80,
  "ranged_attack": 150
};
var cost = 0;
_.forEach(body, function(part) { cost += bodyCost[part]; });
console.log("Cost of construction: " + cost);

事实上,我会将这件事用于我自己的蠕动来计算成本。感谢您找出各种费用:)

答案 2 :(得分:4)

我使用@Brett的常量更新了我的解决方案。但是我将for in循环更改为for循环,因为此StackOverflow question

中显示了性能
for (var index = 0; index < cfg.body.length; ++index) {
  var bodypart = cfg.body[index];
    switch(bodypart){
      case MOVE:
      case CARRY:
        buildCost += 50;
        break;
      case WORK:
        buildCost += 100;
        break;
      case ATTACK:
        buildCost += 80;
        break;
      case RANGED_ATTACK:
        buildCost += 150;
        break;
      case HEAL:
        buildCost += 250;
        break;
      case TOUGH:
        buildCost += 10;
        break;
      case CLAIM:
        buildCost += 600;
        break;
    }
  console.log(bodypart.toUpperCase()+" costs "+buildCost);
}

我还要注意每次构建蠕变时都不应该使用此循环更好地硬编码值。但是,如果这种值不存在或需要仔细检查,它可能会很方便。

答案 3 :(得分:4)

使用lodash的总和方法,它更容易:

let cost = _.sum(creep.body.map(function (b) {
               return BODYPART_COST[b];
           }));

箭头方法使它更优雅:

let cost = _.sum(creep.body.map((b) => BODYPART_COST[b]));

答案 4 :(得分:2)

我很确定你应该比较小写字母而不是大写字母。如果做不到这一点,请尝试使用Game.MOVE,Game.WORK等等。

答案 5 :(得分:2)

我更新了您的脚本以执行您想要的操作。

for(在cfg.body中的车身部分)中的身体部位只是一个索引

var body = [Game.ATTACK,Game.MOVE,Game.WORK,Game.CARRY,Game.MOVE];
var buildCost = 0;
for(var bodypart in body){
    var bodymodule = body[bodypart];
    switch(bodymodule){
        case Game.MOVE:
        case Game.CARRY:
           buildCost+=50;
           break;
        case Game.WORK:
           buildCost+=20;
           break;
        case Game.HEAL:
           buildCost+=200;
           break;
        case Game.TOUGH:
           buildCost+=5;
           break;
        case Game.ATTACK:
           buildCost+=100;
           break;
        case Game.RANGED_ATTACK:
           buildCost+=150;
           break;
    }
}
console.log(body+" costs "+buildCost);