fix line endings, and dealt with tower logic

This commit is contained in:
Tyrel Souza 2016-06-30 10:37:58 -04:00
parent ed4c5eb67f
commit 856706ae28
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
7 changed files with 263 additions and 228 deletions

107
battle.js
View File

@ -1,55 +1,52 @@
// 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 run: run
}; };
/* function findEnemy (searcher) {
var target = findEnemy(creep); var targets = searcher.room.find(FIND_HOSTILE_CREEPS, {
*/ filter: (c) => {
function findEnemy (creep) { return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1);
var targets = creep.room.find(FIND_HOSTILE_CREEPS, { }
filter: (c) => { });
return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1); if (!targets.length) {
} return searcher.room.controller;
}); }
if (!targets.length) { return searcher.pos.findClosestByPath(targets);
return creep.room.controller; }
}
return creep.pos.findClosestByPath(targets); function run (creep) {
} var target = findEnemy(creep);
if (!target) {
function run (creep) { target = findWall(creep);
var target = findEnemy(creep); // getDirectionTo
if (!target) { if (!creep.pos.isNearTo(target)) {
target = findWall(creep); return creep.moveTo(target);
// getDirectionTo } else {
if (!creep.pos.isNearTo(target)) { creep.attack(target);
return creep.moveTo(target); }
} else { var direction = creep.pos.getDirectionTo(target);
creep.attack(target); creep.memory.wallDirection = direction;
} } else {
var direction = creep.pos.getDirectionTo(target); creep.moveTo(target);
creep.memory.wallDirection = direction; creep.attack(target);
} else { }
creep.moveTo(target); }
creep.attack(target);
} function findWall (creep) {
} var targets = creep.room.find(FIND_STRUCTURES, {
filter: function(object) {
function findWall (creep) { if (object.my) {
var targets = creep.room.find(FIND_STRUCTURES, { return false;
filter: function(object) { }
if (object.my) { if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_WALL) {
return false; return false;
} }
if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_WALL) { return true;
return false; }
} });
return true; return creep.pos.findClosestByPath(targets);
} }
});
return creep.pos.findClosestByPath(targets);
}

View File

@ -1,16 +1,16 @@
var helpers = { var helpers = {
generateName: function(role){ generateName: function(role){
return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) { return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16); return v.toString(16);
}); });
}, },
getLastFlag: function(flagName){ getLastFlag: function(flagName){
return _(Object.keys(Game.flags)) return _(Object.keys(Game.flags))
.filter((f) => f.startsWith(flagName)) .filter((f) => f.startsWith(flagName))
.map((f) => f.slice(flagName.length)) .map((f) => f.slice(flagName.length))
.max(); .max();
}, },
}; };
module.exports = helpers; module.exports = helpers;

148
main.js
View File

@ -1,74 +1,74 @@
var roleHarvester = require('role.harvester'); 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 roleGuard = require('role.guard'); var roleGuard = require('role.guard');
var helpers = require('helpers'); var helpers = require('helpers');
var creepRolePriority = ['harvester', 'builder', 'upgrader', 'guard']; var creepRolePriority = ['harvester', 'builder', 'upgrader', 'guard'];
var creepsConfig = { var creepsConfig = {
harvester: { harvester: {
bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE], bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE],
minimumCreeps: 3, minimumCreeps: 4,
role: roleHarvester role: roleHarvester
}, },
builder: { builder: {
bodyParts: [WORK, CARRY, MOVE, MOVE], bodyParts: [WORK, CARRY, MOVE, MOVE],
minimumCreeps: 2, minimumCreeps: 4,
role: roleBuilder role: roleBuilder
}, },
upgrader: { upgrader: {
bodyParts: [WORK, CARRY, CARRY, MOVE, MOVE], bodyParts: [WORK, CARRY, CARRY, MOVE, MOVE],
minimumCreeps: 6, minimumCreeps: 4,
role: roleUpgrader role: roleUpgrader
}, },
guard: { guard: {
bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK], bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK],
minimumCreeps: 3, minimumCreeps: 4,
role: roleGuard, role: roleGuard,
} }
}; };
module.exports.loop = function(){ module.exports.loop = function(){
console.log(' '); console.log(' ');
// Cleanup Creeps // Cleanup Creeps
for (var name in Memory.creeps){ for (var name in Memory.creeps){
if (!Game.creeps[name]){ if (!Game.creeps[name]){
delete Memory.creeps[name]; delete Memory.creeps[name];
console.log('Cleaning non-existing creep memory:', name); console.log('Cleaning non-existing creep memory:', name);
} }
} }
// Refactor some similar behavior for all roles // Refactor some similar behavior for all roles
for (var i in creepRolePriority){ for (var i in creepRolePriority){
var roleName = creepRolePriority[i]; var roleName = creepRolePriority[i];
var defaults = creepsConfig[roleName]; var defaults = creepsConfig[roleName];
let creepsOfKind = _.filter(Game.creeps, (creep) => {return creep.memory.role == roleName}); let creepsOfKind = _.filter(Game.creeps, (creep) => {return creep.memory.role == roleName});
if (creepsOfKind.length < defaults.minimumCreeps){ if (creepsOfKind.length < defaults.minimumCreeps){
var newName = Game.spawns.Spawn1.createCreep(defaults.bodyParts, var newName = Game.spawns.Spawn1.createCreep(defaults.bodyParts,
helpers.generateName(roleName), // Make names show the roles. Change me to undefined if this gets annoying. helpers.generateName(roleName), // Make names show the roles. Change me to undefined if this gets annoying.
{role: roleName}); {role: roleName});
if(newName == ERR_NOT_ENOUGH_ENERGY){ if(newName == ERR_NOT_ENOUGH_ENERGY){
console.log('not enough resources to spawn'); console.log('not enough resources to spawn');
} else { } else {
console.log('Spawning', newName); console.log('Spawning', newName);
} }
} }
} }
// Run stuff for each creep // Run stuff for each creep
var roleCounts = {harvester: 0, builder: 0, upgrader: 0, guard: 0}; var roleCounts = {harvester: 0, builder: 0, upgrader: 0, guard: 0};
var expected = _(creepsConfig).mapValues('minimumCreeps'); var expected = _(creepsConfig).mapValues('minimumCreeps');
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;
roleCounts[role] += 1; roleCounts[role] += 1;
creepsConfig[role].role.run(creep); creepsConfig[role].role.run(creep);
} }
console.log('current\t', JSON.stringify(roleCounts)); console.log('current\t', JSON.stringify(roleCounts));
console.log('expect\t', JSON.stringify(expected)); console.log('expect\t', JSON.stringify(expected));
console.log('#############################################################'); console.log('#############################################################');
}; };

View File

@ -1,28 +1,28 @@
var BreakException= {}; var BreakException= {};
var roleBuilder = { var roleBuilder = {
run: function(creep){ run: function(creep){
if(creep.memory.building && creep.carry.energy == 0){ if(creep.memory.building && creep.carry.energy == 0){
creep.memory.building = false; creep.memory.building = false;
} }
if (!creep.memory.building && creep.carry.energy == creep.carryCapacity){ if (!creep.memory.building && creep.carry.energy == creep.carryCapacity){
creep.memory.building = true; creep.memory.building = true;
} }
if (creep.memory.building){ if (creep.memory.building){
var targets = creep.room.find(FIND_CONSTRUCTION_SITES); var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
var transferErrorCode = creep.build(targets[0]); var transferErrorCode = creep.build(targets[0]);
if(transferErrorCode == ERR_NOT_IN_RANGE) { if(transferErrorCode == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]); creep.moveTo(targets[0]);
} }
} 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){
creep.moveTo(sources[0]); creep.moveTo(sources[0]);
} }
} }
} }
}; };
module.exports = roleBuilder; module.exports = roleBuilder;

View File

@ -1,32 +1,32 @@
var roleHarvester = { var roleHarvester = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
console.log(creep, 'running Harvest'); console.log(creep, 'running Harvest');
if(creep.carry.energy < creep.carryCapacity) { if(creep.carry.energy < creep.carryCapacity) {
var source = creep.pos.findClosestByPath(FIND_SOURCES); var source = creep.pos.findClosestByPath(FIND_SOURCES);
if(creep.harvest(source) == ERR_NOT_IN_RANGE) { if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
console.log('\tmoving to', source); console.log('\tmoving to', source);
creep.moveTo(source); creep.moveTo(source);
} }
} }
else { else {
var targets = creep.room.find(FIND_STRUCTURES, { var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION return (structure.structureType == STRUCTURE_EXTENSION
|| structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_SPAWN
|| structure.structureType == STRUCTURE_TOWER || structure.structureType == STRUCTURE_TOWER
) && structure.energy < structure.energyCapacity; ) && structure.energy < structure.energyCapacity;
} }
}); });
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]); console.log('\tmoving to', targets[0]);
creep.moveTo(targets[0]); creep.moveTo(targets[0]);
} }
} }
} }
} }
}; };
module.exports = roleHarvester; module.exports = roleHarvester;

View File

@ -1,26 +1,26 @@
var roleUpgrader = { var roleUpgrader = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
if(creep.memory.upgrading && creep.carry.energy == 0) { if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false; creep.memory.upgrading = false;
} }
if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) { if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) {
creep.memory.upgrading = true; creep.memory.upgrading = true;
} }
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) {
creep.moveTo(creep.room.controller); creep.moveTo(creep.room.controller);
} }
} }
else { else {
var source = creep.pos.findClosestByPath(FIND_SOURCES); var source = creep.pos.findClosestByPath(FIND_SOURCES);
if(creep.harvest(source) == ERR_NOT_IN_RANGE) { if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source); creep.moveTo(source);
} }
} }
} }
}; };
module.exports = roleUpgrader; module.exports = roleUpgrader;

38
structure.tower.js Normal file
View File

@ -0,0 +1,38 @@
var battle = require('battle')
//var helpers = require('helpers');
var structureTower= {
run: function(structure){
this.structure = structure;
console.log(structure, 'running Tower')
var target = battle.findEnemy(this.structure);
if(target != structure.room.controller) {
console.log('\t', this.structure, 'attacking:',target)
this.structure.attack(target);
} else {
var damagedStructure = this.findDamagedStructure();
if(damagedStructure){
console.log("repairing", damagedStructure);
this.structure.repair(damagedStructure);
}
}
},
findDamagedStructure: function(searcher){
var targets = searcher.room.find(FIND_STRUCTURES, {
filter: function(s){
if(s.structureType == STRUCTURE_TOWER){
return false;
}
if (s.hits < s.hitsMax){
return true;
}
return false;
}
});
return searcher.pos.findClosestByPath(targets);
}
}
module.exports = structureTower;