guard, and change some numbers

This commit is contained in:
Tyrel Souza 2016-06-29 13:21:09 -04:00
parent b8ada37da4
commit be4b33d728
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
7 changed files with 252 additions and 228 deletions

108
battle.js
View File

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

View File

@ -1,14 +1,14 @@
/** /**
* Created by tsouza on 6/28/2016. * Created by tsouza on 6/28/2016.
*/ */
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);
}); });
} }
}; };
module.exports = helpers; module.exports = helpers;

145
main.js
View File

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

View File

@ -1,32 +1,32 @@
var BreakException= {}; var BreakException= {};
var roleBuilder = { var roleBuilder = {
run: function(creep){ run: function(creep){
console.log(creep, "running Builder"); 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;
} }
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){
console.log("\t is building"); console.log("\t is 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) {
console.log("\t moving to", targets[0]); console.log("\t moving to", targets[0]);
creep.moveTo(targets[0]); creep.moveTo(targets[0]);
} }
} else { } else {
console.log("\t","isFindingSources"); 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]);
} }
} }
} }
}; };
module.exports = roleBuilder; module.exports = roleBuilder;

17
role.guard.js Normal file
View File

@ -0,0 +1,17 @@
var battle = require("battle")
var roleGuard = {
run: function(creep){
console.log(creep, "running Guard")
var target = battle.findEnemy(creep);
console.log("\t", creep, "tageting:",target)
if(target && creep.hits > creep.hitsMax - 500 /* no more attack */) {
creep.moveTo(target);
creep.attack(target);
} else {
creep.moveTo(Game.spawns.Spawn1);
}
}
}
module.exports = roleGuard;

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,30 +1,30 @@
var roleUpgrader = { var roleUpgrader = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
console.log(creep, "running Upgrade"); 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;
} }
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) {
console.log("\tmoving to", creep.room.controller); console.log("\tmoving to", creep.room.controller);
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) {
console.log("\tmoving to", source); console.log("\tmoving to", source);
creep.moveTo(source); creep.moveTo(source);
} }
} }
} }
}; };
module.exports = roleUpgrader; module.exports = roleUpgrader;