/* controller.h
 *
 * The controller class which controls the movement of trains in the
 * network.  The controller does two things:
 *
 * 1. Receives and acts upon information from a track section about
 * trains entering and leaving the section.  This involves setting the
 * status of neighbouring track sections to avoid collisions and to
 * direct trains as needed.  In a real system, this corresponds to
 * setting the signal lights on the track.
 *
 * 2. Receives and acts upon information from a train about (a) the
 * train's departure from a platform and (b) the train's reversal of
 * direction.
 */


#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "defs.h"

class Train;
class Station;
class Section;
class Platform;


#define RED_SPEED 0		// max speed (m/s) in a track section, based on status
#define YELLOW_SPEED 4.17	// 4.17 m/s = 15 km/h
#define GREEN_SPEED 16.67	// 16.67 m/s = 60 km/h


typedef enum { AHEAD, BEHIND } Direction;


class Controller {

  // Some internal functions.  See the comments in controller.C

  void gotoMainSectionPreceding( Section * &s );
  void gotoMainSectionFollowing( Section * &s );
  void gotoBranchSectionFollowing( Section * &s );
  bool TrackIsUnoccupied( Section *s, float distance, Direction dir );

public:

  // These are called when a train enters or leaves a track section:

  void trainIsEnteringSection( Train *t, Section *s );
  void trainIsLeavingSection( Train *t, Section *s );

  // These are called when a train leaves a platform or reverses

  void trainIsWaitingOnPlatform( Train *t, Platform *platform, int numPassengers );
  void trainIsLeavingPlatform( Train *t, Station *nextStation );
  void trainIsReversing( Train *t );

  // Attempt to set the signals so that train t can switch onto the
  // next branch section ahead of it.

  bool switchToBranch( Train *t );
};

#endif
