Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generated by CoffeeScript 1.10.0
- var PATH, isTileWalkable, obj, transformArray, transforms;
- module.exports = {
- loadResources: function() {
- var base, name, ref, room;
- if (Memory.delegators == null) {
- Memory.delegators = {};
- }
- if ((base = Memory.delegators).resources == null) {
- base.resources = {};
- }
- ref = Game.rooms;
- for (name in ref) {
- room = ref[name];
- if (Memory.delegators.resources[name] != null) {
- continue;
- }
- this.loadResourcesForRoom(room);
- break;
- }
- },
- getFreeSourceHarvestLocationInRoom: function(room, flagName) {
- var data, flag, flags, j, len, path, pos, posStr, ref, ref1, ref2, ref3, resources, x, y;
- resources = (ref = Memory.delegators) != null ? (ref1 = ref.resources) != null ? ref1[room.name] : void 0 : void 0;
- if (resources == null) {
- this.loadResources();
- resources = (ref2 = Memory.delegators) != null ? (ref3 = ref2.resources) != null ? ref3[room.name] : void 0 : void 0;
- if (resources == null) {
- return null;
- }
- }
- for (posStr in resources) {
- data = resources[posStr];
- if (!data.freeTiles) {
- continue;
- }
- pos = posStr.split(',');
- x = parseInt(pos[0]);
- y = parseInt(pos[1]);
- path = 0;
- if (data.freeTiles & PATH.UP_LEFT) {
- path = PATH.UP_LEFT;
- x -= 1;
- y -= 1;
- } else if (data.freeTiles & PATH.UP) {
- path = PATH.UP;
- y -= 1;
- } else if (data.freeTiles & PATH.UP_RIGHT) {
- path = PATH.UP_RIGHT;
- x += 1;
- y -= 1;
- } else if (data.freeTiles & PATH.LEFT) {
- path = PATH.LEFT;
- x -= 1;
- } else if (data.freeTiles & PATH.RIGHT) {
- path = PATH.RIGHT;
- x += 1;
- } else if (data.freeTiles & PATH.DOWN_LEFT) {
- path = PATH.DOWN_LEFT;
- x -= 1;
- y += 1;
- } else if (data.freeTiles & PATH.DOWN) {
- path = PATH.DOWN;
- y += 1;
- } else if (data.freeTiles & PATH.DOWN_RIGHT) {
- path = PATH.DOWN_RIGHT;
- x += 1;
- y += 1;
- }
- data.freeTiles &= ~path;
- flags = room.lookForAt(LOOK_FLAGS, x, y);
- for (j = 0, len = flags.length; j < len; j++) {
- flag = flags[j];
- flag.remove();
- }
- if (flagName != null) {
- room.createFlag(x, y, flagName, COLOR_YELLOW);
- }
- return {
- sourceID: data.sourceID,
- pos: new RoomPosition(x, y, room.name),
- path: path
- };
- }
- return null;
- },
- stoppedHarvestingAt: function(arg) {
- var flag, flags, j, len, path, pos, ref, ref1, resources, room, transform, x, y;
- pos = arg.pos, path = arg.path;
- resources = (ref = Memory.delegators) != null ? (ref1 = ref.resources) != null ? ref1[pos.roomName] : void 0 : void 0;
- transform = transforms["" + path];
- x = pos.x - transform.x;
- y = pos.y - transform.y;
- resources[x + "," + y].freeTiles |= path;
- room = Game.rooms[pos.roomName];
- flags = room.lookForAt(LOOK_FLAGS, pos.x, pos.y);
- for (j = 0, len = flags.length; j < len; j++) {
- flag = flags[j];
- flag.remove();
- }
- },
- loadResourcesForRoom: function(room) {
- var j, len, paths, source, sources;
- paths = {};
- sources = room.find(FIND_SOURCES);
- for (j = 0, len = sources.length; j < len; j++) {
- source = sources[j];
- paths[source.pos.x + "," + source.pos.y] = this.calculateValidPathsForSource(source, room);
- }
- Memory.delegators.resources[room.name] = paths;
- },
- calculateValidPathsForSource: function(source, room) {
- var count, j, len, sourceX, sourceY, tiles, transform;
- sourceX = source.pos.x;
- sourceY = source.pos.y;
- tiles = 0;
- count = 0;
- for (j = 0, len = transformArray.length; j < len; j++) {
- transform = transformArray[j];
- if (isTileWalkable(room, sourceX + transform.x, sourceY + transform.y)) {
- tiles |= transform.path;
- count++;
- }
- }
- return {
- sourceID: source.id,
- freeTiles: tiles
- };
- }
- };
- PATH = {
- UP_LEFT: 1 << 0,
- UP: 1 << 1,
- UP_RIGHT: 1 << 2,
- LEFT: 1 << 3,
- RIGHT: 1 << 4,
- DOWN_LEFT: 1 << 5,
- DOWN: 1 << 6,
- DOWN_RIGHT: 1 << 7
- };
- transforms = (
- obj = {},
- obj["" + PATH.UP_LEFT] = {
- x: -1,
- y: -1
- },
- obj["" + PATH.UP] = {
- x: 0,
- y: -1
- },
- obj["" + PATH.UP_RIGHT] = {
- x: 1,
- y: -1
- },
- obj["" + PATH.LEFT] = {
- x: -1,
- y: 0
- },
- obj["" + PATH.RIGHT] = {
- x: 1,
- y: 0
- },
- obj["" + PATH.DOWN_LEFT] = {
- x: -1,
- y: 1
- },
- obj["" + PATH.DOWN] = {
- x: 0,
- y: 1
- },
- obj["" + PATH.DOWN_RIGHT] = {
- x: 1,
- y: 1
- },
- obj
- );
- transformArray = [
- {
- x: -1,
- y: -1,
- path: PATH.UP_LEFT
- }, {
- x: 0,
- y: -1,
- path: PATH.UP
- }, {
- x: 1,
- y: -1,
- path: PATH.UP_RIGHT
- }, {
- x: -1,
- y: 0,
- path: PATH.LEFT
- }, {
- x: 1,
- y: 0,
- path: PATH.RIGHT
- }, {
- x: -1,
- y: 1,
- path: PATH.DOWN_LEFT
- }, {
- x: 0,
- y: 1,
- path: PATH.DOWN
- }, {
- x: 1,
- y: 1,
- path: PATH.DOWN_RIGHT
- }
- ];
- isTileWalkable = function(room, x, y) {
- var i, item, items, j, len, obstructed;
- items = room.lookAt(x, y);
- obstructed = false;
- for (i = j = 0, len = items.length; j < len; i = ++j) {
- item = items[i];
- if (item.type === LOOK_CREEPS) {
- continue;
- }
- if (OBSTACLE_OBJECT_TYPES.includes(item.type) || OBSTACLE_OBJECT_TYPES.includes(item[item.type])) {
- obstructed = true;
- break;
- }
- }
- return !obstructed;
- };
Advertisement
Add Comment
Please, Sign In to add comment