Advertisement
SWAG98

Road Calculator

Mar 8th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var calculateRoads = function calculate(){
  2.     var queue = [];
  3.     for(let room1 in Game.rooms){
  4.         let r1 = Game.rooms[room1];
  5.         // dont execute when theres more than 50 construction sites
  6.         if(Object.keys(Game.constructionSites).length > 50){console.log("construction treshold reached");return}
  7.         // 1. Connections between rooms (from controller to controller or 25,25 of each room)
  8.         var r1_pos = new RoomPosition(25,25,r1.name);
  9.         var r1_coords = roomCoordinates.nameToCoordinates(r1.name);
  10.         if(r1.controller){
  11.             r1_pos = r1.controller.pos;
  12.         }
  13.         for(let room2 in Game.rooms){
  14.             let r2 = Game.rooms[room2];
  15.             var r2_coords = roomCoordinates.nameToCoordinates(r2.name);
  16.             if(Math.abs(r1_coords.x - r2_coords.x) + Math.abs(r1_coords.y - r2_coords.y) !== 1){ //if the rooms are not adjecant to eachother (or identical) continue with the next pair
  17.                 continue;
  18.             }
  19.             var r2_pos = new RoomPosition(25,25,r2.name);
  20.             if(r2.controller){
  21.                 r2_pos = r2.controller.pos;
  22.             }
  23.             console.log(r1_pos,r2_pos)
  24.             var path = PathFinder.search(r1_pos,r2_pos).path;
  25.             for(var i in path){
  26.                 var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  27.                 if(tile[0].type === "terrain" && tile[0].terrain !== "wall"){
  28.                     queue.push(path[i]);
  29.                 }
  30.             }
  31.         }
  32.         // 2. from controller to storage
  33.         if(r1.storage){
  34.             var path = PathFinder.search(r1.controller.pos,r1.storage.pos).path;
  35.             for(var i in path){
  36.                 var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  37.                 if(tile.terrain === "plain"){
  38.                     queue.push(path[i]);
  39.                 }
  40.             }
  41.         }
  42.        
  43.         //3. from controller to other structures
  44.         var structures = r1.find(FIND_STRUCTURES,{
  45.             filter:(structure)=>{
  46.                 return structure.my && (
  47.                     structure.structureType === STRUCTURE_EXTENSION ||
  48.                     structure.structureType === STRUCTURE_SPAWN ||
  49.                     structure.structureType === STRUCTURE_EXTRACTOR
  50.                 )
  51.             }
  52.         });
  53.        
  54.         if(structures){
  55.             for(let structure of structures){
  56.                 var path = PathFinder.search(r1.controller.pos,structure.pos).path;
  57.                 for(let point of path){
  58.                     var tile = Game.rooms[point.roomName].lookAt(point.x,point.y)
  59.                     if(tile.terrain === "plain"){
  60.                         queue.push(point);
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.        
  66.         //4. from controller to sources
  67.         var sources = r1.find(FIND_SOURCES)
  68.        
  69.         if(sources){
  70.             for(let source of sources){
  71.                 var pos1 = new RoomPosition(25,25,r1.name);
  72.                 if(r1.controller){
  73.                     pos1 = r1.controller.pos;
  74.                 }
  75.                 var path = PathFinder.search(pos1,source.pos).path;
  76.                 if(!path){continue}
  77.                 for(let point of path){
  78.                     var tile = Game.rooms[point.roomName].lookAt(point.x,point.y);
  79.                     if(tile.terrain === "plain"){
  80.                         queue.push(point);
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.  
  86.         //5. from sources to extensions
  87.         var extensions = r1.find(FIND_STRUCTURES,{
  88.             filter:(structure)=>{
  89.                 return structure.my && structure.structureType === STRUCTURE_EXTENSION
  90.             }
  91.         });
  92.        
  93.         for(let source of sources){
  94.             for(let extension of extensions){
  95.                 var path = PathFinder.search(extension.pos,source.pos).path;
  96.                 for(let point of path){
  97.                     var tile = Game.rooms[point.roomName].lookAt(point.x,point.y)
  98.                     if(tile.terrain === "plain"){
  99.                         queue.push(point);
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.        
  105.         //Set ConstructionSites
  106.         for(let point of queue){
  107.             if(Object.keys(Game.constructionSites).length > 50){continue;}
  108.             console.log("construction log: "+JSON.stringify(point)+" type: "+STRUCTURE_ROAD);
  109.             Game.rooms[point.roomName].createConstructionSite(point.x,point.y,STRUCTURE_ROAD);
  110.         }
  111.        
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement