Commit 8ca7dd73 authored by Lucas Delcros's avatar Lucas Delcros
Browse files

solved multiple bug

parent ececf834
package map_generation.map;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import map_generation.tiles.Tile;
......@@ -24,17 +26,19 @@ public class Map implements Serializable {
private MapPoint positionPlayerAtStart;
private MapPoint stairsPosition;
private Entity[] entities;
private ArrayList<Surface> corridors;
private int height = 0, width = 0;
public Map() {}
public Map(Tile[][] m,Entity[] entities , MapPoint positionPlayerAtStart,MapPoint stairsPosition) {
public Map(Tile[][] m, ArrayList<Surface> corridors, Entity[] entities , MapPoint positionPlayerAtStart,MapPoint stairsPosition) {
map = m;
this.entities = entities;
this.positionPlayerAtStart=positionPlayerAtStart;
this.stairsPosition=stairsPosition;
height = m.length;
width = m[0].length;
this.corridors = corridors;
}
private void checkBounds(int x, int y){
......@@ -88,7 +92,53 @@ public class Map implements Serializable {
//return new Tile(map[x][y]);
return map[x][y];
}
public boolean check() {
// Application of a BFS to find the number of different surfaces in the map. If the map is correct it should be 1.
int nb=0;
boolean bb=true;
boolean[][] mask=new boolean[height][width];
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
if(map[i][j].getType()==TileType.GROUND || map[i][j].getType()==TileType.OBSIDIAN || map[i][j].getType()==TileType.WATER || map[i][j].getType()==TileType.STAIRS) mask[i][j]=true;
else mask[i][j]=false;
}
}
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
if(mask[i][j]) {
nb++;
List<MapPoint> l=new ArrayList<>();
MapPoint element=new MapPoint(i,j);
l.add(element);
mask[i][j]=false;
while(l.size()>0) {
List<MapPoint> l1=new ArrayList<>();
for(int k=0;k<l.size();k++) {
int ii=l.get(k).getI();
int jj=l.get(k).getJ();
if(ii>0 && mask[ii-1][jj]) {l1.add(new MapPoint(ii-1,jj));mask[ii-1][jj]=false;}
if(ii<height-1 && mask[ii+1][jj]) {l1.add(new MapPoint(ii+1,jj));mask[ii+1][jj]=false;}
if(jj>0 && mask[ii][jj-1]) {l1.add(new MapPoint(ii,jj-1));mask[ii][jj-1]=false;}
if(jj<width-1 && mask[ii][jj+1]) {l1.add(new MapPoint(ii,jj+1));mask[ii][jj+1]=false;}
}
l.clear();
for(int k=0;k<l1.size();k++) l.add(l1.get(k));
}
}
}
}
if(nb!=1) bb=false;
for(int i=0;i<corridors.size();i++) {
for(int j=0;j<corridors.size();j++) {
if(i!=j) {
if(SurfacesMapGeneration.distanceBetweenTwoSurface(corridors.get(i),corridors.get(j))<1) {
bb=false;
}
}
}
}
return(bb);
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
......
......@@ -55,8 +55,8 @@ public final class MapBuilder implements Serializable{
* @param room2
*/
public void addCorridor(Surface corridor, RoomBuilder room1, RoomBuilder room2){
if(!roomTree.areLinked(room1, room2)){
corridors.add(corridor);
if(!roomTree.areLinked(room1, room2)){
roomTree.linkRooms(room1, room2);
}
}
......@@ -141,6 +141,6 @@ public final class MapBuilder implements Serializable{
rooms[i] = this.rooms.get(i).getSurface();
}
Entity[] entities = this.entities.toArray(new Entity[this.entities.size()]);
return new Map(TileBuilder.buildTiles(map), entities, positionPlayerAtStart, stairsPosition);
return new Map(TileBuilder.buildTiles(map), corridors,entities, positionPlayerAtStart, stairsPosition);
}
}
......@@ -17,8 +17,6 @@ public class MapGeneration implements Serializable {
map = SurfacesMapGeneration.roomsRandomGeneration(meanRoomSize,numberOfRooms);
if (map != null && map.getPositionPlayerStart() != null) bb=false;
}
//System.out.println("NbCheck : "+nbCheck);
//map.addSomeCorridors();
return(map);
}
......
......@@ -13,16 +13,24 @@ public class Surface implements Serializable{
public final int i1, i2, j1, j2, height, width;
public Surface(int i1, int j1, int i2, int j2) {
// if(i1>i2 || j1>j2){
// int ti = i1, tj = j1;
// i1 = i2;
// j1 = j2;
// j2 = tj;
// i2 = ti;
// }
this.i1 = i1;
this.i2 = i2;
this.j1 = j1;
this.j2 = j2;
width = j2-j1+1;
height = i2-i1+1;
checkSurface();
}
public boolean checkSurface(){
return (height>0 && width>0);
public void checkSurface(){
if (!(height>0 && width>0)) throw new IllegalArgumentException("wrong surface i1 "+i1+" j1 "+j1+" i2 "+i2+" j2 "+j2);
}
public boolean isOnSurface(MapPoint mp){
......
......@@ -21,7 +21,7 @@ public class SurfacesMapGeneration implements Serializable{
TileBuilder[][] tilebs = new TileBuilder[h][w];
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
tilebs[i][j]=new TileBuilder();
tilebs[i][j]=new TileBuilder();
}
}
return tilebs;
......@@ -40,9 +40,10 @@ public class SurfacesMapGeneration implements Serializable{
int dX=Math.max(surface1.i1-surface2.i2+2,0)+Math.max(surface2.i1-surface1.i2+2,0);
return (dY>0 && dX>0)?dX+dY+4:dX+dY; // There's an extra to the distance between rooms that aren't facing each other horizontally or vertically (to avoid corridors with corners in the MST when possible)
}
public static LinkedList<Surface> creationCorridor(Surface room1,Surface room2) {
// This method creates a corridor between two rooms
int dNorth=Math.max(room2.j1-room1.j2-1,0);
int dNorth=Math.max(room2.j1-room1.j2,0);
int dY=Math.max(room2.j1-room1.j2+1,0)+Math.max(room1.j1-room2.j2+1,0);
int dX=Math.max(room2.i1-room1.i2+1,0)+Math.max(room1.i1-room2.i2+1,0);
LinkedList<Surface> corridors=new LinkedList<>();
......@@ -67,34 +68,22 @@ public class SurfacesMapGeneration implements Serializable{
else {
int xTarget=-1;
int yTarget=-1;
if(dNorth>0) {
xTarget=(room1.i1+room1.i2)/2;
yTarget=(room2.j1+room2.j2)/2;
Surface corridor1 = new Surface(xTarget, room1.j2, xTarget+1, yTarget);
corridors.addLast(corridor1);
if(xTarget<room2.i1) {
Surface corridor2 = new Surface(xTarget, yTarget-1, room2.i1, yTarget);
corridors.addLast(corridor2);
}
else {
Surface corridor2 = new Surface(room2.i2, yTarget-1, xTarget, yTarget);
corridors.addLast(corridor2);
}
if(dNorth<=0){
Surface t = room1;
room1 = room2;
room2 = t;
}
xTarget=(room1.i1+room1.i2)/2;
yTarget=(room2.j1+room2.j2)/2;
Surface corridor1 = new Surface(xTarget, Math.min(room1.j2, yTarget), xTarget+1, Math.max(room1.j2, yTarget));
corridors.addLast(corridor1);
if(xTarget<room2.i1) {
Surface corridor2 = new Surface(xTarget, yTarget-1, room2.i1, yTarget);
corridors.addLast(corridor2);
}
else {
xTarget=(room2.i1+room2.i2)/2;
yTarget=(room1.j1+room1.j2)/2;
Surface corridor1 = new Surface(xTarget, room2.j2+1, xTarget+1, yTarget);
corridors.addLast(corridor1);
if(xTarget<room1.i1) {
Surface corridor2 = new Surface(xTarget, yTarget-1, room1.i1, yTarget);
corridors.addLast(corridor2);
}
else {
Surface corridor2 = new Surface(room1.i2, yTarget-1, xTarget, yTarget);
corridors.addLast(corridor2);
}
Surface corridor2 = new Surface(room2.i2, yTarget-1, xTarget, yTarget);
corridors.addLast(corridor2);
}
return(corridors);
}
......@@ -110,11 +99,11 @@ public class SurfacesMapGeneration implements Serializable{
distance[i][j]=d;
distance[j][i]=d;
if(d<12) { // If we find two rooms with a small distance in between, we add a corridor anyway, independantly of the MST
LinkedList<Surface> corridorsToAdd=creationCorridor(rooms[i].getSurface(),rooms[j].getSurface());
for (Surface corridor : corridorsToAdd) {
if(corridorsToAdd != null)map.addCorridor(corridor, rooms[i], rooms[j]);
LinkedList<Surface> corridorsToAdd=creationCorridor(rooms[i].getSurface(),rooms[j].getSurface());
for (Surface corridor : corridorsToAdd) {
if(corridorsToAdd != null)map.addCorridor(corridor, rooms[i], rooms[j]);
}
}
}
}
}
......@@ -139,11 +128,11 @@ public class SurfacesMapGeneration implements Serializable{
}
used[node2]=true;
nbConnected++;
LinkedList<Surface> corridorsToAdd=creationCorridor(rooms[node1].getSurface(),rooms[node2].getSurface());
for (Surface corridor : corridorsToAdd) {
if(corridorsToAdd != null)map.addCorridor(corridor, rooms[node1], rooms[node2]);
LinkedList<Surface> corridorsToAdd=creationCorridor(rooms[node1].getSurface(),rooms[node2].getSurface());
for (Surface corridor : corridorsToAdd) {
if(corridorsToAdd != null)map.addCorridor(corridor, rooms[node1], rooms[node2]);
}
}
}
}
public static Map roomsRandomGeneration(int meanRoomSize, int numberOfRooms) {
......@@ -187,7 +176,7 @@ public class SurfacesMapGeneration implements Serializable{
RoomBuilder startRoom=defineStartPosition(rooms);
map.addRooms(rooms, startRoom);
addCorridorsByMST(rooms, map); // Creation of the set of corridors
// defineStairsPosition(map,heightMap,widthMap,rooms,startRoom);
// defineStairsPosition(map,heightMap,widthMap,rooms,startRoom);
map.setHeight(heightMap);
map.setWidth(widthMap);
......
......@@ -13,7 +13,12 @@ import core.gamestate.SpeciesArray;
import core.zone.Point;
public class MapTests {
@Test
public void testHardEveryRoomAreReachable(){
Map map = MapGeneration.mapGeneration(20, 1000);
assertTrue(map.check());
}
@Test
public void testNoRonflexOnWater(){
Map map = MapGeneration.mapGeneration(20, 100);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment