SWAG98

Screeps Road calculator (untested)

Mar 5th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Road Calculator
  2. var roads = function (){
  3.     var queue = {};
  4.     for(var r1 in Game.rooms){
  5.         // dont execute when theres more than 50 construction sites
  6.         if(Game.constructionSites.length > 50){return}
  7.        
  8.         // 1. Connections between rooms (from controller to controller or 25,25 of each room)
  9.         var r1_pos = new RoomPosition(25,25,r1);
  10.         var r1_coords = roomCoordinates.nameToCoordinates(r1);
  11.         if(Game.rooms[r1].controller){
  12.             r1_pos = Game.rooms[r1].controller.pos;
  13.         }
  14.         for(var r2 in Game.rooms){
  15.             var r2_coords = roomCoordinates.nameToCoordinates(r2);
  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);
  20.             if(Game.rooms[r1].controller){
  21.                 r2_pos = Game.rooms[r2].controller.pos;
  22.             }
  23.            
  24.             var path = PathFinder.search(r1_pos,r1_pos).path;
  25.            
  26.             for(var i in path){
  27.                 var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  28.                 if(tile.terrain === "plain"){
  29.                     queue.push(path[i]);
  30.                 }
  31.             }
  32.         }
  33.         // 2. from controller to storage
  34.         if(Game.rooms[r1].storage){
  35.             var path = PathFinder.search(Game.rooms[r1].controller.pos,Game.rooms[r1].storage.pos).path;
  36.             for(var i in path){
  37.                 var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  38.                 if(tile.terrain === "plain"){
  39.                     queue.push(path[i]);
  40.                 }
  41.             }
  42.         }
  43.         //3. from controller to other structures
  44.         var structures = Game.rooms[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(var structure in structures){
  56.                 var path = PathFinder.search(Game.rooms[r1].controller.pos,structures[structure].pos).path;
  57.                 for(var i in path){
  58.                     var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  59.                     if(tile.terrain === "plain"){
  60.                         queue.push(path[i]);
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.         //4. from controller to sources
  66.         var sources = Game.room[r1].find(FIND_SOURCES)
  67.        
  68.         if(sources){
  69.             for(var source in sources){
  70.                 var path = PathFinder.search(Game.rooms[r1].controller.pos,sources[source].pos).path;
  71.                 for(var i in path){
  72.                     var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  73.                     if(tile.terrain === "plain"){
  74.                         queue.push(path[i]);
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.         //4. from sources to extensions
  80.         var extensions = Game.rooms[r1].find(FIND_STRUCTURES,{
  81.             filter:(structure)=>{
  82.                 return structure.my && structure.structureType === STRUCTURE_EXTENSION
  83.             }
  84.         });
  85.        
  86.         for(source in sources){
  87.             for(extension in extensions){
  88.                 var path = PathFinder.search(extensions[extension].pos,sources[source].pos).path;
  89.                 for(var i in path){
  90.                     var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
  91.                     if(tile.terrain === "plain"){
  92.                         queue.push(path[i]);
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.        
  98.         //Set ConstructionSites
  99.         for(var i in queue){
  100.             Game.rooms[queue[i].roomName].createConstructionSite(queue[i].x,queue[i].y,STRUCTURE_ROAD);
  101.         }
  102.        
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment