commit 490877fb9c2039b5cff48f502dfd354a21818a79 Author: Tyrel Souza Date: Tue Jun 28 16:31:39 2016 -0400 Initial Commit diff --git a/battle.js b/battle.js new file mode 100644 index 0000000..6439906 --- /dev/null +++ b/battle.js @@ -0,0 +1,18 @@ +// usernames are all lowercase, we compare with to lower +var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens']; + +module.exports = { + findEnemy: findEnemy +}; + +/* + var target = findEnemy(creep); +*/ +function findEnemy (creep) { + var targets = creep.room.find(FIND_HOSTILE_CREEPS, { + filter: (c) => { + return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1); + } + }); + return creep.pos.findClosestByPath(targets); +} \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..024b227 --- /dev/null +++ b/main.js @@ -0,0 +1,40 @@ +var roleHarvester = require('role.harvester'); +var roleUpgrader = require('role.upgrader'); +var roleBuilder = require('role.builder'); + +module.exports.loop = function(){ + // Cleanup Creeps + for (var name in Memory.creeps){ + if (!Game.creeps[name]){ + delete Memory.creeps[name]; + console.log("Clearning non-existing creep memory: ", name); + } + } + + // Refactor some similar behavior for all roles + var roleNames = ["harvester", "builder", "upgrader"]; + roleNames.forEach(function(roleName){ + var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName); + if (creepsOfKind.length < 2){ + var newName = Game.spawns.Spawn1.createCreep([WORK, CARRY, MOVE], undefined, {role: roleName}); + console.log("Spawning new ", roleName, ": ", newName); + } + }); + + // Run stuff for each creep + for (var name in Game.creeps){ + var creep = Game.creeps[name]; + if (creep.memory.role == 'harvester'){ + roleHarvester.run(creep); + } + + if (creep.memory.role == 'upgrader'){ + roleUpgrader.run(creep); + } + + if (creep.memory.role == 'builder'){ + roleBuilder.run(creep); + } + + } +} \ No newline at end of file diff --git a/role.builder.js b/role.builder.js new file mode 100644 index 0000000..dcaeffc --- /dev/null +++ b/role.builder.js @@ -0,0 +1,25 @@ +var roleBuilder = { + run: function(creep){ + if(creep.memory.building && creep.carry.energy == 0){ + creep.memory.building = false; + } + if (!creep.memory.building && creep.carry.energy == creep.carryCapacity){ + creep.memory.building = true; + } + if (creep.memory.building){ + var targets = creep.room.find(FIND_CONSTRUCTION_SITES); + if(targets.length){ + if (creep.build(targets[0]) == ERR_NOT_IN_RANGE){ + creep.moveTo(targets[0]); + } + } + + } else { + var sources = creep.room.find(FIND_SOURCES); + if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ + creep.moveTo(sources[0]); + } + } + } +} +module.exports = roleBuilder; diff --git a/role.harvester.js b/role.harvester.js new file mode 100644 index 0000000..aa1328d --- /dev/null +++ b/role.harvester.js @@ -0,0 +1,29 @@ +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 targets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_EXTENSION + || structure.structureType == STRUCTURE_SPAWN + || structure.structureType == STRUCTURE_TOWER + ) && structure.energy < structure.energyCapacity; + } + }); + if(targets.length > 0) { + if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(targets[0]); + } + } + } + } +}; + +module.exports = roleHarvester; \ No newline at end of file diff --git a/role.upgrader.js b/role.upgrader.js new file mode 100644 index 0000000..7374160 --- /dev/null +++ b/role.upgrader.js @@ -0,0 +1,27 @@ +var roleUpgrader = { + + /** @param {Creep} creep **/ + run: function(creep) { + + if(creep.memory.upgrading && creep.carry.energy == 0) { + creep.memory.upgrading = false; + } + if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) { + creep.memory.upgrading = true; + } + + if(creep.memory.upgrading) { + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller); + } + } + else { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0]); + } + } + } +}; + +module.exports = roleUpgrader; \ No newline at end of file