每个生成的 creep 都具有相同的角色且无法更改
Every spawning creep with same role and unable to change
我是这个游戏的新手。
我按照教程尝试自动生成一些 creep,但每个 creep 都以相同的角色生成,当我更改内存时,在下一个 tick 中它会变回 'upgrader'.
我的主脚本
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);
}
}
}
我只粘贴升级角色,但我可以用其他角色更新它。
role.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');
我是这个游戏的新手。 我按照教程尝试自动生成一些 creep,但每个 creep 都以相同的角色生成,当我更改内存时,在下一个 tick 中它会变回 'upgrader'.
我的主脚本
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);
}
}
}
我只粘贴升级角色,但我可以用其他角色更新它。
role.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');