Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Tutorial My way of adding new areas to the default web

Conelander
Joined
Oct 30, 2014
Messages
3,610
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..


Content:
  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.

El9L5rf.png

Code:
    public static List<Coordinate> getSurroundingWeb(final Locatable loc, final int radius, final int divisibleBy) {
        final Coordinate[] reachable = (Coordinate[]) loc.getPosition().getReachableCoordinates().toArray();
        List<Coordinate> web = new ArrayList<Coordinate>();
        for (Coordinate c : reachable) {
            if (c.getX() % divisibleBy == 0 && c.getY() % divisibleBy == 0 && c.distanceTo(loc) <= radius) {
                web.add(c);
            }
        }
        return web;
    }

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:
    public void addCoordinate(Coordinate... coordinates) {
        for (Coordinate c : coordinates) {
            final CoordinateVertex cv = new CoordinateVertex(c);
            web.addVertices(cv);
            final int[][] add = new int[][]{{-2, +2}, {0, +2}, {+2, +2}, {+2, 0}, {+2, -2}, {0, -2}, {-2, -2}, {-2, 0}};
            for (int[] i : add) {
                final Coordinate yes = new Coordinate(c.getX() + i[0], c.getY() + i[1], 0);
                final WebVertex v = getVertexAt(yes);
                if (v != null) {
                    cv.addBidirectionalEdge(v);
                }
            }
        }
    }

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:
    WebVertex getVertexAt(Coordinate coordinate) {
        for (WebVertex v : web.getRegularVertices()) {
            if (v.getPosition().equals(coordinate)) {
                return v;
            }
        }
        return null;
    }

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. :)


-Defeat3d
 
Last edited:
Engineer
Joined
Jul 28, 2013
Messages
2,776
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.
 
Conelander
Joined
Oct 30, 2014
Messages
3,610
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.
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).
 
Conelander
Joined
Oct 30, 2014
Messages
3,610
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:
    public void merge(final File file) {
        try {
            final byte[] data = Files.readAllBytes(file.toPath());
            final FileWeb web = FileWeb.fromByteArray(data);
            this.web.addVertices(web.getVertices());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The tool now also adds the area you generate to previously generated areas (this run) so you can create way bigger areas at once.
 
Top