Created
August 21, 2015 15:38
-
-
Save new-guy/fd3c3eb0ae09f33613c8 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 profiler = require('profiler'); | |
var AGGRESSION_COOLDOWN = 1800; //Wait 10 minutes before trying to go back on an aggressed route | |
var SHOULD_BE_COMPLAINING = false; | |
//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 miningTargetData = Memory.remoteMiningTargets[miningTarget]; | |
//check to make sure it's the right room | |
if(miningTargetData.storagePosition.roomName !== spawn.room.name) | |
{ | |
continue; | |
} | |
if(miningTargetData.assaulted) | |
{ | |
if(miningTargetData.ticksSinceAssault % 30 === 0 && SHOULD_BE_COMPLAINING) console.log("WARNING! MINING ROUTE IN " + miningTargetData.sourcePosition.roomName + " HAS BEEN ASSAULTED"); | |
miningTargetData.ticksSinceAssault++; | |
if(miningTargetData.ticksSinceAssault >= AGGRESSION_COOLDOWN) | |
{ | |
miningTargetData.ticksSinceAssault = 0; | |
miningTargetData.assaulted = false; | |
} | |
continue; | |
} | |
for(var i = 0; i < miningTargetData.minerIds.length; i++) | |
{ | |
var miner = Game.getObjectById(miningTargetData.minerIds[i]); | |
if(miner === null) | |
{ | |
miningTargetData.minerIds.splice(i, 1); | |
} | |
else | |
{ | |
minerCount++; | |
} | |
} | |
var minersInQueue = getRemoteMinersInQueueByMiningTarget(miningTarget, spawn); | |
if(minerCount < miningTargetData.minerCount && minerCount + minersInQueue < miningTargetData.minerCount) | |
{ | |
//console.log(spawn.name); | |
spawnRemoteMiner(spawn, miningTarget, minerCount + minersInQueue, miningTargetData.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, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE]; | |
var minerCost = 1550; | |
//var remoteMinerBody = [WORK, CARRY, MOVE]; | |
console.log(spawn.name); | |
if(spawn.memory.creepDefinitions === undefined) return; | |
if(spawn.memory.creepDefinitions.remoteMiner !== undefined) | |
{ | |
remoteMinerBody = spawn.memory.creepDefinitions.remoteMiner.body; | |
if(spawn.room.name == 'W3N2') minerCost = 1050; | |
} | |
console.log('Adding remote miner for ' + miningTarget + '(' + (minerCount+1) + '/' + targetMinerCount + ') to queue' ); | |
spawn.addToSpawnQueue(remoteMinerBody, {role: "remoteMiner", miningTarget: miningTarget, homeSpawnId: spawn.id, cost: minerCost}); | |
} | |
function getRemoteMinersInQueueByMiningTarget(miningTarget, spawn) | |
{ | |
var minersInQueueCount = 0; | |
if(spawn.memory.spawnQueue === undefined) | |
{ | |
spawn.memory.spawnQueue = []; | |
} | |
for(var i = 0; i < spawn.memory.spawnQueue.length; i++) | |
{ | |
var creep = spawn.memory.spawnQueue[i]; | |
if(creep.options === null) | |
{ | |
continue; | |
} | |
if(creep.options.role !=='remoteMiner') | |
{ | |
continue; | |
} | |
if(creep.options.miningTarget === miningTarget) | |
{ | |
minersInQueueCount++; | |
} | |
} | |
return minersInQueueCount; | |
} | |
////////////MINER LOGIC////////////// | |
exports.doRemoteMining = function(creep) | |
{ | |
var miningTarget = handleMemory(creep); | |
if(creep.memory.ticksAtStartOfTrip === undefined) | |
{ | |
creep.memory.ticksAtStartOfTrip = creep.ticksToLive; | |
} | |
if(miningTarget === null || miningTarget === undefined) | |
{ | |
return; | |
} | |
handleAssault(creep, miningTarget); | |
if(creep.carry.energy < creep.carryCapacity) | |
{ | |
if(/*creep.room.name == 'W3N2'*/ false && creep.carry.energy > 0) | |
{ | |
creep.say(miningTarget.sourcePosition.roomName + '-Exp'); | |
fillNearbyExpansions(creep); | |
} | |
else | |
{ | |
creep.say(miningTarget.sourcePosition.roomName + '-Mine'); | |
handleMining(creep, miningTarget); | |
if(creep.memory.hasMined === true) | |
{ | |
handleProfitAnalysis(creep, miningTarget); | |
} | |
} | |
} | |
if(creep.carry.energy === creep.carryCapacity) | |
{ | |
creep.say(miningTarget.sourcePosition.roomName + '-Ret'); | |
handleReturn(creep, miningTarget); | |
creep.memory.hasMined = true; | |
} | |
} | |
function fillNearbyExpansions(creep) | |
{ | |
var nearbyNonfullExpansions = creep.pos.findInRange(FIND_STRUCTURES, 4, { | |
filter: function (e) { | |
return e.structureType === STRUCTURE_EXTENSION && e.energy < e.energyCapacity; | |
} | |
}); | |
if(nearbyNonfullExpansions.length > 0) | |
{ | |
creep.moveTo(nearbyNonfullExpansions[0]); | |
//creep.moveToPosition(nearbyNonfullExpansions[0].pos, {globalCache: true}); | |
creep.transferEnergy(nearbyNonfullExpansions[0]); | |
} | |
} | |
function handleAssault(creep, miningTarget) | |
{ | |
//check to see if creep.memory.lastHits === creep.hits | |
//If it's less, then do the assaulted tick | |
if(creep.memory.lastHits === undefined) | |
{ | |
creep.memory.lastHits = creep.hits; | |
} | |
if(creep.hits < creep.memory.lastHits) | |
{ | |
console.log(creep.name + ' ASSAULTED IN ' + creep.room.name); | |
miningTarget.ticksSinceAssault = 0; | |
miningTarget.assaulted = true; | |
creep.memory.lastHits = creep.hits; | |
} | |
} | |
function handleMemory(creep) | |
{ | |
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); | |
if(creep.carry.energy == 0) | |
{ | |
creep.moveTo(source); | |
} | |
} | |
else | |
{ | |
creep.moveTo(miningTargetPosition, {reusePath: 50, heuristicWeight:0.6}); | |
} | |
} | |
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) | |
{ | |
storage = creep.room.lookForAt("spawn", returnPosition.x, returnPosition.y)[0]; | |
} | |
if(storage === null) | |
{ | |
console.log("WARNING: INCORRECT STRUCTURE ASSIGNED FOR " + creep.name); | |
} | |
creep.transferEnergy(storage); | |
} | |
else | |
{ | |
creep.moveTo(returnPosition); | |
} | |
} | |
function handleProfitAnalysis(creep, miningTarget) | |
{ | |
creep.memory.hasMined = false; | |
if(creep.memory.revenue === undefined) | |
{ | |
creep.memory.revenue = 0; | |
} | |
if(creep.memory.tripCount === undefined) | |
{ | |
creep.memory.tripCount = 0; | |
} | |
miningTarget.revenue = creep.carryCapacity; | |
miningTarget.cost = (creep.memory.cost === undefined ? 1300 : creep.memory.cost); | |
if(miningTarget.ticksPerTrip === undefined) | |
{ | |
miningTarget.ticksPerTrip = []; | |
} | |
var ticksForThisTrip = creep.memory.ticksAtStartOfTrip - creep.ticksToLive; | |
miningTarget.ticksPerTrip.push(ticksForThisTrip); | |
if(miningTarget.ticksPerTrip.length > 20) miningTarget.ticksPerTrip.pop(); | |
creep.memory.ticksAtStartOfTrip = creep.ticksToLive; | |
creep.memory.revenue += creep.carryCapacity; | |
creep.memory.tripCount++; | |
//console.log(creep.name + ' has made ' + creep.memory.revenue + ' in revenue, ' + (creep.memory.revenue - (creep.memory.cost === undefined ? 1300 : creep.memory.cost)) + ' in profit | Trips: ' + creep.memory.tripCount + ' Ticks: ' + ticksForThisTrip + ' Ticks Left: ' + creep.ticksToLive + " | " + creep.memory.miningTarget); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment