screeps/role.phase1worker.js

142 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-11-09 19:31:39 -06:00
//'use strict';
let Phase1Worker = {
run: function(creep) {
let ignorecreeps=true
if(creep.memory.lastpos==undefined || creep.memory.timeatpos==undefined){
creep.memory.lastpos = creep.pos
creep.memory.timeatpos = 0
}
2020-11-10 01:29:27 -06:00
if(creep.memory.assignedroom==undefined){
creep.memory.assignedroom=creep.room.name
}
2020-11-09 19:31:39 -06:00
let lastpos = _.create(RoomPosition.prototype, creep.memory.lastpos)
if(creep.pos.isEqualTo(lastpos)){
creep.memory.timeatpos = creep.memory.timeatpos+1
if(creep.memory.timeatpos>2){
//console.log(creep.name + " " + creep.memory.timeatpos)
ignorecreeps=false
} else { }
} else { creep.memory.timeatpos=0}
let road = creep.pos.lookFor(LOOK_STRUCTURES);
let filllevel = _.sum(creep.carry)
if(creep.memory.working && filllevel == 0) {
creep.memory.working = false;
creep.say('Gathering');
}
if(!creep.memory.working && creep.carry.energy == creep.carryCapacity) {
creep.memory.working = true;
creep.say('working');
2020-11-14 01:18:05 -06:00
}
2020-11-09 19:31:39 -06:00
if(creep.memory.working){
let look=creep.pos.lookFor(LOOK_STRUCTURES)
2022-05-04 08:30:12 -05:00
let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_TERMINAL ) && _.sum(s.store) < s.store.getCapacity()) ;}});
2020-11-09 19:31:39 -06:00
if(creep.room.memory.NeedsRecharge==1){
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
2022-04-26 23:41:43 -05:00
creep.say(creep.room.controller.ticksToDowngrade + " of " + CONTROLLER_DOWNGRADE[creep.room.controller.level] * .2)
2020-11-09 19:31:39 -06:00
creep.moveTo(creep.room.controller)
}
2020-11-14 01:18:05 -06:00
} else if((creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_SPAWN, STRUCTURE_EXTENSION, STRUCTURE_TOWER].includes(s.structureType)) && s.energy < s.energyCapacity))}}))&& (creep.room.memory.movercount<1||creep.room.memory.movercount==undefined)){
2020-11-09 19:31:39 -06:00
let spawntarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (((s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_TOWER) && s.energy < s.energyCapacity))}});
if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(spawntarget,{ignoreCreeps:ignorecreeps})
}
} else if(creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return ((([STRUCTURE_RAMPART].includes(s.structureType)) && s.hits < 20000))}})){
let ramparttarget = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => {return (((s.structureType == STRUCTURE_RAMPART) && s.hits < s.hitsMax))}});
if(creep.repair(ramparttarget) == ERR_NOT_IN_RANGE) {
creep.moveTo(ramparttarget,{ignoreCreeps:ignorecreeps})
}
} else if(creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES)){
target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES)
if(creep.build(target) == ERR_NOT_IN_RANGE) {
if (road.length > 0) {creep.repair(road);}
creep.moveTo(target,{ignoreCreeps:ignorecreeps})
}
} else if(storagetargets && creep.room.controller.level*125000> creep.room.storage.store.getUsedCapacity('energy')){
2020-11-09 19:31:39 -06:00
if(creep.transfer(storagetargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
if (road.length > 0) {creep.repair(road);}
creep.moveTo(storagetargets,{ignoreCreeps:ignorecreeps})
}
}else {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.say('MTRC')
2020-11-10 01:29:27 -06:00
if (road.length > 0) {creep.repair(road);}
creep.moveTo(Game.rooms[creep.memory.assignedroom].controller,{ignoreCreeps:ignorecreeps})
2020-11-09 19:31:39 -06:00
}
}
2022-04-26 23:41:43 -05:00
} else if ((creep.ticksToLive < 300 || creep.ticksToLive <= creep.memory.renewto) && (Game.rooms[creep.room.name].find(FIND_MY_SPAWNS, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}})) ) {
2020-11-17 10:30:13 -06:00
if(creep.memory.renewto == undefined){
creep.memory.renewto = 1200
} else {
if(creep.ticksToLive >= creep.memory.renewto){
delete creep.memory.renewto
}
}
creep.say('renewing')
console.log(creep.name + ": " + creep.ticksToLive + " " + creep.memory.renewto)
let spawn = creep.pos.findClosestByRange(FIND_MY_SPAWNS)
2020-11-30 03:41:26 -06:00
if(creep.room.memory.NeedsRecharge==1 && creep.carry.energy>0){
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
2022-04-26 23:41:43 -05:00
creep.say(creep.room.controller.ticksToDowngrade + " of " + (CONTROLLER_DOWNGRADE[creep.room.controller.level] * .2))
2020-11-30 03:41:26 -06:00
creep.moveTo(creep.room.controller)
}
} else {
if(spawn.renewCreep(creep) == ERR_NOT_IN_RANGE){
creep.moveTo(spawn);
}
2020-11-17 10:30:13 -06:00
}
2020-11-09 19:31:39 -06:00
} else {
let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: (r) =>{return ( r.resourceType==RESOURCE_ENERGY&& r.amount>200)}});
2020-11-14 01:18:05 -06:00
let tombstone = creep.pos.findClosestByRange(FIND_TOMBSTONES, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>200)}});
//console.log(tombstones)
if((droppedenergy == undefined) && (tombstone==undefined)){
2020-11-09 19:31:39 -06:00
if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined}
let mysource=Game.getObjectById(creep.memory.destsource.id)
2022-05-04 08:30:12 -05:00
try{
if(mysource.energy==0){
//creep.say("No Energy Left to get")
let storagetarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_CONTAINER) && s.store.getUsedCapacity('energy') >= 1000) ;}});
if(storagetarget===undefined){
storagetarget=creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE) && s.store.getUsedCapacity('energy') >= 100000) ;}});
if(storagetarget==undefined && creep.room.terminal && creep.room.terminal.store.getUsedCapacity('energy') > 100000){
storagetarget=creep.room.terminal
}
}
if(storagetarget != undefined && creep.withdraw(storagetarget,RESOURCE_ENERGY)== ERR_NOT_IN_RANGE) {
//creep.say('Getting Energy')
creep.travelTo(storagetarget);
}
}else {
if(creep.harvest(mysource) == ERR_NOT_IN_RANGE){
creep.moveTo(mysource,{ignoreCreeps:ignorecreeps})
}
}
} catch(e){
console.log(e)
2020-11-09 19:31:39 -06:00
}
} else {
2020-11-14 01:18:05 -06:00
if(droppedenergy){
if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) {
if(global.verbosity>0){
creep.say("MTDE");
}
creep.moveTo(droppedenergy,{ignoreCreeps:ignorecreeps})
}
} else {
if(creep.withdraw(tombstone,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
if(global.verbosity>0){
creep.say("MTTS");
}
creep.moveTo(tombstone.pos,{ignoreCreeps:ignorecreeps})
2020-11-09 19:31:39 -06:00
}
2020-11-14 01:18:05 -06:00
}
2020-11-09 19:31:39 -06:00
}
}
creep.memory.lastpos=creep.pos
}
};
module.exports = Phase1Worker;