thislooksfun

delegator.resource.js

Jun 25th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Generated by CoffeeScript 1.10.0
  2. var PATH, isTileWalkable, obj, transformArray, transforms;
  3.  
  4. module.exports = {
  5.   loadResources: function() {
  6.     var base, name, ref, room;
  7.     if (Memory.delegators == null) {
  8.       Memory.delegators = {};
  9.     }
  10.     if ((base = Memory.delegators).resources == null) {
  11.       base.resources = {};
  12.     }
  13.     ref = Game.rooms;
  14.     for (name in ref) {
  15.       room = ref[name];
  16.       if (Memory.delegators.resources[name] != null) {
  17.         continue;
  18.       }
  19.       this.loadResourcesForRoom(room);
  20.       break;
  21.     }
  22.   },
  23.   getFreeSourceHarvestLocationInRoom: function(room, flagName) {
  24.     var data, flag, flags, j, len, path, pos, posStr, ref, ref1, ref2, ref3, resources, x, y;
  25.     resources = (ref = Memory.delegators) != null ? (ref1 = ref.resources) != null ? ref1[room.name] : void 0 : void 0;
  26.     if (resources == null) {
  27.       this.loadResources();
  28.       resources = (ref2 = Memory.delegators) != null ? (ref3 = ref2.resources) != null ? ref3[room.name] : void 0 : void 0;
  29.       if (resources == null) {
  30.         return null;
  31.       }
  32.     }
  33.     for (posStr in resources) {
  34.       data = resources[posStr];
  35.       if (!data.freeTiles) {
  36.         continue;
  37.       }
  38.       pos = posStr.split(',');
  39.       x = parseInt(pos[0]);
  40.       y = parseInt(pos[1]);
  41.       path = 0;
  42.       if (data.freeTiles & PATH.UP_LEFT) {
  43.         path = PATH.UP_LEFT;
  44.         x -= 1;
  45.         y -= 1;
  46.       } else if (data.freeTiles & PATH.UP) {
  47.         path = PATH.UP;
  48.         y -= 1;
  49.       } else if (data.freeTiles & PATH.UP_RIGHT) {
  50.         path = PATH.UP_RIGHT;
  51.         x += 1;
  52.         y -= 1;
  53.       } else if (data.freeTiles & PATH.LEFT) {
  54.         path = PATH.LEFT;
  55.         x -= 1;
  56.       } else if (data.freeTiles & PATH.RIGHT) {
  57.         path = PATH.RIGHT;
  58.         x += 1;
  59.       } else if (data.freeTiles & PATH.DOWN_LEFT) {
  60.         path = PATH.DOWN_LEFT;
  61.         x -= 1;
  62.         y += 1;
  63.       } else if (data.freeTiles & PATH.DOWN) {
  64.         path = PATH.DOWN;
  65.         y += 1;
  66.       } else if (data.freeTiles & PATH.DOWN_RIGHT) {
  67.         path = PATH.DOWN_RIGHT;
  68.         x += 1;
  69.         y += 1;
  70.       }
  71.       data.freeTiles &= ~path;
  72.       flags = room.lookForAt(LOOK_FLAGS, x, y);
  73.       for (j = 0, len = flags.length; j < len; j++) {
  74.         flag = flags[j];
  75.         flag.remove();
  76.       }
  77.       if (flagName != null) {
  78.         room.createFlag(x, y, flagName, COLOR_YELLOW);
  79.       }
  80.       return {
  81.         sourceID: data.sourceID,
  82.         pos: new RoomPosition(x, y, room.name),
  83.         path: path
  84.       };
  85.     }
  86.     return null;
  87.   },
  88.   stoppedHarvestingAt: function(arg) {
  89.     var flag, flags, j, len, path, pos, ref, ref1, resources, room, transform, x, y;
  90.     pos = arg.pos, path = arg.path;
  91.     resources = (ref = Memory.delegators) != null ? (ref1 = ref.resources) != null ? ref1[pos.roomName] : void 0 : void 0;
  92.     transform = transforms["" + path];
  93.     x = pos.x - transform.x;
  94.     y = pos.y - transform.y;
  95.     resources[x + "," + y].freeTiles |= path;
  96.     room = Game.rooms[pos.roomName];
  97.     flags = room.lookForAt(LOOK_FLAGS, pos.x, pos.y);
  98.     for (j = 0, len = flags.length; j < len; j++) {
  99.       flag = flags[j];
  100.       flag.remove();
  101.     }
  102.   },
  103.   loadResourcesForRoom: function(room) {
  104.     var j, len, paths, source, sources;
  105.     paths = {};
  106.     sources = room.find(FIND_SOURCES);
  107.     for (j = 0, len = sources.length; j < len; j++) {
  108.       source = sources[j];
  109.       paths[source.pos.x + "," + source.pos.y] = this.calculateValidPathsForSource(source, room);
  110.     }
  111.     Memory.delegators.resources[room.name] = paths;
  112.   },
  113.   calculateValidPathsForSource: function(source, room) {
  114.     var count, j, len, sourceX, sourceY, tiles, transform;
  115.     sourceX = source.pos.x;
  116.     sourceY = source.pos.y;
  117.     tiles = 0;
  118.     count = 0;
  119.     for (j = 0, len = transformArray.length; j < len; j++) {
  120.       transform = transformArray[j];
  121.       if (isTileWalkable(room, sourceX + transform.x, sourceY + transform.y)) {
  122.         tiles |= transform.path;
  123.         count++;
  124.       }
  125.     }
  126.     return {
  127.       sourceID: source.id,
  128.       freeTiles: tiles
  129.     };
  130.   }
  131. };
  132.  
  133. PATH = {
  134.   UP_LEFT: 1 << 0,
  135.   UP: 1 << 1,
  136.   UP_RIGHT: 1 << 2,
  137.   LEFT: 1 << 3,
  138.   RIGHT: 1 << 4,
  139.   DOWN_LEFT: 1 << 5,
  140.   DOWN: 1 << 6,
  141.   DOWN_RIGHT: 1 << 7
  142. };
  143.  
  144. transforms = (
  145.   obj = {},
  146.   obj["" + PATH.UP_LEFT] = {
  147.     x: -1,
  148.     y: -1
  149.   },
  150.   obj["" + PATH.UP] = {
  151.     x: 0,
  152.     y: -1
  153.   },
  154.   obj["" + PATH.UP_RIGHT] = {
  155.     x: 1,
  156.     y: -1
  157.   },
  158.   obj["" + PATH.LEFT] = {
  159.     x: -1,
  160.     y: 0
  161.   },
  162.   obj["" + PATH.RIGHT] = {
  163.     x: 1,
  164.     y: 0
  165.   },
  166.   obj["" + PATH.DOWN_LEFT] = {
  167.     x: -1,
  168.     y: 1
  169.   },
  170.   obj["" + PATH.DOWN] = {
  171.     x: 0,
  172.     y: 1
  173.   },
  174.   obj["" + PATH.DOWN_RIGHT] = {
  175.     x: 1,
  176.     y: 1
  177.   },
  178.   obj
  179. );
  180.  
  181. transformArray = [
  182.   {
  183.     x: -1,
  184.     y: -1,
  185.     path: PATH.UP_LEFT
  186.   }, {
  187.     x: 0,
  188.     y: -1,
  189.     path: PATH.UP
  190.   }, {
  191.     x: 1,
  192.     y: -1,
  193.     path: PATH.UP_RIGHT
  194.   }, {
  195.     x: -1,
  196.     y: 0,
  197.     path: PATH.LEFT
  198.   }, {
  199.     x: 1,
  200.     y: 0,
  201.     path: PATH.RIGHT
  202.   }, {
  203.     x: -1,
  204.     y: 1,
  205.     path: PATH.DOWN_LEFT
  206.   }, {
  207.     x: 0,
  208.     y: 1,
  209.     path: PATH.DOWN
  210.   }, {
  211.     x: 1,
  212.     y: 1,
  213.     path: PATH.DOWN_RIGHT
  214.   }
  215. ];
  216.  
  217. isTileWalkable = function(room, x, y) {
  218.   var i, item, items, j, len, obstructed;
  219.   items = room.lookAt(x, y);
  220.   obstructed = false;
  221.   for (i = j = 0, len = items.length; j < len; i = ++j) {
  222.     item = items[i];
  223.     if (item.type === LOOK_CREEPS) {
  224.       continue;
  225.     }
  226.     if (OBSTACLE_OBJECT_TYPES.includes(item.type) || OBSTACLE_OBJECT_TYPES.includes(item[item.type])) {
  227.       obstructed = true;
  228.       break;
  229.     }
  230.   }
  231.   return !obstructed;
  232. };
Advertisement
Add Comment
Please, Sign In to add comment