@screeps/engine
Version:
This is a module for Screeps standalone server. See [main repository](https://github.com/screeps/screeps) for more info.
64 lines (55 loc) • 1.89 kB
JavaScript
var _ = require('lodash'),
utils = require('../../../utils'),
driver = utils.getDriver(),
C = driver.constants;
module.exports = function(x, y, room, amount, roomObjects, bulk, resourceType, dropToContainer) {
resourceType = resourceType || 'energy';
amount = Math.round(amount);
if(amount <= 0) {
return;
}
var container = _.find(roomObjects, {type: 'container', x, y});
if(!container && dropToContainer) {
container = {
room,
x,
y,
type: 'container',
energyCapacity: 0,
hits: C.CONTAINER_HITS,
hitsMax: 0
};
container._id = bulk.insert(container);
roomObjects[container._id] = container;
}
if(container && container.hits > 0) {
var targetTotal = utils.calcResources(container);
var toContainerAmount = Math.min(amount, container.energyCapacity - targetTotal);
if(dropToContainer) {
toContainerAmount = amount;
}
if(toContainerAmount > 0) {
container[resourceType] = container[resourceType] || 0;
container[resourceType] += toContainerAmount;
bulk.update(container, {[resourceType]: container[resourceType]});
amount -= toContainerAmount;
}
}
if(amount > 0) {
var existingDrop = _.find(roomObjects, {type: 'energy', x, y, resourceType});
if (existingDrop) {
bulk.update(existingDrop, {
[resourceType]: existingDrop[resourceType] + amount
});
}
else {
bulk.insert({
type: 'energy',
x, y,
room: room,
[resourceType]: amount,
resourceType
})
}
}
};