修理工不断提取能量

时间:2017-11-09 19:51:58

标签: javascript screeps

我开始玩Screeps>昨天,我试图制作自己的“修理工脚本”!但我失败了,我不得不使用互联网上的一个脚本.. 问题是,当维修人员没有工作要做时,即使他们已经满员,他们也会留在能源提取旁边。我试图修改代码,但我不确定它是否会继续工作.. 所以我会在这里粘贴代码:

var roleRepairer = {

    /** @param {Creep} creep **/
    run: function(creep) {

        if (creep.memory.working == true && creep.carry.energy == 0) {
            // switch state
            creep.memory.working = false;
        }
        // if creep is harvesting energy but is full
        else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
            // switch state
            creep.memory.working = true;
        }

        // if creep is supposed to repair something
        if (creep.memory.working == true) {
            // find closest structure with less than max hits
            // Exclude walls because they have way too many max hits and would keep
            // our repairers busy forever. We have to find a solution for that later.
            var structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {

                filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
            });

            // if we find one
            if (structure != undefined) {
                // try to repair it, if it is out of range
                if (creep.repair(structure) == ERR_NOT_IN_RANGE) {
                    // move towards it
                    creep.moveTo(structure);
                }
            }
            // if we can't fine one
            else {
                // look for construction sites
                roleBuilder.run(creep);
            }
        }
        // if creep is supposed to harvest energy from source
        else {
            // find closest source
            var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
            // try to harvest energy, if the source is not in range
            if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
                // move towards the source
                creep.moveTo(source);
            }

        } 
    }
};

module.exports = roleRepairer;

我希望你能帮助我更多地了解这个精彩的游戏! 很多人都说了!

1 个答案:

答案 0 :(得分:1)

根据您的评论,听起来您想要替换调用roleBuilder.run函数的行。在下面的示例中,12和42是一些空白区域的x和y cooridinates。或者,您可以放置​​一个标志并让它们移动到它。如果你在多个房间里爬行,你可能需要重新设计这个解决方案。

var roleRepairer = {

    /** @param {Creep} creep **/
    run: function(creep) {

        if (creep.memory.working == true && creep.carry.energy == 0) {
            // switch state
            creep.memory.working = false;
        }
        // if creep is harvesting energy but is full
        else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
            // switch state
            creep.memory.working = true;
        }

        // if creep is supposed to repair something
        if (creep.memory.working == true) {
            // find closest structure with less than max hits
            // Exclude walls because they have way too many max hits and would keep
            // our repairers busy forever. We have to find a solution for that later.
            var structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {

                filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
            });

            // if we find one
            if (structure != undefined) {
                // try to repair it, if it is out of range
                if (creep.repair(structure) == ERR_NOT_IN_RANGE) {
                    // move towards it
                    creep.moveTo(structure);
                }
            }
            // if we can't fine one
            else {
                // look for construction sites
                //roleBuilder.run(creep);
                creep.moveTo(12, 42); //!!!HERE!!!
            }
        }
        // if creep is supposed to harvest energy from source
        else {
            // find closest source
            var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
            // try to harvest energy, if the source is not in range
            if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
                // move towards the source
                creep.moveTo(source);
            }

        } 
    }
};

module.exports = roleRepairer;