Initial Commit

This commit is contained in:
Tyrel Souza 2016-06-28 16:31:39 -04:00
commit 490877fb9c
5 changed files with 139 additions and 0 deletions

18
battle.js Normal file
View File

@ -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);
}

40
main.js Normal file
View File

@ -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);
}
}
}

25
role.builder.js Normal file
View File

@ -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;

29
role.harvester.js Normal file
View File

@ -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;

27
role.upgrader.js Normal file
View File

@ -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;