在游戏中,我试图从另一个文件中调用一个变量。 这是我的主要档案:
lakeData = load('C:\Users\Justin\Desktop\EGR2222\MATLAB\Data\LAKE_POWELL.txt');
year1= lakeData(:,1);
year2= lakeData (:,2);
year3= lakeData (:, 3);
year4= lakeData (:, 4);
year5= lakeData (:, 5);
year6= lakeData (:,6);
year7= lakeData (:,7);
year8= lakeData (:,8);
lakePowell = [year1, year2, year3, year4, year5, year6, year7, year8];
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007];
average = mean(lakePowell)
fprintf('The average water level was %s in %s\n', average, years)
现在从我的role.builder文件中,我尝试编写一个IF-Else语句,该语句必须检查来自main的变量:
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var roleVoyager = require('role.voyager');
var roleAttacker = require('role.attacker');
var roleTransporter = require('role.transporter');
var roleHealer = require('role.healer');
var roleRepairer = require('role.repairer');
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');
console.log('Harvesters: ' + harvesters.length);
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
console.log('Upgraders: ' + upgraders.length);
var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
console.log('Builders: ' + builders.length);
var voyagers = _.filter(Game.creeps, (creep) => creep.memory.role == 'voyager');
console.log('Voyagers: ' + voyagers.length);
var attackers = _.filter(Game.creeps, (creep) => creep.memory.role == 'attacker');
console.log('Attackers: ' + attackers.length);
var transporters = _.filter(Game.creeps, (creep) => creep.memory.role == 'transporter');
console.log('Transporters: ' + transporters.length);
var healers = _.filter(Game.creeps, (creep) => creep.memory.role == 'healer');
console.log('Healers: ' + healers.length);
var repairers = _.filter(Game.creeps, (creep) => creep.memory.role == 'repairer');
console.log('Repairers: ' + repairers.length);
if(harvesters.length < 3) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE], creepName = Creep.getRandomName("[Harvester]"), {role: 'harvester'});
console.log('Spawning new harvester: ' + newName);
}
if(upgraders.length < 3 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,CARRY,CARRY,MOVE,MOVE], creepName = Creep.getRandomName("[Upgrader]"), {role: 'upgrader'});
console.log('Spawning new upgrader: ' + newName);
}
if(builders.length < 2 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,CARRY,MOVE,MOVE], creepName = Creep.getRandomName("[Builder]"), {role: 'builder'});
console.log('Spawning new builder: ' + newName);
}
if(voyagers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([MOVE], creepName = Creep.getRandomName("[Voyager]"), {role: 'voyager'});
console.log('Spawning new voyager: ' + newName);
}
if(attackers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([ATTACK,ATTACK,MOVE], creepName = Creep.getRandomName("[Attacker]"), {role: 'attacker'});
console.log('Spawning new Attacker: ' + newName);
}
if(transporters.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,MOVE], creepName = Creep.getRandomName("[Transporter]"), {role: 'transporter'});
console.log('Spawning new Transporter: ' + newName);
}
if(healers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([HEAL,MOVE,MOVE], creepName = Creep.getRandomName("[Healer]"), {role: 'healer'});
console.log('Spawning new Healer: ' + newName);
}
if(repairers.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([CARRY,CARRY,WORK,MOVE,MOVE], creepName = Creep.getRandomName("[Repairer]"), {role: 'repairer'});
console.log('Spawning new Repairer: ' + newName);
}
for(var name in Game.creeps) {
var creep = Game.creeps[name];
if(creep.memory.role == 'harvester') { roleHarvester.run(creep); }
if(creep.memory.role == 'upgrader') { roleUpgrader.run(creep); }
if(creep.memory.role == 'builder') { roleBuilder.run(creep); }
if(creep.memory.role == 'voyager') { roleVoyager.run(creep); }
if(creep.memory.role == 'attacker') { roleAttacker.run(creep); }
if(creep.memory.role == 'transporter') { roleTransporter.run(creep); }
if(creep.memory.role == 'healer') { roleHealer.run(creep); }
if(creep.memory.role == 'repairer') { roleRepairer.run(creep); }
}
}
但我得到错误&#39;主要操作系统没有定义&#39; 我怎么称呼te主文件呢?
答案 0 :(得分:2)
您尝试做的事情非常低效:您多次迭代Game.creeps
集合(我至少计算了8次)。如果你想计算角色中的小兵,我建议你使用
var groups = _.groupBy(Game.creeps, (c) => { return c.memory.role; });
这将返回一个对象,每个角色都作为一个键,在键后面是该特定角色中所有creep的集合:
{
harvester: [Creep, Creep, Creep],
builder: [Creep],
upgrader: [Creep, Creep, Creep]
}
我想你会希望在每个房间而不是全球范围内获得这些信息,因为低级别房间的行为应该与高级别房间不同(从某种意义上说,更高级别的房间可能有更少的小房间)。 p>
不知道Game对象的生命周期,但是如果你在creep更新例程的开头循环Game.rooms
,如下所示,你可以在需要时立即获得这些信息,只需要循环它一旦:
for (var name in Game.rooms) {
var room = Game.rooms[name];
room.roles = _.groupBy(room.find(FIND_MY_CREEPS), (c) => { return c.memory.role; });
}
答案 1 :(得分:0)
刚才发现我自己。 如果我做了一些谎言:
var count = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
我可以使用'count.length'var:)
轻松制作一个if-else