Search in sources :

Example 11 with Vector2i

use of com.lilithsthrone.utils.Vector2i 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 12 with Vector2i

use of com.lilithsthrone.utils.Vector2i 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)

Example 13 with Vector2i

use of com.lilithsthrone.utils.Vector2i in project liliths-throne-public by Innoxia.

the class World method loadFromXML.

public static World loadFromXML(Element parentElement, Document doc) {
    Cell[][] newGrid = new Cell[Integer.valueOf(parentElement.getAttribute("width"))][Integer.valueOf(parentElement.getAttribute("height"))];
    for (int i = 0; i < ((Element) parentElement.getElementsByTagName("grid").item(0)).getElementsByTagName("cell").getLength(); i++) {
        Element e = (Element) ((Element) parentElement.getElementsByTagName("grid").item(0)).getElementsByTagName("cell").item(i);
        Cell c = Cell.loadFromXML(e, doc);
        newGrid[c.getLocation().getX()][c.getLocation().getY()] = c;
    }
    WorldType type = WorldType.EMPTY;
    if (parentElement.getAttribute("worldType").equals("SEWERS")) {
        type = WorldType.SUBMISSION;
    } else {
        type = WorldType.valueOf(parentElement.getAttribute("worldType"));
    }
    World world = new World(Integer.valueOf(parentElement.getAttribute("width")), Integer.valueOf(parentElement.getAttribute("height")), newGrid, type);
    for (int i = 0; i < ((Element) parentElement.getElementsByTagName("placesOfInterest").item(0)).getElementsByTagName("entry").getLength(); i++) {
        Element e = (Element) ((Element) parentElement.getElementsByTagName("placesOfInterest").item(0)).getElementsByTagName("entry").item(i);
        world.addPlaceOfInterest(GenericPlace.loadFromXML((Element) ((Element) e.getElementsByTagName("placeEntry").item(0)).getElementsByTagName("place").item(0), doc), new Vector2i(Integer.valueOf(((Element) e.getElementsByTagName("locationEntry").item(0)).getAttribute("x")), Integer.valueOf(((Element) e.getElementsByTagName("locationEntry").item(0)).getAttribute("y"))));
    }
    return world;
}
Also used : Element(org.w3c.dom.Element) Vector2i(com.lilithsthrone.utils.Vector2i)

Example 14 with Vector2i

use of com.lilithsthrone.utils.Vector2i in project liliths-throne-public by Innoxia.

the class MainController method moveNorth.

/**
 * Moves the player North.
 */
public void moveNorth() {
    if (Main.game.getPlayer().getLocation().getY() + 1 < Main.game.getActiveWorld().WORLD_HEIGHT) {
        if (Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation().getX(), Main.game.getPlayer().getLocation().getY() + 1).getPlace().getPlaceType() != PlaceType.GENERIC_IMPASSABLE) {
            if (Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation()).getPlace().isItemsDisappear()) {
                Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation()).resetInventory(Util.newArrayListOfValues(new ListValue<>(Rarity.LEGENDARY)));
            }
            Main.game.getPlayer().setLocation(new Vector2i(Main.game.getPlayer().getLocation().getX(), Main.game.getPlayer().getLocation().getY() + 1));
            DialogueNodeOld dn = Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation()).getPlace().getDialogue(true);
            Main.game.setContent(new Response("", "", dn));
        }
    }
}
Also used : Response(com.lilithsthrone.game.dialogue.responses.Response) ListValue(com.lilithsthrone.utils.Util.ListValue) DialogueNodeOld(com.lilithsthrone.game.dialogue.DialogueNodeOld) Vector2i(com.lilithsthrone.utils.Vector2i)

Example 15 with Vector2i

use of com.lilithsthrone.utils.Vector2i in project liliths-throne-public by Innoxia.

the class MainController method moveWest.

/**
 * Moves the player West.
 */
public void moveWest() {
    if (Main.game.getPlayer().getLocation().getX() - 1 >= 0) {
        if (Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation().getX() - 1, Main.game.getPlayer().getLocation().getY()).getPlace().getPlaceType() != PlaceType.GENERIC_IMPASSABLE) {
            if (Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation()).getPlace().isItemsDisappear()) {
                Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation()).resetInventory(Util.newArrayListOfValues(new ListValue<>(Rarity.LEGENDARY)));
            }
            Main.game.getPlayer().setLocation(new Vector2i(Main.game.getPlayer().getLocation().getX() - 1, Main.game.getPlayer().getLocation().getY()));
            DialogueNodeOld dn = Main.game.getActiveWorld().getCell(Main.game.getPlayer().getLocation()).getPlace().getDialogue(true);
            Main.game.setContent(new Response("", "", dn));
        }
    }
}
Also used : Response(com.lilithsthrone.game.dialogue.responses.Response) ListValue(com.lilithsthrone.utils.Util.ListValue) DialogueNodeOld(com.lilithsthrone.game.dialogue.DialogueNodeOld) Vector2i(com.lilithsthrone.utils.Vector2i)

Aggregations

Vector2i (com.lilithsthrone.utils.Vector2i)15 DialogueNodeOld (com.lilithsthrone.game.dialogue.DialogueNodeOld)5 Response (com.lilithsthrone.game.dialogue.responses.Response)5 ListValue (com.lilithsthrone.utils.Util.ListValue)5 GenericPlace (com.lilithsthrone.world.places.GenericPlace)4 PlaceType (com.lilithsthrone.world.places.PlaceType)4 ArrayList (java.util.ArrayList)4 Element (org.w3c.dom.Element)4 Attribute (com.lilithsthrone.game.character.attributes.Attribute)2 EnchantmentEventListener (com.lilithsthrone.controller.eventListeners.EnchantmentEventListener)1 InventorySelectedItemEventListener (com.lilithsthrone.controller.eventListeners.InventorySelectedItemEventListener)1 InventoryTooltipEventListener (com.lilithsthrone.controller.eventListeners.InventoryTooltipEventListener)1 SetContentEventListener (com.lilithsthrone.controller.eventListeners.SetContentEventListener)1 TooltipHideEventListener (com.lilithsthrone.controller.eventListeners.TooltipHideEventListener)1 TooltipInformationEventListener (com.lilithsthrone.controller.eventListeners.TooltipInformationEventListener)1 TooltipMoveEventListener (com.lilithsthrone.controller.eventListeners.TooltipMoveEventListener)1 TooltipResponseDescriptionEventListener (com.lilithsthrone.controller.eventListeners.TooltipResponseDescriptionEventListener)1 TooltipResponseMoveEventListener (com.lilithsthrone.controller.eventListeners.TooltipResponseMoveEventListener)1 ButtonCharactersEventListener (com.lilithsthrone.controller.eventListeners.buttons.ButtonCharactersEventListener)1 ButtonCopyDialogueEventListener (com.lilithsthrone.controller.eventListeners.buttons.ButtonCopyDialogueEventListener)1