Commit eb63b414 authored by Lucas Delcros's avatar Lucas Delcros
Browse files

Added javadoc

parent d5a3df8f
...@@ -12,15 +12,12 @@ import core.gamestate.Entity; ...@@ -12,15 +12,12 @@ import core.gamestate.Entity;
/** /**
* *@author mapgen team
* Represents a map of tiles * Represents a map of tiles
* *
*/ */
public class Map implements Serializable { public class Map implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7578251078403583045L; private static final long serialVersionUID = -7578251078403583045L;
private Tile[][] map; private Tile[][] map;
private MapPoint positionPlayerAtStart; private MapPoint positionPlayerAtStart;
...@@ -45,7 +42,8 @@ public class Map implements Serializable { ...@@ -45,7 +42,8 @@ public class Map implements Serializable {
if(x < 0 || x > height || y < 0 || y > width) { if(x < 0 || x > height || y < 0 || y > width) {
throw new ArrayIndexOutOfBoundsException("Trying to get tile at "+x+" , "+y+" but map has dimension "+height+" , "+width); throw new ArrayIndexOutOfBoundsException("Trying to get tile at "+x+" , "+y+" but map has dimension "+height+" , "+width);
} }
} }
/** /**
* *
* @return the height of the map in tiles * @return the height of the map in tiles
...@@ -81,17 +79,22 @@ public class Map implements Serializable { ...@@ -81,17 +79,22 @@ public class Map implements Serializable {
} }
return map[x][y].getType(); return map[x][y].getType();
} }
/** /**
* *
* @param x * @param x
* @param y * @param y
* @return clone of tile at position x, y in map[][] * @return tile at position x, y in map[][]
*/ */
public Tile getTileAt(int x, int y){ public Tile getTileAt(int x, int y){
checkBounds(x, y); checkBounds(x, y);
//return new Tile(map[x][y]);
return map[x][y]; return map[x][y];
} }
/**
* This functions checks if every tile is reachable in the map by BFS
* @return true if every tile in the map is reachable
*/
public boolean check() { 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. // 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; int nb=0;
......
...@@ -24,7 +24,6 @@ public final class MapBuilder implements Serializable{ ...@@ -24,7 +24,6 @@ public final class MapBuilder implements Serializable{
private ArrayList<RoomBuilder> rooms; private ArrayList<RoomBuilder> rooms;
private ArrayList<Surface> corridors; private ArrayList<Surface> corridors;
private ArrayList<Surface> doors;
private ArrayList<Entity> entities; private ArrayList<Entity> entities;
private RoomGraph roomTree; private RoomGraph roomTree;
private MapPoint positionPlayerAtStart; private MapPoint positionPlayerAtStart;
...@@ -33,7 +32,6 @@ public final class MapBuilder implements Serializable{ ...@@ -33,7 +32,6 @@ public final class MapBuilder implements Serializable{
public MapBuilder() { public MapBuilder() {
corridors = new ArrayList<>(); corridors = new ArrayList<>();
doors = new ArrayList<>();
entities = new ArrayList<Entity>(); entities = new ArrayList<Entity>();
} }
......
package map_generation.map; package map_generation.map;
import java.io.Serializable; import java.io.Serializable;
import java.util.Random;
import map_generation.map.RoomBuilder.RoomType;
/**
*
* @author mapgen team
* Main class to generate a map
*/
public class MapGeneration implements Serializable { public class MapGeneration implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6471655849722890871L; private static final long serialVersionUID = -6471655849722890871L;
/**
* Build a correct map
* @param meanRoomSize
* @param numberOfRooms
* @return
*/
public static Map mapGeneration(int meanRoomSize, int numberOfRooms) { public static Map mapGeneration(int meanRoomSize, int numberOfRooms) {
boolean bb=true; boolean bb=true;
Map map = new Map(); Map map = new Map();
//int nbCheck=0;
while(bb) { while(bb) {
//nbCheck++; map = roomsRandomGeneration(meanRoomSize,numberOfRooms);
map = SurfacesMapGeneration.roomsRandomGeneration(meanRoomSize,numberOfRooms); if (map != null && map.getPositionPlayerStart() != null) bb=false;//check correctness
if (map != null && map.getPositionPlayerStart() != null) bb=false;
} }
return(map); return(map);
} }
/**
* Main function to create a map
* @param meanRoomSize
* @param numberOfRooms
* @return
*/
private static Map roomsRandomGeneration(int meanRoomSize, int numberOfRooms) {
// This is for the moment the InputConfigurationFrame method to create a map
// It first creates the map attributes and then calls the MapBuilder to build the map with those
int heightMap=(meanRoomSize+6)*(int)Math.sqrt(numberOfRooms);
int widthMap=(meanRoomSize+6)*(int)Math.sqrt(numberOfRooms);
MapBuilder map = new MapBuilder();
Random r = new Random();
RoomBuilder[] rooms = new RoomBuilder[numberOfRooms];
int numberOfRoomsCreated=0;
int nbIteration=0;
// Creation of the rooms
while (numberOfRoomsCreated<numberOfRooms) {
nbIteration++;
if (nbIteration>500*numberOfRooms) { // The size of the map is initially 10*10 and while in 2000 iterations the rooms aren't created the size increases by a factor of 1.1
heightMap=(int) ((heightMap)*1.1);
widthMap=(int) ((widthMap)*1.1);
numberOfRoomsCreated=0;
nbIteration=0;
}
int height=(int)(meanRoomSize*(1+r.nextGaussian()));
int width=(int)(meanRoomSize*(1+r.nextGaussian()));
int positionY=r.nextInt(heightMap);
int positionX=r.nextInt(widthMap);
// The positions and sizes of the rooms are choosen randomly
if (Math.abs(height-width)<meanRoomSize && height>=8 && width>=8 && positionY>0 && positionX>0 && positionY+height<heightMap && positionX+width<widthMap) {
boolean bb=true;
for (int i=0;i<numberOfRoomsCreated;i++) { // Test of intersection with a room already created
// There's a Math.minimum distance between two rooms of 4 tiles
if(positionY<=rooms[i].getSurface().j2+6 && positionY+height-1>=rooms[i].getSurface().j1-6 && positionX<=rooms[i].getSurface().i2+6 && positionX+width-1>=rooms[i].getSurface().i1-6) bb=false;
}
if (bb) { // If the current room fits with the others we had it
Surface room = new Surface(positionX, positionY, positionX+width-1, positionY+height-1);
rooms[numberOfRoomsCreated] = new RoomBuilder(room, RoomType.NORMAL);
numberOfRoomsCreated++;
}
}
}
RoomBuilder startRoom=SurfacesMapGeneration.defineStartPosition(rooms);
map.addRooms(rooms, startRoom);
SurfacesMapGeneration.addCorridorsByMST(rooms, map); // Creation of the set of corridors
// defineStairsPosition(map,heightMap,widthMap,rooms,startRoom);
map.setHeight(heightMap);
map.setWidth(widthMap);
for (RoomBuilder room : rooms) {
room.setRandomlySpecial(0.3);
}
return map.build();
}
/**
*
* @param size
* @return A map containing a single room
*/
public static Map simpleRoom(int size) {
// This is a very simple model of a map that contains only one squared room of side size
MapBuilder map = new MapBuilder();
map.setHeight(size);
map.setWidth(size);
Surface room=new Surface(2,2,size-3,size-3);
RoomBuilder[] rooms= {new RoomBuilder(room, RoomType.NORMAL)};
RoomBuilder startRoom=SurfacesMapGeneration.defineStartPosition(rooms);
map.addRooms(rooms, startRoom);
SurfacesMapGeneration.addCorridorsByMST(rooms, map);
return(map.build());
}
/**
* Build a map of size size
* @param size
* @return
*/
public static Map mapGeneration(MapSize size){ public static Map mapGeneration(MapSize size){
switch(size){ switch(size){
case SMALL : case SMALL :
......
package map_generation.map; package map_generation.map;
/**
*
* @author Clement Beauseigneur
*
*/
public class MapPattern { public class MapPattern {
private int id; private int id;
......
package map_generation.map; package map_generation.map;
import logging.Logging;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import logging.Logging;
import assets.UsedForLoadingSprites; import assets.UsedForLoadingSprites;
/**
*
* @author Clement Beauseigneur
*
*/
public class MapPatterns { public class MapPatterns {
public static MapPattern[] patterns; public static MapPattern[] patterns;
......
...@@ -4,6 +4,11 @@ import core.zone.Point; ...@@ -4,6 +4,11 @@ import core.zone.Point;
import java.io.Serializable; import java.io.Serializable;
/**
*
* @author Clement Beauseigneur
*
*/
public class MapPoint implements Serializable { public class MapPoint implements Serializable {
/** /**
* *
......
...@@ -7,10 +7,15 @@ import core.zone.Point; ...@@ -7,10 +7,15 @@ import core.zone.Point;
import map_generation.tiles.TileBuilder; import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType; import map_generation.tiles.TileType;
/**
*
* @author Lucas Delcros
* Represents room
*/
public final class RoomBuilder { public final class RoomBuilder {
public static enum RoomType{START, END, NORMAL, TORCHES, FULL_LAVA, CHESTS}; public static enum RoomType{START, END, NORMAL, TORCHES, FULL_LAVA, CHESTS};
private static int NB_ENTITIES = 0; private static int NB_ENTITIES = 0;//Total number of entities in the game
private Surface surface; private Surface surface;
public RoomType type; public RoomType type;
...@@ -21,6 +26,10 @@ public final class RoomBuilder { ...@@ -21,6 +26,10 @@ public final class RoomBuilder {
this.type = type; this.type = type;
} }
/**
* Resets the static parameters of the class
* Must be use before generating a new Map
*/
public static void reset(){ public static void reset(){
NB_ENTITIES = 0; NB_ENTITIES = 0;
} }
...@@ -32,6 +41,10 @@ public final class RoomBuilder { ...@@ -32,6 +41,10 @@ public final class RoomBuilder {
return surface; return surface;
} }
/**
* Add a lava lake in the room
* @param map
*/
private void addFullLava(TileBuilder[][] map){ private void addFullLava(TileBuilder[][] map){
Random r = new Random(); Random r = new Random();
int dw = r.nextInt((surface.width-2)/2)+1, dh = r.nextInt((surface.height-2)/2)+1; int dw = r.nextInt((surface.width-2)/2)+1, dh = r.nextInt((surface.height-2)/2)+1;
...@@ -39,6 +52,11 @@ public final class RoomBuilder { ...@@ -39,6 +52,11 @@ public final class RoomBuilder {
s.fillSurfaceWith(map, TileType.WATER); s.fillSurfaceWith(map, TileType.WATER);
} }
/**
* Add stairs in the room
* @param map
* @param mapB
*/
private void addStairs(TileBuilder[][] map, MapBuilder mapB){ private void addStairs(TileBuilder[][] map, MapBuilder mapB){
int posY=(surface.j1+surface.j2)/2; int posY=(surface.j1+surface.j2)/2;
int posX=(surface.i1+surface.i2)/2; int posX=(surface.i1+surface.i2)/2;
...@@ -46,12 +64,21 @@ public final class RoomBuilder { ...@@ -46,12 +64,21 @@ public final class RoomBuilder {
mapB.setStairPosition(new MapPoint(posY, posX)); mapB.setStairPosition(new MapPoint(posY, posX));
} }
/**
* Add four torches in the room
* @param map
*/
private void addTorches(TileBuilder[][] map){ private void addTorches(TileBuilder[][] map){
map[surface.j1 + 2][surface.i1 + 2].setType(TileType.TORCH); map[surface.j1 + 2][surface.i1 + 2].setType(TileType.TORCH);
map[surface.j2 - 2][surface.i1 + 2].setType(TileType.TORCH); map[surface.j2 - 2][surface.i1 + 2].setType(TileType.TORCH);
map[surface.j1 + 2][surface.i2 - 2].setType(TileType.TORCH); map[surface.j1 + 2][surface.i2 - 2].setType(TileType.TORCH);
map[surface.j2 - 2][surface.i2 - 2].setType(TileType.TORCH); map[surface.j2 - 2][surface.i2 - 2].setType(TileType.TORCH);
} }
/**
* Change a NORMAL room to a special room (LAVA, TORCHES) with probability p
* @param p
*/
public void setRandomlySpecial(double p){ public void setRandomlySpecial(double p){
Random r = new Random(); Random r = new Random();
if(type == RoomType.NORMAL && r.nextDouble() <= p){ if(type == RoomType.NORMAL && r.nextDouble() <= p){
...@@ -60,6 +87,13 @@ public final class RoomBuilder { ...@@ -60,6 +87,13 @@ public final class RoomBuilder {
else setType(RoomType.TORCHES); else setType(RoomType.TORCHES);
} }
} }
/**
* Generates entities
* @param tb
* @param mapB
* @param specieName the name of the entity
*/
private void generateEntities(TileBuilder[][] tb, MapBuilder mapB, String specieName) { private void generateEntities(TileBuilder[][] tb, MapBuilder mapB, String specieName) {
Random r = new Random(); Random r = new Random();
int nbRooms = mapB.getNbRooms(), ratio = Integer.max((mapB.getMaxEntities()/nbRooms), 1); int nbRooms = mapB.getNbRooms(), ratio = Integer.max((mapB.getMaxEntities()/nbRooms), 1);
...@@ -76,6 +110,13 @@ public final class RoomBuilder { ...@@ -76,6 +110,13 @@ public final class RoomBuilder {
} }
} }
} }
/**
* Builds the room according to its type and generates monsters according to the given patern
* @param map
* @param mapB
* @param iPattern the id of the monster spawning patern
*/
public void build(TileBuilder[][] map, MapBuilder mapB, int iPattern){ public void build(TileBuilder[][] map, MapBuilder mapB, int iPattern){
surface.fillSurfaceWith(map, TileType.GROUND); surface.fillSurfaceWith(map, TileType.GROUND);
switch (type){ switch (type){
......
...@@ -5,21 +5,24 @@ import java.io.Serializable; ...@@ -5,21 +5,24 @@ import java.io.Serializable;
import map_generation.tiles.TileBuilder; import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType; import map_generation.tiles.TileType;
/**
*
* @author Lucas Delcros
* Represents a surface
*/
public class Surface implements Serializable{ public class Surface implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1607241988633571053L; private static final long serialVersionUID = -1607241988633571053L;
public final int i1, i2, j1, j2, height, width; public final int i1, i2, j1, j2, height, width;
/**
*
* @param i1 the i position of the upper left corner
* @param j1 the j position of the upper left corner
* @param i2 the i position of the down right corner
* @param j2 the j position of the down right corner
*/
public Surface(int i1, int j1, int i2, int j2) { 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.i1 = i1;
this.i2 = i2; this.i2 = i2;
this.j1 = j1; this.j1 = j1;
...@@ -29,15 +32,25 @@ public class Surface implements Serializable{ ...@@ -29,15 +32,25 @@ public class Surface implements Serializable{
checkSurface(); checkSurface();
} }
public void checkSurface(){ /**
* Checks if a surface is coherent
*/
private void checkSurface(){
if (!(height>0 && width>0)) throw new IllegalArgumentException("wrong surface i1 "+i1+" j1 "+j1+" i2 "+i2+" j2 "+j2); if (!(height>0 && width>0)) throw new IllegalArgumentException("wrong surface i1 "+i1+" j1 "+j1+" i2 "+i2+" j2 "+j2);
} }
/**
*
* @param mp
* @return true if the given point is on the surface
*/
public boolean isOnSurface(MapPoint mp){ public boolean isOnSurface(MapPoint mp){
return mp.getI() >= i1 && mp.getI() <= i2 && mp.getJ() >= j1 && mp.getJ() <= j2; return mp.getI() >= i1 && mp.getI() <= i2 && mp.getJ() >= j1 && mp.getJ() <= j2;
} }
/** /**
* Implements Bresenham's algorithm to make a line of walls beetween two nodes; * Implements Bresenham's algorithm to make a line of walls between a and b;
* @param tilebs * @param tilebs
* @param xa * @param xa
* @param ya * @param ya
...@@ -75,10 +88,13 @@ public class Surface implements Serializable{ ...@@ -75,10 +88,13 @@ public class Surface implements Serializable{
} }
} }
} }
/** /**
* Surround a surface with walls * Surround a surface with the given type
* @param map * @param map
* @param surface * @param surface
* @param type
*/ */
public void surroundWith(TileBuilder[][] map, TileType type){ public void surroundWith(TileBuilder[][] map, TileType type){
final int x1 = i1 - 1, x2 = i2 +1; final int x1 = i1 - 1, x2 = i2 +1;
...@@ -92,9 +108,10 @@ public class Surface implements Serializable{ ...@@ -92,9 +108,10 @@ public class Surface implements Serializable{
} }
/** /**
* Fills a surface * Fills a surface with the given type
* @param map * @param map
* @param surface * @param surface
* @param type
*/ */
public void fillSurfaceWith(TileBuilder[][] map, TileType tt){ public void fillSurfaceWith(TileBuilder[][] map, TileType tt){
for (int i = j1; i <= j2; i++) { for (int i = j1; i <= j2; i++) {
......
...@@ -7,16 +7,20 @@ import java.util.Random; ...@@ -7,16 +7,20 @@ import java.util.Random;
import map_generation.map.RoomBuilder.RoomType; import map_generation.map.RoomBuilder.RoomType;
import map_generation.tiles.TileBuilder; import map_generation.tiles.TileBuilder;
/** /**
* * @author mapgen team
* This class contains methods that generates simple map to help for tests * Contains static functions that create a map of abstract surfaces
* *
*/ */
public class SurfacesMapGeneration implements Serializable{ public class SurfacesMapGeneration implements Serializable{
private static final long serialVersionUID = 5179076009464463054L;
/** /**
* * Create a new map of tileBuilders
* @param h
* @param w
* @return
*/ */
private static final long serialVersionUID = 5179076009464463054L;
public static TileBuilder[][] emptyTiles(int h, int w){ public static TileBuilder[][] emptyTiles(int h, int w){
TileBuilder[][] tilebs = new TileBuilder[h][w]; TileBuilder[][] tilebs = new TileBuilder[h][w];
for(int i=0;i<h;i++) { for(int i=0;i<h;i++) {
...@@ -26,21 +30,40 @@ public class SurfacesMapGeneration implements Serializable{ ...@@ -26,21 +30,40 @@ public class SurfacesMapGeneration implements Serializable{
} }
return tilebs; return tilebs;
}