Search in sources :

Example 6 with GenericPlace

use of com.lilithsthrone.world.places.GenericPlace in project liliths-throne-public by Innoxia.

the class LilayaHomeGeneric method getRoomModificationsDescription.

private static String getRoomModificationsDescription() {
    GenericPlace place = Main.game.getPlayer().getLocationPlace();
    roomSB.setLength(0);
    for (PlaceUpgrade pu : PlaceUpgrade.values()) {
        if (place.getPlaceUpgrades().contains(pu)) {
            roomSB.append(formatRoomUpgrade(pu));
        }
    }
    return roomSB.toString();
}
Also used : PlaceUpgrade(com.lilithsthrone.world.places.PlaceUpgrade) GenericPlace(com.lilithsthrone.world.places.GenericPlace)

Example 7 with GenericPlace

use of com.lilithsthrone.world.places.GenericPlace in project liliths-throne-public by Innoxia.

the class Generation method worldGeneration.

public void worldGeneration(WorldType worldType) {
    if (worldType.isUsesFile()) {
        try {
            BufferedImage img = ImageIO.read((getClass().getResource(worldType.getFileLocation())));
            World world = new World(img.getWidth(), img.getHeight(), null, worldType);
            Main.game.getWorlds().put(worldType, world);
            if (debug)
                System.out.println(worldType.getName() + " Start-File 1");
            Cell[][] grid = new Cell[img.getWidth()][img.getHeight()];
            for (int i = 0; i < img.getWidth(); i++) {
                for (int j = 0; j < img.getHeight(); j++) {
                    grid[i][j] = new Cell(worldType, new Vector2i(i, j));
                    if (worldType.isRevealedOnStart()) {
                        grid[i][j].setDiscovered(true);
                    }
                }
            }
            if (debug)
                System.out.println(worldType.getName() + " Start-File 2");
            for (int w = 0; w < img.getWidth(); w++) {
                for (int h = 0; h < img.getHeight(); h++) {
                    grid[w][img.getHeight() - 1 - h].setPlace(new GenericPlace(worldType.getPlacesMap().get(new Color(img.getRGB(w, h)))));
                    world.addPlaceOfInterest(grid[w][img.getHeight() - 1 - h].getPlace(), new Vector2i(w, img.getHeight() - 1 - h));
                }
            }
            if (debug)
                System.out.println(worldType.getName() + " Start-File 3");
            world.setGrid(grid);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        if (debug)
            System.out.println(worldType.getName() + " Start 1");
        int width = worldType.getWorldSize();
        int height = worldType.getWorldSize();
        if (debug)
            System.out.println(worldType.getName() + " Start 2  [width:" + width + "] [height:" + height + "]");
        // Create new world of this type:
        World w = new World(width * 2 - 1, height * 2 - 1, null, worldType);
        Main.game.getWorlds().put(w.getWorldType(), w);
        if (debug)
            System.out.println(worldType.getName() + " Start 3");
        // Initialise grid:
        Cell[][] grid = new Cell[width][height];
        if (debug)
            System.out.println(worldType.getName() + " Start 4");
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                grid[i][j] = new Cell(worldType, new Vector2i(i, j));
                if (worldType.isRevealedOnStart()) {
                    grid[i][j].setDiscovered(true);
                }
            }
        }
        if (debug)
            System.out.println(worldType.getName() + " Init finished");
        // Put blocks in alternating checkerboard of 4 squares:
        /*
			 * C = chance for 1 block in the 2x2 square . = never a block here
			 * C|C|.|.|C|C
			 * C|C|.|.|C|C
			 * .|.|C|C|.|.
			 * .|.|C|C|.|.
			 * C|C|.|.|C|C
			 * C|C|.|.|C|C
			 * 
			 * This will stop any impassable areas being created
			 */
        boolean[][] visited = new boolean[width][height];
        int x = 0, y = 0;
        List<Vector2i> dangerousPlaces = new ArrayList<>();
        for (int i = 0; i < width / 2; i++) {
            for (int j = 0; j < height / 2; j++) {
                if ((i + j) % 2 == 0) {
                    switch(rnd.nextInt(4)) {
                        case 0:
                            x = 0;
                            y = 0;
                            break;
                        case 1:
                            x = 0;
                            y = 1;
                            break;
                        case 2:
                            x = 1;
                            y = 0;
                            break;
                        case 3:
                            x = 0;
                            y = 1;
                            break;
                    }
                    visited[i * 2 + x][j * 2 + y] = true;
                    grid[i * 2 + x][j * 2 + y].setBlocked(true);
                    if (worldType.getCutOffZone() != null) {
                        grid[i * 2 + x][j * 2 + y].setPlace(new GenericPlace(worldType.getCutOffZone()));
                        dangerousPlaces.add(new Vector2i(i * 2 + x, j * 2 + y));
                    }
                }
            }
        }
        if (debug)
            System.out.println(worldType.getName() + " Break 1");
        int coreX = 0, coreY = 0;
        // Set aligned entrances:
        for (PlaceType pt : worldType.getPlaces()) {
            if (pt.getParentWorldType() != null) {
                if (pt.getParentAlignment() != null) {
                    if (debug)
                        System.out.println(pt.getName() + " Break 1a");
                    Vector2i location = null;
                    switch(pt.getParentAlignment()) {
                        case ALIGNED:
                            location = Main.game.getWorlds().get(pt.getParentWorldType()).getPlacesOfInterest().get(new GenericPlace(pt.getParentPlaceType()));
                            if (debug)
                                System.out.println(location);
                            grid[location.getX() / 2][location.getY() / 2].setPlace(new GenericPlace(pt));
                            grid[location.getX() / 2][location.getY() / 2].setBlocked(false);
                            visited[location.getX() / 2][location.getY() / 2] = false;
                            if (debug)
                                System.out.println(location);
                            break;
                        case ALIGNED_FLIP_HORIZONTAL:
                            location = Main.game.getWorlds().get(pt.getParentWorldType()).getPlacesOfInterest().get(new GenericPlace(pt.getParentPlaceType()));
                            grid[width - 1 - (location.getX()) / 2][location.getY() / 2].setPlace(new GenericPlace(pt));
                            grid[width - 1 - (location.getX()) / 2][location.getY() / 2].setBlocked(false);
                            visited[width - 1 - (location.getX()) / 2][location.getY() / 2] = false;
                            if (debug)
                                System.out.println(location);
                            break;
                        case ALIGNED_FLIP_VERTICAL:
                            location = Main.game.getWorlds().get(pt.getParentWorldType()).getPlacesOfInterest().get(new GenericPlace(pt.getParentPlaceType()));
                            grid[location.getX() / 2][height - 1 - (location.getY()) / 2].setPlace(new GenericPlace(pt));
                            grid[location.getX() / 2][height - 1 - (location.getY()) / 2].setBlocked(false);
                            visited[location.getX() / 2][height - 1 - (location.getY()) / 2] = false;
                            if (debug)
                                System.out.println(location);
                            break;
                        default:
                            break;
                    }
                    if (debug)
                        System.out.println(pt.getName() + " Break 1b");
                }
            }
            if (debug)
                System.out.println(worldType.getName() + " Break 1c");
        }
        if (debug)
            System.out.println(worldType.getName() + " Break 2");
        // Set exits:
        for (PlaceType pt : worldType.getPlaces()) {
            if (pt.getBearing() != null) {
                // To get a little bit of spread, use quadrants of map to place exits.
                int quadrant = 1;
                // Don't place exits in corners.
                do {
                    if (pt.getBearing() == Bearing.NORTH) {
                        coreY = height - 1;
                        if (quadrant == 1) {
                            coreX = rnd.nextInt(width / 2 - 1) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else {
                            coreX = (width / 2) + rnd.nextInt(width / 2 - 1);
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant = 1;
                        }
                    } else if (pt.getBearing() == Bearing.EAST) {
                        coreX = width - 1;
                        if (quadrant == 1) {
                            coreY = rnd.nextInt(height / 2 - 1) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else {
                            coreY = (height / 2) + rnd.nextInt(height / 2 - 1);
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant = 1;
                        }
                    } else if (pt.getBearing() == Bearing.SOUTH) {
                        coreY = 0;
                        if (quadrant == 1) {
                            coreX = rnd.nextInt(width / 2 - 1) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else {
                            coreX = (width / 2) + rnd.nextInt(width / 2 - 1);
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant = 1;
                        }
                    } else if (pt.getBearing() == Bearing.WEST) {
                        coreX = 0;
                        if (quadrant == 1) {
                            coreY = rnd.nextInt(height / 2 - 1) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else {
                            coreY = (height / 2) + rnd.nextInt(height / 2 - 1);
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant = 1;
                        }
                    } else {
                        if (quadrant == 1) {
                            coreX = rnd.nextInt((width - 2) / 2) + 1;
                            coreY = rnd.nextInt((height - 2) / 2) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else if (quadrant == 2) {
                            coreX = (width - 2) / 2 + rnd.nextInt((width - 2) / 2) + 1;
                            coreY = rnd.nextInt((height - 2) / 2) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else if (quadrant == 3) {
                            coreX = rnd.nextInt((width - 2) / 2) + 1;
                            coreY = (height - 2) / 2 + rnd.nextInt((height - 2) / 2) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant++;
                        } else {
                            coreX = (width - 2) / 2 + rnd.nextInt((width - 2) / 2) + 1;
                            coreY = (height - 2) / 2 + rnd.nextInt((height - 2) / 2) + 1;
                            if (!grid[coreX][coreY].isBlocked())
                                quadrant = 1;
                        }
                    }
                } while (grid[coreX][coreY].isBlocked());
                grid[coreX][coreY].setBlocked(false);
                visited[coreX][coreY] = false;
                grid[coreX][coreY].setPlace(new GenericPlace(pt));
                if (pt.getBearing() == Bearing.NORTH)
                    grid[coreX][coreY].setNorthAccess(true);
                else if (pt.getBearing() == Bearing.EAST)
                    grid[coreX][coreY].setEastAccess(true);
                else if (pt.getBearing() == Bearing.SOUTH)
                    grid[coreX][coreY].setSouthAccess(true);
                else if (pt.getBearing() == Bearing.WEST)
                    grid[coreX][coreY].setWestAccess(true);
            }
        }
        if (debug)
            System.out.println(worldType.getName() + " Break 3");
        // Add places:
        int quadrant = 1;
        List<PlaceType> places = new ArrayList<>();
        for (PlaceType p : worldType.getPlaces()) {
            if (p.getBearing() == null && p.getParentAlignment() == null)
                places.add(p);
        }
        Collections.shuffle(places);
        for (PlaceType p : places) {
            do {
                coreX = rnd.nextInt(width);
                coreY = rnd.nextInt(height);
                if (quadrant == 1) {
                    coreX = rnd.nextInt(width / 2);
                    coreY = rnd.nextInt(height / 2);
                } else if (quadrant == 2) {
                    coreX = width / 2 + rnd.nextInt(width / 2);
                    coreY = rnd.nextInt(height / 2);
                } else if (quadrant == 3) {
                    coreX = rnd.nextInt(width / 2);
                    coreY = height / 2 + rnd.nextInt(height / 2);
                } else {
                    coreX = width / 2 + rnd.nextInt(width / 2);
                    coreY = height / 2 + rnd.nextInt(height / 2);
                }
            } while (grid[coreX][coreY].isBlocked() || grid[coreX][coreY].getPlace().getPlaceType() != worldType.getStandardPlace());
            quadrant++;
            if (quadrant > 4)
                quadrant = 1;
            grid[coreX][coreY].setPlace(new GenericPlace(p));
        }
        // Add cuttOffZone places:
        if (worldType.getDangerousPlaces() != null)
            for (PlaceType p : worldType.getDangerousPlaces()) {
                Vector2i vTemp = dangerousPlaces.get(Util.random.nextInt(dangerousPlaces.size()));
                grid[vTemp.getX()][vTemp.getY()].setPlace(new GenericPlace(p));
                dangerousPlaces.remove(vTemp);
            }
        Cell[][] finalGrid = generateMap(width / 2, height / 2, grid, visited);
        if (debug)
            System.out.println(worldType.getName() + " Break 4");
        // Expand the generated grid:
        Cell[][] expandedGrid = new Cell[width * 2 - 1][height * 2 - 1];
        for (int i = 0; i < width * 2 - 1; i++) for (int j = 0; j < height * 2 - 1; j++) expandedGrid[i][j] = new Cell(worldType, new Vector2i(i, j));
        for (int i = 0; i < width * 2 - 1; i++) for (int j = 0; j < height * 2 - 1; j++) {
            if (i % 2 == 0 && j % 2 == 0) {
                if (finalGrid[i / 2][j / 2].getPlace().getPlaceType() != worldType.getStandardPlace()) {
                    expandedGrid[i][j].setPlace(finalGrid[i / 2][j / 2].getPlace());
                    w.addPlaceOfInterest(finalGrid[i / 2][j / 2].getPlace(), new Vector2i(i, j));
                }
            } else if (i % 2 == 0) {
                if (finalGrid[i / 2][j / 2].isNorthAccess())
                    expandedGrid[i][j].setSouthAccess(true);
                if (finalGrid[i / 2][(j + 1) / 2].isSouthAccess())
                    expandedGrid[i][j].setNorthAccess(true);
                if (!finalGrid[i / 2][j / 2].isNorthAccess() && !finalGrid[i / 2][(j + 1) / 2].isSouthAccess()) {
                    expandedGrid[i][j].setPlace(new GenericPlace(worldType.getCutOffZone()));
                }
            } else if (j % 2 == 0) {
                if (finalGrid[i / 2][j / 2].isEastAccess())
                    expandedGrid[i][j].setWestAccess(true);
                if (finalGrid[(i + 1) / 2][j / 2].isWestAccess())
                    expandedGrid[i][j].setEastAccess(true);
                if (!finalGrid[i / 2][j / 2].isEastAccess() && !finalGrid[(i + 1) / 2][j / 2].isWestAccess()) {
                    expandedGrid[i][j].setPlace(new GenericPlace(worldType.getCutOffZone()));
                }
            } else {
                if (Math.random() > 0.8) {
                    expandedGrid[i][j].setPlace(new GenericPlace(worldType.getCutOffZone()));
                } else {
                    expandedGrid[i][j].setPlace(new GenericPlace(PlaceType.GENERIC_IMPASSABLE));
                }
            }
        }
        // printMaze(expandedGrid);
        w.setGrid(expandedGrid);
    }
}
Also used : PlaceType(com.lilithsthrone.world.places.PlaceType) Color(java.awt.Color) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Vector2i(com.lilithsthrone.utils.Vector2i) GenericPlace(com.lilithsthrone.world.places.GenericPlace)

Example 8 with GenericPlace

use of com.lilithsthrone.world.places.GenericPlace in project liliths-throne-public by Innoxia.

the class World method saveAsXML.

@Override
public Element saveAsXML(Element parentElement, Document doc) {
    Element element = doc.createElement("world");
    parentElement.appendChild(element);
    CharacterUtils.addAttribute(doc, element, "worldType", this.getWorldType().toString());
    CharacterUtils.addAttribute(doc, element, "width", String.valueOf(this.WORLD_WIDTH));
    CharacterUtils.addAttribute(doc, element, "height", String.valueOf(this.WORLD_HEIGHT));
    Element innerElement = doc.createElement("grid");
    element.appendChild(innerElement);
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[0].length; j++) {
            grid[i][j].saveAsXML(innerElement, doc);
        }
    }
    innerElement = doc.createElement("placesOfInterest");
    element.appendChild(innerElement);
    for (Entry<GenericPlace, Vector2i> entry : placesOfInterest.entrySet()) {
        Element e = doc.createElement("entry");
        innerElement.appendChild(e);
        Element place = doc.createElement("placeEntry");
        e.appendChild(place);
        entry.getKey().saveAsXML(place, doc);
        Element location = doc.createElement("locationEntry");
        e.appendChild(location);
        CharacterUtils.addAttribute(doc, location, "x", String.valueOf(entry.getValue().getX()));
        CharacterUtils.addAttribute(doc, location, "y", String.valueOf(entry.getValue().getY()));
    }
    return element;
}
Also used : Element(org.w3c.dom.Element) GenericPlace(com.lilithsthrone.world.places.GenericPlace) Vector2i(com.lilithsthrone.utils.Vector2i)

Aggregations

GenericPlace (com.lilithsthrone.world.places.GenericPlace)8 Vector2i (com.lilithsthrone.utils.Vector2i)4 ArrayList (java.util.ArrayList)3 PlaceUpgrade (com.lilithsthrone.world.places.PlaceUpgrade)2 NPC (com.lilithsthrone.game.character.npc.NPC)1 Cell (com.lilithsthrone.world.Cell)1 PlaceType (com.lilithsthrone.world.places.PlaceType)1 Color (java.awt.Color)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 Random (java.util.Random)1 Element (org.w3c.dom.Element)1