Tutorial My way of adding new areas to the default web

Discussion in 'Tutorials & Resources' started by Defeat3d, Nov 9, 2014.

  1. So.. Since other people might be interested in how I add areas to the web, I'm writing this tutorial on how I do it. It's probably wrong in many ways, but I'm sure some of you have no clue, and this might help. I'm open for any constructive feedback, I'm in a learning phase, so I'll take it. :)

    So let's begin..

    1. Getting the coordinates
    2. Adding and connecting the vertices

    1. Getting the coordinates
    I created a simple tool that gets the reachable coordinates around you in a web form (skips some of the tiles) within a given radius and logs them. There's also an option to write them to a file.


    Code (Text):
    1.     public static List<Coordinate> getSurroundingWeb(final Locatable loc, final int radius, final int divisibleBy) {
    2.         final Coordinate[] reachable = (Coordinate[]) loc.getPosition().getReachableCoordinates().toArray();
    3.         List<Coordinate> web = new ArrayList<Coordinate>();
    4.         for (Coordinate c : reachable) {
    5.             if (c.getX() % divisibleBy == 0 && c.getY() % divisibleBy == 0 && c.distanceTo(loc) <= radius) {
    6.                 web.add(c);
    7.             }
    8.         }
    9.         return web;
    10.     }

    2. Adding and connecting the vertices
    This is the part where I think I might be doing stuff wrong, but here's my logic.
    For each coordinate, I look for existing vertices in the web to connect to. If there is, I add a bidirectional edge between the two vertices.

    Code (Text):
    1.     public void addCoordinate(Coordinate... coordinates) {
    2.         for (Coordinate c : coordinates) {
    3.             final CoordinateVertex cv = new CoordinateVertex(c);
    4.             web.addVertices(cv);
    5.             final int[][] add = new int[][]{{-2, +2}, {0, +2}, {+2, +2}, {+2, 0}, {+2, -2}, {0, -2}, {-2, -2}, {-2, 0}};
    6.             for (int[] i : add) {
    7.                 final Coordinate yes = new Coordinate(c.getX() + i[0], c.getY() + i[1], 0);
    8.                 final WebVertex v = getVertexAt(yes);
    9.                 if (v != null) {
    10.                     cv.addBidirectionalEdge(v);
    11.                 }
    12.             }
    13.         }
    14.     }

    I'm adding/removing 2 from the coordinate's x/y because my web additions have one tile in between each other, if you use more space in between, the code should be fairly easy to adjust.

    I'm using a method called "getVertexAt" which simply returns the existing vertex at a position if it's already in the web.

    Code (Text):
    1.     WebVertex getVertexAt(Coordinate coordinate) {
    2.         for (WebVertex v : web.getRegularVertices()) {
    3.             if (v.getPosition().equals(coordinate)) {
    4.                 return v;
    5.             }
    6.         }
    7.         return null;
    8.     }

    And that's basically my way of adding new areas to the default web. If you have any questions or advice, feel free to comment or contact me in another way. Thanks for reading. :)

    #1 Defeat3d, Nov 9, 2014
    Last edited: Nov 11, 2014
    Xhyalus likes this.
  2. It's a decent idea for implementing it, I pushed your tool onto the bot store. You call isReachable far too often, you should do a single call to getReachable and then just check if the tile is within the list. Calling isReachable should only be done when you're only checking a couple tiles, because it takes just as long to call that as it does to call getReachable.
    Defeat3d likes this.
  3. Thanks, I'll edit my stuff if I find time for it tomorrow.

    I just noticed I completely forgot about coordinate planes. Fix soon(TM).
  4. Updated the tool and thread.
  5. The tool now also creates a .web file which is much more compact and way fast to load. I use this method to merge the .web file with the default web:

    Code (Text):
    1.     public void merge(final File file) {
    2.         try {
    3.             final byte[] data = Files.readAllBytes(file.toPath());
    4.             final FileWeb web = FileWeb.fromByteArray(data);
    5.             this.web.addVertices(web.getVertices());
    6.         } catch (Exception e) {
    7.             e.printStackTrace();
    8.         }
    9.     }
    The tool now also adds the area you generate to previously generated areas (this run) so you can create way bigger areas at once.
  6. NVM, problem solved

Share This Page