Author Topic: Wall And Tube Indexing  (Read 813 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4829
Wall And Tube Indexing
« on: July 04, 2008, 01:00:29 AM »
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.

Code: [Select]
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.

Code: [Select]
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.
« Last Edit: July 04, 2008, 01:01:41 AM by Hooman »

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4829
Wall And Tube Indexing
« Reply #1 on: July 05, 2008, 02:38:56 AM »
Just a slight update on how the game works with this stuff.

For each of the 4 surrounding tiles, there are an extra 4 tables, which translate the existing tile index into a new tile index that now has a connection towards the center. This is basically a third level index.

Not only that, but there is also another 4 tables for when a tube or wall is removed, and it basically undoes the work of the above 4 tables, by translating the tile index into one without a connection towards the center.

So, in all, 9 tables are used, plus the direction bit indexing scheme to translate the tile graphics.


But, like I said before, this can all be done using only the direction bit indexing scheme (if you reordered the tiles). Even if you consider the "optimization" of not having to scan the four neighbors of each of the four neighbors to determine how they translate, you could have done this by simple blind addition (for building) or subtraction (for removal). That's not quite so easy once you introduce the table lookup part though. Oh well, no big deal I suppose.