MZ Swapping autotiles on script.

I have a simple script which is getting the layer 1 tile ID via a variable, this is being set by a Get Location Info within eventing.

I'm then overriding several tiles with this tileID so that it has the same lower tile as stored in the variable. The code for doing that is:
JavaScript:
// Looping through the map
for(let x=0, w=$dataMap.width; x < w; x++){
    for(let y=0, h=$dataMap.height; y < h; y++){
        // Check the Variable is one we want to swap
        var tileRegion = $gameMap.regionId(x,y);
        if( tileRegion == 1 ){
           // Swap the current map tile with our stored variable.
           $dataMap.data[(1 * h+y) * w+x] = $gameVariables.value(8);
       }
    }
}

This works absouletely fine, until it meets an autotile such as carpet or path at which point it paints in the tile as it was in the variable, which makes sense.

Screenshot 2021-04-02 at 20.18.14.png

Working fine with grass
Screenshot 2021-04-02 at 20.18.35.png

Less so when on the path

What I want to do is to work out how to get the non border version of the autotile and use that instead. Looking at the Tilemap class I can see what I think I need to call in the _drawAutotile() function but without knowing which bitmap to grab I can't see how to use it.

so a TLDR; Trying to work out when I have a tileID alone, from a autotile generated tile, how to get the border free version of that autotile.

Realise this is also in the wrong forum, and probably is a little off learning JS
I'm 80% sure the autotile format hasn't changed in XX years. So this tutorial talking about autotile IDs should still apply.


So you basically need to do some math to convert autotile part ID into the center autotile ID.
So you basically need to do some math to convert autotile part ID into the center autotile ID.
Ah basic maths, the point one can easily fall over.

However I believe I have solved it, I can skip some math with Tilemap.getAutotileKind($gameVariables.value(8)) which is great, my above corner is 17.
Then to get the 00 index it's just
Code:
2048+(48*Tilemap.getAutotileKind($gameVariables.value(8)))
This based on the math from the post @MechPen linked to
Code:
 2048 + (48 * autotile) + index
Can skip the + index as in this case the index is 00.

JavaScript:
var l = 1;
var t = 2048+(48*Tilemap.getAutotileKind($gameVariables.value(8)));
for(let x=0, w=$dataMap.width; x < w; x++){
    for(let y=0, h=$dataMap.height; y < h; y++){
        var tileRegion = $gameMap.regionId(x,y);
        if( tileRegion == 1 ){ $dataMap.data[(l * h+y) * w+x] = t;}
    }
}
Screenshot 2021-04-02 at 22.29.50.png


Getting somewhere! Thank you for the nudge, I had even read that post, previously.

Original thread: https://forums.rpgmakerweb.com/threads/swapping-autotiles-on-script.135084/