# Autotile

**URL:** <https://forum.kirupa.com/t/autotile/323252>\
**Category:** Uncategorized\
**Created:** [June 22, 2011, 3:00pm UTC](https://forum.kirupa.com/t/autotile/323252 "2011-06-22T15:00:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalyman](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kalyman](https://forum.kirupa.com/u/kalyman)\
**Post date:** [June 22, 2011, 3:00pm UTC](https://forum.kirupa.com/t/autotile/323252/1 "2011-06-22T15:00:22Z")

</div>

Hello guys,

I wonder if you can help me, am working on game editor for strategy game and issue that I have is how to create autotile for a map. Ya know, 2d array with pieces that if one piece is marked to be grass neighboring pieces around it will adjust so it doesnt seem like square piece \> like this

code so far that I have has pieces, each of them divided to 4 parts that a,b,c and d that marks if piece will be full, half or small corner piece

```auto
public static function wrapPieces(array:Array):void
        {
            // Tile is constructed from 4 parts
            // ___
            // |a|b|
            // |-|-|
            // |c|d|
            // |___|
            
            var i : Number;
            var j : Number;
            var k : Number;
            var text:String="";
            
            // Loop thru all pieces 
            for (i = 0; i < array.length ; i ++ )
            {
                for (j = 0; j < array*.length ; j++)
                {
                    // find the one that is full 
                    if (array*[j].a == 1 && array*[j].b == 1
                        && array*[j].c == 1 && array*[j].d == 1)
                        {
                            // get neighbors of this piece
                            var neighbors:Array = findNeighborsIn2dArray(i, j, array);
                            for (k = 0; k < neighbors.length ; k++)
                            {
                                text += neighbors[k].xPos + ":" + neighbors[k].yPos + "|";
                                // found non wrapped field within neighbors
                                if (neighbors[k].a == 0 && neighbors[k].b == 0 
                                    && neighbors[k].c == 0 && neighbors[k].d == 0)
                                    {
                                        // check which side it is on and use wrapper
                                        if (neighbors[k].xPos == array*[j].xPos - 1 
                                            && neighbors[k].yPos == array*[j].yPos -1 )
                                            {
                                                neighbors[k].d = 1;
                                            }
                                        if (neighbors[k].xPos == array*[j].xPos - 1
                                            && neighbors[k].yPos == array*[j].yPos )
                                            {
                                                neighbors[k].b = 1;
                                                neighbors[k].d = 1;
                                            }
                                        if (neighbors[k].xPos == array*[j].xPos - 1
                                            && neighbors[k].yPos == array*[j].yPos + 1 )
                                            {
                                                neighbors[k].b = 1;
                                            }
                                        
                                        if (neighbors[k].xPos == array*[j].xPos 
                                            && neighbors[k].yPos == array*[j].yPos + 1)
                                            {
                                                neighbors[k].a = 1;
                                                neighbors[k].b = 1;
                                            }
                                            
                                        if (neighbors[k].xPos == array*[j].xPos + 1
                                            && neighbors[k].yPos == array*[j].yPos + 1)
                                            {
                                                neighbors[k].a = 1;
                                            }
                                        if (neighbors[k].xPos == array*[j].xPos + 1
                                            && neighbors[k].yPos == array*[j].yPos)
                                            {
                                                neighbors[k].a = 1;
                                                neighbors[k].c = 1;
                                            }
                                        if (neighbors[k].xPos == array*[j].xPos + 1
                                            && neighbors[k].yPos == array*[j].yPos -1)
                                            {
                                                neighbors[k].c = 1;
                                            }
                                        if (neighbors[k].xPos == array*[j].xPos
                                            && neighbors[k].yPos == array*[j].yPos -1)
                                            {
                                                neighbors[k].c = 1;
                                                neighbors[k].d = 1;
                                            }
                                    }
                                    else
                                    {
                                        // rest of the code :crying:
                                    }
                                
                            }
                        }
                }
            }
        }

```

Is there any easier way of doing that or I have to code each part neighbor of tiles? Any sugestions?

Thanks
