Advertisement
Guest User

WorldGen

a guest
Nov 12th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 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(6, 6);
  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.     SetPairMap<Rectangle,Rectangle> adjacentnodes = new SetPairMap<>();
  51.     PairMap<Rectangle,Rectangle> adjacentnodesB = new PairMap<>();
  52.    
  53.     public void pathRooms(){
  54.         while(true){
  55.             searchRooms();
  56.             if(next.isEmpty()){
  57.                 break;
  58.             }
  59.         }
  60.         removeEnds();
  61.     }
  62.    
  63.     public void searchRooms(){
  64.         if(firstrun){
  65.         donotplace = new Rectangle(width/4,height/4,width/2,height/2);
  66.         current = roomlanding[(int)height/2][(int)width/2];    
  67.         next.push(current);
  68.         firstrun = false;
  69.        
  70.             for(int x = 0; x < donotplace.width; x++){
  71.                 for(int y = 0; y < donotplace.height; y++){
  72.                     roomlanding[y+donotplace.x][x+donotplace.y] = null;
  73.                 }
  74.             }
  75.        
  76.         }
  77.         if(current != null){
  78.             marked.add(current);
  79.             path.add(current);
  80.            
  81.             SetPairMap<Integer,Rectangle> adjacent = getAdjacent(current,true);        
  82.        
  83.             PairMap<Rectangle,Integer> choices = new PairMap<>();  
  84.             PairMap<Rectangle,Rectangle> parent = new PairMap<>();
  85.                 Set<Rectangle> adjB;               
  86.                     for(int i : adjacent.getKeys()){
  87.                         for(Rectangle rA : adjacent.get(i)){
  88.                             adjB = getAdjacent(rA,true).get(i);
  89.                                                        
  90.                             if(adjB == null){
  91.                                 marked.add(rA);
  92.                                 continue;
  93.                             }
  94.                             for(Rectangle rB : getAdjacent(rA,true).get(i)){
  95.                                 if(current.equals(rB)){
  96.                                     continue;
  97.                                 }
  98.                                
  99.                                 if(r.nextBoolean() && (rA.x % 5 == 0 || rA.y % 5 == 0)){
  100.                                    
  101.                                     continue;                                  
  102.                                 }
  103.                                     if(!marked.contains(rA) && !marked.contains(rB)){                                      
  104.                                         choices.put(rB,i); 
  105.                                         parent.put(rB, rA);                                
  106.                                     }
  107.                                     if(!marked.contains(rA) && marked.contains(rB)){
  108.                                         marked.add(rA);
  109.                                     }                          
  110.                         }
  111.                     }
  112.                    
  113.                
  114.             }
  115.             if(choices.size() > 0){            
  116.                 int rnum = r.nextInt(choices.getValues().size());
  117.                 int rDir = (int)choices.getValues().toArray()[rnum];
  118.                
  119.                 int oppdir = 0;
  120.                 switch(rDir){
  121.                 case 0:
  122.                     oppdir = 1;
  123.                     break;
  124.                 case 1:
  125.                     oppdir = 0;
  126.                     break;
  127.                 case 2:
  128.                     oppdir = 3;
  129.                     break;
  130.                 case 3:
  131.                     oppdir = 2;
  132.                     break;                 
  133.                 }
  134.                
  135.                 SetPairMap<Integer, Rectangle> adj = getAdjacent(current,true);
  136.                
  137.                 Rectangle nextrect = choices.getAvailableKeyOfValue(rDir);
  138.                 Rectangle parentrect = parent.get(nextrect);
  139.                 if(adj.get(oppdir) != null){
  140.                 marked.addAll(adj.get(oppdir));
  141.                 }
  142.                 adj = getAdjacent(parentrect,true);
  143.                 //marked.addAll(adj.getValues());
  144.                 path.add(nextrect);
  145.                 path.add(parentrect);
  146.                 marked.add(nextrect);
  147.                 marked.add(parentrect);
  148.                
  149.                 adjacentnodes.put(nextrect,current);
  150.                 current = next.push(nextrect);             
  151.             }else
  152.                 if(!next.isEmpty()){
  153.                     current = next.pop();
  154.                 }
  155.             }
  156.        
  157.        
  158.     }
  159.    
  160.     public SetPairMap<Integer,Rectangle> getAdjacent(Rectangle box,boolean biased){
  161.         SetPairMap<Integer, Rectangle> boxes=  new SetPairMap<>();
  162.             if(box == null){
  163.                 return boxes;
  164.             }
  165.             for(Rectangle r: rooms){
  166.                 if(r.equals(box)){
  167.                     continue;
  168.                 }
  169.                 for(int x = -1; x < 2; x++){
  170.                     for(int y = -1; y < 2; y++){
  171.                        
  172.                         if(x == y || (x != 0 && y !=0)){
  173.                             continue;
  174.                         }
  175.                         Rectangle nB = new Rectangle(box.x+x,box.y+y,box.width,box.height);
  176.                         if(r.intersects(nB)){
  177.                             int dir = 0;
  178.                             if(y == -1){
  179.                                 dir = 0;
  180.                             }
  181.                             if(y == 1){
  182.                                 dir = 1;
  183.                             }
  184.                             if(x == -1){
  185.                                 dir = 2;
  186.                             }
  187.                             if(x == 1){
  188.                                 dir = 3;
  189.                             }
  190.                            
  191.                             //if(donotplace.contains(box) || donotplace.intersects(box)){                          
  192.                             //boxes.put(dir, r);
  193.                             //}else{
  194.                             //if(!(donotplace.intersects(r) || donotplace.contains(r))){
  195.                             //  boxes.put(dir, r);
  196.                             //}
  197.                             //}
  198.                            
  199.                             if((!marked.contains(r) && biased) || !biased){
  200.                                 boxes.put(dir, r);
  201.                             }
  202.                            
  203.                         }
  204.                     }
  205.                 }
  206.             }
  207.        
  208.         return boxes;
  209.     }
  210.    
  211.     public void removeEnds(){      
  212.         Set<Rectangle> nocheck = new HashSet<>();
  213.         for(int i = 0 ; i < 15; i++){
  214.             for(Rectangle r: adjacentnodes.getKeys()){
  215.                 if(nocheck.contains(r)){
  216.                     //continue;
  217.                 }                      
  218.                         Set<Rectangle> adj = getAdjacent(r,false).getValues();
  219.                         Set<Rectangle> inverse = new HashSet<>(adj);
  220.                         adj.removeAll(path);
  221.                         inverse.removeAll(adj);
  222.                         //System.out.println(adj);
  223.                         if(inverse.size() < 2){
  224.                             path.remove(r);
  225.                             path.removeAll(inverse);
  226.                             if(new Random().nextInt(100) < 2){
  227.                             //nocheck.add(r);
  228.                             }
  229.                         }                  
  230.             }
  231.         }
  232.     }
  233.    
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement