mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-24 18:14:19 +00:00
elevators are now working correctly when full
This commit is contained in:
parent
86dabd455a
commit
fe32aba7c8
@ -31,7 +31,7 @@ int get_next_call_Building(THIS(Building), int elevator_floor){
|
||||
int next_target = -1;
|
||||
for(i=0; i<FLOORS; i++){
|
||||
if(waiting_floors[i] > 0){
|
||||
if(abs(elevator_floor - i) < best_diff){
|
||||
if(abs(elevator_floor - i) < best_diff && elevator_floor != i/*beware*/){
|
||||
best_diff = abs(elevator_floor - i);
|
||||
next_target = i;
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ int get_next_passenger_stop_Elevator(THIS(Elevator)){
|
||||
temp_element = this->passengers->get_element(this->passengers, i);
|
||||
temp_passenger = (Passenger*) temp_element->get_data(temp_element);
|
||||
temp_floor = temp_passenger->get_destination(temp_passenger);
|
||||
if(abs(this->floor - temp_floor) < min_diff /*&& temp_floor != this->floor*/){
|
||||
if(abs(this->floor - temp_floor) < min_diff && temp_floor != this->floor/*beware*/){
|
||||
min_diff = abs(this->floor - temp_floor);
|
||||
next_floor = temp_floor;
|
||||
}
|
||||
@ -109,8 +109,8 @@ int can_get_inside_Elevator(THIS(Elevator), int floor){
|
||||
permission = (this->passengers->get_size(this->passengers) < MAX_ELEVATOR_CAPACITY &&
|
||||
/*this->state == WAITING &&*/ this->floor == floor);
|
||||
|
||||
if(this->floor != floor)
|
||||
printf("DEBUG : Cause de la préemption : l'ascenseur %s n'est pas là !\n", this->name);
|
||||
//if(this->floor != floor)
|
||||
//printf("DEBUG : Cause de la préemption : l'ascenseur %s n'est pas là !\n", this->name);
|
||||
pthread_mutex_unlock(&this->mutex_floor);
|
||||
pthread_mutex_unlock(&this->mutex_state);
|
||||
pthread_mutex_unlock(&this->mutex_passengers);
|
||||
@ -130,24 +130,37 @@ void *runnable_Elevator(void * void_this){
|
||||
|
||||
int next_call;
|
||||
int next_passenger_stop;
|
||||
int i;
|
||||
|
||||
AGENT_OPTIONS
|
||||
|
||||
printf("Initialisation de l'ascenseur %s\n", this->name);
|
||||
for (;;){
|
||||
building->signal_elevator_at_floor(data->main_building, this->get_floor(this));
|
||||
usleep(500000);
|
||||
usleep(250000);
|
||||
if(this->target_floor == this->get_floor(this)){
|
||||
next_passenger_stop = this->get_next_passenger_stop(this);
|
||||
next_call = building->get_next_call(building, this->floor);
|
||||
if (next_passenger_stop == this->get_floor(this) || next_call == this->get_floor(this)){
|
||||
printf("Ascenseur %s attends pour déposer ou prendre un passager supplémentaire\n", this->name); //while pour attendre ?
|
||||
}else{
|
||||
if(next_passenger_stop != -1){
|
||||
this->target_floor = next_passenger_stop;
|
||||
}else if(next_call != -1){
|
||||
this->target_floor = next_call;
|
||||
|
||||
pthread_mutex_lock(&this->mutex_passengers);//on débarque les passagers
|
||||
Element* temp_element = this->passengers->head;
|
||||
while(temp_element != NULL){
|
||||
Passenger* temp_passenger = ((Passenger*) temp_element->data);
|
||||
if(temp_passenger->get_destination(temp_passenger) == this->get_floor(this)){
|
||||
building->signal_elevator_at_floor(building, this->get_floor(this));
|
||||
}
|
||||
temp_element = temp_element->next;
|
||||
}
|
||||
pthread_mutex_unlock(&this->mutex_passengers);
|
||||
|
||||
for(i=0;i<building->waiting_floors[this->get_floor(this)];i++){ //on embarque les passagers
|
||||
building->signal_elevator_at_floor(building, this->get_floor(this));
|
||||
}
|
||||
|
||||
next_passenger_stop = this->get_next_passenger_stop(this);
|
||||
next_call = building->get_next_call(building, this->get_floor(this));
|
||||
|
||||
if(next_passenger_stop != -1){
|
||||
this->target_floor = next_passenger_stop;
|
||||
}else if(next_call != -1){
|
||||
this->target_floor = next_call;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user