screeps/role.builder.js

64 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-06-06 13:17:26 -05:00
let buildparts=require('bodypartbuilder')
2016-09-28 08:37:28 -05:00
let roleBuilder = {
2016-09-23 00:12:42 -05:00
run: function(creep) {
2016-09-28 08:37:28 -05:00
if(creep.memory.originroom === undefined){
creep.memory.originroom = creep.room.name
}
2016-09-15 00:30:24 -05:00
if(creep.memory.building && creep.carry.energy == 0) {
creep.memory.building = false;
creep.say('Gathering');
}
if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
creep.memory.building = true;
creep.say('building');
}
if(creep.memory.building) {
2017-06-06 13:17:26 -05:00
2016-09-28 08:37:28 -05:00
if(creep.memory.buildsite==undefined|| Game.getObjectById(creep.memory.buildsite.id) == undefined){
creep.memory.buildsite = Game.rooms[creep.memory.originroom].find(FIND_CONSTRUCTION_SITES)[0];
}
let target = Game.getObjectById(creep.memory.buildsite.id)
if(target) {
2017-06-06 13:17:26 -05:00
if(creep.room.memory.containerstoragepercent > creep.room.memory.minbuildpct || creep.room.memory.containerstoragepercent === undefined ){
2016-09-28 08:37:28 -05:00
if(creep.build(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
if(creep.fatigue<1){
creep.say("MTCS");
} else { creep.say("Tired")}
}
2017-06-06 13:17:26 -05:00
}else {
creep.say(creep.room.memory.containerstoragepercent + ' parking1')
creep.moveTo(creep.room.memory.builderparkx,creep.room.memory.builderparky,creep.room.roomName)
}
2016-09-28 08:37:28 -05:00
} else {
2017-06-06 13:17:26 -05:00
creep.say('parking2')
creep.moveTo(creep.room.memory.builderparkx,creep.room.memory.builderparky,creep.room.roomName)
}
2016-09-15 00:30:24 -05:00
}
else {
2016-09-28 08:37:28 -05:00
let containers =Game.rooms[creep.memory.originroom].find(FIND_STRUCTURES, {
2017-06-06 13:17:26 -05:00
filter: (structure) => {return ((structure.structureType == STRUCTURE_CONTAINER||structure.structureType == STRUCTURE_TERMINAL || structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;}});
2016-09-28 08:37:28 -05:00
let allcontainers = creep.room.find(FIND_STRUCTURES, {
2016-09-15 00:30:24 -05:00
filter: (structure) => {
2017-06-06 13:17:26 -05:00
return (structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE ) ;
2016-09-15 00:30:24 -05:00
}});
2017-06-06 13:17:26 -05:00
let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: {resourceType: RESOURCE_ENERGY}});
//find(FIND_DROPPED_RESOURCES, {filter: r => r.resourceType === RESOURCE_ENERGY}) is another option
if(allcontainers.length==0){
2016-09-23 00:12:42 -05:00
if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) {
2016-09-15 00:30:24 -05:00
creep.say("MTDE");
2016-09-23 00:12:42 -05:00
creep.moveTo(droppedenergy);
}
} else {
if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
2016-09-15 00:30:24 -05:00
creep.say("MTSC");
creep.moveTo(containers[0]);
}
2016-09-23 00:12:42 -05:00
2016-09-15 00:30:24 -05:00
}
}
2016-09-23 00:12:42 -05:00
}
2016-09-15 00:30:24 -05:00
};
module.exports = roleBuilder;