/* events.C
 */


#include <math.h>
#include <stdlib.h>
#include "events.h"
#include "train.h"
#include "station.h"
#include "scheduler.h"
#include "statistics.h"
#include "main.h"
#include "outputWindow.h"
#include "passenger.h"
#include "platform.h"


#define MEAN_ACCIDENT_DURATION 200.0
#define TIME_BETWEEN_ACCIDENTS 80.0


/* When a train departs, its departure function is called.
 * The train will tell the controller of its next arrival
 */

void
TrainDepartureEvent::handleEvent()

{
  train->departFromPlatform();
}


/* All trains advance in fixed intervals.  The TrainAdvanceEvent
 * causes each train to advance, and then schedules the next
 * TrainAdvanceEvent.
 */

void
TrainAdvanceEvent::handleEvent()

{
  TrainAdvanceEvent *e;

  for (int i=0; i<allTrains.size(); i++)
    allTrains[i]->advance();

  e = new TrainAdvanceEvent( currentTime + trainAdvanceInterval );
  scheduler.addEvent( e );
}


/* When a passenger appears at a station, the station receives the
 * passenger and the controller schedules the next appearance event.
 */

void
PassengerAppearanceEvent::handleEvent()

{
  if (outputEvents && outputPassengerAppearances)
    cout << currentTime << ": " << *passenger << "(" << *destinationStation
	 << ") appears at " << *sourceStation << ".\n";

  sourceStation->receivePassenger( passenger );
  sourceStation->scheduleNextAppearance( destinationStation );
}


/* When a report is made, text is printed in the terminal window and
 * the screen picture is updated.
 */

void
ReportEvent::handleEvent()

{
  ReportEvent *event;

  if (redrawUponReport)
    window->redraw();

  if (!testing)
    statistics.periodicReport();

  event = new ReportEvent( currentTime + reportInterval );
  scheduler.addEvent( event );
}
