lots of logs

This commit is contained in:
Tyrel Souza 2016-06-28 19:38:52 -04:00
parent a2ea1a4d1f
commit efcc50a27b
5 changed files with 72 additions and 15 deletions

View File

@ -1,10 +1,11 @@
// usernames are all lowercase, we compare with to lower // usernames are all lowercase, we compare with to lower
var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens']; var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens'];
module.exports = { module.exports = {
findEnemy: findEnemy findEnemy: findEnemy,
run: run
}; };
/* /*
var target = findEnemy(creep); var target = findEnemy(creep);
*/ */
@ -14,5 +15,41 @@ function findEnemy (creep) {
return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1); return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1);
} }
}); });
if (!targets.length) {
return creep.room.controller;
}
return creep.pos.findClosestByPath(targets);
}
function run (creep) {
var target = findEnemy(creep);
if (!target) {
target = findWall(creep);
// getDirectionTo
if (!creep.pos.isNearTo(target)) {
return creep.moveTo(target);
} else {
creep.attack(target);
}
var direction = creep.pos.getDirectionTo(target);
creep.memory.wallDirection = direction;
} else {
creep.moveTo(target);
creep.attack(target);
}
}
function findWall (creep) {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: function(object) {
if (object.my) {
return false;
}
if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_WALL) {
return false;
}
return true;
}
});
return creep.pos.findClosestByPath(targets); return creep.pos.findClosestByPath(targets);
} }

19
main.js
View File

@ -2,10 +2,11 @@ var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader'); var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder'); var roleBuilder = require('role.builder');
var creepRolePriority = ['harvester', 'builder', 'upgrader'];
var creepsConfig = { var creepsConfig = {
harvester: { harvester: {
bodyParts: [WORK, CARRY, MOVE], bodyParts: [WORK, CARRY, MOVE],
minimumCreeps: 4, minimumCreeps: 2,
role: roleHarvester role: roleHarvester
}, },
builder: { builder: {
@ -27,23 +28,27 @@ module.exports.loop = function(){
delete Memory.creeps[name]; delete Memory.creeps[name];
console.log("Clearning non-existing creep memory: ", name); console.log("Clearning non-existing creep memory: ", name);
} }
}
// Refactor some similar behavior for all roles
for (var roleName in creepsConfig){ }
// Refactor some similar behavior for all roles
for (var i in creepRolePriority){
var roleName = creepRolePriority[i];
var defaults = creepsConfig[roleName];
var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName); var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName);
if (creepsOfKind.length < creepsConfig[roleName].minimumCreeps){ if (creepsOfKind.length < creepsConfig[roleName].minimumCreeps){
var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts, var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts,
undefined, undefined,
{role: roleName}); {role: roleName});
console.log("Spawning new ", roleName, ": ", newName); console.log("Spawning", newName);
} }
}; }
// Run stuff for each creep // Run stuff for each creep
for (var name in Game.creeps){ for (var name in Game.creeps){
var creep = Game.creeps[name]; var creep = Game.creeps[name];
var role = creep.memory.role; var role = creep.memory.role;
creepsConfig[role].role.run(creep); creepsConfig[role].role.run(creep);
} }
console.log("tick");
} }

View File

@ -1,5 +1,8 @@
var BreakException= {};
var roleBuilder = { var roleBuilder = {
run: function(creep){ run: function(creep){
console.log(creep, "running Builder");
if(creep.memory.building && creep.carry.energy == 0){ if(creep.memory.building && creep.carry.energy == 0){
creep.memory.building = false; creep.memory.building = false;
} }
@ -7,14 +10,19 @@ var roleBuilder = {
creep.memory.building = true; creep.memory.building = true;
} }
if (creep.memory.building){ if (creep.memory.building){
var targets = creep.room.find(FIND_CONSTRUCTION_SITES); console.log("\t","isBuilding");
if(targets.length){ var targets = creep.room.find(FIND_CONSTRUCTION_SITES, {
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE){ filter: (target) => {
return target.energy < target.energyCapacity;
}
});
console.log("")
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]); creep.moveTo(targets[0]);
} }
}
} else { } else {
console.log("\t","isFindingSources");
var sources = creep.room.find(FIND_SOURCES); var sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){
creep.moveTo(sources[0]); creep.moveTo(sources[0]);

View File

@ -2,9 +2,12 @@ var roleHarvester = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
console.log(creep, "running Harvest");
if(creep.carry.energy < creep.carryCapacity) { if(creep.carry.energy < creep.carryCapacity) {
var sources = creep.room.find(FIND_SOURCES); var sources = creep.room.find(FIND_SOURCES);
// var source = Game.getObjectById("576a9c8257110ab231d8934b");
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
console.log(creep, "moving to", sources[0]);
creep.moveTo(sources[0]); creep.moveTo(sources[0]);
} }
} }
@ -19,6 +22,7 @@ var roleHarvester = {
}); });
if(targets.length > 0) { if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
console.log("\tmoving to", targets[0]);
creep.moveTo(targets[0]); creep.moveTo(targets[0]);
} }
} }

View File

@ -2,6 +2,7 @@ var roleUpgrader = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
console.log(creep, "running Upgrade");
if(creep.memory.upgrading && creep.carry.energy == 0) { if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false; creep.memory.upgrading = false;
@ -12,12 +13,14 @@ var roleUpgrader = {
if(creep.memory.upgrading) { if(creep.memory.upgrading) {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
console.log("\tmoving to", creep.room.controller);
creep.moveTo(creep.room.controller); creep.moveTo(creep.room.controller);
} }
} }
else { else {
var sources = creep.room.find(FIND_SOURCES); var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
console.log("\tmoving to", sources[0]);
creep.moveTo(sources[0]); creep.moveTo(sources[0]);
} }
} }