Skip to content

Commit

Permalink
feat: new method StructureLab.unboostCreep
Browse files Browse the repository at this point in the history
  • Loading branch information
o4kapuk committed Sep 12, 2018
1 parent efc703f commit 49469d4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/game/structures.js
Expand Up @@ -496,6 +496,25 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
return C.OK; return C.OK;
}); });


StructureLab.prototype.unboostCreep = register.wrapFn(function(target) {
if(!target || !target.id || !register.creeps[target.id] || !(target instanceof globals.Creep)) {
register.assertTargetObject(target);
return C.ERR_INVALID_TARGET;
}
if(!this.my || !target.my) {
return C.ERR_NOT_OWNER;
}
if(!_.some(target.body, p => !!p.boost)) {
return C.ERR_NOT_FOUND;
}
if(!this.pos.isNearTo(target)) {
return C.ERR_NOT_IN_RANGE;
}

intents.set(this.id, 'unboostCreep', {id: target.id});
return C.OK;
});

Object.defineProperty(globals, 'StructureLab', {enumerable: true, value: StructureLab}); Object.defineProperty(globals, 'StructureLab', {enumerable: true, value: StructureLab});


/** /**
Expand Down
3 changes: 3 additions & 0 deletions src/processor/intents/labs/intents.js
Expand Up @@ -10,4 +10,7 @@ module.exports = function(object, objectIntents, scope) {


if(objectIntents.boostCreep) if(objectIntents.boostCreep)
require('./boost-creep')(object, objectIntents.boostCreep, scope); require('./boost-creep')(object, objectIntents.boostCreep, scope);

if(objectIntents.unboostCreep)
require('./unboost-creep')(object, objectIntents.unboostCreep, roomObjects, roomTerrain, bulk, bulkUsers, roomController, stats);
}; };
38 changes: 38 additions & 0 deletions src/processor/intents/labs/unboost-creep.js
@@ -0,0 +1,38 @@
const _ = require('lodash'),
utils = require('../../../utils'),
driver = utils.getDriver(),
C = driver.constants;

module.exports = function(object, intent, roomObjects, roomTerrain, bulk) {
const target = roomObjects[intent.id];
if(!target || target.type != 'creep' || target.user != object.user) {
return;
}
if(Math.abs(target.x - object.x) > 1 || Math.abs(target.y - object.y) > 1) {
return;
}
const boostedParts = _.mapValues(_.groupBy(_.filter(target.body,p=>!!p.boost), 'boost'), v=>v.length);
if(!_.some(boostedParts)) {
return;
}

target.body.forEach(function(p) {p.boost = null;});
require('../creeps/_recalc-body')(target);
bulk.update(target, {body: target.body, energyCapacity: target.energyCapacity});

C.RESOURCES_ALL.forEach(function(r){
if(!boostedParts[r]) {
return;
}

const energyReturn = boostedParts[r]*C.LAB_UNBOOST_ENERGY;
if(energyReturn>0) {
require('../creeps/_create-energy')(target.x, target.y, target.room, energyReturn, roomObjects, bulk, C.RESOURCE_ENERGY);
}

const mineralReturn = boostedParts[r]*C.LAB_UNBOOST_MINERAL;
if(mineralReturn > 0) {
require('../creeps/_create-energy')(target.x, target.y, target.room, mineralReturn, roomObjects, bulk, r);
}
});
};
5 changes: 5 additions & 0 deletions src/utils.js
Expand Up @@ -783,6 +783,11 @@ exports.storeIntents = function(userId, userIntents, userRuntimeData) {
bodyPartsCount: parseInt(objectIntentsResult.boostCreep.bodyPartsCount) bodyPartsCount: parseInt(objectIntentsResult.boostCreep.bodyPartsCount)
}; };
} }
if(objectIntentsResult.unboostCreep) {
objectIntents.unboostCreep = {
id: ""+objectIntentsResult.unboostCreep.id
}
}
if(objectIntentsResult.send) { if(objectIntentsResult.send) {
objectIntents.send = { objectIntents.send = {
targetRoomName: ""+objectIntentsResult.send.targetRoomName, targetRoomName: ""+objectIntentsResult.send.targetRoomName,
Expand Down

0 comments on commit 49469d4

Please sign in to comment.