/// <summary> /// Try to find the index of the vector node connecting this path node to the (given) nextNode. /// </summary> /// <returns>The index of the vector node connection, or -1</returns> public override int FindTvnIndex(TrainpathNode nextNode) { TrainpathVectorNode nextAsVectorNode = nextNode as TrainpathVectorNode; if (nextAsVectorNode != null) { // from junction to vector node. if (this.ConnectsToTrack(nextAsVectorNode.TvnIndex)) { return(nextAsVectorNode.TvnIndex); } else { //node is perhaps not broken, but connecting track is return(-1); } } //both this node and the next node are junctions: find the vector node connecting them. //Probably this can be faster, by just finding the TrPins from this and next junction and find the common one. int nextJunctionIndex = (nextNode as TrainpathJunctionNode).JunctionIndex; for (int i = 0; i < TrackDB.TrackNodes.Count(); i++) { TrackNode tn = TrackDB.TrackNodes[i]; if (tn == null || tn.TrVectorNode == null) { continue; } if ((tn.JunctionIndexAtStart() == this.JunctionIndex && tn.JunctionIndexAtEnd() == nextJunctionIndex) || (tn.JunctionIndexAtEnd() == this.JunctionIndex && tn.JunctionIndexAtStart() == nextJunctionIndex)) { return(i); } } return(-1); }
/// <summary> /// Determine where exactly the current trainpath node is on the track node /// </summary> /// <param name="startNode">The start node</param> /// <param name="nextNode">The next node (so also the direction can be understood)</param> /// <param name="tn">The tracknode connecting the startNode and nextNode</param> /// <param name="isForward">Output: whether going from startNode to nextNode is in the forward direction of the track</param> /// <param name="tvsiStart">Output: the track vector section index of where the startNode is</param> /// <param name="sectionOffsetStart">Output: the offset in the section (in the direction of the tracknode, not necessarily in the direction from startNode to nextNode)</param> private void DetermineSectionDetails(TrainpathNode startNode, TrainpathNode nextNode, TrackNode tn, out bool isForward, out int tvsiStart, out float sectionOffsetStart) { TrainpathVectorNode currentNodeAsVector = startNode as TrainpathVectorNode; TrainpathJunctionNode currentNodeAsJunction = startNode as TrainpathJunctionNode; if (currentNodeAsJunction != null) { // we start at a junction node isForward = (currentNodeAsJunction.JunctionIndex == tn.JunctionIndexAtStart()); if (isForward) { tvsiStart = 0; sectionOffsetStart = 0; } else { tvsiStart = tn.TrVectorNode.TrVectorSections.Count() - 1; sectionOffsetStart = SectionLengthAlongTrack(tn, tvsiStart); } } else { // we start at a vector node isForward = currentNodeAsVector.IsEarlierOnTrackThan(nextNode); tvsiStart = currentNodeAsVector.TrackVectorSectionIndex; sectionOffsetStart = currentNodeAsVector.TrackSectionOffset; } }
/// <summary> /// From the current pathnode and the linking tracknode, fin the junctionIndex of the next junction (or possibly end-point) /// </summary> /// <param name="linkingTrackNodeIndex">The index of the tracknode leaving the node</param> /// <returns>The index of the junction index at the end of the track (as seen from the node)</returns> public override int GetNextJunctionIndex(int linkingTrackNodeIndex) { TrackNode linkingTrackNode = TrackDB.TrackNodes[linkingTrackNodeIndex]; return(ForwardOriented ? linkingTrackNode.JunctionIndexAtEnd() : linkingTrackNode.JunctionIndexAtStart()); }
/// <summary> /// From the current pathnode and the linking tracknode, find the junctionIndex of the previous junction (or possibly end-point) /// </summary> /// <param name="linkingTrackNodeIndex">The index of the tracknode leaving the node</param> /// <returns>The index of the junction index at the beginning of the track (as seen from the node)</returns> public int GetPrevJunctionIndex(int linkingTrackNodeIndex) { TrackNode linkingTrackNode = TrackDB.TrackNodes[linkingTrackNodeIndex]; bool towardsNodeIsForwardOriented = (this.NodeType == TrainpathNodeType.Reverse) ? !ForwardOriented : ForwardOriented; return(towardsNodeIsForwardOriented ? linkingTrackNode.JunctionIndexAtStart() : linkingTrackNode.JunctionIndexAtEnd()); }
/// <summary> /// Get the index of the junction node at the other side of the linking track vector node. /// This uses only the track database, no trainpath nodes. /// </summary> /// <param name="junctionIndex">Index of this junction node</param> /// <param name="linkingTrackNodeIndex">index of the vector node linking the two junction nodes</param> /// <returns>The index of the junction node at the other end, or 0 in case of trouble</returns> public static int GetNextJunctionIndex(int junctionIndex, int linkingTrackNodeIndex) { if (linkingTrackNodeIndex <= 0) { return(0); // link is not well-defined } TrackNode linkingTrackNode = trackNodes[linkingTrackNodeIndex]; if (linkingTrackNode == null) { return(0); } if (junctionIndex == linkingTrackNode.JunctionIndexAtStart()) { return(linkingTrackNode.JunctionIndexAtEnd()); } else { return(linkingTrackNode.JunctionIndexAtStart()); } }
/// <summary> /// Place a traveller at the junction node location, but on a track leaving it. /// </summary> /// <param name="linkingTvnIndex">The index of the track leaving it</param> /// <returns>The traveller, with direction leaving this node.</returns> public Traveller PlaceTravellerAfterJunction(int linkingTvnIndex) { // it is a junction. Place a traveller onto the tracknode and find the orientation from it. try { //for broken paths the tracknode doesn't exit or the traveller cannot be placed. TrackNode linkingTN = TrackDB.TrackNodes[linkingTvnIndex]; Traveller traveller = new Traveller(TsectionDat, TrackDB.TrackNodes, linkingTN, Location.TileX, Location.TileZ, Location.Location.X, Location.Location.Z, Traveller.TravellerDirection.Forward); if (linkingTN.JunctionIndexAtStart() != this.JunctionIndex) { // the tracknode is oriented in the other direction. traveller.ReverseDirection(); } return(traveller); } catch { return(null); } }