Advertisement
Guest User

Untitled

a guest
Nov 13th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package Core;
  2.  
  3. import java.awt.Rectangle;
  4. import java.util.HashSet;
  5. import java.util.Random;
  6. import java.util.Set;
  7. import java.util.Stack;
  8.  
  9. import Base.custom.PairMap;
  10. import Base.custom.SetPairMap;
  11.  
  12. public class World {
  13.    
  14.     public Set<Rectangle> rooms = new HashSet<>();
  15.     public RectSplitter rs;
  16.    
  17.     public Rectangle[][] roomlanding;
  18.    
  19.     public int width,height;
  20.    
  21.     public World(int width, int height){
  22.         this.width = width;
  23.         this.height = height;
  24.         rs = new RectSplitter(width,height);
  25.         bounds = rs.start.rect;
  26.         roomlanding = new Rectangle[height][width];
  27.         rooms = rs.Split(4, 4);
  28.        
  29.         for(Rectangle r: rooms){
  30.             for(int x = 0; x < r.width; x++){
  31.                 for(int y = 0; y < r.height; y++){
  32.                     roomlanding[y+r.y][x+r.x] = r;     
  33.                 }
  34.             }
  35.         }
  36.         searchRooms();
  37.     }
  38.    
  39.     Rectangle bounds;
  40.     public Set<Rectangle> marked = new HashSet<>();
  41.     public Set<Rectangle> path = new HashSet<Rectangle>();
  42.  
  43.     Random r = new Random();
  44.     Rectangle current;
  45.     Stack<Rectangle> next = new Stack<>();
  46.     boolean firstrun = true;
  47.    
  48.     Rectangle donotplace;
  49.    
  50.    
  51.     SetPairMap<Rectangle,Rectangle> adjacentnodes = new SetPairMap<>();
  52.     PairMap<Rectangle,Rectangle> adjacentnodesB = new PairMap<>();
  53.    
  54.     public void pathRooms(){
  55.         while(true){
  56.             searchRooms();
  57.             if(next.isEmpty()){
  58.                 break;
  59.             }
  60.         }
  61.         removeEnds();
  62.     }
  63.    
  64.     public void searchRooms(){
  65.         if(firstrun){
  66.         donotplace = new Rectangle(width/4,height/4,width/2,height/2);
  67.         current = roomlanding[(int)height/2][(int)width/2];    
  68.         next.push(current);
  69.         firstrun = false;
  70.        
  71.             for(int x = 0; x < donotplace.width; x++){
  72.                 for(int y = 0; y < donotplace.height; y++){
  73.                     roomlanding[y+donotplace.x][x+donotplace.y] = null;
  74.                 }
  75.             }
  76.        
  77.         }
  78.         if(current != null){
  79.             marked.add(current);
  80.             path.add(current);
  81.            
  82.             SetPairMap<Integer,Rectangle> adjacent = getAdjacent(current,true);        
  83.        
  84.             PairMap<Rectangle,Integer> choices = new PairMap<>();  
  85.             PairMap<Rectangle,Rectangle> parent = new PairMap<>();
  86.                 Set<Rectangle> adjB;               
  87.                     for(int i : adjacent.getKeys()){
  88.                         for(Rectangle rA : adjacent.get(i)){
  89.                             adjB = getAdjacent(rA,true).get(i);
  90.                                                        
  91.                             if(adjB == null){
  92.                                 marked.add(rA);
  93.                                 continue;
  94.                             }
  95.                             for(Rectangle rB : getAdjacent(rA,true).get(i)){
  96.                                 if(current.equals(rB)){
  97.                                     continue;
  98.                                 }
  99.                                
  100.                                 if(r.nextBoolean() && (rA.x % 5 == 0 || rA.y % 5 == 0)){
  101.                                    
  102.                                     continue;                                  
  103.                                 }
  104.                                     if(!marked.contains(rA) && !marked.contains(rB)){                                      
  105.                                         choices.put(rB,i); 
  106.                                         parent.put(rB, rA);                                
  107.                                     }
  108.                                     if(!marked.contains(rA) && marked.contains(rB)){
  109.                                         marked.add(rA);
  110.                                     }                          
  111.                         }
  112.                     }
  113.                    
  114.                
  115.             }
  116.             if(choices.size() > 0){            
  117.                 int rnum = r.nextInt(choices.getValues().size());
  118.                 int rDir = (int)choices.getValues().toArray()[rnum];
  119.                
  120.                 int oppdir = 0;
  121.                 switch(rDir){
  122.                 case 0:
  123.                     oppdir = 1;
  124.                     break;
  125.                 case 1:
  126.                     oppdir = 0;
  127.                     break;
  128.                 case 2:
  129.                     oppdir = 3;
  130.                     break;
  131.                 case 3:
  132.                     oppdir = 2;
  133.                     break;                 
  134.                 }
  135.                
  136.                 SetPairMap<Integer, Rectangle> adj = getAdjacent(current,true);
  137.                
  138.                 Rectangle nextrect = choices.getAvailableKeyOfValue(rDir);
  139.                 Rectangle parentrect = parent.get(nextrect);
  140.                 if(adj.get(oppdir) != null){
  141.                 marked.addAll(adj.get(oppdir));
  142.                 }
  143.                 adj = getAdjacent(parentrect,true);
  144.                 //marked.addAll(adj.getValues());
  145.                 path.add(nextrect);
  146.                 path.add(parentrect);
  147.                 marked.add(nextrect);
  148.                 marked.add(parentrect);
  149.                
  150.                 adjacentnodes.put(nextrect,current);
  151.                 current = next.push(nextrect);             
  152.             }else
  153.                 if(!next.isEmpty()){
  154.                     current = next.pop();
  155.                 }
  156.             }
  157.        
  158.        
  159.     }
  160.    
  161.     public SetPairMap<Integer,Rectangle> getAdjacent(Rectangle box,boolean biased){
  162.         SetPairMap<Integer, Rectangle> boxes=  new SetPairMap<>();
  163.             if(box == null){
  164.                 return boxes;
  165.             }
  166.             for(Rectangle r: rooms){
  167.                 if(r.equals(box)){
  168.                     continue;
  169.                 }
  170.                 for(int x = -1; x < 2; x++){
  171.                     for(int y = -1; y < 2; y++){
  172.                        
  173.                         if(x == y || (x != 0 && y !=0)){
  174.                             continue;
  175.                         }
  176.                         Rectangle nB = new Rectangle(box.x+x,box.y+y,box.width,box.height);
  177.                         if(r.intersects(nB)){
  178.                             int dir = 0;
  179.                             if(y == -1){
  180.                                 dir = 0;
  181.                             }
  182.                             if(y == 1){
  183.                                 dir = 1;
  184.                             }
  185.                             if(x == -1){
  186.                                 dir = 2;
  187.                             }
  188.                             if(x == 1){
  189.                                 dir = 3;
  190.                             }
  191.                            
  192.                             //if(donotplace.contains(box) || donotplace.intersects(box)){                          
  193.                             //boxes.put(dir, r);
  194.                             //}else{
  195.                             //if(!(donotplace.intersects(r) || donotplace.contains(r))){
  196.                             //  boxes.put(dir, r);
  197.                             //}
  198.                             //}
  199.                            
  200.                             if((!marked.contains(r) && biased) || !biased){
  201.                                 boxes.put(dir, r);
  202.                             }
  203.                            
  204.                         }
  205.                     }
  206.                 }
  207.             }
  208.        
  209.         return boxes;
  210.     }
  211.    
  212.     public void removeEnds(){      
  213.         Set<Rectangle> nocheck = new HashSet<>();
  214.         for(int i = 0 ; i < 15; i++){
  215.             for(Rectangle r: adjacentnodes.getKeys()){
  216.                 if(nocheck.contains(r)){
  217.                     //continue;
  218.                 }                      
  219.                         Set<Rectangle> adj = getAdjacent(r,false).getValues();
  220.                         Set<Rectangle> inverse = new HashSet<>(adj);
  221.                         adj.removeAll(path);
  222.                         inverse.removeAll(adj);
  223.                         //System.out.println(adj);
  224.                         if(inverse.size() < 2){
  225.                             path.remove(r);
  226.                             path.removeAll(inverse);
  227.                             if(new Random().nextInt(100) < 2){
  228.                             //nocheck.add(r);
  229.                             }
  230.                         }                  
  231.             }
  232.         }
  233.     }
  234.    
  235.     public SetPairMap<Rectangle,Rectangle> doormap = new SetPairMap<>();
  236.     public void placeDoors(){      
  237.        
  238.         for(Rectangle r: path){
  239.             for(Rectangle adj: getAdjacent(r, false).getValues()){
  240.                 if(path.contains(adj) && (!doormap.containsKey(adj) || doormap.get(adj).size() < 1)){
  241.                     doormap.put(r, adj);
  242.                     doormap.put(adj, r);
  243.                 }
  244.             }
  245.         }      
  246.     }
  247.    
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement