gpt4 book ai didi

javascript - 寻找能量最高的结构

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:29:20 25 4
gpt4 key购买 nike

所以我有一个名为 storer 的 creep Angular 色,它应该从容器中获取能量并将其带到存储中。然而,目前,它会找到距离路径最近且能量水平大于某个阈值的容器,这样每次矿工重新填充容器时它都不会在那里等待数小时。

我遇到的问题是,如果我降低阈值,storer 将来回运行到相同的容器,忽略房间内更远的任何容器并让它们填满。
而提高阈值会让他在外面等太久,没有足够的时间清空容器,因此仓库几乎一直都是空的。

我需要一种方法让 creep 确定能量最高的容器并从那里填满。

这是它运行的代码:

if ((source = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] >= 150)}})) != undefined) {
if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}

编辑:这是我试过的代码,但我觉得它使用了太多的 CPU 能力,可以用更好的方式完成:

for (let i = 2000; i>=0; i=i-100) {
source = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] >= i}});
if (source != undefined) {
break;
}
}
if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}

最佳答案

您可以做的是遍历所有容器一次,了解它们的能量水平并选择最高的一个。在 creep 工作时设置一个值,这样当 creep 变得更高时就不会移动到另一个容器。

这是我为 storer Angular 色编写的代码:

module.exports = {

run: function( creep ) {

// Setting the working variable so the creep focus
// on getting the ressource or returning it.
if ( creep.memory.working && creep.carry.energy == 0 ) {
creep.memory.working = false;
}

if ( ! creep.memory.working && creep.carry.energy == creep.carryCapacity ) {
creep.memory.working = true;
creep.memory.targetContainer = false;
}

if ( creep.memory.working ) {

// Bring the ressources to the storage.
var theStorage = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_STORAGE );
}
});

if ( creep.transfer( theStorage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo( theStorage );
}

} else {

// If the creep have a target.
if ( creep.memory.targetContainer ) {

// Go to the container.
var theContainer = Game.getObjectById( creep.memory.targetContainer );

if ( creep.withdraw( theContainer, RESOURCE_ENERGY ) == ERR_NOT_IN_RANGE ) {
creep.moveTo( theContainer );
}

} else {

// Find the container with the most energy.
var target = creep.room.find( FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER );
}
});

if ( target.length ) {

var allContainer = [];

// Calculate the percentage of energy in each container.
for ( var i = 0; i < target.length; i++ ) {

allContainer.push( { energyPercent: ( ( target[i].store.energy / target[i].storeCapacity ) * 100 ), id: target[i].id } );

}

// Get the container containing the most energy.
var highestContainer = _.max( allContainer, function( container ){ return container.energyPercent; });

console.log( 'Going for the container id "' + highestContainer.id + '" at ' + highestContainer.energyPercent + '% full.' );

// set the target in memory so the creep dosen't
// change target in the middle of the room.
creep.memory.targetContainer = highestContainer.id;

}
}
}
}
};

关于javascript - 寻找能量最高的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38350499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com