Skip to content

Instantly share code, notes, and snippets.

@new-guy
Created August 21, 2015 15:41
Show Gist options
  • Save new-guy/18d7a9d2c76019235969 to your computer and use it in GitHub Desktop.
Save new-guy/18d7a9d2c76019235969 to your computer and use it in GitHub Desktop.
/*
* Module code goes here. Use 'module.exports' to export things:
* module.exports = 'a thing';
*
* You can import it from another modules like this:
* var mod = require('creepmanager'); // -> 'a thing'
*/
//Need to be able to state "I want x of type x, y of type y, and z of type z"
var Display = require('display');
var spawnQueue = require('spawnQueue');
var profiler = require('profiler');
var creepRoleArray = ['harvester', 'balancer', 'miner', 'hauler', 'roadMan', 'builder', 'roomUpgrader', 'upgradeFeeder', 'repairMan', 'guard', 'medic', 'warrior', 'scout', 'rampartDefender', 'combatMedic', 'energyRetriever']; //Array of creeps to look at
function creepDefinition(role, targetCount, body)
{
this.role = role;
this.targetCount = targetCount;
this.body = body;
this.cost = calculateCost(body);
}
function calculateCost(partArray)
{
var cost = 0;
for(var i = 0; i < partArray.length; i++)
{
var workingPart = partArray[i];
if(workingPart === MOVE) cost += 50;
if(workingPart === WORK) cost += 100;
if(workingPart === CARRY) cost += 50;
if(workingPart === ATTACK) cost += 80;
if(workingPart === RANGED_ATTACK) cost += 150;
if(workingPart === HEAL) cost += 250;
if(workingPart === TOUGH) cost += 10;
}
return cost;
}
var forceDefinitionUpdate = true;
var expeditionRoom = Game.rooms.W6N6;
Spawn.prototype.initDefinitions = function()
{
if(typeof this.memory.creepDefinitions === 'undefined')
{
this.memory.creepDefinitions = {};
}
if(this.name === 'Spawn1')
{
var workingCreepDefinition = new creepDefinition('harvester', 0, [WORK, CARRY, MOVE]);
this.memory.creepDefinitions.harvester = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('balancer', 3, [CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.balancer = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('miner', 2, [WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.miner = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('hauler', 2, [CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.hauler = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('roadMan', 1, [WORK, CARRY, CARRY, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.roadMan = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('builder', 1, [WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.builder = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('roomUpgrader', 2, [WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.roomUpgrader = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('upgradeFeeder', 1, [CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.upgradeFeeder = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('repairMan', 3, [WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE])
this.memory.creepDefinitions.repairMan = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('warrior', 0, [TOUGH, TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK]);
this.memory.creepDefinitions.warrior = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('guard', 0, [TOUGH, TOUGH, TOUGH, TOUGH, ATTACK, MOVE, MOVE]);
this.memory.creepDefinitions.guard = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('medic', 0, [HEAL, HEAL, HEAL, MOVE, MOVE]);
this.memory.creepDefinitions.medic = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('scout', 0, [TOUGH, MOVE]);
this.memory.creepDefinitions.scout = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('rampartDefender', 0, [MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK]);
this.memory.creepDefinitions.rampartDefender = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('combatMedic', 4, [TOUGH, TOUGH, TOUGH, TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, ATTACK, RANGED_ATTACK, ATTACK, RANGED_ATTACK, HEAL, MOVE]);
this.memory.creepDefinitions.combatMedic = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('energyRetriever', 0, [CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.energyRetriever = workingCreepDefinition;
}
if(this.name === 'Spawn2')
{
var workingCreepDefinition = new creepDefinition('harvester', 0, [WORK, WORK, CARRY, CARRY, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.harvester = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('balancer', 2, [CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.balancer = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('miner', 2, [WORK, WORK, WORK, WORK, WORK, WORK, CARRY, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.miner = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('hauler', 4, [CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.hauler = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('roadMan', 1, [WORK, CARRY, CARRY, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.roadMan = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('builder', 1, [WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE]);
this.memory.creepDefinitions.builder = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('roomUpgrader', 3, [WORK, WORK, WORK, WORK, WORK, CARRY, MOVE]);
this.memory.creepDefinitions.roomUpgrader = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('upgradeFeeder', 3, [CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.upgradeFeeder = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('repairMan', 3, [WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE])
this.memory.creepDefinitions.repairMan = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('warrior', 0, [TOUGH, TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK]);
this.memory.creepDefinitions.warrior = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('guard', 0, [TOUGH, TOUGH, TOUGH, TOUGH, ATTACK, MOVE, MOVE]);
this.memory.creepDefinitions.guard = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('medic', 0, [HEAL, HEAL, HEAL, MOVE, MOVE]);
this.memory.creepDefinitions.medic = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('scout', 0, [TOUGH, MOVE]);
this.memory.creepDefinitions.scout = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('rampartDefender', 0, [MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK]);
this.memory.creepDefinitions.rampartDefender = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('combatMedic', 0, [TOUGH, TOUGH, TOUGH, TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, ATTACK, RANGED_ATTACK, ATTACK, RANGED_ATTACK, HEAL, MOVE]);
this.memory.creepDefinitions.combatMedic = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('remoteMiner', 0, [WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.remoteMiner = workingCreepDefinition;
//800
}
}
Spawn.prototype.getCreepDefinition = function(role)
{
if(typeof this.memory.creepDefinitions === 'undefined')
{
this.initDefinitions();
}
return this.memory.creepDefinitions[role];
}
Spawn.prototype.setCreepDefinition = function(definition)
{
if(typeof this.memory.creepDefinitions === 'undefined')
{
this.initDefinitions();
}
this.memory.creepDefinitions[definition.role] = definition;
}
Spawn.prototype.setCreepTarget = function(target, role)
{
var workingDefinition = this.getCreepDefinition(role);
workingDefinition.targetCount = target;
this.setCreepDefinition(workingDefinition);
};
Spawn.prototype.updateCreepSpawning = function()
{
//if(true)
var hostileCreepCount = this.room.find(FIND_HOSTILE_CREEPS).length;
if(hostileCreepCount === 0)
{
if(this.memory.defense)
{
this.memory.spawnQueue = [];
}
this.memory.defense = false;
}
if(hostileCreepCount > 0 && this.room.name === 'W2N1')
{
this.defenseProtocol();
if(!this.memory.defense)
{
this.memory.spawnQueue = [];
}
this.memory.defense = true;
}
else if(typeof this.memory.creepDefinitions === 'undefined' || forceDefinitionUpdate)
{
this.initDefinitions();
}
var creepDetailsArray = []
for(var i = 0; i < creepRoleArray.length; i++)
{
var workingDefinition = this.memory.creepDefinitions[creepRoleArray[i]];
if(typeof workingDefinition === 'undefined')
{
continue;
}
if(workingDefinition.targetCount > 0) //THIS IS CODE FOR DISPLAYING CREEP COUNTS
{
var creepDetails = {
role: workingDefinition.role,
count: creepCount,
target: workingDefinition.targetCount
};
creepDetailsArray.push(creepDetails);
}
var creepCount = spawnQueue.getHomeRoomCreepCount(creepRoleArray[i], this.room.name);//getCreepCount(this.room, workingDefinition);
var queueCreepCount = this.getQueueCreepCount(creepRoleArray[i]);
if(creepCount < workingDefinition.targetCount && creepCount+queueCreepCount < workingDefinition.targetCount)
{
if(creepCount+queueCreepCount === 0 && creepRoleArray[i] === 'balancer')
{
console.log('Forcing balancers to front of spawn queue');
this.addToFrontOfSpawnQueue(workingDefinition.body, {role: workingDefinition.role});
}
else
{
console.log('Adding ' + creepRoleArray[i] + ' to ' + this.name + ' queue');
this.addToSpawnQueue(workingDefinition.body, {role: workingDefinition.role});
}
}
}
//renderCreepDetails(creepDetailsArray);
};
Spawn.prototype.defenseProtocol = function()
{
this.memory.creepDefinitions = {};
var workingCreepDefinition = new creepDefinition('miner', 2, [WORK, WORK, WORK, WORK, CARRY, MOVE]);
this.memory.creepDefinitions.miner = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('hauler', 2, [CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE]);
this.memory.creepDefinitions.hauler = workingCreepDefinition;
workingCreepDefinition = new creepDefinition('rampartDefender', 40, [MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK, RANGED_ATTACK])
this.memory.creepDefinitions.rampartDefender = workingCreepDefinition;
};
function renderCreepDetails(creepDetailsArray)
{
var stringArray = [];
for(var i = 0; i < creepDetailsArray.length; i++)
{
var creepDetails = creepDetailsArray[i];
var string = creepDetails.role + ": " + creepDetails.count + "/" + creepDetails.target;
stringArray.push(string);
}
Display.renderStringArrayAsFlags(Game.rooms.W2N1, stringArray, new RoomPosition(44, 20, 'W2N1'), 'down');
}
function spawnCreep(spawner, definition)
{
if(spawner.room.energyAvailable < definition.cost)
{
return;
}
spawner.createCreep(definition.body, null, {role: definition.role, homeRoom: spawner.room.name});
}
function getCreepCount(workingRoom, creepDefinition)
{
var myCreepArray = workingRoom.find(FIND_MY_CREEPS);
var targetRole = creepDefinition.role;
var count = 0;
for(var i = 0; i < myCreepArray.length; i++)
{
if(myCreepArray[i].memory.role === targetRole)
{
count++;
}
}
return count;
}
exports.getNumOfNeededCreep = function(workingRoom, creepRole)
{
var creepCount = getCreepCount(workingRoom, {'role': creepRole});
var spawn = workingRoom.find(FIND_MY_SPAWNS)[0];
var creepTarget = spawn.memory.creepDefinitions[creepRole].targetCount;
return Math.max(creepTarget - creepCount, 0);
}
exports.getCreepCount = getCreepCount;
Spawn.prototype.addToSpawnQueue = function(body, options)
{
if(this.memory.spawnQueue === undefined)
{
this.memory.spawnQueue = [];
}
this.memory.spawnQueue.push({body: body, options: options});
}
Spawn.prototype.addToFrontOfSpawnQueue = function(body, options)
{
if(this.memory.spawnQueue === undefined)
{
this.memory.spawnQueue = [];
}
this.memory.spawnQueue.unshift({body: body, options: options});
}
//ADD TO QUEUE FROM DICTIONARY
Spawn.prototype.clearSpawnQueue = function()
{
this.memory.spawnQueue = [];
}
Spawn.prototype.processQueue = function()
{
if(this.memory.spawnQueue.length === 0) return;
var creepToSpawn = this.memory.spawnQueue[0];
if(creepToSpawn.options === null)
{
return;
}
if(creepToSpawn.options.homeRoom === undefined)
{
creepToSpawn.options.homeRoom = this.room.name;
}
//console.log(this.canCreateCreep(creepToSpawn.body))
if(this.canCreateCreep(creepToSpawn.body) === 0)
{
if(creepToSpawn.options.role !== undefined) console.log(this.name + ' spawning ' + creepToSpawn.options.role);
this.createCreep(creepToSpawn.body, null, creepToSpawn.options);
this.memory.spawnQueue.shift();
}
}
Spawn.prototype.getQueueCreepCount = function(role)
{
var count = 0;
if(this.memory.spawnQueue === undefined)
{
this.memory.spawnQueue = [];
}
for(var i = 0; i < this.memory.spawnQueue.length; i++)
{
var workingCreep = this.memory.spawnQueue[i];
if(workingCreep.options === null)
{
continue;
}
if(workingCreep.options.role === role)
{
count++;
}
}
return count;
}
exports.getHomeRoomCreepCount = function(role, roomName)
{
var count = 0;
for(var creep in Game.creeps)
{
var workingCreep = Game.creeps[creep];
if(workingCreep.memory.homeRoom === roomName && workingCreep.memory.role === role)
{
count++;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment