I took a quick peek at some of the tile sets again today, and thought I never quite understood exactly how the indexing worked when new tubes or walls are placed.
Here's an image of the wall tiles. (This is an actual tile group pulled from the appendix to the map files).
The directionality for tubes and the other wall types is identical.
So, the problem is, when an earthworker places a tube or wall, how does it use the presence of tubes/walls in adjacent tiles to determine which graphic to display? Turns out the game uses a a double indexing scheme. The first step is to check each of the surrounding tiles (Top, Bottom, Left, Right), and assign a bit according to the presence or absence of tubes/walls on that tile, and use those 4 bits to form a number (0..15). The bit are in the order just given, from least significant, to most significant.
Top = 1
Bottom = 2
Left = 4
Right = 8
That number formed from those 4 bits (0..15) is used to index into a table. The table is as follows.
IndexTranslationTable[] =
{
15, 12, 11, 1, 14, 4, 2, 9, 13, 5, 3, 10, 0, 6, 7, 8
}
That is now the index into the tile group. In the image given, those indexes are in reading order, left to right, top to bottom.
Example:
_T_
_OT
_T_
Suppose we are placing a tube at the "O". Then we check the surrounding tiles, and see there are tubes to the Top, Bottom, and Right. These have values 1, 2, 8, so the 4 bit index has value 11. That is (RLBT) = 1011 = 8+2+1 = 11. (Note, that is Right, Left, Bottom, Top, so that the most significant bit is written on the left). Looking up the value in the table (starting from 0), we get IndexTranslationTable[11] = 10. In the diagram, index 10 coresponds to the tile at (2,2) (again starting from 0), which is the correct image.
Note also that you not only have to update the tile at the center, but also each of the tiles that has a tube to the top, bottom, left, and right. That is, perform this procedure up to another 4 times (although, only an extra 3 times in the example given). That way all the tubes will appear to link correctly.
Note: If they had reordered the tiles in the tile set, there would have been no need for that table lookup. That is, use the order naturally given by the first index.