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