refactor 4 space indent
This commit is contained in:
parent
13d37b94f3
commit
98b97cbca6
|
|
@ -13,7 +13,8 @@
|
||||||
},
|
},
|
||||||
"rules": {
|
"rules": {
|
||||||
"prettier/prettier": ["error",{
|
"prettier/prettier": ["error",{
|
||||||
"endOfLine":"auto"
|
"endOfLine":"auto",
|
||||||
|
"tabWidth":4
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
"plugins": [
|
"plugins": [
|
||||||
|
|
|
||||||
166
builder.js
166
builder.js
|
|
@ -5,97 +5,101 @@
|
||||||
const utils = require("./utils");
|
const utils = require("./utils");
|
||||||
|
|
||||||
function upgradeController_job(creep) {
|
function upgradeController_job(creep) {
|
||||||
const { working_target_id } = creep;
|
const { working_target_id } = creep;
|
||||||
const working_target = Game.getObjectById(working_target_id);
|
const 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) {
|
||||||
creep.moveTo(working_target);
|
creep.moveTo(working_target);
|
||||||
} else {
|
} else {
|
||||||
const all_dropped_resource = creep.room.find(FIND_DROPPED_RESOURCES);
|
const all_dropped_resource = creep.room.find(FIND_DROPPED_RESOURCES);
|
||||||
const nearest_dropped_enengy = _.filter(all_dropped_resource, function (
|
const nearest_dropped_enengy = _.filter(all_dropped_resource, function (
|
||||||
energy
|
energy
|
||||||
) {
|
|
||||||
return (
|
|
||||||
energy.resourceType === RESOURCE_ENERGY &&
|
|
||||||
utils.distance(energy.pos, creep.pos) < 1.5
|
|
||||||
);
|
|
||||||
});
|
|
||||||
if (nearest_dropped_enengy.length > 0) {
|
|
||||||
const Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
|
||||||
if (OK === Re_code_pickup || ERR_FULL === Re_code_pickup) {
|
|
||||||
// TODO:需要适应不同的建筑
|
|
||||||
const Re_code = creep.upgradeController(working_target);
|
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
if (OK === Re_code) {
|
|
||||||
} else if (
|
|
||||||
Re_code !== ERR_NOT_ENOUGH_RESOURCES &&
|
|
||||||
Re_code !== ERR_NOT_IN_RANGE
|
|
||||||
) {
|
) {
|
||||||
console.log(
|
return (
|
||||||
`${creep.name} failed to upgrade room controller:err ${Re_code}`
|
energy.resourceType === RESOURCE_ENERGY &&
|
||||||
);
|
utils.distance(energy.pos, creep.pos) < 1.5
|
||||||
|
);
|
||||||
|
});
|
||||||
|
if (nearest_dropped_enengy.length > 0) {
|
||||||
|
const Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
||||||
|
if (OK === Re_code_pickup || ERR_FULL === Re_code_pickup) {
|
||||||
|
// TODO:需要适应不同的建筑
|
||||||
|
const Re_code = creep.upgradeController(working_target);
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
if (OK === Re_code) {
|
||||||
|
} 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}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`${creep.name} failed to pickup: err ${Re_code_pickup}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log(`${creep.name} failed to pickup: err ${Re_code_pickup}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function build_construction(creep) {
|
function build_construction(creep) {
|
||||||
const { working_target_id } = creep;
|
const { working_target_id } = creep;
|
||||||
const working_target = Game.getObjectById(working_target_id);
|
const 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) {
|
||||||
creep.moveTo(working_target);
|
creep.moveTo(working_target);
|
||||||
} else {
|
} else {
|
||||||
const all_dropped_resource = creep.room.find(FIND_DROPPED_RESOURCES);
|
const all_dropped_resource = creep.room.find(FIND_DROPPED_RESOURCES);
|
||||||
const nearest_dropped_enengy = _.filter(all_dropped_resource, function (
|
const nearest_dropped_enengy = _.filter(all_dropped_resource, function (
|
||||||
energy
|
energy
|
||||||
) {
|
|
||||||
return (
|
|
||||||
energy.resourceType === RESOURCE_ENERGY &&
|
|
||||||
utils.distance(energy.pos, creep.pos) < 1.5
|
|
||||||
);
|
|
||||||
});
|
|
||||||
if (nearest_dropped_enengy.length > 0) {
|
|
||||||
const Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
|
||||||
if (OK === Re_code_pickup || ERR_FULL === Re_code_pickup) {
|
|
||||||
// TODO:需要适应不同的建筑
|
|
||||||
const Re_code = creep.build(working_target);
|
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
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}`);
|
return (
|
||||||
|
energy.resourceType === RESOURCE_ENERGY &&
|
||||||
|
utils.distance(energy.pos, creep.pos) < 1.5
|
||||||
|
);
|
||||||
|
});
|
||||||
|
if (nearest_dropped_enengy.length > 0) {
|
||||||
|
const Re_code_pickup = creep.pickup(nearest_dropped_enengy[0]);
|
||||||
|
if (OK === Re_code_pickup || ERR_FULL === Re_code_pickup) {
|
||||||
|
// TODO:需要适应不同的建筑
|
||||||
|
const Re_code = creep.build(working_target);
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
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}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.log(`${creep.name} failed to pickup: err ${Re_code_pickup}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(creep) {
|
run(creep) {
|
||||||
if (creep.working_target_id) {
|
if (creep.working_target_id) {
|
||||||
const { working_target_id } = creep;
|
const { working_target_id } = creep;
|
||||||
const working_target = Game.getObjectById(working_target_id);
|
const working_target = Game.getObjectById(working_target_id);
|
||||||
if (!working_target) {
|
if (!working_target) {
|
||||||
// creep.working_target_id=undefined//目标已经完成,转为空闲状态
|
// creep.working_target_id=undefined//目标已经完成,转为空闲状态
|
||||||
// _.each(Memery.WorkingTarget[creep.name].working_creep_names,creep_name=>Game.creeps[creep_name].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]
|
// delete Memery.WorkingTarget[creep.name]
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (working_target.structureType === STRUCTURE_CONTROLLER) {
|
if (working_target.structureType === STRUCTURE_CONTROLLER) {
|
||||||
creep.say("ug");
|
creep.say("ug");
|
||||||
upgradeController_job(creep);
|
upgradeController_job(creep);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
creep.say("bd");
|
creep.say("bd");
|
||||||
build_construction(creep);
|
build_construction(creep);
|
||||||
} else {
|
} else {
|
||||||
creep.say("SB");
|
creep.say("SB");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
/* eslint-disable no-undef */
|
/* eslint-disable no-undef */
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(creep) {
|
run(creep) {
|
||||||
if (Game.spawns.Spawn1.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
|
if (Game.spawns.Spawn1.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
|
||||||
if (
|
if (
|
||||||
creep.transfer(Game.spawns.Spawn1.room.controller, RESOURCE_ENERGY) ===
|
creep.transfer(
|
||||||
ERR_NOT_IN_RANGE
|
Game.spawns.Spawn1.room.controller,
|
||||||
) {
|
RESOURCE_ENERGY
|
||||||
creep.moveTo(Game.spawns.Spawn1.room.controller.pos);
|
) === ERR_NOT_IN_RANGE
|
||||||
}
|
) {
|
||||||
}
|
creep.moveTo(Game.spawns.Spawn1.room.controller.pos);
|
||||||
},
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
102
harvester.js
102
harvester.js
|
|
@ -3,65 +3,65 @@
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
function harvester_job(creep) {
|
function harvester_job(creep) {
|
||||||
if (creep.carry.energy < creep.carryCapacity) {
|
if (creep.carry.energy < creep.carryCapacity) {
|
||||||
const { working_target_id } = creep;
|
const { working_target_id } = creep;
|
||||||
const working_target = Game.getObjectById(working_target_id);
|
const working_target = Game.getObjectById(working_target_id);
|
||||||
if (creep.harvest(working_target) === ERR_NOT_IN_RANGE) {
|
if (creep.harvest(working_target) === ERR_NOT_IN_RANGE) {
|
||||||
creep.moveTo(working_target);
|
creep.moveTo(working_target);
|
||||||
path.visualPath(creep.pos, working_target.pos);
|
path.visualPath(creep.pos, working_target.pos);
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
creep.transfer(Game.spawns.Spawn1, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE
|
||||||
|
) {
|
||||||
|
creep.moveTo(Game.spawns.Spawn1);
|
||||||
|
path.visualPath(creep.pos, Game.spawns.Spawn1.pos);
|
||||||
}
|
}
|
||||||
} else if (
|
|
||||||
creep.transfer(Game.spawns.Spawn1, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE
|
|
||||||
) {
|
|
||||||
creep.moveTo(Game.spawns.Spawn1);
|
|
||||||
path.visualPath(creep.pos, Game.spawns.Spawn1.pos);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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("SB");
|
creep.say("SB");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { working_target_id } = creep;
|
const { working_target_id } = creep;
|
||||||
if (!working_target_id) {
|
if (!working_target_id) {
|
||||||
creep.say("SB");
|
creep.say("SB");
|
||||||
}
|
}
|
||||||
const working_target = Game.getObjectById(working_target_id);
|
const working_target = Game.getObjectById(working_target_id);
|
||||||
const Re_code = creep.harvest(working_target);
|
const Re_code = creep.harvest(working_target);
|
||||||
if (Re_code === ERR_NOT_IN_RANGE) {
|
if (Re_code === ERR_NOT_IN_RANGE) {
|
||||||
creep.moveTo(working_target);
|
creep.moveTo(working_target);
|
||||||
} else if (OK === Re_code) {
|
} else if (OK === Re_code) {
|
||||||
creep.harvest(working_target);
|
creep.harvest(working_target);
|
||||||
creep.drop(RESOURCE_ENERGY);
|
creep.drop(RESOURCE_ENERGY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function change_status() {
|
function change_status() {
|
||||||
if (
|
if (
|
||||||
_(Game.creeps)
|
_(Game.creeps)
|
||||||
.filter({ memory: { role: "transfer" } })
|
.filter({ memory: { role: "transfer" } })
|
||||||
.size() > 0
|
.size() > 0
|
||||||
) {
|
) {
|
||||||
return "picker";
|
return "picker";
|
||||||
}
|
}
|
||||||
return "harvester";
|
return "harvester";
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(creep) {
|
run(creep) {
|
||||||
const status = change_status();
|
const status = change_status();
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case "harvester":
|
case "harvester":
|
||||||
creep.say("hv");
|
creep.say("hv");
|
||||||
harvester_job(creep);
|
harvester_job(creep);
|
||||||
break;
|
break;
|
||||||
case "picker":
|
case "picker":
|
||||||
creep.say("pc");
|
creep.say("pc");
|
||||||
picker_job(creep);
|
picker_job(creep);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
22
init.js
22
init.js
|
|
@ -1,14 +1,14 @@
|
||||||
/* eslint-disable no-undef */
|
/* eslint-disable no-undef */
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init() {
|
init() {
|
||||||
if (!Memory.Task) {
|
if (!Memory.Task) {
|
||||||
Memory.Task = {};
|
Memory.Task = {};
|
||||||
}
|
}
|
||||||
Memory.Task.HighPorioty = [];
|
Memory.Task.HighPorioty = [];
|
||||||
Memory.Task.LowPorioty = [];
|
Memory.Task.LowPorioty = [];
|
||||||
// WorkingTarget是被跟踪物,可以是一个structure、energy、constructionSite,也可以是一个creep
|
// WorkingTarget是被跟踪物,可以是一个structure、energy、constructionSite,也可以是一个creep
|
||||||
if (!Memory.WorkingTarget) {
|
if (!Memory.WorkingTarget) {
|
||||||
Memory.WorkingTarget = {};
|
Memory.WorkingTarget = {};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
693
main.js
693
main.js
|
|
@ -7,396 +7,411 @@ const init = require("./init");
|
||||||
const util = require("./utils");
|
const util = require("./utils");
|
||||||
|
|
||||||
function extra() {
|
function extra() {
|
||||||
// Game.map.visual.circle(Game.getObjectById('5bbcaaeb9099fc012e6326f1').pos)
|
// Game.map.visual.circle(Game.getObjectById('5bbcaaeb9099fc012e6326f1').pos)
|
||||||
// console.log(Game.getObjectById('5f953aa1ad1f54190745a9c3').pos)
|
// console.log(Game.getObjectById('5f953aa1ad1f54190745a9c3').pos)
|
||||||
// Memory.WorkingTarget.CONSTRUCTION_5f952fea10782b5f4ec3690a.working_creep_names=[]
|
// Memory.WorkingTarget.CONSTRUCTION_5f952fea10782b5f4ec3690a.working_creep_names=[]
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanCreeps() {
|
function cleanCreeps() {
|
||||||
_.each(Object.keys(Memory.creeps), function (key) {
|
_.each(Object.keys(Memory.creeps), function (key) {
|
||||||
if (!Game.creeps[key]) {
|
if (!Game.creeps[key]) {
|
||||||
console.log(`${key} is invalid, removed from Memory.creeps`);
|
console.log(`${key} is invalid, removed from Memory.creeps`);
|
||||||
delete Memory.creeps[key];
|
delete Memory.creeps[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanInvalidMemoryCreep() {
|
function cleanInvalidMemoryCreep() {
|
||||||
// 清理Memory中已经不存在的creep
|
// 清理Memory中已经不存在的creep
|
||||||
_.each(Memory.WorkingTarget, function (target) {
|
_.each(Memory.WorkingTarget, function (target) {
|
||||||
_.remove(target.working_creep_names, function (creep_name) {
|
_.remove(target.working_creep_names, function (creep_name) {
|
||||||
if (!Game.creeps[creep_name]) {
|
if (!Game.creeps[creep_name]) {
|
||||||
console.log(
|
console.log(
|
||||||
`${creep_name} is invalid, removed from working_creep_names`
|
`${creep_name} is invalid, removed from working_creep_names`
|
||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanTargetMemory() {
|
function cleanTargetMemory() {
|
||||||
_.each(Object.keys(Memory.WorkingTarget), function (target) {
|
_.each(Object.keys(Memory.WorkingTarget), function (target) {
|
||||||
if (!Game.getObjectById(target.id)) {
|
if (!Game.getObjectById(target.id)) {
|
||||||
console.log(
|
console.log(
|
||||||
`target ${target.id} is invalid, removed from Memory.WorkingTarget.Set their creep to idle`
|
`target ${target.id} is invalid, removed from Memory.WorkingTarget.Set their creep to idle`
|
||||||
);
|
);
|
||||||
_.each(target.working_creep_names, (creep_name) => {
|
_.each(target.working_creep_names, (creep_name) => {
|
||||||
Game.creeps[creep_name].working_target_id = undefined;
|
Game.creeps[creep_name].working_target_id = undefined;
|
||||||
});
|
});
|
||||||
delete Memory.WorkingTarget[i];
|
delete Memory.WorkingTarget[i];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_creeps(opt) {
|
function assign_creeps(opt) {
|
||||||
const { memory } = opt;
|
const { memory } = opt;
|
||||||
const { creep_name_to_assign_array } = opt; // 需要分配工作的creeps
|
const { creep_name_to_assign_array } = opt; // 需要分配工作的creeps
|
||||||
_.each(creep_name_to_assign_array, function (creep_name) {
|
_.each(creep_name_to_assign_array, function (creep_name) {
|
||||||
// 先计算每一个target目前有多少个creeps了
|
// 先计算每一个target目前有多少个creeps了
|
||||||
const working_creeps_of_target_array = []; // 记录每个target已有的creep数量
|
const working_creeps_of_target_array = []; // 记录每个target已有的creep数量
|
||||||
_.each(memory, (m) =>
|
_.each(memory, (m) =>
|
||||||
working_creeps_of_target_array.push(m.working_creep_names.length)
|
working_creeps_of_target_array.push(m.working_creep_names.length)
|
||||||
);
|
);
|
||||||
const target_to_add_creep = _.filter(
|
const target_to_add_creep = _.filter(
|
||||||
memory,
|
memory,
|
||||||
(m) =>
|
(m) =>
|
||||||
m.working_creep_names.length === _.min(working_creeps_of_target_array)
|
m.working_creep_names.length ===
|
||||||
); // 选择目前已有creep最少的,对其添加creep
|
_.min(working_creeps_of_target_array)
|
||||||
if (target_to_add_creep.length > 0) {
|
); // 选择目前已有creep最少的,对其添加creep
|
||||||
target_to_add_creep[0].working_creep_names.push(creep_name); // target记录自己安排的creep
|
if (target_to_add_creep.length > 0) {
|
||||||
Game.creeps[creep_name].working_target_id = target_to_add_creep[0].id; // 将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
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_transfer(target_memory) {
|
function assign_transfer(target_memory) {
|
||||||
const NO_IDLE_TRANSFER = -1;
|
const NO_IDLE_TRANSFER = -1;
|
||||||
const OK_TRANSFER = 0;
|
const OK_TRANSFER = 0;
|
||||||
const MEMORY_EMPTY = -2;
|
const MEMORY_EMPTY = -2;
|
||||||
if (target_memory.length > 0) {
|
if (target_memory.length > 0) {
|
||||||
const idle_transfer = _.filter(
|
const idle_transfer = _.filter(
|
||||||
Game.creeps,
|
Game.creeps,
|
||||||
(creep) => !creep.working_target_id && creep.memory.role === "transfer"
|
(creep) =>
|
||||||
);
|
!creep.working_target_id && creep.memory.role === "transfer"
|
||||||
if (idle_transfer.length > 0) {
|
);
|
||||||
// 一次只安排一个creep
|
if (idle_transfer.length > 0) {
|
||||||
const opt = {
|
// 一次只安排一个creep
|
||||||
memory: target_memory,
|
const opt = {
|
||||||
creep_name_to_assign_array: [idle_transfer[0].name],
|
memory: target_memory,
|
||||||
};
|
creep_name_to_assign_array: [idle_transfer[0].name],
|
||||||
assign_creeps(opt);
|
};
|
||||||
return OK_TRANSFER;
|
assign_creeps(opt);
|
||||||
|
return OK_TRANSFER;
|
||||||
|
}
|
||||||
|
return NO_IDLE_TRANSFER;
|
||||||
}
|
}
|
||||||
return NO_IDLE_TRANSFER;
|
return MEMORY_EMPTY;
|
||||||
}
|
|
||||||
return MEMORY_EMPTY;
|
|
||||||
}
|
}
|
||||||
// TODO:应该和assign_transfer_to_builder合并
|
// TODO:应该和assign_transfer_to_builder合并
|
||||||
function assign_transfer_to_harvester() {
|
function assign_transfer_to_harvester() {
|
||||||
// 判断有多少个picker的transfer为0
|
// 判断有多少个picker的transfer为0
|
||||||
const harvester_without_creep = _.filter(Game.creeps, function (creep) {
|
const harvester_without_creep = _.filter(Game.creeps, function (creep) {
|
||||||
return (
|
return (
|
||||||
!Memory.WorkingTarget[creep.name] && creep.memory.role === "harvester"
|
!Memory.WorkingTarget[creep.name] &&
|
||||||
); // Memory.WorkingTarget中没有这个harvester目标
|
creep.memory.role === "harvester"
|
||||||
});
|
); // Memory.WorkingTarget中没有这个harvester目标
|
||||||
_.each(harvester_without_creep, function (harvester_creep) {
|
});
|
||||||
Memory.WorkingTarget[harvester_creep.name] = {
|
_.each(harvester_without_creep, function (harvester_creep) {
|
||||||
id: harvester_creep.id,
|
Memory.WorkingTarget[harvester_creep.name] = {
|
||||||
working_creep_names: [],
|
id: harvester_creep.id,
|
||||||
cate: "harvester",
|
working_creep_names: [],
|
||||||
}; // 在Memory.WorkingTarget加入需要creep的Picker
|
cate: "harvester",
|
||||||
});
|
}; // 在Memory.WorkingTarget加入需要creep的Picker
|
||||||
const harvester_needs_creep_memory = _.filter(
|
});
|
||||||
Memory.WorkingTarget,
|
const harvester_needs_creep_memory = _.filter(
|
||||||
(memory) =>
|
Memory.WorkingTarget,
|
||||||
memory.working_creep_names.length === 0 && memory.cate === "harvester"
|
(memory) =>
|
||||||
); // 需要安排creep的harvester
|
memory.working_creep_names.length === 0 &&
|
||||||
assign_transfer(harvester_needs_creep_memory);
|
memory.cate === "harvester"
|
||||||
|
); // 需要安排creep的harvester
|
||||||
|
assign_transfer(harvester_needs_creep_memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_task() {
|
function assign_task() {
|
||||||
_.each(Object.keys(Game.creeps), (creep_name) => {
|
_.each(Object.keys(Game.creeps), (creep_name) => {
|
||||||
const creep = Game.creeps[creep_name];
|
const creep = Game.creeps[creep_name];
|
||||||
creep.action();
|
creep.action();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function createBuilderCreep() {
|
function createBuilderCreep() {
|
||||||
// create new creep
|
// create new creep
|
||||||
const new_creep_name = `AUTO_CREATE_Builder_${util.uuid()}`;
|
const new_creep_name = `AUTO_CREATE_Builder_${util.uuid()}`;
|
||||||
const Re_code = Game.spawns.Spawn1.createCreep(
|
const Re_code = Game.spawns.Spawn1.createCreep(
|
||||||
[MOVE, CARRY, WORK],
|
[MOVE, CARRY, WORK],
|
||||||
new_creep_name
|
new_creep_name
|
||||||
);
|
);
|
||||||
if (Re_code === new_creep_name) {
|
if (Re_code === new_creep_name) {
|
||||||
Game.creeps[new_creep_name].memory.role = "builder";
|
Game.creeps[new_creep_name].memory.role = "builder";
|
||||||
console.log(`Create new creep ${new_creep_name}`);
|
console.log(`Create new creep ${new_creep_name}`);
|
||||||
} else {
|
} else {
|
||||||
// console.log(`failed to create builder with code ${Re_code}`);
|
// console.log(`failed to create builder with code ${Re_code}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_transfer_to_builder() {
|
function assign_transfer_to_builder() {
|
||||||
// 判断有多少个builder的transfer为0
|
// 判断有多少个builder的transfer为0
|
||||||
const builder_without_creep = _.filter(Game.creeps, function (creep) {
|
const builder_without_creep = _.filter(Game.creeps, function (creep) {
|
||||||
return !Memory.WorkingTarget[creep.name] && creep.memory.role === "builder"; // Memory.WorkingTarget中没有这个builder目标
|
return (
|
||||||
});
|
!Memory.WorkingTarget[creep.name] && creep.memory.role === "builder"
|
||||||
_.each(builder_without_creep, function (builder) {
|
); // Memory.WorkingTarget中没有这个builder目标
|
||||||
Memory.WorkingTarget[builder.name] = {
|
});
|
||||||
id: builder.id,
|
_.each(builder_without_creep, function (builder) {
|
||||||
working_creep_names: [],
|
Memory.WorkingTarget[builder.name] = {
|
||||||
cate: "builder",
|
id: builder.id,
|
||||||
}; // 在Memory.WorkingTarget加入需要creep的builder
|
working_creep_names: [],
|
||||||
});
|
cate: "builder",
|
||||||
const builder_needs_creep_memory = _.filter(
|
}; // 在Memory.WorkingTarget加入需要creep的builder
|
||||||
Memory.WorkingTarget,
|
});
|
||||||
(memory) =>
|
const builder_needs_creep_memory = _.filter(
|
||||||
memory.working_creep_names.length === 0 && memory.cate === "builder"
|
Memory.WorkingTarget,
|
||||||
); // 需要安排creep的builder
|
(memory) =>
|
||||||
if (assign_transfer(builder_needs_creep_memory) === -1) {
|
memory.working_creep_names.length === 0 && memory.cate === "builder"
|
||||||
console.log(
|
); // 需要安排creep的builder
|
||||||
`no enough transfer to builder. Try to create a new one autmatically`
|
if (assign_transfer(builder_needs_creep_memory) === -1) {
|
||||||
);
|
console.log(
|
||||||
createBuilderCreep();
|
`no enough transfer to builder. Try to create a new one autmatically`
|
||||||
}
|
);
|
||||||
|
createBuilderCreep();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_builder_to_construction(construction_id) {
|
function assign_builder_to_construction(construction_id) {
|
||||||
// 检查construction是否已经是target
|
// 检查construction是否已经是target
|
||||||
const constuction_key = `CONSTRUCTION_${construction_id}`;
|
const constuction_key = `CONSTRUCTION_${construction_id}`;
|
||||||
if (
|
if (
|
||||||
!_.find(Memory.WorkingTarget, (target) => target.id === construction_id)
|
!_.find(Memory.WorkingTarget, (target) => target.id === construction_id)
|
||||||
) {
|
) {
|
||||||
Memory.WorkingTarget[constuction_key] = {
|
Memory.WorkingTarget[constuction_key] = {
|
||||||
id: construction_id,
|
id: construction_id,
|
||||||
working_creep_names: [],
|
working_creep_names: [],
|
||||||
cate: "constuction_site",
|
cate: "constuction_site",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// 安排一个builder过去
|
// 安排一个builder过去
|
||||||
const builder = _.filter(
|
const builder = _.filter(
|
||||||
Game.creeps,
|
Game.creeps,
|
||||||
(creep) => creep.memory.role === "builder" && !creep.working_target_id
|
(creep) => creep.memory.role === "builder" && !creep.working_target_id
|
||||||
);
|
|
||||||
if (builder.length > 0) {
|
|
||||||
const 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();
|
if (builder.length > 0) {
|
||||||
}
|
const opt = {
|
||||||
// //开始给建造constrction的每个creep安排transfer
|
memory: _.filter(
|
||||||
// _.each(Memory.WorkingTarget[constuction_key].working_creep_names,function(builder_creep_name){
|
Memory.WorkingTarget,
|
||||||
// if(!Memory.WorkingTarget[builder_creep_name]){
|
(_memory) =>
|
||||||
// Memory.WorkingTarget[builder_creep_name]={id:Game.creeps[builder_creep_name].id,working_creep_names: [], cate: 'builder'}
|
_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_harvester_to_enery_source() {
|
function assign_harvester_to_enery_source() {
|
||||||
const sources = Game.spawns.Spawn1.room.find(FIND_SOURCES_ACTIVE);
|
const sources = Game.spawns.Spawn1.room.find(FIND_SOURCES_ACTIVE);
|
||||||
const all_memory = Memory.WorkingTarget;
|
const all_memory = Memory.WorkingTarget;
|
||||||
_.each(Object.keys(sources), function (source) {
|
_.each(Object.keys(sources), function (source) {
|
||||||
const { id } = source;
|
const { id } = source;
|
||||||
const key = `Energy_${id}`;
|
const key = `Energy_${id}`;
|
||||||
if (!Memory.WorkingTarget[key]) {
|
if (!Memory.WorkingTarget[key]) {
|
||||||
Memory.WorkingTarget[key] = {
|
Memory.WorkingTarget[key] = {
|
||||||
id,
|
id,
|
||||||
working_creep_names: [],
|
working_creep_names: [],
|
||||||
cate: "ENERGY_SOURCE",
|
cate: "ENERGY_SOURCE",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// 向Memory.WorkingTarget里添加所有的Energy Source
|
// 向Memory.WorkingTarget里添加所有的Energy Source
|
||||||
// 寻找Energy Source的Memory
|
// 寻找Energy Source的Memory
|
||||||
const energy_memory = _.filter(all_memory, { cate: "ENERGY_SOURCE" });
|
const energy_memory = _.filter(all_memory, { cate: "ENERGY_SOURCE" });
|
||||||
// 寻找creep为0的energy source
|
// 寻找creep为0的energy source
|
||||||
const energy_source_need_creep_array = _.filter(
|
const energy_source_need_creep_array = _.filter(
|
||||||
energy_memory,
|
energy_memory,
|
||||||
(m) => m.working_creep_names.length === 0
|
(m) => m.working_creep_names.length === 0
|
||||||
);
|
|
||||||
if (energy_source_need_creep_array.length > 0) {
|
|
||||||
// 找到一个空闲的creep
|
|
||||||
const available_creep_array = _.filter(
|
|
||||||
Game.creeps,
|
|
||||||
(creep) => !creep.working_target_id && creep.memory.role === "harvester"
|
|
||||||
);
|
);
|
||||||
if (available_creep_array.length > 0) {
|
if (energy_source_need_creep_array.length > 0) {
|
||||||
// 每次只安排一个
|
// 找到一个空闲的creep
|
||||||
const opt = {
|
const available_creep_array = _.filter(
|
||||||
memory: energy_memory,
|
Game.creeps,
|
||||||
creep_name_to_assign_array: [available_creep_array[0].name],
|
(creep) =>
|
||||||
};
|
!creep.working_target_id && creep.memory.role === "harvester"
|
||||||
assign_creeps(opt);
|
);
|
||||||
|
if (available_creep_array.length > 0) {
|
||||||
|
// 每次只安排一个
|
||||||
|
const opt = {
|
||||||
|
memory: energy_memory,
|
||||||
|
creep_name_to_assign_array: [available_creep_array[0].name],
|
||||||
|
};
|
||||||
|
assign_creeps(opt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function assign_builder_to_controller() {
|
function assign_builder_to_controller() {
|
||||||
// 如果存在正在建造的construction,且没有多余的builder,就暂停upgrade controller
|
// 如果存在正在建造的construction,且没有多余的builder,就暂停upgrade controller
|
||||||
if (Game.spawns.Spawn1.room.find(FIND_MY_CONSTRUCTION_SITES).length > 0) {
|
if (Game.spawns.Spawn1.room.find(FIND_MY_CONSTRUCTION_SITES).length > 0) {
|
||||||
const working_builder = _.filter(
|
const working_builder = _.filter(
|
||||||
Game.creeps,
|
Game.creeps,
|
||||||
(creep) => creep.memory.role === "builder" && !creep.working_target_id
|
(creep) =>
|
||||||
);
|
creep.memory.role === "builder" && !creep.working_target_id
|
||||||
if (working_builder.length === 0) {
|
);
|
||||||
// 没有可以用的builder了
|
if (working_builder.length === 0) {
|
||||||
const controller_id = Game.spawns.Spawn1.room.controller.id;
|
// 没有可以用的builder了
|
||||||
const controller_memory =
|
const controller_id = Game.spawns.Spawn1.room.controller.id;
|
||||||
Memory.WorkingTarget[`RoomController_${controller_id}`];
|
const controller_memory =
|
||||||
if (controller_memory) {
|
Memory.WorkingTarget[`RoomController_${controller_id}`];
|
||||||
_.each(controller_memory.working_creep_names, (creep_name) => {
|
if (controller_memory) {
|
||||||
Game.creeps[creep_name].working_target_id = 0;
|
_.each(controller_memory.working_creep_names, (creep_name) => {
|
||||||
});
|
Game.creeps[creep_name].working_target_id = 0;
|
||||||
memory.working_creep_names = [];
|
});
|
||||||
return;
|
memory.working_creep_names = [];
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
// 判断room controller 是否有creep,没有则添加
|
||||||
// 判断room controller 是否有creep,没有则添加
|
if (
|
||||||
if (
|
!_.find(
|
||||||
!_.find(
|
Memory.WorkingTarget,
|
||||||
Memory.WorkingTarget,
|
(target) => target.id === Game.spawns.Spawn1.room.controller.id
|
||||||
(target) => target.id === Game.spawns.Spawn1.room.controller.id
|
)
|
||||||
)
|
) {
|
||||||
) {
|
Memory.WorkingTarget[
|
||||||
Memory.WorkingTarget[
|
`RoomController_${Game.spawns.Spawn1.room.controller.id}`
|
||||||
`RoomController_${Game.spawns.Spawn1.room.controller.id}`
|
] = {
|
||||||
] = {
|
id: Game.spawns.Spawn1.room.controller.id,
|
||||||
id: Game.spawns.Spawn1.room.controller.id,
|
working_creep_names: [],
|
||||||
working_creep_names: [],
|
cate: "ROOM_CONTROLLER",
|
||||||
cate: "ROOM_CONTROLLER",
|
};
|
||||||
};
|
}
|
||||||
}
|
const room_controller_mem = _.filter(Memory.WorkingTarget, {
|
||||||
const room_controller_mem = _.filter(Memory.WorkingTarget, {
|
cate: "ROOM_CONTROLLER",
|
||||||
cate: "ROOM_CONTROLLER",
|
});
|
||||||
});
|
// 寻找creep为0的room controller
|
||||||
// 寻找creep为0的room controller
|
const room_controller_need_creep_array = _.filter(
|
||||||
const room_controller_need_creep_array = _.filter(
|
room_controller_mem,
|
||||||
room_controller_mem,
|
(m) =>
|
||||||
(m) => m.working_creep_names.length === 0 && m.cate === "ROOM_CONTROLLER"
|
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) {
|
||||||
const builder_creep = _.filter(
|
const builder_creep = _.filter(
|
||||||
Game.creeps,
|
Game.creeps,
|
||||||
(creep) => !creep.working_target_id && creep.memory.role === "builder"
|
(creep) =>
|
||||||
);
|
!creep.working_target_id && creep.memory.role === "builder"
|
||||||
if (builder_creep.length > 0) {
|
);
|
||||||
const opt = {
|
if (builder_creep.length > 0) {
|
||||||
memory: room_controller_mem,
|
const opt = {
|
||||||
creep_name_to_assign_array: [builder_creep[0].name],
|
memory: room_controller_mem,
|
||||||
};
|
creep_name_to_assign_array: [builder_creep[0].name],
|
||||||
assign_creeps(opt);
|
};
|
||||||
|
assign_creeps(opt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createHarvestCreep() {
|
function createHarvestCreep() {
|
||||||
// create new creep
|
// create new creep
|
||||||
const new_creep_name = `AUTO_CREATE_Harvester_${util.uuid()}`;
|
const new_creep_name = `AUTO_CREATE_Harvester_${util.uuid()}`;
|
||||||
const Re_code = Game.spawns.Spawn1.createCreep(
|
const Re_code = Game.spawns.Spawn1.createCreep(
|
||||||
[MOVE, CARRY, WORK],
|
[MOVE, CARRY, WORK],
|
||||||
new_creep_name
|
new_creep_name
|
||||||
);
|
);
|
||||||
if (Re_code === new_creep_name) {
|
if (Re_code === new_creep_name) {
|
||||||
Game.creeps[new_creep_name].memory.role = "harvester";
|
Game.creeps[new_creep_name].memory.role = "harvester";
|
||||||
console.log(`Create new creep ${new_creep_name}`);
|
console.log(`Create new creep ${new_creep_name}`);
|
||||||
} else {
|
} else {
|
||||||
// console.log(`failed to create harvester with code ${Re_code}`);
|
// console.log(`failed to create harvester with code ${Re_code}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createTransferCreep() {
|
function createTransferCreep() {
|
||||||
// create new creep
|
// create new creep
|
||||||
const new_creep_name = `AUTO_CREATE_Transfer_${util.uuid()}`;
|
const new_creep_name = `AUTO_CREATE_Transfer_${util.uuid()}`;
|
||||||
const Re_code = Game.spawns.Spawn1.createCreep(
|
const Re_code = Game.spawns.Spawn1.createCreep(
|
||||||
[MOVE, CARRY, CARRY],
|
[MOVE, CARRY, CARRY],
|
||||||
new_creep_name
|
new_creep_name
|
||||||
);
|
);
|
||||||
if (new_creep_name === Re_code) {
|
if (new_creep_name === Re_code) {
|
||||||
Game.creeps[new_creep_name].memory.role = "transfer";
|
Game.creeps[new_creep_name].memory.role = "transfer";
|
||||||
console.log(`Create new creep ${new_creep_name}`);
|
console.log(`Create new creep ${new_creep_name}`);
|
||||||
} else {
|
} else {
|
||||||
// console.log(`failed to create transfer with code ${Re_code}`);
|
// console.log(`failed to create transfer with code ${Re_code}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.loop = function _() {
|
module.exports.loop = function _() {
|
||||||
extra();
|
extra();
|
||||||
init.init();
|
init.init();
|
||||||
mount.mount();
|
mount.mount();
|
||||||
cleanCreeps();
|
cleanCreeps();
|
||||||
cleanInvalidMemoryCreep();
|
cleanInvalidMemoryCreep();
|
||||||
cleanTargetMemory();
|
cleanTargetMemory();
|
||||||
assign_harvester_to_enery_source();
|
assign_harvester_to_enery_source();
|
||||||
assign_transfer_to_harvester();
|
assign_transfer_to_harvester();
|
||||||
assign_builder_to_controller();
|
assign_builder_to_controller();
|
||||||
assign_transfer_to_builder();
|
assign_transfer_to_builder();
|
||||||
const construction_site_array = Game.spawns.Spawn1.room.find(
|
const construction_site_array = Game.spawns.Spawn1.room.find(
|
||||||
FIND_MY_CONSTRUCTION_SITES
|
FIND_MY_CONSTRUCTION_SITES
|
||||||
);
|
);
|
||||||
_.each(construction_site_array, function (found_construction_site) {
|
_.each(construction_site_array, function (found_construction_site) {
|
||||||
const constuction_key = `CONSTRUCTION_${found_construction_site.id}`;
|
const constuction_key = `CONSTRUCTION_${found_construction_site.id}`;
|
||||||
if (
|
if (
|
||||||
Memory.WorkingTarget[constuction_key] &&
|
Memory.WorkingTarget[constuction_key] &&
|
||||||
Memory.WorkingTarget[constuction_key].working_creep_names.length === 0
|
Memory.WorkingTarget[constuction_key].working_creep_names.length ===
|
||||||
) {
|
0
|
||||||
assign_builder_to_construction(found_construction_site.id);
|
) {
|
||||||
|
assign_builder_to_construction(found_construction_site.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let isSpawning = false;
|
||||||
|
const energy_source_number = _.filter(
|
||||||
|
Game.spawns.Spawn1.room.find(FIND_SOURCES_ACTIVE)
|
||||||
|
).length;
|
||||||
|
const harvester_number = _(Game.creeps)
|
||||||
|
.filter({ memory: { role: "harvester" } })
|
||||||
|
.size();
|
||||||
|
if (!Game.spawns.Spawn1.spawning) {
|
||||||
|
if (harvester_number < energy_source_number) {
|
||||||
|
isSpawning = true;
|
||||||
|
createHarvestCreep();
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
_(Game.creeps)
|
||||||
|
.filter({ memory: { role: "transfer" } })
|
||||||
|
.size() < energy_source_number &&
|
||||||
|
!isSpawning
|
||||||
|
) {
|
||||||
|
isSpawning = true;
|
||||||
|
createTransferCreep();
|
||||||
|
}
|
||||||
|
const builder_number = _.filter(Game.creeps, {
|
||||||
|
memory: { role: "builder" },
|
||||||
|
}).length;
|
||||||
|
// 创造一个builder升级room controller
|
||||||
|
if (
|
||||||
|
Game.spawns.Spawn1.room.controller &&
|
||||||
|
builder_number < 1 + construction_site_array.length &&
|
||||||
|
!isSpawning
|
||||||
|
) {
|
||||||
|
isSpawning = true;
|
||||||
|
createBuilderCreep();
|
||||||
|
}
|
||||||
|
// 判断是否需要再增加1个transfer
|
||||||
|
const transfer_number = _.filter(Game.creeps, {
|
||||||
|
memory: { role: "transfer" },
|
||||||
|
}).length;
|
||||||
|
if (
|
||||||
|
builder_number + harvester_number > transfer_number &&
|
||||||
|
!isSpawning
|
||||||
|
) {
|
||||||
|
isSpawning = true;
|
||||||
|
createTransferCreep();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
assign_task();
|
||||||
let isSpawning = false;
|
|
||||||
const energy_source_number = _.filter(
|
|
||||||
Game.spawns.Spawn1.room.find(FIND_SOURCES_ACTIVE)
|
|
||||||
).length;
|
|
||||||
const harvester_number = _(Game.creeps)
|
|
||||||
.filter({ memory: { role: "harvester" } })
|
|
||||||
.size();
|
|
||||||
if (!Game.spawns.Spawn1.spawning) {
|
|
||||||
if (harvester_number < energy_source_number) {
|
|
||||||
isSpawning = true;
|
|
||||||
createHarvestCreep();
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
_(Game.creeps)
|
|
||||||
.filter({ memory: { role: "transfer" } })
|
|
||||||
.size() < energy_source_number &&
|
|
||||||
!isSpawning
|
|
||||||
) {
|
|
||||||
isSpawning = true;
|
|
||||||
createTransferCreep();
|
|
||||||
}
|
|
||||||
const builder_number = _.filter(Game.creeps, {
|
|
||||||
memory: { role: "builder" },
|
|
||||||
}).length;
|
|
||||||
// 创造一个builder升级room controller
|
|
||||||
if (
|
|
||||||
Game.spawns.Spawn1.room.controller &&
|
|
||||||
builder_number < 1 + construction_site_array.length &&
|
|
||||||
!isSpawning
|
|
||||||
) {
|
|
||||||
isSpawning = true;
|
|
||||||
createBuilderCreep();
|
|
||||||
}
|
|
||||||
// 判断是否需要再增加1个transfer
|
|
||||||
const transfer_number = _.filter(Game.creeps, {
|
|
||||||
memory: { role: "transfer" },
|
|
||||||
}).length;
|
|
||||||
if (builder_number + harvester_number > transfer_number && !isSpawning) {
|
|
||||||
isSpawning = true;
|
|
||||||
createTransferCreep();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assign_task();
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
68
mount.js
68
mount.js
|
|
@ -7,41 +7,41 @@ const transfer = require("./transfer");
|
||||||
const builder = require("./builder");
|
const builder = require("./builder");
|
||||||
|
|
||||||
function action() {
|
function action() {
|
||||||
const { role } = this.memory;
|
const { role } = this.memory;
|
||||||
// this.say(`${role}`);
|
// this.say(`${role}`);
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case "harvester":
|
case "harvester":
|
||||||
harvester.run(this);
|
harvester.run(this);
|
||||||
break;
|
break;
|
||||||
case "transfer":
|
case "transfer":
|
||||||
transfer.run(this);
|
transfer.run(this);
|
||||||
break;
|
break;
|
||||||
case "builder":
|
case "builder":
|
||||||
builder.run(this);
|
builder.run(this);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
mount() {
|
mount() {
|
||||||
Creep.prototype.action = action;
|
Creep.prototype.action = action;
|
||||||
if (!Creep.prototype.hasOwnProperty("working_target_id")) {
|
if (!Creep.prototype.hasOwnProperty("working_target_id")) {
|
||||||
Object.defineProperty(Creep.prototype, "working_target_id", {
|
Object.defineProperty(Creep.prototype, "working_target_id", {
|
||||||
get() {
|
get() {
|
||||||
return this.memory.working_target_id;
|
return this.memory.working_target_id;
|
||||||
},
|
},
|
||||||
set(id) {
|
set(id) {
|
||||||
this.memory.working_target_id = id;
|
this.memory.working_target_id = id;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Game.cwt = function (creep_name) {
|
Game.cwt = function (creep_name) {
|
||||||
Game.creeps[creep_name].working_target_id = undefined;
|
Game.creeps[creep_name].working_target_id = undefined;
|
||||||
};
|
};
|
||||||
Game.pos = function (id) {
|
Game.pos = function (id) {
|
||||||
console.log(Game.getObjectById(id));
|
console.log(Game.getObjectById(id));
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
2
path.js
2
path.js
|
|
@ -1,3 +1,3 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
visualPath(my_pos, target_pos) {},
|
visualPath(my_pos, target_pos) {},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
10
task.js
10
task.js
|
|
@ -1,9 +1,5 @@
|
||||||
const { memory } = require("console")
|
const { memory } = require("console");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
upgradeController(){
|
upgradeController() {},
|
||||||
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
4
test.js
4
test.js
|
|
@ -1,3 +1,3 @@
|
||||||
a= undefined || {a:'a'}
|
a = undefined || { a: "a" };
|
||||||
|
|
||||||
console.log(a)
|
console.log(a);
|
||||||
|
|
|
||||||
143
transfer.js
143
transfer.js
|
|
@ -7,77 +7,82 @@ const path = require("./path");
|
||||||
// const harvester = require("./harvester");
|
// const harvester = require("./harvester");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(creep) {
|
run(creep) {
|
||||||
creep.say("tf");
|
creep.say("tf");
|
||||||
const { working_target_id } = creep;
|
const { working_target_id } = creep;
|
||||||
if (!working_target_id) {
|
if (!working_target_id) {
|
||||||
creep.say("SB");
|
creep.say("SB");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const working_target = Game.getObjectById(working_target_id);
|
const working_target = Game.getObjectById(working_target_id);
|
||||||
// TODO:处理transfer的对象消失的情况
|
// TODO:处理transfer的对象消失的情况
|
||||||
if (!working_target) {
|
|
||||||
// _.each(Memery.WorkingTarget[creep.name].working_creep_names,creep_name=>Game.creeps[creep_name].working_target_id=undefined)
|
|
||||||
// delete Memery.WorkingTarget[creep.name]
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const working_target_pos = working_target.pos;
|
|
||||||
const my_pos = creep.pos;
|
|
||||||
if (!working_target.memory) {
|
|
||||||
console.log(
|
|
||||||
`check ${creep.name}, working target memory ${JSON.stringify(
|
|
||||||
working_target
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (working_target.memory.role === "harvester") {
|
|
||||||
if (creep.store.energy < creep.store.getCapacity()) {
|
|
||||||
if (!working_target) {
|
if (!working_target) {
|
||||||
// console.log(`${creep.name} working target vanished. Turn to unassigned.`)
|
// _.each(Memery.WorkingTarget[creep.name].working_creep_names,creep_name=>Game.creeps[creep_name].working_target_id=undefined)
|
||||||
return;
|
// delete Memery.WorkingTarget[creep.name]
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (utils.distance(working_target_pos, my_pos) > 1.5) {
|
const working_target_pos = working_target.pos;
|
||||||
// 在附近就是1或者1.414
|
const my_pos = creep.pos;
|
||||||
creep.moveTo(working_target_pos, {
|
if (!working_target.memory) {
|
||||||
visualizePathStyle: { stroke: "#ffffff" },
|
console.log(
|
||||||
});
|
`check ${creep.name}, working target memory ${JSON.stringify(
|
||||||
path.visualPath(creep.pos, working_target_pos);
|
working_target
|
||||||
} else {
|
)}`
|
||||||
// find dropped source
|
);
|
||||||
const found = creep.room.lookForAt(LOOK_ENERGY, working_target_pos);
|
return;
|
||||||
if (found.length > 0) {
|
|
||||||
creep.pickup(found[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (
|
if (working_target.memory.role === "harvester") {
|
||||||
creep.transfer(Game.spawns.Spawn1, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE
|
if (creep.store.energy < creep.store.getCapacity()) {
|
||||||
) {
|
if (!working_target) {
|
||||||
creep.moveTo(Game.spawns.Spawn1, {
|
// console.log(`${creep.name} working target vanished. Turn to unassigned.`)
|
||||||
visualizePathStyle: { stroke: "#ffffff" },
|
return;
|
||||||
});
|
}
|
||||||
path.visualPath(creep.pos, Game.spawns.Spawn1.pos);
|
if (utils.distance(working_target_pos, my_pos) > 1.5) {
|
||||||
}
|
// 在附近就是1或者1.414
|
||||||
}
|
creep.moveTo(working_target_pos, {
|
||||||
if (working_target.memory.role === "builder") {
|
visualizePathStyle: { stroke: "#ffffff" },
|
||||||
if (creep.store.energy > 0) {
|
});
|
||||||
if (utils.distance(working_target_pos, my_pos) > 1.5) {
|
path.visualPath(creep.pos, working_target_pos);
|
||||||
// 在附近就是1或者1.414
|
} else {
|
||||||
creep.moveTo(working_target_pos, {
|
// find dropped source
|
||||||
visualizePathStyle: { stroke: "#ffffff" },
|
const found = creep.room.lookForAt(
|
||||||
});
|
LOOK_ENERGY,
|
||||||
path.visualPath(creep.pos, working_target_pos);
|
working_target_pos
|
||||||
} else {
|
);
|
||||||
creep.drop(RESOURCE_ENERGY);
|
if (found.length > 0) {
|
||||||
|
creep.pickup(found[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
creep.transfer(Game.spawns.Spawn1, RESOURCE_ENERGY) ===
|
||||||
|
ERR_NOT_IN_RANGE
|
||||||
|
) {
|
||||||
|
creep.moveTo(Game.spawns.Spawn1, {
|
||||||
|
visualizePathStyle: { stroke: "#ffffff" },
|
||||||
|
});
|
||||||
|
path.visualPath(creep.pos, Game.spawns.Spawn1.pos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (
|
if (working_target.memory.role === "builder") {
|
||||||
creep.withdraw(Game.spawns.Spawn1, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE
|
if (creep.store.energy > 0) {
|
||||||
) {
|
if (utils.distance(working_target_pos, my_pos) > 1.5) {
|
||||||
creep.moveTo(Game.spawns.Spawn1, {
|
// 在附近就是1或者1.414
|
||||||
visualizePathStyle: { stroke: "#ffffff" },
|
creep.moveTo(working_target_pos, {
|
||||||
});
|
visualizePathStyle: { stroke: "#ffffff" },
|
||||||
path.visualPath(creep.pos, Game.spawns.Spawn1.pos);
|
});
|
||||||
}
|
path.visualPath(creep.pos, working_target_pos);
|
||||||
}
|
} else {
|
||||||
},
|
creep.drop(RESOURCE_ENERGY);
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
creep.withdraw(Game.spawns.Spawn1, RESOURCE_ENERGY) ===
|
||||||
|
ERR_NOT_IN_RANGE
|
||||||
|
) {
|
||||||
|
creep.moveTo(Game.spawns.Spawn1, {
|
||||||
|
visualizePathStyle: { stroke: "#ffffff" },
|
||||||
|
});
|
||||||
|
path.visualPath(creep.pos, Game.spawns.Spawn1.pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
42
utils.js
42
utils.js
|
|
@ -1,23 +1,23 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
uuid() {
|
uuid() {
|
||||||
// 6位
|
// 6位
|
||||||
let uuid = "";
|
let uuid = "";
|
||||||
for (let i = 0; i < 6; i += 1) {
|
for (let i = 0; i < 6; i += 1) {
|
||||||
if (Math.random() > 0.5) {
|
if (Math.random() > 0.5) {
|
||||||
// 大于0.5就是数字,小于则是字母
|
// 大于0.5就是数字,小于则是字母
|
||||||
uuid += Math.round(Math.random() * 100) % 10;
|
uuid += Math.round(Math.random() * 100) % 10;
|
||||||
} else {
|
} else {
|
||||||
uuid += String.fromCharCode(
|
uuid += String.fromCharCode(
|
||||||
97 + (Math.round(Math.random() * 100) % 26)
|
97 + (Math.round(Math.random() * 100) % 26)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return uuid;
|
return uuid;
|
||||||
},
|
},
|
||||||
distance(pos1, pos2) {
|
distance(pos1, pos2) {
|
||||||
if (pos1.roomName === pos2.roomName) {
|
if (pos1.roomName === pos2.roomName) {
|
||||||
return Math.sqrt((pos1.x - pos2.x, 2) ** 2);
|
return Math.sqrt((pos1.x - pos2.x, 2) ** 2);
|
||||||
}
|
}
|
||||||
return -1; // 如果不在一个房间内,就返回负数。
|
return -1; // 如果不在一个房间内,就返回负数。
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue