1.修复bug。2.增加assign_transfer函数,精简代码。
This commit is contained in:
parent
f9856855ba
commit
27750dbc53
58
builder.js
58
builder.js
|
|
@ -1,8 +1,30 @@
|
||||||
const utils = require('utils')
|
const utils = require('utils');
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(creep) {
|
run(creep) {
|
||||||
creep.say('builder')
|
|
||||||
if (creep.working_target_id) {
|
if (creep.working_target_id) {
|
||||||
|
let working_target_id = creep.working_target_id;
|
||||||
|
let working_target = Game.getObjectById(working_target_id);
|
||||||
|
if(!working_target){
|
||||||
|
// creep.working_target_id=undefined//目标已经完成,转为空闲状态
|
||||||
|
// _.each(Memery.WorkingTarget[creep.name].working_creep_names,creep_name=>Game.creeps[creep_name].working_target_id=undefined)
|
||||||
|
// delete Memery.WorkingTarget[creep.name]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (working_target.structureType == STRUCTURE_CONTROLLER) {
|
||||||
|
creep.say('ug')
|
||||||
|
upgradeController_job(creep)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
creep.say('bd')
|
||||||
|
build_construction(creep)
|
||||||
|
}else{
|
||||||
|
creep.say('SB')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function upgradeController_job(creep) {
|
||||||
let working_target_id = creep.working_target_id;
|
let working_target_id = creep.working_target_id;
|
||||||
let working_target = Game.getObjectById(working_target_id);
|
let working_target = Game.getObjectById(working_target_id);
|
||||||
if (utils.distance(creep.pos, working_target.pos) > 1.5) {
|
if (utils.distance(creep.pos, working_target.pos) > 1.5) {
|
||||||
|
|
@ -14,17 +36,47 @@ module.exports = {
|
||||||
})
|
})
|
||||||
if (nearest_dropped_enengy.length > 0) {
|
if (nearest_dropped_enengy.length > 0) {
|
||||||
let Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
let Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
||||||
if (OK == Re_code_pickup) {
|
if (OK == Re_code_pickup || ERR_FULL == Re_code_pickup) {
|
||||||
//TODO:需要适应不同的建筑
|
//TODO:需要适应不同的建筑
|
||||||
let Re_code = creep.upgradeController(working_target)
|
let Re_code = creep.upgradeController(working_target)
|
||||||
if (OK == Re_code) {
|
if (OK == Re_code) {
|
||||||
|
|
||||||
} else if (Re_code != ERR_NOT_ENOUGH_RESOURCES && Re_code != ERR_NOT_IN_RANGE) {
|
} else if (Re_code != ERR_NOT_ENOUGH_RESOURCES && Re_code != ERR_NOT_IN_RANGE) {
|
||||||
console.log(`${creep.name} failed to upgrade room controller:err ${Re_code}`)
|
console.log(`${creep.name} failed to upgrade room controller:err ${Re_code}`)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`${creep.name} failed to pickup: err ${Re_code_pickup}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function build_construction(creep) {
|
||||||
|
let working_target_id = creep.working_target_id;
|
||||||
|
let working_target = Game.getObjectById(working_target_id);
|
||||||
|
if (utils.distance(creep.pos, working_target.pos) > 1.5) {
|
||||||
|
creep.moveTo(working_target);
|
||||||
|
} else {
|
||||||
|
let all_dropped_resource = creep.room.find(FIND_DROPPED_RESOURCES)
|
||||||
|
let nearest_dropped_enengy = _.filter(all_dropped_resource, function (energy) {
|
||||||
|
return energy.resourceType == RESOURCE_ENERGY & utils.distance(energy.pos, creep.pos) < 1.5
|
||||||
|
})
|
||||||
|
if (nearest_dropped_enengy.length > 0) {
|
||||||
|
let Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
||||||
|
if (OK == Re_code_pickup || ERR_FULL == Re_code_pickup) {
|
||||||
|
//TODO:需要适应不同的建筑
|
||||||
|
let Re_code = creep.build(working_target)
|
||||||
|
if (OK == Re_code) {
|
||||||
|
|
||||||
|
} else if (Re_code != ERR_NOT_ENOUGH_RESOURCES && Re_code != ERR_NOT_IN_RANGE) {
|
||||||
|
console.log(`${creep.name} failed to build:err ${Re_code}`)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`${creep.name} failed to pickup: err ${Re_code_pickup}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,11 +4,11 @@ module.exports = {
|
||||||
let status = change_status()
|
let status = change_status()
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 'harvester':
|
case 'harvester':
|
||||||
creep.say('harvester')
|
creep.say('hv')
|
||||||
harvester_job(creep)
|
harvester_job(creep)
|
||||||
break
|
break
|
||||||
case 'picker':
|
case 'picker':
|
||||||
creep.say('picker')
|
creep.say('pc')
|
||||||
picker_job(creep)
|
picker_job(creep)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -17,7 +17,6 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
function harvester_job(creep) {
|
function harvester_job(creep) {
|
||||||
//分配source
|
|
||||||
if (creep.carry.energy < creep.carryCapacity) {
|
if (creep.carry.energy < creep.carryCapacity) {
|
||||||
let working_target_id = creep.working_target_id;
|
let working_target_id = creep.working_target_id;
|
||||||
let working_target = Game.getObjectById(working_target_id);
|
let working_target = Game.getObjectById(working_target_id);
|
||||||
|
|
@ -35,12 +34,12 @@ function harvester_job(creep) {
|
||||||
|
|
||||||
function picker_job(creep) {
|
function picker_job(creep) {
|
||||||
if (Game.spawns['Spawn1'].store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
if (Game.spawns['Spawn1'].store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||||
creep.say('Standby.')
|
creep.say('SB')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let working_target_id = creep.working_target_id;
|
let working_target_id = creep.working_target_id;
|
||||||
if (!working_target_id) {
|
if (!working_target_id) {
|
||||||
creep.say('Standby.')
|
creep.say('SB')
|
||||||
}
|
}
|
||||||
let working_target = Game.getObjectById(working_target_id);
|
let working_target = Game.getObjectById(working_target_id);
|
||||||
let Re_code = creep.harvest(working_target);
|
let Re_code = creep.harvest(working_target);
|
||||||
|
|
|
||||||
207
main.js
207
main.js
|
|
@ -12,6 +12,14 @@ module.exports.loop = function () {
|
||||||
assign_transfer_to_harvester()
|
assign_transfer_to_harvester()
|
||||||
assign_builder_to_controller();
|
assign_builder_to_controller();
|
||||||
assign_transfer_to_builder();
|
assign_transfer_to_builder();
|
||||||
|
let construction_site_array = Game.spawns['Spawn1'].room.find(FIND_MY_CONSTRUCTION_SITES)
|
||||||
|
_.each(construction_site_array, function (found_construction_site) {
|
||||||
|
let constuction_key = `CONSTRUCTION_${found_construction_site.id}`
|
||||||
|
if (Memory.WorkingTarget[constuction_key] && Memory.WorkingTarget[constuction_key].working_creep_names.length == 0) {
|
||||||
|
assign_builder_to_construction(found_construction_site.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
let isSpawning = false
|
let isSpawning = false
|
||||||
let energy_source_number = _.filter(Game.spawns['Spawn1'].room.find(FIND_SOURCES_ACTIVE)).length;
|
let energy_source_number = _.filter(Game.spawns['Spawn1'].room.find(FIND_SOURCES_ACTIVE)).length;
|
||||||
let harvester_number = _(Game.creeps).filter({ memory: { role: 'harvester' } }).size();
|
let harvester_number = _(Game.creeps).filter({ memory: { role: 'harvester' } }).size();
|
||||||
|
|
@ -26,7 +34,7 @@ module.exports.loop = function () {
|
||||||
}
|
}
|
||||||
let builder_number = _.filter(Game.creeps, { memory: { role: 'builder' } }).length
|
let builder_number = _.filter(Game.creeps, { memory: { role: 'builder' } }).length
|
||||||
//创造一个builder升级room controller
|
//创造一个builder升级room controller
|
||||||
if (Game.spawns['Spawn1'].room.controller && builder_number == 0 && !isSpawning) {
|
if (Game.spawns['Spawn1'].room.controller && builder_number < (1 + construction_site_array.length) && !isSpawning) {
|
||||||
isSpawning = true
|
isSpawning = true
|
||||||
createBuilderCreep();
|
createBuilderCreep();
|
||||||
}
|
}
|
||||||
|
|
@ -43,8 +51,9 @@ module.exports.loop = function () {
|
||||||
|
|
||||||
|
|
||||||
function extra() {
|
function extra() {
|
||||||
// Game.map.visual.circle(Game.getObjectById('5f9423c48004fbeff0f4d965').pos)
|
// Game.map.visual.circle(Game.getObjectById('5bbcaaeb9099fc012e6326f1').pos)
|
||||||
// console.log(Game.getObjectById('5f9423c48004fbeff0f4d965').pos)
|
// console.log(Game.getObjectById('5f953aa1ad1f54190745a9c3').pos)
|
||||||
|
// Memory.WorkingTarget.CONSTRUCTION_5f952fea10782b5f4ec3690a.working_creep_names=[]
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanCreeps() {
|
function cleanCreeps() {
|
||||||
|
|
@ -56,67 +65,6 @@ function cleanCreeps() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanTargetMemory() {
|
|
||||||
for (let i of Object.keys(Memory.WorkingTarget)) {
|
|
||||||
let target = Memory.WorkingTarget[i]
|
|
||||||
if (!Game.getObjectById(target.id)) {
|
|
||||||
console.log(`target ${target.id} is invalid, removed from Memory.WorkingTarget`)
|
|
||||||
delete Memory.WorkingTarget[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function assign_transfer_to_harvester() {
|
|
||||||
//判断有多少个picker的transfer为0
|
|
||||||
let harvester_without_creep = _.filter(Game.creeps, function (creep) {
|
|
||||||
return !Memory.WorkingTarget[creep.name] && creep.memory.role=='harvester'//Memory.WorkingTarget中没有这个picker目标
|
|
||||||
})
|
|
||||||
_.each(harvester_without_creep, function (harvester_creep) {
|
|
||||||
Memory.WorkingTarget[harvester_creep.name] = { id: harvester_creep.id, working_creep_names: [], cate: 'harvester' }//在Memory.WorkingTarget加入需要creep的Picker
|
|
||||||
})
|
|
||||||
let harvester_needs_creep_memory = _.filter(Memory.WorkingTarget, memory => memory.working_creep_names.length == 0 && memory.cate=='harvester')
|
|
||||||
if (harvester_needs_creep_memory.length > 0) {
|
|
||||||
let idle_transfer = _.filter(Game.creeps, creep => !creep.working_target_id && (creep.memory.role == 'transfer'))
|
|
||||||
if (idle_transfer.length > 0) {//一次只安排一个creep
|
|
||||||
let opt = {
|
|
||||||
memory: harvester_needs_creep_memory,
|
|
||||||
creep_name_to_assgin_array: [idle_transfer[0].name]
|
|
||||||
}
|
|
||||||
assign_creeps(opt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function assign_task() {
|
|
||||||
for (const creep_id in Game.creeps) {
|
|
||||||
let creep = Game.creeps[creep_id];
|
|
||||||
creep.action();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function assign_transfer_to_builder() {
|
|
||||||
//判断有多少个picker的transfer为0
|
|
||||||
let builder_without_creep = _.filter(Game.creeps, function (creep) {
|
|
||||||
return !Memory.WorkingTarget[creep.name] && creep.memory.role=='builder'//Memory.WorkingTarget中没有这个picker目标
|
|
||||||
})
|
|
||||||
_.each(builder_without_creep, function (builder) {
|
|
||||||
Memory.WorkingTarget[builder.name] = { id: builder.id, working_creep_names: [], cate: 'builder' }//在Memory.WorkingTarget加入需要creep的builder
|
|
||||||
})
|
|
||||||
let builder_needs_creep_memory = _.filter(Memory.WorkingTarget, memory => memory.working_creep_names.length == 0 && memory.cate=='builder')
|
|
||||||
if (builder_needs_creep_memory.length > 0) {
|
|
||||||
let idle_transfer = _.filter(Game.creeps, creep => !creep.working_target_id && (creep.memory.role == 'transfer'))
|
|
||||||
if (idle_transfer.length > 0) {//一次只安排一个creep
|
|
||||||
let opt = {
|
|
||||||
memory: builder_needs_creep_memory,
|
|
||||||
creep_name_to_assgin_array: [idle_transfer[0].name]
|
|
||||||
}
|
|
||||||
assign_creeps(opt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanInvalidMemoryCreep() {
|
function cleanInvalidMemoryCreep() {
|
||||||
//清理Memory中已经不存在的creep
|
//清理Memory中已经不存在的creep
|
||||||
_.each(Memory.WorkingTarget, function (target) {
|
_.each(Memory.WorkingTarget, function (target) {
|
||||||
|
|
@ -131,24 +79,110 @@ function cleanInvalidMemoryCreep() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanTargetMemory() {
|
||||||
|
for (let i of Object.keys(Memory.WorkingTarget)) {
|
||||||
|
let target = Memory.WorkingTarget[i]
|
||||||
|
if (!Game.getObjectById(target.id)) {
|
||||||
|
console.log(`target ${target.id} is invalid, removed from Memory.WorkingTarget.Set their creep to idle`)
|
||||||
|
_.each(Memory.WorkingTarget[i].working_creep_names, creep_name => creep_name.working_target_id = undefined)
|
||||||
|
delete Memory.WorkingTarget[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function assign_transfer(target_memory) {
|
||||||
|
const NO_IDLE_TRANSFER = -1
|
||||||
|
const OK_TRANSFER = 0
|
||||||
|
if (target_memory.length > 0) {
|
||||||
|
let idle_transfer = _.filter(Game.creeps, creep => !creep.working_target_id && (creep.memory.role == 'transfer'))
|
||||||
|
if (idle_transfer.length > 0) {//一次只安排一个creep
|
||||||
|
let opt = {
|
||||||
|
memory: target_memory,
|
||||||
|
creep_name_to_assign_array: [idle_transfer[0].name]
|
||||||
|
}
|
||||||
|
assign_creeps(opt)
|
||||||
|
return OK_TRANSFER
|
||||||
|
} else {
|
||||||
|
return NO_IDLE_TRANSFER
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//TODO:应该和assign_transfer_to_builder合并
|
||||||
|
function assign_transfer_to_harvester() {
|
||||||
|
//判断有多少个picker的transfer为0
|
||||||
|
let harvester_without_creep = _.filter(Game.creeps, function (creep) {
|
||||||
|
return !Memory.WorkingTarget[creep.name] && creep.memory.role == 'harvester'//Memory.WorkingTarget中没有这个harvester目标
|
||||||
|
})
|
||||||
|
_.each(harvester_without_creep, function (harvester_creep) {
|
||||||
|
Memory.WorkingTarget[harvester_creep.name] = { id: harvester_creep.id, working_creep_names: [], cate: 'harvester' }//在Memory.WorkingTarget加入需要creep的Picker
|
||||||
|
})
|
||||||
|
let harvester_needs_creep_memory = _.filter(Memory.WorkingTarget, memory => memory.working_creep_names.length == 0 && memory.cate == 'harvester')//需要安排creep的harvester
|
||||||
|
assign_transfer(harvester_needs_creep_memory)
|
||||||
|
}
|
||||||
|
|
||||||
|
function assign_task() {
|
||||||
|
for (const creep_id in Game.creeps) {
|
||||||
|
let creep = Game.creeps[creep_id];
|
||||||
|
creep.action();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function assign_transfer_to_builder() {
|
||||||
|
//判断有多少个builder的transfer为0
|
||||||
|
let builder_without_creep = _.filter(Game.creeps, function (creep) {
|
||||||
|
return !Memory.WorkingTarget[creep.name] && creep.memory.role == 'builder'//Memory.WorkingTarget中没有这个builder目标
|
||||||
|
})
|
||||||
|
_.each(builder_without_creep, function (builder) {
|
||||||
|
Memory.WorkingTarget[builder.name] = { id: builder.id, working_creep_names: [], cate: 'builder' }//在Memory.WorkingTarget加入需要creep的builder
|
||||||
|
})
|
||||||
|
let builder_needs_creep_memory = _.filter(Memory.WorkingTarget, memory => memory.working_creep_names.length == 0 && memory.cate == 'builder')//需要安排creep的builder
|
||||||
|
if (assign_transfer(builder_needs_creep_memory) == -1) {
|
||||||
|
console.log(`no enough transfer to builder. Try to create a new one autmatically`)
|
||||||
|
createBuilderCreep()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function assign_builder_to_construction(construction_id) {
|
||||||
|
//检查construction是否已经是target
|
||||||
|
let constuction_key = `CONSTRUCTION_${construction_id}`
|
||||||
|
if (!_.find(Memory.WorkingTarget, target => target.id == construction_id)) {
|
||||||
|
Memory.WorkingTarget[constuction_key] = { id: construction_id, working_creep_names: [], cate: 'constuction_site' }
|
||||||
|
}
|
||||||
|
//安排一个builder过去
|
||||||
|
let builder = _.filter(Game.creeps, creep => creep.memory.role == 'builder' && !creep.working_target_id)
|
||||||
|
if (builder.length > 0) {
|
||||||
|
|
||||||
|
let opt = {
|
||||||
|
memory: _.filter(Memory.WorkingTarget, _memory => _memory.working_creep_names.length == 0 && _memory.cate == 'constuction_site'),
|
||||||
|
creep_name_to_assgin_array: [builder[0].name]
|
||||||
|
}
|
||||||
|
assign_creeps(opt)
|
||||||
|
} else {
|
||||||
|
console.log(`no enough builder to constuction_site ${construction_id}. Try to create a new one autmatically`)
|
||||||
|
createBuilderCreep()
|
||||||
|
}
|
||||||
|
// //开始给建造constrction的每个creep安排transfer
|
||||||
|
// _.each(Memory.WorkingTarget[constuction_key].working_creep_names,function(builder_creep_name){
|
||||||
|
// if(!Memory.WorkingTarget[builder_creep_name]){
|
||||||
|
// Memory.WorkingTarget[builder_creep_name]={id:Game.creeps[builder_creep_name].id,working_creep_names: [], cate: 'builder'}
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function assign_creeps(opt) {
|
function assign_creeps(opt) {
|
||||||
let memory = opt.memory
|
let memory = opt.memory
|
||||||
let creep_name_to_assgin_array = opt.creep_name_to_assgin_array//需要分配工作的creeps
|
let creep_name_to_assign_array = opt.creep_name_to_assign_array//需要分配工作的creeps
|
||||||
//清理Memroy中已经不存在的creep
|
_.each(creep_name_to_assign_array, function (creep_name) {
|
||||||
_.each(memory, function (m) {
|
|
||||||
_.remove(m.working_creep_names, function (name) {
|
|
||||||
if (Game.creeps[name] === undefined || Game.creeps[name] === null) {
|
|
||||||
console.log(`remove ${name} from ${m.id} working_creep_names`)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
})
|
|
||||||
_.each(creep_name_to_assgin_array, function (creep_name) {
|
|
||||||
//先计算每一个target目前有多少个creeps了
|
//先计算每一个target目前有多少个creeps了
|
||||||
let working_creeps_of_target_array = []//记录每个target已有的creep数量
|
let working_creeps_of_target_array = []//记录每个target已有的creep数量
|
||||||
_.each(memory, m => working_creeps_of_target_array.push(m.working_creep_names.length))
|
_.each(memory, m => working_creeps_of_target_array.push(m.working_creep_names.length))
|
||||||
let target_to_add_creep = _.filter(memory, m => m.working_creep_names.length == _.min(working_creeps_of_target_array))//选择目前已有creep最少的,对其添加creep
|
let target_to_add_creep = _.filter(memory, m => m.working_creep_names.length == _.min(working_creeps_of_target_array))//选择目前已有creep最少的,对其添加creep
|
||||||
|
console.log(target_to_add_creep.length)
|
||||||
if (target_to_add_creep.length > 0) {
|
if (target_to_add_creep.length > 0) {
|
||||||
target_to_add_creep[0].working_creep_names.push(creep_name)//target记录自己安排的creep
|
target_to_add_creep[0].working_creep_names.push(creep_name)//target记录自己安排的creep
|
||||||
Game.creeps[creep_name].working_target_id = target_to_add_creep[0].id//将target安排给creep
|
Game.creeps[creep_name].working_target_id = target_to_add_creep[0].id//将target安排给creep
|
||||||
|
|
@ -177,7 +211,7 @@ function assign_harvester_to_enery_source() {
|
||||||
if (available_creep_array.length > 0) {//每次只安排一个
|
if (available_creep_array.length > 0) {//每次只安排一个
|
||||||
let opt = {
|
let opt = {
|
||||||
memory: energy_memory,
|
memory: energy_memory,
|
||||||
creep_name_to_assgin_array: [available_creep_array[0].name]
|
creep_name_to_assign_array: [available_creep_array[0].name]
|
||||||
}
|
}
|
||||||
assign_creeps(opt)
|
assign_creeps(opt)
|
||||||
}
|
}
|
||||||
|
|
@ -187,6 +221,19 @@ function assign_harvester_to_enery_source() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_builder_to_controller() {
|
function assign_builder_to_controller() {
|
||||||
|
//如果存在正在建造的construction,且没有多余的builder,就暂停upgrade controller
|
||||||
|
if (Game.spawns['Spawn1'].room.find(FIND_MY_CONSTRUCTION_SITES).length > 0) {
|
||||||
|
let working_builder = _.filter(Game.creeps, creep => creep.memory.role == 'builder' && !creep.working_target_id)
|
||||||
|
if (working_builder.length == 0) {//没有可以用的builder了
|
||||||
|
let controller_id = Game.spawns['Spawn1'].room.controller.id
|
||||||
|
let controller_memory = Memory.WorkingTarget[`RoomController_${controller_id}`]
|
||||||
|
if (controller_memory) {
|
||||||
|
_.each(controller_memory.working_creep_names, creep_name => Game.creeps[creep_name].working_target_id = 0)
|
||||||
|
memory.working_creep_names = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//判断room controller 是否有creep,没有则添加
|
//判断room controller 是否有creep,没有则添加
|
||||||
if (!_.find(Memory.WorkingTarget, target => target.id == Game.spawns['Spawn1'].room.controller.id)) {
|
if (!_.find(Memory.WorkingTarget, target => target.id == Game.spawns['Spawn1'].room.controller.id)) {
|
||||||
Memory.WorkingTarget[`RoomController_${Game.spawns['Spawn1'].room.controller.id}`] = { id: Game.spawns['Spawn1'].room.controller.id, working_creep_names: [], cate: 'ROOM_CONTROLLER' }
|
Memory.WorkingTarget[`RoomController_${Game.spawns['Spawn1'].room.controller.id}`] = { id: Game.spawns['Spawn1'].room.controller.id, working_creep_names: [], cate: 'ROOM_CONTROLLER' }
|
||||||
|
|
@ -195,12 +242,11 @@ function assign_builder_to_controller() {
|
||||||
//寻找creep为0的room controller
|
//寻找creep为0的room controller
|
||||||
let room_controller_need_creep_array = _.filter(room_controller_mem, m => m.working_creep_names.length == 0 && m.cate == 'ROOM_CONTROLLER')
|
let room_controller_need_creep_array = _.filter(room_controller_mem, m => m.working_creep_names.length == 0 && m.cate == 'ROOM_CONTROLLER')
|
||||||
if (room_controller_need_creep_array.length > 0) {
|
if (room_controller_need_creep_array.length > 0) {
|
||||||
let builder_creep = _.filter(Game.creeps, creep => !creep.working_target_id || creep.memory.role == 'builder')
|
let builder_creep = _.filter(Game.creeps, creep => !creep.working_target_id && creep.memory.role == 'builder')
|
||||||
if (builder_creep.length > 0) {
|
if (builder_creep.length > 0) {
|
||||||
|
|
||||||
let opt = {
|
let opt = {
|
||||||
memory: room_controller_mem,
|
memory: room_controller_mem,
|
||||||
creep_name_to_assgin_array: [builder_creep[0].name]
|
creep_name_to_assign_array: [builder_creep[0].name]
|
||||||
}
|
}
|
||||||
assign_creeps(opt)
|
assign_creeps(opt)
|
||||||
}
|
}
|
||||||
|
|
@ -249,3 +295,4 @@ function createBuilderCreep() {
|
||||||
// console.log(`failed to create builder with code ${Re_code}`);
|
// console.log(`failed to create builder with code ${Re_code}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
6
mount.js
6
mount.js
|
|
@ -15,6 +15,12 @@ module.exports = {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Game.cwt = function (creep_name) {
|
||||||
|
Game.creeps[creep_name].working_target_id = undefined
|
||||||
|
}
|
||||||
|
Game.pos=function(id){
|
||||||
|
console.log(Game.getObjectById(id))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
23
picker.js
23
picker.js
|
|
@ -1,23 +0,0 @@
|
||||||
//只收集,不运输
|
|
||||||
module.exports = {
|
|
||||||
run(creep) {
|
|
||||||
if(Game.spawns['Spawn1'].store.getFreeCapacity(RESOURCE_ENERGY)==0){
|
|
||||||
creep.say('Standby.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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(working_target);
|
|
||||||
} else {
|
|
||||||
if (OK == Re_code) {
|
|
||||||
creep.harvest(working_target);
|
|
||||||
creep.drop(RESOURCE_ENERGY);
|
|
||||||
} else {
|
|
||||||
console.log(`${creep.name} ${creep.memory.role} working target ${working_target_id}`)
|
|
||||||
console.log(`${creep.name} ${creep.memory.role} err: ${Re_code}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
4
task.js
4
task.js
|
|
@ -1,5 +1,9 @@
|
||||||
|
const { memory } = require("console")
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
upgradeController(){
|
upgradeController(){
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
42
test.js
42
test.js
|
|
@ -1,41 +1,3 @@
|
||||||
function Person(age, name) {
|
a= undefined || {a:'a'}
|
||||||
this.name = name;
|
|
||||||
this.age = age;
|
|
||||||
};
|
|
||||||
|
|
||||||
Object.defineProperty(Person.prototype, "age", {
|
console.log(a)
|
||||||
get: function() {
|
|
||||||
return this._age;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Added a few things to demonstrate additional logic on the setter
|
|
||||||
set: function(num) {
|
|
||||||
num = parseInt(num, 10);
|
|
||||||
if(num > 0) {
|
|
||||||
this._age = num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(Person.prototype.hasOwnProperty('age'))
|
|
||||||
console.log(Person.hasOwnProperty('age'))
|
|
||||||
|
|
||||||
if(!(Person.prototype.hasOwnProperty('age'))){
|
|
||||||
Object.defineProperty(Person.prototype, "age", {
|
|
||||||
get: function() {
|
|
||||||
return this._age;
|
|
||||||
},
|
|
||||||
|
|
||||||
// Added a few things to demonstrate additional logic on the setter
|
|
||||||
set: function(num) {
|
|
||||||
num = parseInt(num, 10);
|
|
||||||
if(num > 0) {
|
|
||||||
this._age = num;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
a= new Person(1,2)
|
|
||||||
a.name=10
|
|
||||||
console.log(a.age)
|
|
||||||
|
|
@ -5,16 +5,17 @@ const path = require('path')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(creep) {
|
run(creep) {
|
||||||
creep.say('transfer')
|
creep.say('tf')
|
||||||
let working_target_id = creep.working_target_id
|
let working_target_id = creep.working_target_id
|
||||||
if(!working_target_id){
|
if(!working_target_id){
|
||||||
creep.say('Standby')
|
creep.say('SB')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let working_target = Game.getObjectById(working_target_id)
|
let working_target = Game.getObjectById(working_target_id)
|
||||||
//TODO:处理transfer的对象消失的情况
|
//TODO:处理transfer的对象消失的情况
|
||||||
if(!working_target){
|
if(!working_target){
|
||||||
creep.working_target_id=null
|
// _.each(Memery.WorkingTarget[creep.name].working_creep_names,creep_name=>Game.creeps[creep_name].working_target_id=undefined)
|
||||||
|
// delete Memery.WorkingTarget[creep.name]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let working_target_pos = working_target.pos
|
let working_target_pos = working_target.pos
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue