//given a train, switch, and a branch, determines if //the train is heading towards the switch public int?trainHeadingTowardsSwitch(Train t, Switch s, int whichBranch) { if (s.switchId == 3) { } if (t.direction == 0 || t.direction == null) { //Console.WriteLine("Error. Train has no direction"); } switch (whichBranch) { case 0: //if train is contained in source branch if (checkWithinRange(t.currBlock, (int)s.sourceBlockId, (int)s.sourceBlockId_end)) { int srcDir = (int)TrackControllerWindow.plc.getSwitchDirection(s, 0); if (srcDir > 0 || srcDir < 0) { return(srcDir); } else if (srcDir == 0) { if (t.direction > 0 && s.sourceBlockId > s.sourceBlockId_end) { return(1); } else if (t.direction < 0 && s.sourceBlockId < s.sourceBlockId_end) { return(1); } else { return(-1); } } } break; case 1: //if train is contained in source branch if (checkWithinRange(t.currBlock, (int)s.targetBlockId1, (int)s.targetBlockId1_end)) { int t1Dir = (int)TrackControllerWindow.plc.getSwitchDirection(s, 1); if (t1Dir > 0 || t1Dir < 0) { return(t1Dir); } else if (t1Dir == 0) { if (t.direction > 0 && s.targetBlockId1 > s.targetBlockId1_end) { return(1); } else if (t.direction < 0 && s.targetBlockId1 < s.targetBlockId1_end) { return(1); } else { return(-1); } } } break; case 2: if (checkWithinRange(t.currBlock, (int)s.targetBlockId2, (int)s.targetBlockId2_end)) { int t2Dir = (int)TrackControllerWindow.plc.getSwitchDirection(s, 2); if (t2Dir > 0 || t2Dir < 0) { return(t2Dir); } else if (t2Dir == 0) { if (t.direction > 0 && s.targetBlockId2 > s.targetBlockId2_end) { return(1); } else if (t.direction < 0 && s.targetBlockId2 < s.targetBlockId2_end) { return(1); } else { return(-1); } } } break; default: break; } return(null); }
public void addNewTrain(Train t) { trains.Add(t); }