Created
August 18, 2015 22:21
-
-
Save new-guy/193505325ce3bba99f81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AGGRESSION_COOLDOWN = 600; //Wait 10 minutes before trying to go back on an aggressed route | |
//Have a function to allow adding a room position for a source as a mining target to memory. Contains room, coords, count for miners to send, and assaulted flag. It'll be under Memory.remoteSources[roomx#y#] | |
//require('remoteMiner').addRemoteMiningTarget('XXXX', 30, 35, 'W2N1', 23, 32, 2); | |
exports.addRemoteMiningTarget = function(sourceRoomName, source_x, source_y, storageRoomName, storage_x, storage_y, minerCount) | |
{ | |
//memory.remoteminingtargets | |
var remoteMiningTargets = Memory.remoteMiningTargets; | |
if(remoteMiningTargets === undefined) | |
{ | |
Memory.remoteMiningTargets = {}; | |
} | |
Memory.remoteMiningTargets[sourceRoomName + "x" + source_x + "y" + source_y] = { | |
sourcePosition: new RoomPosition(source_x, source_y, sourceRoomName), | |
storagePosition: new RoomPosition(storage_x, storage_y, storageRoomName), | |
minerCount: minerCount, | |
minerIds: [], | |
assaulted: false, | |
ticksSinceAssault: 0 | |
}; | |
//check if it exists, create if it doesn't | |
//add Memory.remoteSources[roomx#y#] | |
//Contains room, coords, count for miners to send, and assaulted flag | |
} | |
exports.updateMinerSpawns = function(spawn) | |
{ | |
if(spawn.defense) return; //Don't spawn any miners when on defense | |
for(var miningTarget in Memory.remoteMiningTargets) | |
{ | |
var minerCount = 0; | |
var sourceData = Memory.remoteMiningTargets[miningTarget]; | |
if(sourceData.assaulted) | |
{ | |
if(sourceData.ticksSinceAssault % 30 == 0) console.log("WARNING! MINING ROUTE IN " + sourceData.sourcePosition.roomName + " HAS BEEN ASSAULTED"); | |
sourceData.ticksSinceAssault++; | |
if(sourceData.ticksSinceAssault >= AGGRESSION_COOLDOWN) | |
{ | |
sourceData.ticksSinceAssault = 0; | |
sourceData.assaulted = false; | |
} | |
continue; | |
} | |
for(var i = 0; i < sourceData.minerIds.length; i++) | |
{ | |
var miner = Game.getObjectById(sourceData.minerIds[i]); | |
if(miner === null) | |
{ | |
sourceData.minerIds.splice(i, 1); | |
} | |
else | |
{ | |
minerCount++; | |
} | |
} | |
if(minerCount < sourceData.minerCount) | |
{ | |
spawnRemoteMiner(spawn, miningTarget, minerCount, sourceData.minerCount); | |
} | |
} | |
//Upon spawning a miner, push it into the minerIds array | |
} | |
//Each tick, count the number of creeps role="remoteMiner" assigned to each source (Memory.remoteSources[roomx#y#]). | |
function spawnRemoteMiner(spawn, miningTarget, minerCount, targetMinerCount) | |
{ | |
var remoteMinerBody = [WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]; | |
//var remoteMinerBody = [WORK, CARRY, MOVE]; | |
if(spawn.canCreateCreep(remoteMinerBody) === 0) | |
{ | |
console.log('Spawning remote miner for ' + miningTarget + '(' + minerCount + '/' + targetMinerCount + ')' ); | |
spawn.createCreep(remoteMinerBody, null, {role: "remoteMiner", miningTarget: miningTarget, homeSpawnId: spawn.id}); | |
} | |
} | |
//If the number is less than the count, spawn another miner (unless assaulted), with the options role="remoteMiner", remoteSource=roomx#y#, storagePosition="" | |
exports.doRemoteMining = function(creep) | |
{ | |
var miningTarget = handleMemory(creep); | |
if(miningTarget === null || miningTarget === undefined) | |
{ | |
return; | |
} | |
//if(under attack) fleeAndFlag(); | |
//At any time, if an enemy gets within range 4, start booking it back home | |
//Trigger asssauled flag | |
if(creep.hits < creep.hitsMax) | |
{ | |
miningTarget.ticksSinceAssault = 0; | |
miningTarget.assaulted = true; | |
} | |
//1250 | |
//if(false) | |
if(creep.carry.energy < creep.carryCapacity) | |
{ | |
creep.say(miningTarget.sourcePosition.roomName + '-Mine'); | |
handleMining(creep, miningTarget); | |
if(creep.memory.hasMined == true) | |
{ | |
creep.memory.hasMined = false; | |
if(creep.memory.revenue === undefined) | |
{ | |
creep.memory.revenue = 0; | |
} | |
if(creep.memory.tripCount === undefined) | |
{ | |
creep.memory.tripCount = 0; | |
} | |
creep.memory.revenue += creep.carryCapacity; | |
creep.memory.tripCount++; | |
console.log(creep.name + ' has made ' + creep.memory.revenue + ' in revenue, ' + (creep.memory.revenue - 1250) + ' in profit | Trips: ' + creep.memory.tripCount + ' Ticks Left: ' + creep.ticksToLive + " | " + creep.memory.miningTarget); | |
} | |
} | |
//if(true) | |
if(creep.carry.energy == creep.carryCapacity) | |
{ | |
creep.say(miningTarget.sourcePosition.roomName + '-Ret'); | |
handleReturn(creep, miningTarget); | |
creep.memory.hasMined = true; | |
} | |
} | |
function handleMemory(creep) | |
//Check if assigned to mining target array | |
//If not, add self to mining target array | |
{ | |
var miningTarget = Memory.remoteMiningTargets[creep.memory.miningTarget]; | |
var hasBeenAssigned = false; | |
if(miningTarget === null || miningTarget === undefined) | |
{ | |
console.log(creep.name + " has no mining target. Has no will to live"); | |
creep.suicide(); | |
return; | |
} | |
for(var i = 0; i < miningTarget.minerIds.length; i++) | |
{ | |
if(miningTarget.minerIds[i] == creep.id) | |
{ | |
hasBeenAssigned = true; | |
} | |
} | |
if(!hasBeenAssigned) | |
{ | |
miningTarget.minerIds.push(creep.id); | |
} | |
return miningTarget; | |
} | |
function handleMining(creep, miningTarget) | |
{ | |
var miningTargetPosition = new RoomPosition(miningTarget.sourcePosition.x, miningTarget.sourcePosition.y, miningTarget.sourcePosition.roomName); | |
if(creep.pos.isNearTo(miningTargetPosition)) | |
{ | |
var source = creep.room.lookForAt("source", miningTarget.sourcePosition.x, miningTarget.sourcePosition.y)[0]; | |
if(source == null) | |
{ | |
console.log("WARNING: INCORRECT SOURCE ASSIGNED FOR " + creep.name); | |
} | |
creep.harvest(source); | |
} | |
else | |
{ | |
creep.moveTo(miningTargetPosition, {reusePath: 20}); | |
} | |
} | |
function handleReturn(creep, miningTarget) | |
{ | |
var returnPosition = new RoomPosition(miningTarget.storagePosition.x, miningTarget.storagePosition.y, miningTarget.storagePosition.roomName); | |
if(creep.pos.isNearTo(returnPosition)) | |
{ | |
var storage = creep.room.lookForAt("structure", returnPosition.x, returnPosition.y)[0]; | |
if(storage == null) | |
{ | |
console.log("WARNING: INCORRECT STRUCTURE ASSIGNED FOR " + creep.name); | |
} | |
creep.transferEnergy(storage); | |
} | |
else | |
{ | |
creep.moveTo(returnPosition); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment