UNPKG

@screeps/engine

Version:

This is a module for Screeps standalone server. See [main repository](https://github.com/screeps/screeps) for more info.

67 lines (54 loc) 2.3 kB
'use strict'; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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: x, y: y }); if (!container && dropToContainer) { container = { room: room, x: x, y: 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, _defineProperty({}, resourceType, container[resourceType])); amount -= toContainerAmount; } } if (amount > 0) { var existingDrop = _.find(roomObjects, { type: 'energy', x: x, y: y, resourceType: resourceType }); if (existingDrop) { bulk.update(existingDrop, _defineProperty({}, resourceType, existingDrop[resourceType] + amount)); } else { var _bulk$insert; bulk.insert((_bulk$insert = { type: 'energy', x: x, y: y, room: room }, _defineProperty(_bulk$insert, resourceType, amount), _defineProperty(_bulk$insert, 'resourceType', resourceType), _bulk$insert)); } } }; //# sourceMappingURL=../../../sourcemaps/processor/intents/creeps/_create-energy.js.map