/* File: cars.cpp
 *
 * This file contains the functions local to the Car class.
 */

#include "cars.h"
#include "random_numbers.h"

/*
 * random_source() - input a cumulative prob. distribution and a size and
 *          choose the car source at random
 */
void Car::random_source(double *distribution, int size)
{
    double r = get_cont_random(1.0);     // get a random value

    for (int i = 0; i < size; i++)       // and find where it lies in the
       if (r <= distribution[i])         //   distribution
       {
           source = i;
           break;
       }
}

/*
 * random_destination() - input a cumulative prob. distribution and a size and
 *          choose the car destination at random
 */
void Car::random_destination(double *distribution, int size)
{
    double r = get_cont_random(1.0);     // get a random value

    for (int i = 0; i < size; i++)       // and find where it lies in the
       if (r <= distribution[i])         // distribution
       {
           destination = i;
           break;
       }
}

/*
 * random_depart() - choose the departure time at uniformly random 
 *                   between start and end
 */
void Car::random_depart(int start, int end)
{
    departure_time = start + get_discrete_random(end - start + 1);
}

/*
 * set_route() - takes a path of <= size number of intersections and the
 *        path holds the route the car will take to get from its current
 *        location to its destination.   The program allocates
 *        space for the route and copies the path array to route.
 */
void Car::set_route(int size, int *path)
{
    if (route)                        // delete and old route
        delete[] route;

    route = new int[size];            // make room for this route

    for (int i = 0; i < size; i++)    // and copy the data
        route[i] = path[i];
                                      // set the first step of the route
    next_step_of_route = 0;           // to be the next step taken by car
}


