OpenTTD AIs and Game Scripts • Re: Can somebody teach me tile arithmetic?
I see, what this function does is returns a factor that produces 16 possible directions, this is done by left shift to produce 16 unique numbers, out of which only 12 are needed for rail directions.
No. Tha function creates a 4-bit flags field with 4 possible directions. But it doesn't matter now, let's focus in the original problem. I figured out the "undocumented reason".
The formula you quoted is:
Code:
1067: local dir = (cur_node - par_tile) / parDistance;
cur_node is the tile being evaluated for build, par_tile is its parent in the path. parDistance is the Manhattan distance between. Note that these tiles are always in the same row/column.
parDistance values can be:
- 0 (cur_node is the first tile in the path so it doesn't have a parent)
- 1 (adjacent tiles)
- MapSizeX (crossing the NW or SE borders)
- other (bridge heads, tunnel entrances or straight buildable terrain)
According to line 1066, dir is only calculated if parDistance is grater than one, as this is integer math a value of MapSizeX gives zero, so the function is aborted in line 1068 (it is in the Japanese comment) If the tiles are in the X direction, then dir equals to 1 * sign(cur_node - par_tile), if they are in the Y direction, multiply that by MapSizeX Anyway, in the next line:
Code:
1069: local next_tile = cur_node + dir;
The adjacent tile in the right direction is obtained. That was the purpose of the math and the answer to your original question.
Statistics: Posted by HGus — 15 Feb 2026 12:34
Discussion in the ATmosphere