I’ve been staring at the code I wrote for the train game for a few days now. I can’t figure out my own code although I know why things are the way they are.
The test program, which is really just a program to allow me to draw lines with proper railroad-style connectivity, does just that. it’s a drawing program. It also moves a dot along the track network in a random fashion. this allows me to quickly see where things have gone wrong.
I left the program sit for years in a broke state. I’ll explain. The track network consists of waypoints and segments between them. The only reason there are segments maintained between the waypoints is to know the geometric nature of the railroad track. If this were a purely data related problem, waypoints or nodes would be all that is needed. The segments are a geometric data structure called an arcline that can be, as indicated by the name, an arc or a line. The part that gave me trouble was knowing about the start and end of an arcline and which one is located at a waypoint. The waypoints need to know which end of the arcline is connected and also need to keep track of direction. The direction is just a number that describes which way a segment is connected to a waypoint and allows the data to be traversed as if it were railroad track. in other words, when a train gets to a switch from the one track that splits into two, there are two directions it can go but if the train approaches the switch on one of the split legs, it can only go one direction. it can’t take the other track because of the physical impossibility of it.
Keeping track of all of this ten years ago, or whenever it was that i wrote the code, was tricky. I could create the data manually but allowing the track to be drawn caused problems. I needed to figure out from the geometry of a new arcline how it related to the waypoint where it is being drawn from.
Drawing is allowed from an existing waypoint, from a completely new point, or from a point on a segment. In some cases, I just got it wrong when I created new track geometry based on what I’m drawing with the mouse.
I also got the architecture wrong. At least it is unusable as-is if I were to incorporate it into a game. Some of the code related to drawing should be moved from the code that handles mouse messages into the track network code. The use interface and the data structures need to be more separate so that I can change how the new information is generated and still have it work with the network properly. For instance, a computer player would not be moving a mouse but would still need to add track to their network.
If I have an epiphany, I’ll blog it. For now, I’m stuck trying to figure out where I went wrong in my data structures that makes them too complex to understand at first glance.