Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Core;
- import java.awt.Rectangle;
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Set;
- import java.util.Stack;
- import Base.custom.PairMap;
- import Base.custom.SetPairMap;
- public class World {
- public Set<Rectangle> rooms = new HashSet<>();
- public RectSplitter rs;
- public Rectangle[][] roomlanding;
- public int width,height;
- public World(int width, int height){
- this.width = width;
- this.height = height;
- rs = new RectSplitter(width,height);
- bounds = rs.start.rect;
- roomlanding = new Rectangle[height][width];
- rooms = rs.Split(4, 4);
- for(Rectangle r: rooms){
- for(int x = 0; x < r.width; x++){
- for(int y = 0; y < r.height; y++){
- roomlanding[y+r.y][x+r.x] = r;
- }
- }
- }
- searchRooms();
- }
- Rectangle bounds;
- public Set<Rectangle> marked = new HashSet<>();
- public Set<Rectangle> path = new HashSet<Rectangle>();
- Random r = new Random();
- Rectangle current;
- Stack<Rectangle> next = new Stack<>();
- boolean firstrun = true;
- Rectangle donotplace;
- SetPairMap<Rectangle,Rectangle> adjacentnodes = new SetPairMap<>();
- PairMap<Rectangle,Rectangle> adjacentnodesB = new PairMap<>();
- public void pathRooms(){
- while(true){
- searchRooms();
- if(next.isEmpty()){
- break;
- }
- }
- removeEnds();
- }
- public void searchRooms(){
- if(firstrun){
- donotplace = new Rectangle(width/4,height/4,width/2,height/2);
- current = roomlanding[(int)height/2][(int)width/2];
- next.push(current);
- firstrun = false;
- for(int x = 0; x < donotplace.width; x++){
- for(int y = 0; y < donotplace.height; y++){
- roomlanding[y+donotplace.x][x+donotplace.y] = null;
- }
- }
- }
- if(current != null){
- marked.add(current);
- path.add(current);
- SetPairMap<Integer,Rectangle> adjacent = getAdjacent(current,true);
- PairMap<Rectangle,Integer> choices = new PairMap<>();
- PairMap<Rectangle,Rectangle> parent = new PairMap<>();
- Set<Rectangle> adjB;
- for(int i : adjacent.getKeys()){
- for(Rectangle rA : adjacent.get(i)){
- adjB = getAdjacent(rA,true).get(i);
- if(adjB == null){
- marked.add(rA);
- continue;
- }
- for(Rectangle rB : getAdjacent(rA,true).get(i)){
- if(current.equals(rB)){
- continue;
- }
- if(r.nextBoolean() && (rA.x % 5 == 0 || rA.y % 5 == 0)){
- continue;
- }
- if(!marked.contains(rA) && !marked.contains(rB)){
- choices.put(rB,i);
- parent.put(rB, rA);
- }
- if(!marked.contains(rA) && marked.contains(rB)){
- marked.add(rA);
- }
- }
- }
- }
- if(choices.size() > 0){
- int rnum = r.nextInt(choices.getValues().size());
- int rDir = (int)choices.getValues().toArray()[rnum];
- int oppdir = 0;
- switch(rDir){
- case 0:
- oppdir = 1;
- break;
- case 1:
- oppdir = 0;
- break;
- case 2:
- oppdir = 3;
- break;
- case 3:
- oppdir = 2;
- break;
- }
- SetPairMap<Integer, Rectangle> adj = getAdjacent(current,true);
- Rectangle nextrect = choices.getAvailableKeyOfValue(rDir);
- Rectangle parentrect = parent.get(nextrect);
- if(adj.get(oppdir) != null){
- marked.addAll(adj.get(oppdir));
- }
- adj = getAdjacent(parentrect,true);
- //marked.addAll(adj.getValues());
- path.add(nextrect);
- path.add(parentrect);
- marked.add(nextrect);
- marked.add(parentrect);
- adjacentnodes.put(nextrect,current);
- current = next.push(nextrect);
- }else
- if(!next.isEmpty()){
- current = next.pop();
- }
- }
- }
- public SetPairMap<Integer,Rectangle> getAdjacent(Rectangle box,boolean biased){
- SetPairMap<Integer, Rectangle> boxes= new SetPairMap<>();
- if(box == null){
- return boxes;
- }
- for(Rectangle r: rooms){
- if(r.equals(box)){
- continue;
- }
- for(int x = -1; x < 2; x++){
- for(int y = -1; y < 2; y++){
- if(x == y || (x != 0 && y !=0)){
- continue;
- }
- Rectangle nB = new Rectangle(box.x+x,box.y+y,box.width,box.height);
- if(r.intersects(nB)){
- int dir = 0;
- if(y == -1){
- dir = 0;
- }
- if(y == 1){
- dir = 1;
- }
- if(x == -1){
- dir = 2;
- }
- if(x == 1){
- dir = 3;
- }
- //if(donotplace.contains(box) || donotplace.intersects(box)){
- //boxes.put(dir, r);
- //}else{
- //if(!(donotplace.intersects(r) || donotplace.contains(r))){
- // boxes.put(dir, r);
- //}
- //}
- if((!marked.contains(r) && biased) || !biased){
- boxes.put(dir, r);
- }
- }
- }
- }
- }
- return boxes;
- }
- public void removeEnds(){
- Set<Rectangle> nocheck = new HashSet<>();
- for(int i = 0 ; i < 15; i++){
- for(Rectangle r: adjacentnodes.getKeys()){
- if(nocheck.contains(r)){
- //continue;
- }
- Set<Rectangle> adj = getAdjacent(r,false).getValues();
- Set<Rectangle> inverse = new HashSet<>(adj);
- adj.removeAll(path);
- inverse.removeAll(adj);
- //System.out.println(adj);
- if(inverse.size() < 2){
- path.remove(r);
- path.removeAll(inverse);
- if(new Random().nextInt(100) < 2){
- //nocheck.add(r);
- }
- }
- }
- }
- }
- public SetPairMap<Rectangle,Rectangle> doormap = new SetPairMap<>();
- public void placeDoors(){
- for(Rectangle r: path){
- for(Rectangle adj: getAdjacent(r, false).getValues()){
- if(path.contains(adj) && (!doormap.containsKey(adj) || doormap.get(adj).size() < 1)){
- doormap.put(r, adj);
- doormap.put(adj, r);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement