const init = require('init') const harvester = require('harvester'); const picker = require('picker'); const util = require('utils'); const transfer = require('transfer'); module.exports.loop = function () { init.init(); // if (Game.time % 20 == 0) { assign_creep_to_enery_source();//由于循环较多,每20 tick执行一次 // } Creep.prototype.action = action; if ((_(Game.creeps).filter({ memory: { role: 'harvester' } }).size() + _(Game.creeps).filter({ memory: { role: 'picker' } }).size()) < 2) { createHarvestCreep(); } else { if (_(Game.creeps).filter({ memory: { role: 'transfer' } }).size() < 2) { createTransferCreep(); } } chanage_role(); assign_task(); } function action() { let role = this.memory.role; // this.say(`${role}`); switch (role) { case "harvester": harvester.run(this) break; case "picker": picker.run(this) break; case "transfer": transfer.run(this) break; } } function assign_task() { for (const creep_id in Game.creeps) { let creep = Game.creeps[creep_id]; creep.action(); } } function assign_creep_to_enery_source() { //清理Energy中已经没有的Source if (Memory.Source.Energy) { _(Memory.Source.Energy).remove(function (r) { return Game.getObjectById(r['id']) === undefined; }); } let sources = Game.spawns['Spawn1'].room.find(FIND_SOURCES); for (let source of sources) { let id = source.id if (_(Memory.Source.Energy).findIndex(f => f['id'] == id) == -1) { Memory.Source.Energy.push({ 'id': id, 'working_creep_names': [] }); } } let harvester_creeps = _(Game.creeps).filter(function (r) { return r.memory.role == 'harvester' | r.memory.role == 'picker'; }).value(); // 从Memory.Source.Energy中移除已经没有的creep for (let mem_source of Memory.Source.Energy) { _(mem_source['working_creep_names']).each(function (creep_name) { if (Game.creeps[creep_name] === undefined) { console.log(`deleted ${creep_name}`); return true; } return false; }) _(mem_source['working_creep_names']).remove(creep_name => Game.creeps[creep_name] === undefined) // let new_working_creeep_name_array=[]; // new_working_creeep_name_array.push(...mem_source['working_creep_names']) // for(let creep_name of mem_source['working_creep_names']){ // if(Game.creeps[creep_name]===undefined){ // delete new_working_creeep_name_array[creep_name] // console.log(`deleted ${creep_name}`); // } // } // new_working_creeep_name_array=_(new_working_creeep_name_array).filter(r=>r!=undefined).value(); // mem_source['working_creep_names'].splice(0,mem_source['working_creep_names'].length); // mem_source['working_creep_names'].push(...new_working_creeep_name_array); // console.log(`Now it's ${mem_source['working_creep_names']}`); } let unassigned_creeps_names = []; for (let creep of harvester_creeps) {//统计哪些没有工作 // let assigned = false; if (creep.working_target_id === undefined) { unassigned_creeps_names.push(creep.name); } } //开始安排 for (let unassigned_creep_name of unassigned_creeps_names) { //能源矿已有creep的数量 let energy_working_creep_n = []; for (let mem_source of Memory.Source.Energy) { energy_working_creep_n.push(mem_source['working_creep_names'].length); } let energy_index = _(energy_working_creep_n).indexOf(_(energy_working_creep_n).min()); Memory.Source.Energy[energy_index]['working_creep_names'].push(unassigned_creep_name); Game.creeps[unassigned_creep_name].working_target_id = Memory.Source.Energy[energy_index]['id']; } } function chanage_role() { if (_(Game.creeps).filter({ memory: { role: 'transfer' } }).size() > 0) { let harvester_array = _(Game.creeps).filter({ memory: { role: 'harvester' } }).value(); // console.log(harvester_array[0].memory.role); for (let harvester_creep of harvester_array) { harvester_creep.memory.role = 'picker'; } } else { let picker_array = _(Game.creeps).filter({ memory: { role: 'picker' } }).value(); // console.log(harvester_array[0].memory.role); for (let picker_creep of picker_array) { picker_creep.memory.role = 'harvester'; } } } function get_creep_with_name(inlcude_string) { let creeps = []; for (const creep_id in Game.creeps) { let creep = Game.creeps[creep_id]; if (creep.name.include(inlcude_string)) { creeps.push(creep); } } return creeps; } function count(hash_container) { return Object.keys(hash_container).length } function createHarvestCreep() { //create new creep let new_creep_name = "AUTO_CREATE_Harvester_" + util.uuid(); let Re_code; Re_code = Game.spawns['Spawn1'].createCreep([MOVE, CARRY, WORK], new_creep_name) if (Re_code == new_creep_name) { Game.creeps[new_creep_name].memory.role = 'harvester'; console.log('Create new creep name ' + new_creep_name); } else { console.log(`failed to create harvester with code ${Re_code}`); } } function createTransferCreep() { //create new creep let new_creep_name = "AUTO_CREATE_Transfer_" + util.uuid(); let Re_code; Re_code = Game.spawns['Spawn1'].createCreep([MOVE, CARRY, CARRY], new_creep_name); if (new_creep_name == Re_code) { Game.creeps[new_creep_name].memory.role = 'transfer'; console.log('Create new creep name ' + new_creep_name); } else { console.log(`failed to create transfer with code ${Re_code}`); } }