X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=elevator.cpp;h=038bc297cba2dbbe30cacdbc1674eef4e9021b49;hb=f95be7131a6221491745f0a001f45589aa6acca3;hp=62dfb19d1de0519ca3d0c9964e94079815194422;hpb=3c8c1f8575d2379a73942f8d6c10edd0c366bcd4;p=cs356-p1-elevator.git diff --git a/elevator.cpp b/elevator.cpp index 62dfb19..038bc29 100644 --- a/elevator.cpp +++ b/elevator.cpp @@ -78,6 +78,9 @@ void Elevator::stop_at (Stop &stop) { StopList::iterator it; + if (stop.getDirection() == IDLE) + throw bad_direction(); +#if 0 /* If this is an "ALL" Stop, it supercedes all others */ if (stop.getDirection() == ALL) { @@ -85,6 +88,7 @@ void Elevator::stop_at (Stop &stop) stops_.push_back (stop); return; } +#endif /* Check if this stop already exists. If so, just leave */ for (it = stops_.begin(); it != stops_.end(); it++) @@ -126,7 +130,7 @@ void Elevator::transition_move_up () position_ += ELEVATOR_STEP; // TODO: Call into the GUI to update the position - gui_update_position_label (number_, (float)position_); + gui_update_position_label (number_, (float)position_, direction_); std::cout << "Updating the GUI with our position: " << position_ << std::endl; } @@ -136,13 +140,15 @@ void Elevator::transition_move_down () position_ -= ELEVATOR_STEP; // TODO: Call into the GUI to update the position - gui_update_position_label (number_, (float)position_); + gui_update_position_label (number_, (float)position_, direction_); std::cout << "Updating the GUI with our position: " << position_ << std::endl; } void Elevator::transition_move_idle () { direction_ = IDLE; + // TODO: Call into the GUI to update the position + gui_update_position_label (number_, (float)position_, direction_); // do not change position while IDLE } @@ -164,28 +170,36 @@ void Elevator::transition_open_door () ++stops_below; } + std::cout << "Elevator: " << number_ << " stopping in direction=" << direction_ << std::endl; + + /* Always unpress the in-elevator button for this stop, as well + * as clear the stop. This is like letting people off at a floor. */ + gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, ALL)); + + /* If we are going to switch direction, clear all stops here, * regardless of direction. * * Otherwise, just clear this stop */ if (direction_ == UP && stops_above == 0) { - stops_.remove (Stop(position_, ALL)); - gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, UP)); + stops_.remove (Stop(position_, DOWN)); gui_unpress_call_button ((int)position_, UP); gui_unpress_call_button ((int)position_, DOWN); } else if (direction_ == DOWN && stops_below == 0) { - stops_.remove (Stop(position_, ALL)); - gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, UP)); + stops_.remove (Stop(position_, DOWN)); gui_unpress_call_button ((int)position_, UP); gui_unpress_call_button ((int)position_, DOWN); } else if (direction_ == IDLE) { - stops_.remove (Stop(position_, ALL)); - gui_unpress_request_button (number_, (int)position_); + stops_.remove (Stop(position_, UP)); + stops_.remove (Stop(position_, DOWN)); gui_unpress_call_button ((int)position_, UP); gui_unpress_call_button ((int)position_, DOWN); } @@ -193,7 +207,6 @@ void Elevator::transition_open_door () { stops_.remove (Stop(position_, direction_)); gui_unpress_call_button ((int)position_, direction_); - gui_unpress_request_button (number_, (int)position_); } // TODO: Call into the GUI to open the door @@ -517,7 +530,27 @@ void Elevator::move () bool Elevator::is_idle () const { + if (stops_.size() != 0) + return false; + return direction_ == IDLE; } +int Elevator::getLoad () const +{ + return stops_.size (); +} + +bool Elevator::willStopAt (int floor, Direction direction) const +{ + Stop s (floor, direction); + StopList::const_iterator it; + + for (it=stops_.begin(); it != stops_.end(); it++) + if (*it == s) + return true; + + return false; +} + /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */