2D Tiled Randomly Generated

Okay, so i want to make some game where it randomly generates an island, the noise functions and island masks are all good but i have the following problem: if i want to do this with tiles, i don’t have the tiles for some specific positions :confused:

example:

And this is the tiles i have:

I know that I could simply draw the missing tiles, but I would prefer to have the code change the map so that it doesn’t need those tiles.

What i mean is like the following: (Tiles = X and O)

Case 1:

XXX
XOX
XXX
gets changed to
XXX
XXX
XXX

Case 2:

OOXXOO
OOOOOO
OOXXOO
gets changed to
OOXXOO
OOXXOO
OOXXOO

Because these Tiles aren’t supported but I have no idea how this would work for the case in the picture so that i don’t need to draw additional tiles :confused:

Code fixing the other 2 Issues and an attempt to count the ones that are still there:

		boolean done = false;
		int looped = 0;
		while (!done) {
			Start.errors = 0;
			looped++;
			done = true;
			// REDO
			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					// Check null
					if (!exist(x - 1, y) || !exist(x + 1, y)
							|| !exist(x, y - 1) || !exist(x, y + 1)) {
						// Do nothing
					} else {
						int amount = 0;
						type t = null;
						if (tiles[(x - 1) + y * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[(x - 1) + y * w];
						}
						if (tiles[(x + 1) + y * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[(x + 1) + y * w];
						}
						if (tiles[x + (y - 1) * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[x + (y - 1) * w];
						}
						if (tiles[x + (y + 1) * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[x + (y + 1) * w];
						}
						if (amount <= 1) {
							// Change
							tiles[x + y * w] = t;
							done = false;
						}
					}

				}
			}
			// Lines
			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					// Check null
					if (!exist(x - 1, y) || !exist(x + 1, y)
							|| !exist(x, y - 1) || !exist(x, y + 1)) {
						// Do nothing
					} else {
						int amount = 0;
						type t = null;
						if (tiles[(x - 1) + y * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[(x - 1) + y * w];
						}
						if (tiles[(x + 1) + y * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[(x + 1) + y * w];
						}
						if (tiles[x + (y - 1) * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[x + (y - 1) * w];
						}
						if (tiles[x + (y + 1) * w] == tiles[x + y * w]) {
							amount++;
						} else {
							t = tiles[x + (y + 1) * w];
						}
						if (amount == 2) {
							if (tiles[x + (y - 1) * w] == tiles[x + (y + 1) * w]) {
								if (tiles[x + (y - 1) * w] == tiles[x + y * w]) {
									tiles[x + (y - 1) * w] = t;
									tiles[x + (y + 1) * w] = t;
								} else {

								}
								tiles[x + y * w] = t;
								done = false;
							}
						}
					}

				}
			}
			// Edge to edge
			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					// Check null
					if (!exist(x - 1, y) || !exist(x + 1, y)
							|| !exist(x, y - 1) || !exist(x, y + 1)) {
						// Do nothing
					} else {
						int amount = 0;
						if (tiles[(x - 1) + (y - 1) * w] == tiles[x + y * w]) {
							amount++;
						}
						if (tiles[(x + 1) + (y - 1) * w] == tiles[x + y * w]) {
							amount++;
						}
						if (tiles[(x - 1) + (y + 1) * w] == tiles[x + y * w]) {
							amount++;
						}
						if (tiles[(x + 1) + (y + 1) * w] == tiles[x + y * w]) {
							amount++;
						}
						if (amount == 2) {
							if (tiles[(x + 1) + (y + 1) * w] == tiles[(x - 1)
									+ (y - 1) * w]) {
								Start.errors++;
								if (done) {
									tiles[x + y * w] = type.ERROR;
								}
							}
						}
					}

				}
			}

			if (looped >= 10) {
				Start.errors = -1;
				continue;
			}
		}

The way I see it, you have 3 options:

  • Come up with a data structure that maps combinations of bad tiles to tiles that should replace them.
  • Tweak your generator so it doesn’t generate bad tiles in the first place.
  • Create sprites for the bad tiles so there aren’t any bad tiles anymore.

Which option are you trying to do?

It seems like you’re going for option 1. How are you representing the underlying data that tells you which tiles to draw?

Keep in mind that there might be an extremely large number of possible bad combinations, in which case option 2 or even option 3 might end up being less work.

As always, the answer is to break your problem down into smaller steps.

Can you write a function that takes a 2D array (or whatever) that represents a bad combination and returns whether that can be found in the data structure (is it another 2D array?) that represents your whole map?

Can you modify that function to return the position of the bad combination?

Can you write another function that takes a 2D array (or whatever) that represents a GOOD combination, as well as a position, and inserts that combination into the whole map at that position?

Once you have those functions working, then you can combine them in another method that loops through all of the bad combinations and tries to replace them.

I suggest simply just drawing every tile combo you need. It’ll make for a more natural looking map in the long run. An alternative instead of adding tiles could be removing them instead, just have some way to detect the tiles around the tile you’re placing, if it creates an “illegal” situation, skip placing or delete that tile.

One idea; build a byte[3][3] array to check surrounding tiles on every tile of your map, making a 3x3 grid, center tile being the one checked. Then you’ll have an output of something like this:
(1 == matching tile found, 0 == no tile, or mismatched tile)
111
110
100

Then compare that to a set of byte[3][3] arrays that you deem “illegal” tile placement. If it matches any of them, delete the tile. It’s not the fastest way to do it, but it’s a simple solution. Another, slightly more hacky solution would be to use an int string, and make an int like this: 111110100, and compare that to a list of “illegal ints”.

These are all not really the best solutions, but the coding is simple.

I just drew myself the missing tiles so it’s fine now :slight_smile: and improved the way it detects “wrong tiles”