Search in sources :

Example 21 with Place

use of mudmap2.backend.Place in project mudmap2 by Neop.

the class WorldFileDefaultTest method testReadWriteFile.

/**
 * Test of readFile an writeFile methods, of class WorldFileDefault.
 * @throws java.lang.Exception
 */
@Test
public void testReadWriteFile() throws Exception {
    System.out.println("writeFile");
    String worldName = "FooBar";
    World world = new World(worldName);
    Layer layer1 = world.getNewLayer();
    Layer layer2 = world.getNewLayer();
    String[] placeNames = { "Foo", "Foo Bar", "Baz" };
    Integer[] placeX = { 0, 1, 6 };
    Integer[] placeY = { 0, 0, 4 };
    Place pl0 = new Place(placeNames[0], placeX[0], placeY[0], layer1);
    Place pl1 = new Place(placeNames[1], placeX[1], placeY[1], layer1);
    Place pl2 = new Place(placeNames[2], placeX[2], placeY[2], layer2);
    layer1.put(pl0);
    layer1.put(pl1);
    layer2.put(pl2);
    Path path0 = new Path(pl0, "n", pl1, "s");
    Path path1 = new Path(pl1, "e", pl0, "w");
    pl0.connectPath(path0);
    pl1.connectPath(path1);
    pl0.connectChild(pl2);
    String pgName0 = "myGroup";
    String pgName1 = "my second area";
    Color pgCol0 = Color.orange;
    Color pgCol1 = Color.blue;
    PlaceGroup pg0 = new PlaceGroup(pgName0, pgCol0);
    PlaceGroup pg1 = new PlaceGroup(pgName1, pgCol1);
    pl0.setPlaceGroup(pg0);
    pl1.setPlaceGroup(pg1);
    String flag0 = "a";
    String flag1 = "cd e";
    String flag2 = "fgh";
    pl0.setFlag(flag0, true);
    pl0.setFlag(flag1, true);
    pl0.setFlag(flag2, false);
    String comment0 = "This is a test comment";
    String comment1 = "and a second comment line";
    String comment2 = "and another one";
    pl0.addComment(comment0);
    pl0.addComment(comment1);
    pl0.addComment(comment2);
    String wfFile = folder.getRoot() + "/wfj";
    WorldFileDefault instanceWriter = new WorldFileDefault(wfFile);
    instanceWriter.writeFile(world);
    WorldFileDefault instanceReader = new WorldFileDefault(wfFile);
    World result = instanceReader.readFile();
    assertEquals(worldName, result.getName());
    assertEquals(2, result.getLayers().size());
    int placeNum = 0;
    Layer layer1new = null;
    Layer layer2new = null;
    for (Layer layer : result.getLayers()) {
        placeNum += layer.getPlaces().size();
        // find corresponding layers
        if (layer.getPlaces().size() == 1)
            layer2new = layer;
        else if (layer.getPlaces().size() == 2)
            layer1new = layer;
    }
    assertEquals(3, placeNum);
    assertNotNull(layer1new);
    assertNotNull(layer2new);
    Place pl0r = layer1new.get(placeX[0], placeY[0]);
    Place pl1r = layer1new.get(placeX[1], placeY[1]);
    Place pl2r = layer2new.get(placeX[2], placeY[2]);
    assertNotNull(pl0r);
    assertNotNull(pl1r);
    assertNotNull(pl2r);
    assertEquals(pl0.getName(), pl0r.getName());
    assertEquals(pl1.getName(), pl1r.getName());
    assertEquals(pl2.getName(), pl2r.getName());
    assertEquals(pl0.getX(), pl0r.getX());
    assertEquals(pl0.getY(), pl0r.getY());
    assertEquals(pl1.getX(), pl1r.getX());
    assertEquals(pl1.getY(), pl1r.getY());
    assertEquals(pl2.getX(), pl2r.getX());
    assertEquals(pl2.getY(), pl2r.getY());
    assertEquals(pl0r.getLayer(), pl1r.getLayer());
    assertNotSame(pl0r.getLayer().getId(), pl2r.getLayer().getId());
    assertEquals(2, pl0r.getPaths().size());
    assertEquals(2, pl1r.getPaths().size());
    assertEquals(0, pl2r.getPaths().size());
    Path path0r = pl0r.getPaths().toArray(new Path[2])[0];
    Path path1r = pl0r.getPaths().toArray(new Path[2])[1];
    Path path2r = pl1r.getPaths().toArray(new Path[2])[0];
    Path path3r = pl1r.getPaths().toArray(new Path[2])[1];
    assertTrue(path0r != path1r);
    assertTrue(path0r == path2r || path0r == path3r);
    assertTrue(path1r == path2r || path1r == path3r);
    assertEquals(pl0r.getPathTo("n"), pl1r.getPathTo("s"));
    assertEquals(pl0r.getPathTo("w"), pl1r.getPathTo("e"));
    assertEquals(1, pl0r.getChildren().size());
    assertEquals(0, pl1r.getChildren().size());
    assertEquals(0, pl2r.getChildren().size());
    assertEquals(0, pl0r.getParents().size());
    assertEquals(0, pl1r.getParents().size());
    assertEquals(1, pl2r.getParents().size());
    assertEquals(pl0r.getChildren().toArray(new Place[1])[0], pl2r);
    assertEquals(pl2r.getParents().toArray(new Place[1])[0], pl0r);
    assertNotNull(pl0r.getPlaceGroup());
    assertNotNull(pl1r.getPlaceGroup());
    assertNull(pl2r.getPlaceGroup());
    assertEquals(pl0.getPlaceGroup().getName(), pl0r.getPlaceGroup().getName());
    assertEquals(pl1.getPlaceGroup().getName(), pl1r.getPlaceGroup().getName());
    assertEquals(2, pl0r.getFlags().size());
    assertEquals(0, pl1r.getFlags().size());
    assertEquals(0, pl2r.getFlags().size());
    assertTrue(pl0r.getFlag(flag0));
    assertTrue(pl0r.getFlag(flag1));
    assertFalse(pl0r.getFlag(flag2));
    assertEquals(3, pl0r.getComments().size());
    assertEquals(0, pl1r.getComments().size());
    assertEquals(0, pl2r.getComments().size());
    assertTrue(pl0r.getComments().contains(comment0));
    assertTrue(pl0r.getComments().contains(comment1));
    assertTrue(pl0r.getComments().contains(comment2));
}
Also used : Path(mudmap2.backend.Path) Color(java.awt.Color) World(mudmap2.backend.World) PlaceGroup(mudmap2.backend.PlaceGroup) Layer(mudmap2.backend.Layer) Place(mudmap2.backend.Place) Test(org.junit.Test)

Example 22 with Place

use of mudmap2.backend.Place in project mudmap2 by Neop.

the class WorldFileJSONTest method testReadWriteFile.

/**
 * Test of readFile and writeFile methods, of class WorldFileJSON.
 * @throws java.lang.Exception
 */
@Test
public void testReadWriteFile() throws Exception {
    System.out.println("readFile / writeFile");
    String worldName = "FooBar";
    World world = new World(worldName);
    Layer layer1 = world.getNewLayer();
    Layer layer2 = world.getNewLayer();
    layer1.setName("MyLayer");
    String[] placeNames = { "Foo", "Foo Bar", "Baz" };
    Integer[] placeX = { 0, 1, 6 };
    Integer[] placeY = { 0, 0, 4 };
    Place pl0 = new Place(placeNames[0], placeX[0], placeY[0], layer1);
    Place pl1 = new Place(placeNames[1], placeX[1], placeY[1], layer1);
    Place pl2 = new Place(placeNames[2], placeX[2], placeY[2], layer2);
    layer1.put(pl0);
    layer1.put(pl1);
    layer2.put(pl2);
    Path path0 = new Path(pl0, "n", pl1, "s");
    Path path1 = new Path(pl1, "e", pl0, "w");
    pl0.connectPath(path0);
    pl1.connectPath(path1);
    pl0.connectChild(pl2);
    String pgName0 = "myArea";
    String pgName1 = "my second area";
    Color pgCol0 = Color.orange;
    Color pgCol1 = Color.blue;
    PlaceGroup pg0 = new PlaceGroup(pgName0, pgCol0);
    PlaceGroup pg1 = new PlaceGroup(pgName1, pgCol1);
    pl0.setPlaceGroup(pg0);
    pl1.setPlaceGroup(pg1);
    String flag0 = "a";
    String flag1 = "cd e";
    String flag2 = "fgh";
    pl0.setFlag(flag0, true);
    pl0.setFlag(flag1, true);
    pl0.setFlag(flag2, false);
    String comment0 = "This is a test comment";
    String comment1 = "and a second comment line";
    String comment2 = "and another one";
    pl0.addComment(comment0);
    pl0.addComment(comment1);
    pl0.addComment(comment2);
    String wfjFile = folder.getRoot() + "/wfj";
    WorldFileJSON instanceWriter = new WorldFileJSON(wfjFile);
    instanceWriter.writeFile(world);
    WorldFileJSON instanceReader = new WorldFileJSON(wfjFile);
    World result = instanceReader.readFile();
    assertEquals(worldName, result.getName());
    assertEquals(2, result.getLayers().size());
    int placeNum = 0;
    Layer layer1new = null;
    Layer layer2new = null;
    for (Layer layer : result.getLayers()) {
        placeNum += layer.getPlaces().size();
        // find corresponding layers
        if (layer.getPlaces().size() == 1)
            layer2new = layer;
        else if (layer.getPlaces().size() == 2)
            layer1new = layer;
    }
    assertEquals(3, placeNum);
    assertNotNull(layer1new);
    assertNotNull(layer2new);
    Place pl0r = layer1new.get(placeX[0], placeY[0]);
    Place pl1r = layer1new.get(placeX[1], placeY[1]);
    Place pl2r = layer2new.get(placeX[2], placeY[2]);
    assertNotNull(pl0r);
    assertNotNull(pl1r);
    assertNotNull(pl2r);
    assertEquals(pl0.getName(), pl0r.getName());
    assertEquals(pl1.getName(), pl1r.getName());
    assertEquals(pl2.getName(), pl2r.getName());
    assertEquals(pl0.getX(), pl0r.getX());
    assertEquals(pl0.getY(), pl0r.getY());
    assertEquals(pl1.getX(), pl1r.getX());
    assertEquals(pl1.getY(), pl1r.getY());
    assertEquals(pl2.getX(), pl2r.getX());
    assertEquals(pl2.getY(), pl2r.getY());
    assertEquals(pl0r.getLayer(), pl1r.getLayer());
    assertNotSame(pl0r.getLayer().getId(), pl2r.getLayer().getId());
    assertEquals(pl0.getLayer().getName(), pl0r.getLayer().getName());
    assertTrue(pl0r.getLayer().hasName());
    assertEquals(pl1.getLayer().getName(), pl1r.getLayer().getName());
    assertFalse(pl2r.getLayer().hasName());
    assertEquals(2, pl0r.getPaths().size());
    assertEquals(2, pl1r.getPaths().size());
    assertEquals(0, pl2r.getPaths().size());
    Path path0r = pl0r.getPaths().toArray(new Path[2])[0];
    Path path1r = pl0r.getPaths().toArray(new Path[2])[1];
    Path path2r = pl1r.getPaths().toArray(new Path[2])[0];
    Path path3r = pl1r.getPaths().toArray(new Path[2])[1];
    assertTrue(path0r != path1r);
    assertTrue(path0r == path2r || path0r == path3r);
    assertTrue(path1r == path2r || path1r == path3r);
    assertEquals(pl0r.getPathTo("n"), pl1r.getPathTo("s"));
    assertEquals(pl0r.getPathTo("w"), pl1r.getPathTo("e"));
    assertEquals(1, pl0r.getChildren().size());
    assertEquals(0, pl1r.getChildren().size());
    assertEquals(0, pl2r.getChildren().size());
    assertEquals(0, pl0r.getParents().size());
    assertEquals(0, pl1r.getParents().size());
    assertEquals(1, pl2r.getParents().size());
    assertEquals(pl0r.getChildren().toArray(new Place[1])[0], pl2r);
    assertEquals(pl2r.getParents().toArray(new Place[1])[0], pl0r);
    assertNotNull(pl0r.getPlaceGroup());
    assertNotNull(pl1r.getPlaceGroup());
    assertNull(pl2r.getPlaceGroup());
    assertEquals(pl0.getPlaceGroup().getName(), pl0r.getPlaceGroup().getName());
    assertEquals(pl1.getPlaceGroup().getName(), pl1r.getPlaceGroup().getName());
    assertEquals(2, pl0r.getFlags().size());
    assertEquals(0, pl1r.getFlags().size());
    assertEquals(0, pl2r.getFlags().size());
    assertTrue(pl0r.getFlag(flag0));
    assertTrue(pl0r.getFlag(flag1));
    assertFalse(pl0r.getFlag(flag2));
    assertEquals(3, pl0r.getComments().size());
    assertEquals(0, pl1r.getComments().size());
    assertEquals(0, pl2r.getComments().size());
    assertTrue(pl0r.getComments().contains(comment0));
    assertTrue(pl0r.getComments().contains(comment1));
    assertTrue(pl0r.getComments().contains(comment2));
// TODO: test labels
}
Also used : Path(mudmap2.backend.Path) Color(java.awt.Color) World(mudmap2.backend.World) PlaceGroup(mudmap2.backend.PlaceGroup) Layer(mudmap2.backend.Layer) Place(mudmap2.backend.Place) Test(org.junit.Test)

Example 23 with Place

use of mudmap2.backend.Place in project mudmap2 by Neop.

the class WorldPanelTest method testGetSelectedPlace.

/**
 * Test of getSelectedPlace method, of class WorldPanel.
 */
@Test
public void testGetSelectedPlace() {
    try {
        System.out.println("getSelectedPlace");
        World world = new World();
        Layer l = world.getNewLayer();
        Place pl1 = new Place("BLa", 4, 6, l);
        l.put(pl1);
        Place pl2 = new Place("Blub", 7, 1, l);
        l.put(pl2);
        WorldPanel instance = new WorldPanel(null, world, false);
        instance.pushPosition(new WorldCoordinate(l.getId(), 0, 0));
        instance.setCursor(2, 3);
        assertNull(instance.getSelectedPlace());
        instance.setCursor(pl1.getX(), pl1.getY());
        assertEquals(pl1, instance.getSelectedPlace());
        instance.setCursor(pl2.getX(), pl2.getY());
        assertEquals(pl2, instance.getSelectedPlace());
    } catch (Layer.PlaceNotInsertedException ex) {
        Logger.getLogger(WorldPanelTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : WorldCoordinate(mudmap2.backend.WorldCoordinate) World(mudmap2.backend.World) Layer(mudmap2.backend.Layer) Place(mudmap2.backend.Place) Test(org.junit.Test)

Example 24 with Place

use of mudmap2.backend.Place in project mudmap2 by Neop.

the class WorldFileJSON method writeFile.

/**
 * Write world to file
 * @param world
 * @throws java.io.IOException
 */
@Override
public void writeFile(World world) throws IOException {
    JSONObject root = new JSONObject();
    // metaWriter data
    // mudmap version
    String mudmapVer = getClass().getPackage().getImplementationVersion();
    if (mudmapVer != null)
        root.put("mudmapVer", getClass().getPackage().getImplementationVersion());
    else
        root.put("mudmapVer", "dev");
    // file version
    root.put("fileVer", versionMajor + "." + versionMinor);
    // world name
    root.put("worldName", world.getName());
    root.put("showPlaceID", world.getShowPlaceId());
    // tile center color
    if (world.getTileCenterColor() != null) {
        root.put("tileCenterCol", colToHex(world.getTileCenterColor()));
    }
    // cardinal and non cardinal path color
    if (world.getPathColor() != null) {
        root.put("pathCol", colToHex(world.getPathColor()));
    }
    if (world.getPathColorNstd() != null) {
        root.put("pathColNonCardinal", colToHex(world.getPathColorNstd()));
    }
    // other path colors
    JSONArray pathColorsArray = new JSONArray();
    root.put("pathColDefs", pathColorsArray);
    for (Map.Entry<String, Color> pathCol : world.getPathColors().entrySet()) {
        if (pathCol.getValue() != null) {
            JSONObject pathColObj = new JSONObject();
            pathColObj.put("path", pathCol.getKey());
            pathColObj.put("col", colToHex(pathCol.getValue()));
            pathColorsArray.put(pathColObj);
        }
    }
    // risk level colors
    JSONArray riskLevelColors = new JSONArray();
    root.put("riskLevels", riskLevelColors);
    for (RiskLevel rlc : world.getRiskLevels()) {
        JSONObject rlo = new JSONObject();
        rlo.put("id", rlc.getId());
        rlo.put("desc", rlc.getDescription());
        rlo.put("col", colToHex(rlc.getColor()));
        riskLevelColors.put(rlo);
    }
    // areaArray
    // create IDs for areaArray
    HashMap<PlaceGroup, Integer> areaIDs = new HashMap<>();
    // incremental id
    Integer cnt = 0;
    for (PlaceGroup a : world.getPlaceGroups()) {
        Boolean inUse = false;
        // removePlace unused
        for (Layer layer : world.getLayers()) {
            for (Place place : layer.getPlaces()) {
                if (place.getPlaceGroup() == a) {
                    inUse = true;
                    break;
                }
            }
        }
        if (inUse)
            areaIDs.put(a, ++cnt);
    }
    // add areaArray
    JSONArray areas = new JSONArray();
    root.put("areas", areas);
    for (PlaceGroup area : world.getPlaceGroups()) {
        JSONObject areaObj = new JSONObject();
        areaObj.put("id", areaIDs.get(area));
        areaObj.put("name", area.getName());
        areaObj.put("col", colToHex(area.getColor()));
        areas.put(areaObj);
    }
    // helper to assign new layer ids
    Integer nextLayerID = 0;
    layerIDs = new HashMap<>();
    // layers (for quadtree optimization
    JSONArray layers = new JSONArray();
    root.put("layers", layers);
    for (Layer layer : world.getLayers()) {
        if (!layer.getPlaces().isEmpty()) {
            JSONObject layerObj = new JSONObject();
            // add layer to id map
            Integer layerID = nextLayerID++;
            layerIDs.put(layer.getId(), layerID);
            layerObj.put("id", layerID);
            layerObj.put("centerX", layer.getCenterX());
            layerObj.put("centerY", layer.getCenterY());
            if (layer.hasName())
                layerObj.put("name", layer.getName());
            layers.put(layerObj);
        }
    }
    // places
    JSONArray places = new JSONArray();
    root.put("places", places);
    for (Layer layer : world.getLayers()) {
        for (Place place : layer.getPlaces()) {
            JSONObject placeObj = new JSONObject();
            placeObj.put("id", place.getId());
            placeObj.put("n", place.getName());
            placeObj.put("l", translateLayerID(place.getLayer().getId()));
            placeObj.put("x", place.getX());
            placeObj.put("y", place.getY());
            if (place.getPlaceGroup() != null)
                placeObj.put("a", areaIDs.get(place.getPlaceGroup()));
            if (place.getRiskLevel() != null)
                placeObj.put("r", place.getRiskLevel().getId());
            if (place.getRecLevelMin() > -1)
                placeObj.put("lvlMin", place.getRecLevelMin());
            if (place.getRecLevelMax() > -1)
                placeObj.put("lvlMax", place.getRecLevelMax());
            // child places
            if (!place.getChildren().isEmpty()) {
                JSONArray children = new JSONArray();
                placeObj.put("c", children);
                for (Place child : place.getChildren()) {
                    children.put(child.getId());
                }
            }
            // parent places
            if (!place.getParents().isEmpty()) {
                JSONArray parents = new JSONArray();
                placeObj.put("p", parents);
                for (Place parent : place.getParents()) {
                    parents.put(parent.getId());
                }
            }
            // flags
            if (!place.getFlags().isEmpty()) {
                JSONArray flags = new JSONArray();
                placeObj.put("f", flags);
                for (Map.Entry<String, Boolean> flag : place.getFlags().entrySet()) {
                    if (flag.getValue())
                        flags.put(flag.getKey());
                }
            }
            // comments
            if (!place.getComments().isEmpty()) {
                JSONArray comments = new JSONArray();
                placeObj.put("co", comments);
                int idx = 0;
                for (String comment : place.getComments()) {
                    comments.put(idx++, comment);
                }
            }
            places.put(placeObj);
        }
    }
    // paths
    JSONArray pathsArray = new JSONArray();
    root.put("paths", pathsArray);
    // paths that have already been added
    HashSet<Path> paths = new HashSet<>();
    for (Layer layer : world.getLayers()) {
        for (Place place : layer.getPlaces()) {
            for (Path path : place.getPaths()) {
                if (!paths.contains(path)) {
                    JSONArray pathObj = new JSONArray();
                    JSONObject p1 = new JSONObject();
                    p1.put("p", path.getPlaces()[0].getId());
                    p1.put("e", path.getExit(path.getPlaces()[0]));
                    pathObj.put(p1);
                    JSONObject p2 = new JSONObject();
                    p2.put("p", path.getPlaces()[1].getId());
                    p2.put("e", path.getExit(path.getPlaces()[1]));
                    pathObj.put(p2);
                    pathsArray.put(pathObj);
                    paths.add(path);
                }
            }
        }
    }
    // home position
    WorldCoordinate home = world.getHome();
    JSONObject obj = new JSONObject();
    obj.put("l", translateLayerID(home.getLayer()));
    obj.put("x", home.getX());
    obj.put("y", home.getY());
    root.put("home", obj);
    // add metaWriter data from WorldTab
    if (metaWriter != null)
        root.put("meta", metaWriter.getMeta(layerIDs));
    try (FileWriter writer = new FileWriter(filename)) {
        // indentation for better readability (for debugging), increases file size
        // root.write(writer, 4, 0);
        root.write(writer);
        fileRoot = root;
    } catch (Exception ex) {
        System.out.println(ex.getLocalizedMessage());
    }
}
Also used : Path(mudmap2.backend.Path) WorldCoordinate(mudmap2.backend.WorldCoordinate) HashMap(java.util.HashMap) Color(java.awt.Color) FileWriter(java.io.FileWriter) Layer(mudmap2.backend.Layer) WorldFileInvalidTypeException(mudmap2.backend.WorldFileReader.Exception.WorldFileInvalidTypeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PlaceGroup(mudmap2.backend.PlaceGroup) HashMap(java.util.HashMap) Map(java.util.Map) RiskLevel(mudmap2.backend.RiskLevel) Place(mudmap2.backend.Place) HashSet(java.util.HashSet)

Example 25 with Place

use of mudmap2.backend.Place in project mudmap2 by Neop.

the class WorldFileJSON method readFile.

/**
 * Read world file
 * @return new world object or null if file is invalid
 * @throws Exception
 * @throws WorldFileInvalidTypeException
 * @ throws WorldFileReadError
 */
@Override
public World readFile() throws Exception {
    World world = null;
    try {
        JSONObject root = getJSONRoot();
        // check file version
        if (root.has("fileVer")) {
            String[] fileVer = root.getString("fileVer").split("\\.");
            if (versionMajor != Integer.parseInt(fileVer[0])) {
                // version major not equal: different file format
                throw new WorldFileInvalidTypeException(filename, "invalid world file version", null);
            }
            if (versionMinor < Integer.parseInt(fileVer[1])) {
                // file was created by a newer MUD Map: might have unsupported features
                int ret = JOptionPane.showConfirmDialog(null, "World file version is greater than the reader version. " + "Please update MUD Map. Continuing might cause data loss.", "Loading world", JOptionPane.OK_CANCEL_OPTION);
                if (ret == JOptionPane.CANCEL_OPTION)
                    throw new WorldFileInvalidTypeException(filename, "Could not read world file", null);
            }
        } else {
            throw new WorldFileInvalidTypeException(filename, "could not read world file version", null);
        }
        // getPlace world name
        String worldName = "";
        if (root.has("worldName"))
            worldName = root.getString("worldName");
        if (worldName.isEmpty()) {
            Integer begin = worldName.lastIndexOf('/');
            if (begin == -1)
                begin = worldName.lastIndexOf('\\');
            begin += 1;
            worldName = worldName.substring(begin);
        }
        // create world root
        world = new World(worldName);
        world.setWorldFile(this);
        // showPlaceID
        if (root.has("showPlaceID")) {
            world.setShowPlaceID(World.ShowPlaceID.valueOf(root.getString("showPlaceID")));
        }
        // tileCenterCol
        if (root.has("tileCenterCol")) {
            world.setTileCenterColor(hexToCol(root.getString("tileCenterCol")));
        }
        // pathCol
        if (root.has("pathCol")) {
            world.setPathColor(hexToCol(root.getString("pathCol")));
        }
        // pathColNonCardinal
        if (root.has("pathColNonCardinal")) {
            world.setPathColorNstd(hexToCol(root.getString("pathColNonCardinal")));
        }
        // pathColDefs
        if (root.has("pathColDefs")) {
            JSONArray pathColDefs = root.getJSONArray("pathColDefs");
            Integer length = pathColDefs.length();
            for (Integer i = 0; i < length; ++i) {
                JSONObject pathColDef = pathColDefs.getJSONObject(i);
                if (pathColDef.has("path") && pathColDef.has("col")) {
                    world.setPathColor(pathColDef.getString("path"), hexToCol(pathColDef.getString("col")));
                }
            }
        }
        // home
        if (root.has("home")) {
            JSONObject home = root.getJSONObject("home");
            if (home.has("l") && home.has("x") && home.has("y")) {
                Integer l = home.getInt("l");
                Double x = home.getDouble("x");
                Double y = home.getDouble("y");
                world.setHome(new WorldCoordinate(l, x, y));
            }
        }
        // riskLevels
        if (root.has("riskLevels")) {
            // remove existing risk levels
            world.getRiskLevels().clear();
            JSONArray riskLevels = root.getJSONArray("riskLevels");
            Integer length = riskLevels.length();
            for (Integer i = 0; i < length; ++i) {
                JSONObject riskLevel = riskLevels.getJSONObject(i);
                if (riskLevel.has("id") && riskLevel.has("desc") && riskLevel.has("col")) {
                    Integer id = riskLevel.getInt("id");
                    String desc = riskLevel.getString("desc");
                    Color col = hexToCol(riskLevel.getString("col"));
                    world.setRiskLevel(new RiskLevel(id, desc, col));
                }
            }
        }
        // areaArray
        HashMap<Integer, PlaceGroup> areas = new HashMap<>();
        if (root.has("areas")) {
            JSONArray areaArray = root.getJSONArray("areas");
            Integer length = areaArray.length();
            for (Integer i = 0; i < length; ++i) {
                JSONObject area = areaArray.getJSONObject(i);
                if (area.has("id") && area.has("name") && area.has("col")) {
                    Integer id = area.getInt("id");
                    String name = area.getString("name");
                    Color col = hexToCol(area.getString("col"));
                    PlaceGroup a = new PlaceGroup(name, col);
                    areas.put(id, a);
                    world.addPlaceGroup(a);
                }
            }
        }
        // layers
        if (root.has("layers")) {
            JSONArray layers = root.getJSONArray("layers");
            Integer length = layers.length();
            for (Integer i = 0; i < length; ++i) {
                JSONObject layer = layers.getJSONObject(i);
                if (layer.has("id")) {
                    Integer id = layer.getInt("id");
                    // create layer
                    Layer l = new Layer(id, world);
                    if (layer.has("centerX") && layer.has("centerY")) {
                        // set quadtree center
                        Integer centerX = layer.getInt("centerX");
                        Integer centerY = layer.getInt("centerY");
                        l.setQuadtree(centerX, centerY);
                    }
                    if (layer.has("name")) {
                        // set layer name
                        l.setName(layer.getString("name"));
                    }
                    world.addLayer(l);
                }
            }
        }
        // places
        HashMap<Integer, Place> places = new HashMap<>();
        HashMap<Place, HashSet<Integer>> childrenMapping = new HashMap<>();
        if (root.has("places")) {
            JSONArray jPlaces = root.getJSONArray("places");
            Integer length = jPlaces.length();
            for (Integer i = 0; i < length; ++i) {
                JSONObject jPlace = jPlaces.getJSONObject(i);
                if (jPlace.has("id") && jPlace.has("n") && jPlace.has("l") && jPlace.has("x") && jPlace.has("y")) {
                    Integer id = jPlace.getInt("id");
                    String name = jPlace.getString("n");
                    Integer layerId = jPlace.getInt("l");
                    Integer x = jPlace.getInt("x");
                    Integer y = jPlace.getInt("y");
                    // get layer
                    Layer layer = world.getLayer(layerId);
                    if (layer == null) {
                        layer = new Layer(layerId, world);
                        world.addLayer(layer);
                    }
                    // create place
                    Place place = new Place(id, name, x, y, layer);
                    places.put(id, place);
                    // area
                    if (jPlace.has("a")) {
                        Integer area = jPlace.getInt("a");
                        place.setPlaceGroup(areas.get(area));
                    }
                    // risk level
                    if (jPlace.has("r")) {
                        Integer risk = jPlace.getInt("r");
                        place.setRiskLevel(world.getRiskLevel(risk));
                    }
                    // rec level
                    if (jPlace.has("lvlMin")) {
                        Integer lvlMin = jPlace.getInt("lvlMin");
                        place.setRecLevelMin(lvlMin);
                    }
                    if (jPlace.has("lvlMax")) {
                        Integer lvlMax = jPlace.getInt("lvlMax");
                        place.setRecLevelMin(lvlMax);
                    }
                    // children
                    if (jPlace.has("c")) {
                        JSONArray children = jPlace.getJSONArray("c");
                        HashSet<Integer> set = new HashSet<>();
                        childrenMapping.put(place, set);
                        Integer lc = children.length();
                        for (Integer c = 0; c < lc; ++c) {
                            set.add(children.getInt(c));
                        }
                    }
                    // flags
                    if (jPlace.has("f")) {
                        JSONArray flags = jPlace.getJSONArray("f");
                        Integer lf = flags.length();
                        for (Integer f = 0; f < lf; ++f) {
                            String flagname = flags.getString(f);
                            place.setFlag(flagname, true);
                        }
                    }
                    // comments
                    if (jPlace.has("co")) {
                        JSONArray comments = jPlace.getJSONArray("co");
                        Integer lc = comments.length();
                        for (Integer c = 0; c < lc; ++c) {
                            place.addComment(comments.getString(c));
                        }
                    }
                    layer.put(place);
                }
            }
        }
        // connect children
        for (Entry<Place, HashSet<Integer>> entry : childrenMapping.entrySet()) {
            Place place = entry.getKey();
            for (Integer id : entry.getValue()) {
                place.connectChild(places.get(id));
            }
        }
        // paths
        if (root.has("paths")) {
            JSONArray paths = root.getJSONArray("paths");
            Integer length = paths.length();
            for (Integer i = 0; i < length; ++i) {
                JSONArray path = paths.getJSONArray(i);
                if (path.length() == 2) {
                    JSONObject p0 = path.getJSONObject(0);
                    JSONObject p1 = path.getJSONObject(1);
                    if (p0.has("p") && p0.has("e") && p1.has("p") && p1.has("e")) {
                        Place pl0 = places.get(p0.getInt("p"));
                        Place pl1 = places.get(p1.getInt("p"));
                        if (pl0 != null && pl1 != null) {
                            Path p = new Path(pl0, p0.getString("e"), pl1, p1.getString("e"));
                            pl0.connectPath(p);
                        }
                    }
                }
            }
        }
        // remember meta data for WorldTab
        if (root.has("meta"))
            metaData = root.getJSONObject("meta");
    } catch (JSONException ex) {
        System.out.println(ex.getLocalizedMessage());
        throw new WorldFileReadError(filename, ex.getLocalizedMessage(), ex);
    }
    return world;
}
Also used : WorldCoordinate(mudmap2.backend.WorldCoordinate) HashMap(java.util.HashMap) WorldFileReadError(mudmap2.backend.WorldFileReader.Exception.WorldFileReadError) World(mudmap2.backend.World) HashSet(java.util.HashSet) Path(mudmap2.backend.Path) Color(java.awt.Color) Layer(mudmap2.backend.Layer) WorldFileInvalidTypeException(mudmap2.backend.WorldFileReader.Exception.WorldFileInvalidTypeException) PlaceGroup(mudmap2.backend.PlaceGroup) RiskLevel(mudmap2.backend.RiskLevel) Place(mudmap2.backend.Place)

Aggregations

Place (mudmap2.backend.Place)20 Layer (mudmap2.backend.Layer)14 ActionEvent (java.awt.event.ActionEvent)7 ActionListener (java.awt.event.ActionListener)7 JLabel (javax.swing.JLabel)7 Path (mudmap2.backend.Path)7 PlaceGroup (mudmap2.backend.PlaceGroup)7 WorldCoordinate (mudmap2.backend.WorldCoordinate)7 Color (java.awt.Color)6 GridBagConstraints (java.awt.GridBagConstraints)6 GridBagLayout (java.awt.GridBagLayout)6 JButton (javax.swing.JButton)6 Insets (java.awt.Insets)5 Pair (mudmap2.utils.Pair)5 HashMap (java.util.HashMap)4 JCheckBox (javax.swing.JCheckBox)4 World (mudmap2.backend.World)4 Dimension (java.awt.Dimension)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3