89 lines
3.3 KiB
JavaScript
89 lines
3.3 KiB
JavaScript
/* eslint-disable no-undef */
|
|
/* eslint-disable no-console */
|
|
/* eslint-disable camelcase */
|
|
const utils = require("./utils");
|
|
const path = require("./path");
|
|
|
|
// const harvester = require("./harvester");
|
|
|
|
module.exports = {
|
|
run(creep) {
|
|
creep.say("tf");
|
|
const { working_target_id } = creep;
|
|
if (!working_target_id) {
|
|
creep.say("SB");
|
|
return;
|
|
}
|
|
const working_target = Game.getObjectById(working_target_id);
|
|
// 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) {
|
|
// console.log(`${creep.name} working target vanished. Turn to unassigned.`)
|
|
return;
|
|
}
|
|
if (utils.distance(working_target_pos, my_pos) > 1.5) {
|
|
// 在附近就是1或者1.414
|
|
creep.moveTo(working_target_pos, {
|
|
visualizePathStyle: { stroke: "#ffffff" },
|
|
});
|
|
path.visualPath(creep.pos, working_target_pos);
|
|
} else {
|
|
// find dropped source
|
|
const found = creep.room.lookForAt(
|
|
LOOK_ENERGY,
|
|
working_target_pos
|
|
);
|
|
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);
|
|
}
|
|
}
|
|
if (working_target.memory.role === "builder") {
|
|
if (creep.store.energy > 0) {
|
|
if (utils.distance(working_target_pos, my_pos) > 1.5) {
|
|
// 在附近就是1或者1.414
|
|
creep.moveTo(working_target_pos, {
|
|
visualizePathStyle: { stroke: "#ffffff" },
|
|
});
|
|
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);
|
|
}
|
|
}
|
|
},
|
|
};
|