diff --git a/init.js b/init.js index 8ad1fce..515b0e5 100644 --- a/init.js +++ b/init.js @@ -1,15 +1,11 @@ module.exports = { - init(){ - if(!Memory.Source){ - Memory.Source=new Object(); - Memory.Source.Energy=[]; - // let sources = creep.room.find(FIND_SOURCES); - - // for(let source in sources){ - - // } + init() { + if (!Memory.Source) { + Memory.Source = new Object(); + Memory.Source.Energy = []; } } -} \ No newline at end of file +} + diff --git a/main.js b/main.js index 3402981..a1428e0 100644 --- a/main.js +++ b/main.js @@ -1,10 +1,14 @@ -const init=require('init') +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(); @@ -41,6 +45,67 @@ function assign_task() { 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() { diff --git a/picker.js b/picker.js index 2e40cc8..46dcfa9 100644 --- a/picker.js +++ b/picker.js @@ -1,13 +1,15 @@ //只收集,不运输 module.exports = { run(creep) { - let sources = creep.room.find(FIND_SOURCES); - let Re_code=creep.harvest(sources[0]); + // let sources = creep.room.find(FIND_SOURCES); + let working_target_id=creep.working_target_id; + let working_target=Game.getObjectById(working_target_id); + let Re_code=creep.harvest(working_target); if ( Re_code== ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0]); + creep.moveTo(working_target); }else{ if(OK==Re_code){ - creep.harvest(sources[0]); + creep.harvest(working_target); creep.drop(RESOURCE_ENERGY); } }