I can try to help, i programmed a code to create autotiled sprites to show where a character can move in a turn based combat (like in xcom), which is basically the same logic (i don't know what you mean by tile ID though).
here a chunk of the code that creaters each of the autotile's tile based on a grid representing whre the character can move (basically a grid of bool, the same thing as having coordinates of where water tiles are or are not)
if (tile.col_top) {
if (tile.col_left) tile0 = autotile.cornerTile(0); // corner 0
else tile0 = autotile.sideTile(0); // top side 0
if (tile.col_right) tile1 = autotile.cornerTile(1); // corner 1
else tile1 = autotile.sideTile(0); // top side 1
} else {
if (tile.col_left) tile0 = autotile.sideTile(3); // left side 0
else if (tile_up.col_left || tile_left.col_top) tile0 = autotile.elbowTile(0); // elbow 0
else tile0 = autotile.centerTile(); // center 0
if (tile.col_right) tile1 = autotile.sideTile(1); // right side 1
else if (tile_up.col_right || tile_right.col_top) tile1 = autotile.elbowTile(1); // elbow 1
else tile1 = autotile.centerTile(); // center 1
}
if (tile.col_bottom) {
if (tile.col_right) tile2 = autotile.cornerTile(2); // corner 2
else tile2 = autotile.sideTile(2); // bottom side 2
if (tile.col_left) tile3 = autotile.cornerTile(3); // corner 3
else tile3 = autotile.sideTile(2); // bottom side 3
} else {
if (tile.col_right) tile2 = autotile.sideTile(1); // right side 2
else if (tile_down.col_right || tile_right.col_bottom) tile2 = autotile.elbowTile(2); // elbow 2
else tile2 = autotile.centerTile(); // center 2
if (tile.col_left) tile3 = autotile.sideTile(3); // left side 3
else if (tile_down.col_left || tile_left.col_bottom) tile3 = autotile.elbowTile(3); // elbow 3
else tile3 = autotile.centerTile(); // center 3
}
this is the code that for each tile cheks if the surrounding tiles are either or not true and create the tile corrispondently. In this case the code doesnt' pick 1 of 48 tiles but gives to each corner of the tile one of the five possible graphics (corner, elbow, orizontal side, vertical side, filled)
This code is tested so i think is a nice way to start, i'll try now to convert it in pseudocode for you to be more clear
tile = tile being chekced;
if (tile_top != tile) { //checks if the tile above the tile checked is of the same kind, from now on != will be implied to improve clarity
if (!tile_left) tile.topleft_corner = corner;
else tile.topleft_corner = orizontal side;
if (!tile_right) tile.topright_corner = corner;
else tile.topright_corner = orizontal side;
} else {
if (!tile_left) tile.topleft_corner = vertical side;
else if (!tile_topleft) tile.topleft_corner = elbow;
else tile.topleft_corner = filled;
if (!tile_right) tile.topright_corner = vertical side;
else if (!tile_topright) tile.topright_corner = elbow;
else tile.topright_corner = filled;
}
if (!tile_bottom) {
if (!tile_right) tile.bottomright_corner = corner;
else tile.bottomright_corner = orizontal side;
if (!tile_left) tile.bottomleft_corner = corner;
else tile.bottomleft_corner = orizontal side;
} else {
if (!tile_right) tile.bottomleft_corner = verical side;
else if (!tile_bottomright) tile.bottomleft_corner = elbow;
else tile.bottomleft_corner = filled;
if (!tile_left) tile.bottomright_corner = vertical side;
else if (!tile_bottomleft) tile.bottomright_corner = elbow;
else tile.bottomright_corner = filled;
}
With this code you obtain for each of the 4 tile corners the tipe o piece u want to put in.
I'll write now how to get from here to returning 1 specific tile from the 48 matrix.
Ill write it as an edit of this post
EDIT
Ok now, i don't know if you need to take exactely 1 of 48 graphics for every tile, as far as i know you can just use the four corner pieces when creating the map grapchis, which is the thing i did in my code (so basically i just combined every tipe of corner pieces into the final graphic).
Still, transitioning from this method to the one you are looking for is a short step.
So... i think the best solution for that is storing the 48 possible tiles in a 4 dimetional array.
Every dimension of this array correspond to a corner, so to retrieve a specific tile u do a thing like
Autotile_4D_Array[topleft_corner_typeID][topright_corner_typeID][bottomleft_corner_typeID][bottomright_corner_typeID]
In this case the dimensions are:
0 = top left corner
1 = top right corner
2 = bottom left corner
3 = bottom right corner
Now let's associate every corner tipe with a number like:
var AUTOT_CORNER = 0;
var AUTOT_ELBOW = 1;
var AUTOT_ORSIDE = 2;
var AUTOT_VERSIDE = 3;
var AUTOT_FILLED = 4;
So the final code will be like
tile = tile being chekced;
var tile_corners = []; //this variable should exists only in the scope of the funciton
// the followings are constants/enumerators and instead should be declared once outside the function to improve code speed
var AUTOT_CORNER = 0;
var AUTOT_ELBOW = 1;
var AUTOT_ORSIDE = 2;
var AUTOT_VERSIDE = 3;
var AUTOT_FILLED = 4;
var CORN_TOPLEFT = 0;
var CORN_TOPRIGHT = 1;
var CORN_BOTTOMRIGHT = 2;
var CORN_BOTTOMLEFT = 3;
if (tile_top != tile) { //checks if the tile above the tile checked is of the same kind, from now on != will be implied to improve clarity
if (!tile_left) tile_corners[CORN_TOPLEFT] = AUTOT_CORNER;
else tile_corners[CORN_TOPLEFT] = AUTOT_ORSIDE;
if (!tile_right) tile_corners[CORN_TOPRIGHT] = AUTOT_CORNER;
else tile_corners[CORN_TOPRIGHT] = AUTOT_ORSIDE;
} else {
if (!tile_left) tile_corners[CORN_TOPLEFT] = AUTOT_VERSIDE;
else if (!tile_topleft) tile_corners[CORN_TOPLEFT] = AUTOT_ELBOW;
else tile_corners[CORN_TOPLEFT] = AUTOT_FILLED;
if (!tile_right) tile_corners[CORN_TOPRIGHT] = AUTOT_VERSIDE;
else if (!tile_topright) tile_corners[CORN_TOPRIGHT] = AUTOT_ELBOW;
else tile_corners[CORN_TOPRIGHT] = AUTOT_FILLED;
}
if (!tile_bottom) {
if (!tile_right) tile_corners[CORN_BOTTOMRIGHT] = AUTOT_CORNER;
else tile_corners[CORN_BOTTOMRIGHT] = AUTOT_ORSIDE;
if (!tile_left) tile_corners[CORN_BOTTOMLEFT] = AUTOT_CORNER;
else tile_corners[CORN_BOTTOMLEFT] = AUTOT_ORSIDE;
} else {
if (!tile_right) tile_corners[CORN_BOTTOMLEFT] = AUTOT_VERSIDE;
else if (!tile_bottomright) tile_corners[CORN_BOTTOMLEFT] = AUTOT_ELBOW;
else tile_corners[CORN_BOTTOMLEFT] = AUTOT_FILLED;
if (!tile_left) tile_corners[CORN_BOTTOMRIGHT] = AUTOT_VERSIDE;
else if (!tile_bottomleft) tile_corners[CORN_BOTTOMRIGHT] = AUTOT_ELBOW;
else tile_corners[CORN_BOTTOMRIGHT] = AUTOT_FILLED;
}
return Autotile_graphics[tile_corners[0]][tile_corners[1]][tile_corners[2]][tile_corners[3]];
Now, to store each of the 48 graphics in the 4D array you just have to kinda reverse engeneer this code is not that complicated. Anyway i think i a waste cause basically you ahve to combine the 4 corner in every combination and store the results when you can just create the final graphic by combining the 4 cornrers, the only difference is that it should be slightly slower to run, but since this thing is processed once for map load i think that it doesn't matter.
I hope this has been usefull 🙂
Thanks for that - it's not quite what I'm trying to do.
What I'm doing is painting autotiles on-the-fly. I am editing the map data, selecting some tiles that are not autotiles, and changing them to autotiles. These are likely to occur in groups. If I was drawing it in the editor, the editor would do the task of determining which Tile ID to use (there are 48 in each group - 48 possible combinations that can be made from a single autotile image), based on what tiles around it are the same type. I need to figure out how the editor does that, as I need to do the same thing at runtime.
I dont' knwo if you can do this thing at run time, the base code does it once on map load as far as i know.
When i did something like this to show where a player can move i tried to run it every cicle and it's heavy of the fps, not for the code logic itslef, but for the composition of final image. For what i've seen MV doesnt' use GPU to process graphics but relies on the CPU instead, this means that every graphic related thing affects ehavily the runnign speed of the game.
Probably using 48 big tiles instead of combinig every frame 4 corners is faster (thing you can do by using my code and adding the code that stores the 48 graphics in a 4d array, still the code can be greately improved, but is only the logic part which basically means nothing compared to the graphic computing time)
but i suggest you to test if at the end will be too slow. Try giving a random graphic from the 48 to each tile every frame and see if the fps drop
I've got a pretty good idea of what to do 😉 Just after the least intensive way to determine which tile ID to use.
I think I've come up with something, so going to go test for a bit 🙂 It's kind of along the same lines as what you've got, but using bits to turn a tile on/off.