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

Added javadoc

parent d5a3df8f
......@@ -12,15 +12,12 @@ import core.gamestate.Entity;
/**
*
*@author mapgen team
* Represents a map of tiles
*
*/
public class Map implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7578251078403583045L;
private Tile[][] map;
private MapPoint positionPlayerAtStart;
......@@ -46,6 +43,7 @@ public class Map implements Serializable {
throw new ArrayIndexOutOfBoundsException("Trying to get tile at "+x+" , "+y+" but map has dimension "+height+" , "+width);
}
}
/**
*
* @return the height of the map in tiles
......@@ -81,17 +79,22 @@ public class Map implements Serializable {
}
return map[x][y].getType();
}
/**
*
* @param x
* @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){
checkBounds(x, y);
//return new Tile(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() {
// 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;
......
......@@ -24,7 +24,6 @@ public final class MapBuilder implements Serializable{
private ArrayList<RoomBuilder> rooms;
private ArrayList<Surface> corridors;
private ArrayList<Surface> doors;
private ArrayList<Entity> entities;
private RoomGraph roomTree;
private MapPoint positionPlayerAtStart;
......@@ -33,7 +32,6 @@ public final class MapBuilder implements Serializable{
public MapBuilder() {
corridors = new ArrayList<>();
doors = new ArrayList<>();
entities = new ArrayList<Entity>();
}
......
package map_generation.map;
import java.io.Serializable;
import java.util.Random;
public class MapGeneration implements Serializable {
/**
import map_generation.map.RoomBuilder.RoomType;
/**
*
* @author mapgen team
* Main class to generate a map
*/
public class MapGeneration implements Serializable {
private static final long serialVersionUID = -6471655849722890871L;
/**
* Build a correct map
* @param meanRoomSize
* @param numberOfRooms
* @return
*/
public static Map mapGeneration(int meanRoomSize, int numberOfRooms) {
boolean bb=true;
Map map = new Map();
//int nbCheck=0;
while(bb) {
//nbCheck++;
map = SurfacesMapGeneration.roomsRandomGeneration(meanRoomSize,numberOfRooms);
if (map != null && map.getPositionPlayerStart() != null) bb=false;
map = roomsRandomGeneration(meanRoomSize,numberOfRooms);
if (map != null && map.getPositionPlayerStart() != null) bb=false;//check correctness
}
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){
switch(size){
case SMALL :
......
package map_generation.map;
/**
*
* @author Clement Beauseigneur
*
*/
public class MapPattern {
private int id;
......
package map_generation.map;
import logging.Logging;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import logging.Logging;
import assets.UsedForLoadingSprites;
/**
*
* @author Clement Beauseigneur
*
*/
public class MapPatterns {
public static MapPattern[] patterns;
......
......@@ -4,6 +4,11 @@ import core.zone.Point;
import java.io.Serializable;
/**
*
* @author Clement Beauseigneur
*
*/
public class MapPoint implements Serializable {
/**
*
......
......@@ -7,10 +7,15 @@ import core.zone.Point;
import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType;
/**
*
* @author Lucas Delcros
* Represents room
*/
public final class RoomBuilder {
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;
public RoomType type;
......@@ -21,6 +26,10 @@ public final class RoomBuilder {
this.type = type;
}
/**
* Resets the static parameters of the class
* Must be use before generating a new Map
*/
public static void reset(){
NB_ENTITIES = 0;
}
......@@ -32,6 +41,10 @@ public final class RoomBuilder {
return surface;
}
/**
* Add a lava lake in the room
* @param map
*/
private void addFullLava(TileBuilder[][] map){
Random r = new Random();
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 {
s.fillSurfaceWith(map, TileType.WATER);
}
/**
* Add stairs in the room
* @param map
* @param mapB
*/
private void addStairs(TileBuilder[][] map, MapBuilder mapB){
int posY=(surface.j1+surface.j2)/2;
int posX=(surface.i1+surface.i2)/2;
......@@ -46,12 +64,21 @@ public final class RoomBuilder {
mapB.setStairPosition(new MapPoint(posY, posX));
}
/**
* Add four torches in the room
* @param map
*/
private void addTorches(TileBuilder[][] map){
map[surface.j1 + 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.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){
Random r = new Random();
if(type == RoomType.NORMAL && r.nextDouble() <= p){
......@@ -60,6 +87,13 @@ public final class RoomBuilder {
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) {
Random r = new Random();
int nbRooms = mapB.getNbRooms(), ratio = Integer.max((mapB.getMaxEntities()/nbRooms), 1);
......@@ -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){
surface.fillSurfaceWith(map, TileType.GROUND);
switch (type){
......
......@@ -5,21 +5,24 @@ import java.io.Serializable;
import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType;
public class Surface implements Serializable{
/**
/**
*
* @author Lucas Delcros
* Represents a surface
*/
public class Surface implements Serializable{
private static final long serialVersionUID = -1607241988633571053L;
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) {
// 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;
......@@ -29,15 +32,25 @@ public class Surface implements Serializable{
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);
}
/**
*
* @param mp
* @return true if the given point is on the surface
*/
public boolean isOnSurface(MapPoint mp){
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 xa
* @param ya
......@@ -75,10 +88,13 @@ public class Surface implements Serializable{
}
}
}
/**
* Surround a surface with walls
* Surround a surface with the given type
* @param map
* @param surface
* @param type
*/
public void surroundWith(TileBuilder[][] map, TileType type){
final int x1 = i1 - 1, x2 = i2 +1;
......@@ -92,9 +108,10 @@ public class Surface implements Serializable{
}
/**
* Fills a surface
* Fills a surface with the given type
* @param map
* @param surface
* @param type
*/
public void fillSurfaceWith(TileBuilder[][] map, TileType tt){
for (int i = j1; i <= j2; i++) {
......
......@@ -7,16 +7,20 @@ import java.util.Random;
import map_generation.map.RoomBuilder.RoomType;
import map_generation.tiles.TileBuilder;
/**
*
* This class contains methods that generates simple map to help for tests
* @author mapgen team
* Contains static functions that create a map of abstract surfaces
*
*/
public class SurfacesMapGeneration implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5179076009464463054L;
/**
* Create a new map of tileBuilders
* @param h
* @param w
* @return
*/
public static TileBuilder[][] emptyTiles(int h, int w){
TileBuilder[][] tilebs = new TileBuilder[h][w];
for(int i=0;i<h;i++) {
......@@ -27,13 +31,24 @@ public class SurfacesMapGeneration implements Serializable{
return tilebs;
}
private static RoomBuilder defineStartPosition(RoomBuilder[] rooms) {
/**
* Define a starting room randomly
* @param rooms
* @return
*/
public static RoomBuilder defineStartPosition(RoomBuilder[] rooms) {
Random r = new Random();
int idRoom=r.nextInt(rooms.length);
rooms[idRoom].setType(RoomType.START);
return rooms[idRoom];
}
/**
* Gives the distance between surface1 and surface2
* @param surface1
* @param surface2
* @return
*/
public static int distanceBetweenTwoSurface(Surface surface1,Surface surface2) {
// Method that computes the distance between two rooms
int dY=Math.max(surface1.j1-surface2.j2+2,0)+Math.max(surface2.j1-surface1.j2+2,0);
......@@ -41,6 +56,14 @@ public class SurfacesMapGeneration implements Serializable{
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)
}
/**
* Create corridor(s) between two rooms
* If the two rooms are facing each other there will be only one corridor
* Other wise there will be two corridor since the rooms wills be linked by a "corner corridor"
* @param room1
* @param room2
* @return
*/
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,0);
......@@ -88,7 +111,14 @@ public class SurfacesMapGeneration implements Serializable{
return(corridors);
}
}
private static void addCorridorsByMST(RoomBuilder[] rooms, MapBuilder map) {
/**
* Add corridors using MST on the full graph of rooms
* If two rooms are close enough there will also be linked
* @param rooms
* @param map
*/
public static void addCorridorsByMST(RoomBuilder[] rooms, MapBuilder map) {
// Considering the graph made by the rooms with edges corresponding to the distance between any two of them
// I find the MST (Math.minimum SpamMath.ming Tree) of this graph in order to create a first set of corridors that connects all the rooms
int nbRooms=rooms.length;
......@@ -135,69 +165,4 @@ public class SurfacesMapGeneration implements Serializable{
}
}
}
public 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=defineStartPosition(rooms);
map.addRooms(rooms, startRoom);
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();
}
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=defineStartPosition(rooms);
map.addRooms(rooms, startRoom);
addCorridorsByMST(rooms, map);
return(map.build());
}
}
Markdown is supported
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