From 50d648af02abf38bc60cab5fcf0ced83e739a2a4 Mon Sep 17 00:00:00 2001 From: paradizelost Date: Thu, 15 Sep 2016 00:30:24 -0500 Subject: [PATCH] Initial Commit --- Spawnloop.js | 40 ++++++ bodypartbuilder.js | 4 + energyinfo.js | 69 +++++++++++ main.js | 88 ++++++++++++++ role.builder.js | 77 ++++++++++++ role.harvester.js | 50 ++++++++ role.harvester2.js | 49 ++++++++ role.hauler.js | 46 +++++++ role.hauler2.js | 42 +++++++ role.miner.js | 24 ++++ role.miner2.js | 22 ++++ role.towerrecharger.js | 42 +++++++ role.upgrader.js | 53 ++++++++ role.warrior.js | 29 +++++ totest.js | 270 +++++++++++++++++++++++++++++++++++++++++ tower.js | 57 +++++++++ 16 files changed, 962 insertions(+) create mode 100644 Spawnloop.js create mode 100644 bodypartbuilder.js create mode 100644 energyinfo.js create mode 100644 main.js create mode 100644 role.builder.js create mode 100644 role.harvester.js create mode 100644 role.harvester2.js create mode 100644 role.hauler.js create mode 100644 role.hauler2.js create mode 100644 role.miner.js create mode 100644 role.miner2.js create mode 100644 role.towerrecharger.js create mode 100644 role.upgrader.js create mode 100644 role.warrior.js create mode 100644 totest.js create mode 100644 tower.js diff --git a/Spawnloop.js b/Spawnloop.js new file mode 100644 index 0000000..7174c2d --- /dev/null +++ b/Spawnloop.js @@ -0,0 +1,40 @@ +Object.defineProperty(Room.prototype, "spawns", { + /** + * Holds array of spawns. If you do not own the room, returns ERR_NOT_OWNER + * + * @returns {number|ERR_NOT_OWNER|object} + */ + configurable: true, + get: function () { + if (!this.controller || !this.controller.my) { + return ERR_NOT_OWNER; + } + let spawnObjects = []; + if (!this.memory.spawns) { + let spawns = []; + spawnObjects = this.find(FIND_MY_STRUCTURES, { + filter: { + structureType: STRUCTURE_SPAWN + } + }); + spawnObjects.forEach( + function (spawn) { + spawns.push(spawn.id) + }); + this.memory.spawns = spawns; + } + if (spawnObjects.length == 0 && this.memory.spawns.length > 0) { + for (let key in this.memory.spawns) { + let spawnId = this.memory.spawns[key]; + let spawn = Game.getObjectById(spawnId); + if (!spawn) { + this.log('No spawn found for ID ' + spawnId + ' despite being in cache, cleaning.', WARNING, true); + delete this.memory.spawns; + } else { + spawnObjects.push(spawn); + } + } + } + return spawnObjects; + } +}); \ No newline at end of file diff --git a/bodypartbuilder.js b/bodypartbuilder.js new file mode 100644 index 0000000..e3d7f02 --- /dev/null +++ b/bodypartbuilder.js @@ -0,0 +1,4 @@ +bodyParts.forEach(function (bodyPart) { + var part = typeof bodyPart === 'string' ? bodyPart : bodyPart.type; + cost += BODYPART_COST[part]; + }); \ No newline at end of file diff --git a/energyinfo.js b/energyinfo.js new file mode 100644 index 0000000..d41af68 --- /dev/null +++ b/energyinfo.js @@ -0,0 +1,69 @@ +import {Util} from "../Util"; + +/** + * @Serializable + */ +export class EnergyInformation { + + energy: number; + energyCapacity: number; + availableForDistribution: boolean; + + static isFull(energyInformation: EnergyInformation): boolean { + return energyInformation.energy === energyInformation.energyCapacity; + } + + static isEmpty(energyInformation: EnergyInformation): boolean { + return energyInformation.energy === 0; + } + + /** + * @param structure + * @returns {EnergyInformation} if structure can hold energy + */ + static ofStructure(structure: Structure): EnergyInformation|undefined { + var storedEnergy = 0; + var energyCapacity = 0; + var energyAvailableForDistribution: boolean; + + switch (structure.structureType) { + case STRUCTURE_EXTENSION: + energyAvailableForDistribution = false; + storedEnergy = ( structure).energy; + energyCapacity = ( structure).energyCapacity; + break; + case STRUCTURE_CONTAINER: + case STRUCTURE_STORAGE: + // both, for StructureStorage and StructureContainer use *StructureStorage* + energyAvailableForDistribution = true; + var store = ( structure).store; + storedEnergy = store[RESOURCE_ENERGY] || 0; + var allStorageUnits: number = _.sum(store); + energyCapacity = ( structure).storeCapacity - (allStorageUnits - storedEnergy); + break; + case STRUCTURE_LINK: + energyAvailableForDistribution = true; + storedEnergy = ( structure).energy; + energyCapacity = ( structure).energyCapacity; + break; + case STRUCTURE_SPAWN: + energyAvailableForDistribution = false; + storedEnergy = ( structure).energy; + energyCapacity = ( structure).energyCapacity; + break; + case STRUCTURE_TOWER: + energyAvailableForDistribution = false; + storedEnergy = ( structure).energy; + energyCapacity = ( structure).energyCapacity; + break; + default: + return undefined; + } + + var energyInformation = new EnergyInformation(); + energyInformation.energy = storedEnergy; + energyInformation.energyCapacity = energyCapacity; + energyInformation.availableForDistribution = energyAvailableForDistribution; + return energyInformation; + } +} \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..f01e025 --- /dev/null +++ b/main.js @@ -0,0 +1,88 @@ +var roleHarvester = require('role.harvester'); +var roleHarvester2 = require('role.harvester2'); +var roleUpgrader = require('role.upgrader'); +var roleBuilder = require('role.builder'); +var roleMiner = require('role.miner'); +var roleMiner2 = require('role.miner2'); +var roleHauler = require('role.hauler'); +var roleHauler2 = require('role.hauler2'); +var roleWarrior = require('role.warrior'); +var runTower = require('tower'); +var roleTowerrecharger = require('role.towerrecharger'); +var purgetype="NONE" +var clearflags=0 +module.exports.loop = function () { + + if(purgetype != "NONE"){ _(Game.creeps).filter(c=>c.memory.role == purgetype).forEach(c=>c.suicide()).value() } + + runTower.tick(); + if(clearflags>0){ + console.log("Clearing Flags"); + _.forEach(Game.flags, function(flag) {flag.remove()}); + } + console.log("--------") + for(var name in Game.rooms) { + if(Game.rooms[name].memory.maxbuilders == undefined){ Game.rooms[name].memory.maxbuilders =4 } + if(Game.rooms[name].memory.maxupgraders == undefined){ Game.rooms[name].memory.maxupgraders = 3 } + if(Game.rooms[name].memory.maxminers == undefined){ Game.rooms[name].memory.maxminers = 1 } + if(Game.rooms[name].memory.maxminer2s == undefined){ Game.rooms[name].memory.maxminer2s = 1 } + if(Game.rooms[name].memory.maxhaulers == undefined){ Game.rooms[name].memory.maxhaulers = 2 } + if(Game.rooms[name].memory.maxhauler2s == undefined){ Game.rooms[name].memory.maxhauler2s = 1 } + if(Game.rooms[name].memory.maxwarriors == undefined){ Game.rooms[name].memory.maxwarriors = 2 } + if(Game.rooms[name].memory.maxtowerrechargers == undefined){ Game.rooms[name].memory.maxtowerrechargers = 3 } + if(Game.rooms[name].memory.maxharvesters == undefined){ Game.rooms[name].memory.maxharvesters = 0 } + if(Game.rooms[name].memory.maxharvester2s == undefined){ Game.rooms[name].memory.maxharvester2s = 0 } + + console.log('Room "'+name+'" has ' + Game.rooms[name].energyAvailable + ' energy'); + } + for(var name in Memory.creeps) { + if(!Game.creeps[name]) { + delete Memory.creeps[name]; + console.log('Clearing non-existing creep memory:', name); + } + } + + roleMiner.spawn() + roleMiner2.spawn() + roleHarvester.spawn() + roleHarvester2.spawn() + roleBuilder.spawn() + roleUpgrader.spawn() + roleHauler.spawn() + roleHauler2.spawn() + roleTowerrecharger.spawn() + roleWarrior.spawn() + for(var name in Game.creeps) { + var creep = Game.creeps[name]; + if(creep.memory.role == 'harvester') { + roleHarvester.run(creep); + } + if(creep.memory.role == 'harvester2') { + roleHarvester2.run(creep); + } + if(creep.memory.role == 'upgrader') { + roleUpgrader.run(creep); + } + if(creep.memory.role == 'builder') { + roleBuilder.run(creep); + } + if(creep.memory.role == 'miner') { + roleMiner.run(creep); + } + if(creep.memory.role == 'miner2') { + roleMiner2.run(creep); + } + if(creep.memory.role == 'hauler'){ + roleHauler.run(creep) + } + if(creep.memory.role == 'hauler2'){ + roleHauler2.run(creep) + } + if(creep.memory.role == 'warrior'){ + roleWarrior.run(creep) + } + if(creep.memory.role == 'towerrecharger'){ + roleTowerrecharger.run(creep) + } + } +} \ No newline at end of file diff --git a/role.builder.js b/role.builder.js new file mode 100644 index 0000000..4b36d2b --- /dev/null +++ b/role.builder.js @@ -0,0 +1,77 @@ +var roleBuilder = { + + /** @param {Creep} creep **/ + run: function(creep,mysource) { + + var ttl = creep.ticksToLive + if(ttl < 300) { + console.log(creep.name + ': ' + ttl + ' - ' + creep.memory.role ) + } + if(ttl < 20) { + roleBuilder.spawn("true") + } + 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) { + var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES); + if(target != undefined) { + if(creep.build(target) == ERR_NOT_IN_RANGE) { + creep.moveTo(target); + if(creep.fatigue<1){ + creep.say("MTCS"); + } else { creep.say("Tired")} + } + } else { + + creep.say("MTF") + creep.moveTo(Game.flags.Flag2); + } + } + else { + var containers = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 0) ; + }}); + var allcontainers = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER ) ; + }}); + var sources = creep.room.find(FIND_SOURCES); + var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); + + + if(allcontainers.length<1){ + if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) { + creep.say("MTDE"); + creep.moveTo(droppedenergy[0]); + } + } else{ + if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say("MTSC"); + creep.moveTo(containers[0]); + } + } + } + }, + spawn: function(dyingcreep="false"){ + + var myrole='builder'; + var nummyrole=4; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole || dyingcreep=="true" ) { + if(dyingcreep=="true"){console.log(myrole + " dying. preventative spawn")} + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +}; + +module.exports = roleBuilder; \ No newline at end of file diff --git a/role.harvester.js b/role.harvester.js new file mode 100644 index 0000000..435d0e2 --- /dev/null +++ b/role.harvester.js @@ -0,0 +1,50 @@ +var roleHarvester = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.carry.energy < creep.carryCapacity) { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0]); + } + } + else { + var spawntargets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) + /**|| (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) **/ ; + } + + }); + var containertargets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) ; + } + + }); + if(spawntargets.length > 0) { + if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntargets[0]); + } + } else if (containertargets.length > 0) { + if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(containertargets[0]); + } + }else { + creep.moveTo(Game.flags.Flag1); + } + } + }, + spawn: function(){ + var myrole='harvester'; + var nummyrole=0; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +}; + +module.exports = roleHarvester; \ No newline at end of file diff --git a/role.harvester2.js b/role.harvester2.js new file mode 100644 index 0000000..812c390 --- /dev/null +++ b/role.harvester2.js @@ -0,0 +1,49 @@ +var roleHarvester2 = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.carry.energy < creep.carryCapacity) { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[1]); + } + } + else { + var spawntargets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) + } + + }); + var containertargets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) ; + } + + }); + if(spawntargets.length > 0) { + if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntargets[0]); + } + } else if (containertargets.length > 0) { + if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(containertargets[0]); + } + }else { + creep.moveTo(Game.flags.Flag1); + } + } + }, + spawn: function(){ + var myrole='harvester2'; + var nummyrole=0; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +}; + +module.exports = roleHarvester2; \ No newline at end of file diff --git a/role.hauler.js b/role.hauler.js new file mode 100644 index 0000000..151a224 --- /dev/null +++ b/role.hauler.js @@ -0,0 +1,46 @@ +var roleHauler = { + run: function(creep) { + var sources = creep.room.find(FIND_DROPPED_ENERGY ); + if(creep.pickup(sources[0]) == ERR_NOT_IN_RANGE && creep.carryCapacity/2 > creep.carry.energy) { + creep.moveTo(sources[0]); + } else if(sources != undefined ) + { var spawntargets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) + } + }); + var containertargets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) ; + } + + }); + if(spawntargets.length > 0) { + if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(spawntargets[0]); + } + } else if (containertargets.length > 0) { + if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(containertargets[0]); + } + }else { + creep.say("") + creep.moveTo(Game.flags.Flag1); + } + }else{ + creep.say("NS,NT") + creep.moveTo(Game.flags.Flag1); + } + }, + spawn: function(){ + var myrole='hauler'; + var nummyrole=2; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +}; +module.exports = roleHauler; \ No newline at end of file diff --git a/role.hauler2.js b/role.hauler2.js new file mode 100644 index 0000000..a69d59d --- /dev/null +++ b/role.hauler2.js @@ -0,0 +1,42 @@ + var roleHauler2 = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.carryCapacity > creep.carry.energy){ + var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 0) ; + }}); + if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say("MTSC"); + creep.moveTo(container); + } + } else { + var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) + } + }); + if(spawntarget != undefined) { + if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say('refilling') + creep.moveTo(spawntarget); + } + } else { + creep.say('MTF') + creep.moveTo(Game.flags.Flag1); + } + } + }, + spawn: function(){ + var myrole='hauler2'; + var nummyrole=1; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([MOVE,CARRY], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } + }; + module.exports = roleHauler2; \ No newline at end of file diff --git a/role.miner.js b/role.miner.js new file mode 100644 index 0000000..f5c4fb8 --- /dev/null +++ b/role.miner.js @@ -0,0 +1,24 @@ +var roleMiner = { + + /** @param {Creep} creep **/ + run: function(creep,mysource) { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0]); + } + }, + spawn: function(){ + var myrole='miner'; + var nummyrole=1; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + + console.log('Miners: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } + +}; + +module.exports = roleMiner; \ No newline at end of file diff --git a/role.miner2.js b/role.miner2.js new file mode 100644 index 0000000..1765a58 --- /dev/null +++ b/role.miner2.js @@ -0,0 +1,22 @@ +var roleMiner2 = { + + /** @param {Creep} creep **/ + run: function(creep) { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[1]); + } + }, + spawn: function(){ + var myrole='miner2'; + var nummyrole=1; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log('Miners: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +}; + +module.exports = roleMiner2; \ No newline at end of file diff --git a/role.towerrecharger.js b/role.towerrecharger.js new file mode 100644 index 0000000..0eaa615 --- /dev/null +++ b/role.towerrecharger.js @@ -0,0 +1,42 @@ + var roleTowerrecharger = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.carryCapacity > creep.carry.energy){ + var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 1000) ; + }}); + if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say("MTSC"); + creep.moveTo(container); + } + } else { + var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_TOWER && structure.energy < structure.energyCapacity) + } + }); + if(spawntarget != undefined) { + if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.say('refilling') + creep.moveTo(spawntarget); + } + } else { + creep.say('MTF') + creep.moveTo(Game.flags.Flag1); + } + } + }, + spawn: function(){ + var myrole='towerrecharger'; + var nummyrole=2; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } + }; + module.exports = roleTowerrecharger; \ No newline at end of file diff --git a/role.upgrader.js b/role.upgrader.js new file mode 100644 index 0000000..3933600 --- /dev/null +++ b/role.upgrader.js @@ -0,0 +1,53 @@ +var roleUpgrader = { + + /** @param {Creep} creep **/ + run: function(creep) { + + if(creep.memory.upgrading && creep.carry.energy == 0) { + creep.memory.upgrading = false; + creep.say('harvesting'); + } + if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) { + creep.memory.upgrading = true; + creep.say('upgrading'); + } + + if(creep.memory.upgrading) { + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller); + } + } + else { + var containers = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 0) ; + }}); + var allcontainers = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER) ; + }}); + var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); + if(allcontainers.length<0){ + if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(droppedenergy[0]); + } + } else{ + if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(containers[0]); + } + } + } + }, + spawn: function(){ + var myrole='upgrader'; + var nummyrole=3; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,MOVE,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +}; + +module.exports = roleUpgrader; \ No newline at end of file diff --git a/role.warrior.js b/role.warrior.js new file mode 100644 index 0000000..be1b54e --- /dev/null +++ b/role.warrior.js @@ -0,0 +1,29 @@ +var roleWarrior = { + + /** @param {Creep} creep **/ + run: function(creep) { + var hostile = creep.pos.findClosestByRange(Game.HOSTILE_CREEPS); + + if(hostile!=undefined) { + var username = hostile.owner.username; + Game.notify(`User ${username} spotted in room ${roomName}`); + creep.moveTo(hostile); + creep.attack(hostile); + + } else{ + creep.moveTo(Game.flags.Flag3); + } + }, + spawn: function(){ + var myrole='warrior'; + var nummyrole=2; + var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); + if(myroles.length < nummyrole) { + console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); + var newName = Game.spawns['Spawn1'].createCreep([ATTACK,RANGED_ATTACK,MOVE], undefined, {role: myrole}); + console.log('Spawning new ' + myrole + ': ' + newName); + } + } +} + +module.exports = roleWarrior; \ No newline at end of file diff --git a/totest.js b/totest.js new file mode 100644 index 0000000..80d6835 --- /dev/null +++ b/totest.js @@ -0,0 +1,270 @@ +let roleHarvester = require('role.harvester'); +let roleBuilder = require('role.builder'); +let roleRepairer = require('role.repairer'); +let roleDistributor = require('role.distributor'); +let roleUpgrader = require('role.upgrader'); +let roleClaimer = require('role.claimer'); +let roleMiner = require("role.miner"); + +let roleDefender = require('role.defender'); + +let structureTower = require('structure.tower'); +let structureLink = require('structure.link'); + +const FLAG_PRIORITIES = { 'Flag1': 100, 'Flag2': 70, 'Flag3': 80, 'Flag4': 75, 'Flag5': 100}; +const ROLE_PRIORITIES = { 'Claimers': 100, 'Harvesters': 95, 'Distributors': 94, 'Repairers': 85, 'Builders': 80, 'Upgraders': 75, 'Defenders': 90 , 'Miners' : 85 }; + +module.exports.loop = function () { + let spawnStack = new Array(); + + cleanUpCreepsFromMemory(); + + for(let room in Memory.myRooms){ + let roomBuf = Memory.myRooms[room]; + let spawnsInRoom = Game.rooms[room].find(FIND_MY_STRUCTURES, { filter: (s) => s.structureType == STRUCTURE_SPAWN }); + + structureLink.run();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + for(let tower in Game.rooms[room].find(FIND_MY_STRUCTURES, {filter: s => s.structureType == STRUCTURE_TOWER})) + structureTower.run(Game.rooms[room].find(FIND_MY_STRUCTURES, {filter: s => s.structureType == STRUCTURE_TOWER})[tower]); + + for(let flag in roomBuf){ + let flagBuf = roomBuf[flag]; + + for(let role in flagBuf){ + let roleBuf = flagBuf[role]; + + while(roleBuf.creeps.length < roleBuf.amountToSpawn){ + roleBuf.creeps.push("undefined"); + } + + for(let i = roleBuf.creeps.length-1; i > 0; i--){ + if(i >= roleBuf.amountToSpawn && !Game.creeps[roleBuf.creeps[i]]){ + roleBuf.creeps.pop(); + }else { + break; + } + } + + for(let creep in roleBuf.creeps){ + let creepBuf = roleBuf.creeps[creep]; + + if(!Game.creeps[creepBuf]){ + addToSpawnStack(spawnStack, room, flag, role, creep); + }else { + var start = Game.cpu.getUsed(); + orderCreep(room, flag, role, creepBuf, creep); + var end = Game.cpu.getUsed(); + //console.log(end-start + " " + flag + " " + role); + + let hostileCreeps = Game.creeps[creepBuf].room.find(FIND_HOSTILE_CREEPS); + if(hostileCreeps.length > 0){ + Game.flags['DD'].setPosition(hostileCreeps[0].pos); + } + } + } + } + } + + spawnStack.sort(function(s1, s2){ return s1.priority > s2.priority }); + for(let spawn in spawnsInRoom){ + let spawnBuf = spawnsInRoom[spawn]; + + if(spawnBuf.isSpawning == null && spawnStack.length > 0) + spawnFromStack(spawnStack, spawnBuf); + } + } +} + +let cleanUpCreepsFromMemory = function () { + for (var name in Memory.creeps) { + if (!Game.creeps[name]) { + delete Memory.creeps[name]; + console.log('Clearing non-existing creep memory:', name); + } + } +} + +let addToSpawnStack = function(spawnStack, room, flag, role, creep){ + let priority = FLAG_PRIORITIES[flag] * ROLE_PRIORITIES[role]; + spawnStack.push({'priority': priority,'room': room, 'flag': flag, 'role': role, 'creep': creep}); + //console.log("Pushed: " + priority + " " + room + " " + flag + " " + role + " " + creep); +} + +let spawnFromStack = function(spawnStack, spawn){ + if(spawn.isSpawning != null) + return; + + let creepToSpawn = spawnStack.pop(); + + let room = creepToSpawn.room; + let flag = creepToSpawn.flag; + let role = creepToSpawn.role; + let creep = creepToSpawn.creep; + let newCreep = spawnCreep(role, spawn); + + if(Game.creeps[newCreep]){ + Memory.myRooms[room][flag][role].creeps[creep] = newCreep; + console.log("Spawned new Creep: " + newCreep + " " + role + " " + flag + " " + room); + }else { + spawnStack.push(creepToSpawn); + } +} + +let spawnCreep = function(role, spawn){ + let newCreep; + if(role == 'Harvesters'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); + else + newCreep = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], undefined); + }else if(role == 'Distributors'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); + else + newCreep = spawn.createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], undefined); + }else if(role == 'Upgraders'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); + else + ; + }else if(role == 'Builders'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); + else + newCreep = spawn.createCreep([WORK,WORK,WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], undefined); + }else if(role == 'Repairers'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); + else + newCreep = spawn.createCreep([WORK,WORK,WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], undefined); + }else if(role == 'Miners'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE], undefined); + else + newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE], undefined); + }else if(role == 'Claimers'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([CLAIM, CLAIM, MOVE, MOVE], undefined); + else + newCreep = spawn.createCreep([CLAIM,CLAIM,MOVE,MOVE], undefined); + }else if(role == 'Defenders'){ + if(spawn.name == "Spawn1") + newCreep = spawn.createCreep([TOUGH, TOUGH, TOUGH, TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE ,MOVE, HEAL, HEAL, HEAL, HEAL, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK], undefined); + else + ; + } + return newCreep; +} + +let orderCreep = function(room, flag, role, creepName, creepIndex){ + if(role == 'Harvesters'){ + + roleHarvester.run(room, flag, creepName, creepIndex); + + }else if(role == 'Distributors'){ + + roleDistributor.run(room, flag, creepName, creepIndex); + + }else if(role == 'Upgraders'){ + + roleUpgrader.run(flag, creepName); + + }else if(role == 'Builders'){ + + roleBuilder.run(room, flag, creepName); + + }else if(role == 'Repairers'){ + + roleRepairer.run(room, flag, creepName); + + }else if(role == 'Miners'){ + + roleMiner.run(room, flag, creepName); + + }else if(role == 'Claimers'){ + + roleClaimer.run(flag, creepName); + + }else if(role == 'Defenders'){ + + roleDefender.run('DD', creepName); + + } +} + +let init = function(){ + Memory.myRooms = + { + 'E22S57': + { + 'Flag1': + { + 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, + }, + + 'Flag2': + { + 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, + }, + + 'Flag3': + { + 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, + }, + + 'Flag4': + { + 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, + } + }, + + 'E24S57': + { + 'Flag5': + { + 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, + 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, + } + } + }; +} \ No newline at end of file diff --git a/tower.js b/tower.js new file mode 100644 index 0000000..61df122 --- /dev/null +++ b/tower.js @@ -0,0 +1,57 @@ +var towers = { + + /** @param {Game} game **/ + tick: function() { + + towers = Game.spawns.Spawn1.room.find(FIND_MY_STRUCTURES, { + filter: { structureType: STRUCTURE_TOWER } + }) + _.forEach(towers, function(tower){ + + + var myclosestDamagedStructure = tower.room.find(FIND_STRUCTURES, { + filter: (structure) => structure.hits < structure.hitsMax + }); + var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax)); + + var allcontainers = tower.room.find(FIND_STRUCTURES, { + filter: (s) => { + return ( s.structureType == STRUCTURE_CONTAINER) + } + }); + var usedstorage=0 + var mycapacity=0 + for(var i=0; i < allcontainers.length;i++){ + usedstorage+=_.sum(allcontainers[i].store) + mycapacity+=allcontainers[i].storeCapacity + } + console.log(usedstorage + " " + mycapacity) + + var storagepercent = usedstorage/mycapacity + var importantstructures = tower.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; + }}); + importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax) + if(tower.energy > tower.energyCapacity * .7 ){ + if(importantstructures.length > 0){ + tower.repair(importantstructures[0]); + } else + { + if(closestDamagedStructure) { + if(storagepercent > .8){ + console.log("tower repairing. Currently at: " + storagepercent) + tower.repair(closestDamagedStructure); + } else { console.log("tower waiting for more storage. Currently at: " + storagepercent)} + } + } + var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS); + if(closestHostile) { + tower.attack(closestHostile); + } + } + }) + } +}; + +module.exports = towers; \ No newline at end of file