/* station.C
 */


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

#ifdef GL
  #include <GL/gl.h>
#endif


int Station::nextIdent = 0;


// Receive a passenger, p, into the station.  The passenger is moved
// to the appropriate platform based on the passenger's destination

void
Station::receivePassenger( Passenger *p )

{
  int i;
  Station *next;

  // has the passenger completed the journey?

  if (p->destStation() == this) {
    statistics.reportArrival( p );
    return;			
  }

  // find the next station that the passenger must exit at.  This
  // might be the passenger's destination, or might be a transfer
  // station.
  
  next = nextStation[ p->destStation()->index ];

#if 0
  if (outputEvents)
    cout << currentTime << ": " << *p << " arrives at " << *this
	 << ", platform " << *destPlatforms[ next->index ]
	 << ", heading to " << *p->destStation() << "\n";
#endif

  p->nextStation = next;	// set the passenger's immediate destination

  destPlatforms[ next->index ]->addPassenger( p ); // add to the platform
}


/* Schedule the next appearance of a passenger at this station.  The 
 * station stores a sequence of (destination,meanArrivalTime) pairs:
 * For a particular destination station, the corresponding pair gives
 * the mean arrival time of passengers who want to go to that station.
 *
 * YOU MUST MODIFY THIS FUNCTION TO CORRECTLY GENERATE THE NEXT
 * APPEARANCE.  CURRENTLY, IT DOES THIS INCORRECTLY.
 */


void
Station::scheduleNextAppearance( Station *destStation )

{
  Passenger *passenger;
  float appearanceTime;
  PassengerAppearanceEvent *e;
  int i;

  // Create a passenger

  passenger = new Passenger( this, destStation );

  // Choose the passenger's appearance time at random in [+0,+60]
  // seconds from now.  You should use means[] to determine the mean
  // arrival time

  appearanceTime = currentTime + drand48() * 60;

  // Create and schedule the event

  e = new PassengerAppearanceEvent( appearanceTime, passenger, this, destStation );
  scheduler.addEvent( e );
}


/* Draw the station
 */

void
Station::draw()

{
#ifdef GL
  // don't draw the station!
#endif  
}

