Search in sources :

Example 11 with WorldCoordinate

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

the class WorldPanelTest method testSetHome.

/**
 * Test of gotoHome method, of class WorldPanel.
 */
/*@Test
    public void testGotoHome() {
        System.out.println("gotoHome");

        World world = new World();
        WorldCoordinate home = new WorldCoordinate(1, 2, 3);
        world.setHome(home);

        WorldPanel instance = new WorldPanel(world, false);
        instance.gotoHome();

        assertEquals(home.getLayer(), instance.getPosition().getLayer());
        assertEquals(home.getX(), instance.getPosition().getX(), 0.01);
        assertEquals(home.getY(), instance.getPosition().getY(), 0.01);
    }*/
/**
 * Test of setHome method, of class WorldPanel.
 */
@Test
public void testSetHome() {
    System.out.println("setHome");
    World world = new World();
    WorldCoordinate coord = new WorldCoordinate(1, 6, 9);
    WorldPanel instance = new WorldPanel(null, world, false);
    instance.pushPosition(coord);
    // might be changed by moveScreenToCursor()
    coord = instance.getPosition();
    instance.setHome();
    assertEquals(coord.getLayer(), world.getHome().getLayer());
    assertEquals(coord.getX(), world.getHome().getX(), 0.01);
    assertEquals(coord.getY(), world.getHome().getY(), 0.01);
}
Also used : WorldCoordinate(mudmap2.backend.WorldCoordinate) World(mudmap2.backend.World) Test(org.junit.Test)

Example 12 with WorldCoordinate

use of mudmap2.backend.WorldCoordinate 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 13 with WorldCoordinate

use of mudmap2.backend.WorldCoordinate 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 14 with WorldCoordinate

use of mudmap2.backend.WorldCoordinate 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)

Example 15 with WorldCoordinate

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

the class PlaceSelectionDialog method create.

@Override
void create() {
    optionPane = new JOptionPane();
    optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
    setContentPane(optionPane);
    optionPane.setMessage(worldtab = new WorldTab(parentFrame, world, true));
    worldtab.getWorldPanel().setCursorForced(true);
    worldtab.getWorldPanel().resetHistory(new WorldCoordinate(default_coordinate));
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    optionPane.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent arg0) {
            if (isVisible() && arg0.getSource() == optionPane && arg0.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
                int value = ((Integer) optionPane.getValue());
                ok = value == JOptionPane.OK_OPTION;
                dispose();
                if (parentFrame != null)
                    parentFrame.repaint();
            }
        }
    });
    setSize(500, 500);
}
Also used : WorldTab(mudmap2.frontend.WorldTab) PropertyChangeEvent(java.beans.PropertyChangeEvent) WorldCoordinate(mudmap2.backend.WorldCoordinate) PropertyChangeListener(java.beans.PropertyChangeListener) JOptionPane(javax.swing.JOptionPane)

Aggregations

WorldCoordinate (mudmap2.backend.WorldCoordinate)14 Layer (mudmap2.backend.Layer)6 Place (mudmap2.backend.Place)6 Color (java.awt.Color)4 World (mudmap2.backend.World)4 MapPainterDefault (mudmap2.frontend.GUIElement.WorldPanel.MapPainterDefault)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Path (mudmap2.backend.Path)3 Pair (mudmap2.utils.Pair)3 Test (org.junit.Test)3 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2 Graphics2D (java.awt.Graphics2D)2 BufferedImage (java.awt.image.BufferedImage)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 PlaceGroup (mudmap2.backend.PlaceGroup)2 RiskLevel (mudmap2.backend.RiskLevel)2 WorldFileInvalidTypeException (mudmap2.backend.WorldFileReader.Exception.WorldFileInvalidTypeException)2