/* main.C
 *
 * The main() part of the TTC program.  This just reads command-line
 * options, sets up the network from an initialization file, and
 * schedules some initial events.
 */


#include "main.h"
#include "setup.h"
#include "events.h"
#include "scheduler.h"
#include "station.h"
#include "outputWindow.h"
#include "options.h"
#include "statistics.h"


float currentTime    = 0;	// global time
float reportInterval = 1;	// report every 5 seconds
float trainAdvanceInterval = 1; // advance trains every 1 second
float stoppingTime = 1000;	// time that simulation stops

bool testing        = false;	// tutor's testing mode
bool textOutput     = false;	// periodic report outputs text
bool outputEvents   = true;     // output each event as it occurs
bool outputGraphics = true;     // update graphics with each report
bool outputPassengerAppearances = false; // output upon each PassengerAppearanceEvent (lots of output!)
bool redrawUponReport = true;	// redraw the screen at each report time
bool showSectionDirections = false; // put arrows on the sections (for debugging)
bool allowAccidents = true;	// allow accidents to occur

char  *configFileName = "network1"; // name of network configuration file

Controller controller;		// train controller
Scheduler  scheduler;		// event scheduler
Statistics statistics;		// statistic gathering

seq<Station*> allStations;	// list of all stations
seq<Platform*> allPlatforms;	// list of all platforms
seq<Section*> allSections;	// list of all track sections
seq<Train*> allTrains;		// list of all trains


/* Execute the next event
 */


void
nextEvent()

{
  if (scheduler.eventsRemain())
    scheduler.handleNextEvent();
}


/* Main program
 */


int
main( int argc, char **argv )

{
  // Read the command-line options

  getOptions( argc, argv );

  // Read the configuration file

  setupNetwork( configFileName );

  // Schedule the initial events

  scheduler.addEvent( new TrainAdvanceEvent( currentTime ) );
  scheduler.addEvent( new ReportEvent( currentTime ) );

  // For each source/destination pair, schedule the first passenger
  // who takes that route

  for (int src=0; src<allStations.size(); src++)
    for (int dst=0; dst<allStations.size(); dst++)
      if (src != dst)
	allStations[src]->scheduleNextAppearance( allStations[dst] );

  // handle the window and call nextEvent() periodically

  runWindow( nextEvent );
}
