5

I've been trying to get tile marching to work like in Terraria, and I got something to work. I don't think it's actually very good way of doing this, so I'm wondering what would be really the main way of doing this.

Here's a picture of what I want to accomplish:

Tiles

As you can see, this really does the thing that I want, but it's so horrible looking and it'd be a pain to extend for different types of tiles.

Here is my code for this. I coded this as a test, so my full game would use tile classes etc.

for x, tile in enumerate(row):
    if tile:
        try:
            air = row[x + 1]
        except Exception:
            pass
        else:
            if not air:
                if not LEVEL[y - 1][x]:
                    tile = 3
                else:
                    tile = 6
        air = row[x - 1]
        if not air:
            if not LEVEL[y - 1][x]:
                if tile == 3:
                    tile = 5
                else:
                    tile = 4
            else:
                if tile == 6:
                    tile = 8
                else:
                    tile = 7
        LEVEL[y][x] = tile
for y, row in enumerate(LEVEL):
    for x, tile in enumerate(row):
        if LEVEL[y - 1][x] in (6, 3, 14):
            try:
            pos = LEVEL[y][x + 1]
        except Exception as e:
            print(e)
        else:
            if pos in (2, 3):
                LEVEL[y][x] = 11
    if LEVEL[y - 1][x] in (7, 4, 15):
        pos = LEVEL[y][x - 1]
        if pos in (2, 4):
            LEVEL[y][x] = 12
    if LEVEL[y - 1][x] in (5, 8):
        if LEVEL[y][x] == 1:
            LEVEL[y][x] = 13
    if LEVEL[y][x] == 6:
        if LEVEL[y][x - 1] in (2, 4):
            LEVEL[y][x] = 14
    if LEVEL[y][x] == 7:
        try:
            tile = LEVEL[y][x + 1]
        except Exception as e:
            print(e)
        if tile in (2, 3):
            LEVEL[y][x] = 15

The basic idea behind this is just to look for some air and dirt, and set the tiles image from those.

Basically then when I'm drawing this I just take the value at a coord and blit the corresponding image to the screen.

So how would I go about making this as readable and easily extendable as possible? Does anyone know how Terraria handles this?

Pip
  • 1,454
  • 1
  • 16
  • 32
  • "March"? Typo in title? I think I understand what you are asking. Sadly I don't have an answer as I have the same problem :P – TheNickmaster21 Oct 13 '13 at 14:12
  • I don't really know what to call this, I'm not a native english speaker and I found something called tile marching that had something to do with this. –  Oct 13 '13 at 14:19
  • You want the terrain to be smoothed? You might be referring to marching cubes (or marching squares in the case of 2D). That's a technique for turning a point cloud into a mesh. Terraria didn't use that. – House Oct 13 '13 at 16:37
  • I just want to add a layer of grass on top of the dirt, and eventually effeciently add a layer on top of other tiles like stone. So basicaly I want to smooth the edges of tiles that are connected to air. –  Oct 14 '13 at 12:03
  • 1
    http://gamedev.stackexchange.com/questions/29524/choose-tile-based-on-adjacent-tiles – House Aug 03 '16 at 18:38
  • @Byte56 Yup, an obvious duplicate. I must have missed that back in the days. –  Aug 03 '16 at 20:28
  • It's alright, I just noticed when looking for a recent duplicate. Just making sure it shows up in the linked questions on the right. – House Aug 03 '16 at 20:30
  • Yeah, that was my duplicate flag. Are you sure this question shouldn't be marked duplicate as well? –  Aug 03 '16 at 20:32
  • I smell the development process of GunHero. – Bradman175 Aug 24 '16 at 06:27
  • Your nose is partially right. I asked this question while working on a different project, but surely what I learned from the awesome answer below helped me implement this terrain pattern to GunHero over two years later. –  Aug 24 '16 at 07:07

1 Answers1

8

If I understand right, your map stores whether something is dirt or air, and the simplest thing would be to have dirt and air tiles. However, to make things look better, you have separate images for air above dirt, dirt above air, dirt left of air, and so on. So you're trying to figure out which image to use, given a tile and its neighbors. Is that right?

If so, take a look at one of these articles:

amitp
  • 6,036
  • 3
  • 27
  • 41