public override void Execute(State state, FastPriorityQueue <Event> eventQueue) { System.Diagnostics.Debug.WriteLine($"ClearSwitchLane: {station.name}, {lane}, time: {state.time}"); /* Kind of a critical point here: * - We have a possible departing tram * - We have a possible incoming tram * - The incoming tram can never go to the departing tram platform (not free yet) * This means that if the departing tram is on platform B, the arriving tram cannot enter because the cross will be used. * If the departing tram is on platform A, they can drive past each other, so it's okay to queue the arrival. */ // Clear the switch station._switch.FreeSwitch(lane); // Check if tram is in queue at switch, give priority to departing trams var(departingTram, departingPlatform) = station.GetFirstDepartingTram(); if (departingTram != null) { // Queue the departure SwitchLane departureLane = Switch.ExitLaneFor(departingPlatform); station._switch.UseSwitchLane(departureLane); Event e = new DepartureStartstation(departingTram, station, departingPlatform); eventQueue.Enqueue(e, state.time); } // Get the best free platform Platform arrivalPlatform = station.BestFreePlatform(); if (arrivalPlatform != Platform.None) { // Check if there are trams arriving from the depot, if so prefer those. if (station.depotQueue.Count > 0) { Tram arrivingTram = station.OccupyFromDepotQueue(arrivalPlatform); Event e = new ArrivalEndstation(arrivingTram, station, arrivalPlatform, true); eventQueue.Enqueue(e, state.time); } // Check if we can enqueue an arrival as well else if (station.HasQueue() && station._switch.SwitchLaneFree(Switch.ArrivalLaneFor(arrivalPlatform))) { // Get best available platform && queue Tram arrivingTram = station.OccupyFromQueue(arrivalPlatform); SwitchLane lane = Switch.ArrivalLaneFor(arrivalPlatform); station._switch.UseSwitchLane(lane); // Queue the arrival Event e = new ArrivalEndstation(arrivingTram, station, arrivalPlatform); eventQueue.Enqueue(e, state.time); } } }
public override void Execute(State state, FastPriorityQueue <Event> eventQueue) { System.Diagnostics.Debug.WriteLine($"ExpectedArrivalEndstation: tram {tram.id}, station: {station.name}, time: {state.time}"); // Check if there is a free platform available and if switch lane is free Platform bestPlatform = station.BestFreePlatform(); if (station.depotQueue.Count == 0 && bestPlatform != Platform.None) { station.Occupy(tram, bestPlatform); station._switch.UseSwitchLane(Switch.ArrivalLaneFor(bestPlatform)); eventQueue.Enqueue(new ArrivalEndstation(tram, station, bestPlatform), state.time); } else { station.Enqueue(tram); } }