Which tile ID refers to which tile file is defined in the track file. Also, some environmental values such as the light sources and the background pictures are set in the track.
For example: look at the picture below. The tile on the left is a straight road. On the left and right boundary it has the flag "normal_road" set, so that only tiles with a normal road may be left and right neighbours. It also has the flag "horiz_grass" set, so that tiles with diagonal ground at the boundary or with no ground at all may not me next to it. The second tile has these flags on its left side, so this is a valid situation. On its right it has not set the "normal_road" flag, but it has set the "horiz_grass" and the "solid_bridge" flags. This ensures that the third tile on the base level is also of that type.
So far so good. The problem arises with the continuation of the road. Of course
we want to validate that the road continues, but we do not yet have a way to
check that, because the third tile on the top level does not have a boundary
with the second tile on the base level. The solution is to let the second tile
on the ground level have an additional restriction on the left boundary of the
third tile on the top level, that it needs to have the "normal_road" flag set.
So, a generalisation is necessary. Every tile will have the possibility to put restrictions to the flags of any boundary, relative to its own position and orientation. For example, the second tile on the base level will have these restrictions:
(0,0)left: horiz_grass and normal_road (0,0)top: air (0,0)right: horiz_gras and solid_bridge (0,1)right: normal_road
Of course these boundary conditions cannot always be met. A way to solve this is to only present the user the set of tiles that will not give problems when adding them at the cursor location. So the editor needs to simulate the recursive changes before the user chooses a tile, for every tile.
To try it out, I've made the following "before" and "after" pictures:
The blue rectangle is the cursor. The user decides to place a straight viaduct
road. The straight viaduct road has the requirement that below it, there needs
to be a solid bridge tile, and the present straight road tile does not meet
those requirements. That tile needs to be replaced, with the boundary conditions
that the road must not be changed, and that the ground must remain the same.
One tile that fits the boundary conditions and the solid bridge requirement is
the viaduct tile, so that tile is placed. It also fits the left boundary nicely.
Now you can also see why my pictures are wrong. After placing the viaduct tile, the boundary at the right of the viaduct tile is invalid, so in reality that tile should also be changed. One way to do this would be to place a road that goes downward. Of course, to make it completely valid, at the end of that road, another road tile should be placed, and after that another one, etc. etc.. This could grow to infinity, unless we have a "dead end" tile in our collection.
This recursive fixing of the track will be a very interesting, and complicated, programming excercise. It will be like finding the shortest path through a labyrinth. If there is no way to "exit the labyrinth", then the possibility to add the tile should not be presented to the user. If only one solution is possible, then that one should be added automatically, and the cursor should be moved automatically.