gpt4 book ai didi

javascript - 每个生成的小兵都具有相同的 Angular 色并且无法改变

转载 作者:行者123 更新时间:2023-12-04 09:46:15 24 4
gpt4 key购买 nike

我对这个游戏很陌生。
我按照教程尝试自动生成一些小兵,但是每个小兵都以相同的 Angular 色生成,当我更改内存时,在下一个刻度中它会变回“升级者”。

我的主要脚本

var _ = require('lodash');

var roleAttacker = require('role.attacker');
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');

module.exports.loop = function () {

for(var name in Memory.creeps) {
if(!Game.creeps[name]) {
delete Memory.creeps[name];
console.log('Clearing non-existing creep memory:', name);
}
}


var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role = 'upgrader');

if(harvesters.length > 2) {
var newName = 'Harvester' + Game.time;
console.log('Spawning: ' + newName)
Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
{memory: {role: 'harvester'}});
}

if(upgraders.length < 1 && Game.spawns.Spawn1.energy >= 200 && harvesters.length == 2) {
var newName = 'Upgrader' + Game.time;
console.log('Spawning: ' + newName)
Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
{memory: {role: 'upgrader'}});
}

if(builders.length < 1 && Game.spawns.Spawn1.energy >= 200 && harvesters.length == 2 && upgraders.length == 1) {
var newName = 'Builder' + Game.time;
console.log('Spawning: ' + newName)
Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
{memory: {role: 'builder'}});
}

if(Game.spawns['Spawn1'].spawning) {
var spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name];
Game.spawns['Spawn1'].room.visual.text(
'🛠️' + spawningCreep.memory.role,
Game.spawns['Spawn1'].pos.x + 1,
Game.spawns['Spawn1'].pos.y,
{align: 'left', opacity: 0.8});
}

var tower = Game.getObjectById('TOWER_ID');
if(tower) {
var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax
});
if(closestDamagedStructure) {
tower.repair(closestDamagedStructure);
}

var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
if(closestHostile) {
tower.attack(closestHostile);
}
}

for(var name in Game.creeps) {
var creep = Game.creeps[name];
switch(creep.memory.role) {
case 'harvester':
roleHarvester.run(creep);
break;
case 'upgrader':
roleUpgrader.run(creep);
break;
case 'builder':
roleBuilder.run(creep);
break;
case 'attacker':
roleAttacker.run(creep);
break;
default:
roleHarvester.run(creep);
}
}
}

我只粘贴升级 Angular 色,但我可以用其他 Angular 色更新它。

Angular 色.upgrader.js

var roleUpgrader = {

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

if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.upgrading = false;
creep.say('🔄 harvest');
}
if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) {
creep.memory.upgrading = true;
creep.say('⚡ upgrade');
}

if(creep.memory.upgrading) {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
else {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
}
};
module.exports = roleUpgrader;

先感谢您。

最佳答案

刚刚发现问题,忘记了=当我过滤升级程序时:

var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader'); 

关于javascript - 每个生成的小兵都具有相同的 Angular 色并且无法改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62093769/

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