Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Road Calculator
- var roads = function (){
- var queue = {};
- for(var r1 in Game.rooms){
- // dont execute when theres more than 50 construction sites
- if(Game.constructionSites.length > 50){return}
- // 1. Connections between rooms (from controller to controller or 25,25 of each room)
- var r1_pos = new RoomPosition(25,25,r1);
- var r1_coords = roomCoordinates.nameToCoordinates(r1);
- if(Game.rooms[r1].controller){
- r1_pos = Game.rooms[r1].controller.pos;
- }
- for(var r2 in Game.rooms){
- var r2_coords = roomCoordinates.nameToCoordinates(r2);
- 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
- continue;
- }
- var r2_pos = new RoomPosition(25,25,r2);
- if(Game.rooms[r1].controller){
- r2_pos = Game.rooms[r2].controller.pos;
- }
- var path = PathFinder.search(r1_pos,r1_pos).path;
- for(var i in path){
- var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
- if(tile.terrain === "plain"){
- queue.push(path[i]);
- }
- }
- }
- // 2. from controller to storage
- if(Game.rooms[r1].storage){
- var path = PathFinder.search(Game.rooms[r1].controller.pos,Game.rooms[r1].storage.pos).path;
- for(var i in path){
- var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
- if(tile.terrain === "plain"){
- queue.push(path[i]);
- }
- }
- }
- //3. from controller to other structures
- var structures = Game.rooms[r1].find(FIND_STRUCTURES,{
- filter:(structure)=>{
- return structure.my && (
- structure.structureType === STRUCTURE_EXTENSION ||
- structure.structureType === STRUCTURE_SPAWN ||
- structure.structureType === STRUCTURE_EXTRACTOR
- )
- }
- });
- if(structures){
- for(var structure in structures){
- var path = PathFinder.search(Game.rooms[r1].controller.pos,structures[structure].pos).path;
- for(var i in path){
- var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
- if(tile.terrain === "plain"){
- queue.push(path[i]);
- }
- }
- }
- }
- //4. from controller to sources
- var sources = Game.room[r1].find(FIND_SOURCES)
- if(sources){
- for(var source in sources){
- var path = PathFinder.search(Game.rooms[r1].controller.pos,sources[source].pos).path;
- for(var i in path){
- var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
- if(tile.terrain === "plain"){
- queue.push(path[i]);
- }
- }
- }
- }
- //4. from sources to extensions
- var extensions = Game.rooms[r1].find(FIND_STRUCTURES,{
- filter:(structure)=>{
- return structure.my && structure.structureType === STRUCTURE_EXTENSION
- }
- });
- for(source in sources){
- for(extension in extensions){
- var path = PathFinder.search(extensions[extension].pos,sources[source].pos).path;
- for(var i in path){
- var tile = Game.rooms[path[i].roomName].lookAt(path[i].x,path[i].y)
- if(tile.terrain === "plain"){
- queue.push(path[i]);
- }
- }
- }
- }
- //Set ConstructionSites
- for(var i in queue){
- Game.rooms[queue[i].roomName].createConstructionSite(queue[i].x,queue[i].y,STRUCTURE_ROAD);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment