Subversion Repositories programming

Rev

Rev 331 | Rev 334 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 331 Rev 332
Line 4... Line 4...
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
4
Controller::Controller (int num_floors, int num_elevators) : num_floors (num_floors), num_elevators (num_elevators)
5
{
5
{
6
    assert (num_floors > 2);
6
    assert (num_floors > 2);
7
    assert (num_elevators > 0);
7
    assert (num_elevators > 0);
8
 
8
 
9
    elevators = std::vector<Elevator> (num_elevators, Elevator (num_floors));
9
    elevators.reserve (num_elevators);
10
 
10
 
11
    for (int i=0; i<num_elevators; i++)
11
    for (int i=0; i<num_elevators; i++)
-
 
12
    {
-
 
13
        elevators.push_back (Elevator (num_floors));
12
        elevators[i].set_controller (this);
14
        elevators[i].set_controller (this);
-
 
15
    }
13
}
16
}
14
 
17
 
15
Controller::~Controller ()
18
Controller::~Controller ()
16
{
19
{
17
}
20
}
Line 31... Line 34...
31
    if (floor_already_requested (on_floor))
34
    if (floor_already_requested (on_floor))
32
        return;
35
        return;
33
 
36
 
34
    // find elevator that is closest, AND NOT moving away
37
    // find elevator that is closest, AND NOT moving away
35
    // "push" it's button
38
    // "push" it's button
36
    elevator_number = find_closest_elevator (on_floor);
39
    elevator_number = find_closest_elevator (on_floor, direction);
37
    elevators[elevator_number].push_button (on_floor);
40
    elevators[elevator_number].push_button (on_floor);
-
 
41
 
-
 
42
    printf ("elevator[%d] gets queued for floor: %d\n", elevator_number, on_floor);
38
}
43
}
39
 
44
 
40
/**
45
/**
41
 * Check if there is an elevator already in the queue to stop
46
 * Check if there is an elevator already in the queue to stop
42
 * at the floor given as a parameter.
47
 * at the floor given as a parameter.
Line 58... Line 63...
58
 *
63
 *
59
 * We only want to choose elevators that are close and
64
 * We only want to choose elevators that are close and
60
 * heading in the right direction, or the closest elevator
65
 * heading in the right direction, or the closest elevator
61
 * if it is sitting idle.
66
 * if it is sitting idle.
62
 */
67
 */
63
int Controller::find_closest_elevator (int to_floor)
68
int Controller::find_closest_elevator (int to_floor, int in_direction)
64
{
69
{
65
    int i;
70
    int i;
66
    float distance = INT_MAX;
71
    float distance = INT_MAX;
67
    float temp_distance;
72
    float temp_distance;
68
    int answer;
73
    int answer;
Line 74... Line 79...
74
    {
79
    {
75
        cur_floor = elevators[i].get_current_floor ();
80
        cur_floor = elevators[i].get_current_floor ();
76
        direction = elevators[i].get_direction ();
81
        direction = elevators[i].get_direction ();
77
        temp_distance = fabsf (cur_floor - to_floor);
82
        temp_distance = fabsf (cur_floor - to_floor);
78
 
83
 
-
 
84
        // Automatically discard elevators that are moving in the wrong
-
 
85
        // direction, but DO consider ones that are idle.
-
 
86
        if (direction != IDLE || direction != in_direction)
-
 
87
            continue;
-
 
88
 
79
        // Check that this elevator is closer
89
        // Check that this elevator is closer
80
        if (temp_distance < distance)
90
        if (temp_distance < distance)
81
        {
91
        {
82
            // If we are below the floor we want to go to and we
92
            // If we are below the floor we want to go to and we
83
            // are moving up, then this is an ok choice
93
            // are moving up, then this is an ok choice