Subversion Repositories programming

Rev

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

Rev 338 Rev 339
Line 47... Line 47...
47
    // find elevator that is closest, AND NOT moving away
47
    // find elevator that is closest, AND NOT moving away
48
    // "push" it's button
48
    // "push" it's button
49
    elevator_number = find_closest_elevator (on_floor, direction);
49
    elevator_number = find_closest_elevator (on_floor, direction);
50
    elevator.at(elevator_number) -> push_button (on_floor);
50
    elevator.at(elevator_number) -> push_button (on_floor);
51
 
51
 
52
    printf ("elevator[%d] gets queued for floor: %d\n", elevator_number, on_floor);
52
    //printf ("elevator[%d] gets queued for floor: %d\n", elevator_number, on_floor);
53
}
53
}
54
 
54
 
55
/**
55
/**
56
 * Check if there is an elevator already in the queue to stop
56
 * Check if there is an elevator already in the queue to stop
57
 * at the floor given as a parameter.
57
 * at the floor given as a parameter.
Line 90... Line 90...
90
    int answer = -1;
90
    int answer = -1;
91
 
91
 
92
    float cur_floor;
92
    float cur_floor;
93
    int   cur_direction;
93
    int   cur_direction;
94
 
94
 
95
    printf ("finding closest elevator: to_floor=%d -- in_direction=%d\n", to_floor, in_direction);
95
    //printf ("finding closest elevator: to_floor=%d -- in_direction=%d\n", to_floor, in_direction);
96
 
96
 
97
    for (i=0; i<num_elevators; i++)
97
    for (i=0; i<num_elevators; i++)
98
    {
98
    {
99
        cur_floor = elevator.at(i) -> get_current_floor ();
99
        cur_floor = elevator.at(i) -> get_current_floor ();
100
        cur_direction = elevator.at(i) -> get_direction ();
100
        cur_direction = elevator.at(i) -> get_direction ();
101
        temp_distance = fabsf (cur_floor - to_floor);
101
        temp_distance = fabsf (cur_floor - to_floor);
102
 
102
 
103
        printf ("cur_floor=%e -- direc=%d -- t_dist=%e\n", cur_floor, cur_direction, temp_distance);
103
        //printf ("cur_floor=%e -- direc=%d -- t_dist=%e\n", cur_floor, cur_direction, temp_distance);
104
 
104
 
105
        // Automatically discard elevators that are moving in the wrong
105
        // Automatically discard elevators that are moving in the wrong
106
        // direction, but DO consider ones that are idle.
106
        // direction, but DO consider ones that are idle.
107
        if (cur_direction == MOVE_DOWN && in_direction == MOVE_UP)
107
        if (cur_direction == MOVE_DOWN && in_direction == MOVE_UP)
108
        {
108
        {
109
            printf ("type 1: skipped elevator[%d]\n", i);
109
            //printf ("type 1: skipped elevator[%d]\n", i);
110
            continue;
110
            continue;
111
        }
111
        }
112
 
112
 
113
        if (cur_direction == MOVE_UP && in_direction == MOVE_DOWN)
113
        if (cur_direction == MOVE_UP && in_direction == MOVE_DOWN)
114
        {
114
        {
115
            printf ("type 2: skipped elevator[%d]\n", i);
115
            //printf ("type 2: skipped elevator[%d]\n", i);
116
            continue;
116
            continue;
117
        }
117
        }
118
 
118
 
119
        if (cur_floor < to_floor && cur_direction == MOVE_UP)
119
        if (cur_floor < to_floor && cur_direction == MOVE_UP)
120
        {
120
        {
121
            printf ("good cantidate type 1\n");
121
            //printf ("good cantidate type 1\n");
122
            // GOOD CANTIDATE
122
            // GOOD CANTIDATE
123
            if (temp_distance < distance)
123
            if (temp_distance < distance)
124
            {
124
            {
125
                answer = i;
125
                answer = i;
126
                distance = temp_distance;
126
                distance = temp_distance;
127
            }
127
            }
128
        }
128
        }
129
 
129
 
130
        if (cur_floor > to_floor && cur_direction == MOVE_DOWN)
130
        if (cur_floor > to_floor && cur_direction == MOVE_DOWN)
131
        {
131
        {
132
            printf ("good cantidate type 2\n");
132
            //printf ("good cantidate type 2\n");
133
            // GOOD CANTIDATE
133
            // GOOD CANTIDATE
134
            if (temp_distance < distance)
134
            if (temp_distance < distance)
135
            {
135
            {
136
                answer = i;
136
                answer = i;
137
                distance = temp_distance;
137
                distance = temp_distance;
138
            }
138
            }
139
        }
139
        }
140
 
140
 
141
        if (cur_direction == IDLE && temp_distance < distance)
141
        if (cur_direction == IDLE && temp_distance < distance)
142
        {
142
        {
143
            printf ("idle cantidate\n");
143
            //printf ("idle cantidate\n");
144
            answer = i;
144
            answer = i;
145
            distance = temp_distance;
145
            distance = temp_distance;
146
        }
146
        }
147
    }
147
    }
148
 
148